stm32f0xx_hal_rtc.c 63 KB

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