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.
 
 
 

1913 lines
57 KiB

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