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.
 
 
 

1595 lines
48 KiB

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