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

1865 lines
60 KiB

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