You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_rtc.c 61KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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. ##### Backup Domain Operating Condition #####
  17. ==============================================================================
  18. [..] The real-time clock (RTC), the RTC backup registers, and the backup
  19. SRAM (BKP SRAM) can be powered from the VBAT voltage when the main
  20. VDD supply is powered off.
  21. To retain the content of the RTC backup registers, backup SRAM, and supply
  22. the RTC when VDD is turned off, VBAT pin can be connected to an optional
  23. standby voltage supplied by a battery or by another source.
  24. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  25. off, the VBAT pin powers the following blocks:
  26. (#) The RTC
  27. (#) The LSE oscillator
  28. (#) The backup SRAM when the low power backup regulator is enabled
  29. (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
  30. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  31. the following pins are available:
  32. (#) PC14 and PC15 can be used as either GPIO or LSE pins
  33. (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
  34. (#) PI8 can be used as a GPIO or as the RTC_AF2 pin
  35. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  36. because VDD is not present), the following pins are available:
  37. (#) PC14 and PC15 can be used as LSE pins only
  38. (#) PC13 can be used as the RTC_AF1 pin
  39. (#) PI8 can be used as the RTC_AF2 pin
  40. ##### Backup Domain Reset #####
  41. ==================================================================
  42. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  43. to their reset values. The BKPSRAM is not affected by this reset. The only
  44. way to reset the BKPSRAM is through the Flash interface by requesting
  45. a protection level change from 1 to 0.
  46. [..] A backup domain reset is generated when one of the following events occurs:
  47. (#) Software reset, triggered by setting the BDRST bit in the
  48. RCC Backup domain control register (RCC_BDCR).
  49. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  50. ##### Backup Domain Access #####
  51. ==================================================================
  52. [..] After reset, the backup domain (RTC registers, RTC backup data
  53. registers and backup SRAM) is protected against possible unwanted write
  54. accesses.
  55. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  56. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  57. __HAL_RCC_PWR_CLK_ENABLE() function.
  58. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  59. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  60. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  61. ##### How to use this driver #####
  62. ==================================================================
  63. [..]
  64. (+) Enable the RTC domain access (see description in the section above).
  65. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  66. format using the HAL_RTC_Init() function.
  67. *** Time and Date configuration ***
  68. ===================================
  69. [..]
  70. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  71. and HAL_RTC_SetDate() functions.
  72. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  73. *** Alarm configuration ***
  74. ===========================
  75. [..]
  76. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  77. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  78. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  79. ##### RTC and low power modes #####
  80. ==================================================================
  81. [..] The MCU can be woken up from a low power mode by an RTC alternate
  82. function.
  83. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  84. RTC wake-up, RTC tamper event detection and RTC time stamp event detection.
  85. These RTC alternate functions can wake up the system from the Stop and
  86. Standby low power modes.
  87. [..] The system can also wake up from low power modes without depending
  88. on an external interrupt (Auto-wake-up mode), by using the RTC alarm
  89. or the RTC wake-up events.
  90. [..] The RTC provides a programmable time base for waking up from the
  91. Stop or Standby mode at regular intervals.
  92. Wake-up from STOP and STANDBY modes is possible only when the RTC clock source
  93. is LSE or LSI.
  94. *** Callback registration ***
  95. =============================================
  96. [..]
  97. The compilation define USE_HAL_RTC_REGISTER_CALLBACKS when set to 1
  98. allows the user to configure dynamically the driver callbacks.
  99. Use Function @ref HAL_RTC_RegisterCallback() to register an interrupt callback.
  100. [..]
  101. Function @ref HAL_RTC_RegisterCallback() allows to register following callbacks:
  102. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  103. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  104. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  105. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  106. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  107. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  108. (+) MspInitCallback : RTC MspInit callback.
  109. (+) MspDeInitCallback : RTC MspDeInit callback.
  110. [..]
  111. This function takes as parameters the HAL peripheral handle, the Callback ID
  112. and a pointer to the user callback function.
  113. [..]
  114. Use function @ref HAL_RTC_UnRegisterCallback() to reset a callback to the default
  115. weak function.
  116. @ref HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  117. and the Callback ID.
  118. This function allows to reset following callbacks:
  119. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  120. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  121. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  122. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  123. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  124. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  125. (+) MspInitCallback : RTC MspInit callback.
  126. (+) MspDeInitCallback : RTC MspDeInit callback.
  127. [..]
  128. By default, after the @ref HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  129. all callbacks are set to the corresponding weak functions :
  130. examples @ref AlarmAEventCallback(), @ref WakeUpTimerEventCallback().
  131. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  132. in the @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit() only when these callbacks are null
  133. (not registered beforehand).
  134. If not, MspInit or MspDeInit are not null, @ref HAL_RTC_Init()/@ref HAL_RTC_DeInit()
  135. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  136. [..]
  137. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  138. Exception done MspInit/MspDeInit that can be registered/unregistered
  139. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  140. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  141. In that case first register the MspInit/MspDeInit user callbacks
  142. using @ref HAL_RTC_RegisterCallback() before calling @ref HAL_RTC_DeInit()
  143. or @ref HAL_RTC_Init() function.
  144. [..]
  145. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  146. not defined, the callback registration feature is not available and all callbacks
  147. are set to the corresponding weak functions.
  148. @endverbatim
  149. ******************************************************************************
  150. * @attention
  151. *
  152. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  153. * All rights reserved.</center></h2>
  154. *
  155. * This software component is licensed by ST under BSD 3-Clause license,
  156. * the "License"; You may not use this file except in compliance with the
  157. * License. You may obtain a copy of the License at:
  158. * opensource.org/licenses/BSD-3-Clause
  159. *
  160. ******************************************************************************
  161. */
  162. /* Includes ------------------------------------------------------------------*/
  163. #include "stm32f4xx_hal.h"
  164. /** @addtogroup STM32F4xx_HAL_Driver
  165. * @{
  166. */
  167. /** @defgroup RTC RTC
  168. * @brief RTC HAL module driver
  169. * @{
  170. */
  171. #ifdef HAL_RTC_MODULE_ENABLED
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private define ------------------------------------------------------------*/
  174. /* Private macro -------------------------------------------------------------*/
  175. /* Private variables ---------------------------------------------------------*/
  176. /* Private function prototypes -----------------------------------------------*/
  177. /* Exported functions --------------------------------------------------------*/
  178. /** @defgroup RTC_Exported_Functions RTC Exported Functions
  179. * @{
  180. */
  181. /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
  182. * @brief Initialization and Configuration functions
  183. *
  184. @verbatim
  185. ===============================================================================
  186. ##### Initialization and de-initialization functions #####
  187. ===============================================================================
  188. [..] This section provides functions allowing to initialize and configure the
  189. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  190. RTC registers Write protection, enter and exit the RTC initialization mode,
  191. RTC registers synchronization check and reference clock detection enable.
  192. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  193. It is split into 2 programmable prescalers to minimize power consumption.
  194. (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
  195. (++) When both prescalers are used, it is recommended to configure the
  196. asynchronous prescaler to a high value to minimize power consumption.
  197. (#) All RTC registers are Write protected. Writing to the RTC registers
  198. is enabled by writing a key into the Write Protection register, RTC_WPR.
  199. (#) To configure the RTC Calendar, user application should enter
  200. initialization mode. In this mode, the calendar counter is stopped
  201. and its value can be updated. When the initialization sequence is
  202. complete, the calendar restarts counting after 4 RTCCLK cycles.
  203. (#) To read the calendar through the shadow registers after Calendar
  204. initialization, calendar update or after wake-up from low power modes
  205. the software must first clear the RSF flag. The software must then
  206. wait until it is set again before reading the calendar, which means
  207. that the calendar registers have been correctly copied into the
  208. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  209. implements the above software sequence (RSF clear and RSF check).
  210. @endverbatim
  211. * @{
  212. */
  213. /**
  214. * @brief Initializes the RTC peripheral
  215. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  216. * the configuration information for RTC.
  217. * @retval HAL status
  218. */
  219. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  220. {
  221. /* Check the RTC peripheral state */
  222. if(hrtc == NULL)
  223. {
  224. return HAL_ERROR;
  225. }
  226. /* Check the parameters */
  227. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  228. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  229. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  230. assert_param (IS_RTC_OUTPUT(hrtc->Init.OutPut));
  231. assert_param (IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  232. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  233. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  234. if(hrtc->State == HAL_RTC_STATE_RESET)
  235. {
  236. /* Allocate lock resource and initialize it */
  237. hrtc->Lock = HAL_UNLOCKED;
  238. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  239. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  240. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  241. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  242. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  243. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  244. if(hrtc->MspInitCallback == NULL)
  245. {
  246. hrtc->MspInitCallback = HAL_RTC_MspInit;
  247. }
  248. /* Init the low level hardware */
  249. hrtc->MspInitCallback(hrtc);
  250. if(hrtc->MspDeInitCallback == NULL)
  251. {
  252. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  253. }
  254. }
  255. #else
  256. if(hrtc->State == HAL_RTC_STATE_RESET)
  257. {
  258. /* Allocate lock resource and initialize it */
  259. hrtc->Lock = HAL_UNLOCKED;
  260. /* Initialize RTC MSP */
  261. HAL_RTC_MspInit(hrtc);
  262. }
  263. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  264. /* Set RTC state */
  265. hrtc->State = HAL_RTC_STATE_BUSY;
  266. /* Disable the write protection for RTC registers */
  267. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  268. /* Set Initialization mode */
  269. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  270. {
  271. /* Enable the write protection for RTC registers */
  272. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  273. /* Set RTC state */
  274. hrtc->State = HAL_RTC_STATE_ERROR;
  275. return HAL_ERROR;
  276. }
  277. else
  278. {
  279. /* Clear RTC_CR FMT, OSEL and POL Bits */
  280. hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  281. /* Set RTC_CR register */
  282. hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  283. /* Configure the RTC PRER */
  284. hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
  285. hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16U);
  286. /* Exit Initialization mode */
  287. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  288. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  289. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  290. {
  291. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  292. {
  293. /* Enable the write protection for RTC registers */
  294. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  295. hrtc->State = HAL_RTC_STATE_ERROR;
  296. return HAL_ERROR;
  297. }
  298. }
  299. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  300. hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
  301. /* Enable the write protection for RTC registers */
  302. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  303. /* Set RTC state */
  304. hrtc->State = HAL_RTC_STATE_READY;
  305. return HAL_OK;
  306. }
  307. }
  308. /**
  309. * @brief DeInitializes the RTC peripheral
  310. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  311. * the configuration information for RTC.
  312. * @note This function doesn't reset the RTC Backup Data registers.
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  316. {
  317. uint32_t tickstart = 0U;
  318. /* Set RTC state */
  319. hrtc->State = HAL_RTC_STATE_BUSY;
  320. /* Disable the write protection for RTC registers */
  321. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  322. /* Set Initialization mode */
  323. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  324. {
  325. /* Enable the write protection for RTC registers */
  326. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  327. /* Set RTC state */
  328. hrtc->State = HAL_RTC_STATE_ERROR;
  329. return HAL_ERROR;
  330. }
  331. else
  332. {
  333. /* Reset TR, DR and CR registers */
  334. hrtc->Instance->TR = 0x00000000U;
  335. hrtc->Instance->DR = 0x00002101U;
  336. /* Reset All CR bits except CR[2:0] */
  337. hrtc->Instance->CR &= 0x00000007U;
  338. /* Get tick */
  339. tickstart = HAL_GetTick();
  340. /* Wait till WUTWF flag is set and if Time out is reached exit */
  341. while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
  342. {
  343. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  344. {
  345. /* Enable the write protection for RTC registers */
  346. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  347. /* Set RTC state */
  348. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  349. return HAL_TIMEOUT;
  350. }
  351. }
  352. /* Reset all RTC CR register bits */
  353. hrtc->Instance->CR &= 0x00000000U;
  354. hrtc->Instance->WUTR = 0x0000FFFFU;
  355. hrtc->Instance->PRER = 0x007F00FFU;
  356. hrtc->Instance->CALIBR = 0x00000000U;
  357. hrtc->Instance->ALRMAR = 0x00000000U;
  358. hrtc->Instance->ALRMBR = 0x00000000U;
  359. hrtc->Instance->SHIFTR = 0x00000000U;
  360. hrtc->Instance->CALR = 0x00000000U;
  361. hrtc->Instance->ALRMASSR = 0x00000000U;
  362. hrtc->Instance->ALRMBSSR = 0x00000000U;
  363. /* Reset ISR register and exit initialization mode */
  364. hrtc->Instance->ISR = 0x00000000U;
  365. /* Reset Tamper and alternate functions configuration register */
  366. hrtc->Instance->TAFCR = 0x00000000U;
  367. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  368. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  369. {
  370. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  371. {
  372. /* Enable the write protection for RTC registers */
  373. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  374. hrtc->State = HAL_RTC_STATE_ERROR;
  375. return HAL_ERROR;
  376. }
  377. }
  378. }
  379. /* Enable the write protection for RTC registers */
  380. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  381. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  382. if(hrtc->MspDeInitCallback == NULL)
  383. {
  384. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  385. }
  386. /* DeInit the low level hardware: CLOCK, NVIC.*/
  387. hrtc->MspDeInitCallback(hrtc);
  388. #else
  389. /* De-Initialize RTC MSP */
  390. HAL_RTC_MspDeInit(hrtc);
  391. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  392. hrtc->State = HAL_RTC_STATE_RESET;
  393. /* Release Lock */
  394. __HAL_UNLOCK(hrtc);
  395. return HAL_OK;
  396. }
  397. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  398. /**
  399. * @brief Register a User RTC Callback
  400. * To be used instead of the weak predefined callback
  401. * @param hrtc RTC handle
  402. * @param CallbackID ID of the callback to be registered
  403. * This parameter can be one of the following values:
  404. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  405. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  406. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  407. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID Wake-Up Timer Event Callback ID
  408. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  409. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  410. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  411. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  412. * @param pCallback pointer to the Callback function
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
  416. {
  417. HAL_StatusTypeDef status = HAL_OK;
  418. if(pCallback == NULL)
  419. {
  420. return HAL_ERROR;
  421. }
  422. /* Process locked */
  423. __HAL_LOCK(hrtc);
  424. if(HAL_RTC_STATE_READY == hrtc->State)
  425. {
  426. switch (CallbackID)
  427. {
  428. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  429. hrtc->AlarmAEventCallback = pCallback;
  430. break;
  431. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  432. hrtc->AlarmBEventCallback = pCallback;
  433. break;
  434. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  435. hrtc->TimeStampEventCallback = pCallback;
  436. break;
  437. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  438. hrtc->WakeUpTimerEventCallback = pCallback;
  439. break;
  440. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  441. hrtc->Tamper1EventCallback = pCallback;
  442. break;
  443. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  444. hrtc->Tamper2EventCallback = pCallback;
  445. break;
  446. case HAL_RTC_MSPINIT_CB_ID :
  447. hrtc->MspInitCallback = pCallback;
  448. break;
  449. case HAL_RTC_MSPDEINIT_CB_ID :
  450. hrtc->MspDeInitCallback = pCallback;
  451. break;
  452. default :
  453. /* Return error status */
  454. status = HAL_ERROR;
  455. break;
  456. }
  457. }
  458. else if(HAL_RTC_STATE_RESET == hrtc->State)
  459. {
  460. switch (CallbackID)
  461. {
  462. case HAL_RTC_MSPINIT_CB_ID :
  463. hrtc->MspInitCallback = pCallback;
  464. break;
  465. case HAL_RTC_MSPDEINIT_CB_ID :
  466. hrtc->MspDeInitCallback = pCallback;
  467. break;
  468. default :
  469. /* Return error status */
  470. status = HAL_ERROR;
  471. break;
  472. }
  473. }
  474. else
  475. {
  476. /* Return error status */
  477. status = HAL_ERROR;
  478. }
  479. /* Release Lock */
  480. __HAL_UNLOCK(hrtc);
  481. return status;
  482. }
  483. /**
  484. * @brief Unregister an RTC Callback
  485. * RTC callabck is redirected to the weak predefined callback
  486. * @param hrtc RTC handle
  487. * @param CallbackID ID of the callback to be unregistered
  488. * This parameter can be one of the following values:
  489. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  490. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  491. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  492. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID Wake-Up Timer Event Callback ID
  493. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  494. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  495. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  496. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  497. * @retval HAL status
  498. */
  499. HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
  500. {
  501. HAL_StatusTypeDef status = HAL_OK;
  502. /* Process locked */
  503. __HAL_LOCK(hrtc);
  504. if(HAL_RTC_STATE_READY == hrtc->State)
  505. {
  506. switch (CallbackID)
  507. {
  508. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  509. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  510. break;
  511. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  512. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  513. break;
  514. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  515. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  516. break;
  517. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  518. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  519. break;
  520. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  521. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  522. break;
  523. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  524. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  525. break;
  526. case HAL_RTC_MSPINIT_CB_ID :
  527. hrtc->MspInitCallback = HAL_RTC_MspInit;
  528. break;
  529. case HAL_RTC_MSPDEINIT_CB_ID :
  530. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  531. break;
  532. default :
  533. /* Return error status */
  534. status = HAL_ERROR;
  535. break;
  536. }
  537. }
  538. else if(HAL_RTC_STATE_RESET == hrtc->State)
  539. {
  540. switch (CallbackID)
  541. {
  542. case HAL_RTC_MSPINIT_CB_ID :
  543. hrtc->MspInitCallback = HAL_RTC_MspInit;
  544. break;
  545. case HAL_RTC_MSPDEINIT_CB_ID :
  546. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  547. break;
  548. default :
  549. /* Return error status */
  550. status = HAL_ERROR;
  551. break;
  552. }
  553. }
  554. else
  555. {
  556. /* Return error status */
  557. status = HAL_ERROR;
  558. }
  559. /* Release Lock */
  560. __HAL_UNLOCK(hrtc);
  561. return status;
  562. }
  563. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  564. /**
  565. * @brief Initializes the RTC MSP.
  566. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  567. * the configuration information for RTC.
  568. * @retval None
  569. */
  570. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  571. {
  572. /* Prevent unused argument(s) compilation warning */
  573. UNUSED(hrtc);
  574. /* NOTE : This function Should not be modified, when the callback is needed,
  575. the HAL_RTC_MspInit could be implemented in the user file
  576. */
  577. }
  578. /**
  579. * @brief DeInitializes the RTC MSP.
  580. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  581. * the configuration information for RTC.
  582. * @retval None
  583. */
  584. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  585. {
  586. /* Prevent unused argument(s) compilation warning */
  587. UNUSED(hrtc);
  588. /* NOTE : This function Should not be modified, when the callback is needed,
  589. the HAL_RTC_MspDeInit could be implemented in the user file
  590. */
  591. }
  592. /**
  593. * @}
  594. */
  595. /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
  596. * @brief RTC Time and Date functions
  597. *
  598. @verbatim
  599. ===============================================================================
  600. ##### RTC Time and Date functions #####
  601. ===============================================================================
  602. [..] This section provides functions allowing to configure Time and Date features
  603. @endverbatim
  604. * @{
  605. */
  606. /**
  607. * @brief Sets RTC current time.
  608. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  609. * the configuration information for RTC.
  610. * @param sTime Pointer to Time structure
  611. * @param Format Specifies the format of the entered parameters.
  612. * This parameter can be one of the following values:
  613. * @arg RTC_FORMAT_BIN: Binary data format
  614. * @arg RTC_FORMAT_BCD: BCD data format
  615. * @retval HAL status
  616. */
  617. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  618. {
  619. uint32_t tmpreg = 0U;
  620. /* Check the parameters */
  621. assert_param(IS_RTC_FORMAT(Format));
  622. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  623. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  624. /* Process Locked */
  625. __HAL_LOCK(hrtc);
  626. hrtc->State = HAL_RTC_STATE_BUSY;
  627. if(Format == RTC_FORMAT_BIN)
  628. {
  629. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  630. {
  631. assert_param(IS_RTC_HOUR12(sTime->Hours));
  632. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  633. }
  634. else
  635. {
  636. sTime->TimeFormat = 0x00U;
  637. assert_param(IS_RTC_HOUR24(sTime->Hours));
  638. }
  639. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  640. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  641. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
  642. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
  643. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
  644. (((uint32_t)sTime->TimeFormat) << 16U));
  645. }
  646. else
  647. {
  648. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  649. {
  650. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
  651. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  652. }
  653. else
  654. {
  655. sTime->TimeFormat = 0x00U;
  656. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  657. }
  658. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  659. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  660. tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
  661. ((uint32_t)(sTime->Minutes) << 8U) | \
  662. ((uint32_t)sTime->Seconds) | \
  663. ((uint32_t)(sTime->TimeFormat) << 16U));
  664. }
  665. /* Disable the write protection for RTC registers */
  666. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  667. /* Set Initialization mode */
  668. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  669. {
  670. /* Enable the write protection for RTC registers */
  671. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  672. /* Set RTC state */
  673. hrtc->State = HAL_RTC_STATE_ERROR;
  674. /* Process Unlocked */
  675. __HAL_UNLOCK(hrtc);
  676. return HAL_ERROR;
  677. }
  678. else
  679. {
  680. /* Set the RTC_TR register */
  681. hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  682. /* Clear the bits to be configured */
  683. hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
  684. /* Configure the RTC_CR register */
  685. hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
  686. /* Exit Initialization mode */
  687. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  688. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  689. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  690. {
  691. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  692. {
  693. /* Enable the write protection for RTC registers */
  694. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  695. hrtc->State = HAL_RTC_STATE_ERROR;
  696. /* Process Unlocked */
  697. __HAL_UNLOCK(hrtc);
  698. return HAL_ERROR;
  699. }
  700. }
  701. /* Enable the write protection for RTC registers */
  702. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  703. hrtc->State = HAL_RTC_STATE_READY;
  704. __HAL_UNLOCK(hrtc);
  705. return HAL_OK;
  706. }
  707. }
  708. /**
  709. * @brief Gets RTC current time.
  710. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  711. * the configuration information for RTC.
  712. * @param sTime Pointer to Time structure
  713. * @param Format Specifies the format of the entered parameters.
  714. * This parameter can be one of the following values:
  715. * @arg RTC_FORMAT_BIN: Binary data format
  716. * @arg RTC_FORMAT_BCD: BCD data format
  717. * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  718. * value in second fraction ratio with time unit following generic formula:
  719. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  720. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  721. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  722. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  723. * Reading RTC current time locks the values in calendar shadow registers until current date is read.
  724. * @retval HAL status
  725. */
  726. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  727. {
  728. uint32_t tmpreg = 0U;
  729. /* Check the parameters */
  730. assert_param(IS_RTC_FORMAT(Format));
  731. /* Get subseconds structure field from the corresponding register */
  732. sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
  733. /* Get SecondFraction structure field from the corresponding register field*/
  734. sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
  735. /* Get the TR register */
  736. tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
  737. /* Fill the structure fields with the read parameters */
  738. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  739. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  740. sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  741. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
  742. /* Check the input parameters format */
  743. if(Format == RTC_FORMAT_BIN)
  744. {
  745. /* Convert the time structure parameters to Binary format */
  746. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  747. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  748. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  749. }
  750. return HAL_OK;
  751. }
  752. /**
  753. * @brief Sets RTC current date.
  754. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  755. * the configuration information for RTC.
  756. * @param sDate Pointer to date structure
  757. * @param Format specifies the format of the entered parameters.
  758. * This parameter can be one of the following values:
  759. * @arg RTC_FORMAT_BIN: Binary data format
  760. * @arg RTC_FORMAT_BCD: BCD data format
  761. * @retval HAL status
  762. */
  763. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  764. {
  765. uint32_t datetmpreg = 0U;
  766. /* Check the parameters */
  767. assert_param(IS_RTC_FORMAT(Format));
  768. /* Process Locked */
  769. __HAL_LOCK(hrtc);
  770. hrtc->State = HAL_RTC_STATE_BUSY;
  771. if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  772. {
  773. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  774. }
  775. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  776. if(Format == RTC_FORMAT_BIN)
  777. {
  778. assert_param(IS_RTC_YEAR(sDate->Year));
  779. assert_param(IS_RTC_MONTH(sDate->Month));
  780. assert_param(IS_RTC_DATE(sDate->Date));
  781. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
  782. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
  783. ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
  784. ((uint32_t)sDate->WeekDay << 13U));
  785. }
  786. else
  787. {
  788. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  789. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  790. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  791. datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
  792. (((uint32_t)sDate->Month) << 8U) | \
  793. ((uint32_t)sDate->Date) | \
  794. (((uint32_t)sDate->WeekDay) << 13U));
  795. }
  796. /* Disable the write protection for RTC registers */
  797. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  798. /* Set Initialization mode */
  799. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  800. {
  801. /* Enable the write protection for RTC registers */
  802. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  803. /* Set RTC state*/
  804. hrtc->State = HAL_RTC_STATE_ERROR;
  805. /* Process Unlocked */
  806. __HAL_UNLOCK(hrtc);
  807. return HAL_ERROR;
  808. }
  809. else
  810. {
  811. /* Set the RTC_DR register */
  812. hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
  813. /* Exit Initialization mode */
  814. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  815. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  816. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  817. {
  818. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  819. {
  820. /* Enable the write protection for RTC registers */
  821. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  822. hrtc->State = HAL_RTC_STATE_ERROR;
  823. /* Process Unlocked */
  824. __HAL_UNLOCK(hrtc);
  825. return HAL_ERROR;
  826. }
  827. }
  828. /* Enable the write protection for RTC registers */
  829. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  830. hrtc->State = HAL_RTC_STATE_READY ;
  831. /* Process Unlocked */
  832. __HAL_UNLOCK(hrtc);
  833. return HAL_OK;
  834. }
  835. }
  836. /**
  837. * @brief Gets RTC current date.
  838. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  839. * the configuration information for RTC.
  840. * @param sDate Pointer to Date structure
  841. * @param Format Specifies the format of the entered parameters.
  842. * This parameter can be one of the following values:
  843. * @arg RTC_FORMAT_BIN: Binary data format
  844. * @arg RTC_FORMAT_BCD: BCD data format
  845. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  846. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  847. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  848. * @retval HAL status
  849. */
  850. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  851. {
  852. uint32_t datetmpreg = 0U;
  853. /* Check the parameters */
  854. assert_param(IS_RTC_FORMAT(Format));
  855. /* Get the DR register */
  856. datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
  857. /* Fill the structure fields with the read parameters */
  858. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
  859. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  860. sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
  861. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
  862. /* Check the input parameters format */
  863. if(Format == RTC_FORMAT_BIN)
  864. {
  865. /* Convert the date structure parameters to Binary format */
  866. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  867. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  868. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  869. }
  870. return HAL_OK;
  871. }
  872. /**
  873. * @}
  874. */
  875. /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
  876. * @brief RTC Alarm functions
  877. *
  878. @verbatim
  879. ===============================================================================
  880. ##### RTC Alarm functions #####
  881. ===============================================================================
  882. [..] This section provides functions allowing to configure Alarm feature
  883. @endverbatim
  884. * @{
  885. */
  886. /**
  887. * @brief Sets the specified RTC Alarm.
  888. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  889. * the configuration information for RTC.
  890. * @param sAlarm Pointer to Alarm structure
  891. * @param Format Specifies the format of the entered parameters.
  892. * This parameter can be one of the following values:
  893. * @arg RTC_FORMAT_BIN: Binary data format
  894. * @arg RTC_FORMAT_BCD: BCD data format
  895. * @retval HAL status
  896. */
  897. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  898. {
  899. uint32_t tickstart = 0U;
  900. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  901. /* Check the parameters */
  902. assert_param(IS_RTC_FORMAT(Format));
  903. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  904. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  905. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  906. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  907. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  908. /* Process Locked */
  909. __HAL_LOCK(hrtc);
  910. hrtc->State = HAL_RTC_STATE_BUSY;
  911. if(Format == RTC_FORMAT_BIN)
  912. {
  913. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  914. {
  915. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  916. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  917. }
  918. else
  919. {
  920. sAlarm->AlarmTime.TimeFormat = 0x00U;
  921. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  922. }
  923. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  924. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  925. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  926. {
  927. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  928. }
  929. else
  930. {
  931. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  932. }
  933. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  934. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  935. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  936. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  937. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  938. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  939. ((uint32_t)sAlarm->AlarmMask));
  940. }
  941. else
  942. {
  943. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  944. {
  945. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  946. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  947. }
  948. else
  949. {
  950. sAlarm->AlarmTime.TimeFormat = 0x00U;
  951. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  952. }
  953. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  954. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  955. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  956. {
  957. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  958. }
  959. else
  960. {
  961. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  962. }
  963. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  964. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  965. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  966. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  967. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  968. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  969. ((uint32_t)sAlarm->AlarmMask));
  970. }
  971. /* Configure the Alarm A or Alarm B Sub Second registers */
  972. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  973. /* Disable the write protection for RTC registers */
  974. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  975. /* Configure the Alarm register */
  976. if(sAlarm->Alarm == RTC_ALARM_A)
  977. {
  978. /* Disable the Alarm A interrupt */
  979. __HAL_RTC_ALARMA_DISABLE(hrtc);
  980. /* In case of interrupt mode is used, the interrupt source must disabled */
  981. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  982. /* Get tick */
  983. tickstart = HAL_GetTick();
  984. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  985. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  986. {
  987. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  988. {
  989. /* Enable the write protection for RTC registers */
  990. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  991. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  992. /* Process Unlocked */
  993. __HAL_UNLOCK(hrtc);
  994. return HAL_TIMEOUT;
  995. }
  996. }
  997. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  998. /* Configure the Alarm A Sub Second register */
  999. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1000. /* Configure the Alarm state: Enable Alarm */
  1001. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1002. }
  1003. else
  1004. {
  1005. /* Disable the Alarm B interrupt */
  1006. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1007. /* In case of interrupt mode is used, the interrupt source must disabled */
  1008. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  1009. /* Get tick */
  1010. tickstart = HAL_GetTick();
  1011. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1012. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  1013. {
  1014. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1015. {
  1016. /* Enable the write protection for RTC registers */
  1017. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1018. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1019. /* Process Unlocked */
  1020. __HAL_UNLOCK(hrtc);
  1021. return HAL_TIMEOUT;
  1022. }
  1023. }
  1024. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1025. /* Configure the Alarm B Sub Second register */
  1026. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1027. /* Configure the Alarm state: Enable Alarm */
  1028. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1029. }
  1030. /* Enable the write protection for RTC registers */
  1031. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1032. /* Change RTC state */
  1033. hrtc->State = HAL_RTC_STATE_READY;
  1034. /* Process Unlocked */
  1035. __HAL_UNLOCK(hrtc);
  1036. return HAL_OK;
  1037. }
  1038. /**
  1039. * @brief Sets the specified RTC Alarm with Interrupt
  1040. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1041. * the configuration information for RTC.
  1042. * @param sAlarm Pointer to Alarm structure
  1043. * @param Format Specifies the format of the entered parameters.
  1044. * This parameter can be one of the following values:
  1045. * @arg RTC_FORMAT_BIN: Binary data format
  1046. * @arg RTC_FORMAT_BCD: BCD data format
  1047. * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1050. {
  1051. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  1052. __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U) ;
  1053. /* Check the parameters */
  1054. assert_param(IS_RTC_FORMAT(Format));
  1055. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1056. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1057. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1058. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1059. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1060. /* Process Locked */
  1061. __HAL_LOCK(hrtc);
  1062. hrtc->State = HAL_RTC_STATE_BUSY;
  1063. if(Format == RTC_FORMAT_BIN)
  1064. {
  1065. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  1066. {
  1067. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1068. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1069. }
  1070. else
  1071. {
  1072. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1073. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1074. }
  1075. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1076. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1077. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1078. {
  1079. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1080. }
  1081. else
  1082. {
  1083. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1084. }
  1085. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  1086. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  1087. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  1088. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  1089. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  1090. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1091. ((uint32_t)sAlarm->AlarmMask));
  1092. }
  1093. else
  1094. {
  1095. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  1096. {
  1097. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1098. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1099. }
  1100. else
  1101. {
  1102. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1103. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1104. }
  1105. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1106. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1107. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1108. {
  1109. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1110. }
  1111. else
  1112. {
  1113. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1114. }
  1115. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  1116. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  1117. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  1118. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  1119. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  1120. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1121. ((uint32_t)sAlarm->AlarmMask));
  1122. }
  1123. /* Configure the Alarm A or Alarm B Sub Second registers */
  1124. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1125. /* Disable the write protection for RTC registers */
  1126. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1127. /* Configure the Alarm register */
  1128. if(sAlarm->Alarm == RTC_ALARM_A)
  1129. {
  1130. /* Disable the Alarm A interrupt */
  1131. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1132. /* Clear flag alarm A */
  1133. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1134. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1135. do
  1136. {
  1137. if (count-- == 0U)
  1138. {
  1139. /* Enable the write protection for RTC registers */
  1140. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1141. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1142. /* Process Unlocked */
  1143. __HAL_UNLOCK(hrtc);
  1144. return HAL_TIMEOUT;
  1145. }
  1146. }
  1147. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET);
  1148. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  1149. /* Configure the Alarm A Sub Second register */
  1150. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1151. /* Configure the Alarm state: Enable Alarm */
  1152. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1153. /* Configure the Alarm interrupt */
  1154. __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
  1155. }
  1156. else
  1157. {
  1158. /* Disable the Alarm B interrupt */
  1159. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1160. /* Clear flag alarm B */
  1161. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1162. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1163. do
  1164. {
  1165. if (count-- == 0U)
  1166. {
  1167. /* Enable the write protection for RTC registers */
  1168. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1169. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1170. /* Process Unlocked */
  1171. __HAL_UNLOCK(hrtc);
  1172. return HAL_TIMEOUT;
  1173. }
  1174. }
  1175. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET);
  1176. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1177. /* Configure the Alarm B Sub Second register */
  1178. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1179. /* Configure the Alarm state: Enable Alarm */
  1180. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1181. /* Configure the Alarm interrupt */
  1182. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
  1183. }
  1184. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1185. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1186. EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
  1187. /* Enable the write protection for RTC registers */
  1188. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1189. hrtc->State = HAL_RTC_STATE_READY;
  1190. /* Process Unlocked */
  1191. __HAL_UNLOCK(hrtc);
  1192. return HAL_OK;
  1193. }
  1194. /**
  1195. * @brief Deactivate the specified RTC Alarm
  1196. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1197. * the configuration information for RTC.
  1198. * @param Alarm Specifies the Alarm.
  1199. * This parameter can be one of the following values:
  1200. * @arg RTC_ALARM_A: AlarmA
  1201. * @arg RTC_ALARM_B: AlarmB
  1202. * @retval HAL status
  1203. */
  1204. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  1205. {
  1206. uint32_t tickstart = 0U;
  1207. /* Check the parameters */
  1208. assert_param(IS_RTC_ALARM(Alarm));
  1209. /* Process Locked */
  1210. __HAL_LOCK(hrtc);
  1211. hrtc->State = HAL_RTC_STATE_BUSY;
  1212. /* Disable the write protection for RTC registers */
  1213. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1214. if(Alarm == RTC_ALARM_A)
  1215. {
  1216. /* AlarmA */
  1217. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1218. /* In case of interrupt mode is used, the interrupt source must disabled */
  1219. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  1220. /* Get tick */
  1221. tickstart = HAL_GetTick();
  1222. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1223. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  1224. {
  1225. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1226. {
  1227. /* Enable the write protection for RTC registers */
  1228. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1229. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1230. /* Process Unlocked */
  1231. __HAL_UNLOCK(hrtc);
  1232. return HAL_TIMEOUT;
  1233. }
  1234. }
  1235. }
  1236. else
  1237. {
  1238. /* AlarmB */
  1239. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1240. /* In case of interrupt mode is used, the interrupt source must disabled */
  1241. __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
  1242. /* Get tick */
  1243. tickstart = HAL_GetTick();
  1244. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1245. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  1246. {
  1247. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1248. {
  1249. /* Enable the write protection for RTC registers */
  1250. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1251. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1252. /* Process Unlocked */
  1253. __HAL_UNLOCK(hrtc);
  1254. return HAL_TIMEOUT;
  1255. }
  1256. }
  1257. }
  1258. /* Enable the write protection for RTC registers */
  1259. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1260. hrtc->State = HAL_RTC_STATE_READY;
  1261. /* Process Unlocked */
  1262. __HAL_UNLOCK(hrtc);
  1263. return HAL_OK;
  1264. }
  1265. /**
  1266. * @brief Gets the RTC Alarm value and masks.
  1267. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1268. * the configuration information for RTC.
  1269. * @param sAlarm Pointer to Date structure
  1270. * @param Alarm Specifies the Alarm.
  1271. * This parameter can be one of the following values:
  1272. * @arg RTC_ALARM_A: AlarmA
  1273. * @arg RTC_ALARM_B: AlarmB
  1274. * @param Format Specifies the format of the entered parameters.
  1275. * This parameter can be one of the following values:
  1276. * @arg RTC_FORMAT_BIN: Binary data format
  1277. * @arg RTC_FORMAT_BCD: BCD data format
  1278. * @retval HAL status
  1279. */
  1280. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1281. {
  1282. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  1283. /* Check the parameters */
  1284. assert_param(IS_RTC_FORMAT(Format));
  1285. assert_param(IS_RTC_ALARM(Alarm));
  1286. if(Alarm == RTC_ALARM_A)
  1287. {
  1288. /* AlarmA */
  1289. sAlarm->Alarm = RTC_ALARM_A;
  1290. tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
  1291. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
  1292. }
  1293. else
  1294. {
  1295. sAlarm->Alarm = RTC_ALARM_B;
  1296. tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
  1297. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  1298. }
  1299. /* Fill the structure with the read parameters */
  1300. sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
  1301. sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
  1302. sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
  1303. sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
  1304. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1305. sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24U);
  1306. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1307. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1308. if(Format == RTC_FORMAT_BIN)
  1309. {
  1310. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  1311. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  1312. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  1313. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  1314. }
  1315. return HAL_OK;
  1316. }
  1317. /**
  1318. * @brief This function handles Alarm interrupt request.
  1319. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1320. * the configuration information for RTC.
  1321. * @retval None
  1322. */
  1323. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
  1324. {
  1325. /* Get the AlarmA interrupt source enable status */
  1326. if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != (uint32_t)RESET)
  1327. {
  1328. /* Get the pending status of the AlarmA Interrupt */
  1329. if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
  1330. {
  1331. /* AlarmA callback */
  1332. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1333. hrtc->AlarmAEventCallback(hrtc);
  1334. #else
  1335. HAL_RTC_AlarmAEventCallback(hrtc);
  1336. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1337. /* Clear the AlarmA interrupt pending bit */
  1338. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
  1339. }
  1340. }
  1341. /* Get the AlarmB interrupt source enable status */
  1342. if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != (uint32_t)RESET)
  1343. {
  1344. /* Get the pending status of the AlarmB Interrupt */
  1345. if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != (uint32_t)RESET)
  1346. {
  1347. /* AlarmB callback */
  1348. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1349. hrtc->AlarmBEventCallback(hrtc);
  1350. #else
  1351. HAL_RTCEx_AlarmBEventCallback(hrtc);
  1352. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1353. /* Clear the AlarmB interrupt pending bit */
  1354. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
  1355. }
  1356. }
  1357. /* Clear the EXTI's line Flag for RTC Alarm */
  1358. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  1359. /* Change RTC state */
  1360. hrtc->State = HAL_RTC_STATE_READY;
  1361. }
  1362. /**
  1363. * @brief Alarm A callback.
  1364. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1365. * the configuration information for RTC.
  1366. * @retval None
  1367. */
  1368. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1369. {
  1370. /* Prevent unused argument(s) compilation warning */
  1371. UNUSED(hrtc);
  1372. /* NOTE : This function should not be modified, when the callback is needed,
  1373. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1374. */
  1375. }
  1376. /**
  1377. * @brief This function handles AlarmA Polling request.
  1378. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1379. * the configuration information for RTC.
  1380. * @param Timeout Timeout duration
  1381. * @retval HAL status
  1382. */
  1383. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1384. {
  1385. uint32_t tickstart = 0U;
  1386. /* Get tick */
  1387. tickstart = HAL_GetTick();
  1388. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
  1389. {
  1390. if(Timeout != HAL_MAX_DELAY)
  1391. {
  1392. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1393. {
  1394. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1395. return HAL_TIMEOUT;
  1396. }
  1397. }
  1398. }
  1399. /* Clear the Alarm interrupt pending bit */
  1400. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1401. /* Change RTC state */
  1402. hrtc->State = HAL_RTC_STATE_READY;
  1403. return HAL_OK;
  1404. }
  1405. /**
  1406. * @}
  1407. */
  1408. /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
  1409. * @brief Peripheral Control functions
  1410. *
  1411. @verbatim
  1412. ===============================================================================
  1413. ##### Peripheral Control functions #####
  1414. ===============================================================================
  1415. [..]
  1416. This subsection provides functions allowing to
  1417. (+) Wait for RTC Time and Date Synchronization
  1418. @endverbatim
  1419. * @{
  1420. */
  1421. /**
  1422. * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  1423. * synchronized with RTC APB clock.
  1424. * @note The RTC Resynchronization mode is write protected, use the
  1425. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1426. * @note To read the calendar through the shadow registers after Calendar
  1427. * initialization, calendar update or after wake-up from low power modes
  1428. * the software must first clear the RSF flag.
  1429. * The software must then wait until it is set again before reading
  1430. * the calendar, which means that the calendar registers have been
  1431. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  1432. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1433. * the configuration information for RTC.
  1434. * @retval HAL status
  1435. */
  1436. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
  1437. {
  1438. uint32_t tickstart = 0U;
  1439. /* Clear RSF flag */
  1440. hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
  1441. /* Get tick */
  1442. tickstart = HAL_GetTick();
  1443. /* Wait the registers to be synchronised */
  1444. while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
  1445. {
  1446. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1447. {
  1448. return HAL_TIMEOUT;
  1449. }
  1450. }
  1451. return HAL_OK;
  1452. }
  1453. /**
  1454. * @}
  1455. */
  1456. /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
  1457. * @brief Peripheral State functions
  1458. *
  1459. @verbatim
  1460. ===============================================================================
  1461. ##### Peripheral State functions #####
  1462. ===============================================================================
  1463. [..]
  1464. This subsection provides functions allowing to
  1465. (+) Get RTC state
  1466. @endverbatim
  1467. * @{
  1468. */
  1469. /**
  1470. * @brief Returns the RTC state.
  1471. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1472. * the configuration information for RTC.
  1473. * @retval HAL state
  1474. */
  1475. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
  1476. {
  1477. return hrtc->State;
  1478. }
  1479. /**
  1480. * @}
  1481. */
  1482. /**
  1483. * @brief Enters the RTC Initialization mode.
  1484. * @note The RTC Initialization mode is write protected, use the
  1485. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1486. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1487. * the configuration information for RTC.
  1488. * @retval HAL status
  1489. */
  1490. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
  1491. {
  1492. uint32_t tickstart = 0U;
  1493. /* Check if the Initialization mode is set */
  1494. if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  1495. {
  1496. /* Set the Initialization mode */
  1497. hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
  1498. /* Get tick */
  1499. tickstart = HAL_GetTick();
  1500. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1501. while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  1502. {
  1503. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1504. {
  1505. return HAL_TIMEOUT;
  1506. }
  1507. }
  1508. }
  1509. return HAL_OK;
  1510. }
  1511. /**
  1512. * @brief Converts a 2 digit decimal to BCD format.
  1513. * @param Value Byte to be converted
  1514. * @retval Converted byte
  1515. */
  1516. uint8_t RTC_ByteToBcd2(uint8_t Value)
  1517. {
  1518. uint32_t bcdhigh = 0U;
  1519. while(Value >= 10U)
  1520. {
  1521. bcdhigh++;
  1522. Value -= 10U;
  1523. }
  1524. return ((uint8_t)(bcdhigh << 4U) | Value);
  1525. }
  1526. /**
  1527. * @brief Converts from 2 digit BCD to Binary.
  1528. * @param Value BCD value to be converted
  1529. * @retval Converted word
  1530. */
  1531. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1532. {
  1533. uint32_t tmp = 0U;
  1534. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1535. return (tmp + (Value & (uint8_t)0x0F));
  1536. }
  1537. /**
  1538. * @}
  1539. */
  1540. #endif /* HAL_RTC_MODULE_ENABLED */
  1541. /**
  1542. * @}
  1543. */
  1544. /**
  1545. * @}
  1546. */
  1547. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/