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.
 
 
 

1854 lines
57 KiB

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