stm32f1xx_hal_rtc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  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. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==================================================================
  18. [..]
  19. (+) Enable the RTC domain access (see description in the section above).
  20. (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
  21. using the HAL_RTC_Init() function.
  22. *** Time and Date configuration ***
  23. ===================================
  24. [..]
  25. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  26. and HAL_RTC_SetDate() functions.
  27. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  28. *** Alarm configuration ***
  29. ===========================
  30. [..]
  31. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  32. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  33. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  34. *** Tamper configuration ***
  35. ============================
  36. [..]
  37. (+) Enable the RTC Tamper and configure the Tamper Level using the
  38. HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
  39. mode using HAL_RTCEx_SetTamper_IT() function.
  40. (+) The TAMPER1 alternate function can be mapped to PC13
  41. *** Backup Data Registers configuration ***
  42. ===========================================
  43. [..]
  44. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  45. function.
  46. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  47. function.
  48. ##### WARNING: Drivers Restrictions #####
  49. ==================================================================
  50. [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
  51. (other families) will be not supported on F1.
  52. [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
  53. managed by SW.
  54. [..] Then, there are some restrictions compared to other families:
  55. (+) Only format 24 hours supported in HAL (format 12 hours not supported)
  56. (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
  57. User should implement a way to save date before entering in low power mode (an
  58. example is provided with firmware package based on backup registers)
  59. (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
  60. (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
  61. to program a new alarm)
  62. ##### Backup Domain Operating Condition #####
  63. ==============================================================================
  64. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  65. from the VBAT voltage when the main VDD supply is powered off.
  66. To retain the content of the RTC backup registers and supply the RTC
  67. when VDD is turned off, VBAT pin can be connected to an optional
  68. standby voltage supplied by a battery or by another source.
  69. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  70. off, the VBAT pin powers the following blocks:
  71. (#) The RTC
  72. (#) The LSE oscillator
  73. (#) The backup SRAM when the low power backup regulator is enabled
  74. (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
  75. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  76. the following pins are available:
  77. (+) PC13 can be used as a Tamper pin
  78. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  79. because VDD is not present), the following pins are available:
  80. (+) PC13 can be used as the Tamper pin
  81. ##### Backup Domain Reset #####
  82. ==================================================================
  83. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  84. to their reset values.
  85. [..] A backup domain reset is generated when one of the following events occurs:
  86. (#) Software reset, triggered by setting the BDRST bit in the
  87. RCC Backup domain control register (RCC_BDCR).
  88. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  89. (#) Tamper detection event resets all data backup registers.
  90. ##### Backup Domain Access #####
  91. ==================================================================
  92. [..] After reset, the backup domain (RTC registers, RTC backup data
  93. registers and backup SRAM) is protected against possible unwanted write
  94. accesses.
  95. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  96. (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
  97. PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
  98. (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
  99. ##### RTC and low power modes #####
  100. ==================================================================
  101. [..] The MCU can be woken up from a low power mode by an RTC alternate
  102. function.
  103. [..] The RTC alternate functions are the RTC alarms (Alarm A),
  104. and RTC tamper event detection.
  105. These RTC alternate functions can wake up the system from the Stop and
  106. Standby low power modes.
  107. [..] The system can also wake up from low power modes without depending
  108. on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
  109. *** Callback registration ***
  110. =============================================
  111. [..]
  112. The compilation define USE_HAL_RTC_REGISTER_CALLBACKS when set to 1
  113. allows the user to configure dynamically the driver callbacks.
  114. Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
  115. [..]
  116. Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
  117. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  118. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  119. (+) MspInitCallback : RTC MspInit callback.
  120. (+) MspDeInitCallback : RTC MspDeInit callback.
  121. [..]
  122. This function takes as parameters the HAL peripheral handle, the Callback ID
  123. and a pointer to the user callback function.
  124. [..]
  125. Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
  126. weak function.
  127. @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  128. and the Callback ID.
  129. This function allows to reset following callbacks:
  130. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  131. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  132. (+) MspInitCallback : RTC MspInit callback.
  133. (+) MspDeInitCallback : RTC MspDeInit callback.
  134. [..]
  135. By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  136. all callbacks are set to the corresponding weak functions :
  137. example @ref AlarmAEventCallback().
  138. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  139. in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
  140. (not registered beforehand).
  141. If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
  142. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  143. [..]
  144. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  145. Exception done MspInit/MspDeInit that can be registered/unregistered
  146. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  147. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  148. In that case first register the MspInit/MspDeInit user callbacks
  149. using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
  150. or @ref HAL_RTC_Init() function.
  151. [..]
  152. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  153. not defined, the callback registration feature is not available and all callbacks
  154. are set to the corresponding weak functions.
  155. @endverbatim
  156. ******************************************************************************
  157. * @attention
  158. *
  159. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  160. * All rights reserved.</center></h2>
  161. *
  162. * This software component is licensed by ST under BSD 3-Clause license,
  163. * the "License"; You may not use this file except in compliance with the
  164. * License. You may obtain a copy of the License at:
  165. * opensource.org/licenses/BSD-3-Clause
  166. *
  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. MODIFY_REG(hrtc->Instance->PRLH, RTC_PRLH_PRL, (prescaler >> 16U));
  338. MODIFY_REG(hrtc->Instance->PRLL, RTC_PRLL_PRL, (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 callabck 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 Deactive 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. */
  1649. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/