py32f0xx_hal_rtc.c 56 KB

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