stm32g4xx_hal_rtc_ex.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) Extended peripheral:
  8. * + RTC Time Stamp functions
  9. * + RTC Tamper functions
  10. * + RTC Wake-up functions
  11. * + Extended Control functions
  12. * + Extended RTC features functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2019 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. (+) Enable the RTC domain access.
  31. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  32. format using the HAL_RTC_Init() function.
  33. *** RTC Wakeup configuration ***
  34. ================================
  35. [..]
  36. (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
  37. function. You can also configure the RTC Wakeup timer with interrupt mode
  38. using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  39. (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  40. function.
  41. *** Outputs configuration ***
  42. =============================
  43. [..] The RTC has 2 different outputs:
  44. (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
  45. and WaKeUp signals.
  46. To output the selected RTC signal, use the HAL_RTC_Init() function.
  47. (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
  48. To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
  49. (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB2) managed on
  50. the RTC_OR register.
  51. (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
  52. automatically configured in output alternate function.
  53. *** Smooth digital Calibration configuration ***
  54. ================================================
  55. [..]
  56. (+) Configure the RTC Original Digital Calibration Value and the corresponding
  57. calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
  58. function.
  59. *** TimeStamp configuration ***
  60. ===============================
  61. [..]
  62. (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
  63. You can also configure the RTC TimeStamp with interrupt mode using the
  64. HAL_RTCEx_SetTimeStamp_IT() function.
  65. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  66. function.
  67. *** Internal TimeStamp configuration ***
  68. ===============================
  69. [..]
  70. (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
  71. User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
  72. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  73. function.
  74. *** Tamper configuration ***
  75. ============================
  76. [..]
  77. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  78. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  79. value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
  80. Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
  81. with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
  82. (+) The default configuration of the Tamper erases the backup registers. To avoid
  83. erase, enable the NoErase field on the RTC_TAMPCR register.
  84. (+) If you do not intend to have tamper using RTC clock, you can bypass its initialization
  85. by setting ClockEnable init field to RTC_CLOCK_DISABLE.
  86. (+) Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
  87. setting Interrupt field.
  88. *** Backup Data Registers configuration ***
  89. ===========================================
  90. [..]
  91. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  92. function.
  93. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  94. function.
  95. @endverbatim
  96. ******************************************************************************
  97. */
  98. /* Includes ------------------------------------------------------------------*/
  99. #include "stm32g4xx_hal.h"
  100. /** @addtogroup STM32G4xx_HAL_Driver
  101. * @{
  102. */
  103. /** @addtogroup RTCEx
  104. * @brief RTC Extended HAL module driver
  105. * @{
  106. */
  107. #ifdef HAL_RTC_MODULE_ENABLED
  108. /* Private typedef -----------------------------------------------------------*/
  109. /* Private define ------------------------------------------------------------*/
  110. /* Private macro -------------------------------------------------------------*/
  111. /* Private variables ---------------------------------------------------------*/
  112. /* Private function prototypes -----------------------------------------------*/
  113. /* Exported functions --------------------------------------------------------*/
  114. /** @addtogroup RTCEx_Exported_Functions
  115. * @{
  116. */
  117. /** @addtogroup RTCEx_Exported_Functions_Group1
  118. * @brief RTC TimeStamp and Tamper functions
  119. *
  120. @verbatim
  121. ===============================================================================
  122. ##### RTC TimeStamp and Tamper functions #####
  123. ===============================================================================
  124. [..] This section provides functions allowing to configure TimeStamp feature
  125. @endverbatim
  126. * @{
  127. */
  128. /**
  129. * @brief Set TimeStamp.
  130. * @note This API must be called before enabling the TimeStamp feature.
  131. * @param hrtc RTC handle
  132. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  133. * activated.
  134. * This parameter can be one of the following values:
  135. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  136. * rising edge of the related pin.
  137. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  138. * falling edge of the related pin.
  139. * @param RTC_TimeStampPin specifies the RTC TimeStamp Pin.
  140. * This parameter can be one of the following values:
  141. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  142. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  143. * compatibility, this parameter is required.
  144. * @retval HAL status
  145. */
  146. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  147. {
  148. /* Check the parameters */
  149. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  150. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  151. UNUSED(RTC_TimeStampPin);
  152. /* Process Locked */
  153. __HAL_LOCK(hrtc);
  154. hrtc->State = HAL_RTC_STATE_BUSY;
  155. /* Get the RTC_CR register and clear the bits to be configured */
  156. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_TSEDGE | RTC_CR_TSE));
  157. /* Disable the write protection for RTC registers */
  158. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  159. /* Configure the Time Stamp TSEDGE and Enable bits */
  160. SET_BIT(hrtc->Instance->CR, (uint32_t)TimeStampEdge | RTC_CR_TSE);
  161. /* Enable the write protection for RTC registers */
  162. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  163. /* Change RTC state */
  164. hrtc->State = HAL_RTC_STATE_READY;
  165. /* Process Unlocked */
  166. __HAL_UNLOCK(hrtc);
  167. return HAL_OK;
  168. }
  169. /**
  170. * @brief Set TimeStamp with Interrupt.
  171. * @note This API must be called before enabling the TimeStamp feature.
  172. * @param hrtc RTC handle
  173. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  174. * activated.
  175. * This parameter can be one of the following values:
  176. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  177. * rising edge of the related pin.
  178. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  179. * falling edge of the related pin.
  180. * @param RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
  181. * This parameter can be one of the following values:
  182. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  183. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  184. * compatibility, this parameter is required.
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  188. {
  189. /* Check the parameters */
  190. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  191. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  192. UNUSED(RTC_TimeStampPin);
  193. /* Process Locked */
  194. __HAL_LOCK(hrtc);
  195. hrtc->State = HAL_RTC_STATE_BUSY;
  196. /* Disable the write protection for RTC registers */
  197. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  198. /* Get the RTC_CR register and clear the bits to be configured */
  199. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
  200. /* Configure the Time Stamp TSEDGE before Enable bit to avoid unwanted TSF setting. */
  201. SET_BIT(hrtc->Instance->CR, (uint32_t)TimeStampEdge);
  202. /* clear interrupt flag if any */
  203. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CITSF);
  204. /* Enable IT timestamp */
  205. SET_BIT(hrtc->Instance->CR, (RTC_CR_TSE | RTC_CR_TSIE));
  206. /* Enable the write protection for RTC registers */
  207. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  208. /* RTC timestamp Interrupt Configuration: EXTI configuration (always rising edge)*/
  209. __HAL_RTC_TIMESTAMP_EXTI_RISING_IT();
  210. __HAL_RTC_TIMESTAMP_EXTI_ENABLE_IT();
  211. hrtc->State = HAL_RTC_STATE_READY;
  212. /* Process Unlocked */
  213. __HAL_UNLOCK(hrtc);
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief Deactivate TimeStamp.
  218. * @param hrtc RTC handle
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  222. {
  223. /* Process Locked */
  224. __HAL_LOCK(hrtc);
  225. hrtc->State = HAL_RTC_STATE_BUSY;
  226. /* Disable the write protection for RTC registers */
  227. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  228. /* clear event or interrupt flag */
  229. WRITE_REG(hrtc->Instance->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
  230. /* In case of interrupt mode is used, the interrupt source must disabled */
  231. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_TSEDGE | RTC_CR_TSE | RTC_CR_TSIE));
  232. __HAL_RTC_TIMESTAMP_EXTI_CLEAR_IT();
  233. /* Enable the write protection for RTC registers */
  234. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  235. hrtc->State = HAL_RTC_STATE_READY;
  236. /* Process Unlocked */
  237. __HAL_UNLOCK(hrtc);
  238. return HAL_OK;
  239. }
  240. /**
  241. * @brief Set Internal TimeStamp.
  242. * @note This API must be called before enabling the internal TimeStamp feature.
  243. * @param hrtc RTC handle
  244. * @retval HAL status
  245. */
  246. HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  247. {
  248. /* Process Locked */
  249. __HAL_LOCK(hrtc);
  250. hrtc->State = HAL_RTC_STATE_BUSY;
  251. /* Disable the write protection for RTC registers */
  252. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  253. /* Configure the internal Time Stamp Enable bits */
  254. SET_BIT(hrtc->Instance->CR, RTC_CR_ITSE);
  255. /* Enable the write protection for RTC registers */
  256. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  257. /* Change RTC state */
  258. hrtc->State = HAL_RTC_STATE_READY;
  259. /* Process Unlocked */
  260. __HAL_UNLOCK(hrtc);
  261. return HAL_OK;
  262. }
  263. /**
  264. * @brief Deactivate Internal TimeStamp.
  265. * @param hrtc RTC handle
  266. * @retval HAL status
  267. */
  268. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  269. {
  270. /* Process Locked */
  271. __HAL_LOCK(hrtc);
  272. hrtc->State = HAL_RTC_STATE_BUSY;
  273. /* Disable the write protection for RTC registers */
  274. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  275. /* Configure the internal Time Stamp Enable bits */
  276. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_ITSE);
  277. /* Enable the write protection for RTC registers */
  278. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  279. hrtc->State = HAL_RTC_STATE_READY;
  280. /* Process Unlocked */
  281. __HAL_UNLOCK(hrtc);
  282. return HAL_OK;
  283. }
  284. /**
  285. * @brief Get the RTC TimeStamp value.
  286. * @param hrtc RTC handle
  287. * @param sTimeStamp Pointer to Time structure
  288. * @param sTimeStampDate Pointer to Date structure
  289. * @param Format specifies the format of the entered parameters.
  290. * This parameter can be one of the following values:
  291. * @arg RTC_FORMAT_BIN: Binary data format
  292. * @arg RTC_FORMAT_BCD: BCD data format
  293. * @retval HAL status
  294. */
  295. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp,
  296. RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  297. {
  298. uint32_t tmptime, tmpdate;
  299. /* Check the parameters */
  300. assert_param(IS_RTC_FORMAT(Format));
  301. /* Get the TimeStamp time and date registers values */
  302. tmptime = (uint32_t)READ_BIT(hrtc->Instance->TSTR, RTC_TR_RESERVED_MASK);
  303. tmpdate = (uint32_t)READ_BIT(hrtc->Instance->TSDR, RTC_DR_RESERVED_MASK);
  304. /* Fill the Time structure fields with the read parameters */
  305. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
  306. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  307. sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
  308. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
  309. sTimeStamp->SubSeconds = (uint32_t)READ_BIT(hrtc->Instance->TSSSR, RTC_TSSSR_SS);
  310. /* Fill the Date structure fields with the read parameters */
  311. sTimeStampDate->Year = 0U;
  312. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  313. sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
  314. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
  315. /* Check the input parameters format */
  316. if (Format == RTC_FORMAT_BIN)
  317. {
  318. /* Convert the TimeStamp structure parameters to Binary format */
  319. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  320. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  321. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  322. /* Convert the DateTimeStamp structure parameters to Binary format */
  323. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  324. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  325. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  326. }
  327. /* Clear the TIMESTAMP Flags */
  328. WRITE_REG(hrtc->Instance->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
  329. return HAL_OK;
  330. }
  331. /**
  332. * @brief TimeStamp callback.
  333. * @param hrtc RTC handle
  334. * @retval None
  335. */
  336. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  337. {
  338. /* Prevent unused argument(s) compilation warning */
  339. UNUSED(hrtc);
  340. /* NOTE : This function should not be modified, when the callback is needed,
  341. the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  342. */
  343. }
  344. /**
  345. * @brief Handle TimeStamp interrupt request.
  346. * @param hrtc RTC handle
  347. * @retval None
  348. */
  349. void HAL_RTCEx_TimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  350. {
  351. /* Clear the EXTI Flag for RTC TimeStamp */
  352. __HAL_RTC_TIMESTAMP_EXTI_CLEAR_FLAG();
  353. __IO uint32_t misr = READ_REG(hrtc->Instance->MISR);
  354. /* Get the TimeStamp interrupt source enable */
  355. if ((misr & RTC_MISR_TSMF) != 0U)
  356. {
  357. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  358. /* Call TimeStampEvent registered Callback */
  359. hrtc->TimeStampEventCallback(hrtc);
  360. #else
  361. /* TIMESTAMP callback */
  362. HAL_RTCEx_TimeStampEventCallback(hrtc);
  363. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  364. /* check if TimeStamp is Internal, since ITSE bit is set in the CR */
  365. if ((misr & RTC_MISR_ITSMF) != 0U)
  366. {
  367. /* internal Timestamp interrupt */
  368. /* ITSF flag is set, TSF must be cleared together with ITSF (this will clear timestamp time and date registers) */
  369. WRITE_REG(hrtc->Instance->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
  370. }
  371. else
  372. {
  373. /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
  374. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CTSF);
  375. }
  376. }
  377. /* Change RTC state */
  378. hrtc->State = HAL_RTC_STATE_READY;
  379. }
  380. /**
  381. * @brief Handle TimeStamp polling request.
  382. * @param hrtc RTC handle
  383. * @param Timeout Timeout duration
  384. * @retval HAL status
  385. */
  386. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  387. {
  388. uint32_t tickstart = HAL_GetTick();
  389. while (READ_BIT(hrtc->Instance->SR, RTC_SR_TSF) == 0U)
  390. {
  391. if (READ_BIT(hrtc->Instance->SR, RTC_SR_TSOVF) != 0U)
  392. {
  393. /* Clear the TIMESTAMP OverRun Flag */
  394. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CTSOVF);
  395. /* Change TIMESTAMP state */
  396. hrtc->State = HAL_RTC_STATE_ERROR;
  397. return HAL_ERROR;
  398. }
  399. if (Timeout != HAL_MAX_DELAY)
  400. {
  401. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  402. {
  403. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  404. /* Process Unlocked */
  405. __HAL_UNLOCK(hrtc);
  406. return HAL_TIMEOUT;
  407. }
  408. }
  409. }
  410. /* Change RTC state */
  411. hrtc->State = HAL_RTC_STATE_READY;
  412. return HAL_OK;
  413. }
  414. /**
  415. * @}
  416. */
  417. /** @addtogroup RTCEx_Exported_Functions_Group2
  418. * @brief RTC Wake-up functions
  419. *
  420. @verbatim
  421. ===============================================================================
  422. ##### RTC Wake-up functions #####
  423. ===============================================================================
  424. [..] This section provides functions allowing to configure Wake-up feature
  425. @endverbatim
  426. * @{
  427. */
  428. /**
  429. * @brief Set wake up timer.
  430. * @param hrtc RTC handle
  431. * @param WakeUpCounter Wake up counter
  432. * @param WakeUpClock Wake up clock
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  436. {
  437. uint32_t tickstart;
  438. /* Check the parameters */
  439. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  440. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  441. /* Process Locked */
  442. __HAL_LOCK(hrtc);
  443. hrtc->State = HAL_RTC_STATE_BUSY;
  444. /* Disable the write protection for RTC registers */
  445. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  446. /* Clear WUTE in RTC_CR to disable the wakeup timer */
  447. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  448. /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
  449. counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
  450. calendar initialization mode. */
  451. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
  452. {
  453. tickstart = HAL_GetTick();
  454. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  455. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
  456. {
  457. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  458. {
  459. /* Enable the write protection for RTC registers */
  460. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  461. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  462. /* Process Unlocked */
  463. __HAL_UNLOCK(hrtc);
  464. return HAL_TIMEOUT;
  465. }
  466. }
  467. }
  468. /* Configure the clock source */
  469. MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
  470. /* Configure the Wakeup Timer counter */
  471. WRITE_REG(hrtc->Instance->WUTR, (uint32_t)WakeUpCounter);
  472. /* Enable the Wakeup Timer */
  473. SET_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  474. /* Enable the write protection for RTC registers */
  475. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  476. hrtc->State = HAL_RTC_STATE_READY;
  477. /* Process Unlocked */
  478. __HAL_UNLOCK(hrtc);
  479. return HAL_OK;
  480. }
  481. /**
  482. * @brief Set wake up timer with interrupt.
  483. * @param hrtc RTC handle
  484. * @param WakeUpCounter Wake up counter
  485. * @param WakeUpClock Wake up clock
  486. * @retval HAL status
  487. */
  488. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  489. {
  490. uint32_t tickstart;
  491. /* Check the parameters */
  492. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  493. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  494. /* Process Locked */
  495. __HAL_LOCK(hrtc);
  496. hrtc->State = HAL_RTC_STATE_BUSY;
  497. /* Disable the write protection for RTC registers */
  498. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  499. /* Clear WUTE in RTC_CR to disable the wakeup timer */
  500. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  501. /* Clear flag Wake-Up */
  502. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  503. /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
  504. counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
  505. calendar initialization mode. */
  506. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
  507. {
  508. tickstart = HAL_GetTick();
  509. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  510. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
  511. {
  512. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  513. {
  514. /* Enable the write protection for RTC registers */
  515. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  516. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  517. /* Process Unlocked */
  518. __HAL_UNLOCK(hrtc);
  519. return HAL_TIMEOUT;
  520. }
  521. }
  522. }
  523. /* Configure the Wakeup Timer counter */
  524. WRITE_REG(hrtc->Instance->WUTR, (uint32_t)WakeUpCounter);
  525. /* Configure the clock source */
  526. MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
  527. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  528. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  529. __HAL_RTC_WAKEUPTIMER_EXTI_RISING_IT();
  530. /* Configure the Interrupt in the RTC_CR register and Enable the Wakeup Timer */
  531. SET_BIT(hrtc->Instance->CR, (RTC_CR_WUTIE | RTC_CR_WUTE));
  532. /* Enable the write protection for RTC registers */
  533. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  534. hrtc->State = HAL_RTC_STATE_READY;
  535. /* Process Unlocked */
  536. __HAL_UNLOCK(hrtc);
  537. return HAL_OK;
  538. }
  539. /**
  540. * @brief Deactivate wake up timer counter.
  541. * @param hrtc RTC handle
  542. * @retval HAL status
  543. */
  544. HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  545. {
  546. uint32_t tickstart;
  547. /* Process Locked */
  548. __HAL_LOCK(hrtc);
  549. hrtc->State = HAL_RTC_STATE_BUSY;
  550. /* Disable the write protection for RTC registers */
  551. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  552. /* Disable the Wakeup Timer */
  553. /* In case of interrupt mode is used, the interrupt source must disabled */
  554. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE | RTC_CR_WUTIE);
  555. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_IT();
  556. tickstart = HAL_GetTick();
  557. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  558. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
  559. {
  560. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  561. {
  562. /* Enable the write protection for RTC registers */
  563. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  564. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  565. /* Process Unlocked */
  566. __HAL_UNLOCK(hrtc);
  567. return HAL_TIMEOUT;
  568. }
  569. }
  570. /* Enable the write protection for RTC registers */
  571. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  572. hrtc->State = HAL_RTC_STATE_READY;
  573. /* Process Unlocked */
  574. __HAL_UNLOCK(hrtc);
  575. return HAL_OK;
  576. }
  577. /**
  578. * @brief Get wake up timer counter.
  579. * @param hrtc RTC handle
  580. * @retval Counter value
  581. */
  582. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  583. {
  584. /* Get the counter value */
  585. return (uint32_t)(READ_BIT(hrtc->Instance->WUTR, RTC_WUTR_WUT));
  586. }
  587. /**
  588. * @brief Handle Wake Up Timer interrupt request.
  589. * @param hrtc RTC handle
  590. * @retval None
  591. */
  592. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  593. {
  594. /* Get the pending status of the WAKEUPTIMER Interrupt */
  595. if (READ_BIT(hrtc->Instance->SR, RTC_SR_WUTF) != 0U)
  596. {
  597. /* Clear the WAKEUPTIMER interrupt pending bit */
  598. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CWUTF);
  599. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_IT();
  600. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  601. /* Call WakeUpTimerEvent registered Callback */
  602. hrtc->WakeUpTimerEventCallback(hrtc);
  603. #else
  604. /* WAKEUPTIMER callback */
  605. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  606. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  607. }
  608. /* Change RTC state */
  609. hrtc->State = HAL_RTC_STATE_READY;
  610. }
  611. /**
  612. * @brief Wake Up Timer callback.
  613. * @param hrtc RTC handle
  614. * @retval None
  615. */
  616. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  617. {
  618. /* Prevent unused argument(s) compilation warning */
  619. UNUSED(hrtc);
  620. /* NOTE : This function should not be modified, when the callback is needed,
  621. the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  622. */
  623. }
  624. /**
  625. * @brief Handle Wake Up Timer Polling.
  626. * @param hrtc RTC handle
  627. * @param Timeout Timeout duration
  628. * @retval HAL status
  629. */
  630. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  631. {
  632. uint32_t tickstart = HAL_GetTick();
  633. while (READ_BIT(hrtc->Instance->SR, RTC_SR_WUTF) == 0U)
  634. {
  635. if (Timeout != HAL_MAX_DELAY)
  636. {
  637. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  638. {
  639. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  640. /* Process Unlocked */
  641. __HAL_UNLOCK(hrtc);
  642. return HAL_TIMEOUT;
  643. }
  644. }
  645. }
  646. /* Clear the WAKEUPTIMER Flag */
  647. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CWUTF);
  648. /* Change RTC state */
  649. hrtc->State = HAL_RTC_STATE_READY;
  650. return HAL_OK;
  651. }
  652. /**
  653. * @}
  654. */
  655. /** @addtogroup RTCEx_Exported_Functions_Group3
  656. * @brief Extended Peripheral Control functions
  657. *
  658. @verbatim
  659. ===============================================================================
  660. ##### Extended Peripheral Control functions #####
  661. ===============================================================================
  662. [..]
  663. This subsection provides functions allowing to
  664. (+) Write a data in a specified RTC Backup data register
  665. (+) Read a data in a specified RTC Backup data register
  666. (+) Set the Coarse calibration parameters.
  667. (+) Deactivate the Coarse calibration parameters
  668. (+) Set the Smooth calibration parameters.
  669. (+) Configure the Synchronization Shift Control Settings.
  670. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  671. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  672. (+) Enable the RTC reference clock detection.
  673. (+) Disable the RTC reference clock detection.
  674. (+) Enable the Bypass Shadow feature.
  675. (+) Disable the Bypass Shadow feature.
  676. @endverbatim
  677. * @{
  678. */
  679. /**
  680. * @brief Set the Smooth calibration parameters.
  681. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  682. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  683. * SmoothCalibMinusPulsesValue must be equal to 0.
  684. * @param hrtc RTC handle
  685. * @param SmoothCalibPeriod Select the Smooth Calibration Period.
  686. * This parameter can be can be one of the following values :
  687. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  688. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  689. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  690. * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  691. * This parameter can be one of the following values:
  692. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  693. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  694. * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  695. * This parameter can be one any value from 0 to 0x000001FF.
  696. * @retval HAL status
  697. */
  698. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod,
  699. uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  700. {
  701. uint32_t tickstart;
  702. /* Check the parameters */
  703. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  704. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  705. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  706. /* Process Locked */
  707. __HAL_LOCK(hrtc);
  708. hrtc->State = HAL_RTC_STATE_BUSY;
  709. /* Disable the write protection for RTC registers */
  710. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  711. /* check if a calibration is pending*/
  712. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_RECALPF) != 0U)
  713. {
  714. tickstart = HAL_GetTick();
  715. /* check if a calibration is pending*/
  716. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_RECALPF) != 0U)
  717. {
  718. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  719. {
  720. /* Enable the write protection for RTC registers */
  721. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  722. /* Change RTC state */
  723. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  724. /* Process Unlocked */
  725. __HAL_UNLOCK(hrtc);
  726. return HAL_TIMEOUT;
  727. }
  728. }
  729. }
  730. /* Configure the Smooth calibration settings */
  731. MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM),
  732. (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
  733. /* Enable the write protection for RTC registers */
  734. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  735. /* Change RTC state */
  736. hrtc->State = HAL_RTC_STATE_READY;
  737. /* Process Unlocked */
  738. __HAL_UNLOCK(hrtc);
  739. return HAL_OK;
  740. }
  741. /**
  742. * @brief Configure the Synchronization Shift Control Settings.
  743. * @note When REFCKON is set, firmware must not write to Shift control register.
  744. * @param hrtc RTC handle
  745. * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
  746. * This parameter can be one of the following values:
  747. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  748. * @arg RTC_SHIFTADD1S_RESET: No effect.
  749. * @param ShiftSubFS Select the number of Second Fractions to substitute.
  750. * This parameter can be one any value from 0 to 0x7FFF.
  751. * @retval HAL status
  752. */
  753. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  754. {
  755. uint32_t tickstart;
  756. /* Check the parameters */
  757. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  758. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  759. /* Process Locked */
  760. __HAL_LOCK(hrtc);
  761. hrtc->State = HAL_RTC_STATE_BUSY;
  762. /* Disable the write protection for RTC registers */
  763. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  764. tickstart = HAL_GetTick();
  765. /* Wait until the shift is completed*/
  766. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_SHPF) != 0U)
  767. {
  768. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  769. {
  770. /* Enable the write protection for RTC registers */
  771. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  772. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  773. /* Process Unlocked */
  774. __HAL_UNLOCK(hrtc);
  775. return HAL_TIMEOUT;
  776. }
  777. }
  778. /* Check if the reference clock detection is disabled */
  779. if (READ_BIT(hrtc->Instance->CR, RTC_CR_REFCKON) == 0U)
  780. {
  781. /* Configure the Shift settings */
  782. MODIFY_REG(hrtc->Instance->SHIFTR, RTC_SHIFTR_SUBFS, (uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S));
  783. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  784. if (READ_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD) == 0U)
  785. {
  786. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  787. {
  788. /* Enable the write protection for RTC registers */
  789. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  790. hrtc->State = HAL_RTC_STATE_ERROR;
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hrtc);
  793. return HAL_ERROR;
  794. }
  795. }
  796. }
  797. else
  798. {
  799. /* Enable the write protection for RTC registers */
  800. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  801. /* Change RTC state */
  802. hrtc->State = HAL_RTC_STATE_ERROR;
  803. /* Process Unlocked */
  804. __HAL_UNLOCK(hrtc);
  805. return HAL_ERROR;
  806. }
  807. /* Enable the write protection for RTC registers */
  808. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  809. /* Change RTC state */
  810. hrtc->State = HAL_RTC_STATE_READY;
  811. /* Process Unlocked */
  812. __HAL_UNLOCK(hrtc);
  813. return HAL_OK;
  814. }
  815. /**
  816. * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  817. * @param hrtc RTC handle
  818. * @param CalibOutput Select the Calibration output Selection .
  819. * This parameter can be one of the following values:
  820. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  821. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  822. * @retval HAL status
  823. */
  824. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
  825. {
  826. /* Check the parameters */
  827. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  828. /* Process Locked */
  829. __HAL_LOCK(hrtc);
  830. hrtc->State = HAL_RTC_STATE_BUSY;
  831. /* Disable the write protection for RTC registers */
  832. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  833. /* Configure the RTC_CR register */
  834. MODIFY_REG(hrtc->Instance->CR, RTC_CR_COSEL, (uint32_t)CalibOutput);
  835. /* Enable calibration output */
  836. SET_BIT(hrtc->Instance->CR, RTC_CR_COE);
  837. /* Enable the write protection for RTC registers */
  838. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  839. /* Change RTC state */
  840. hrtc->State = HAL_RTC_STATE_READY;
  841. /* Process Unlocked */
  842. __HAL_UNLOCK(hrtc);
  843. return HAL_OK;
  844. }
  845. /**
  846. * @brief Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  847. * @param hrtc RTC handle
  848. * @retval HAL status
  849. */
  850. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
  851. {
  852. /* Process Locked */
  853. __HAL_LOCK(hrtc);
  854. hrtc->State = HAL_RTC_STATE_BUSY;
  855. /* Disable the write protection for RTC registers */
  856. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  857. /* Disable calibration output */
  858. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_COE);
  859. /* Enable the write protection for RTC registers */
  860. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  861. /* Change RTC state */
  862. hrtc->State = HAL_RTC_STATE_READY;
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(hrtc);
  865. return HAL_OK;
  866. }
  867. /**
  868. * @brief Enable the RTC reference clock detection.
  869. * @param hrtc RTC handle
  870. * @retval HAL status
  871. */
  872. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
  873. {
  874. HAL_StatusTypeDef status;
  875. /* Process Locked */
  876. __HAL_LOCK(hrtc);
  877. hrtc->State = HAL_RTC_STATE_BUSY;
  878. /* Disable the write protection for RTC registers */
  879. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  880. /* Enter Initialization mode */
  881. status = RTC_EnterInitMode(hrtc);
  882. if (status == HAL_OK)
  883. {
  884. /* Enable clockref detection */
  885. SET_BIT(hrtc->Instance->CR, RTC_CR_REFCKON);
  886. /* Exit Initialization mode */
  887. status = RTC_ExitInitMode(hrtc);
  888. }
  889. /* Enable the write protection for RTC registers */
  890. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  891. if (status == HAL_OK)
  892. {
  893. hrtc->State = HAL_RTC_STATE_READY;
  894. }
  895. /* Process Unlocked */
  896. __HAL_UNLOCK(hrtc);
  897. return status;
  898. }
  899. /**
  900. * @brief Disable the RTC reference clock detection.
  901. * @param hrtc RTC handle
  902. * @retval HAL status
  903. */
  904. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
  905. {
  906. HAL_StatusTypeDef status;
  907. /* Process Locked */
  908. __HAL_LOCK(hrtc);
  909. hrtc->State = HAL_RTC_STATE_BUSY;
  910. /* Disable the write protection for RTC registers */
  911. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  912. /* Enter Initialization mode */
  913. status = RTC_EnterInitMode(hrtc);
  914. if (status == HAL_OK)
  915. {
  916. /* Disable clockref detection */
  917. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_REFCKON);
  918. /* Exit Initialization mode */
  919. status = RTC_ExitInitMode(hrtc);
  920. }
  921. /* Enable the write protection for RTC registers */
  922. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  923. if (status == HAL_OK)
  924. {
  925. hrtc->State = HAL_RTC_STATE_READY;
  926. }
  927. /* Process Unlocked */
  928. __HAL_UNLOCK(hrtc);
  929. return status;
  930. }
  931. /**
  932. * @brief Enable the Bypass Shadow feature.
  933. * @note When the Bypass Shadow is enabled the calendar value are taken
  934. * directly from the Calendar counter.
  935. * @param hrtc RTC handle
  936. * @retval HAL status
  937. */
  938. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
  939. {
  940. /* Process Locked */
  941. __HAL_LOCK(hrtc);
  942. hrtc->State = HAL_RTC_STATE_BUSY;
  943. /* Disable the write protection for RTC registers */
  944. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  945. /* Set the BYPSHAD bit */
  946. SET_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
  947. /* Enable the write protection for RTC registers */
  948. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  949. /* Change RTC state */
  950. hrtc->State = HAL_RTC_STATE_READY;
  951. /* Process Unlocked */
  952. __HAL_UNLOCK(hrtc);
  953. return HAL_OK;
  954. }
  955. /**
  956. * @brief Disable the Bypass Shadow feature.
  957. * @note When the Bypass Shadow is enabled the calendar value are taken
  958. * directly from the Calendar counter.
  959. * @param hrtc RTC handle
  960. * @retval HAL status
  961. */
  962. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
  963. {
  964. /* Process Locked */
  965. __HAL_LOCK(hrtc);
  966. hrtc->State = HAL_RTC_STATE_BUSY;
  967. /* Disable the write protection for RTC registers */
  968. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  969. /* Reset the BYPSHAD bit */
  970. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
  971. /* Enable the write protection for RTC registers */
  972. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  973. /* Change RTC state */
  974. hrtc->State = HAL_RTC_STATE_READY;
  975. /* Process Unlocked */
  976. __HAL_UNLOCK(hrtc);
  977. return HAL_OK;
  978. }
  979. /**
  980. * @}
  981. */
  982. /** @addtogroup RTCEx_Exported_Functions_Group4
  983. * @brief Extended features functions
  984. *
  985. @verbatim
  986. ===============================================================================
  987. ##### Extended features functions #####
  988. ===============================================================================
  989. [..] This section provides functions allowing to:
  990. (+) RTC Alarm B callback
  991. (+) RTC Poll for Alarm B request
  992. @endverbatim
  993. * @{
  994. */
  995. /**
  996. * @brief Alarm B callback.
  997. * @param hrtc RTC handle
  998. * @retval None
  999. */
  1000. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1001. {
  1002. /* Prevent unused argument(s) compilation warning */
  1003. UNUSED(hrtc);
  1004. /* NOTE : This function should not be modified, when the callback is needed,
  1005. the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
  1006. */
  1007. }
  1008. /**
  1009. * @brief Handle Alarm B Polling request.
  1010. * @param hrtc RTC handle
  1011. * @param Timeout Timeout duration
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1015. {
  1016. uint32_t tickstart = HAL_GetTick();
  1017. while (READ_BIT(hrtc->Instance->SR, RTC_SR_ALRBF) == 0U)
  1018. {
  1019. if (Timeout != HAL_MAX_DELAY)
  1020. {
  1021. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1022. {
  1023. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1024. /* Process Unlocked */
  1025. __HAL_UNLOCK(hrtc);
  1026. return HAL_TIMEOUT;
  1027. }
  1028. }
  1029. }
  1030. /* Clear the Alarm Flag */
  1031. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRBF);
  1032. /* Change RTC state */
  1033. hrtc->State = HAL_RTC_STATE_READY;
  1034. return HAL_OK;
  1035. }
  1036. /**
  1037. * @}
  1038. */
  1039. /** @addtogroup RTCEx_Exported_Functions_Group5
  1040. * @brief Extended RTC Tamper functions
  1041. *
  1042. @verbatim
  1043. ==============================================================================
  1044. ##### Tamper functions #####
  1045. ==============================================================================
  1046. [..]
  1047. (+) Before calling any tamper or internal tamper function, you have to call first
  1048. HAL_RTC_Init() function.
  1049. (+) In that ine you can select to output tamper event on RTC pin.
  1050. [..]
  1051. (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
  1052. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  1053. value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
  1054. Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
  1055. You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
  1056. (+) The default configuration of the Tamper erases the backup registers. To avoid
  1057. erase, enable the NoErase field on the TAMP_TAMPCR register.
  1058. [..]
  1059. (+) Enable Internal Tamper and configure it with interrupt, timestamp using
  1060. the HAL_RTCEx_SetInternalTamper() function.
  1061. @endverbatim
  1062. * @{
  1063. */
  1064. /**
  1065. * @brief Set Tamper
  1066. * @param hrtc RTC handle
  1067. * @param sTamper Pointer to Tamper Structure.
  1068. * @retval HAL status
  1069. */
  1070. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1071. {
  1072. uint32_t tmpreg;
  1073. /* Check the parameters */
  1074. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1075. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1076. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1077. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1078. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1079. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1080. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1081. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1082. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1083. /* Trigger and Filter have exclusive configurations */
  1084. assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
  1085. || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
  1086. /* Configuration register 2 */
  1087. tmpreg = READ_REG(TAMP->CR2);
  1088. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  1089. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  1090. {
  1091. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  1092. }
  1093. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  1094. {
  1095. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  1096. }
  1097. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  1098. {
  1099. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  1100. }
  1101. WRITE_REG(TAMP->CR2, tmpreg);
  1102. /* Filter control register */
  1103. WRITE_REG(TAMP->FLTCR, (sTamper->Filter | sTamper->SamplingFrequency | \
  1104. sTamper->PrechargeDuration | sTamper->TamperPullUp));
  1105. /* timestamp on tamper */
  1106. if (READ_BIT(hrtc->Instance->CR, RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
  1107. {
  1108. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1109. MODIFY_REG(hrtc->Instance->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
  1110. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1111. }
  1112. /* Control register 1 */
  1113. SET_BIT(TAMP->CR1, sTamper->Tamper);
  1114. return HAL_OK;
  1115. }
  1116. /**
  1117. * @brief Set Tamper in IT mode
  1118. * @param hrtc RTC handle
  1119. * @param sTamper Pointer to Tamper Structure.
  1120. * @retval HAL status
  1121. */
  1122. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1123. {
  1124. uint32_t tmpreg;
  1125. /* Check the parameters */
  1126. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1127. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1128. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1129. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1130. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1131. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1132. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1133. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1134. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1135. /* Configuration register 2 */
  1136. tmpreg = READ_REG(TAMP->CR2);
  1137. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  1138. if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  1139. {
  1140. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  1141. }
  1142. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  1143. {
  1144. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  1145. }
  1146. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  1147. {
  1148. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  1149. }
  1150. WRITE_REG(TAMP->CR2, tmpreg);
  1151. /* Filter control register */
  1152. WRITE_REG(TAMP->FLTCR, (sTamper->Filter | sTamper->SamplingFrequency | \
  1153. sTamper->PrechargeDuration | sTamper->TamperPullUp));
  1154. /* timestamp on tamper */
  1155. if (READ_BIT(hrtc->Instance->CR, RTC_CR_TAMPTS) != sTamper->TimeStampOnTamperDetection)
  1156. {
  1157. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1158. MODIFY_REG(hrtc->Instance->CR, RTC_CR_TAMPTS, sTamper->TimeStampOnTamperDetection);
  1159. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1160. }
  1161. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  1162. __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
  1163. __HAL_RTC_TAMPER_EXTI_RISING_IT();
  1164. __HAL_RTC_TAMPER_EXTI_CLEAR_IT();
  1165. /* Interrupt enable register */
  1166. SET_BIT(TAMP->IER, sTamper->Tamper);
  1167. /* Control register 1 */
  1168. SET_BIT(TAMP->CR1, sTamper->Tamper);
  1169. return HAL_OK;
  1170. }
  1171. /**
  1172. * @brief Deactivate Tamper.
  1173. * @param hrtc RTC handle
  1174. * @param Tamper Selected tamper pin.
  1175. * This parameter can be a combination of the following values:
  1176. * @arg RTC_TAMPER_1
  1177. * @arg RTC_TAMPER_2
  1178. * @retval HAL status
  1179. */
  1180. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  1181. {
  1182. UNUSED(hrtc);
  1183. assert_param(IS_RTC_TAMPER(Tamper));
  1184. /* Disable the selected Tamper pin */
  1185. CLEAR_BIT(TAMP->CR1, Tamper);
  1186. /* Clear tamper mask/noerase/trigger configuration */
  1187. CLEAR_BIT(TAMP->CR2, ((Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | (Tamper << TAMP_CR2_TAMP1NOERASE_Pos)));
  1188. /* Clear tamper interrupt mode configuration */
  1189. CLEAR_BIT(TAMP->IER, Tamper);
  1190. /* Clear tamper interrupt and event flags (WO register) */
  1191. WRITE_REG(TAMP->SCR, Tamper);
  1192. /* In case of interrupt mode is used, the interrupt source must disabled */
  1193. __HAL_RTC_TAMPER_EXTI_CLEAR_IT();
  1194. return HAL_OK;
  1195. }
  1196. /**
  1197. * @brief Tamper event polling.
  1198. * @param hrtc RTC handle
  1199. * @param Tamper Selected tamper pin.
  1200. * This parameter can be a combination of the following values:
  1201. * @arg RTC_TAMPER_1
  1202. * @arg RTC_TAMPER_2
  1203. * @param Timeout Timeout duration
  1204. * @retval HAL status
  1205. */
  1206. HAL_StatusTypeDef HAL_RTCEx_PollForTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t Tamper, uint32_t Timeout)
  1207. {
  1208. uint32_t tickstart = HAL_GetTick();
  1209. UNUSED(hrtc);
  1210. assert_param(IS_RTC_TAMPER(Tamper));
  1211. /* Get the status of the Interrupt */
  1212. while (READ_BIT(TAMP->SR, Tamper) != Tamper)
  1213. {
  1214. if (Timeout != HAL_MAX_DELAY)
  1215. {
  1216. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1217. {
  1218. return HAL_TIMEOUT;
  1219. }
  1220. }
  1221. }
  1222. /* Clear the Tamper Flag */
  1223. WRITE_REG(TAMP->SCR, Tamper);
  1224. return HAL_OK;
  1225. }
  1226. /**
  1227. * @brief Set Internal Tamper in interrupt mode
  1228. * @param hrtc RTC handle
  1229. * @param sIntTamper Pointer to Internal Tamper Structure.
  1230. * @retval HAL status
  1231. */
  1232. HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
  1233. {
  1234. /* Check the parameters */
  1235. assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
  1236. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
  1237. /* timestamp on internal tamper */
  1238. if (READ_BIT(hrtc->Instance->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
  1239. {
  1240. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1241. MODIFY_REG(hrtc->Instance->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
  1242. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1243. }
  1244. /* Control register 1 */
  1245. SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
  1246. return HAL_OK;
  1247. }
  1248. /**
  1249. * @brief Set Internal Tamper
  1250. * @param hrtc RTC handle
  1251. * @param sIntTamper Pointer to Internal Tamper Structure.
  1252. * @retval HAL status
  1253. */
  1254. HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
  1255. {
  1256. /* Check the parameters */
  1257. assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
  1258. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
  1259. /* timestamp on internal tamper */
  1260. if (READ_BIT(hrtc->Instance->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
  1261. {
  1262. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1263. MODIFY_REG(hrtc->Instance->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
  1264. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1265. }
  1266. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  1267. __HAL_RTC_TAMPER_EXTI_ENABLE_IT();
  1268. __HAL_RTC_TAMPER_EXTI_RISING_IT();
  1269. /* Interrupt enable register */
  1270. SET_BIT(TAMP->IER, sIntTamper->IntTamper);
  1271. /* Control register 1 */
  1272. SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
  1273. return HAL_OK;
  1274. }
  1275. /**
  1276. * @brief Deactivate Internal Tamper.
  1277. * @param hrtc RTC handle
  1278. * @param IntTamper Selected internal tamper event.
  1279. * This parameter can be any combination of existing internal tampers.
  1280. * @retval HAL status
  1281. */
  1282. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
  1283. {
  1284. UNUSED(hrtc);
  1285. assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
  1286. /* Disable the selected Tamper pin */
  1287. CLEAR_BIT(TAMP->CR1, IntTamper);
  1288. /* Clear internal tamper interrupt mode configuration */
  1289. CLEAR_BIT(TAMP->IER, IntTamper);
  1290. /* Clear internal tamper interrupt */
  1291. WRITE_REG(TAMP->SCR, IntTamper);
  1292. /* In case of interrupt mode is used, the interrupt source must disabled */
  1293. __HAL_RTC_TAMPER_EXTI_CLEAR_IT();
  1294. return HAL_OK;
  1295. }
  1296. /**
  1297. * @brief Internal Tamper event polling.
  1298. * @param hrtc RTC handle
  1299. * @param IntTamper selected tamper.
  1300. * This parameter can be any combination of existing internal tampers.
  1301. * @param Timeout Timeout duration
  1302. * @retval HAL status
  1303. */
  1304. HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
  1305. {
  1306. UNUSED(hrtc);
  1307. assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
  1308. uint32_t tickstart = HAL_GetTick();
  1309. /* Get the status of the Interrupt */
  1310. while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
  1311. {
  1312. if (Timeout != HAL_MAX_DELAY)
  1313. {
  1314. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1315. {
  1316. return HAL_TIMEOUT;
  1317. }
  1318. }
  1319. }
  1320. /* Clear the Tamper Flag */
  1321. WRITE_REG(TAMP->SCR, IntTamper);
  1322. return HAL_OK;
  1323. }
  1324. /**
  1325. * @brief Handle Tamper interrupt request.
  1326. * @param hrtc RTC handle
  1327. * @retval None
  1328. */
  1329. void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
  1330. {
  1331. uint32_t tmp;
  1332. /* Get interrupt status */
  1333. tmp = READ_REG(TAMP->MISR);
  1334. /* Check enable interrupts */
  1335. tmp &= READ_REG(TAMP->IER);
  1336. /* Immediately clear flags */
  1337. WRITE_REG(TAMP->SCR, tmp);
  1338. /* In case of interrupt mode is used, the interrupt source must disabled */
  1339. __HAL_RTC_TAMPER_EXTI_CLEAR_IT();
  1340. /* Check Tamper1 status */
  1341. if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
  1342. {
  1343. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1344. /* Call Tamper 1 Event registered Callback */
  1345. hrtc->Tamper1EventCallback(hrtc);
  1346. #else
  1347. /* Tamper1 callback */
  1348. HAL_RTCEx_Tamper1EventCallback(hrtc);
  1349. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1350. }
  1351. /* Check Tamper2 status */
  1352. if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
  1353. {
  1354. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1355. /* Call Tamper 2 Event registered Callback */
  1356. hrtc->Tamper2EventCallback(hrtc);
  1357. #else
  1358. /* Tamper2 callback */
  1359. HAL_RTCEx_Tamper2EventCallback(hrtc);
  1360. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1361. }
  1362. #if (RTC_TAMP_NB == 3)
  1363. /* Check Tamper3 status */
  1364. if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
  1365. {
  1366. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1367. /* Call Tamper 3 Event registered Callback */
  1368. hrtc->Tamper3EventCallback(hrtc);
  1369. #else
  1370. /* Tamper3 callback */
  1371. HAL_RTCEx_Tamper3EventCallback(hrtc);
  1372. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1373. }
  1374. #endif /* RTC_TAMP_NB */
  1375. #ifdef RTC_TAMP_INT_1_SUPPORT
  1376. /* Check Internal Tamper1 status */
  1377. if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
  1378. {
  1379. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1380. /* Call Internal Tamper 1 Event registered Callback */
  1381. hrtc->InternalTamper1EventCallback(hrtc);
  1382. #else
  1383. /* Internal Tamper1 callback */
  1384. HAL_RTCEx_InternalTamper1EventCallback(hrtc);
  1385. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1386. }
  1387. #endif /* RTC_TAMP_INT_1_SUPPORT */
  1388. #ifdef RTC_TAMP_INT_2_SUPPORT
  1389. /* Check Internal Tamper2 status */
  1390. if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
  1391. {
  1392. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1393. /* Call Internal Tamper 2 Event registered Callback */
  1394. hrtc->InternalTamper2EventCallback(hrtc);
  1395. #else
  1396. /* Internal Tamper2 callback */
  1397. HAL_RTCEx_InternalTamper2EventCallback(hrtc);
  1398. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1399. }
  1400. #endif /* RTC_TAMP_INT_2_SUPPORT */
  1401. /* Check Internal Tamper3 status */
  1402. if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
  1403. {
  1404. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1405. /* Call Internal Tamper 3 Event registered Callback */
  1406. hrtc->InternalTamper3EventCallback(hrtc);
  1407. #else
  1408. /* Internal Tamper3 callback */
  1409. HAL_RTCEx_InternalTamper3EventCallback(hrtc);
  1410. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1411. }
  1412. /* Check Internal Tamper4 status */
  1413. if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
  1414. {
  1415. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1416. /* Call Internal Tamper 4 Event registered Callback */
  1417. hrtc->InternalTamper4EventCallback(hrtc);
  1418. #else
  1419. /* Internal Tamper4 callback */
  1420. HAL_RTCEx_InternalTamper4EventCallback(hrtc);
  1421. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1422. }
  1423. /* Check Internal Tamper5 status */
  1424. if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
  1425. {
  1426. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1427. /* Call Internal Tamper 5 Event registered Callback */
  1428. hrtc->InternalTamper5EventCallback(hrtc);
  1429. #else
  1430. /* Internal Tamper5 callback */
  1431. HAL_RTCEx_InternalTamper5EventCallback(hrtc);
  1432. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1433. }
  1434. #ifdef RTC_TAMP_INT_6_SUPPORT
  1435. /* Check Internal Tamper6 status */
  1436. if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
  1437. {
  1438. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1439. /* Call Internal Tamper 6 Event registered Callback */
  1440. hrtc->InternalTamper6EventCallback(hrtc);
  1441. #else
  1442. /* Internal Tamper6 callback */
  1443. HAL_RTCEx_InternalTamper6EventCallback(hrtc);
  1444. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1445. }
  1446. #endif /* RTC_TAMP_INT_6_SUPPORT */
  1447. #ifdef RTC_TAMP_INT_7_SUPPORT
  1448. /* Check Internal Tamper7 status */
  1449. if ((tmp & RTC_INT_TAMPER_7) == RTC_INT_TAMPER_7)
  1450. {
  1451. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1452. /* Call Internal Tamper 7 Event registered Callback */
  1453. hrtc->InternalTamper7EventCallback(hrtc);
  1454. #else
  1455. /* Internal Tamper7 callback */
  1456. HAL_RTCEx_InternalTamper7EventCallback(hrtc);
  1457. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1458. }
  1459. #endif /* RTC_TAMP_INT_7_SUPPORT */
  1460. }
  1461. /**
  1462. * @brief Tamper 1 callback.
  1463. * @param hrtc RTC handle
  1464. * @retval None
  1465. */
  1466. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  1467. {
  1468. /* Prevent unused argument(s) compilation warning */
  1469. UNUSED(hrtc);
  1470. /* NOTE : This function should not be modified, when the callback is needed,
  1471. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  1472. */
  1473. }
  1474. /**
  1475. * @brief Tamper 2 callback.
  1476. * @param hrtc RTC handle
  1477. * @retval None
  1478. */
  1479. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  1480. {
  1481. /* Prevent unused argument(s) compilation warning */
  1482. UNUSED(hrtc);
  1483. /* NOTE : This function should not be modified, when the callback is needed,
  1484. the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  1485. */
  1486. }
  1487. #if (RTC_TAMP_NB == 3)
  1488. /**
  1489. * @brief Tamper 3 callback.
  1490. * @param hrtc RTC handle
  1491. * @retval None
  1492. */
  1493. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  1494. {
  1495. /* Prevent unused argument(s) compilation warning */
  1496. UNUSED(hrtc);
  1497. /* NOTE : This function should not be modified, when the callback is needed,
  1498. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  1499. */
  1500. }
  1501. #endif /* RTC_TAMP_NB */
  1502. #ifdef RTC_TAMP_INT_1_SUPPORT
  1503. /**
  1504. * @brief Internal Tamper 1 callback.
  1505. * @param hrtc RTC handle
  1506. * @retval None
  1507. */
  1508. __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
  1509. {
  1510. /* Prevent unused argument(s) compilation warning */
  1511. UNUSED(hrtc);
  1512. /* NOTE : This function should not be modified, when the callback is needed,
  1513. the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
  1514. */
  1515. }
  1516. #endif /* RTC_TAMP_INT_1_SUPPORT */
  1517. #ifdef RTC_TAMP_INT_2_SUPPORT
  1518. /**
  1519. * @brief Internal Tamper 2 callback.
  1520. * @param hrtc RTC handle
  1521. * @retval None
  1522. */
  1523. __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
  1524. {
  1525. /* Prevent unused argument(s) compilation warning */
  1526. UNUSED(hrtc);
  1527. /* NOTE : This function should not be modified, when the callback is needed,
  1528. the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
  1529. */
  1530. }
  1531. #endif /* RTC_TAMP_INT_2_SUPPORT */
  1532. /**
  1533. * @brief Internal Tamper 3 callback.
  1534. * @param hrtc RTC handle
  1535. * @retval None
  1536. */
  1537. __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
  1538. {
  1539. /* Prevent unused argument(s) compilation warning */
  1540. UNUSED(hrtc);
  1541. /* NOTE : This function should not be modified, when the callback is needed,
  1542. the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
  1543. */
  1544. }
  1545. /**
  1546. * @brief Internal Tamper 4 callback.
  1547. * @param hrtc RTC handle
  1548. * @retval None
  1549. */
  1550. __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
  1551. {
  1552. /* Prevent unused argument(s) compilation warning */
  1553. UNUSED(hrtc);
  1554. /* NOTE : This function should not be modified, when the callback is needed,
  1555. the HAL_RTCEx_InternalTamper4EventCallback could be implemented in the user file
  1556. */
  1557. }
  1558. /**
  1559. * @brief Internal Tamper 5 callback.
  1560. * @param hrtc RTC handle
  1561. * @retval None
  1562. */
  1563. __weak void HAL_RTCEx_InternalTamper5EventCallback(RTC_HandleTypeDef *hrtc)
  1564. {
  1565. /* Prevent unused argument(s) compilation warning */
  1566. UNUSED(hrtc);
  1567. /* NOTE : This function should not be modified, when the callback is needed,
  1568. the HAL_RTCEx_InternalTamper5EventCallback could be implemented in the user file
  1569. */
  1570. }
  1571. #ifdef RTC_TAMP_INT_6_SUPPORT
  1572. /**
  1573. * @brief Internal Tamper 6 callback.
  1574. * @param hrtc RTC handle
  1575. * @retval None
  1576. */
  1577. __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
  1578. {
  1579. /* Prevent unused argument(s) compilation warning */
  1580. UNUSED(hrtc);
  1581. /* NOTE : This function should not be modified, when the callback is needed,
  1582. the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
  1583. */
  1584. }
  1585. #endif /* RTC_TAMP_INT_6_SUPPORT */
  1586. #ifdef RTC_TAMP_INT_7_SUPPORT
  1587. /**
  1588. * @brief Internal Tamper 7 callback.
  1589. * @param hrtc RTC handle
  1590. * @retval None
  1591. */
  1592. __weak void HAL_RTCEx_InternalTamper7EventCallback(RTC_HandleTypeDef *hrtc)
  1593. {
  1594. /* Prevent unused argument(s) compilation warning */
  1595. UNUSED(hrtc);
  1596. /* NOTE : This function should not be modified, when the callback is needed,
  1597. the HAL_RTCEx_InternalTamper7EventCallback could be implemented in the user file
  1598. */
  1599. }
  1600. #endif /* RTC_TAMP_INT_7_SUPPORT */
  1601. /**
  1602. * @}
  1603. */
  1604. /** @addtogroup RTCEx_Exported_Functions_Group6
  1605. * @brief Extended RTC Backup register functions
  1606. *
  1607. @verbatim
  1608. ===============================================================================
  1609. ##### Extended RTC Backup register functions #####
  1610. ===============================================================================
  1611. [..]
  1612. (+) Before calling any tamper or internal tamper function, you have to call first
  1613. HAL_RTC_Init() function.
  1614. (+) In that ine you can select to output tamper event on RTC pin.
  1615. [..]
  1616. This subsection provides functions allowing to
  1617. (+) Write a data in a specified RTC Backup data register
  1618. (+) Read a data in a specified RTC Backup data register
  1619. @endverbatim
  1620. * @{
  1621. */
  1622. /**
  1623. * @brief Write a data in a specified TAMP Backup data register.
  1624. * @param hrtc RTC handle
  1625. * @param BackupRegister RTC Backup data Register number.
  1626. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1627. * specify the register.
  1628. * @param Data Data to be written in the specified TAMP Backup data register.
  1629. * @retval None
  1630. */
  1631. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1632. {
  1633. uint32_t tmp;
  1634. UNUSED(hrtc);
  1635. /* Check the parameters */
  1636. assert_param(IS_RTC_BKP(BackupRegister));
  1637. tmp = (uint32_t) &(TAMP->BKP0R);
  1638. tmp += (BackupRegister * 4U);
  1639. /* Write the specified register */
  1640. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1641. }
  1642. /**
  1643. * @brief Reads data from the specified TAMP Backup data Register.
  1644. * @param hrtc RTC handle
  1645. * @param BackupRegister RTC Backup data Register number.
  1646. * This parameter can be: RTC_BKP_DRx where x can be from 0 to RTC_BACKUP_NB to
  1647. * specify the register.
  1648. * @retval Read value
  1649. */
  1650. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1651. {
  1652. uint32_t tmp;
  1653. UNUSED(hrtc);
  1654. /* Check the parameters */
  1655. assert_param(IS_RTC_BKP(BackupRegister));
  1656. tmp = (uint32_t) &(TAMP->BKP0R);
  1657. tmp += (BackupRegister * 4U);
  1658. /* Read the specified register */
  1659. return (*(__IO uint32_t *)tmp);
  1660. }
  1661. /**
  1662. * @}
  1663. */
  1664. /**
  1665. * @}
  1666. */
  1667. #endif /* HAL_RTC_MODULE_ENABLED */
  1668. /**
  1669. * @}
  1670. */
  1671. /**
  1672. * @}
  1673. */