stm32f7xx_hal_rtc_ex.c 64 KB

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