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.
 
 
 

1878 lines
58 KiB

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