選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

1921 行
66 KiB

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