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.
 
 
 

3018 lines
89 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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) 2017 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 "stm32h7xx_hal.h"
  96. /** @addtogroup STM32H7xx_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. #define TAMP_ALL (TAMP_CR1_TAMP1E | TAMP_CR1_TAMP2E | TAMP_CR1_TAMP3E)
  107. /* Private macro -------------------------------------------------------------*/
  108. /* Private variables ---------------------------------------------------------*/
  109. /* Private function prototypes -----------------------------------------------*/
  110. /* Exported functions --------------------------------------------------------*/
  111. /** @addtogroup RTCEx_Exported_Functions
  112. * @{
  113. */
  114. /** @addtogroup RTCEx_Exported_Functions_Group1
  115. * @brief RTC TimeStamp and Tamper functions
  116. *
  117. @verbatim
  118. ===============================================================================
  119. ##### RTC TimeStamp and Tamper functions #####
  120. ===============================================================================
  121. [..] This section provides functions allowing to configure TimeStamp feature
  122. @endverbatim
  123. * @{
  124. */
  125. /**
  126. * @brief Set TimeStamp.
  127. * @note This API must be called before enabling the TimeStamp feature.
  128. * @param hrtc RTC handle
  129. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  130. * activated.
  131. * This parameter can be one of the following values:
  132. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  133. * rising edge of the related pin.
  134. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  135. * falling edge of the related pin.
  136. * @param RTC_TimeStampPin specifies the RTC TimeStamp Pin.
  137. * This parameter can be one of the following values:
  138. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  139. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  140. * compatibility, this parameter is required.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  144. {
  145. uint32_t tmpreg;
  146. /* Check the parameters */
  147. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  148. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  149. /* Prevent unused argument(s) compilation warning if no assert_param check */
  150. UNUSED(RTC_TimeStampPin);
  151. /* Process Locked */
  152. __HAL_LOCK(hrtc);
  153. hrtc->State = HAL_RTC_STATE_BUSY;
  154. /* Get the RTC_CR register and clear the bits to be configured */
  155. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  156. tmpreg |= TimeStampEdge;
  157. /* Disable the write protection for RTC registers */
  158. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  159. /* Configure the Time Stamp TSEDGE and Enable bits */
  160. hrtc->Instance->CR = (uint32_t)tmpreg;
  161. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  162. /* Enable the write protection for RTC registers */
  163. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  164. /* Change RTC state */
  165. hrtc->State = HAL_RTC_STATE_READY;
  166. /* Process Unlocked */
  167. __HAL_UNLOCK(hrtc);
  168. return HAL_OK;
  169. }
  170. /**
  171. * @brief Set TimeStamp with Interrupt.
  172. * @note This API must be called before enabling the TimeStamp feature.
  173. * @param hrtc RTC handle
  174. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  175. * activated.
  176. * This parameter can be one of the following values:
  177. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  178. * rising edge of the related pin.
  179. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  180. * falling edge of the related pin.
  181. * @param RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
  182. * This parameter can be one of the following values:
  183. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  184. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  185. * compatibility, this parameter is required.
  186. * @retval HAL status
  187. */
  188. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  189. {
  190. uint32_t tmpreg;
  191. /* Check the parameters */
  192. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  193. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  194. /* Prevent unused argument(s) compilation warning if no assert_param check */
  195. UNUSED(RTC_TimeStampPin);
  196. /* Process Locked */
  197. __HAL_LOCK(hrtc);
  198. hrtc->State = HAL_RTC_STATE_BUSY;
  199. /* Get the RTC_CR register and clear the bits to be configured */
  200. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  201. tmpreg |= TimeStampEdge;
  202. /* Disable the write protection for RTC registers */
  203. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  204. /* Configure the Time Stamp TSEDGE and Enable bits */
  205. hrtc->Instance->CR = (uint32_t)tmpreg;
  206. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  207. /* Enable IT timestamp */
  208. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
  209. #if !defined(DUAL_CORE)
  210. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  211. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  212. #endif
  213. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  214. /* Enable the write protection for RTC registers */
  215. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  216. hrtc->State = HAL_RTC_STATE_READY;
  217. /* Process Unlocked */
  218. __HAL_UNLOCK(hrtc);
  219. return HAL_OK;
  220. }
  221. /**
  222. * @brief Deactivate TimeStamp.
  223. * @param hrtc RTC handle
  224. * @retval HAL status
  225. */
  226. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  227. {
  228. uint32_t tmpreg;
  229. /* Process Locked */
  230. __HAL_LOCK(hrtc);
  231. hrtc->State = HAL_RTC_STATE_BUSY;
  232. /* Disable the write protection for RTC registers */
  233. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  234. /* In case of interrupt mode is used, the interrupt source must disabled */
  235. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  236. /* Get the RTC_CR register and clear the bits to be configured */
  237. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  238. /* Configure the Time Stamp TSEDGE and Enable bits */
  239. hrtc->Instance->CR = (uint32_t)tmpreg;
  240. /* Enable the write protection for RTC registers */
  241. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  242. hrtc->State = HAL_RTC_STATE_READY;
  243. /* Process Unlocked */
  244. __HAL_UNLOCK(hrtc);
  245. return HAL_OK;
  246. }
  247. /**
  248. * @brief Set Internal TimeStamp.
  249. * @note This API must be called before enabling the internal TimeStamp feature.
  250. * @param hrtc RTC handle
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  254. {
  255. /* Process Locked */
  256. __HAL_LOCK(hrtc);
  257. hrtc->State = HAL_RTC_STATE_BUSY;
  258. /* Disable the write protection for RTC registers */
  259. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  260. /* Configure the internal Time Stamp Enable bits */
  261. __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
  262. /* Enable the write protection for RTC registers */
  263. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  264. /* Change RTC state */
  265. hrtc->State = HAL_RTC_STATE_READY;
  266. /* Process Unlocked */
  267. __HAL_UNLOCK(hrtc);
  268. return HAL_OK;
  269. }
  270. /**
  271. * @brief Deactivate Internal TimeStamp.
  272. * @param hrtc RTC handle
  273. * @retval HAL status
  274. */
  275. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  276. {
  277. /* Process Locked */
  278. __HAL_LOCK(hrtc);
  279. hrtc->State = HAL_RTC_STATE_BUSY;
  280. /* Disable the write protection for RTC registers */
  281. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  282. /* Configure the internal Time Stamp Enable bits */
  283. __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
  284. /* Enable the write protection for RTC registers */
  285. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  286. hrtc->State = HAL_RTC_STATE_READY;
  287. /* Process Unlocked */
  288. __HAL_UNLOCK(hrtc);
  289. return HAL_OK;
  290. }
  291. /**
  292. * @brief Get the RTC TimeStamp value.
  293. * @param hrtc RTC handle
  294. * @param sTimeStamp Pointer to Time structure
  295. * @param sTimeStampDate Pointer to Date structure
  296. * @param Format specifies the format of the entered parameters.
  297. * This parameter can be one of the following values:
  298. * @arg RTC_FORMAT_BIN: Binary data format
  299. * @arg RTC_FORMAT_BCD: BCD data format
  300. * @retval HAL status
  301. */
  302. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  303. {
  304. uint32_t tmptime;
  305. uint32_t tmpdate;
  306. /* Check the parameters */
  307. assert_param(IS_RTC_FORMAT(Format));
  308. /* Get the TimeStamp time and date registers values */
  309. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  310. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  311. /* Fill the Time structure fields with the read parameters */
  312. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
  313. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  314. sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
  315. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
  316. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  317. /* Fill the Date structure fields with the read parameters */
  318. sTimeStampDate->Year = 0U;
  319. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  320. sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
  321. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
  322. /* Check the input parameters format */
  323. if (Format == RTC_FORMAT_BIN)
  324. {
  325. /* Convert the TimeStamp structure parameters to Binary format */
  326. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  327. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  328. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  329. /* Convert the DateTimeStamp structure parameters to Binary format */
  330. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  331. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  332. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  333. }
  334. /* Clear the TIMESTAMP Flags */
  335. __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
  336. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  337. return HAL_OK;
  338. }
  339. /**
  340. * @}
  341. */
  342. /** @addtogroup RTCEx_Exported_Functions_Group5
  343. * @brief Extended RTC Tamper functions
  344. *
  345. @verbatim
  346. ==============================================================================
  347. ##### Tamper functions #####
  348. ==============================================================================
  349. [..]
  350. (+) Before calling any tamper or internal tamper function, you have to call first
  351. HAL_RTC_Init() function.
  352. (+) In that ine you can select to output tamper event on RTC pin.
  353. [..]
  354. (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
  355. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  356. value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
  357. Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
  358. You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
  359. (+) The default configuration of the Tamper erases the backup registers. To avoid
  360. erase, enable the NoErase field on the TAMP_TAMPCR register.
  361. [..]
  362. (+) Enable Internal Tamper and configure it with interrupt, timestamp using
  363. the HAL_RTCEx_SetInternalTamper() function.
  364. @endverbatim
  365. * @{
  366. */
  367. #if defined(TAMP_CR1_TAMP1E)
  368. /**
  369. * @brief Set Tamper
  370. * @param hrtc RTC handle
  371. * @param sTamper Pointer to Tamper Structure.
  372. * @retval HAL status
  373. */
  374. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
  375. {
  376. uint32_t tmpreg;
  377. /* Point on TAMPER registers base address */
  378. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
  379. /* Check the parameters */
  380. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  381. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  382. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  383. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  384. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  385. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  386. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  387. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  388. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  389. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  390. /* Configuration register 2 */
  391. tmpreg = tamp->CR2;
  392. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  393. /* Configure the tamper trigger bit */
  394. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  395. {
  396. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  397. }
  398. /* Configure the tamper flags masking bit */
  399. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  400. {
  401. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  402. }
  403. /* Configure the tamper backup registers erasure bit */
  404. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  405. {
  406. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  407. }
  408. tamp->CR2 = tmpreg;
  409. /* Configure filtering parameters */
  410. tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
  411. (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
  412. /* Configure Timestamp saving on tamper detection */
  413. if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
  414. {
  415. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  416. tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
  417. hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
  418. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  419. }
  420. /* Enable selected tamper */
  421. tamp->CR1 |= (sTamper->Tamper);
  422. return HAL_OK;
  423. }
  424. #endif /* TAMP_CR1_TAMP1E */
  425. #if defined (RTC_TAMPCR_TAMP1E)
  426. /**
  427. * @brief Set Tamper.
  428. * @note By calling this API we disable the tamper interrupt for all tampers.
  429. * @param hrtc RTC handle
  430. * @param sTamper Pointer to Tamper Structure.
  431. * @retval HAL status
  432. */
  433. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
  434. {
  435. uint32_t tmpreg;
  436. /* Check the parameters */
  437. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  438. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  439. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  440. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  441. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  442. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  443. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  444. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  445. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  446. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  447. /* Process Locked */
  448. __HAL_LOCK(hrtc);
  449. hrtc->State = HAL_RTC_STATE_BUSY;
  450. /* Copy control register into temporary variable */
  451. tmpreg = hrtc->Instance->TAMPCR;
  452. /* Enable selected tamper */
  453. tmpreg |= (sTamper->Tamper);
  454. /* Configure the bit (located just next to the tamper enable bit) */
  455. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  456. {
  457. /* Set the tamper trigger bit */
  458. tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  459. }
  460. else
  461. {
  462. /* Clear the tamper trigger bit */
  463. tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  464. }
  465. /* Configure the tamper backup registers erasure bit */
  466. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  467. {
  468. #if defined(RTC_TAMPCR_TAMP1E)
  469. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  470. {
  471. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
  472. }
  473. #endif /* RTC_TAMPCR_TAMP1E */
  474. #if defined(RTC_TAMPCR_TAMP2E)
  475. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  476. {
  477. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
  478. }
  479. #endif /* RTC_TAMPCR_TAMP2E */
  480. #if defined(RTC_TAMPCR_TAMP3E)
  481. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  482. {
  483. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
  484. }
  485. #endif /* RTC_TAMPCR_TAMP3E */
  486. }
  487. else
  488. {
  489. #if defined(RTC_TAMPCR_TAMP1E)
  490. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  491. {
  492. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
  493. }
  494. #endif /* RTC_TAMPCR_TAMP1E */
  495. #if defined(RTC_TAMPCR_TAMP2E)
  496. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  497. {
  498. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
  499. }
  500. #endif /* RTC_TAMPCR_TAMP2E */
  501. #if defined(RTC_TAMPCR_TAMP3E)
  502. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  503. {
  504. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
  505. }
  506. #endif /* RTC_TAMPCR_TAMP3E */
  507. }
  508. /* Configure the tamper flags masking bit */
  509. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  510. {
  511. #if defined(RTC_TAMPCR_TAMP1E)
  512. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  513. {
  514. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
  515. }
  516. #endif /* RTC_TAMPCR_TAMP1E */
  517. #if defined(RTC_TAMPCR_TAMP2E)
  518. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  519. {
  520. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
  521. }
  522. #endif /* RTC_TAMPCR_TAMP2E */
  523. #if defined(RTC_TAMPCR_TAMP3E)
  524. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  525. {
  526. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
  527. }
  528. #endif /* RTC_TAMPCR_TAMP3E */
  529. }
  530. else
  531. {
  532. #if defined(RTC_TAMPCR_TAMP1E)
  533. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  534. {
  535. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
  536. }
  537. #endif /* RTC_TAMPCR_TAMP1E */
  538. #if defined(RTC_TAMPCR_TAMP2E)
  539. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  540. {
  541. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
  542. }
  543. #endif /* RTC_TAMPCR_TAMP2E */
  544. #if defined(RTC_TAMPCR_TAMP3E)
  545. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  546. {
  547. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
  548. }
  549. #endif /* RTC_TAMPCR_TAMP3E */
  550. }
  551. /* Clearing remaining fields before setting them */
  552. tmpreg &= ~(RTC_TAMPERFILTER_MASK | RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  553. RTC_TAMPERPRECHARGEDURATION_MASK | RTC_TAMPER_PULLUP_MASK | \
  554. RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  555. /* Set remaining parameters of desired configuration into temporary variable */
  556. tmpreg |= ((uint32_t)sTamper->Filter | \
  557. (uint32_t)sTamper->SamplingFrequency | \
  558. (uint32_t)sTamper->PrechargeDuration | \
  559. (uint32_t)sTamper->TamperPullUp | \
  560. (uint32_t)sTamper->TimeStampOnTamperDetection);
  561. /* Copy desired configuration into configuration register */
  562. hrtc->Instance->TAMPCR = tmpreg;
  563. hrtc->State = HAL_RTC_STATE_READY;
  564. /* Process Unlocked */
  565. __HAL_UNLOCK(hrtc);
  566. return HAL_OK;
  567. }
  568. #endif /* RTC_TAMPCR_TAMP1E */
  569. #if defined(TAMP_CR1_TAMP1E)
  570. /**
  571. * @brief Set Tamper with interrupt.
  572. * @param hrtc RTC handle
  573. * @param sTamper Pointer to Tamper Structure.
  574. * @retval HAL status
  575. */
  576. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
  577. {
  578. uint32_t tmpreg;
  579. /* Point on TAMPER registers base address */
  580. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
  581. /* Check the parameters */
  582. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  583. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  584. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  585. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  586. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  587. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  588. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  589. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  590. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  591. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  592. /* Copy configuration register into temporary variable */
  593. tmpreg = tamp->CR2;
  594. /* Clear the bits that are going to be configured and leave the others unchanged */
  595. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  596. /* Configure the tamper trigger bit */
  597. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  598. {
  599. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  600. }
  601. /* Configure the tamper flags masking bit */
  602. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  603. {
  604. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  605. }
  606. /* Configure the tamper backup registers erasure bit */
  607. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  608. {
  609. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  610. }
  611. tamp->CR2 = tmpreg;
  612. /* Configure filtering parameters */
  613. tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
  614. (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
  615. /* Configure Timestamp saving on tamper detection */
  616. if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
  617. {
  618. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  619. tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
  620. hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
  621. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  622. }
  623. /* Configure RTC Tamper Interrupt: EXTI configuration */
  624. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  625. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
  626. /* Enable interrupt on selected tamper */
  627. tamp->IER |= sTamper->Tamper;
  628. /* Enable selected tamper */
  629. tamp->CR1 |= sTamper->Tamper;
  630. return HAL_OK;
  631. }
  632. #endif /* TAMP_CR1_TAMP1E */
  633. #if defined(RTC_TAMPCR_TAMP1E)
  634. /**
  635. * @brief Set Tamper with interrupt.
  636. * @note By calling this API we force the tamper interrupt for all tampers.
  637. * @param hrtc RTC handle
  638. * @param sTamper Pointer to Tamper Structure.
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef * hrtc, RTC_TamperTypeDef * sTamper)
  642. {
  643. uint32_t tmpreg;
  644. /* Check the parameters */
  645. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  646. assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
  647. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  648. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  649. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  650. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  651. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  652. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  653. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  654. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  655. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  656. /* Process Locked */
  657. __HAL_LOCK(hrtc);
  658. hrtc->State = HAL_RTC_STATE_BUSY;
  659. /* Copy control register into temporary variable */
  660. tmpreg = hrtc->Instance->TAMPCR;
  661. /* Enable selected tamper */
  662. tmpreg |= (sTamper->Tamper);
  663. /* Configure the tamper trigger bit (located just next to the tamper enable bit) */
  664. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  665. {
  666. /* Set the tamper trigger bit */
  667. tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  668. }
  669. else
  670. {
  671. /* Clear the tamper trigger bit */
  672. tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  673. }
  674. /* Configure the tamper backup registers erasure bit */
  675. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  676. {
  677. #if defined(RTC_TAMPCR_TAMP1E)
  678. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  679. {
  680. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1NOERASE);
  681. }
  682. #endif /* RTC_TAMPCR_TAMP1E */
  683. #if defined(RTC_TAMPCR_TAMP2E)
  684. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  685. {
  686. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2NOERASE);
  687. }
  688. #endif /* RTC_TAMPCR_TAMP2E */
  689. #if defined(RTC_TAMPCR_TAMP3E)
  690. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  691. {
  692. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3NOERASE);
  693. }
  694. #endif /* RTC_TAMPCR_TAMP3E */
  695. }
  696. else
  697. {
  698. #if defined(RTC_TAMPCR_TAMP1E)
  699. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  700. {
  701. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1NOERASE);
  702. }
  703. #endif /* RTC_TAMPCR_TAMP1E */
  704. #if defined(RTC_TAMPCR_TAMP2E)
  705. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  706. {
  707. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2NOERASE);
  708. }
  709. #endif /* RTC_TAMPCR_TAMP2E */
  710. #if defined(RTC_TAMPCR_TAMP3E)
  711. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  712. {
  713. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3NOERASE);
  714. }
  715. #endif /* RTC_TAMPCR_TAMP3E */
  716. }
  717. /* Configure the tamper flags masking bit */
  718. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  719. {
  720. #if defined(RTC_TAMPCR_TAMP1E)
  721. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  722. {
  723. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP1MF);
  724. }
  725. #endif /* RTC_TAMPCR_TAMP1E */
  726. #if defined(RTC_TAMPCR_TAMP2E)
  727. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  728. {
  729. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP2MF);
  730. }
  731. #endif /* RTC_TAMPCR_TAMP2E */
  732. #if defined(RTC_TAMPCR_TAMP3E)
  733. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  734. {
  735. tmpreg |= (uint32_t)(RTC_TAMPCR_TAMP3MF);
  736. }
  737. #endif /* RTC_TAMPCR_TAMP3E */
  738. }
  739. else
  740. {
  741. #if defined(RTC_TAMPCR_TAMP1E)
  742. if ((sTamper->Tamper & RTC_TAMPER_1) != 0U)
  743. {
  744. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP1MF);
  745. }
  746. #endif /* RTC_TAMPCR_TAMP1E */
  747. #if defined(RTC_TAMPCR_TAMP2E)
  748. if ((sTamper->Tamper & RTC_TAMPER_2) != 0U)
  749. {
  750. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP2MF);
  751. }
  752. #endif /* RTC_TAMPCR_TAMP2E */
  753. #if defined(RTC_TAMPCR_TAMP3E)
  754. if ((sTamper->Tamper & RTC_TAMPER_3) != 0U)
  755. {
  756. tmpreg &= (uint32_t)~(RTC_TAMPCR_TAMP3MF);
  757. }
  758. #endif /* RTC_TAMPCR_TAMP3E */
  759. }
  760. /* Clearing remaining fields before setting them */
  761. tmpreg &= ~(RTC_TAMPERFILTER_MASK | RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  762. RTC_TAMPERPRECHARGEDURATION_MASK | RTC_TAMPER_PULLUP_MASK | \
  763. RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  764. /* Set remaining parameters of desired configuration into temporary variable */
  765. tmpreg |= ((uint32_t)sTamper->Filter | \
  766. (uint32_t)sTamper->SamplingFrequency | \
  767. (uint32_t)sTamper->PrechargeDuration | \
  768. (uint32_t)sTamper->TamperPullUp | \
  769. (uint32_t)sTamper->TimeStampOnTamperDetection);
  770. /* Enable interrupt on selected tamper */
  771. tmpreg |= (uint32_t)sTamper->Interrupt;
  772. /* Copy desired configuration into configuration register */
  773. hrtc->Instance->TAMPCR = tmpreg;
  774. #if !defined(DUAL_CORE)
  775. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  776. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  777. #endif
  778. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  779. hrtc->State = HAL_RTC_STATE_READY;
  780. /* Process Unlocked */
  781. __HAL_UNLOCK(hrtc);
  782. return HAL_OK;
  783. }
  784. #endif /* RTC_TAMPCR_TAMP1E */
  785. #if defined(TAMP_CR1_TAMP1E)
  786. /**
  787. * @brief Deactivate Tamper.
  788. * @param hrtc RTC handle
  789. * @param Tamper Selected tamper pin.
  790. * This parameter can be a combination of the following values:
  791. * @arg RTC_TAMPER_1
  792. * @arg RTC_TAMPER_2
  793. * @retval HAL status
  794. */
  795. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc, uint32_t Tamper)
  796. {
  797. /* Point on TAMPER registers base address */
  798. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
  799. assert_param(IS_RTC_TAMPER(Tamper));
  800. /* Disable the selected Tamper pin */
  801. tamp->CR1 &= ~Tamper;
  802. /* Disable the selected Tamper interrupt */
  803. tamp->IER &= ~Tamper;
  804. /* Clear the selected tamper flags in SR register by setting corresponding bits in SCR register */
  805. tamp->SCR = Tamper;
  806. /* Clear the selected tamper configuration (trigger, mask flag, and no-erase) */
  807. tamp->CR2 &= ~((Tamper << TAMP_CR2_TAMP1TRG_Pos) | (Tamper << TAMP_CR2_TAMP1MSK_Pos) | (Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  808. return HAL_OK;
  809. }
  810. #endif /* TAMP_CR1_TAMP1E */
  811. #if defined(RTC_TAMPCR_TAMP1E)
  812. /**
  813. * @brief Deactivate Tamper.
  814. * @param hrtc RTC handle
  815. * @param Tamper Selected tamper pin.
  816. * This parameter can be any combination of the following values:
  817. * @arg RTC_TAMPER_1
  818. * @arg RTC_TAMPER_2
  819. * @arg RTC_TAMPER_3
  820. * @retval HAL status
  821. */
  822. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef * hrtc, uint32_t Tamper)
  823. {
  824. assert_param(IS_RTC_TAMPER(Tamper));
  825. /* Process Locked */
  826. __HAL_LOCK(hrtc);
  827. hrtc->State = HAL_RTC_STATE_BUSY;
  828. /* Disable the selected Tamper pin */
  829. hrtc->Instance->TAMPCR &= ((uint32_t)~Tamper);
  830. /* Disable the selected Tamper interrupt */
  831. #if defined(RTC_TAMPCR_TAMP1E)
  832. if ((Tamper & RTC_TAMPER_1) != 0U)
  833. {
  834. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
  835. }
  836. #endif /* RTC_TAMPCR_TAMP1E */
  837. #if defined(RTC_TAMPCR_TAMP2E)
  838. if ((Tamper & RTC_TAMPER_2) != 0U)
  839. {
  840. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
  841. }
  842. #endif /* RTC_TAMPCR_TAMP2E */
  843. #if defined(RTC_TAMPCR_TAMP3E)
  844. if ((Tamper & RTC_TAMPER_3) != 0U)
  845. {
  846. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
  847. }
  848. #endif /* RTC_TAMPCR_TAMP3E */
  849. hrtc->State = HAL_RTC_STATE_READY;
  850. /* Process Unlocked */
  851. __HAL_UNLOCK(hrtc);
  852. return HAL_OK;
  853. }
  854. #endif /* RTC_TAMPCR_TAMP1E */
  855. #if defined(TAMP_CR1_ITAMP1E)
  856. /**
  857. * @brief Set Internal Tamper
  858. * @param hrtc RTC handle
  859. * @param sIntTamper Pointer to Internal Tamper Structure.
  860. * @retval HAL status
  861. */
  862. HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
  863. {
  864. /* Check the parameters */
  865. assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
  866. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
  867. /* Time-Stamp on internal tamper */
  868. if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
  869. {
  870. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  871. MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
  872. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  873. }
  874. /* Control register 1 */
  875. SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
  876. return HAL_OK;
  877. }
  878. /**
  879. * @brief Set Internal Tamper in interrupt mode
  880. * @param hrtc RTC handle
  881. * @param sIntTamper Pointer to Internal Tamper Structure.
  882. * @retval HAL status
  883. */
  884. HAL_StatusTypeDef HAL_RTCEx_SetInternalTamper_IT(RTC_HandleTypeDef *hrtc, RTC_InternalTamperTypeDef *sIntTamper)
  885. {
  886. /* Check the parameters */
  887. assert_param(IS_RTC_INTERNAL_TAMPER(sIntTamper->IntTamper));
  888. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sIntTamper->TimeStampOnTamperDetection));
  889. /* Time-stamp on internal tamper */
  890. if (READ_BIT(RTC->CR, RTC_CR_TAMPTS) != sIntTamper->TimeStampOnTamperDetection)
  891. {
  892. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  893. MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sIntTamper->TimeStampOnTamperDetection);
  894. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  895. }
  896. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  897. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  898. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
  899. /* Interrupt enable register */
  900. SET_BIT(TAMP->IER, sIntTamper->IntTamper);
  901. /* Control register 1 */
  902. SET_BIT(TAMP->CR1, sIntTamper->IntTamper);
  903. return HAL_OK;
  904. }
  905. /**
  906. * @brief Deactivate Internal Tamper.
  907. * @param hrtc RTC handle
  908. * @param IntTamper Selected internal tamper event.
  909. * This parameter can be any combination of existing internal tampers.
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTamper(RTC_HandleTypeDef *hrtc, uint32_t IntTamper)
  913. {
  914. UNUSED(hrtc);
  915. assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
  916. /* Disable the selected Tamper pin */
  917. CLEAR_BIT(TAMP->CR1, IntTamper);
  918. /* Clear internal tamper interrupt mode configuration */
  919. CLEAR_BIT(TAMP->IER, IntTamper);
  920. /* Clear internal tamper interrupt */
  921. WRITE_REG(TAMP->SCR, IntTamper);
  922. return HAL_OK;
  923. }
  924. #endif /* TAMP_CR1_ITAMP1E */
  925. #if defined(TAMP_ATCR1_TAMP1AM)
  926. /**
  927. * @brief Set all active Tampers at the same time.
  928. * @param hrtc RTC handle
  929. * @param sAllTamper Pointer to active Tamper Structure.
  930. * @retval HAL status
  931. */
  932. HAL_StatusTypeDef HAL_RTCEx_SetActiveTampers(RTC_HandleTypeDef *hrtc, RTC_ActiveTampersTypeDef *sAllTamper)
  933. {
  934. uint32_t IER, CR1, CR2, ATCR1, CR, i, tickstart;
  935. #ifdef USE_FULL_ASSERT
  936. for (i = 0; i < RTC_TAMP_NB; i++)
  937. {
  938. assert_param(IS_RTC_TAMPER_ERASE_MODE(sAllTamper->TampInput[i].NoErase));
  939. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sAllTamper->TampInput[i].MaskFlag));
  940. /* Mask flag only supported by TAMPER 1, 2 and 3 */
  941. assert_param(!((sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE) && (i > RTC_TAMPER_3)));
  942. }
  943. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sAllTamper->TimeStampOnTamperDetection));
  944. #endif /* #ifdef USE_FULL_ASSERT */
  945. /* Active Tampers must not be already enabled */
  946. if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) != 0U)
  947. {
  948. /* Disable all actives tampers with HAL_RTCEx_DeactivateActiveTampers and try again */
  949. return HAL_ERROR;
  950. }
  951. /* Set TimeStamp on tamper detection */
  952. CR = READ_REG(RTC->CR);
  953. if ((CR & RTC_CR_TAMPTS) != (sAllTamper->TimeStampOnTamperDetection))
  954. {
  955. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  956. MODIFY_REG(RTC->CR, RTC_CR_TAMPTS, sAllTamper->TimeStampOnTamperDetection);
  957. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  958. }
  959. CR1 = READ_REG(TAMP->CR1);
  960. CR2 = READ_REG(TAMP->CR2);
  961. IER = READ_REG(TAMP->IER);
  962. /* Set common parameters */
  963. ATCR1 = (sAllTamper->ActiveFilter | (sAllTamper->ActiveOutputChangePeriod << TAMP_ATCR1_ATPER_Pos) | sAllTamper->ActiveAsyncPrescaler);
  964. /* Set specific parameters for each active tamper inputs if enable */
  965. for (i = 0; i < RTC_TAMP_NB; i++)
  966. {
  967. if (sAllTamper->TampInput[i].Enable != RTC_ATAMP_DISABLE)
  968. {
  969. CR1 |= (TAMP_CR1_TAMP1E << i);
  970. ATCR1 |= (TAMP_ATCR1_TAMP1AM << i);
  971. if (sAllTamper->TampInput[i].Interrupt != RTC_ATAMP_INTERRUPT_DISABLE)
  972. {
  973. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  974. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  975. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  976. /* Interrupt enable register */
  977. IER |= (TAMP_IER_TAMP1IE << i);
  978. }
  979. if (sAllTamper->TampInput[i].MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  980. {
  981. CR2 |= (TAMP_CR2_TAMP1MSK << i);
  982. }
  983. if (sAllTamper->TampInput[i].NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  984. {
  985. CR2 |= (TAMP_CR2_TAMP1NOERASE << i);
  986. }
  987. /* Set ATOSHARE and configure ATOSELx[] in case of output sharing */
  988. if (sAllTamper->TampInput[i].Output != i)
  989. {
  990. ATCR1 |= TAMP_ATCR1_ATOSHARE;
  991. ATCR1 |= sAllTamper->TampInput[i].Output << ((2u * i) + TAMP_ATCR1_ATOSEL1_Pos);
  992. }
  993. }
  994. }
  995. WRITE_REG(TAMP->IER, IER);
  996. WRITE_REG(TAMP->IER, IER);
  997. WRITE_REG(TAMP->ATCR1, ATCR1);
  998. #if defined(TAMP_ATCR2_ATOSEL1)
  999. WRITE_REG(TAMP->ATCR2, ATCR2);
  1000. #endif /* TAMP_ATCR2_ATOSEL1 */
  1001. WRITE_REG(TAMP->CR2, CR2);
  1002. WRITE_REG(TAMP->CR1, CR1);
  1003. /* Write seed */
  1004. for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
  1005. {
  1006. WRITE_REG(TAMP->ATSEEDR, sAllTamper->Seed[i]);
  1007. }
  1008. /* Wait till RTC SEEDF flag is set and if Time out is reached exit */
  1009. tickstart = HAL_GetTick();
  1010. while (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0u)
  1011. {
  1012. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1013. {
  1014. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1015. return HAL_TIMEOUT;
  1016. }
  1017. }
  1018. return HAL_OK;
  1019. }
  1020. #endif /* TAMP_ATCR1_TAMP1AM */
  1021. #if defined(TAMP_ATSEEDR_SEED)
  1022. /**
  1023. * @brief Write a new seed. Active tamper must be enabled.
  1024. * @param hrtc RTC handle
  1025. * @param pSeed Pointer to active tamper seed values.
  1026. * @retval HAL status
  1027. */
  1028. HAL_StatusTypeDef HAL_RTCEx_SetActiveSeed(RTC_HandleTypeDef *hrtc, uint32_t *pSeed)
  1029. {
  1030. uint32_t i, tickstart;
  1031. /* Active Tampers must be enabled */
  1032. if (READ_BIT(TAMP->ATOR, TAMP_ATOR_INITS) == 0U)
  1033. {
  1034. return HAL_ERROR;
  1035. }
  1036. for (i = 0; i < RTC_ATAMP_SEED_NB_UINT32; i++)
  1037. {
  1038. WRITE_REG(TAMP->ATSEEDR, pSeed[i]);
  1039. }
  1040. /* Wait till RTC SEEDF flag is set and if Time out is reached exit */
  1041. tickstart = HAL_GetTick();
  1042. while (READ_BIT(TAMP->ATOR, TAMP_ATOR_SEEDF) != 0U)
  1043. {
  1044. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1045. {
  1046. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1047. return HAL_TIMEOUT;
  1048. }
  1049. }
  1050. return HAL_OK;
  1051. }
  1052. #endif /* TAMP_ATSEEDR_SEED */
  1053. #if defined(TAMP_ATCR1_TAMP1AM)
  1054. /**
  1055. * @brief Deactivate all Active Tampers at the same time.
  1056. * @param hrtc RTC handle
  1057. * @retval HAL status
  1058. */
  1059. HAL_StatusTypeDef HAL_RTCEx_DeactivateActiveTampers(RTC_HandleTypeDef *hrtc)
  1060. {
  1061. /* Get Active tampers */
  1062. uint32_t ATamp_mask = READ_BIT(TAMP->ATCR1, TAMP_ALL);
  1063. UNUSED(hrtc);
  1064. /* Disable all actives tampers but not passives tampers */
  1065. CLEAR_BIT(TAMP->CR1, ATamp_mask);
  1066. /* Disable no erase and mask */
  1067. CLEAR_BIT(TAMP->CR2, (ATamp_mask | ((ATamp_mask & (TAMP_ATCR1_TAMP1AM | TAMP_ATCR1_TAMP2AM | TAMP_ATCR1_TAMP3AM)) << TAMP_CR2_TAMP1MSK_Pos)));
  1068. /* Clear tamper interrupt and event flags (WO register) of all actives tampers but not passives tampers */
  1069. WRITE_REG(TAMP->SCR, ATamp_mask);
  1070. /* Clear all active tampers interrupt mode configuration but not passives tampers */
  1071. CLEAR_BIT(TAMP->IER, ATamp_mask);
  1072. CLEAR_BIT(TAMP->ATCR1, TAMP_ALL | TAMP_ATCR1_ATCKSEL | TAMP_ATCR1_ATPER | \
  1073. TAMP_ATCR1_ATOSHARE | TAMP_ATCR1_FLTEN);
  1074. #if defined(TAMP_ATCR2_ATOSEL1)
  1075. CLEAR_BIT(TAMP->ATCR2, TAMP_ATCR2_ATOSEL1 | TAMP_ATCR2_ATOSEL2 | TAMP_ATCR2_ATOSEL3 | TAMP_ATCR2_ATOSEL4 |
  1076. TAMP_ATCR2_ATOSEL5 | TAMP_ATCR2_ATOSEL6 | TAMP_ATCR2_ATOSEL7 | TAMP_ATCR2_ATOSEL8);
  1077. #endif /* TAMP_ATCR2_ATOSEL1 */
  1078. return HAL_OK;
  1079. }
  1080. #endif /* TAMP_ATCR1_TAMP1AM */
  1081. /**
  1082. * @}
  1083. */
  1084. /** @addtogroup RTCEx_Exported_Functions_Group1
  1085. * @brief RTC TimeStamp and Tamper functions
  1086. *
  1087. * @{
  1088. */
  1089. /**
  1090. * @brief Handle TimeStamp interrupt request.
  1091. * @param hrtc RTC handle
  1092. * @retval None
  1093. */
  1094. #if defined(RTC_MISR_TSMF)
  1095. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  1096. {
  1097. /* Point on TAMPER registers base address */
  1098. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET);
  1099. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  1100. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  1101. if ((hrtc->Instance->MISR & RTC_MISR_TSMF) != 0u)
  1102. {
  1103. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1104. /* Call TimeStampEvent registered Callback */
  1105. hrtc->TimeStampEventCallback(hrtc);
  1106. #else
  1107. HAL_RTCEx_TimeStampEventCallback(hrtc);
  1108. #endif
  1109. /* Not immediately clear flags because the content of RTC_TSTR and RTC_TSDR are cleared when TSF bit is reset.*/
  1110. hrtc->Instance->SCR = RTC_SCR_CTSF;
  1111. }
  1112. /* Get interrupt status */
  1113. uint32_t tmp = tamp->MISR;
  1114. /* Immediately clear flags */
  1115. tamp->SCR = tmp;
  1116. #if defined(TAMP_CR1_TAMP1E)
  1117. /* Check Tamper1 status */
  1118. if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
  1119. {
  1120. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1121. /* Call Tamper 1 Event registered Callback */
  1122. hrtc->Tamper1EventCallback(hrtc);
  1123. #else
  1124. /* Tamper1 callback */
  1125. HAL_RTCEx_Tamper1EventCallback(hrtc);
  1126. #endif
  1127. }
  1128. #endif /* TAMP_CR1_TAMP1E */
  1129. #if defined(TAMP_CR1_TAMP2E)
  1130. /* Check Tamper2 status */
  1131. if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
  1132. {
  1133. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1134. /* Call Tamper 2 Event registered Callback */
  1135. hrtc->Tamper2EventCallback(hrtc);
  1136. #else
  1137. /* Tamper2 callback */
  1138. HAL_RTCEx_Tamper2EventCallback(hrtc);
  1139. #endif
  1140. }
  1141. #endif /* TAMP_CR1_TAMP2E */
  1142. #if defined(TAMP_CR1_TAMP3E)
  1143. /* Check Tamper3 status */
  1144. if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
  1145. {
  1146. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1147. /* Call Tamper 3 Event registered Callback */
  1148. hrtc->Tamper3EventCallback(hrtc);
  1149. #else
  1150. /* Tamper3 callback */
  1151. HAL_RTCEx_Tamper3EventCallback(hrtc);
  1152. #endif
  1153. }
  1154. #endif /* TAMP_CR1_TAMP3E */
  1155. #if defined(TAMP_CR1_ITAMP1E)
  1156. /* Check Internal Tamper status */
  1157. if ((tmp & RTC_INT_TAMPER_1) == RTC_INT_TAMPER_1)
  1158. {
  1159. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1160. /* Call Internal Tamper Event registered callback */
  1161. hrtc->InternalTamper1EventCallback(hrtc);
  1162. #else
  1163. /* Call Internal Tamper Event by-default callback */
  1164. HAL_RTCEx_InternalTamper1EventCallback(hrtc);
  1165. #endif
  1166. }
  1167. #endif /* TAMP_CR1_ITAMP1E */
  1168. #if defined(TAMP_CR1_ITAMP2E)
  1169. /* Check Internal Tamper status */
  1170. if ((tmp & RTC_INT_TAMPER_2) == RTC_INT_TAMPER_2)
  1171. {
  1172. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1173. /* Call Internal Tamper Event registered callback */
  1174. hrtc->InternalTamper2EventCallback(hrtc);
  1175. #else
  1176. /* Call Internal Tamper Event by-default callback */
  1177. HAL_RTCEx_InternalTamper2EventCallback(hrtc);
  1178. #endif
  1179. }
  1180. #endif /* TAMP_CR1_ITAMP2E */
  1181. #if defined(TAMP_CR1_ITAMP3E)
  1182. /* Check Internal Tamper status */
  1183. if ((tmp & RTC_INT_TAMPER_3) == RTC_INT_TAMPER_3)
  1184. {
  1185. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1186. /* Call Internal Tamper Event registered callback */
  1187. hrtc->InternalTamper3EventCallback(hrtc);
  1188. #else
  1189. /* Call Internal Tamper Event by-default callback */
  1190. HAL_RTCEx_InternalTamper3EventCallback(hrtc);
  1191. #endif
  1192. }
  1193. #endif /* TAMP_CR1_ITAMP3E */
  1194. #if defined(TAMP_CR1_ITAMP4E)
  1195. /* Check Internal Tamper status */
  1196. if ((tmp & RTC_INT_TAMPER_4) == RTC_INT_TAMPER_4)
  1197. {
  1198. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1199. /* Call Internal Tamper Event registered callback */
  1200. hrtc->InternalTamper4EventCallback(hrtc);
  1201. #else
  1202. /* Call Internal Tamper Event by-default callback */
  1203. HAL_RTCEx_InternalTamper4EventCallback(hrtc);
  1204. #endif
  1205. }
  1206. #endif /* TAMP_CR1_ITAMP4E */
  1207. #if defined(TAMP_CR1_ITAMP5E)
  1208. /* Check Internal Tamper status */
  1209. if ((tmp & RTC_INT_TAMPER_5) == RTC_INT_TAMPER_5)
  1210. {
  1211. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1212. /* Call Internal Tamper Event registered callback */
  1213. hrtc->InternalTamper5EventCallback(hrtc);
  1214. #else
  1215. /* Call Internal Tamper Event by-default callback */
  1216. HAL_RTCEx_InternalTamper5EventCallback(hrtc);
  1217. #endif
  1218. }
  1219. #endif /* TAMP_CR1_ITAMP5E */
  1220. #if defined(TAMP_CR1_ITAMP6E)
  1221. /* Check Internal Tamper status */
  1222. if ((tmp & RTC_INT_TAMPER_6) == RTC_INT_TAMPER_6)
  1223. {
  1224. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1225. /* Call Internal Tamper Event registered callback */
  1226. hrtc->InternalTamper6EventCallback(hrtc);
  1227. #else
  1228. /* Call Internal Tamper Event by-default callback */
  1229. HAL_RTCEx_InternalTamper6EventCallback(hrtc);
  1230. #endif
  1231. }
  1232. #endif /* TAMP_CR1_ITAMP6E */
  1233. #if defined(TAMP_CR1_ITAMP8E)
  1234. /* Check Internal Tamper status */
  1235. if ((tmp & RTC_INT_TAMPER_8) == RTC_INT_TAMPER_8)
  1236. {
  1237. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1238. /* Call Internal Tamper Event registered callback */
  1239. hrtc->InternalTamper8EventCallback(hrtc);
  1240. #else
  1241. /* Call Internal Tamper Event by-default callback */
  1242. HAL_RTCEx_InternalTamper8EventCallback(hrtc);
  1243. #endif
  1244. }
  1245. #endif /* TAMP_CR1_ITAMP8E */
  1246. /* Change RTC state */
  1247. hrtc->State = HAL_RTC_STATE_READY;
  1248. }
  1249. #endif /* RTC_MISR_TSMF */
  1250. #if defined(RTC_ISR_TSF)
  1251. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  1252. {
  1253. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  1254. #if defined(DUAL_CORE)
  1255. if (HAL_GetCurrentCPUID() == CM7_CPUID)
  1256. {
  1257. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  1258. }
  1259. else
  1260. {
  1261. __HAL_RTC_TAMPER_TIMESTAMP_EXTID2_CLEAR_FLAG();
  1262. }
  1263. #else /* SINGLE_CORE */
  1264. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  1265. #endif /* DUAL_CORE */
  1266. /* Get the TimeStamp interrupt source enable status */
  1267. if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
  1268. {
  1269. /* Get the pending status of the TIMESTAMP Interrupt */
  1270. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
  1271. {
  1272. /* TIMESTAMP callback */
  1273. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1274. hrtc->TimeStampEventCallback(hrtc);
  1275. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1276. HAL_RTCEx_TimeStampEventCallback(hrtc);
  1277. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1278. /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
  1279. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  1280. }
  1281. }
  1282. #if defined(RTC_TAMPCR_TAMP1E)
  1283. /* Get the Tamper1 interrupt source enable status */
  1284. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
  1285. {
  1286. /* Get the pending status of the Tamper1 Interrupt */
  1287. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
  1288. {
  1289. /* Clear the Tamper1 interrupt pending bit */
  1290. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  1291. /* Tamper1 callback */
  1292. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1293. hrtc->Tamper1EventCallback(hrtc);
  1294. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1295. HAL_RTCEx_Tamper1EventCallback(hrtc);
  1296. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1297. }
  1298. }
  1299. #endif /* RTC_TAMPCR_TAMP1E */
  1300. #if defined(RTC_TAMPCR_TAMP2E)
  1301. /* Get the Tamper2 interrupt source enable status */
  1302. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
  1303. {
  1304. /* Get the pending status of the Tamper2 Interrupt */
  1305. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
  1306. {
  1307. /* Clear the Tamper2 interrupt pending bit */
  1308. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  1309. /* Tamper2 callback */
  1310. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1311. hrtc->Tamper2EventCallback(hrtc);
  1312. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1313. HAL_RTCEx_Tamper2EventCallback(hrtc);
  1314. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1315. }
  1316. }
  1317. #endif /* RTC_TAMPCR_TAMP2E */
  1318. #if defined(RTC_TAMPCR_TAMP3E)
  1319. /* Get the Tamper3 interrupts source enable status */
  1320. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
  1321. {
  1322. /* Get the pending status of the Tamper3 Interrupt */
  1323. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
  1324. {
  1325. /* Clear the Tamper3 interrupt pending bit */
  1326. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  1327. /* Tamper3 callback */
  1328. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1329. hrtc->Tamper3EventCallback(hrtc);
  1330. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1331. HAL_RTCEx_Tamper3EventCallback(hrtc);
  1332. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  1333. }
  1334. }
  1335. #endif /* RTC_TAMPCR_TAMP3E */
  1336. /* Change RTC state */
  1337. hrtc->State = HAL_RTC_STATE_READY;
  1338. }
  1339. #endif /* RTC_ISR_TSF */
  1340. /**
  1341. * @brief TimeStamp callback.
  1342. * @param hrtc RTC handle
  1343. * @retval None
  1344. */
  1345. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  1346. {
  1347. /* Prevent unused argument(s) compilation warning */
  1348. UNUSED(hrtc);
  1349. /* NOTE : This function should not be modified, when the callback is needed,
  1350. the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  1351. */
  1352. }
  1353. /**
  1354. * @}
  1355. */
  1356. /** @addtogroup RTCEx_Exported_Functions_Group5
  1357. * @brief Extended RTC Tamper functions
  1358. *
  1359. * @{
  1360. */
  1361. #if defined(RTC_TAMPER_1)
  1362. /**
  1363. * @brief Tamper 1 callback.
  1364. * @param hrtc RTC handle
  1365. * @retval None
  1366. */
  1367. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef * hrtc)
  1368. {
  1369. /* Prevent unused argument(s) compilation warning */
  1370. UNUSED(hrtc);
  1371. /* NOTE : This function should not be modified, when the callback is needed,
  1372. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  1373. */
  1374. }
  1375. #endif /* RTC_TAMPER_1 */
  1376. #if defined(RTC_TAMPER_2)
  1377. /**
  1378. * @brief Tamper 2 callback.
  1379. * @param hrtc RTC handle
  1380. * @retval None
  1381. */
  1382. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef * hrtc)
  1383. {
  1384. /* Prevent unused argument(s) compilation warning */
  1385. UNUSED(hrtc);
  1386. /* NOTE : This function should not be modified, when the callback is needed,
  1387. the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  1388. */
  1389. }
  1390. #endif /* RTC_TAMPER_2 */
  1391. #if defined(RTC_TAMPER_3)
  1392. /**
  1393. * @brief Tamper 3 callback.
  1394. * @param hrtc RTC handle
  1395. * @retval None
  1396. */
  1397. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef * hrtc)
  1398. {
  1399. /* Prevent unused argument(s) compilation warning */
  1400. UNUSED(hrtc);
  1401. /* NOTE : This function should not be modified, when the callback is needed,
  1402. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  1403. */
  1404. }
  1405. #endif /* RTC_TAMPER_3 */
  1406. /**
  1407. * @brief Internal Tamper 1 callback.
  1408. * @param hrtc RTC handle
  1409. * @retval None
  1410. */
  1411. __weak void HAL_RTCEx_InternalTamper1EventCallback(RTC_HandleTypeDef *hrtc)
  1412. {
  1413. /* Prevent unused argument(s) compilation warning */
  1414. UNUSED(hrtc);
  1415. /* NOTE : This function should not be modified, when the callback is needed,
  1416. the HAL_RTCEx_InternalTamper1EventCallback could be implemented in the user file
  1417. */
  1418. }
  1419. /**
  1420. * @brief Internal Tamper 2 callback.
  1421. * @param hrtc RTC handle
  1422. * @retval None
  1423. */
  1424. __weak void HAL_RTCEx_InternalTamper2EventCallback(RTC_HandleTypeDef *hrtc)
  1425. {
  1426. /* Prevent unused argument(s) compilation warning */
  1427. UNUSED(hrtc);
  1428. /* NOTE : This function should not be modified, when the callback is needed,
  1429. the HAL_RTCEx_InternalTamper2EventCallback could be implemented in the user file
  1430. */
  1431. }
  1432. /**
  1433. * @brief Internal Tamper 3 callback.
  1434. * @param hrtc RTC handle
  1435. * @retval None
  1436. */
  1437. __weak void HAL_RTCEx_InternalTamper3EventCallback(RTC_HandleTypeDef *hrtc)
  1438. {
  1439. /* Prevent unused argument(s) compilation warning */
  1440. UNUSED(hrtc);
  1441. /* NOTE : This function should not be modified, when the callback is needed,
  1442. the HAL_RTCEx_InternalTamper3EventCallback could be implemented in the user file
  1443. */
  1444. }
  1445. /**
  1446. * @brief Internal Tamper 4 callback.
  1447. * @param hrtc RTC handle
  1448. * @retval None
  1449. */
  1450. __weak void HAL_RTCEx_InternalTamper4EventCallback(RTC_HandleTypeDef *hrtc)
  1451. {
  1452. /* Prevent unused argument(s) compilation warning */
  1453. UNUSED(hrtc);
  1454. /* NOTE : This function should not be modified, when the callback is needed,
  1455. the HAL_RTCEx_InternalTamper4EventCallback could be implemented in the user file
  1456. */
  1457. }
  1458. /**
  1459. * @brief Internal Tamper 5 callback.
  1460. * @param hrtc RTC handle
  1461. * @retval None
  1462. */
  1463. __weak void HAL_RTCEx_InternalTamper5EventCallback(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_InternalTamper5EventCallback could be implemented in the user file
  1469. */
  1470. }
  1471. /**
  1472. * @brief Internal Tamper 6 callback.
  1473. * @param hrtc RTC handle
  1474. * @retval None
  1475. */
  1476. __weak void HAL_RTCEx_InternalTamper6EventCallback(RTC_HandleTypeDef *hrtc)
  1477. {
  1478. /* Prevent unused argument(s) compilation warning */
  1479. UNUSED(hrtc);
  1480. /* NOTE : This function should not be modified, when the callback is needed,
  1481. the HAL_RTCEx_InternalTamper6EventCallback could be implemented in the user file
  1482. */
  1483. }
  1484. /**
  1485. * @brief Internal Tamper 8 callback.
  1486. * @param hrtc RTC handle
  1487. * @retval None
  1488. */
  1489. __weak void HAL_RTCEx_InternalTamper8EventCallback(RTC_HandleTypeDef *hrtc)
  1490. {
  1491. /* Prevent unused argument(s) compilation warning */
  1492. UNUSED(hrtc);
  1493. /* NOTE : This function should not be modified, when the callback is needed,
  1494. the HAL_RTCEx_InternalTamper8EventCallback could be implemented in the user file
  1495. */
  1496. }
  1497. /**
  1498. * @}
  1499. */
  1500. /** @addtogroup RTCEx_Exported_Functions_Group1
  1501. * @brief RTC TimeStamp and Tamper functions
  1502. *
  1503. * @{
  1504. */
  1505. /**
  1506. * @brief Handle TimeStamp polling request.
  1507. * @param hrtc RTC handle
  1508. * @param Timeout Timeout duration
  1509. * @retval HAL status
  1510. */
  1511. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1512. {
  1513. uint32_t tickstart = HAL_GetTick();
  1514. while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
  1515. {
  1516. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
  1517. {
  1518. /* Clear the TIMESTAMP OverRun Flag */
  1519. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  1520. /* Change TIMESTAMP state */
  1521. hrtc->State = HAL_RTC_STATE_ERROR;
  1522. return HAL_ERROR;
  1523. }
  1524. if (Timeout != HAL_MAX_DELAY)
  1525. {
  1526. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1527. {
  1528. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1529. return HAL_TIMEOUT;
  1530. }
  1531. }
  1532. }
  1533. /* Change RTC state */
  1534. hrtc->State = HAL_RTC_STATE_READY;
  1535. return HAL_OK;
  1536. }
  1537. /**
  1538. * @}
  1539. */
  1540. /** @addtogroup RTCEx_Exported_Functions_Group5
  1541. * @brief Extended RTC Tamper functions
  1542. *
  1543. * @{
  1544. */
  1545. #if defined(RTC_TAMPER_1)
  1546. /**
  1547. * @brief Handle Tamper1 Polling.
  1548. * @param hrtc RTC handle
  1549. * @param Timeout Timeout duration
  1550. * @retval HAL status
  1551. */
  1552. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
  1553. {
  1554. uint32_t tickstart = HAL_GetTick();
  1555. /* Get the status of the Interrupt */
  1556. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
  1557. {
  1558. if (Timeout != HAL_MAX_DELAY)
  1559. {
  1560. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1561. {
  1562. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1563. return HAL_TIMEOUT;
  1564. }
  1565. }
  1566. }
  1567. /* Clear the Tamper Flag */
  1568. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  1569. /* Change RTC state */
  1570. hrtc->State = HAL_RTC_STATE_READY;
  1571. return HAL_OK;
  1572. }
  1573. #endif /* RTC_TAMPER_1 */
  1574. #if defined(RTC_TAMPER_2)
  1575. /**
  1576. * @brief Handle Tamper2 Polling.
  1577. * @param hrtc RTC handle
  1578. * @param Timeout Timeout duration
  1579. * @retval HAL status
  1580. */
  1581. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
  1582. {
  1583. uint32_t tickstart = HAL_GetTick();
  1584. /* Get the status of the Interrupt */
  1585. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
  1586. {
  1587. if (Timeout != HAL_MAX_DELAY)
  1588. {
  1589. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1590. {
  1591. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1592. return HAL_TIMEOUT;
  1593. }
  1594. }
  1595. }
  1596. /* Clear the Tamper Flag */
  1597. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  1598. /* Change RTC state */
  1599. hrtc->State = HAL_RTC_STATE_READY;
  1600. return HAL_OK;
  1601. }
  1602. #endif /* RTC_TAMPER_2 */
  1603. #if defined(RTC_TAMPER_3)
  1604. /**
  1605. * @brief Handle Tamper3 Polling.
  1606. * @param hrtc RTC handle
  1607. * @param Timeout Timeout duration
  1608. * @retval HAL status
  1609. */
  1610. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
  1611. {
  1612. uint32_t tickstart = HAL_GetTick();
  1613. /* Get the status of the Interrupt */
  1614. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
  1615. {
  1616. if (Timeout != HAL_MAX_DELAY)
  1617. {
  1618. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1619. {
  1620. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1621. return HAL_TIMEOUT;
  1622. }
  1623. }
  1624. }
  1625. /* Clear the Tamper Flag */
  1626. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  1627. /* Change RTC state */
  1628. hrtc->State = HAL_RTC_STATE_READY;
  1629. return HAL_OK;
  1630. }
  1631. #endif /* RTC_TAMPER_3 */
  1632. #if defined(TAMP_CR1_ITAMP1E)
  1633. /**
  1634. * @brief Internal Tamper event polling.
  1635. * @param hrtc RTC handle
  1636. * @param IntTamper selected tamper.
  1637. * This parameter can be any combination of existing internal tampers.
  1638. * @param Timeout Timeout duration
  1639. * @retval HAL status
  1640. */
  1641. HAL_StatusTypeDef HAL_RTCEx_PollForInternalTamperEvent(RTC_HandleTypeDef *hrtc, uint32_t IntTamper, uint32_t Timeout)
  1642. {
  1643. UNUSED(hrtc);
  1644. assert_param(IS_RTC_INTERNAL_TAMPER(IntTamper));
  1645. uint32_t tickstart = HAL_GetTick();
  1646. /* Get the status of the Interrupt */
  1647. while (READ_BIT(TAMP->SR, IntTamper) != IntTamper)
  1648. {
  1649. if (Timeout != HAL_MAX_DELAY)
  1650. {
  1651. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1652. {
  1653. return HAL_TIMEOUT;
  1654. }
  1655. }
  1656. }
  1657. /* Clear the Tamper Flag */
  1658. WRITE_REG(TAMP->SCR, IntTamper);
  1659. return HAL_OK;
  1660. }
  1661. #endif /* TAMP_CR1_ITAMP1E */
  1662. /**
  1663. * @}
  1664. */
  1665. /** @addtogroup RTCEx_Exported_Functions_Group2
  1666. * @brief RTC Wake-up functions
  1667. *
  1668. @verbatim
  1669. ===============================================================================
  1670. ##### RTC Wake-up functions #####
  1671. ===============================================================================
  1672. [..] This section provides functions allowing to configure Wake-up feature
  1673. @endverbatim
  1674. * @{
  1675. */
  1676. /**
  1677. * @brief Set wake up timer.
  1678. * @param hrtc RTC handle
  1679. * @param WakeUpCounter Wake up counter
  1680. * @param WakeUpClock Wake up clock
  1681. * @retval HAL status
  1682. */
  1683. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  1684. {
  1685. uint32_t tickstart;
  1686. /* Check the parameters */
  1687. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  1688. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  1689. /* Process Locked */
  1690. __HAL_LOCK(hrtc);
  1691. hrtc->State = HAL_RTC_STATE_BUSY;
  1692. /* Disable the write protection for RTC registers */
  1693. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1694. /* Check RTC WUTWF flag is reset only when wake up timer enabled */
  1695. if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  1696. {
  1697. tickstart = HAL_GetTick();
  1698. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  1699. #if defined(RTC_ICSR_WUTWF)
  1700. while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) != 0U)
  1701. #endif /* RTC_ICSR_WUTWF */
  1702. #if defined(RTC_ISR_WUTWF)
  1703. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
  1704. #endif /* RTC_ISR_WUTWF */
  1705. {
  1706. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1707. {
  1708. /* Enable the write protection for RTC registers */
  1709. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1710. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1711. /* Process Unlocked */
  1712. __HAL_UNLOCK(hrtc);
  1713. return HAL_TIMEOUT;
  1714. }
  1715. }
  1716. }
  1717. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1718. tickstart = HAL_GetTick();
  1719. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1720. #if defined(RTC_ICSR_WUTWF)
  1721. while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
  1722. #endif /* RTC_ICSR_WUTWF */
  1723. #if defined(RTC_ISR_WUTWF)
  1724. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  1725. #endif /* RTC_ISR_WUTWF */
  1726. {
  1727. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1728. {
  1729. /* Enable the write protection for RTC registers */
  1730. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1731. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1732. /* Process Unlocked */
  1733. __HAL_UNLOCK(hrtc);
  1734. return HAL_TIMEOUT;
  1735. }
  1736. }
  1737. /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
  1738. {
  1739. uint32_t CR_tmp = hrtc->Instance->CR;
  1740. CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
  1741. CR_tmp |= (uint32_t)WakeUpClock;
  1742. hrtc->Instance->CR = CR_tmp;
  1743. }
  1744. /* Configure the Wakeup Timer counter */
  1745. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  1746. /* Enable the Wakeup Timer */
  1747. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  1748. /* Enable the write protection for RTC registers */
  1749. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1750. hrtc->State = HAL_RTC_STATE_READY;
  1751. /* Process Unlocked */
  1752. __HAL_UNLOCK(hrtc);
  1753. return HAL_OK;
  1754. }
  1755. /**
  1756. * @brief Set wake up timer with interrupt.
  1757. * @param hrtc RTC handle
  1758. * @param WakeUpCounter Wake up counter
  1759. * @param WakeUpClock Wake up clock
  1760. * @retval HAL status
  1761. */
  1762. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  1763. {
  1764. uint32_t tickstart;
  1765. /* Check the parameters */
  1766. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  1767. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  1768. /* Process Locked */
  1769. __HAL_LOCK(hrtc);
  1770. hrtc->State = HAL_RTC_STATE_BUSY;
  1771. /* Disable the write protection for RTC registers */
  1772. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1773. /* Check RTC WUTWF flag is reset only when wake up timer enabled */
  1774. if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  1775. {
  1776. tickstart = HAL_GetTick();
  1777. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  1778. #if defined(RTC_ICSR_WUTWF)
  1779. while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) != 0U)
  1780. #endif /* RTC_ICSR_WUTWF */
  1781. #if defined(RTC_ISR_WUTWF)
  1782. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
  1783. #endif /* RTC_ISR_WUTWF */
  1784. {
  1785. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1786. {
  1787. /* Enable the write protection for RTC registers */
  1788. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1789. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1790. /* Process Unlocked */
  1791. __HAL_UNLOCK(hrtc);
  1792. return HAL_TIMEOUT;
  1793. }
  1794. }
  1795. }
  1796. /* Disable the Wake-Up timer */
  1797. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1798. /* Clear flag Wake-Up */
  1799. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1800. tickstart = HAL_GetTick();
  1801. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1802. #if defined(RTC_ICSR_WUTWF)
  1803. while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
  1804. #endif /* RTC_ICSR_WUTWF */
  1805. #if defined(RTC_ISR_WUTWF)
  1806. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  1807. #endif /* RTC_ISR_WUTWF */
  1808. {
  1809. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1810. {
  1811. /* Enable the write protection for RTC registers */
  1812. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1813. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1814. /* Process Unlocked */
  1815. __HAL_UNLOCK(hrtc);
  1816. return HAL_TIMEOUT;
  1817. }
  1818. }
  1819. /* Configure the Wakeup Timer counter */
  1820. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  1821. /* Clear the Wakeup Timer clock source bits and configure the clock source in CR register */
  1822. {
  1823. uint32_t CR_tmp = hrtc->Instance->CR;
  1824. CR_tmp &= (uint32_t)~RTC_CR_WUCKSEL;
  1825. CR_tmp |= (uint32_t)WakeUpClock;
  1826. hrtc->Instance->CR = CR_tmp;
  1827. }
  1828. #if !defined(DUAL_CORE)
  1829. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  1830. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  1831. #endif
  1832. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1833. /* Configure the Interrupt in the RTC_CR register */
  1834. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
  1835. /* Enable the Wakeup Timer */
  1836. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  1837. /* Enable the write protection for RTC registers */
  1838. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1839. hrtc->State = HAL_RTC_STATE_READY;
  1840. /* Process Unlocked */
  1841. __HAL_UNLOCK(hrtc);
  1842. return HAL_OK;
  1843. }
  1844. /**
  1845. * @brief Deactivate wake up timer counter.
  1846. * @param hrtc RTC handle
  1847. * @retval HAL status
  1848. */
  1849. HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1850. {
  1851. uint32_t tickstart;
  1852. /* Process Locked */
  1853. __HAL_LOCK(hrtc);
  1854. hrtc->State = HAL_RTC_STATE_BUSY;
  1855. /* Disable the write protection for RTC registers */
  1856. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1857. /* Disable the Wakeup Timer */
  1858. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1859. /* In case of interrupt mode is used, the interrupt source must disabled */
  1860. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
  1861. tickstart = HAL_GetTick();
  1862. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1863. #if defined(RTC_ICSR_WUTWF)
  1864. while (READ_BIT(hrtc->Instance->ICSR, RTC_FLAG_WUTWF) == 0U)
  1865. #endif /* RTC_ICSR_WUTWF */
  1866. #if defined(RTC_ISR_WUTWF)
  1867. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  1868. #endif /* RTC_ISR_WUTWF */
  1869. {
  1870. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1871. {
  1872. /* Enable the write protection for RTC registers */
  1873. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1874. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1875. /* Process Unlocked */
  1876. __HAL_UNLOCK(hrtc);
  1877. return HAL_TIMEOUT;
  1878. }
  1879. }
  1880. /* Enable the write protection for RTC registers */
  1881. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1882. hrtc->State = HAL_RTC_STATE_READY;
  1883. /* Process Unlocked */
  1884. __HAL_UNLOCK(hrtc);
  1885. return HAL_OK;
  1886. }
  1887. /**
  1888. * @brief Get wake up timer counter.
  1889. * @param hrtc RTC handle
  1890. * @retval Counter value
  1891. */
  1892. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1893. {
  1894. /* Get the counter value */
  1895. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  1896. }
  1897. /**
  1898. * @brief Handle Wake Up Timer interrupt request.
  1899. * @param hrtc RTC handle
  1900. * @retval None
  1901. */
  1902. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  1903. {
  1904. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  1905. #if defined(DUAL_CORE)
  1906. if (HAL_GetCurrentCPUID() == CM7_CPUID)
  1907. {
  1908. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1909. }
  1910. else
  1911. {
  1912. __HAL_RTC_WAKEUPTIMER_EXTID2_CLEAR_FLAG();
  1913. }
  1914. #else /* SINGLE_CORE */
  1915. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1916. #endif /* DUAL_CORE */
  1917. #if defined(RTC_MISR_WUTMF)
  1918. /* Get the pending status of the WAKEUPTIMER Interrupt */
  1919. if ((hrtc->Instance->MISR & RTC_MISR_WUTMF) != 0u)
  1920. {
  1921. /* Immediately clear flags */
  1922. hrtc->Instance->SCR = RTC_SCR_CWUTF;
  1923. /* WAKEUPTIMER callback */
  1924. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1925. /* Call WakeUpTimerEvent registered Callback */
  1926. hrtc->WakeUpTimerEventCallback(hrtc);
  1927. #else
  1928. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  1929. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1930. }
  1931. #endif /* RTC_MISR_WUTMF */
  1932. #if defined(RTC_ISR_WUTF)
  1933. /* Get the pending status of the WAKEUPTIMER Interrupt */
  1934. if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
  1935. {
  1936. /* Clear the WAKEUPTIMER interrupt pending bit */
  1937. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1938. /* WAKEUPTIMER callback */
  1939. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1940. /* Call WakeUpTimerEvent registered Callback */
  1941. hrtc->WakeUpTimerEventCallback(hrtc);
  1942. #else
  1943. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  1944. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1945. }
  1946. #endif /* RTC_ISR_WUTF */
  1947. /* Change RTC state */
  1948. hrtc->State = HAL_RTC_STATE_READY;
  1949. }
  1950. /**
  1951. * @brief Wake Up Timer callback.
  1952. * @param hrtc RTC handle
  1953. * @retval None
  1954. */
  1955. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef * hrtc)
  1956. {
  1957. /* Prevent unused argument(s) compilation warning */
  1958. UNUSED(hrtc);
  1959. /* NOTE : This function should not be modified, when the callback is needed,
  1960. the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  1961. */
  1962. }
  1963. /**
  1964. * @brief Handle Wake Up Timer Polling.
  1965. * @param hrtc RTC handle
  1966. * @param Timeout Timeout duration
  1967. * @retval HAL status
  1968. */
  1969. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
  1970. {
  1971. uint32_t tickstart = HAL_GetTick();
  1972. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
  1973. {
  1974. if (Timeout != HAL_MAX_DELAY)
  1975. {
  1976. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1977. {
  1978. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1979. return HAL_TIMEOUT;
  1980. }
  1981. }
  1982. }
  1983. /* Clear the WAKEUPTIMER Flag */
  1984. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1985. /* Change RTC state */
  1986. hrtc->State = HAL_RTC_STATE_READY;
  1987. return HAL_OK;
  1988. }
  1989. /**
  1990. * @}
  1991. */
  1992. /** @addtogroup RTCEx_Exported_Functions_Group6
  1993. * @brief Extended RTC Backup register functions
  1994. *
  1995. @verbatim
  1996. ===============================================================================
  1997. ##### Extended RTC Backup register functions #####
  1998. ===============================================================================
  1999. [..]
  2000. (+) Before calling any tamper or internal tamper function, you have to call first
  2001. HAL_RTC_Init() function.
  2002. (+) In that ine you can select to output tamper event on RTC pin.
  2003. [..]
  2004. This subsection provides functions allowing to
  2005. (+) Write a data in a specified RTC Backup data register
  2006. (+) Read a data in a specified RTC Backup data register
  2007. @endverbatim
  2008. * @{
  2009. */
  2010. /**
  2011. * @brief Write a data in a specified RTC Backup data register.
  2012. * @param hrtc RTC handle
  2013. * @param BackupRegister RTC Backup data Register number.
  2014. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
  2015. * specify the register.
  2016. * @param Data Data to be written in the specified Backup data register.
  2017. * @retval None
  2018. */
  2019. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef * hrtc, uint32_t BackupRegister, uint32_t Data)
  2020. {
  2021. uint32_t tmp;
  2022. /* Check the parameters */
  2023. assert_param(IS_RTC_BKP(BackupRegister));
  2024. /* Point on address of first backup register */
  2025. #if defined(TAMP_BKP0R)
  2026. tmp = (uint32_t) & (((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->BKP0R);
  2027. #endif /* TAMP_BKP0R */
  2028. #if defined(RTC_BKP0R)
  2029. tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  2030. #endif /* RTC_BKP0R */
  2031. tmp += (BackupRegister * 4U);
  2032. /* Write the specified register */
  2033. *(__IO uint32_t *)tmp = (uint32_t)Data;
  2034. }
  2035. /**
  2036. * @brief Read data from the specified RTC Backup data Register.
  2037. * @param hrtc RTC handle
  2038. * @param BackupRegister RTC Backup data Register number.
  2039. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
  2040. * specify the register.
  2041. * @retval Read value
  2042. */
  2043. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef * hrtc, uint32_t BackupRegister)
  2044. {
  2045. uint32_t tmp;
  2046. /* Check the parameters */
  2047. assert_param(IS_RTC_BKP(BackupRegister));
  2048. /* Point on address of first backup register */
  2049. #if defined(TAMP_BKP0R)
  2050. tmp = (uint32_t) & (((TAMP_TypeDef *)((uint32_t)hrtc->Instance + TAMP_OFFSET))->BKP0R);
  2051. #endif /* TAMP_BKP0R */
  2052. #if defined(RTC_BKP0R)
  2053. tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  2054. #endif /* RTC_BKP0R */
  2055. tmp += (BackupRegister * 4U);
  2056. /* Read the specified register */
  2057. return (*(__IO uint32_t *)tmp);
  2058. }
  2059. /**
  2060. * @}
  2061. */
  2062. /** @addtogroup RTCEx_Exported_Functions_Group3
  2063. * @brief Extended Peripheral Control functions
  2064. *
  2065. @verbatim
  2066. ===============================================================================
  2067. ##### Extended Peripheral Control functions #####
  2068. ===============================================================================
  2069. [..]
  2070. This subsection provides functions allowing to
  2071. (+) Write a data in a specified RTC Backup data register
  2072. (+) Read a data in a specified RTC Backup data register
  2073. (+) Set the Smooth calibration parameters.
  2074. (+) Set Low Power calibration parameter (if feature supported).
  2075. (+) Configure the Synchronization Shift Control Settings.
  2076. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  2077. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  2078. (+) Enable the RTC reference clock detection.
  2079. (+) Disable the RTC reference clock detection.
  2080. (+) Enable the Bypass Shadow feature.
  2081. (+) Disable the Bypass Shadow feature.
  2082. @endverbatim
  2083. * @{
  2084. */
  2085. /**
  2086. * @brief Set the Smooth calibration parameters.
  2087. * @param hrtc RTC handle
  2088. * @param SmoothCalibPeriod Select the Smooth Calibration Period.
  2089. * This parameter can be can be one of the following values :
  2090. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  2091. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  2092. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  2093. * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  2094. * This parameter can be one of the following values:
  2095. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  2096. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  2097. * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  2098. * This parameter can be one any value from 0 to 0x000001FF.
  2099. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  2100. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  2101. * SmoothCalibMinusPulsesValue must be equal to 0.
  2102. * @retval HAL status
  2103. */
  2104. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef * hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  2105. {
  2106. uint32_t tickstart;
  2107. /* Check the parameters */
  2108. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  2109. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  2110. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  2111. /* Process Locked */
  2112. __HAL_LOCK(hrtc);
  2113. hrtc->State = HAL_RTC_STATE_BUSY;
  2114. /* Disable the write protection for RTC registers */
  2115. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2116. /* check if a calibration operation is pending */
  2117. #if defined(RTC_ICSR_RECALPF)
  2118. if ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  2119. #endif /* RTC_ICSR_RECALPF */
  2120. #if defined(RTC_ISR_RECALPF)
  2121. if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  2122. #endif /* RTC_ISR_RECALPF */
  2123. {
  2124. tickstart = HAL_GetTick();
  2125. /* Wait for pending calibration operation to finish */
  2126. #if defined(RTC_ICSR_RECALPF)
  2127. while ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  2128. #endif /* RTC_ICSR_RECALPF */
  2129. #if defined(RTC_ISR_RECALPF)
  2130. while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  2131. #endif /* RTC_ISR_RECALPF */
  2132. {
  2133. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  2134. {
  2135. /* Enable the write protection for RTC registers */
  2136. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2137. /* Change RTC state */
  2138. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2139. /* Process Unlocked */
  2140. __HAL_UNLOCK(hrtc);
  2141. return HAL_TIMEOUT;
  2142. }
  2143. }
  2144. }
  2145. /* Configure the Smooth calibration settings */
  2146. MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
  2147. /* Enable the write protection for RTC registers */
  2148. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2149. /* Change RTC state */
  2150. hrtc->State = HAL_RTC_STATE_READY;
  2151. /* Process Unlocked */
  2152. __HAL_UNLOCK(hrtc);
  2153. return HAL_OK;
  2154. }
  2155. /**
  2156. * @brief Configure the Synchronization Shift Control Settings.
  2157. * @note When REFCKON is set, firmware must not write to Shift control register.
  2158. * @param hrtc RTC handle
  2159. * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
  2160. * This parameter can be one of the following values:
  2161. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  2162. * @arg RTC_SHIFTADD1S_RESET: No effect.
  2163. * @param ShiftSubFS Select the number of Second Fractions to substitute.
  2164. * This parameter can be one any value from 0 to 0x7FFF.
  2165. * @retval HAL status
  2166. */
  2167. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef * hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  2168. {
  2169. uint32_t tickstart;
  2170. /* Check the parameters */
  2171. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  2172. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  2173. /* Process Locked */
  2174. __HAL_LOCK(hrtc);
  2175. hrtc->State = HAL_RTC_STATE_BUSY;
  2176. /* Disable the write protection for RTC registers */
  2177. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2178. tickstart = HAL_GetTick();
  2179. /* Wait until the shift is completed */
  2180. #if defined(RTC_ICSR_SHPF)
  2181. while ((hrtc->Instance->ICSR & RTC_ICSR_SHPF) != 0U)
  2182. #endif /* RTC_ICSR_SHPF */
  2183. #if defined(RTC_ISR_SHPF)
  2184. while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
  2185. #endif /* RTC_ISR_SHPF */
  2186. {
  2187. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  2188. {
  2189. /* Enable the write protection for RTC registers */
  2190. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2191. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2192. /* Process Unlocked */
  2193. __HAL_UNLOCK(hrtc);
  2194. return HAL_TIMEOUT;
  2195. }
  2196. }
  2197. /* Check if the reference clock detection is disabled */
  2198. if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
  2199. {
  2200. /* Configure the Shift settings */
  2201. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  2202. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  2203. if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
  2204. {
  2205. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  2206. {
  2207. /* Enable the write protection for RTC registers */
  2208. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2209. hrtc->State = HAL_RTC_STATE_ERROR;
  2210. /* Process Unlocked */
  2211. __HAL_UNLOCK(hrtc);
  2212. return HAL_ERROR;
  2213. }
  2214. }
  2215. }
  2216. else
  2217. {
  2218. /* Enable the write protection for RTC registers */
  2219. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2220. /* Change RTC state */
  2221. hrtc->State = HAL_RTC_STATE_ERROR;
  2222. /* Process Unlocked */
  2223. __HAL_UNLOCK(hrtc);
  2224. return HAL_ERROR;
  2225. }
  2226. /* Enable the write protection for RTC registers */
  2227. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2228. /* Change RTC state */
  2229. hrtc->State = HAL_RTC_STATE_READY;
  2230. /* Process Unlocked */
  2231. __HAL_UNLOCK(hrtc);
  2232. return HAL_OK;
  2233. }
  2234. /**
  2235. * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  2236. * @param hrtc RTC handle
  2237. * @param CalibOutput Select the Calibration output Selection.
  2238. * This parameter can be one of the following values:
  2239. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  2240. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  2241. * @retval HAL status
  2242. */
  2243. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef * hrtc, uint32_t CalibOutput)
  2244. {
  2245. /* Check the parameters */
  2246. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  2247. /* Process Locked */
  2248. __HAL_LOCK(hrtc);
  2249. hrtc->State = HAL_RTC_STATE_BUSY;
  2250. /* Disable the write protection for RTC registers */
  2251. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2252. /* Clear flags before config */
  2253. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  2254. /* Configure the RTC_CR register */
  2255. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  2256. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  2257. /* Enable the write protection for RTC registers */
  2258. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2259. /* Change RTC state */
  2260. hrtc->State = HAL_RTC_STATE_READY;
  2261. /* Process Unlocked */
  2262. __HAL_UNLOCK(hrtc);
  2263. return HAL_OK;
  2264. }
  2265. /**
  2266. * @brief Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  2267. * @param hrtc RTC handle
  2268. * @retval HAL status
  2269. */
  2270. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef * hrtc)
  2271. {
  2272. /* Process Locked */
  2273. __HAL_LOCK(hrtc);
  2274. hrtc->State = HAL_RTC_STATE_BUSY;
  2275. /* Disable the write protection for RTC registers */
  2276. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2277. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  2278. /* Enable the write protection for RTC registers */
  2279. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2280. /* Change RTC state */
  2281. hrtc->State = HAL_RTC_STATE_READY;
  2282. /* Process Unlocked */
  2283. __HAL_UNLOCK(hrtc);
  2284. return HAL_OK;
  2285. }
  2286. /**
  2287. * @brief Enable the RTC reference clock detection.
  2288. * @param hrtc RTC handle
  2289. * @retval HAL status
  2290. */
  2291. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef * hrtc)
  2292. {
  2293. /* Process Locked */
  2294. __HAL_LOCK(hrtc);
  2295. hrtc->State = HAL_RTC_STATE_BUSY;
  2296. /* Disable the write protection for RTC registers */
  2297. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2298. /* Set Initialization mode */
  2299. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  2300. {
  2301. /* Enable the write protection for RTC registers */
  2302. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2303. /* Set RTC state*/
  2304. hrtc->State = HAL_RTC_STATE_ERROR;
  2305. /* Process Unlocked */
  2306. __HAL_UNLOCK(hrtc);
  2307. return HAL_ERROR;
  2308. }
  2309. else
  2310. {
  2311. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  2312. /* Exit Initialization mode */
  2313. #if defined(RTC_ICSR_INIT)
  2314. hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
  2315. #endif /* RTC_ICSR_INIT */
  2316. #if defined(RTC_ISR_INIT)
  2317. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  2318. #endif /* RTC_ISR_INIT */
  2319. }
  2320. /* Enable the write protection for RTC registers */
  2321. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2322. /* Change RTC state */
  2323. hrtc->State = HAL_RTC_STATE_READY;
  2324. /* Process Unlocked */
  2325. __HAL_UNLOCK(hrtc);
  2326. return HAL_OK;
  2327. }
  2328. /**
  2329. * @brief Disable the RTC reference clock detection.
  2330. * @param hrtc RTC handle
  2331. * @retval HAL status
  2332. */
  2333. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef * hrtc)
  2334. {
  2335. /* Process Locked */
  2336. __HAL_LOCK(hrtc);
  2337. hrtc->State = HAL_RTC_STATE_BUSY;
  2338. /* Disable the write protection for RTC registers */
  2339. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2340. /* Set Initialization mode */
  2341. if (RTC_EnterInitMode(hrtc) != HAL_OK)
  2342. {
  2343. /* Enable the write protection for RTC registers */
  2344. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2345. /* Set RTC state*/
  2346. hrtc->State = HAL_RTC_STATE_ERROR;
  2347. /* Process Unlocked */
  2348. __HAL_UNLOCK(hrtc);
  2349. return HAL_ERROR;
  2350. }
  2351. else
  2352. {
  2353. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  2354. /* Exit Initialization mode */
  2355. #if defined(RTC_ICSR_INIT)
  2356. hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
  2357. #endif /* RTC_ICSR_INIT */
  2358. #if defined(RTC_ISR_INIT)
  2359. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  2360. #endif /* RTC_ISR_INIT */
  2361. }
  2362. /* Enable the write protection for RTC registers */
  2363. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2364. /* Change RTC state */
  2365. hrtc->State = HAL_RTC_STATE_READY;
  2366. /* Process Unlocked */
  2367. __HAL_UNLOCK(hrtc);
  2368. return HAL_OK;
  2369. }
  2370. /**
  2371. * @brief Enable the Bypass Shadow feature.
  2372. * @note When the Bypass Shadow is enabled the calendar value are taken
  2373. * directly from the Calendar counter.
  2374. * @param hrtc RTC handle
  2375. * @retval HAL status
  2376. */
  2377. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef * hrtc)
  2378. {
  2379. /* Process Locked */
  2380. __HAL_LOCK(hrtc);
  2381. hrtc->State = HAL_RTC_STATE_BUSY;
  2382. /* Disable the write protection for RTC registers */
  2383. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2384. /* Set the BYPSHAD bit */
  2385. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  2386. /* Enable the write protection for RTC registers */
  2387. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2388. /* Change RTC state */
  2389. hrtc->State = HAL_RTC_STATE_READY;
  2390. /* Process Unlocked */
  2391. __HAL_UNLOCK(hrtc);
  2392. return HAL_OK;
  2393. }
  2394. /**
  2395. * @brief Disable the Bypass Shadow feature.
  2396. * @note When the Bypass Shadow is enabled the calendar value are taken
  2397. * directly from the Calendar counter.
  2398. * @param hrtc RTC handle
  2399. * @retval HAL status
  2400. */
  2401. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef * hrtc)
  2402. {
  2403. /* Process Locked */
  2404. __HAL_LOCK(hrtc);
  2405. hrtc->State = HAL_RTC_STATE_BUSY;
  2406. /* Disable the write protection for RTC registers */
  2407. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  2408. /* Reset the BYPSHAD bit */
  2409. hrtc->Instance->CR &= ((uint8_t)~RTC_CR_BYPSHAD);
  2410. /* Enable the write protection for RTC registers */
  2411. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  2412. /* Change RTC state */
  2413. hrtc->State = HAL_RTC_STATE_READY;
  2414. /* Process Unlocked */
  2415. __HAL_UNLOCK(hrtc);
  2416. return HAL_OK;
  2417. }
  2418. #if defined(TAMP_COUNTR)
  2419. /**
  2420. * @brief Increment Monotonic counter.
  2421. * @param hrtc RTC handle
  2422. * @retval HAL status
  2423. */
  2424. HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterIncrement(RTC_HandleTypeDef *hrtc)
  2425. {
  2426. UNUSED(hrtc);
  2427. /* This register is read-only only and is incremented by one when a write access is done to this
  2428. register. This register cannot roll-over and is frozen when reaching the maximum value. */
  2429. CLEAR_REG(TAMP->COUNTR);
  2430. return HAL_OK;
  2431. }
  2432. /**
  2433. * @brief Monotonic counter incrementation.
  2434. * @param hrtc RTC handle
  2435. * @param Counter monotonic counter value
  2436. * @retval HAL status
  2437. */
  2438. HAL_StatusTypeDef HAL_RTCEx_MonotonicCounterGet(RTC_HandleTypeDef *hrtc, uint32_t *Counter)
  2439. {
  2440. UNUSED(hrtc);
  2441. /* This register is read-only only and is incremented by one when a write access is done to this
  2442. register. This register cannot roll-over and is frozen when reaching the maximum value. */
  2443. *Counter = READ_REG(TAMP->COUNTR);
  2444. return HAL_OK;
  2445. }
  2446. #endif /* TAMP_COUNTR */
  2447. /**
  2448. * @}
  2449. */
  2450. /** @addtogroup RTCEx_Exported_Functions_Group4
  2451. * @brief Extended features functions
  2452. *
  2453. @verbatim
  2454. ===============================================================================
  2455. ##### Extended features functions #####
  2456. ===============================================================================
  2457. [..] This section provides functions allowing to:
  2458. (+) RTC Alarm B callback
  2459. (+) RTC Poll for Alarm B request
  2460. @endverbatim
  2461. * @{
  2462. */
  2463. /**
  2464. * @brief Alarm B callback.
  2465. * @param hrtc RTC handle
  2466. * @retval None
  2467. */
  2468. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef * hrtc)
  2469. {
  2470. /* Prevent unused argument(s) compilation warning */
  2471. UNUSED(hrtc);
  2472. /* NOTE : This function should not be modified, when the callback is needed,
  2473. the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
  2474. */
  2475. }
  2476. /**
  2477. * @brief Handle Alarm B Polling request.
  2478. * @param hrtc RTC handle
  2479. * @param Timeout Timeout duration
  2480. * @retval HAL status
  2481. */
  2482. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef * hrtc, uint32_t Timeout)
  2483. {
  2484. uint32_t tickstart = HAL_GetTick();
  2485. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
  2486. {
  2487. if (Timeout != HAL_MAX_DELAY)
  2488. {
  2489. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2490. {
  2491. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2492. return HAL_TIMEOUT;
  2493. }
  2494. }
  2495. }
  2496. /* Clear the Alarm Flag */
  2497. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  2498. /* Change RTC state */
  2499. hrtc->State = HAL_RTC_STATE_READY;
  2500. return HAL_OK;
  2501. }
  2502. /**
  2503. * @}
  2504. */
  2505. /**
  2506. * @}
  2507. */
  2508. #endif /* HAL_RTC_MODULE_ENABLED */
  2509. /**
  2510. * @}
  2511. */
  2512. /**
  2513. * @}
  2514. */
  2515. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/