stm32g4xx_hal_rtc.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_hal_rtc.c
  4. * @author MCD Application Team
  5. * @brief RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real-Time Clock (RTC) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Calendar (Time and Date) configuration
  10. * + Alarms (Alarm A and Alarm B) configuration
  11. * + WakeUp Timer configuration
  12. * + TimeStamp configuration
  13. * + Tampers configuration
  14. * + Backup Data Registers configuration
  15. * + RTC Tamper and TimeStamp Pins Selection
  16. * + Interrupts and flags management
  17. *
  18. ******************************************************************************
  19. * @attention
  20. *
  21. * Copyright (c) 2019 STMicroelectronics.
  22. * All rights reserved.
  23. *
  24. * This software is licensed under terms that can be found in the LICENSE file
  25. * in the root directory of this software component.
  26. * If no LICENSE file comes with this software, it is provided AS-IS.
  27. *
  28. ******************************************************************************
  29. @verbatim
  30. ===============================================================================
  31. ##### RTC Operating Condition #####
  32. ===============================================================================
  33. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  34. from the VBAT voltage when the main VDD supply is powered off.
  35. To retain the content of the RTC backup registers and supply the RTC
  36. when VDD is turned off, VBAT pin can be connected to an optional
  37. standby voltage supplied by a battery or by another source.
  38. ##### Backup Domain Reset #####
  39. ===============================================================================
  40. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  41. to their reset values.
  42. A backup domain reset is generated when one of the following events occurs:
  43. (#) Software reset, triggered by setting the BDRST bit in the
  44. RCC Backup domain control register (RCC_BDCR).
  45. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  46. (#) Tamper detection event resets all data backup registers.
  47. ##### Backup Domain Access #####
  48. ==================================================================
  49. [..] After reset, the backup domain (RTC registers and RTC backup data registers)
  50. is protected against possible unwanted write accesses.
  51. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  52. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  53. __HAL_RCC_PWR_CLK_ENABLE() function.
  54. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  55. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  56. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  57. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  58. (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
  59. PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
  60. (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
  61. ##### How to use RTC Driver #####
  62. ===================================================================
  63. [..]
  64. (+) Enable the RTC domain access (see description in the section above).
  65. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  66. format using the HAL_RTC_Init() function.
  67. *** Time and Date configuration ***
  68. ===================================
  69. [..]
  70. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  71. and HAL_RTC_SetDate() functions.
  72. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  73. *** Alarm configuration ***
  74. ===========================
  75. [..]
  76. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  77. You can also configure the RTC Alarm with interrupt mode using the
  78. HAL_RTC_SetAlarm_IT() function.
  79. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  80. ##### RTC and low power modes #####
  81. ==================================================================
  82. [..] The MCU can be woken up from a low power mode by an RTC alternate
  83. function.
  84. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  85. RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  86. These RTC alternate functions can wake up the system from the Stop and
  87. Standby low power modes.
  88. [..] The system can also wake up from low power modes without depending
  89. on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  90. or the RTC wakeup events.
  91. [..] The RTC provides a programmable time base for waking up from the
  92. Stop or Standby mode at regular intervals.
  93. Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
  94. is LSE or LSI.
  95. *** Callback registration ***
  96. =============================================
  97. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  98. not defined, the callback registration feature is not available and all callbacks
  99. are set to the corresponding weak functions. This is the recommended configuration
  100. in order to optimize memory/code consumption footprint/performances.
  101. [..]
  102. The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
  103. allows the user to configure dynamically the driver callbacks.
  104. Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
  105. [..]
  106. Function HAL_RTC_RegisterCallback() allows to register following callbacks:
  107. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  108. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  109. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  110. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  111. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  112. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  113. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  114. (+) Tamper4EventCallback : RTC Tamper 4 Event callback.
  115. (+) Tamper5EventCallback : RTC Tamper 5 Event callback.
  116. (+) Tamper6EventCallback : RTC Tamper 6 Event callback.
  117. (+) Tamper7EventCallback : RTC Tamper 7 Event callback.
  118. (+) Tamper8EventCallback : RTC Tamper 8 Event callback.
  119. (+) InternalTamper1EventCallback : RTC InternalTamper 1 Event callback.
  120. (+) InternalTamper2EventCallback : RTC InternalTamper 2 Event callback.
  121. (+) InternalTamper3EventCallback : RTC InternalTamper 3 Event callback.
  122. (+) InternalTamper5EventCallback : RTC InternalTamper 5 Event callback.
  123. (+) InternalTamper8EventCallback : RTC InternalTamper 8 Event callback.
  124. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  125. (+) AlarmAEventCallback_S : RTC Alarm A Event callback_S
  126. (+) AlarmBEventCallback_S : RTC Alarm B Event callback_S.
  127. (+) TimeStampEventCallback_S : RTC TimeStampEvent callback_S.
  128. (+) WakeUpTimerEventCallback_S : RTC WakeUpTimerEvent callback_S.
  129. (+) Tamper1EventCallback_S : RTC Tamper 1 Event callback_S.
  130. (+) Tamper2EventCallback_S : RTC Tamper 2 Event callback_S.
  131. (+) Tamper3EventCallback_S : RTC Tamper 3 Event callback_S.
  132. (+) Tamper4EventCallback_S : RTC Tamper 4 Event callback_S.
  133. (+) Tamper5EventCallback_S : RTC Tamper 5 Event callback_S.
  134. (+) Tamper6EventCallback_S : RTC Tamper 6 Event callback_S.
  135. (+) Tamper7EventCallback_S : RTC Tamper 7 Event callback_S.
  136. (+) Tamper8EventCallback_S : RTC Tamper 8 Event callback_S.
  137. (+) InternalTamper1EventCallback_S : RTC InternalTamper 1 Event callback_S.
  138. (+) InternalTamper2EventCallback_S : RTC InternalTamper 2 Event callback_S.
  139. (+) InternalTamper3EventCallback_S : RTC InternalTamper 3 Event callback_S.
  140. (+) InternalTamper5EventCallback_S : RTC InternalTamper 5 Event callback_S.
  141. (+) InternalTamper8EventCallback_S : RTC InternalTamper 8 Event callback_S.
  142. #endif
  143. (+) MspInitCallback : RTC MspInit callback.
  144. (+) MspDeInitCallback : RTC MspDeInit callback.
  145. [..]
  146. This function takes as parameters the HAL peripheral handle, the Callback ID
  147. and a pointer to the user callback function.
  148. [..]
  149. Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
  150. weak function.
  151. HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  152. and the Callback ID.
  153. This function allows to reset following callbacks:
  154. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  155. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  156. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  157. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  158. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  159. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  160. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  161. (+) Tamper4EventCallback : RTC Tamper 4 Event callback.
  162. (+) Tamper5EventCallback : RTC Tamper 5 Event callback.
  163. (+) Tamper6EventCallback : RTC Tamper 6 Event callback.
  164. (+) Tamper7EventCallback : RTC Tamper 7 Event callback.
  165. (+) Tamper8EventCallback : RTC Tamper 8 Event callback.
  166. (+) InternalTamper1EventCallback : RTC Internal Tamper 1 Event callback.
  167. (+) InternalTamper2EventCallback : RTC Internal Tamper 2 Event callback.
  168. (+) InternalTamper3EventCallback : RTC Internal Tamper 3 Event callback.
  169. (+) InternalTamper4EventCallback : RTC Internal Tamper 4 Event callback.
  170. (+) InternalTamper5EventCallback : RTC Internal Tamper 5 Event callback.
  171. (+) InternalTamper6EventCallback : RTC Internal Tamper 6 Event callback.
  172. (+) InternalTamper8EventCallback : RTC Internal Tamper 8 Event callback.
  173. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  174. (+) AlarmAEventCallback_S : RTC Alarm A Event callback secure.
  175. (+) AlarmBEventCallback_S : RTC Alarm B Event callback secure.
  176. (+) TimeStampEventCallback_S : RTC TimeStamp Event callback secure.
  177. (+) WakeUpTimerEventCallback_S : RTC WakeUpTimer Event callback secure.
  178. (+) Tamper1EventCallback_S : RTC Tamper 1 Event callback secure.
  179. (+) Tamper2EventCallback_S : RTC Tamper 2 Event callback secure.
  180. (+) Tamper3EventCallback_S : RTC Tamper 3 Event callback secure.
  181. (+) Tamper4EventCallback_S : RTC Tamper 4 Event callback secure.
  182. (+) Tamper5EventCallback_S : RTC Tamper 5 Event callback secure.
  183. (+) Tamper6EventCallback_S : RTC Tamper 6 Event callback secure.
  184. (+) Tamper7EventCallback_S : RTC Tamper 7 Event callback secure.
  185. (+) Tamper8EventCallback_S : RTC Tamper 8 Event callback secure.
  186. (+) InternalTamper1EventCallback_S : RTC Internal Tamper 1 Event callback secure.
  187. (+) InternalTamper2EventCallback_S : RTC Internal Tamper 2 Event callback secure.
  188. (+) InternalTamper3EventCallback_S : RTC Internal Tamper 3 Event callback secure.
  189. (+) InternalTamper4EventCallback_S : RTC Internal Tamper 4 Event callback secure.
  190. (+) InternalTamper5EventCallback_S : RTC Internal Tamper 5 Event callback secure.
  191. (+) InternalTamper6EventCallback_S : RTC Internal Tamper 6 Event callback secure.
  192. (+) InternalTamper8EventCallback_S : RTC Internal Tamper 8 Event callback secure.
  193. #endif
  194. (+) MspInitCallback : RTC MspInit callback.
  195. (+) MspDeInitCallback : RTC MspDeInit callback.
  196. [..]
  197. By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  198. all callbacks are set to the corresponding weak functions :
  199. examples AlarmAEventCallback(), TimeStampEventCallback().
  200. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  201. in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
  202. (not registered beforehand).
  203. If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
  204. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  205. [..]
  206. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  207. Exception done MspInit/MspDeInit that can be registered/unregistered
  208. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  209. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  210. In that case first register the MspInit/MspDeInit user callbacks
  211. using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
  212. or HAL_RTC_Init() function.
  213. [..]
  214. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  215. not defined, the callback registration feature is not available and all callbacks
  216. are set to the corresponding weak functions.
  217. @endverbatim
  218. ******************************************************************************
  219. */
  220. /* Includes ------------------------------------------------------------------*/
  221. #include "stm32g4xx_hal.h"
  222. /** @addtogroup STM32G4xx_HAL_Driver
  223. * @{
  224. */
  225. /** @addtogroup RTC
  226. * @brief RTC HAL module driver
  227. * @{
  228. */
  229. #ifdef HAL_RTC_MODULE_ENABLED
  230. /* Private typedef -----------------------------------------------------------*/
  231. /* Private define ------------------------------------------------------------*/
  232. /* Private macro -------------------------------------------------------------*/
  233. /* Private variables ---------------------------------------------------------*/
  234. /* Private function prototypes -----------------------------------------------*/
  235. /* Exported functions --------------------------------------------------------*/
  236. /** @addtogroup RTC_Exported_Functions
  237. * @{
  238. */
  239. /** @addtogroup RTC_Exported_Functions_Group1
  240. * @brief Initialization and Configuration functions
  241. *
  242. @verbatim
  243. ===============================================================================
  244. ##### Initialization and de-initialization functions #####
  245. ===============================================================================
  246. [..] This section provides functions allowing to initialize and configure the
  247. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  248. RTC registers Write protection, enter and exit the RTC initialization mode,
  249. RTC registers synchronization check and reference clock detection enable.
  250. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  251. It is split into 2 programmable prescalers to minimize power consumption.
  252. (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
  253. (++) When both prescalers are used, it is recommended to configure the
  254. asynchronous prescaler to a high value to minimize power consumption.
  255. (#) All RTC registers are Write protected. Writing to the RTC registers
  256. is enabled by writing a key into the Write Protection register, RTC_WPR.
  257. (#) To configure the RTC Calendar, user application should enter
  258. initialization mode. In this mode, the calendar counter is stopped
  259. and its value can be updated. When the initialization sequence is
  260. complete, the calendar restarts counting after 4 RTCCLK cycles.
  261. (#) To read the calendar through the shadow registers after Calendar
  262. initialization, calendar update or after wakeup from low power modes
  263. the software must first clear the RSF flag. The software must then
  264. wait until it is set again before reading the calendar, which means
  265. that the calendar registers have been correctly copied into the
  266. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  267. implements the above software sequence (RSF clear and RSF check).
  268. @endverbatim
  269. * @{
  270. */
  271. /**
  272. * @brief Initialize the RTC peripheral
  273. * @param hrtc RTC handle
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  277. {
  278. HAL_StatusTypeDef status = HAL_ERROR;
  279. /* Check the RTC peripheral state */
  280. if (hrtc != NULL)
  281. {
  282. /* Check the parameters */
  283. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  284. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  285. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  286. assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
  287. assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
  288. assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  289. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  290. assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
  291. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  292. if (hrtc->State == HAL_RTC_STATE_RESET)
  293. {
  294. /* Allocate lock resource and initialize it */
  295. hrtc->Lock = HAL_UNLOCKED;
  296. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  297. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  298. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  299. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  300. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  301. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  302. #if (RTC_TAMP_NB == 3)
  303. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  304. #endif /* RTC_TAMP_NB */
  305. #ifdef RTC_TAMP_INT_1_SUPPORT
  306. hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback; /*!< Legacy weak InternalTamper1EventCallback */
  307. #endif /* RTC_TAMP_INT_1_SUPPORT */
  308. #ifdef RTC_TAMP_INT_2_SUPPORT
  309. hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback; /*!< Legacy weak InternalTamper2EventCallback */
  310. #endif /* RTC_TAMP_INT_2_SUPPORT */
  311. hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /*!< Legacy weak InternalTamper3EventCallback */
  312. hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /*!< Legacy weak InternalTamper4EventCallback */
  313. hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /*!< Legacy weak InternalTamper5EventCallback */
  314. #ifdef RTC_TAMP_INT_6_SUPPORT
  315. hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /*!< Legacy weak InternalTamper6EventCallback */
  316. #endif /* RTC_TAMP_INT_6_SUPPORT */
  317. #ifdef RTC_TAMP_INT_7_SUPPORT
  318. hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback; /*!< Legacy weak InternalTamper7EventCallback */
  319. #endif /* RTC_TAMP_INT_7_SUPPORT */
  320. if (hrtc->MspInitCallback == NULL)
  321. {
  322. hrtc->MspInitCallback = HAL_RTC_MspInit;
  323. }
  324. /* Init the low level hardware */
  325. hrtc->MspInitCallback(hrtc);
  326. if (hrtc->MspDeInitCallback == NULL)
  327. {
  328. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  329. }
  330. }
  331. #else
  332. if (hrtc->State == HAL_RTC_STATE_RESET)
  333. {
  334. /* Allocate lock resource and initialize it */
  335. hrtc->Lock = HAL_UNLOCKED;
  336. /* Initialize RTC MSP */
  337. HAL_RTC_MspInit(hrtc);
  338. }
  339. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  340. /* Set RTC state */
  341. hrtc->State = HAL_RTC_STATE_BUSY;
  342. /* Check whether the calendar needs to be initialized */
  343. if (__HAL_RTC_IS_CALENDAR_INITIALIZED(hrtc) == 0U)
  344. {
  345. /* Disable the write protection for RTC registers */
  346. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  347. /* Enter Initialization mode */
  348. status = RTC_EnterInitMode(hrtc);
  349. if (status == HAL_OK)
  350. {
  351. /* Clear RTC_CR FMT, OSEL and POL Bits */
  352. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE));
  353. /* Set RTC_CR register */
  354. SET_BIT(hrtc->Instance->CR, (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity));
  355. /* Configure the RTC PRER */
  356. WRITE_REG(hrtc->Instance->PRER, ((hrtc->Init.SynchPrediv) | (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos)));
  357. /* Exit Initialization mode */
  358. status = RTC_ExitInitMode(hrtc);
  359. }
  360. if (status == HAL_OK)
  361. {
  362. MODIFY_REG(hrtc->Instance->CR, \
  363. RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN, \
  364. hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
  365. }
  366. /* Enable the write protection for RTC registers */
  367. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  368. }
  369. else
  370. {
  371. /* The calendar is already initialized */
  372. status = HAL_OK;
  373. }
  374. if (status == HAL_OK)
  375. {
  376. hrtc->State = HAL_RTC_STATE_READY;
  377. }
  378. }
  379. return status;
  380. }
  381. /**
  382. * @brief DeInitialize the RTC peripheral.
  383. * @note This function does not reset the RTC Backup Data registers.
  384. * @param hrtc RTC handle
  385. * @retval HAL status
  386. */
  387. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  388. {
  389. HAL_StatusTypeDef status;
  390. /* Set RTC state */
  391. hrtc->State = HAL_RTC_STATE_BUSY;
  392. /* Disable the write protection for RTC registers */
  393. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  394. status = RTC_EnterInitMode(hrtc);
  395. /* Set Initialization mode */
  396. if (status != HAL_OK)
  397. {
  398. /* Set RTC state */
  399. hrtc->State = HAL_RTC_STATE_ERROR;
  400. }
  401. else
  402. {
  403. /* Reset all RTC CR register bits */
  404. CLEAR_REG(hrtc->Instance->CR);
  405. WRITE_REG(hrtc->Instance->DR, (uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
  406. CLEAR_REG(hrtc->Instance->TR);
  407. WRITE_REG(hrtc->Instance->WUTR, RTC_WUTR_WUT);
  408. WRITE_REG(hrtc->Instance->PRER, ((uint32_t)(RTC_PRER_PREDIV_A | 0xFFU)));
  409. CLEAR_REG(hrtc->Instance->ALRMAR);
  410. CLEAR_REG(hrtc->Instance->ALRMBR);
  411. CLEAR_REG(hrtc->Instance->SHIFTR);
  412. CLEAR_REG(hrtc->Instance->CALR);
  413. CLEAR_REG(hrtc->Instance->ALRMASSR);
  414. CLEAR_REG(hrtc->Instance->ALRMBSSR);
  415. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CITSF | RTC_SCR_CTSOVF | RTC_SCR_CTSF | RTC_SCR_CWUTF | RTC_SCR_CALRBF | RTC_SCR_CALRAF);
  416. /* Exit initialization mode */
  417. CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
  418. status = HAL_RTC_WaitForSynchro(hrtc);
  419. if (status != HAL_OK)
  420. {
  421. hrtc->State = HAL_RTC_STATE_ERROR;
  422. }
  423. else
  424. {
  425. /* Reset TAMP registers */
  426. WRITE_REG(TAMP->CR1, RTC_INT_TAMPER_ALL);
  427. CLEAR_REG(TAMP->CR2);
  428. CLEAR_REG(TAMP->FLTCR);
  429. }
  430. }
  431. /* Enable the write protection for RTC registers */
  432. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  433. if (status == HAL_OK)
  434. {
  435. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  436. if (hrtc->MspDeInitCallback == NULL)
  437. {
  438. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  439. }
  440. /* DeInit the low level hardware: CLOCK, NVIC.*/
  441. hrtc->MspDeInitCallback(hrtc);
  442. #else
  443. /* De-Initialize RTC MSP */
  444. HAL_RTC_MspDeInit(hrtc);
  445. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  446. hrtc->State = HAL_RTC_STATE_RESET;
  447. }
  448. /* Release Lock */
  449. __HAL_UNLOCK(hrtc);
  450. return status;
  451. }
  452. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  453. /**
  454. * @brief Register a User RTC Callback
  455. * To be used instead of the weak predefined callback
  456. * @param hrtc RTC handle
  457. * @param CallbackID ID of the callback to be registered
  458. * This parameter can be one of the following values:
  459. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  460. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  461. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  462. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  463. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  464. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  465. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  466. * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
  467. * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
  468. * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
  469. * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
  470. * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
  471. * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
  472. * @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID Internal Tamper 7 Callback ID
  473. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  474. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  475. * @param pCallback pointer to the Callback function
  476. * @retval HAL status
  477. */
  478. HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID,
  479. pRTC_CallbackTypeDef pCallback)
  480. {
  481. HAL_StatusTypeDef status = HAL_OK;
  482. if (pCallback == NULL)
  483. {
  484. return HAL_ERROR;
  485. }
  486. /* Process locked */
  487. __HAL_LOCK(hrtc);
  488. if (HAL_RTC_STATE_READY == hrtc->State)
  489. {
  490. switch (CallbackID)
  491. {
  492. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  493. hrtc->AlarmAEventCallback = pCallback;
  494. break;
  495. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  496. hrtc->AlarmBEventCallback = pCallback;
  497. break;
  498. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  499. hrtc->TimeStampEventCallback = pCallback;
  500. break;
  501. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  502. hrtc->WakeUpTimerEventCallback = pCallback;
  503. break;
  504. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  505. hrtc->Tamper1EventCallback = pCallback;
  506. break;
  507. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  508. hrtc->Tamper2EventCallback = pCallback;
  509. break;
  510. #if (RTC_TAMP_NB == 3)
  511. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  512. hrtc->Tamper3EventCallback = pCallback;
  513. break;
  514. #endif /* RTC_TAMP_NB */
  515. #ifdef RTC_TAMP_INT_1_SUPPORT
  516. case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
  517. hrtc->InternalTamper1EventCallback = pCallback;
  518. break;
  519. #endif /* RTC_TAMP_INT_1_SUPPORT */
  520. #ifdef RTC_TAMP_INT_2_SUPPORT
  521. case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
  522. hrtc->InternalTamper2EventCallback = pCallback;
  523. break;
  524. #endif /* RTC_TAMP_INT_2_SUPPORT */
  525. case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
  526. hrtc->InternalTamper3EventCallback = pCallback;
  527. break;
  528. case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
  529. hrtc->InternalTamper4EventCallback = pCallback;
  530. break;
  531. case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
  532. hrtc->InternalTamper5EventCallback = pCallback;
  533. break;
  534. #ifdef RTC_TAMP_INT_6_SUPPORT
  535. case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
  536. hrtc->InternalTamper6EventCallback = pCallback;
  537. break;
  538. #endif /* RTC_TAMP_INT_6_SUPPORT */
  539. #ifdef RTC_TAMP_INT_7_SUPPORT
  540. case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
  541. hrtc->InternalTamper7EventCallback = pCallback;
  542. break;
  543. #endif /* RTC_TAMP_INT_7_SUPPORT */
  544. case HAL_RTC_MSPINIT_CB_ID :
  545. hrtc->MspInitCallback = pCallback;
  546. break;
  547. case HAL_RTC_MSPDEINIT_CB_ID :
  548. hrtc->MspDeInitCallback = pCallback;
  549. break;
  550. default :
  551. /* Return error status */
  552. status = HAL_ERROR;
  553. break;
  554. }
  555. }
  556. else if (HAL_RTC_STATE_RESET == hrtc->State)
  557. {
  558. switch (CallbackID)
  559. {
  560. case HAL_RTC_MSPINIT_CB_ID :
  561. hrtc->MspInitCallback = pCallback;
  562. break;
  563. case HAL_RTC_MSPDEINIT_CB_ID :
  564. hrtc->MspDeInitCallback = pCallback;
  565. break;
  566. default :
  567. /* Return error status */
  568. status = HAL_ERROR;
  569. break;
  570. }
  571. }
  572. else
  573. {
  574. /* Return error status */
  575. status = HAL_ERROR;
  576. }
  577. /* Release Lock */
  578. __HAL_UNLOCK(hrtc);
  579. return status;
  580. }
  581. /**
  582. * @brief Unregister an RTC Callback
  583. * RTC callback is redirected to the weak predefined callback
  584. * @param hrtc RTC handle
  585. * @param CallbackID ID of the callback to be unregistered
  586. * This parameter can be one of the following values:
  587. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  588. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  589. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  590. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  591. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  592. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  593. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  594. * @arg @ref HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID Internal Tamper 1 Callback ID
  595. * @arg @ref HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID Internal Tamper 2 Callback ID
  596. * @arg @ref HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID Internal Tamper 3 Callback ID
  597. * @arg @ref HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID Internal Tamper 4 Callback ID
  598. * @arg @ref HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID Internal Tamper 5 Callback ID
  599. * @arg @ref HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID Internal Tamper 6 Callback ID
  600. * @arg @ref HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID Internal Tamper 7 Callback ID
  601. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  602. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  603. * @retval HAL status
  604. */
  605. HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
  606. {
  607. HAL_StatusTypeDef status = HAL_OK;
  608. /* Process locked */
  609. __HAL_LOCK(hrtc);
  610. if (HAL_RTC_STATE_READY == hrtc->State)
  611. {
  612. switch (CallbackID)
  613. {
  614. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  615. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  616. break;
  617. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  618. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  619. break;
  620. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  621. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  622. break;
  623. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  624. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  625. break;
  626. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  627. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  628. break;
  629. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  630. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  631. break;
  632. #if (RTC_TAMP_NB == 3)
  633. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  634. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  635. break;
  636. #endif /* RTC_TAMP_NB */
  637. #ifdef RTC_TAMP_INT_1_SUPPORT
  638. case HAL_RTC_INTERNAL_TAMPER1_EVENT_CB_ID :
  639. hrtc->InternalTamper1EventCallback = HAL_RTCEx_InternalTamper1EventCallback; /* Legacy weak InternalTamper1EventCallback */
  640. break;
  641. #endif /* RTC_TAMP_INT_1_SUPPORT */
  642. #ifdef RTC_TAMP_INT_2_SUPPORT
  643. case HAL_RTC_INTERNAL_TAMPER2_EVENT_CB_ID :
  644. hrtc->InternalTamper2EventCallback = HAL_RTCEx_InternalTamper2EventCallback; /* Legacy weak InternalTamper2EventCallback */
  645. break;
  646. #endif /* RTC_TAMP_INT_2_SUPPORT */
  647. case HAL_RTC_INTERNAL_TAMPER3_EVENT_CB_ID :
  648. hrtc->InternalTamper3EventCallback = HAL_RTCEx_InternalTamper3EventCallback; /* Legacy weak InternalTamper3EventCallback */
  649. break;
  650. case HAL_RTC_INTERNAL_TAMPER4_EVENT_CB_ID :
  651. hrtc->InternalTamper4EventCallback = HAL_RTCEx_InternalTamper4EventCallback; /* Legacy weak InternalTamper4EventCallback */
  652. break;
  653. case HAL_RTC_INTERNAL_TAMPER5_EVENT_CB_ID :
  654. hrtc->InternalTamper5EventCallback = HAL_RTCEx_InternalTamper5EventCallback; /* Legacy weak InternalTamper5EventCallback */
  655. break;
  656. #ifdef RTC_TAMP_INT_6_SUPPORT
  657. case HAL_RTC_INTERNAL_TAMPER6_EVENT_CB_ID :
  658. hrtc->InternalTamper6EventCallback = HAL_RTCEx_InternalTamper6EventCallback; /* Legacy weak InternalTamper6EventCallback */
  659. break;
  660. #endif /* RTC_TAMP_INT_6_SUPPORT */
  661. #ifdef RTC_TAMP_INT_7_SUPPORT
  662. case HAL_RTC_INTERNAL_TAMPER7_EVENT_CB_ID :
  663. hrtc->InternalTamper7EventCallback = HAL_RTCEx_InternalTamper7EventCallback; /* Legacy weak InternalTamper7EventCallback */
  664. break;
  665. #endif /* RTC_TAMP_INT_7_SUPPORT */
  666. case HAL_RTC_MSPINIT_CB_ID :
  667. hrtc->MspInitCallback = HAL_RTC_MspInit;
  668. break;
  669. case HAL_RTC_MSPDEINIT_CB_ID :
  670. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  671. break;
  672. default :
  673. /* Return error status */
  674. status = HAL_ERROR;
  675. break;
  676. }
  677. }
  678. else if (HAL_RTC_STATE_RESET == hrtc->State)
  679. {
  680. switch (CallbackID)
  681. {
  682. case HAL_RTC_MSPINIT_CB_ID :
  683. hrtc->MspInitCallback = HAL_RTC_MspInit;
  684. break;
  685. case HAL_RTC_MSPDEINIT_CB_ID :
  686. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  687. break;
  688. default :
  689. /* Return error status */
  690. status = HAL_ERROR;
  691. break;
  692. }
  693. }
  694. else
  695. {
  696. /* Return error status */
  697. status = HAL_ERROR;
  698. }
  699. /* Release Lock */
  700. __HAL_UNLOCK(hrtc);
  701. return status;
  702. }
  703. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  704. /**
  705. * @brief Initialize the RTC MSP.
  706. * @param hrtc RTC handle
  707. * @retval None
  708. */
  709. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
  710. {
  711. /* Prevent unused argument(s) compilation warning */
  712. UNUSED(hrtc);
  713. /* NOTE : This function should not be modified, when the callback is needed,
  714. the HAL_RTC_MspInit could be implemented in the user file
  715. */
  716. }
  717. /**
  718. * @brief DeInitialize the RTC MSP.
  719. * @param hrtc RTC handle
  720. * @retval None
  721. */
  722. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
  723. {
  724. /* Prevent unused argument(s) compilation warning */
  725. UNUSED(hrtc);
  726. /* NOTE : This function should not be modified, when the callback is needed,
  727. the HAL_RTC_MspDeInit could be implemented in the user file
  728. */
  729. }
  730. /**
  731. * @}
  732. */
  733. /** @addtogroup RTC_Exported_Functions_Group2
  734. * @brief RTC Time and Date functions
  735. *
  736. @verbatim
  737. ===============================================================================
  738. ##### RTC Time and Date functions #####
  739. ===============================================================================
  740. [..] This section provides functions allowing to configure Time and Date features
  741. @endverbatim
  742. * @{
  743. */
  744. /**
  745. * @brief Set RTC current time.
  746. * @param hrtc RTC handle
  747. * @param sTime Pointer to Time structure
  748. * @param Format Specifies the format of the entered parameters.
  749. * This parameter can be one of the following values:
  750. * @arg RTC_FORMAT_BIN: Binary data format
  751. * @arg RTC_FORMAT_BCD: BCD data format
  752. * @retval HAL status
  753. */
  754. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  755. {
  756. uint32_t tmpreg;
  757. HAL_StatusTypeDef status;
  758. /* Check the parameters */
  759. assert_param(IS_RTC_FORMAT(Format));
  760. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  761. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  762. /* Process Locked */
  763. __HAL_LOCK(hrtc);
  764. hrtc->State = HAL_RTC_STATE_BUSY;
  765. /* Disable the write protection for RTC registers */
  766. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  767. /* Enter Initialization mode */
  768. status = RTC_EnterInitMode(hrtc);
  769. if (status == HAL_OK)
  770. {
  771. if (Format == RTC_FORMAT_BIN)
  772. {
  773. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  774. {
  775. assert_param(IS_RTC_HOUR12(sTime->Hours));
  776. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  777. }
  778. else
  779. {
  780. sTime->TimeFormat = 0x00U;
  781. assert_param(IS_RTC_HOUR24(sTime->Hours));
  782. }
  783. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  784. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  785. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
  786. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  787. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
  788. (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
  789. }
  790. else
  791. {
  792. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  793. {
  794. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
  795. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  796. }
  797. else
  798. {
  799. sTime->TimeFormat = 0x00U;
  800. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  801. }
  802. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  803. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  804. tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
  805. ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  806. ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
  807. ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
  808. }
  809. /* Set the RTC_TR register */
  810. WRITE_REG(hrtc->Instance->TR, (tmpreg & RTC_TR_RESERVED_MASK));
  811. /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
  812. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  813. /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
  814. SET_BIT(hrtc->Instance->CR, (sTime->DayLightSaving | sTime->StoreOperation));
  815. /* Exit Initialization mode */
  816. status = RTC_ExitInitMode(hrtc);
  817. }
  818. /* Enable the write protection for RTC registers */
  819. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  820. if (status == HAL_OK)
  821. {
  822. hrtc->State = HAL_RTC_STATE_READY;
  823. }
  824. __HAL_UNLOCK(hrtc);
  825. return status;
  826. }
  827. /**
  828. * @brief Get RTC current time.
  829. * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  830. * value in second fraction ratio with time unit following generic formula:
  831. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  832. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  833. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  834. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  835. * Reading RTC current time locks the values in calendar shadow registers until Current date is read
  836. * to ensure consistency between the time and date values.
  837. * @param hrtc RTC handle
  838. * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
  839. * with input format (BIN or BCD), also SubSeconds field returning the
  840. * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  841. * factor to be used for second fraction ratio computation.
  842. * @param Format Specifies the format of the entered parameters.
  843. * This parameter can be one of the following values:
  844. * @arg RTC_FORMAT_BIN: Binary data format
  845. * @arg RTC_FORMAT_BCD: BCD data format
  846. * @retval HAL status
  847. */
  848. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  849. {
  850. uint32_t tmpreg;
  851. /* Check the parameters */
  852. assert_param(IS_RTC_FORMAT(Format));
  853. /* Get subseconds structure field from the corresponding register*/
  854. sTime->SubSeconds = READ_REG(hrtc->Instance->SSR);
  855. /* Get SecondFraction structure field from the corresponding register field*/
  856. sTime->SecondFraction = (uint32_t)(READ_REG(hrtc->Instance->PRER) & RTC_PRER_PREDIV_S);
  857. /* Get the TR register */
  858. tmpreg = (uint32_t)(READ_REG(hrtc->Instance->TR) & RTC_TR_RESERVED_MASK);
  859. /* Fill the structure fields with the read parameters */
  860. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
  861. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
  862. sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
  863. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
  864. /* Check the input parameters format */
  865. if (Format == RTC_FORMAT_BIN)
  866. {
  867. /* Convert the time structure parameters to Binary format */
  868. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  869. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  870. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  871. }
  872. return HAL_OK;
  873. }
  874. /**
  875. * @brief Set RTC current date.
  876. * @param hrtc RTC handle
  877. * @param sDate Pointer to date structure
  878. * @param Format specifies the format of the entered parameters.
  879. * This parameter can be one of the following values:
  880. * @arg RTC_FORMAT_BIN: Binary data format
  881. * @arg RTC_FORMAT_BCD: BCD data format
  882. * @retval HAL status
  883. */
  884. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  885. {
  886. uint32_t datetmpreg;
  887. HAL_StatusTypeDef status;
  888. /* Check the parameters */
  889. assert_param(IS_RTC_FORMAT(Format));
  890. /* Process Locked */
  891. __HAL_LOCK(hrtc);
  892. hrtc->State = HAL_RTC_STATE_BUSY;
  893. if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  894. {
  895. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  896. }
  897. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  898. if (Format == RTC_FORMAT_BIN)
  899. {
  900. assert_param(IS_RTC_YEAR(sDate->Year));
  901. assert_param(IS_RTC_MONTH(sDate->Month));
  902. assert_param(IS_RTC_DATE(sDate->Date));
  903. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
  904. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
  905. ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
  906. ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
  907. }
  908. else
  909. {
  910. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  911. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  912. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  913. datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
  914. (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
  915. (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
  916. (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
  917. }
  918. /* Disable the write protection for RTC registers */
  919. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  920. /* Enter Initialization mode */
  921. status = RTC_EnterInitMode(hrtc);
  922. if (status == HAL_OK)
  923. {
  924. /* Set the RTC_DR register */
  925. WRITE_REG(hrtc->Instance->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
  926. /* Exit Initialization mode */
  927. status = RTC_ExitInitMode(hrtc);
  928. }
  929. /* Enable the write protection for RTC registers */
  930. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  931. if (status == HAL_OK)
  932. {
  933. hrtc->State = HAL_RTC_STATE_READY ;
  934. }
  935. /* Process Unlocked */
  936. __HAL_UNLOCK(hrtc);
  937. return status;
  938. }
  939. /**
  940. * @brief Get RTC current date.
  941. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  942. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  943. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  944. * @param hrtc RTC handle
  945. * @param sDate Pointer to Date structure
  946. * @param Format Specifies the format of the entered parameters.
  947. * This parameter can be one of the following values:
  948. * @arg RTC_FORMAT_BIN: Binary data format
  949. * @arg RTC_FORMAT_BCD: BCD data format
  950. * @retval HAL status
  951. */
  952. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  953. {
  954. uint32_t datetmpreg;
  955. /* Check the parameters */
  956. assert_param(IS_RTC_FORMAT(Format));
  957. /* Get the DR register */
  958. datetmpreg = (uint32_t)(READ_REG(hrtc->Instance->DR) & RTC_DR_RESERVED_MASK);
  959. /* Fill the structure fields with the read parameters */
  960. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
  961. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
  962. sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
  963. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
  964. /* Check the input parameters format */
  965. if (Format == RTC_FORMAT_BIN)
  966. {
  967. /* Convert the date structure parameters to Binary format */
  968. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  969. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  970. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  971. }
  972. return HAL_OK;
  973. }
  974. /**
  975. * @}
  976. */
  977. /** @addtogroup RTC_Exported_Functions_Group3
  978. * @brief RTC Alarm functions
  979. *
  980. @verbatim
  981. ===============================================================================
  982. ##### RTC Alarm functions #####
  983. ===============================================================================
  984. [..] This section provides functions allowing to configure Alarm feature
  985. @endverbatim
  986. * @{
  987. */
  988. /**
  989. * @brief Set the specified RTC Alarm.
  990. * @param hrtc RTC handle
  991. * @param sAlarm Pointer to Alarm structure
  992. * @param Format Specifies the format of the entered parameters.
  993. * This parameter can be one of the following values:
  994. * @arg RTC_FORMAT_BIN: Binary data format
  995. * @arg RTC_FORMAT_BCD: BCD data format
  996. * @retval HAL status
  997. */
  998. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  999. {
  1000. uint32_t tickstart;
  1001. uint32_t tmpreg;
  1002. uint32_t subsecondtmpreg;
  1003. /* Check the parameters */
  1004. assert_param(IS_RTC_FORMAT(Format));
  1005. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1006. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1007. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1008. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1009. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1010. /* Process Locked */
  1011. __HAL_LOCK(hrtc);
  1012. hrtc->State = HAL_RTC_STATE_BUSY;
  1013. if (Format == RTC_FORMAT_BIN)
  1014. {
  1015. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  1016. {
  1017. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1018. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1019. }
  1020. else
  1021. {
  1022. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1023. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1024. }
  1025. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1026. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1027. #ifdef USE_FULL_ASSERT
  1028. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1029. {
  1030. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1031. }
  1032. else
  1033. {
  1034. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1035. }
  1036. #endif /* USE_FULL_ASSERT*/
  1037. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1038. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1039. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1040. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1041. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1042. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1043. ((uint32_t)sAlarm->AlarmMask));
  1044. }
  1045. else /* Format BCD */
  1046. {
  1047. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  1048. {
  1049. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1050. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1051. }
  1052. else
  1053. {
  1054. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1055. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1056. }
  1057. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1058. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1059. #ifdef USE_FULL_ASSERT
  1060. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1061. {
  1062. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1063. }
  1064. else
  1065. {
  1066. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1067. }
  1068. #endif /* USE_FULL_ASSERT */
  1069. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1070. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1071. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1072. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1073. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1074. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1075. ((uint32_t)sAlarm->AlarmMask));
  1076. }
  1077. /* Configure the Alarm A or Alarm B Sub Second registers */
  1078. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1079. /* Disable the write protection for RTC registers */
  1080. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1081. /* Configure the Alarm register */
  1082. if (sAlarm->Alarm == RTC_ALARM_A)
  1083. {
  1084. /* Disable the Alarm A interrupt */
  1085. /* In case of interrupt mode is used, the interrupt source must disabled */
  1086. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
  1087. /* Clear flag alarm A */
  1088. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRAF);
  1089. tickstart = HAL_GetTick();
  1090. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1091. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRAWF) == 0U)
  1092. {
  1093. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1094. {
  1095. /* Enable the write protection for RTC registers */
  1096. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1097. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1098. /* Process Unlocked */
  1099. __HAL_UNLOCK(hrtc);
  1100. return HAL_TIMEOUT;
  1101. }
  1102. }
  1103. WRITE_REG(hrtc->Instance->ALRMAR, tmpreg);
  1104. /* Configure the Alarm A Sub Second register */
  1105. WRITE_REG(hrtc->Instance->ALRMASSR, subsecondtmpreg);
  1106. /* Configure the Alarm state: Enable Alarm */
  1107. SET_BIT(hrtc->Instance->CR, RTC_CR_ALRAE);
  1108. }
  1109. else
  1110. {
  1111. /* Disable the Alarm B interrupt */
  1112. /* In case of interrupt mode is used, the interrupt source must disabled */
  1113. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
  1114. /* Clear flag alarm B */
  1115. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRBF);
  1116. tickstart = HAL_GetTick();
  1117. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1118. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRBWF) == 0U)
  1119. {
  1120. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1121. {
  1122. /* Enable the write protection for RTC registers */
  1123. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1124. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1125. /* Process Unlocked */
  1126. __HAL_UNLOCK(hrtc);
  1127. return HAL_TIMEOUT;
  1128. }
  1129. }
  1130. WRITE_REG(hrtc->Instance->ALRMBR, tmpreg);
  1131. /* Configure the Alarm B Sub Second register */
  1132. WRITE_REG(hrtc->Instance->ALRMBSSR, subsecondtmpreg);
  1133. /* Configure the Alarm state: Enable Alarm */
  1134. SET_BIT(hrtc->Instance->CR, RTC_CR_ALRBE);
  1135. }
  1136. /* Enable the write protection for RTC registers */
  1137. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1138. /* Change RTC state */
  1139. hrtc->State = HAL_RTC_STATE_READY;
  1140. /* Process Unlocked */
  1141. __HAL_UNLOCK(hrtc);
  1142. return HAL_OK;
  1143. }
  1144. /**
  1145. * @brief Set the specified RTC Alarm with Interrupt.
  1146. * @note The Alarm register can only be written when the corresponding Alarm
  1147. * is disabled (Use the HAL_RTC_DeactivateAlarm()).
  1148. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  1149. * @param hrtc RTC handle
  1150. * @param sAlarm Pointer to Alarm structure
  1151. * @param Format Specifies the format of the entered parameters.
  1152. * This parameter can be one of the following values:
  1153. * @arg RTC_FORMAT_BIN: Binary data format
  1154. * @arg RTC_FORMAT_BCD: BCD data format
  1155. * @retval HAL status
  1156. */
  1157. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1158. {
  1159. uint32_t tickstart;
  1160. uint32_t tmpreg;
  1161. uint32_t subsecondtmpreg;
  1162. /* Check the parameters */
  1163. assert_param(IS_RTC_FORMAT(Format));
  1164. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1165. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1166. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1167. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1168. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1169. /* Process Locked */
  1170. __HAL_LOCK(hrtc);
  1171. hrtc->State = HAL_RTC_STATE_BUSY;
  1172. if (Format == RTC_FORMAT_BIN)
  1173. {
  1174. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  1175. {
  1176. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1177. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1178. }
  1179. else
  1180. {
  1181. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1182. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1183. }
  1184. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1185. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1186. #ifdef USE_FULL_ASSERT
  1187. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1188. {
  1189. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1190. }
  1191. else
  1192. {
  1193. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1194. }
  1195. #endif /* USE_FULL_ASSERT */
  1196. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1197. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1198. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1199. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1200. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1201. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1202. ((uint32_t)sAlarm->AlarmMask));
  1203. }
  1204. else /* Format BCD */
  1205. {
  1206. if (READ_BIT(hrtc->Instance->CR, RTC_CR_FMT) != 0U)
  1207. {
  1208. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1209. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1210. }
  1211. else
  1212. {
  1213. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1214. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1215. }
  1216. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1217. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1218. #ifdef USE_FULL_ASSERT
  1219. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1220. {
  1221. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1222. }
  1223. else
  1224. {
  1225. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1226. }
  1227. #endif /* USE_FULL_ASSERT */
  1228. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1229. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1230. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1231. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1232. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1233. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1234. ((uint32_t)sAlarm->AlarmMask));
  1235. }
  1236. /* Configure the Alarm A or Alarm B Sub Second registers */
  1237. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1238. /* Disable the write protection for RTC registers */
  1239. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1240. /* Configure the Alarm register */
  1241. if (sAlarm->Alarm == RTC_ALARM_A)
  1242. {
  1243. /* Disable the Alarm A interrupt */
  1244. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
  1245. /* Clear flag alarm A */
  1246. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRAF);
  1247. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1248. tickstart = HAL_GetTick();
  1249. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1250. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRAWF) == 0U)
  1251. {
  1252. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1253. {
  1254. /* Enable the write protection for RTC registers */
  1255. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1256. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1257. /* Process Unlocked */
  1258. __HAL_UNLOCK(hrtc);
  1259. return HAL_TIMEOUT;
  1260. }
  1261. }
  1262. WRITE_REG(hrtc->Instance->ALRMAR, tmpreg);
  1263. /* Configure the Alarm A Sub Second register */
  1264. WRITE_REG(hrtc->Instance->ALRMASSR, subsecondtmpreg);
  1265. /* Configure the Alarm interrupt : Enable Alarm */
  1266. SET_BIT(hrtc->Instance->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
  1267. }
  1268. else
  1269. {
  1270. /* Disable the Alarm B interrupt */
  1271. CLEAR_BIT(hrtc->Instance->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
  1272. /* Clear flag alarm B */
  1273. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRBF);
  1274. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1275. tickstart = HAL_GetTick();
  1276. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1277. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRBWF) == 0U)
  1278. {
  1279. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1280. {
  1281. /* Enable the write protection for RTC registers */
  1282. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1283. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1284. /* Process Unlocked */
  1285. __HAL_UNLOCK(hrtc);
  1286. return HAL_TIMEOUT;
  1287. }
  1288. }
  1289. WRITE_REG(hrtc->Instance->ALRMBR, tmpreg);
  1290. /* Configure the Alarm B Sub Second register */
  1291. WRITE_REG(hrtc->Instance->ALRMBSSR, subsecondtmpreg);
  1292. /* Configure the Alarm B interrupt : Enable Alarm */
  1293. SET_BIT(hrtc->Instance->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
  1294. }
  1295. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1296. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1297. __HAL_RTC_ALARM_EXTI_RISING_IT();
  1298. /* Enable the write protection for RTC registers */
  1299. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1300. /* Change RTC state */
  1301. hrtc->State = HAL_RTC_STATE_READY;
  1302. /* Process Unlocked */
  1303. __HAL_UNLOCK(hrtc);
  1304. return HAL_OK;
  1305. }
  1306. /**
  1307. * @brief Deactivate the specified RTC Alarm.
  1308. * @param hrtc RTC handle
  1309. * @param Alarm Specifies the Alarm.
  1310. * This parameter can be one of the following values:
  1311. * @arg RTC_ALARM_A: AlarmA
  1312. * @arg RTC_ALARM_B: AlarmB
  1313. * @retval HAL status
  1314. */
  1315. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  1316. {
  1317. uint32_t tickstart;
  1318. /* Check the parameters */
  1319. assert_param(IS_RTC_ALARM(Alarm));
  1320. /* Process Locked */
  1321. __HAL_LOCK(hrtc);
  1322. hrtc->State = HAL_RTC_STATE_BUSY;
  1323. /* Disable the write protection for RTC registers */
  1324. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1325. if (Alarm == RTC_ALARM_A)
  1326. {
  1327. /* AlarmA */
  1328. /* In case of interrupt mode is used, the interrupt source must disabled */
  1329. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
  1330. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1331. tickstart = HAL_GetTick();
  1332. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1333. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRAWF) == 0U)
  1334. {
  1335. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1336. {
  1337. /* Enable the write protection for RTC registers */
  1338. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1339. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1340. /* Process Unlocked */
  1341. __HAL_UNLOCK(hrtc);
  1342. return HAL_TIMEOUT;
  1343. }
  1344. }
  1345. }
  1346. else
  1347. {
  1348. /* AlarmB */
  1349. /* In case of interrupt mode is used, the interrupt source must disabled */
  1350. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
  1351. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1352. tickstart = HAL_GetTick();
  1353. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1354. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_ALRBWF) == 0U)
  1355. {
  1356. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1357. {
  1358. /* Enable the write protection for RTC registers */
  1359. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1360. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1361. /* Process Unlocked */
  1362. __HAL_UNLOCK(hrtc);
  1363. return HAL_TIMEOUT;
  1364. }
  1365. }
  1366. }
  1367. /* Enable the write protection for RTC registers */
  1368. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1369. hrtc->State = HAL_RTC_STATE_READY;
  1370. /* Process Unlocked */
  1371. __HAL_UNLOCK(hrtc);
  1372. return HAL_OK;
  1373. }
  1374. /**
  1375. * @brief Get the RTC Alarm value and masks.
  1376. * @param hrtc RTC handle
  1377. * @param sAlarm Pointer to Date structure
  1378. * @param Alarm Specifies the Alarm.
  1379. * This parameter can be one of the following values:
  1380. * @arg RTC_ALARM_A: AlarmA
  1381. * @arg RTC_ALARM_B: AlarmB
  1382. * @param Format Specifies the format of the entered parameters.
  1383. * This parameter can be one of the following values:
  1384. * @arg RTC_FORMAT_BIN: Binary data format
  1385. * @arg RTC_FORMAT_BCD: BCD data format
  1386. * @retval HAL status
  1387. */
  1388. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1389. {
  1390. uint32_t tmpreg, subsecondtmpreg;
  1391. /* Check the parameters */
  1392. assert_param(IS_RTC_FORMAT(Format));
  1393. assert_param(IS_RTC_ALARM(Alarm));
  1394. if (Alarm == RTC_ALARM_A)
  1395. {
  1396. /* AlarmA */
  1397. sAlarm->Alarm = RTC_ALARM_A;
  1398. tmpreg = READ_REG(hrtc->Instance->ALRMAR);
  1399. subsecondtmpreg = (uint32_t)(READ_REG(hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
  1400. /* Fill the structure with the read parameters */
  1401. sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
  1402. sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
  1403. sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
  1404. sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
  1405. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1406. sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
  1407. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1408. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1409. }
  1410. else
  1411. {
  1412. sAlarm->Alarm = RTC_ALARM_B;
  1413. tmpreg = READ_REG(hrtc->Instance->ALRMBR);
  1414. subsecondtmpreg = (uint32_t)(READ_REG(hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  1415. /* Fill the structure with the read parameters */
  1416. sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
  1417. sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
  1418. sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
  1419. sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
  1420. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1421. sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
  1422. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
  1423. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1424. }
  1425. if (Format == RTC_FORMAT_BIN)
  1426. {
  1427. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  1428. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  1429. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  1430. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  1431. }
  1432. return HAL_OK;
  1433. }
  1434. /**
  1435. * @brief Handle Alarm interrupt request.
  1436. * @param hrtc RTC handle
  1437. * @retval None
  1438. */
  1439. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
  1440. {
  1441. /* Get interrupt status */
  1442. uint32_t tmp = READ_REG(hrtc->Instance->MISR);
  1443. if ((tmp & RTC_MISR_ALRAMF) != 0U)
  1444. {
  1445. /* Clear the AlarmA interrupt pending bit */
  1446. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRAF);
  1447. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1448. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1449. /* Call Compare Match registered Callback */
  1450. hrtc->AlarmAEventCallback(hrtc);
  1451. #else
  1452. HAL_RTC_AlarmAEventCallback(hrtc);
  1453. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1454. }
  1455. if ((tmp & RTC_MISR_ALRBMF) != 0U)
  1456. {
  1457. /* Clear the AlarmB interrupt pending bit */
  1458. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRBF);
  1459. __HAL_RTC_ALARM_EXTI_CLEAR_IT();
  1460. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1461. /* Call Compare Match registered Callback */
  1462. hrtc->AlarmBEventCallback(hrtc);
  1463. #else
  1464. HAL_RTCEx_AlarmBEventCallback(hrtc);
  1465. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1466. }
  1467. /* Change RTC state */
  1468. hrtc->State = HAL_RTC_STATE_READY;
  1469. }
  1470. /**
  1471. * @brief Alarm A callback.
  1472. * @param hrtc RTC handle
  1473. * @retval None
  1474. */
  1475. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1476. {
  1477. /* Prevent unused argument(s) compilation warning */
  1478. UNUSED(hrtc);
  1479. /* NOTE : This function should not be modified, when the callback is needed,
  1480. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1481. */
  1482. }
  1483. /**
  1484. * @brief Handle AlarmA Polling request.
  1485. * @param hrtc RTC handle
  1486. * @param Timeout Timeout duration
  1487. * @retval HAL status
  1488. */
  1489. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1490. {
  1491. uint32_t tickstart = HAL_GetTick();
  1492. while (READ_BIT(hrtc->Instance->SR, RTC_SR_ALRAF) == 0U)
  1493. {
  1494. if (Timeout != HAL_MAX_DELAY)
  1495. {
  1496. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1497. {
  1498. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1499. /* Process Unlocked */
  1500. __HAL_UNLOCK(hrtc);
  1501. return HAL_TIMEOUT;
  1502. }
  1503. }
  1504. }
  1505. /* Clear the Alarm interrupt pending bit */
  1506. WRITE_REG(hrtc->Instance->SCR, RTC_SCR_CALRAF);
  1507. /* Change RTC state */
  1508. hrtc->State = HAL_RTC_STATE_READY;
  1509. return HAL_OK;
  1510. }
  1511. /**
  1512. * @}
  1513. */
  1514. /** @addtogroup RTC_Exported_Functions_Group4
  1515. * @brief Peripheral Control functions
  1516. *
  1517. @verbatim
  1518. ===============================================================================
  1519. ##### Peripheral Control functions #####
  1520. ===============================================================================
  1521. [..]
  1522. This subsection provides functions allowing to
  1523. (+) Wait for RTC Time and Date Synchronization
  1524. @endverbatim
  1525. * @{
  1526. */
  1527. /**
  1528. * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  1529. * synchronized with RTC APB clock.
  1530. * @note The RTC Resynchronization mode is write protected, use the
  1531. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1532. * @note To read the calendar through the shadow registers after Calendar
  1533. * initialization, calendar update or after wakeup from low power modes
  1534. * the software must first clear the RSF flag.
  1535. * The software must then wait until it is set again before reading
  1536. * the calendar, which means that the calendar registers have been
  1537. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  1538. * @param hrtc RTC handle
  1539. * @retval HAL status
  1540. */
  1541. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
  1542. {
  1543. uint32_t tickstart;
  1544. /* Clear RSF flag, keep reserved bits at reset values (setting other flags has no effect) */
  1545. hrtc->Instance->ICSR = ((uint32_t)(RTC_RSF_MASK & RTC_ICSR_RESERVED_MASK));
  1546. tickstart = HAL_GetTick();
  1547. /* Wait the registers to be synchronised */
  1548. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_RSF) == 0U)
  1549. {
  1550. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1551. {
  1552. return HAL_TIMEOUT;
  1553. }
  1554. }
  1555. return HAL_OK;
  1556. }
  1557. /**
  1558. * @}
  1559. */
  1560. /** @addtogroup RTC_Exported_Functions_Group5
  1561. * @brief Peripheral State functions
  1562. *
  1563. @verbatim
  1564. ===============================================================================
  1565. ##### Peripheral State functions #####
  1566. ===============================================================================
  1567. [..]
  1568. This subsection provides functions allowing to
  1569. (+) Get RTC state
  1570. @endverbatim
  1571. * @{
  1572. */
  1573. /**
  1574. * @brief Return the RTC handle state.
  1575. * @param hrtc RTC handle
  1576. * @retval HAL state
  1577. */
  1578. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
  1579. {
  1580. /* Return RTC handle state */
  1581. return hrtc->State;
  1582. }
  1583. /**
  1584. * @}
  1585. */
  1586. /**
  1587. * @}
  1588. */
  1589. /** @addtogroup RTC_Private_Functions
  1590. * @{
  1591. */
  1592. /**
  1593. * @brief Enter the RTC Initialization mode.
  1594. * @note The RTC Initialization mode is write protected, use the
  1595. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1596. * @param hrtc RTC handle
  1597. * @retval HAL status
  1598. */
  1599. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
  1600. {
  1601. uint32_t tickstart;
  1602. HAL_StatusTypeDef status = HAL_OK;
  1603. /* Check if the Initialization mode is set */
  1604. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
  1605. {
  1606. /* Set the Initialization mode */
  1607. SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
  1608. tickstart = HAL_GetTick();
  1609. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1610. while ((READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
  1611. {
  1612. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1613. {
  1614. status = HAL_TIMEOUT;
  1615. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1616. }
  1617. }
  1618. }
  1619. return status;
  1620. }
  1621. /**
  1622. * @brief Exit the RTC Initialization mode.
  1623. * @param hrtc RTC handle
  1624. * @retval HAL status
  1625. */
  1626. HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
  1627. {
  1628. HAL_StatusTypeDef status = HAL_OK;
  1629. /* Exit Initialization mode */
  1630. CLEAR_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
  1631. /* If CR_BYPSHAD bit = 0, wait for synchro */
  1632. if (READ_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD) == 0U)
  1633. {
  1634. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1635. {
  1636. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1637. status = HAL_TIMEOUT;
  1638. }
  1639. }
  1640. else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry */
  1641. {
  1642. /* Clear BYPSHAD bit */
  1643. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
  1644. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1645. {
  1646. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1647. status = HAL_TIMEOUT;
  1648. }
  1649. /* Restore BYPSHAD bit */
  1650. SET_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
  1651. }
  1652. return status;
  1653. }
  1654. /**
  1655. * @brief Convert a 2 digit decimal to BCD format.
  1656. * @param Value Byte to be converted
  1657. * @retval Converted byte
  1658. */
  1659. uint8_t RTC_ByteToBcd2(uint8_t Value)
  1660. {
  1661. uint32_t bcdhigh = 0U;
  1662. uint8_t tmp_Value = Value;
  1663. while (tmp_Value >= 10U)
  1664. {
  1665. bcdhigh++;
  1666. tmp_Value -= 10U;
  1667. }
  1668. return ((uint8_t)(bcdhigh << 4U) | tmp_Value);
  1669. }
  1670. /**
  1671. * @brief Convert from 2 digit BCD to Binary.
  1672. * @param Value BCD value to be converted
  1673. * @retval Converted word
  1674. */
  1675. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1676. {
  1677. uint32_t tmp;
  1678. tmp = (((uint32_t)Value & 0xF0U) >> 4) * 10U;
  1679. return (uint8_t)(tmp + ((uint32_t)Value & 0x0FU));
  1680. }
  1681. /**
  1682. * @brief Daylight Saving Time, Add one hour to the calendar in one single operation
  1683. * without going through the initialization procedure.
  1684. * @param hrtc RTC handle
  1685. * @retval None
  1686. */
  1687. void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc)
  1688. {
  1689. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1690. SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H);
  1691. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1692. }
  1693. /**
  1694. * @brief Daylight Saving Time, Subtract one hour from the calendar in one
  1695. * single operation without going through the initialization procedure.
  1696. * @param hrtc RTC handle
  1697. * @retval None
  1698. */
  1699. void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
  1700. {
  1701. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1702. SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H);
  1703. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1704. }
  1705. /**
  1706. * @brief Daylight Saving Time, Set the store operation bit.
  1707. * @note It can be used by the software in order to memorize the DST status.
  1708. * @param hrtc RTC handle
  1709. * @retval None
  1710. */
  1711. void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
  1712. {
  1713. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1714. SET_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1715. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1716. }
  1717. /**
  1718. * @brief Daylight Saving Time, Clear the store operation bit.
  1719. * @param hrtc RTC handle
  1720. * @retval None
  1721. */
  1722. void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
  1723. {
  1724. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1725. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1726. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1727. }
  1728. /**
  1729. * @brief Daylight Saving Time, Read the store operation bit.
  1730. * @param hrtc RTC handle
  1731. * @retval operation see RTC_StoreOperation_Definitions
  1732. */
  1733. uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
  1734. {
  1735. return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1736. }
  1737. /**
  1738. * @}
  1739. */
  1740. #endif /* HAL_RTC_MODULE_ENABLED */
  1741. /**
  1742. * @}
  1743. */
  1744. /**
  1745. * @}
  1746. */