stm32f1xx_hal_rtc.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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 and de-initialization functions
  9. * + RTC Time and Date functions
  10. * + RTC Alarm functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 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 (see description in the section above).
  31. (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
  32. using the HAL_RTC_Init() function.
  33. *** Time and Date configuration ***
  34. ===================================
  35. [..]
  36. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  37. and HAL_RTC_SetDate() functions.
  38. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  39. *** Alarm configuration ***
  40. ===========================
  41. [..]
  42. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  43. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  44. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  45. *** Tamper configuration ***
  46. ============================
  47. [..]
  48. (+) Enable the RTC Tamper and configure the Tamper Level using the
  49. HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
  50. mode using HAL_RTCEx_SetTamper_IT() function.
  51. (+) The TAMPER1 alternate function can be mapped to PC13
  52. *** Backup Data Registers configuration ***
  53. ===========================================
  54. [..]
  55. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  56. function.
  57. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  58. function.
  59. ##### WARNING: Drivers Restrictions #####
  60. ==================================================================
  61. [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
  62. (other families) will be not supported on F1.
  63. [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
  64. managed by SW.
  65. [..] Then, there are some restrictions compared to other families:
  66. (+) Only format 24 hours supported in HAL (format 12 hours not supported)
  67. (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
  68. User should implement a way to save date before entering in low power mode (an
  69. example is provided with firmware package based on backup registers)
  70. (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
  71. (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
  72. to program a new alarm)
  73. ##### Backup Domain Operating Condition #####
  74. ==============================================================================
  75. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  76. from the VBAT voltage when the main VDD supply is powered off.
  77. To retain the content of the RTC backup registers and supply the RTC
  78. when VDD is turned off, VBAT pin can be connected to an optional
  79. standby voltage supplied by a battery or by another source.
  80. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  81. off, the VBAT pin powers the following blocks:
  82. (#) The RTC
  83. (#) The LSE oscillator
  84. (#) The backup SRAM when the low power backup regulator is enabled
  85. (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
  86. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  87. the following pins are available:
  88. (+) PC13 can be used as a Tamper pin
  89. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  90. because VDD is not present), the following pins are available:
  91. (+) PC13 can be used as the Tamper pin
  92. ##### Backup Domain Reset #####
  93. ==================================================================
  94. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  95. to their reset values.
  96. [..] A backup domain reset is generated when one of the following events occurs:
  97. (#) Software reset, triggered by setting the BDRST bit in the
  98. RCC Backup domain control register (RCC_BDCR).
  99. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  100. (#) Tamper detection event resets all data backup registers.
  101. ##### Backup Domain Access #####
  102. ==================================================================
  103. [..] After reset, the backup domain (RTC registers, RTC backup data
  104. registers and backup SRAM) is protected against possible unwanted write
  105. accesses.
  106. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  107. (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
  108. PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
  109. (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
  110. ##### RTC and low power modes #####
  111. ==================================================================
  112. [..] The MCU can be woken up from a low power mode by an RTC alternate
  113. function.
  114. [..] The RTC alternate functions are the RTC alarms (Alarm A),
  115. and RTC tamper event detection.
  116. These RTC alternate functions can wake up the system from the Stop and
  117. Standby low power modes.
  118. [..] The system can also wake up from low power modes without depending
  119. on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
  120. *** Callback registration ***
  121. =============================================
  122. [..]
  123. The compilation define USE_HAL_RTC_REGISTER_CALLBACKS when set to 1
  124. allows the user to configure dynamically the driver callbacks.
  125. Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
  126. [..]
  127. Function HAL_RTC_RegisterCallback() allows to register following callbacks:
  128. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  129. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  130. (+) MspInitCallback : RTC MspInit callback.
  131. (+) MspDeInitCallback : RTC MspDeInit callback.
  132. [..]
  133. This function takes as parameters the HAL peripheral handle, the Callback ID
  134. and a pointer to the user callback function.
  135. [..]
  136. Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
  137. weak function.
  138. HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  139. and the Callback ID.
  140. This function allows to reset following callbacks:
  141. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  142. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  143. (+) MspInitCallback : RTC MspInit callback.
  144. (+) MspDeInitCallback : RTC MspDeInit callback.
  145. [..]
  146. By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  147. all callbacks are set to the corresponding weak functions :
  148. example AlarmAEventCallback().
  149. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  150. in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
  151. (not registered beforehand).
  152. If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
  153. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  154. [..]
  155. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  156. Exception done MspInit/MspDeInit that can be registered/unregistered
  157. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  158. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  159. In that case first register the MspInit/MspDeInit user callbacks
  160. using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
  161. or HAL_RTC_Init() function.
  162. [..]
  163. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  164. not defined, the callback registration feature is not available and all callbacks
  165. are set to the corresponding weak functions.
  166. @endverbatim
  167. ******************************************************************************
  168. */
  169. /* Includes ------------------------------------------------------------------*/
  170. #include "stm32f1xx_hal.h"
  171. /** @addtogroup STM32F1xx_HAL_Driver
  172. * @{
  173. */
  174. /** @defgroup RTC RTC
  175. * @brief RTC HAL module driver
  176. * @{
  177. */
  178. #ifdef HAL_RTC_MODULE_ENABLED
  179. /* Private typedef -----------------------------------------------------------*/
  180. /* Private define ------------------------------------------------------------*/
  181. /** @defgroup RTC_Private_Constants RTC Private Constants
  182. * @{
  183. */
  184. #define RTC_ALARM_RESETVALUE_REGISTER (uint16_t)0xFFFF
  185. #define RTC_ALARM_RESETVALUE 0xFFFFFFFFU
  186. /**
  187. * @}
  188. */
  189. /* Private macro -------------------------------------------------------------*/
  190. /** @defgroup RTC_Private_Macros RTC Private Macros
  191. * @{
  192. */
  193. /**
  194. * @}
  195. */
  196. /* Private variables ---------------------------------------------------------*/
  197. /* Private function prototypes -----------------------------------------------*/
  198. /** @defgroup RTC_Private_Functions RTC Private Functions
  199. * @{
  200. */
  201. static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc);
  202. static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef *hrtc, uint32_t TimeCounter);
  203. static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef *hrtc);
  204. static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef *hrtc, uint32_t AlarmCounter);
  205. static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc);
  206. static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc);
  207. static uint8_t RTC_ByteToBcd2(uint8_t Value);
  208. static uint8_t RTC_Bcd2ToByte(uint8_t Value);
  209. static uint8_t RTC_IsLeapYear(uint16_t nYear);
  210. static void RTC_DateUpdate(RTC_HandleTypeDef *hrtc, uint32_t DayElapsed);
  211. static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay);
  212. /**
  213. * @}
  214. */
  215. /* Private functions ---------------------------------------------------------*/
  216. /** @defgroup RTC_Exported_Functions RTC Exported Functions
  217. * @{
  218. */
  219. /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
  220. * @brief Initialization and Configuration functions
  221. *
  222. @verbatim
  223. ===============================================================================
  224. ##### Initialization and de-initialization functions #####
  225. ===============================================================================
  226. [..] This section provides functions allowing to initialize and configure the
  227. RTC Prescaler (Asynchronous), disable RTC registers Write protection,
  228. enter and exit the RTC initialization mode,
  229. RTC registers synchronization check and reference clock detection enable.
  230. (#) The RTC Prescaler should be programmed to generate the RTC 1Hz time base.
  231. (#) All RTC registers are Write protected. Writing to the RTC registers
  232. is enabled by setting the CNF bit in the RTC_CRL register.
  233. (#) To read the calendar after wakeup from low power modes (Standby or Stop)
  234. the software must first wait for the RSF bit (Register Synchronized Flag)
  235. in the RTC_CRL register to be set by hardware.
  236. The HAL_RTC_WaitForSynchro() function implements the above software
  237. sequence (RSF clear and RSF check).
  238. @endverbatim
  239. * @{
  240. */
  241. /**
  242. * @brief Initializes the RTC peripheral
  243. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  244. * the configuration information for RTC.
  245. * @retval HAL status
  246. */
  247. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  248. {
  249. uint32_t prescaler = 0U;
  250. /* Check input parameters */
  251. if (hrtc == NULL)
  252. {
  253. return HAL_ERROR;
  254. }
  255. /* Check the parameters */
  256. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  257. assert_param(IS_RTC_CALIB_OUTPUT(hrtc->Init.OutPut));
  258. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  259. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  260. if (hrtc->State == HAL_RTC_STATE_RESET)
  261. {
  262. /* Allocate lock resource and initialize it */
  263. hrtc->Lock = HAL_UNLOCKED;
  264. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  265. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  266. if (hrtc->MspInitCallback == NULL)
  267. {
  268. hrtc->MspInitCallback = HAL_RTC_MspInit;
  269. }
  270. /* Init the low level hardware */
  271. hrtc->MspInitCallback(hrtc);
  272. if (hrtc->MspDeInitCallback == NULL)
  273. {
  274. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  275. }
  276. }
  277. #else
  278. if (hrtc->State == HAL_RTC_STATE_RESET)
  279. {
  280. /* Allocate lock resource and initialize it */
  281. hrtc->Lock = HAL_UNLOCKED;
  282. /* Initialize RTC MSP */
  283. HAL_RTC_MspInit(hrtc);
  284. }
  285. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  286. /* Set RTC state */
  287. hrtc->State = HAL_RTC_STATE_BUSY;
  288. /* Waiting for synchro */
  289. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  290. {
  291. /* Set RTC state */
  292. hrtc->State = HAL_RTC_STATE_ERROR;
  293. return HAL_ERROR;
  294. }
  295. /* Set Initialization mode */
  296. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  297. {
  298. /* Set RTC state */
  299. hrtc->State = HAL_RTC_STATE_ERROR;
  300. return HAL_ERROR;
  301. }
  302. else
  303. {
  304. /* Clear Flags Bits */
  305. CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_OW | RTC_FLAG_ALRAF | RTC_FLAG_SEC));
  306. if (hrtc->Init.OutPut != RTC_OUTPUTSOURCE_NONE)
  307. {
  308. /* Disable the selected Tamper pin */
  309. CLEAR_BIT(BKP->CR, BKP_CR_TPE);
  310. }
  311. /* Set the signal which will be routed to RTC Tamper pin*/
  312. MODIFY_REG(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS), hrtc->Init.OutPut);
  313. if (hrtc->Init.AsynchPrediv != RTC_AUTO_1_SECOND)
  314. {
  315. /* RTC Prescaler provided directly by end-user*/
  316. prescaler = hrtc->Init.AsynchPrediv;
  317. }
  318. else
  319. {
  320. /* RTC Prescaler will be automatically calculated to get 1 second timebase */
  321. /* Get the RTCCLK frequency */
  322. prescaler = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC);
  323. /* Check that RTC clock is enabled*/
  324. if (prescaler == 0U)
  325. {
  326. /* Should not happen. Frequency is not available*/
  327. hrtc->State = HAL_RTC_STATE_ERROR;
  328. return HAL_ERROR;
  329. }
  330. else
  331. {
  332. /* RTC period = RTCCLK/(RTC_PR + 1) */
  333. prescaler = prescaler - 1U;
  334. }
  335. }
  336. /* Configure the RTC_PRLH / RTC_PRLL */
  337. WRITE_REG(hrtc->Instance->PRLH, ((prescaler >> 16U) & RTC_PRLH_PRL));
  338. WRITE_REG(hrtc->Instance->PRLL, (prescaler & RTC_PRLL_PRL));
  339. /* Wait for synchro */
  340. if (RTC_ExitInitMode(hrtc) != HAL_OK)
  341. {
  342. hrtc->State = HAL_RTC_STATE_ERROR;
  343. return HAL_ERROR;
  344. }
  345. /* Initialize date to 1st of January 2000 */
  346. hrtc->DateToUpdate.Year = 0x00U;
  347. hrtc->DateToUpdate.Month = RTC_MONTH_JANUARY;
  348. hrtc->DateToUpdate.Date = 0x01U;
  349. /* Set RTC state */
  350. hrtc->State = HAL_RTC_STATE_READY;
  351. return HAL_OK;
  352. }
  353. }
  354. /**
  355. * @brief DeInitializes the RTC peripheral
  356. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  357. * the configuration information for RTC.
  358. * @note This function does not reset the RTC Backup Data registers.
  359. * @retval HAL status
  360. */
  361. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  362. {
  363. /* Check input parameters */
  364. if (hrtc == NULL)
  365. {
  366. return HAL_ERROR;
  367. }
  368. /* Check the parameters */
  369. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  370. /* Set RTC state */
  371. hrtc->State = HAL_RTC_STATE_BUSY;
  372. /* Set Initialization mode */
  373. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  374. {
  375. /* Set RTC state */
  376. hrtc->State = HAL_RTC_STATE_ERROR;
  377. /* Release Lock */
  378. __HAL_UNLOCK(hrtc);
  379. return HAL_ERROR;
  380. }
  381. else
  382. {
  383. CLEAR_REG(hrtc->Instance->CNTL);
  384. CLEAR_REG(hrtc->Instance->CNTH);
  385. WRITE_REG(hrtc->Instance->PRLL, 0x00008000U);
  386. CLEAR_REG(hrtc->Instance->PRLH);
  387. /* Reset All CRH/CRL bits */
  388. CLEAR_REG(hrtc->Instance->CRH);
  389. CLEAR_REG(hrtc->Instance->CRL);
  390. if (RTC_ExitInitMode(hrtc) != HAL_OK)
  391. {
  392. hrtc->State = HAL_RTC_STATE_ERROR;
  393. /* Process Unlocked */
  394. __HAL_UNLOCK(hrtc);
  395. return HAL_ERROR;
  396. }
  397. }
  398. /* Wait for synchro*/
  399. HAL_RTC_WaitForSynchro(hrtc);
  400. /* Clear RSF flag */
  401. CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
  402. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  403. if (hrtc->MspDeInitCallback == NULL)
  404. {
  405. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  406. }
  407. /* DeInit the low level hardware: CLOCK, NVIC.*/
  408. hrtc->MspDeInitCallback(hrtc);
  409. #else
  410. /* De-Initialize RTC MSP */
  411. HAL_RTC_MspDeInit(hrtc);
  412. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  413. hrtc->State = HAL_RTC_STATE_RESET;
  414. /* Release Lock */
  415. __HAL_UNLOCK(hrtc);
  416. return HAL_OK;
  417. }
  418. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  419. /**
  420. * @brief Register a User RTC Callback
  421. * To be used instead of the weak predefined callback
  422. * @param hrtc RTC handle
  423. * @param CallbackID ID of the callback to be registered
  424. * This parameter can be one of the following values:
  425. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  426. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  427. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  428. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  429. * @param pCallback pointer to the Callback function
  430. * @retval HAL status
  431. */
  432. HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
  433. {
  434. HAL_StatusTypeDef status = HAL_OK;
  435. if (pCallback == NULL)
  436. {
  437. return HAL_ERROR;
  438. }
  439. /* Process locked */
  440. __HAL_LOCK(hrtc);
  441. if (HAL_RTC_STATE_READY == hrtc->State)
  442. {
  443. switch (CallbackID)
  444. {
  445. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  446. hrtc->AlarmAEventCallback = pCallback;
  447. break;
  448. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  449. hrtc->Tamper1EventCallback = pCallback;
  450. break;
  451. case HAL_RTC_MSPINIT_CB_ID :
  452. hrtc->MspInitCallback = pCallback;
  453. break;
  454. case HAL_RTC_MSPDEINIT_CB_ID :
  455. hrtc->MspDeInitCallback = pCallback;
  456. break;
  457. default :
  458. /* Return error status */
  459. status = HAL_ERROR;
  460. break;
  461. }
  462. }
  463. else if (HAL_RTC_STATE_RESET == hrtc->State)
  464. {
  465. switch (CallbackID)
  466. {
  467. case HAL_RTC_MSPINIT_CB_ID :
  468. hrtc->MspInitCallback = pCallback;
  469. break;
  470. case HAL_RTC_MSPDEINIT_CB_ID :
  471. hrtc->MspDeInitCallback = pCallback;
  472. break;
  473. default :
  474. /* Return error status */
  475. status = HAL_ERROR;
  476. break;
  477. }
  478. }
  479. else
  480. {
  481. /* Return error status */
  482. status = HAL_ERROR;
  483. }
  484. /* Release Lock */
  485. __HAL_UNLOCK(hrtc);
  486. return status;
  487. }
  488. /**
  489. * @brief Unregister an RTC Callback
  490. * RTC callback is redirected to the weak predefined callback
  491. * @param hrtc RTC handle
  492. * @param CallbackID ID of the callback to be unregistered
  493. * This parameter can be one of the following values:
  494. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  495. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  496. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  497. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  498. * @retval HAL status
  499. */
  500. HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
  501. {
  502. HAL_StatusTypeDef status = HAL_OK;
  503. /* Process locked */
  504. __HAL_LOCK(hrtc);
  505. if (HAL_RTC_STATE_READY == hrtc->State)
  506. {
  507. switch (CallbackID)
  508. {
  509. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  510. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  511. break;
  512. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  513. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  514. break;
  515. case HAL_RTC_MSPINIT_CB_ID :
  516. hrtc->MspInitCallback = HAL_RTC_MspInit;
  517. break;
  518. case HAL_RTC_MSPDEINIT_CB_ID :
  519. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  520. break;
  521. default :
  522. /* Return error status */
  523. status = HAL_ERROR;
  524. break;
  525. }
  526. }
  527. else if (HAL_RTC_STATE_RESET == hrtc->State)
  528. {
  529. switch (CallbackID)
  530. {
  531. case HAL_RTC_MSPINIT_CB_ID :
  532. hrtc->MspInitCallback = HAL_RTC_MspInit;
  533. break;
  534. case HAL_RTC_MSPDEINIT_CB_ID :
  535. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  536. break;
  537. default :
  538. /* Return error status */
  539. status = HAL_ERROR;
  540. break;
  541. }
  542. }
  543. else
  544. {
  545. /* Return error status */
  546. status = HAL_ERROR;
  547. }
  548. /* Release Lock */
  549. __HAL_UNLOCK(hrtc);
  550. return status;
  551. }
  552. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  553. /**
  554. * @brief Initializes the RTC MSP.
  555. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  556. * the configuration information for RTC.
  557. * @retval None
  558. */
  559. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
  560. {
  561. /* Prevent unused argument(s) compilation warning */
  562. UNUSED(hrtc);
  563. /* NOTE : This function Should not be modified, when the callback is needed,
  564. the HAL_RTC_MspInit could be implemented in the user file
  565. */
  566. }
  567. /**
  568. * @brief DeInitializes the RTC MSP.
  569. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  570. * the configuration information for RTC.
  571. * @retval None
  572. */
  573. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
  574. {
  575. /* Prevent unused argument(s) compilation warning */
  576. UNUSED(hrtc);
  577. /* NOTE : This function Should not be modified, when the callback is needed,
  578. the HAL_RTC_MspDeInit could be implemented in the user file
  579. */
  580. }
  581. /**
  582. * @}
  583. */
  584. /** @defgroup RTC_Exported_Functions_Group2 Time and Date functions
  585. * @brief RTC Time and Date functions
  586. *
  587. @verbatim
  588. ===============================================================================
  589. ##### RTC Time and Date functions #####
  590. ===============================================================================
  591. [..] This section provides functions allowing to configure Time and Date features
  592. @endverbatim
  593. * @{
  594. */
  595. /**
  596. * @brief Sets RTC current time.
  597. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  598. * the configuration information for RTC.
  599. * @param sTime: Pointer to Time structure
  600. * @param Format: Specifies the format of the entered parameters.
  601. * This parameter can be one of the following values:
  602. * @arg RTC_FORMAT_BIN: Binary data format
  603. * @arg RTC_FORMAT_BCD: BCD data format
  604. * @retval HAL status
  605. */
  606. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  607. {
  608. uint32_t counter_time = 0U, counter_alarm = 0U;
  609. /* Check input parameters */
  610. if ((hrtc == NULL) || (sTime == NULL))
  611. {
  612. return HAL_ERROR;
  613. }
  614. /* Check the parameters */
  615. assert_param(IS_RTC_FORMAT(Format));
  616. /* Process Locked */
  617. __HAL_LOCK(hrtc);
  618. hrtc->State = HAL_RTC_STATE_BUSY;
  619. if (Format == RTC_FORMAT_BIN)
  620. {
  621. assert_param(IS_RTC_HOUR24(sTime->Hours));
  622. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  623. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  624. counter_time = (uint32_t)(((uint32_t)sTime->Hours * 3600U) + \
  625. ((uint32_t)sTime->Minutes * 60U) + \
  626. ((uint32_t)sTime->Seconds));
  627. }
  628. else
  629. {
  630. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  631. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  632. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  633. counter_time = (((uint32_t)(RTC_Bcd2ToByte(sTime->Hours)) * 3600U) + \
  634. ((uint32_t)(RTC_Bcd2ToByte(sTime->Minutes)) * 60U) + \
  635. ((uint32_t)(RTC_Bcd2ToByte(sTime->Seconds))));
  636. }
  637. /* Write time counter in RTC registers */
  638. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  639. {
  640. /* Set RTC state */
  641. hrtc->State = HAL_RTC_STATE_ERROR;
  642. /* Process Unlocked */
  643. __HAL_UNLOCK(hrtc);
  644. return HAL_ERROR;
  645. }
  646. else
  647. {
  648. /* Clear Second and overflow flags */
  649. CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
  650. /* Read current Alarm counter in RTC registers */
  651. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  652. /* Set again alarm to match with new time if enabled */
  653. if (counter_alarm != RTC_ALARM_RESETVALUE)
  654. {
  655. if (counter_alarm < counter_time)
  656. {
  657. /* Add 1 day to alarm counter*/
  658. counter_alarm += (uint32_t)(24U * 3600U);
  659. /* Write new Alarm counter in RTC registers */
  660. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  661. {
  662. /* Set RTC state */
  663. hrtc->State = HAL_RTC_STATE_ERROR;
  664. /* Process Unlocked */
  665. __HAL_UNLOCK(hrtc);
  666. return HAL_ERROR;
  667. }
  668. }
  669. }
  670. hrtc->State = HAL_RTC_STATE_READY;
  671. __HAL_UNLOCK(hrtc);
  672. return HAL_OK;
  673. }
  674. }
  675. /**
  676. * @brief Gets RTC current time.
  677. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  678. * the configuration information for RTC.
  679. * @param sTime: Pointer to Time structure
  680. * @param Format: Specifies the format of the entered parameters.
  681. * This parameter can be one of the following values:
  682. * @arg RTC_FORMAT_BIN: Binary data format
  683. * @arg RTC_FORMAT_BCD: BCD data format
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  687. {
  688. uint32_t counter_time = 0U, counter_alarm = 0U, days_elapsed = 0U, hours = 0U;
  689. /* Check input parameters */
  690. if ((hrtc == NULL) || (sTime == NULL))
  691. {
  692. return HAL_ERROR;
  693. }
  694. /* Check the parameters */
  695. assert_param(IS_RTC_FORMAT(Format));
  696. /* Check if counter overflow occurred */
  697. if (__HAL_RTC_OVERFLOW_GET_FLAG(hrtc, RTC_FLAG_OW))
  698. {
  699. return HAL_ERROR;
  700. }
  701. /* Read the time counter*/
  702. counter_time = RTC_ReadTimeCounter(hrtc);
  703. /* Fill the structure fields with the read parameters */
  704. hours = counter_time / 3600U;
  705. sTime->Minutes = (uint8_t)((counter_time % 3600U) / 60U);
  706. sTime->Seconds = (uint8_t)((counter_time % 3600U) % 60U);
  707. if (hours >= 24U)
  708. {
  709. /* Get number of days elapsed from last calculation */
  710. days_elapsed = (hours / 24U);
  711. /* Set Hours in RTC_TimeTypeDef structure*/
  712. sTime->Hours = (hours % 24U);
  713. /* Read Alarm counter in RTC registers */
  714. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  715. /* Calculate remaining time to reach alarm (only if set and not yet expired)*/
  716. if ((counter_alarm != RTC_ALARM_RESETVALUE) && (counter_alarm > counter_time))
  717. {
  718. counter_alarm -= counter_time;
  719. }
  720. else
  721. {
  722. /* In case of counter_alarm < counter_time */
  723. /* Alarm expiration already occurred but alarm not deactivated */
  724. counter_alarm = RTC_ALARM_RESETVALUE;
  725. }
  726. /* Set updated time in decreasing counter by number of days elapsed */
  727. counter_time -= (days_elapsed * 24U * 3600U);
  728. /* Write time counter in RTC registers */
  729. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  730. {
  731. return HAL_ERROR;
  732. }
  733. /* Set updated alarm to be set */
  734. if (counter_alarm != RTC_ALARM_RESETVALUE)
  735. {
  736. counter_alarm += counter_time;
  737. /* Write time counter in RTC registers */
  738. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  739. {
  740. return HAL_ERROR;
  741. }
  742. }
  743. else
  744. {
  745. /* Alarm already occurred. Set it to reset values to avoid unexpected expiration */
  746. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  747. {
  748. return HAL_ERROR;
  749. }
  750. }
  751. /* Update date */
  752. RTC_DateUpdate(hrtc, days_elapsed);
  753. }
  754. else
  755. {
  756. sTime->Hours = hours;
  757. }
  758. /* Check the input parameters format */
  759. if (Format != RTC_FORMAT_BIN)
  760. {
  761. /* Convert the time structure parameters to BCD format */
  762. sTime->Hours = (uint8_t)RTC_ByteToBcd2(sTime->Hours);
  763. sTime->Minutes = (uint8_t)RTC_ByteToBcd2(sTime->Minutes);
  764. sTime->Seconds = (uint8_t)RTC_ByteToBcd2(sTime->Seconds);
  765. }
  766. return HAL_OK;
  767. }
  768. /**
  769. * @brief Sets RTC current date.
  770. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  771. * the configuration information for RTC.
  772. * @param sDate: Pointer to date structure
  773. * @param Format: specifies the format of the entered parameters.
  774. * This parameter can be one of the following values:
  775. * @arg RTC_FORMAT_BIN: Binary data format
  776. * @arg RTC_FORMAT_BCD: BCD data format
  777. * @retval HAL status
  778. */
  779. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  780. {
  781. uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;
  782. /* Check input parameters */
  783. if ((hrtc == NULL) || (sDate == NULL))
  784. {
  785. return HAL_ERROR;
  786. }
  787. /* Check the parameters */
  788. assert_param(IS_RTC_FORMAT(Format));
  789. /* Process Locked */
  790. __HAL_LOCK(hrtc);
  791. hrtc->State = HAL_RTC_STATE_BUSY;
  792. if (Format == RTC_FORMAT_BIN)
  793. {
  794. assert_param(IS_RTC_YEAR(sDate->Year));
  795. assert_param(IS_RTC_MONTH(sDate->Month));
  796. assert_param(IS_RTC_DATE(sDate->Date));
  797. /* Change the current date */
  798. hrtc->DateToUpdate.Year = sDate->Year;
  799. hrtc->DateToUpdate.Month = sDate->Month;
  800. hrtc->DateToUpdate.Date = sDate->Date;
  801. }
  802. else
  803. {
  804. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  805. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  806. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  807. /* Change the current date */
  808. hrtc->DateToUpdate.Year = RTC_Bcd2ToByte(sDate->Year);
  809. hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
  810. hrtc->DateToUpdate.Date = RTC_Bcd2ToByte(sDate->Date);
  811. }
  812. /* WeekDay set by user can be ignored because automatically calculated */
  813. hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(hrtc->DateToUpdate.Year, hrtc->DateToUpdate.Month, hrtc->DateToUpdate.Date);
  814. sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
  815. /* Reset time to be aligned on the same day */
  816. /* Read the time counter*/
  817. counter_time = RTC_ReadTimeCounter(hrtc);
  818. /* Fill the structure fields with the read parameters */
  819. hours = counter_time / 3600U;
  820. if (hours > 24U)
  821. {
  822. /* Set updated time in decreasing counter by number of days elapsed */
  823. counter_time -= ((hours / 24U) * 24U * 3600U);
  824. /* Write time counter in RTC registers */
  825. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  826. {
  827. /* Set RTC state */
  828. hrtc->State = HAL_RTC_STATE_ERROR;
  829. /* Process Unlocked */
  830. __HAL_UNLOCK(hrtc);
  831. return HAL_ERROR;
  832. }
  833. /* Read current Alarm counter in RTC registers */
  834. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  835. /* Set again alarm to match with new time if enabled */
  836. if (counter_alarm != RTC_ALARM_RESETVALUE)
  837. {
  838. if (counter_alarm < counter_time)
  839. {
  840. /* Add 1 day to alarm counter*/
  841. counter_alarm += (uint32_t)(24U * 3600U);
  842. /* Write new Alarm counter in RTC registers */
  843. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  844. {
  845. /* Set RTC state */
  846. hrtc->State = HAL_RTC_STATE_ERROR;
  847. /* Process Unlocked */
  848. __HAL_UNLOCK(hrtc);
  849. return HAL_ERROR;
  850. }
  851. }
  852. }
  853. }
  854. hrtc->State = HAL_RTC_STATE_READY ;
  855. /* Process Unlocked */
  856. __HAL_UNLOCK(hrtc);
  857. return HAL_OK;
  858. }
  859. /**
  860. * @brief Gets RTC current date.
  861. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  862. * the configuration information for RTC.
  863. * @param sDate: Pointer to Date structure
  864. * @param Format: Specifies the format of the entered parameters.
  865. * This parameter can be one of the following values:
  866. * @arg RTC_FORMAT_BIN: Binary data format
  867. * @arg RTC_FORMAT_BCD: BCD data format
  868. * @retval HAL status
  869. */
  870. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  871. {
  872. RTC_TimeTypeDef stime = {0U};
  873. /* Check input parameters */
  874. if ((hrtc == NULL) || (sDate == NULL))
  875. {
  876. return HAL_ERROR;
  877. }
  878. /* Check the parameters */
  879. assert_param(IS_RTC_FORMAT(Format));
  880. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  881. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  882. {
  883. return HAL_ERROR;
  884. }
  885. /* Fill the structure fields with the read parameters */
  886. sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
  887. sDate->Year = hrtc->DateToUpdate.Year;
  888. sDate->Month = hrtc->DateToUpdate.Month;
  889. sDate->Date = hrtc->DateToUpdate.Date;
  890. /* Check the input parameters format */
  891. if (Format != RTC_FORMAT_BIN)
  892. {
  893. /* Convert the date structure parameters to BCD format */
  894. sDate->Year = (uint8_t)RTC_ByteToBcd2(sDate->Year);
  895. sDate->Month = (uint8_t)RTC_ByteToBcd2(sDate->Month);
  896. sDate->Date = (uint8_t)RTC_ByteToBcd2(sDate->Date);
  897. }
  898. return HAL_OK;
  899. }
  900. /**
  901. * @}
  902. */
  903. /** @defgroup RTC_Exported_Functions_Group3 Alarm functions
  904. * @brief RTC Alarm functions
  905. *
  906. @verbatim
  907. ===============================================================================
  908. ##### RTC Alarm functions #####
  909. ===============================================================================
  910. [..] This section provides functions allowing to configure Alarm feature
  911. @endverbatim
  912. * @{
  913. */
  914. /**
  915. * @brief Sets the specified RTC Alarm.
  916. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  917. * the configuration information for RTC.
  918. * @param sAlarm: Pointer to Alarm structure
  919. * @param Format: Specifies the format of the entered parameters.
  920. * This parameter can be one of the following values:
  921. * @arg RTC_FORMAT_BIN: Binary data format
  922. * @arg RTC_FORMAT_BCD: BCD data format
  923. * @retval HAL status
  924. */
  925. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  926. {
  927. uint32_t counter_alarm = 0U, counter_time;
  928. RTC_TimeTypeDef stime = {0U};
  929. /* Check input parameters */
  930. if ((hrtc == NULL) || (sAlarm == NULL))
  931. {
  932. return HAL_ERROR;
  933. }
  934. /* Check the parameters */
  935. assert_param(IS_RTC_FORMAT(Format));
  936. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  937. /* Process Locked */
  938. __HAL_LOCK(hrtc);
  939. hrtc->State = HAL_RTC_STATE_BUSY;
  940. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  941. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  942. {
  943. return HAL_ERROR;
  944. }
  945. /* Convert time in seconds */
  946. counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
  947. ((uint32_t)stime.Minutes * 60U) + \
  948. ((uint32_t)stime.Seconds));
  949. if (Format == RTC_FORMAT_BIN)
  950. {
  951. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  952. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  953. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  954. counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
  955. ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
  956. ((uint32_t)sAlarm->AlarmTime.Seconds));
  957. }
  958. else
  959. {
  960. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  961. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  962. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  963. counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
  964. ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
  965. ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  966. }
  967. /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
  968. if (counter_alarm < counter_time)
  969. {
  970. /* Add 1 day to alarm counter*/
  971. counter_alarm += (uint32_t)(24U * 3600U);
  972. }
  973. /* Write Alarm counter in RTC registers */
  974. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  975. {
  976. /* Set RTC state */
  977. hrtc->State = HAL_RTC_STATE_ERROR;
  978. /* Process Unlocked */
  979. __HAL_UNLOCK(hrtc);
  980. return HAL_ERROR;
  981. }
  982. else
  983. {
  984. hrtc->State = HAL_RTC_STATE_READY;
  985. __HAL_UNLOCK(hrtc);
  986. return HAL_OK;
  987. }
  988. }
  989. /**
  990. * @brief Sets the specified RTC Alarm with Interrupt
  991. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  992. * the configuration information for RTC.
  993. * @param sAlarm: Pointer to Alarm structure
  994. * @param Format: Specifies the format of the entered parameters.
  995. * This parameter can be one of the following values:
  996. * @arg RTC_FORMAT_BIN: Binary data format
  997. * @arg RTC_FORMAT_BCD: BCD data format
  998. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  999. * @retval HAL status
  1000. */
  1001. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1002. {
  1003. uint32_t counter_alarm = 0U, counter_time;
  1004. RTC_TimeTypeDef stime = {0U};
  1005. /* Check input parameters */
  1006. if ((hrtc == NULL) || (sAlarm == NULL))
  1007. {
  1008. return HAL_ERROR;
  1009. }
  1010. /* Check the parameters */
  1011. assert_param(IS_RTC_FORMAT(Format));
  1012. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1013. /* Process Locked */
  1014. __HAL_LOCK(hrtc);
  1015. hrtc->State = HAL_RTC_STATE_BUSY;
  1016. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  1017. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  1018. {
  1019. return HAL_ERROR;
  1020. }
  1021. /* Convert time in seconds */
  1022. counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
  1023. ((uint32_t)stime.Minutes * 60U) + \
  1024. ((uint32_t)stime.Seconds));
  1025. if (Format == RTC_FORMAT_BIN)
  1026. {
  1027. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1028. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1029. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1030. counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
  1031. ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
  1032. ((uint32_t)sAlarm->AlarmTime.Seconds));
  1033. }
  1034. else
  1035. {
  1036. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1037. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1038. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1039. counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
  1040. ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
  1041. ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1042. }
  1043. /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
  1044. if (counter_alarm < counter_time)
  1045. {
  1046. /* Add 1 day to alarm counter*/
  1047. counter_alarm += (uint32_t)(24U * 3600U);
  1048. }
  1049. /* Write alarm counter in RTC registers */
  1050. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  1051. {
  1052. /* Set RTC state */
  1053. hrtc->State = HAL_RTC_STATE_ERROR;
  1054. /* Process Unlocked */
  1055. __HAL_UNLOCK(hrtc);
  1056. return HAL_ERROR;
  1057. }
  1058. else
  1059. {
  1060. /* Clear flag alarm A */
  1061. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1062. /* Configure the Alarm interrupt */
  1063. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
  1064. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1065. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1066. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  1067. hrtc->State = HAL_RTC_STATE_READY;
  1068. __HAL_UNLOCK(hrtc);
  1069. return HAL_OK;
  1070. }
  1071. }
  1072. /**
  1073. * @brief Gets the RTC Alarm value and masks.
  1074. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1075. * the configuration information for RTC.
  1076. * @param sAlarm: Pointer to Date structure
  1077. * @param Alarm: Specifies the Alarm.
  1078. * This parameter can be one of the following values:
  1079. * @arg RTC_ALARM_A: Alarm
  1080. * @param Format: Specifies the format of the entered parameters.
  1081. * This parameter can be one of the following values:
  1082. * @arg RTC_FORMAT_BIN: Binary data format
  1083. * @arg RTC_FORMAT_BCD: BCD data format
  1084. * @retval HAL status
  1085. */
  1086. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1087. {
  1088. uint32_t counter_alarm = 0U;
  1089. /* Prevent unused argument(s) compilation warning */
  1090. UNUSED(Alarm);
  1091. /* Check input parameters */
  1092. if ((hrtc == NULL) || (sAlarm == NULL))
  1093. {
  1094. return HAL_ERROR;
  1095. }
  1096. /* Check the parameters */
  1097. assert_param(IS_RTC_FORMAT(Format));
  1098. assert_param(IS_RTC_ALARM(Alarm));
  1099. /* Read Alarm counter in RTC registers */
  1100. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  1101. /* Fill the structure with the read parameters */
  1102. /* Set hours in a day range (between 0 to 24)*/
  1103. sAlarm->AlarmTime.Hours = (uint32_t)((counter_alarm / 3600U) % 24U);
  1104. sAlarm->AlarmTime.Minutes = (uint32_t)((counter_alarm % 3600U) / 60U);
  1105. sAlarm->AlarmTime.Seconds = (uint32_t)((counter_alarm % 3600U) % 60U);
  1106. if (Format != RTC_FORMAT_BIN)
  1107. {
  1108. sAlarm->AlarmTime.Hours = RTC_ByteToBcd2(sAlarm->AlarmTime.Hours);
  1109. sAlarm->AlarmTime.Minutes = RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes);
  1110. sAlarm->AlarmTime.Seconds = RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds);
  1111. }
  1112. return HAL_OK;
  1113. }
  1114. /**
  1115. * @brief Deactivate the specified RTC Alarm
  1116. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1117. * the configuration information for RTC.
  1118. * @param Alarm: Specifies the Alarm.
  1119. * This parameter can be one of the following values:
  1120. * @arg RTC_ALARM_A: AlarmA
  1121. * @retval HAL status
  1122. */
  1123. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  1124. {
  1125. /* Prevent unused argument(s) compilation warning */
  1126. UNUSED(Alarm);
  1127. /* Check the parameters */
  1128. assert_param(IS_RTC_ALARM(Alarm));
  1129. /* Check input parameters */
  1130. if (hrtc == NULL)
  1131. {
  1132. return HAL_ERROR;
  1133. }
  1134. /* Process Locked */
  1135. __HAL_LOCK(hrtc);
  1136. hrtc->State = HAL_RTC_STATE_BUSY;
  1137. /* In case of interrupt mode is used, the interrupt source must disabled */
  1138. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  1139. /* Set Initialization mode */
  1140. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  1141. {
  1142. /* Set RTC state */
  1143. hrtc->State = HAL_RTC_STATE_ERROR;
  1144. /* Process Unlocked */
  1145. __HAL_UNLOCK(hrtc);
  1146. return HAL_ERROR;
  1147. }
  1148. else
  1149. {
  1150. /* Clear flag alarm A */
  1151. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1152. /* Set to default values ALRH & ALRL registers */
  1153. WRITE_REG(hrtc->Instance->ALRH, RTC_ALARM_RESETVALUE_REGISTER);
  1154. WRITE_REG(hrtc->Instance->ALRL, RTC_ALARM_RESETVALUE_REGISTER);
  1155. /* RTC Alarm Interrupt Configuration: Disable EXTI configuration */
  1156. __HAL_RTC_ALARM_EXTI_DISABLE_IT();
  1157. /* Wait for synchro */
  1158. if (RTC_ExitInitMode(hrtc) != HAL_OK)
  1159. {
  1160. hrtc->State = HAL_RTC_STATE_ERROR;
  1161. /* Process Unlocked */
  1162. __HAL_UNLOCK(hrtc);
  1163. return HAL_ERROR;
  1164. }
  1165. }
  1166. hrtc->State = HAL_RTC_STATE_READY;
  1167. /* Process Unlocked */
  1168. __HAL_UNLOCK(hrtc);
  1169. return HAL_OK;
  1170. }
  1171. /**
  1172. * @brief This function handles Alarm interrupt request.
  1173. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1174. * the configuration information for RTC.
  1175. * @retval None
  1176. */
  1177. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
  1178. {
  1179. if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA))
  1180. {
  1181. /* Get the status of the Interrupt */
  1182. if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
  1183. {
  1184. /* AlarmA callback */
  1185. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1186. hrtc->AlarmAEventCallback(hrtc);
  1187. #else
  1188. HAL_RTC_AlarmAEventCallback(hrtc);
  1189. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1190. /* Clear the Alarm interrupt pending bit */
  1191. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1192. }
  1193. }
  1194. /* Clear the EXTI's line Flag for RTC Alarm */
  1195. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  1196. /* Change RTC state */
  1197. hrtc->State = HAL_RTC_STATE_READY;
  1198. }
  1199. /**
  1200. * @brief Alarm A callback.
  1201. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1202. * the configuration information for RTC.
  1203. * @retval None
  1204. */
  1205. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1206. {
  1207. /* Prevent unused argument(s) compilation warning */
  1208. UNUSED(hrtc);
  1209. /* NOTE : This function Should not be modified, when the callback is needed,
  1210. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1211. */
  1212. }
  1213. /**
  1214. * @brief This function handles AlarmA Polling request.
  1215. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1216. * the configuration information for RTC.
  1217. * @param Timeout: Timeout duration
  1218. * @retval HAL status
  1219. */
  1220. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1221. {
  1222. uint32_t tickstart = HAL_GetTick();
  1223. /* Check input parameters */
  1224. if (hrtc == NULL)
  1225. {
  1226. return HAL_ERROR;
  1227. }
  1228. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
  1229. {
  1230. if (Timeout != HAL_MAX_DELAY)
  1231. {
  1232. if ((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1233. {
  1234. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1235. return HAL_TIMEOUT;
  1236. }
  1237. }
  1238. }
  1239. /* Clear the Alarm interrupt pending bit */
  1240. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1241. /* Change RTC state */
  1242. hrtc->State = HAL_RTC_STATE_READY;
  1243. return HAL_OK;
  1244. }
  1245. /**
  1246. * @}
  1247. */
  1248. /** @defgroup RTC_Exported_Functions_Group4 Peripheral State functions
  1249. * @brief Peripheral State functions
  1250. *
  1251. @verbatim
  1252. ===============================================================================
  1253. ##### Peripheral State functions #####
  1254. ===============================================================================
  1255. [..]
  1256. This subsection provides functions allowing to
  1257. (+) Get RTC state
  1258. @endverbatim
  1259. * @{
  1260. */
  1261. /**
  1262. * @brief Returns the RTC state.
  1263. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1264. * the configuration information for RTC.
  1265. * @retval HAL state
  1266. */
  1267. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
  1268. {
  1269. return hrtc->State;
  1270. }
  1271. /**
  1272. * @}
  1273. */
  1274. /** @defgroup RTC_Exported_Functions_Group5 Peripheral Control functions
  1275. * @brief Peripheral Control functions
  1276. *
  1277. @verbatim
  1278. ===============================================================================
  1279. ##### Peripheral Control functions #####
  1280. ===============================================================================
  1281. [..]
  1282. This subsection provides functions allowing to
  1283. (+) Wait for RTC Time and Date Synchronization
  1284. @endverbatim
  1285. * @{
  1286. */
  1287. /**
  1288. * @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
  1289. * are synchronized with RTC APB clock.
  1290. * @note This function must be called before any read operation after an APB reset
  1291. * or an APB clock stop.
  1292. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1293. * the configuration information for RTC.
  1294. * @retval HAL status
  1295. */
  1296. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
  1297. {
  1298. uint32_t tickstart = 0U;
  1299. /* Check input parameters */
  1300. if (hrtc == NULL)
  1301. {
  1302. return HAL_ERROR;
  1303. }
  1304. /* Clear RSF flag */
  1305. CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
  1306. tickstart = HAL_GetTick();
  1307. /* Wait the registers to be synchronised */
  1308. while ((hrtc->Instance->CRL & RTC_FLAG_RSF) == (uint32_t)RESET)
  1309. {
  1310. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1311. {
  1312. return HAL_TIMEOUT;
  1313. }
  1314. }
  1315. return HAL_OK;
  1316. }
  1317. /**
  1318. * @}
  1319. */
  1320. /**
  1321. * @}
  1322. */
  1323. /** @addtogroup RTC_Private_Functions
  1324. * @{
  1325. */
  1326. /**
  1327. * @brief Read the time counter available in RTC_CNT registers.
  1328. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1329. * the configuration information for RTC.
  1330. * @retval Time counter
  1331. */
  1332. static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc)
  1333. {
  1334. uint16_t high1 = 0U, high2 = 0U, low = 0U;
  1335. uint32_t timecounter = 0U;
  1336. high1 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
  1337. low = READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
  1338. high2 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
  1339. if (high1 != high2)
  1340. {
  1341. /* In this case the counter roll over during reading of CNTL and CNTH registers,
  1342. read again CNTL register then return the counter value */
  1343. timecounter = (((uint32_t) high2 << 16U) | READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
  1344. }
  1345. else
  1346. {
  1347. /* No counter roll over during reading of CNTL and CNTH registers, counter
  1348. value is equal to first value of CNTL and CNTH */
  1349. timecounter = (((uint32_t) high1 << 16U) | low);
  1350. }
  1351. return timecounter;
  1352. }
  1353. /**
  1354. * @brief Write the time counter in RTC_CNT registers.
  1355. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1356. * the configuration information for RTC.
  1357. * @param TimeCounter: Counter to write in RTC_CNT registers
  1358. * @retval HAL status
  1359. */
  1360. static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef *hrtc, uint32_t TimeCounter)
  1361. {
  1362. HAL_StatusTypeDef status = HAL_OK;
  1363. /* Set Initialization mode */
  1364. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  1365. {
  1366. status = HAL_ERROR;
  1367. }
  1368. else
  1369. {
  1370. /* Set RTC COUNTER MSB word */
  1371. WRITE_REG(hrtc->Instance->CNTH, (TimeCounter >> 16U));
  1372. /* Set RTC COUNTER LSB word */
  1373. WRITE_REG(hrtc->Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));
  1374. /* Wait for synchro */
  1375. if (RTC_ExitInitMode(hrtc) != HAL_OK)
  1376. {
  1377. status = HAL_ERROR;
  1378. }
  1379. }
  1380. return status;
  1381. }
  1382. /**
  1383. * @brief Read the time counter available in RTC_ALR registers.
  1384. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1385. * the configuration information for RTC.
  1386. * @retval Time counter
  1387. */
  1388. static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef *hrtc)
  1389. {
  1390. uint16_t high1 = 0U, low = 0U;
  1391. high1 = READ_REG(hrtc->Instance->ALRH & RTC_CNTH_RTC_CNT);
  1392. low = READ_REG(hrtc->Instance->ALRL & RTC_CNTL_RTC_CNT);
  1393. return (((uint32_t) high1 << 16U) | low);
  1394. }
  1395. /**
  1396. * @brief Write the time counter in RTC_ALR registers.
  1397. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1398. * the configuration information for RTC.
  1399. * @param AlarmCounter: Counter to write in RTC_ALR registers
  1400. * @retval HAL status
  1401. */
  1402. static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef *hrtc, uint32_t AlarmCounter)
  1403. {
  1404. HAL_StatusTypeDef status = HAL_OK;
  1405. /* Set Initialization mode */
  1406. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  1407. {
  1408. status = HAL_ERROR;
  1409. }
  1410. else
  1411. {
  1412. /* Set RTC COUNTER MSB word */
  1413. WRITE_REG(hrtc->Instance->ALRH, (AlarmCounter >> 16U));
  1414. /* Set RTC COUNTER LSB word */
  1415. WRITE_REG(hrtc->Instance->ALRL, (AlarmCounter & RTC_ALRL_RTC_ALR));
  1416. /* Wait for synchro */
  1417. if (RTC_ExitInitMode(hrtc) != HAL_OK)
  1418. {
  1419. status = HAL_ERROR;
  1420. }
  1421. }
  1422. return status;
  1423. }
  1424. /**
  1425. * @brief Enters the RTC Initialization mode.
  1426. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1427. * the configuration information for RTC.
  1428. * @retval HAL status
  1429. */
  1430. static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
  1431. {
  1432. uint32_t tickstart = 0U;
  1433. tickstart = HAL_GetTick();
  1434. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1435. while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  1436. {
  1437. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1438. {
  1439. return HAL_TIMEOUT;
  1440. }
  1441. }
  1442. /* Disable the write protection for RTC registers */
  1443. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1444. return HAL_OK;
  1445. }
  1446. /**
  1447. * @brief Exit the RTC Initialization mode.
  1448. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1449. * the configuration information for RTC.
  1450. * @retval HAL status
  1451. */
  1452. static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
  1453. {
  1454. uint32_t tickstart = 0U;
  1455. /* Disable the write protection for RTC registers */
  1456. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1457. tickstart = HAL_GetTick();
  1458. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1459. while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  1460. {
  1461. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1462. {
  1463. return HAL_TIMEOUT;
  1464. }
  1465. }
  1466. return HAL_OK;
  1467. }
  1468. /**
  1469. * @brief Converts a 2 digit decimal to BCD format.
  1470. * @param Value: Byte to be converted
  1471. * @retval Converted byte
  1472. */
  1473. static uint8_t RTC_ByteToBcd2(uint8_t Value)
  1474. {
  1475. uint32_t bcdhigh = 0U;
  1476. while (Value >= 10U)
  1477. {
  1478. bcdhigh++;
  1479. Value -= 10U;
  1480. }
  1481. return ((uint8_t)(bcdhigh << 4U) | Value);
  1482. }
  1483. /**
  1484. * @brief Converts from 2 digit BCD to Binary.
  1485. * @param Value: BCD value to be converted
  1486. * @retval Converted word
  1487. */
  1488. static uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1489. {
  1490. uint32_t tmp = 0U;
  1491. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10U;
  1492. return (tmp + (Value & (uint8_t)0x0F));
  1493. }
  1494. /**
  1495. * @brief Updates date when time is 23:59:59.
  1496. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1497. * the configuration information for RTC.
  1498. * @param DayElapsed: Number of days elapsed from last date update
  1499. * @retval None
  1500. */
  1501. static void RTC_DateUpdate(RTC_HandleTypeDef *hrtc, uint32_t DayElapsed)
  1502. {
  1503. uint32_t year = 0U, month = 0U, day = 0U;
  1504. uint32_t loop = 0U;
  1505. /* Get the current year*/
  1506. year = hrtc->DateToUpdate.Year;
  1507. /* Get the current month and day */
  1508. month = hrtc->DateToUpdate.Month;
  1509. day = hrtc->DateToUpdate.Date;
  1510. for (loop = 0U; loop < DayElapsed; loop++)
  1511. {
  1512. if ((month == 1U) || (month == 3U) || (month == 5U) || (month == 7U) || \
  1513. (month == 8U) || (month == 10U) || (month == 12U))
  1514. {
  1515. if (day < 31U)
  1516. {
  1517. day++;
  1518. }
  1519. /* Date structure member: day = 31 */
  1520. else
  1521. {
  1522. if (month != 12U)
  1523. {
  1524. month++;
  1525. day = 1U;
  1526. }
  1527. /* Date structure member: day = 31 & month =12 */
  1528. else
  1529. {
  1530. month = 1U;
  1531. day = 1U;
  1532. year++;
  1533. }
  1534. }
  1535. }
  1536. else if ((month == 4U) || (month == 6U) || (month == 9U) || (month == 11U))
  1537. {
  1538. if (day < 30U)
  1539. {
  1540. day++;
  1541. }
  1542. /* Date structure member: day = 30 */
  1543. else
  1544. {
  1545. month++;
  1546. day = 1U;
  1547. }
  1548. }
  1549. else if (month == 2U)
  1550. {
  1551. if (day < 28U)
  1552. {
  1553. day++;
  1554. }
  1555. else if (day == 28U)
  1556. {
  1557. /* Leap year */
  1558. if (RTC_IsLeapYear(year))
  1559. {
  1560. day++;
  1561. }
  1562. else
  1563. {
  1564. month++;
  1565. day = 1U;
  1566. }
  1567. }
  1568. else if (day == 29U)
  1569. {
  1570. month++;
  1571. day = 1U;
  1572. }
  1573. }
  1574. }
  1575. /* Update year */
  1576. hrtc->DateToUpdate.Year = year;
  1577. /* Update day and month */
  1578. hrtc->DateToUpdate.Month = month;
  1579. hrtc->DateToUpdate.Date = day;
  1580. /* Update day of the week */
  1581. hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(year, month, day);
  1582. }
  1583. /**
  1584. * @brief Check whether the passed year is Leap or not.
  1585. * @param nYear year to check
  1586. * @retval 1: leap year
  1587. * 0: not leap year
  1588. */
  1589. static uint8_t RTC_IsLeapYear(uint16_t nYear)
  1590. {
  1591. if ((nYear % 4U) != 0U)
  1592. {
  1593. return 0U;
  1594. }
  1595. if ((nYear % 100U) != 0U)
  1596. {
  1597. return 1U;
  1598. }
  1599. if ((nYear % 400U) == 0U)
  1600. {
  1601. return 1U;
  1602. }
  1603. else
  1604. {
  1605. return 0U;
  1606. }
  1607. }
  1608. /**
  1609. * @brief Determines the week number, the day number and the week day number.
  1610. * @param nYear year to check
  1611. * @param nMonth Month to check
  1612. * @param nDay Day to check
  1613. * @note Day is calculated with hypothesis that year > 2000
  1614. * @retval Value which can take one of the following parameters:
  1615. * @arg RTC_WEEKDAY_MONDAY
  1616. * @arg RTC_WEEKDAY_TUESDAY
  1617. * @arg RTC_WEEKDAY_WEDNESDAY
  1618. * @arg RTC_WEEKDAY_THURSDAY
  1619. * @arg RTC_WEEKDAY_FRIDAY
  1620. * @arg RTC_WEEKDAY_SATURDAY
  1621. * @arg RTC_WEEKDAY_SUNDAY
  1622. */
  1623. static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay)
  1624. {
  1625. uint32_t year = 0U, weekday = 0U;
  1626. year = 2000U + nYear;
  1627. if (nMonth < 3U)
  1628. {
  1629. /*D = { [(23 x month)/9] + day + 4 + year + [(year-1)/4] - [(year-1)/100] + [(year-1)/400] } mod 7*/
  1630. weekday = (((23U * nMonth) / 9U) + nDay + 4U + year + ((year - 1U) / 4U) - ((year - 1U) / 100U) + ((year - 1U) / 400U)) % 7U;
  1631. }
  1632. else
  1633. {
  1634. /*D = { [(23 x month)/9] + day + 4 + year + [year/4] - [year/100] + [year/400] - 2 } mod 7*/
  1635. weekday = (((23U * nMonth) / 9U) + nDay + 4U + year + (year / 4U) - (year / 100U) + (year / 400U) - 2U) % 7U;
  1636. }
  1637. return (uint8_t)weekday;
  1638. }
  1639. /**
  1640. * @}
  1641. */
  1642. #endif /* HAL_RTC_MODULE_ENABLED */
  1643. /**
  1644. * @}
  1645. */
  1646. /**
  1647. * @}
  1648. */