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.
 
 
 

1783 lines
55 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief RTC HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Real Time Clock (RTC) Extension peripheral:
  10. * + RTC Time Stamp functions
  11. * + RTC Tamper functions
  12. * + RTC Wake-up functions
  13. * + Extension Control functions
  14. * + Extension RTC features functions
  15. *
  16. @verbatim
  17. ==============================================================================
  18. ##### How to use this driver #####
  19. ==============================================================================
  20. [..]
  21. (+) Enable the RTC domain access.
  22. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  23. format using the HAL_RTC_Init() function.
  24. *** RTC Wake-up configuration ***
  25. ================================
  26. [..]
  27. (+) To configure the RTC Wake-up Clock source and Counter use the HAL_RTC_SetWakeUpTimer()
  28. function. You can also configure the RTC Wake-up timer in interrupt mode
  29. using the HAL_RTC_SetWakeUpTimer_IT() function.
  30. (+) To read the RTC Wake-up Counter register, use the HAL_RTC_GetWakeUpTimer()
  31. function.
  32. *** TimeStamp configuration ***
  33. ===============================
  34. [..]
  35. (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
  36. HAL_RTC_SetTimeStamp() function. You can also configure the RTC TimeStamp with
  37. interrupt mode using the HAL_RTC_SetTimeStamp_IT() function.
  38. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTC_GetTimeStamp()
  39. function.
  40. (+) The TIMESTAMP alternate function can be mapped either to RTC_AF1 (PC13)
  41. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TSINSEL bit in
  42. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTimeStamp()
  43. or HAL_RTC_SetTimeStamp_IT() function.
  44. *** Tamper configuration ***
  45. ============================
  46. [..]
  47. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  48. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  49. value, sampling frequency, precharge or discharge and Pull-UP using the
  50. HAL_RTC_SetTamper() function. You can configure RTC Tamper in interrupt
  51. mode using HAL_RTC_SetTamper_IT() function.
  52. (+) The TAMPER1 alternate function can be mapped either to RTC_AF1 (PC13)
  53. or RTC_AF2 (PI8 or PA0 only for STM32F446xx devices) depending on the value of TAMP1INSEL bit in
  54. RTC_TAFCR register. The corresponding pin is also selected by HAL_RTC_SetTamper()
  55. or HAL_RTC_SetTamper_IT() function.
  56. *** Backup Data Registers configuration ***
  57. ===========================================
  58. [..]
  59. (+) To write to the RTC Backup Data registers, use the HAL_RTC_BKUPWrite()
  60. function.
  61. (+) To read the RTC Backup Data registers, use the HAL_RTC_BKUPRead()
  62. function.
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32f4xx_hal.h"
  95. /** @addtogroup STM32F4xx_HAL_Driver
  96. * @{
  97. */
  98. /** @defgroup RTCEx RTCEx
  99. * @brief RTC HAL module driver
  100. * @{
  101. */
  102. #ifdef HAL_RTC_MODULE_ENABLED
  103. /* Private typedef -----------------------------------------------------------*/
  104. /* Private define ------------------------------------------------------------*/
  105. /* Private macro -------------------------------------------------------------*/
  106. /* Private variables ---------------------------------------------------------*/
  107. /* Private function prototypes -----------------------------------------------*/
  108. /* Private functions ---------------------------------------------------------*/
  109. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  110. * @{
  111. */
  112. /** @defgroup RTCEx_Exported_Functions_Group1 RTC TimeStamp and Tamper functions
  113. * @brief RTC TimeStamp and Tamper functions
  114. *
  115. @verbatim
  116. ===============================================================================
  117. ##### RTC TimeStamp and Tamper functions #####
  118. ===============================================================================
  119. [..] This section provides functions allowing to configure TimeStamp feature
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Sets TimeStamp.
  125. * @note This API must be called before enabling the TimeStamp feature.
  126. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  127. * the configuration information for RTC.
  128. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  129. * activated.
  130. * This parameter can be one of the following values:
  131. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  132. * rising edge of the related pin.
  133. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  134. * falling edge of the related pin.
  135. * @param RTC_TimeStampPin: specifies the RTC TimeStamp Pin.
  136. * This parameter can be one of the following values:
  137. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  138. * @arg RTC_TIMESTAMPPIN_POS1: PI8/PA0 is selected as RTC TimeStamp Pin.
  139. * (not applicable in the case of STM32F412xx, STM32F413xx and STM32F423xx devices)
  140. * (PI8 for all STM32 devices except for STM32F446xx devices the PA0 is used)
  141. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  142. * @retval HAL status
  143. */
  144. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  145. {
  146. uint32_t tmpreg = 0U;
  147. /* Check the parameters */
  148. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  149. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  150. /* Process Locked */
  151. __HAL_LOCK(hrtc);
  152. hrtc->State = HAL_RTC_STATE_BUSY;
  153. /* Get the RTC_CR register and clear the bits to be configured */
  154. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  155. tmpreg|= TimeStampEdge;
  156. /* Disable the write protection for RTC registers */
  157. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  158. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  159. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  160. /* Configure the Time Stamp TSEDGE and Enable bits */
  161. hrtc->Instance->CR = (uint32_t)tmpreg;
  162. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  163. /* Enable the write protection for RTC registers */
  164. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  165. /* Change RTC state */
  166. hrtc->State = HAL_RTC_STATE_READY;
  167. /* Process Unlocked */
  168. __HAL_UNLOCK(hrtc);
  169. return HAL_OK;
  170. }
  171. /**
  172. * @brief Sets TimeStamp with Interrupt.
  173. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  174. * the configuration information for RTC.
  175. * @note This API must be called before enabling the TimeStamp feature.
  176. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  177. * activated.
  178. * This parameter can be one of the following values:
  179. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  180. * rising edge of the related pin.
  181. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  182. * falling edge of the related pin.
  183. * @param RTC_TimeStampPin: Specifies the RTC TimeStamp Pin.
  184. * This parameter can be one of the following values:
  185. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  186. * @arg RTC_TIMESTAMPPIN_PI8: PI8 is selected as RTC TimeStamp Pin. (not applicable in the case of STM32F446xx, STM32F412xx, STM32F413xx and STM32F423xx devices)
  187. * @arg RTC_TIMESTAMPPIN_PA0: PA0 is selected as RTC TimeStamp Pin only for STM32F446xx devices
  188. * @retval HAL status
  189. */
  190. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  191. {
  192. uint32_t tmpreg = 0U;
  193. /* Check the parameters */
  194. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  195. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  196. /* Process Locked */
  197. __HAL_LOCK(hrtc);
  198. hrtc->State = HAL_RTC_STATE_BUSY;
  199. /* Get the RTC_CR register and clear the bits to be configured */
  200. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  201. tmpreg |= TimeStampEdge;
  202. /* Disable the write protection for RTC registers */
  203. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  204. /* Configure the Time Stamp TSEDGE and Enable bits */
  205. hrtc->Instance->CR = (uint32_t)tmpreg;
  206. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_TSINSEL;
  207. hrtc->Instance->TAFCR |= (uint32_t)(RTC_TimeStampPin);
  208. /* Clear RTC Timestamp flag */
  209. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  210. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  211. /* Enable IT timestamp */
  212. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
  213. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  214. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  215. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  216. /* Enable the write protection for RTC registers */
  217. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  218. hrtc->State = HAL_RTC_STATE_READY;
  219. /* Process Unlocked */
  220. __HAL_UNLOCK(hrtc);
  221. return HAL_OK;
  222. }
  223. /**
  224. * @brief Deactivates TimeStamp.
  225. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  226. * the configuration information for RTC.
  227. * @retval HAL status
  228. */
  229. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  230. {
  231. uint32_t tmpreg = 0U;
  232. /* Process Locked */
  233. __HAL_LOCK(hrtc);
  234. hrtc->State = HAL_RTC_STATE_BUSY;
  235. /* Disable the write protection for RTC registers */
  236. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  237. /* In case of interrupt mode is used, the interrupt source must disabled */
  238. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  239. /* Get the RTC_CR register and clear the bits to be configured */
  240. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  241. /* Configure the Time Stamp TSEDGE and Enable bits */
  242. hrtc->Instance->CR = (uint32_t)tmpreg;
  243. /* Enable the write protection for RTC registers */
  244. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  245. hrtc->State = HAL_RTC_STATE_READY;
  246. /* Process Unlocked */
  247. __HAL_UNLOCK(hrtc);
  248. return HAL_OK;
  249. }
  250. /**
  251. * @brief Gets the RTC TimeStamp value.
  252. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  253. * the configuration information for RTC.
  254. * @param sTimeStamp: Pointer to Time structure
  255. * @param sTimeStampDate: Pointer to Date structure
  256. * @param Format: specifies the format of the entered parameters.
  257. * This parameter can be one of the following values:
  258. * RTC_FORMAT_BIN: Binary data format
  259. * RTC_FORMAT_BCD: BCD data format
  260. * @retval HAL status
  261. */
  262. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
  263. {
  264. uint32_t tmptime = 0U, tmpdate = 0U;
  265. /* Check the parameters */
  266. assert_param(IS_RTC_FORMAT(Format));
  267. /* Get the TimeStamp time and date registers values */
  268. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  269. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  270. /* Fill the Time structure fields with the read parameters */
  271. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  272. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  273. sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  274. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16U);
  275. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  276. /* Fill the Date structure fields with the read parameters */
  277. sTimeStampDate->Year = 0U;
  278. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  279. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  280. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13U);
  281. /* Check the input parameters format */
  282. if(Format == RTC_FORMAT_BIN)
  283. {
  284. /* Convert the TimeStamp structure parameters to Binary format */
  285. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  286. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  287. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  288. /* Convert the DateTimeStamp structure parameters to Binary format */
  289. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  290. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  291. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  292. }
  293. /* Clear the TIMESTAMP Flag */
  294. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  295. return HAL_OK;
  296. }
  297. /**
  298. * @brief Sets Tamper
  299. * @note By calling this API we disable the tamper interrupt for all tampers.
  300. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  301. * the configuration information for RTC.
  302. * @param sTamper: Pointer to Tamper Structure.
  303. * @retval HAL status
  304. */
  305. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  306. {
  307. uint32_t tmpreg = 0U;
  308. /* Check the parameters */
  309. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  310. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  311. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  312. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  313. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  314. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  315. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  316. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  317. /* Process Locked */
  318. __HAL_LOCK(hrtc);
  319. hrtc->State = HAL_RTC_STATE_BUSY;
  320. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  321. {
  322. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
  323. }
  324. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  325. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  326. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  327. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
  328. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  329. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL | (uint32_t)RTC_TAFCR_TAMPIE);
  330. hrtc->Instance->TAFCR |= tmpreg;
  331. hrtc->State = HAL_RTC_STATE_READY;
  332. /* Process Unlocked */
  333. __HAL_UNLOCK(hrtc);
  334. return HAL_OK;
  335. }
  336. /**
  337. * @brief Sets Tamper with interrupt.
  338. * @note By calling this API we force the tamper interrupt for all tampers.
  339. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  340. * the configuration information for RTC.
  341. * @param sTamper: Pointer to RTC Tamper.
  342. * @retval HAL status
  343. */
  344. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  345. {
  346. uint32_t tmpreg = 0U;
  347. /* Check the parameters */
  348. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  349. assert_param(IS_RTC_TAMPER_PIN(sTamper->PinSelection));
  350. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  351. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  352. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  353. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  354. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  355. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  356. /* Process Locked */
  357. __HAL_LOCK(hrtc);
  358. hrtc->State = HAL_RTC_STATE_BUSY;
  359. /* Configure the tamper trigger */
  360. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  361. {
  362. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1U);
  363. }
  364. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->PinSelection | (uint32_t)sTamper->Trigger |\
  365. (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  366. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  367. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1U) | (uint32_t)RTC_TAFCR_TAMPTS |\
  368. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  369. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPINSEL);
  370. hrtc->Instance->TAFCR |= tmpreg;
  371. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  372. hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
  373. if(sTamper->Tamper == RTC_TAMPER_1)
  374. {
  375. /* Clear RTC Tamper 1 flag */
  376. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  377. }
  378. else
  379. {
  380. /* Clear RTC Tamper 2 flag */
  381. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  382. }
  383. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  384. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  385. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  386. hrtc->State = HAL_RTC_STATE_READY;
  387. /* Process Unlocked */
  388. __HAL_UNLOCK(hrtc);
  389. return HAL_OK;
  390. }
  391. /**
  392. * @brief Deactivates Tamper.
  393. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  394. * the configuration information for RTC.
  395. * @param Tamper: Selected tamper pin.
  396. * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
  397. * @retval HAL status
  398. */
  399. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  400. {
  401. assert_param(IS_RTC_TAMPER(Tamper));
  402. /* Process Locked */
  403. __HAL_LOCK(hrtc);
  404. hrtc->State = HAL_RTC_STATE_BUSY;
  405. /* Disable the selected Tamper pin */
  406. hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  407. hrtc->State = HAL_RTC_STATE_READY;
  408. /* Process Unlocked */
  409. __HAL_UNLOCK(hrtc);
  410. return HAL_OK;
  411. }
  412. /**
  413. * @brief This function handles TimeStamp interrupt request.
  414. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  415. * the configuration information for RTC.
  416. * @retval None
  417. */
  418. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  419. {
  420. if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
  421. {
  422. /* Get the status of the Interrupt */
  423. if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
  424. {
  425. /* TIMESTAMP callback */
  426. HAL_RTCEx_TimeStampEventCallback(hrtc);
  427. /* Clear the TIMESTAMP interrupt pending bit */
  428. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
  429. }
  430. }
  431. /* Get the status of the Interrupt */
  432. if(__HAL_RTC_TAMPER_GET_IT(hrtc,RTC_IT_TAMP1))
  433. {
  434. /* Get the TAMPER Interrupt enable bit and pending bit */
  435. if(((hrtc->Instance->TAFCR & (RTC_TAFCR_TAMPIE))) != (uint32_t)RESET)
  436. {
  437. /* Tamper callback */
  438. HAL_RTCEx_Tamper1EventCallback(hrtc);
  439. /* Clear the Tamper interrupt pending bit */
  440. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  441. }
  442. }
  443. /* Get the status of the Interrupt */
  444. if(__HAL_RTC_TAMPER_GET_IT(hrtc, RTC_IT_TAMP2))
  445. {
  446. /* Get the TAMPER Interrupt enable bit and pending bit */
  447. if(((hrtc->Instance->TAFCR & RTC_TAFCR_TAMPIE)) != (uint32_t)RESET)
  448. {
  449. /* Tamper callback */
  450. HAL_RTCEx_Tamper2EventCallback(hrtc);
  451. /* Clear the Tamper interrupt pending bit */
  452. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  453. }
  454. }
  455. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  456. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  457. /* Change RTC state */
  458. hrtc->State = HAL_RTC_STATE_READY;
  459. }
  460. /**
  461. * @brief TimeStamp callback.
  462. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  463. * the configuration information for RTC.
  464. * @retval None
  465. */
  466. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  467. {
  468. /* Prevent unused argument(s) compilation warning */
  469. UNUSED(hrtc);
  470. /* NOTE : This function Should not be modified, when the callback is needed,
  471. the HAL_RTC_TimeStampEventCallback could be implemented in the user file
  472. */
  473. }
  474. /**
  475. * @brief Tamper 1 callback.
  476. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  477. * the configuration information for RTC.
  478. * @retval None
  479. */
  480. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  481. {
  482. /* Prevent unused argument(s) compilation warning */
  483. UNUSED(hrtc);
  484. /* NOTE : This function Should not be modified, when the callback is needed,
  485. the HAL_RTC_Tamper1EventCallback could be implemented in the user file
  486. */
  487. }
  488. /**
  489. * @brief Tamper 2 callback.
  490. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  491. * the configuration information for RTC.
  492. * @retval None
  493. */
  494. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  495. {
  496. /* Prevent unused argument(s) compilation warning */
  497. UNUSED(hrtc);
  498. /* NOTE : This function Should not be modified, when the callback is needed,
  499. the HAL_RTC_Tamper2EventCallback could be implemented in the user file
  500. */
  501. }
  502. /**
  503. * @brief This function handles TimeStamp polling request.
  504. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  505. * the configuration information for RTC.
  506. * @param Timeout: Timeout duration
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  510. {
  511. uint32_t tickstart = 0U;
  512. /* Get tick */
  513. tickstart = HAL_GetTick();
  514. while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
  515. {
  516. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  517. {
  518. /* Clear the TIMESTAMP Overrun Flag */
  519. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  520. /* Change TIMESTAMP state */
  521. hrtc->State = HAL_RTC_STATE_ERROR;
  522. return HAL_ERROR;
  523. }
  524. if(Timeout != HAL_MAX_DELAY)
  525. {
  526. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  527. {
  528. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  529. return HAL_TIMEOUT;
  530. }
  531. }
  532. }
  533. /* Change RTC state */
  534. hrtc->State = HAL_RTC_STATE_READY;
  535. return HAL_OK;
  536. }
  537. /**
  538. * @brief This function handles Tamper1 Polling.
  539. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  540. * the configuration information for RTC.
  541. * @param Timeout: Timeout duration
  542. * @retval HAL status
  543. */
  544. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  545. {
  546. uint32_t tickstart = 0U;
  547. /* Get tick */
  548. tickstart = HAL_GetTick();
  549. /* Get the status of the Interrupt */
  550. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
  551. {
  552. if(Timeout != HAL_MAX_DELAY)
  553. {
  554. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  555. {
  556. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  557. return HAL_TIMEOUT;
  558. }
  559. }
  560. }
  561. /* Clear the Tamper Flag */
  562. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  563. /* Change RTC state */
  564. hrtc->State = HAL_RTC_STATE_READY;
  565. return HAL_OK;
  566. }
  567. /**
  568. * @brief This function handles Tamper2 Polling.
  569. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  570. * the configuration information for RTC.
  571. * @param Timeout: Timeout duration
  572. * @retval HAL status
  573. */
  574. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  575. {
  576. uint32_t tickstart = 0U;
  577. /* Get tick */
  578. tickstart = HAL_GetTick();
  579. /* Get the status of the Interrupt */
  580. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
  581. {
  582. if(Timeout != HAL_MAX_DELAY)
  583. {
  584. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  585. {
  586. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  587. return HAL_TIMEOUT;
  588. }
  589. }
  590. }
  591. /* Clear the Tamper Flag */
  592. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
  593. /* Change RTC state */
  594. hrtc->State = HAL_RTC_STATE_READY;
  595. return HAL_OK;
  596. }
  597. /**
  598. * @}
  599. */
  600. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wake-up functions
  601. * @brief RTC Wake-up functions
  602. *
  603. @verbatim
  604. ===============================================================================
  605. ##### RTC Wake-up functions #####
  606. ===============================================================================
  607. [..] This section provides functions allowing to configure Wake-up feature
  608. @endverbatim
  609. * @{
  610. */
  611. /**
  612. * @brief Sets wake up timer.
  613. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  614. * the configuration information for RTC.
  615. * @param WakeUpCounter: Wake up counter
  616. * @param WakeUpClock: Wake up clock
  617. * @retval HAL status
  618. */
  619. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  620. {
  621. uint32_t tickstart = 0U;
  622. /* Check the parameters */
  623. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  624. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  625. /* Process Locked */
  626. __HAL_LOCK(hrtc);
  627. hrtc->State = HAL_RTC_STATE_BUSY;
  628. /* Disable the write protection for RTC registers */
  629. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  630. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  631. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  632. {
  633. tickstart = HAL_GetTick();
  634. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  635. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
  636. {
  637. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  638. {
  639. /* Enable the write protection for RTC registers */
  640. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  641. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  642. /* Process Unlocked */
  643. __HAL_UNLOCK(hrtc);
  644. return HAL_TIMEOUT;
  645. }
  646. }
  647. }
  648. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  649. tickstart = HAL_GetTick();
  650. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  651. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  652. {
  653. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  654. {
  655. /* Enable the write protection for RTC registers */
  656. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  657. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  658. /* Process Unlocked */
  659. __HAL_UNLOCK(hrtc);
  660. return HAL_TIMEOUT;
  661. }
  662. }
  663. /* Clear the Wake-up Timer clock source bits in CR register */
  664. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  665. /* Configure the clock source */
  666. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  667. /* Configure the Wake-up Timer counter */
  668. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  669. /* Enable the Wake-up Timer */
  670. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  671. /* Enable the write protection for RTC registers */
  672. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  673. hrtc->State = HAL_RTC_STATE_READY;
  674. /* Process Unlocked */
  675. __HAL_UNLOCK(hrtc);
  676. return HAL_OK;
  677. }
  678. /**
  679. * @brief Sets wake up timer with interrupt
  680. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  681. * the configuration information for RTC.
  682. * @param WakeUpCounter: Wake up counter
  683. * @param WakeUpClock: Wake up clock
  684. * @retval HAL status
  685. */
  686. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  687. {
  688. __IO uint32_t count;
  689. /* Check the parameters */
  690. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  691. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  692. /* Process Locked */
  693. __HAL_LOCK(hrtc);
  694. hrtc->State = HAL_RTC_STATE_BUSY;
  695. /* Disable the write protection for RTC registers */
  696. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  697. /* Check RTC WUTWF flag is reset only when wake up timer enabled */
  698. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  699. {
  700. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  701. count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  702. do
  703. {
  704. if(count-- == 0U)
  705. {
  706. /* Enable the write protection for RTC registers */
  707. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  708. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  709. /* Process Unlocked */
  710. __HAL_UNLOCK(hrtc);
  711. return HAL_TIMEOUT;
  712. }
  713. }
  714. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET);
  715. }
  716. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  717. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  718. count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  719. do
  720. {
  721. if(count-- == 0U)
  722. {
  723. /* Enable the write protection for RTC registers */
  724. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  725. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  726. /* Process Unlocked */
  727. __HAL_UNLOCK(hrtc);
  728. return HAL_TIMEOUT;
  729. }
  730. }
  731. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET);
  732. /* Configure the Wake-up Timer counter */
  733. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  734. /* Clear the Wake-up Timer clock source bits in CR register */
  735. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  736. /* Configure the clock source */
  737. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  738. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  739. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  740. EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
  741. /* Clear RTC Wake Up timer Flag */
  742. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  743. /* Configure the Interrupt in the RTC_CR register */
  744. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
  745. /* Enable the Wake-up Timer */
  746. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  747. /* Enable the write protection for RTC registers */
  748. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  749. hrtc->State = HAL_RTC_STATE_READY;
  750. /* Process Unlocked */
  751. __HAL_UNLOCK(hrtc);
  752. return HAL_OK;
  753. }
  754. /**
  755. * @brief Deactivates wake up timer counter.
  756. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  757. * the configuration information for RTC.
  758. * @retval HAL status
  759. */
  760. uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  761. {
  762. uint32_t tickstart = 0U;
  763. /* Process Locked */
  764. __HAL_LOCK(hrtc);
  765. hrtc->State = HAL_RTC_STATE_BUSY;
  766. /* Disable the write protection for RTC registers */
  767. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  768. /* Disable the Wake-up Timer */
  769. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  770. /* In case of interrupt mode is used, the interrupt source must disabled */
  771. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
  772. /* Get tick */
  773. tickstart = HAL_GetTick();
  774. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  775. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  776. {
  777. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  778. {
  779. /* Enable the write protection for RTC registers */
  780. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  781. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  782. /* Process Unlocked */
  783. __HAL_UNLOCK(hrtc);
  784. return HAL_TIMEOUT;
  785. }
  786. }
  787. /* Enable the write protection for RTC registers */
  788. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  789. hrtc->State = HAL_RTC_STATE_READY;
  790. /* Process Unlocked */
  791. __HAL_UNLOCK(hrtc);
  792. return HAL_OK;
  793. }
  794. /**
  795. * @brief Gets wake up timer counter.
  796. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  797. * the configuration information for RTC.
  798. * @retval Counter value
  799. */
  800. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  801. {
  802. /* Get the counter value */
  803. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  804. }
  805. /**
  806. * @brief This function handles Wake Up Timer interrupt request.
  807. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  808. * the configuration information for RTC.
  809. * @retval None
  810. */
  811. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  812. {
  813. if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
  814. {
  815. /* Get the status of the Interrupt */
  816. if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
  817. {
  818. /* WAKEUPTIMER callback */
  819. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  820. /* Clear the WAKEUPTIMER interrupt pending bit */
  821. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  822. }
  823. }
  824. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  825. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  826. /* Change RTC state */
  827. hrtc->State = HAL_RTC_STATE_READY;
  828. }
  829. /**
  830. * @brief Wake Up Timer callback.
  831. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  832. * the configuration information for RTC.
  833. * @retval None
  834. */
  835. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  836. {
  837. /* Prevent unused argument(s) compilation warning */
  838. UNUSED(hrtc);
  839. /* NOTE : This function Should not be modified, when the callback is needed,
  840. the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
  841. */
  842. }
  843. /**
  844. * @brief This function handles Wake Up Timer Polling.
  845. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  846. * the configuration information for RTC.
  847. * @param Timeout: Timeout duration
  848. * @retval HAL status
  849. */
  850. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  851. {
  852. uint32_t tickstart = 0U;
  853. /* Get tick */
  854. tickstart = HAL_GetTick();
  855. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  856. {
  857. if(Timeout != HAL_MAX_DELAY)
  858. {
  859. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  860. {
  861. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  862. return HAL_TIMEOUT;
  863. }
  864. }
  865. }
  866. /* Clear the WAKEUPTIMER Flag */
  867. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  868. /* Change RTC state */
  869. hrtc->State = HAL_RTC_STATE_READY;
  870. return HAL_OK;
  871. }
  872. /**
  873. * @}
  874. */
  875. /** @defgroup RTCEx_Exported_Functions_Group3 Extension Peripheral Control functions
  876. * @brief Extension Peripheral Control functions
  877. *
  878. @verbatim
  879. ===============================================================================
  880. ##### Extension Peripheral Control functions #####
  881. ===============================================================================
  882. [..]
  883. This subsection provides functions allowing to
  884. (+) Write a data in a specified RTC Backup data register
  885. (+) Read a data in a specified RTC Backup data register
  886. (+) Set the Coarse calibration parameters.
  887. (+) Deactivate the Coarse calibration parameters
  888. (+) Set the Smooth calibration parameters.
  889. (+) Configure the Synchronization Shift Control Settings.
  890. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  891. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  892. (+) Enable the RTC reference clock detection.
  893. (+) Disable the RTC reference clock detection.
  894. (+) Enable the Bypass Shadow feature.
  895. (+) Disable the Bypass Shadow feature.
  896. @endverbatim
  897. * @{
  898. */
  899. /**
  900. * @brief Writes a data in a specified RTC Backup data register.
  901. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  902. * the configuration information for RTC.
  903. * @param BackupRegister: RTC Backup data Register number.
  904. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  905. * specify the register.
  906. * @param Data: Data to be written in the specified RTC Backup data register.
  907. * @retval None
  908. */
  909. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  910. {
  911. uint32_t tmp = 0U;
  912. /* Check the parameters */
  913. assert_param(IS_RTC_BKP(BackupRegister));
  914. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  915. tmp += (BackupRegister * 4U);
  916. /* Write the specified register */
  917. *(__IO uint32_t *)tmp = (uint32_t)Data;
  918. }
  919. /**
  920. * @brief Reads data from the specified RTC Backup data Register.
  921. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  922. * the configuration information for RTC.
  923. * @param BackupRegister: RTC Backup data Register number.
  924. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  925. * specify the register.
  926. * @retval Read value
  927. */
  928. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  929. {
  930. uint32_t tmp = 0U;
  931. /* Check the parameters */
  932. assert_param(IS_RTC_BKP(BackupRegister));
  933. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  934. tmp += (BackupRegister * 4U);
  935. /* Read the specified register */
  936. return (*(__IO uint32_t *)tmp);
  937. }
  938. /**
  939. * @brief Sets the Coarse calibration parameters.
  940. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  941. * the configuration information for RTC.
  942. * @param CalibSign: Specifies the sign of the coarse calibration value.
  943. * This parameter can be one of the following values :
  944. * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
  945. * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
  946. * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
  947. *
  948. * @note This Calibration value should be between 0 and 63 when using negative
  949. * sign with a 2-ppm step.
  950. *
  951. * @note This Calibration value should be between 0 and 126 when using positive
  952. * sign with a 4-ppm step.
  953. * @retval HAL status
  954. */
  955. HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
  956. {
  957. /* Check the parameters */
  958. assert_param(IS_RTC_CALIB_SIGN(CalibSign));
  959. assert_param(IS_RTC_CALIB_VALUE(Value));
  960. /* Process Locked */
  961. __HAL_LOCK(hrtc);
  962. hrtc->State = HAL_RTC_STATE_BUSY;
  963. /* Disable the write protection for RTC registers */
  964. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  965. /* Set Initialization mode */
  966. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  967. {
  968. /* Enable the write protection for RTC registers */
  969. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  970. /* Set RTC state*/
  971. hrtc->State = HAL_RTC_STATE_ERROR;
  972. /* Process Unlocked */
  973. __HAL_UNLOCK(hrtc);
  974. return HAL_ERROR;
  975. }
  976. else
  977. {
  978. /* Enable the Coarse Calibration */
  979. __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
  980. /* Set the coarse calibration value */
  981. hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
  982. /* Exit Initialization mode */
  983. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  984. }
  985. /* Enable the write protection for RTC registers */
  986. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  987. /* Change state */
  988. hrtc->State = HAL_RTC_STATE_READY;
  989. /* Process Unlocked */
  990. __HAL_UNLOCK(hrtc);
  991. return HAL_OK;
  992. }
  993. /**
  994. * @brief Deactivates the Coarse calibration parameters.
  995. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  996. * the configuration information for RTC.
  997. * @retval HAL status
  998. */
  999. HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
  1000. {
  1001. /* Process Locked */
  1002. __HAL_LOCK(hrtc);
  1003. hrtc->State = HAL_RTC_STATE_BUSY;
  1004. /* Disable the write protection for RTC registers */
  1005. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1006. /* Set Initialization mode */
  1007. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1008. {
  1009. /* Enable the write protection for RTC registers */
  1010. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1011. /* Set RTC state*/
  1012. hrtc->State = HAL_RTC_STATE_ERROR;
  1013. /* Process Unlocked */
  1014. __HAL_UNLOCK(hrtc);
  1015. return HAL_ERROR;
  1016. }
  1017. else
  1018. {
  1019. /* Enable the Coarse Calibration */
  1020. __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
  1021. /* Exit Initialization mode */
  1022. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1023. }
  1024. /* Enable the write protection for RTC registers */
  1025. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1026. /* Change state */
  1027. hrtc->State = HAL_RTC_STATE_READY;
  1028. /* Process Unlocked */
  1029. __HAL_UNLOCK(hrtc);
  1030. return HAL_OK;
  1031. }
  1032. /**
  1033. * @brief Sets the Smooth calibration parameters.
  1034. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1035. * the configuration information for RTC.
  1036. * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
  1037. * This parameter can be can be one of the following values :
  1038. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  1039. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  1040. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  1041. * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
  1042. * This parameter can be one of the following values:
  1043. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  1044. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1045. * @param SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  1046. * This parameter can be one any value from 0 to 0x000001FF.
  1047. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1048. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1049. * SmouthCalibMinusPulsesValue must be equal to 0.
  1050. * @retval HAL status
  1051. */
  1052. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  1053. {
  1054. uint32_t tickstart = 0U;
  1055. /* Check the parameters */
  1056. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1057. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1058. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  1059. /* Process Locked */
  1060. __HAL_LOCK(hrtc);
  1061. hrtc->State = HAL_RTC_STATE_BUSY;
  1062. /* Disable the write protection for RTC registers */
  1063. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1064. /* check if a calibration is pending*/
  1065. if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1066. {
  1067. /* Get tick */
  1068. tickstart = HAL_GetTick();
  1069. /* check if a calibration is pending*/
  1070. while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1071. {
  1072. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1073. {
  1074. /* Enable the write protection for RTC registers */
  1075. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1076. /* Change RTC state */
  1077. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1078. /* Process Unlocked */
  1079. __HAL_UNLOCK(hrtc);
  1080. return HAL_TIMEOUT;
  1081. }
  1082. }
  1083. }
  1084. /* Configure the Smooth calibration settings */
  1085. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
  1086. /* Enable the write protection for RTC registers */
  1087. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1088. /* Change RTC state */
  1089. hrtc->State = HAL_RTC_STATE_READY;
  1090. /* Process Unlocked */
  1091. __HAL_UNLOCK(hrtc);
  1092. return HAL_OK;
  1093. }
  1094. /**
  1095. * @brief Configures the Synchronization Shift Control Settings.
  1096. * @note When REFCKON is set, firmware must not write to Shift control register.
  1097. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1098. * the configuration information for RTC.
  1099. * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
  1100. * This parameter can be one of the following values :
  1101. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1102. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1103. * @param ShiftSubFS: Select the number of Second Fractions to substitute.
  1104. * This parameter can be one any value from 0 to 0x7FFF.
  1105. * @retval HAL status
  1106. */
  1107. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1108. {
  1109. uint32_t tickstart = 0U;
  1110. /* Check the parameters */
  1111. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1112. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1113. /* Process Locked */
  1114. __HAL_LOCK(hrtc);
  1115. hrtc->State = HAL_RTC_STATE_BUSY;
  1116. /* Disable the write protection for RTC registers */
  1117. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1118. /* Get tick */
  1119. tickstart = HAL_GetTick();
  1120. /* Wait until the shift is completed*/
  1121. while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
  1122. {
  1123. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1124. {
  1125. /* Enable the write protection for RTC registers */
  1126. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1127. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1128. /* Process Unlocked */
  1129. __HAL_UNLOCK(hrtc);
  1130. return HAL_TIMEOUT;
  1131. }
  1132. }
  1133. /* Check if the reference clock detection is disabled */
  1134. if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
  1135. {
  1136. /* Configure the Shift settings */
  1137. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1138. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1139. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  1140. {
  1141. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1142. {
  1143. /* Enable the write protection for RTC registers */
  1144. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1145. hrtc->State = HAL_RTC_STATE_ERROR;
  1146. /* Process Unlocked */
  1147. __HAL_UNLOCK(hrtc);
  1148. return HAL_ERROR;
  1149. }
  1150. }
  1151. }
  1152. else
  1153. {
  1154. /* Enable the write protection for RTC registers */
  1155. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1156. /* Change RTC state */
  1157. hrtc->State = HAL_RTC_STATE_ERROR;
  1158. /* Process Unlocked */
  1159. __HAL_UNLOCK(hrtc);
  1160. return HAL_ERROR;
  1161. }
  1162. /* Enable the write protection for RTC registers */
  1163. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1164. /* Change RTC state */
  1165. hrtc->State = HAL_RTC_STATE_READY;
  1166. /* Process Unlocked */
  1167. __HAL_UNLOCK(hrtc);
  1168. return HAL_OK;
  1169. }
  1170. /**
  1171. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1172. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1173. * the configuration information for RTC.
  1174. * @param CalibOutput: Select the Calibration output Selection .
  1175. * This parameter can be one of the following values:
  1176. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1177. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1178. * @retval HAL status
  1179. */
  1180. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
  1181. {
  1182. /* Check the parameters */
  1183. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1184. /* Process Locked */
  1185. __HAL_LOCK(hrtc);
  1186. hrtc->State = HAL_RTC_STATE_BUSY;
  1187. /* Disable the write protection for RTC registers */
  1188. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1189. /* Clear flags before config */
  1190. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1191. /* Configure the RTC_CR register */
  1192. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1193. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1194. /* Enable the write protection for RTC registers */
  1195. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1196. /* Change RTC state */
  1197. hrtc->State = HAL_RTC_STATE_READY;
  1198. /* Process Unlocked */
  1199. __HAL_UNLOCK(hrtc);
  1200. return HAL_OK;
  1201. }
  1202. /**
  1203. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1204. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1205. * the configuration information for RTC.
  1206. * @retval HAL status
  1207. */
  1208. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1209. {
  1210. /* Process Locked */
  1211. __HAL_LOCK(hrtc);
  1212. hrtc->State = HAL_RTC_STATE_BUSY;
  1213. /* Disable the write protection for RTC registers */
  1214. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1215. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1216. /* Enable the write protection for RTC registers */
  1217. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1218. /* Change RTC state */
  1219. hrtc->State = HAL_RTC_STATE_READY;
  1220. /* Process Unlocked */
  1221. __HAL_UNLOCK(hrtc);
  1222. return HAL_OK;
  1223. }
  1224. /**
  1225. * @brief Enables the RTC reference clock detection.
  1226. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1227. * the configuration information for RTC.
  1228. * @retval HAL status
  1229. */
  1230. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
  1231. {
  1232. /* Process Locked */
  1233. __HAL_LOCK(hrtc);
  1234. hrtc->State = HAL_RTC_STATE_BUSY;
  1235. /* Disable the write protection for RTC registers */
  1236. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1237. /* Set Initialization mode */
  1238. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1239. {
  1240. /* Enable the write protection for RTC registers */
  1241. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1242. /* Set RTC state*/
  1243. hrtc->State = HAL_RTC_STATE_ERROR;
  1244. /* Process Unlocked */
  1245. __HAL_UNLOCK(hrtc);
  1246. return HAL_ERROR;
  1247. }
  1248. else
  1249. {
  1250. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1251. /* Exit Initialization mode */
  1252. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1253. }
  1254. /* Enable the write protection for RTC registers */
  1255. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1256. /* Change RTC state */
  1257. hrtc->State = HAL_RTC_STATE_READY;
  1258. /* Process Unlocked */
  1259. __HAL_UNLOCK(hrtc);
  1260. return HAL_OK;
  1261. }
  1262. /**
  1263. * @brief Disable the RTC reference clock detection.
  1264. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1265. * the configuration information for RTC.
  1266. * @retval HAL status
  1267. */
  1268. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
  1269. {
  1270. /* Process Locked */
  1271. __HAL_LOCK(hrtc);
  1272. hrtc->State = HAL_RTC_STATE_BUSY;
  1273. /* Disable the write protection for RTC registers */
  1274. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1275. /* Set Initialization mode */
  1276. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1277. {
  1278. /* Enable the write protection for RTC registers */
  1279. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1280. /* Set RTC state*/
  1281. hrtc->State = HAL_RTC_STATE_ERROR;
  1282. /* Process Unlocked */
  1283. __HAL_UNLOCK(hrtc);
  1284. return HAL_ERROR;
  1285. }
  1286. else
  1287. {
  1288. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1289. /* Exit Initialization mode */
  1290. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1291. }
  1292. /* Enable the write protection for RTC registers */
  1293. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1294. /* Change RTC state */
  1295. hrtc->State = HAL_RTC_STATE_READY;
  1296. /* Process Unlocked */
  1297. __HAL_UNLOCK(hrtc);
  1298. return HAL_OK;
  1299. }
  1300. /**
  1301. * @brief Enables the Bypass Shadow feature.
  1302. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1303. * the configuration information for RTC.
  1304. * @note When the Bypass Shadow is enabled the calendar value are taken
  1305. * directly from the Calendar counter.
  1306. * @retval HAL status
  1307. */
  1308. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
  1309. {
  1310. /* Process Locked */
  1311. __HAL_LOCK(hrtc);
  1312. hrtc->State = HAL_RTC_STATE_BUSY;
  1313. /* Disable the write protection for RTC registers */
  1314. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1315. /* Set the BYPSHAD bit */
  1316. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1317. /* Enable the write protection for RTC registers */
  1318. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1319. /* Change RTC state */
  1320. hrtc->State = HAL_RTC_STATE_READY;
  1321. /* Process Unlocked */
  1322. __HAL_UNLOCK(hrtc);
  1323. return HAL_OK;
  1324. }
  1325. /**
  1326. * @brief Disables the Bypass Shadow feature.
  1327. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1328. * the configuration information for RTC.
  1329. * @note When the Bypass Shadow is enabled the calendar value are taken
  1330. * directly from the Calendar counter.
  1331. * @retval HAL status
  1332. */
  1333. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
  1334. {
  1335. /* Process Locked */
  1336. __HAL_LOCK(hrtc);
  1337. hrtc->State = HAL_RTC_STATE_BUSY;
  1338. /* Disable the write protection for RTC registers */
  1339. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1340. /* Reset the BYPSHAD bit */
  1341. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1342. /* Enable the write protection for RTC registers */
  1343. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1344. /* Change RTC state */
  1345. hrtc->State = HAL_RTC_STATE_READY;
  1346. /* Process Unlocked */
  1347. __HAL_UNLOCK(hrtc);
  1348. return HAL_OK;
  1349. }
  1350. /**
  1351. * @}
  1352. */
  1353. /** @defgroup RTCEx_Exported_Functions_Group4 Extended features functions
  1354. * @brief Extended features functions
  1355. *
  1356. @verbatim
  1357. ===============================================================================
  1358. ##### Extended features functions #####
  1359. ===============================================================================
  1360. [..] This section provides functions allowing to:
  1361. (+) RTC Alarm B callback
  1362. (+) RTC Poll for Alarm B request
  1363. @endverbatim
  1364. * @{
  1365. */
  1366. /**
  1367. * @brief Alarm B callback.
  1368. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1369. * the configuration information for RTC.
  1370. * @retval None
  1371. */
  1372. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1373. {
  1374. /* Prevent unused argument(s) compilation warning */
  1375. UNUSED(hrtc);
  1376. /* NOTE : This function Should not be modified, when the callback is needed,
  1377. the HAL_RTC_AlarmBEventCallback could be implemented in the user file
  1378. */
  1379. }
  1380. /**
  1381. * @brief This function handles AlarmB Polling request.
  1382. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1383. * the configuration information for RTC.
  1384. * @param Timeout: Timeout duration
  1385. * @retval HAL status
  1386. */
  1387. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1388. {
  1389. uint32_t tickstart = 0U;
  1390. /* Get tick */
  1391. tickstart = HAL_GetTick();
  1392. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  1393. {
  1394. if(Timeout != HAL_MAX_DELAY)
  1395. {
  1396. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1397. {
  1398. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1399. return HAL_TIMEOUT;
  1400. }
  1401. }
  1402. }
  1403. /* Clear the Alarm Flag */
  1404. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1405. /* Change RTC state */
  1406. hrtc->State = HAL_RTC_STATE_READY;
  1407. return HAL_OK;
  1408. }
  1409. /**
  1410. * @}
  1411. */
  1412. /**
  1413. * @}
  1414. */
  1415. #endif /* HAL_RTC_MODULE_ENABLED */
  1416. /**
  1417. * @}
  1418. */
  1419. /**
  1420. * @}
  1421. */
  1422. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/