Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

1859 lines
74 KiB

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