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.
 
 
 

1782 lines
55 KiB

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