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.
 
 
 

1814 lines
56 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  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) 2015 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. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  207. /* Enable IT timestamp */
  208. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
  209. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  210. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  211. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  212. /* Enable the write protection for RTC registers */
  213. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  214. hrtc->State = HAL_RTC_STATE_READY;
  215. /* Process Unlocked */
  216. __HAL_UNLOCK(hrtc);
  217. return HAL_OK;
  218. }
  219. /**
  220. * @brief Deactivates TimeStamp.
  221. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  222. * the configuration information for RTC.
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  226. {
  227. uint32_t tmpreg = 0;
  228. /* Process Locked */
  229. __HAL_LOCK(hrtc);
  230. hrtc->State = HAL_RTC_STATE_BUSY;
  231. /* Disable the write protection for RTC registers */
  232. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  233. /* In case of interrupt mode is used, the interrupt source must disabled */
  234. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  235. /* Get the RTC_CR register and clear the bits to be configured */
  236. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  237. /* Configure the Time Stamp TSEDGE and Enable bits */
  238. hrtc->Instance->CR = (uint32_t)tmpreg;
  239. /* Enable the write protection for RTC registers */
  240. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  241. hrtc->State = HAL_RTC_STATE_READY;
  242. /* Process Unlocked */
  243. __HAL_UNLOCK(hrtc);
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Sets Internal TimeStamp.
  248. * @note This API must be called before enabling the internal TimeStamp feature.
  249. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  250. * the configuration information for RTC.
  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 Deactivates internal TimeStamp.
  272. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  273. * the configuration information for RTC.
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  277. {
  278. /* Process Locked */
  279. __HAL_LOCK(hrtc);
  280. hrtc->State = HAL_RTC_STATE_BUSY;
  281. /* Disable the write protection for RTC registers */
  282. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  283. /* Configure the internal Time Stamp Enable bits */
  284. __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
  285. /* Enable the write protection for RTC registers */
  286. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  287. hrtc->State = HAL_RTC_STATE_READY;
  288. /* Process Unlocked */
  289. __HAL_UNLOCK(hrtc);
  290. return HAL_OK;
  291. }
  292. /**
  293. * @brief Gets the RTC TimeStamp value.
  294. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  295. * the configuration information for RTC.
  296. * @param sTimeStamp: Pointer to Time structure
  297. * @param sTimeStampDate: Pointer to Date structure
  298. * @param Format: specifies the format of the entered parameters.
  299. * This parameter can be one of the following values:
  300. * FORMAT_BIN: Binary data format
  301. * FORMAT_BCD: BCD data format
  302. * @retval HAL status
  303. */
  304. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
  305. {
  306. uint32_t tmptime = 0, tmpdate = 0;
  307. /* Check the parameters */
  308. assert_param(IS_RTC_FORMAT(Format));
  309. /* Get the TimeStamp time and date registers values */
  310. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  311. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  312. /* Fill the Time structure fields with the read parameters */
  313. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  314. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
  315. sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  316. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
  317. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  318. /* Fill the Date structure fields with the read parameters */
  319. sTimeStampDate->Year = 0;
  320. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  321. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  322. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
  323. /* Check the input parameters format */
  324. if(Format == RTC_FORMAT_BIN)
  325. {
  326. /* Convert the TimeStamp structure parameters to Binary format */
  327. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  328. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  329. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  330. /* Convert the DateTimeStamp structure parameters to Binary format */
  331. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  332. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  333. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  334. }
  335. /* Clear the TIMESTAMP Flag */
  336. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  337. return HAL_OK;
  338. }
  339. /**
  340. * @brief Sets Tamper
  341. * @note By calling this API we disable the tamper interrupt for all tampers.
  342. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  343. * the configuration information for RTC.
  344. * @param sTamper: Pointer to Tamper Structure.
  345. * @retval HAL status
  346. */
  347. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  348. {
  349. uint32_t tmpreg = 0;
  350. /* Check the parameters */
  351. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  352. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  353. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  354. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  355. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  356. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  357. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  358. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  359. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  360. /* Process Locked */
  361. __HAL_LOCK(hrtc);
  362. hrtc->State = HAL_RTC_STATE_BUSY;
  363. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  364. {
  365. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  366. }
  367. if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  368. {
  369. sTamper->NoErase = 0;
  370. if((sTamper->Tamper & RTC_TAMPER_1) != 0)
  371. {
  372. sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
  373. }
  374. if((sTamper->Tamper & RTC_TAMPER_2) != 0)
  375. {
  376. sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
  377. }
  378. if((sTamper->Tamper & RTC_TAMPER_3) != 0)
  379. {
  380. sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
  381. }
  382. }
  383. if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  384. {
  385. sTamper->MaskFlag = 0;
  386. if((sTamper->Tamper & RTC_TAMPER_1) != 0)
  387. {
  388. sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
  389. }
  390. if((sTamper->Tamper & RTC_TAMPER_2) != 0)
  391. {
  392. sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
  393. }
  394. if((sTamper->Tamper & RTC_TAMPER_3) != 0)
  395. {
  396. sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
  397. }
  398. }
  399. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
  400. (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
  401. (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  402. hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
  403. (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
  404. (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
  405. (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
  406. (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
  407. (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);
  408. hrtc->Instance->TAMPCR |= tmpreg;
  409. hrtc->State = HAL_RTC_STATE_READY;
  410. /* Process Unlocked */
  411. __HAL_UNLOCK(hrtc);
  412. return HAL_OK;
  413. }
  414. /**
  415. * @brief Sets Tamper with interrupt.
  416. * @note By calling this API we force the tamper interrupt for all tampers.
  417. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  418. * the configuration information for RTC.
  419. * @param sTamper: Pointer to RTC Tamper.
  420. * @retval HAL status
  421. */
  422. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  423. {
  424. uint32_t tmpreg = 0;
  425. /* Check the parameters */
  426. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  427. assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
  428. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  429. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  430. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  431. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  432. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  433. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  434. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  435. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  436. /* Process Locked */
  437. __HAL_LOCK(hrtc);
  438. hrtc->State = HAL_RTC_STATE_BUSY;
  439. /* Configure the tamper trigger */
  440. if(sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  441. {
  442. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  443. }
  444. if(sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  445. {
  446. sTamper->NoErase = 0;
  447. if((sTamper->Tamper & RTC_TAMPER_1) != 0)
  448. {
  449. sTamper->NoErase |= RTC_TAMPCR_TAMP1NOERASE;
  450. }
  451. if((sTamper->Tamper & RTC_TAMPER_2) != 0)
  452. {
  453. sTamper->NoErase |= RTC_TAMPCR_TAMP2NOERASE;
  454. }
  455. if((sTamper->Tamper & RTC_TAMPER_3) != 0)
  456. {
  457. sTamper->NoErase |= RTC_TAMPCR_TAMP3NOERASE;
  458. }
  459. }
  460. if(sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  461. {
  462. sTamper->MaskFlag = 0;
  463. if((sTamper->Tamper & RTC_TAMPER_1) != 0)
  464. {
  465. sTamper->MaskFlag |= RTC_TAMPCR_TAMP1MF;
  466. }
  467. if((sTamper->Tamper & RTC_TAMPER_2) != 0)
  468. {
  469. sTamper->MaskFlag |= RTC_TAMPCR_TAMP2MF;
  470. }
  471. if((sTamper->Tamper & RTC_TAMPER_3) != 0)
  472. {
  473. sTamper->MaskFlag |= RTC_TAMPCR_TAMP3MF;
  474. }
  475. }
  476. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Interrupt | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->NoErase |\
  477. (uint32_t)sTamper->MaskFlag | (uint32_t)sTamper->Filter | (uint32_t)sTamper->SamplingFrequency |\
  478. (uint32_t)sTamper->PrechargeDuration | (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  479. hrtc->Instance->TAMPCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAMPCR_TAMPTS |\
  480. (uint32_t)RTC_TAMPCR_TAMPFREQ | (uint32_t)RTC_TAMPCR_TAMPFLT | (uint32_t)RTC_TAMPCR_TAMPPRCH |\
  481. (uint32_t)RTC_TAMPCR_TAMPPUDIS | (uint32_t)RTC_TAMPCR_TAMPIE | (uint32_t)RTC_TAMPCR_TAMP1IE |\
  482. (uint32_t)RTC_TAMPCR_TAMP2IE | (uint32_t)RTC_TAMPCR_TAMP3IE | (uint32_t)RTC_TAMPCR_TAMP1NOERASE |\
  483. (uint32_t)RTC_TAMPCR_TAMP2NOERASE | (uint32_t)RTC_TAMPCR_TAMP3NOERASE | (uint32_t)RTC_TAMPCR_TAMP1MF |\
  484. (uint32_t)RTC_TAMPCR_TAMP2MF | (uint32_t)RTC_TAMPCR_TAMP3MF);
  485. hrtc->Instance->TAMPCR |= tmpreg;
  486. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  487. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  488. EXTI->RTSR |= RTC_EXTI_LINE_TAMPER_TIMESTAMP_EVENT;
  489. hrtc->State = HAL_RTC_STATE_READY;
  490. /* Process Unlocked */
  491. __HAL_UNLOCK(hrtc);
  492. return HAL_OK;
  493. }
  494. /**
  495. * @brief Deactivates Tamper.
  496. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  497. * the configuration information for RTC.
  498. * @param Tamper: Selected tamper pin.
  499. * This parameter can be RTC_Tamper_1 and/or RTC_TAMPER_2.
  500. * @retval HAL status
  501. */
  502. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  503. {
  504. assert_param(IS_RTC_TAMPER(Tamper));
  505. /* Process Locked */
  506. __HAL_LOCK(hrtc);
  507. hrtc->State = HAL_RTC_STATE_BUSY;
  508. /* Disable the selected Tamper pin */
  509. hrtc->Instance->TAMPCR &= (uint32_t)~Tamper;
  510. if ((Tamper & RTC_TAMPER_1) != 0)
  511. {
  512. /* Disable the Tamper1 interrupt */
  513. hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1);
  514. }
  515. if ((Tamper & RTC_TAMPER_2) != 0)
  516. {
  517. /* Disable the Tamper2 interrupt */
  518. hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2);
  519. }
  520. if ((Tamper & RTC_TAMPER_3) != 0)
  521. {
  522. /* Disable the Tamper2 interrupt */
  523. hrtc->Instance->TAMPCR &= (uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3);
  524. }
  525. hrtc->State = HAL_RTC_STATE_READY;
  526. /* Process Unlocked */
  527. __HAL_UNLOCK(hrtc);
  528. return HAL_OK;
  529. }
  530. /**
  531. * @brief This function handles TimeStamp interrupt request.
  532. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  533. * the configuration information for RTC.
  534. * @retval None
  535. */
  536. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  537. {
  538. if(__HAL_RTC_TIMESTAMP_GET_IT(hrtc, RTC_IT_TS))
  539. {
  540. /* Get the status of the Interrupt */
  541. if((uint32_t)(hrtc->Instance->CR & RTC_IT_TS) != (uint32_t)RESET)
  542. {
  543. /* TIMESTAMP callback */
  544. HAL_RTCEx_TimeStampEventCallback(hrtc);
  545. /* Clear the TIMESTAMP interrupt pending bit */
  546. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc,RTC_FLAG_TSF);
  547. }
  548. }
  549. /* Get the status of the Interrupt */
  550. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== SET)
  551. {
  552. /* Get the TAMPER Interrupt enable bit and pending bit */
  553. if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
  554. (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP1IE)) != (uint32_t)RESET))
  555. {
  556. /* Tamper callback */
  557. HAL_RTCEx_Tamper1EventCallback(hrtc);
  558. /* Clear the Tamper interrupt pending bit */
  559. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  560. }
  561. }
  562. /* Get the status of the Interrupt */
  563. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F)== SET)
  564. {
  565. /* Get the TAMPER Interrupt enable bit and pending bit */
  566. if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
  567. (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP2IE)) != (uint32_t)RESET))
  568. {
  569. /* Tamper callback */
  570. HAL_RTCEx_Tamper2EventCallback(hrtc);
  571. /* Clear the Tamper interrupt pending bit */
  572. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  573. }
  574. }
  575. /* Get the status of the Interrupt */
  576. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F)== SET)
  577. {
  578. /* Get the TAMPER Interrupt enable bit and pending bit */
  579. if((((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMPIE)) != (uint32_t)RESET) || \
  580. (((hrtc->Instance->TAMPCR & RTC_TAMPCR_TAMP3IE)) != (uint32_t)RESET))
  581. {
  582. /* Tamper callback */
  583. HAL_RTCEx_Tamper3EventCallback(hrtc);
  584. /* Clear the Tamper interrupt pending bit */
  585. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  586. }
  587. }
  588. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  589. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  590. /* Change RTC state */
  591. hrtc->State = HAL_RTC_STATE_READY;
  592. }
  593. /**
  594. * @brief TimeStamp callback.
  595. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  596. * the configuration information for RTC.
  597. * @retval None
  598. */
  599. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  600. {
  601. /* NOTE : This function Should not be modified, when the callback is needed,
  602. the HAL_RTC_TimeStampEventCallback could be implemented in the user file
  603. */
  604. }
  605. /**
  606. * @brief Tamper 1 callback.
  607. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  608. * the configuration information for RTC.
  609. * @retval None
  610. */
  611. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  612. {
  613. /* NOTE : This function Should not be modified, when the callback is needed,
  614. the HAL_RTC_Tamper1EventCallback could be implemented in the user file
  615. */
  616. }
  617. /**
  618. * @brief Tamper 2 callback.
  619. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  620. * the configuration information for RTC.
  621. * @retval None
  622. */
  623. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  624. {
  625. /* NOTE : This function Should not be modified, when the callback is needed,
  626. the HAL_RTC_Tamper2EventCallback could be implemented in the user file
  627. */
  628. }
  629. /**
  630. * @brief Tamper 3 callback.
  631. * @param hrtc: RTC handle
  632. * @retval None
  633. */
  634. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  635. {
  636. /* NOTE : This function Should not be modified, when the callback is needed,
  637. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  638. */
  639. }
  640. /**
  641. * @brief This function handles TimeStamp polling request.
  642. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  643. * the configuration information for RTC.
  644. * @param Timeout: Timeout duration
  645. * @retval HAL status
  646. */
  647. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  648. {
  649. uint32_t tickstart = 0;
  650. /* Get tick */
  651. tickstart = HAL_GetTick();
  652. while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
  653. {
  654. if(Timeout != HAL_MAX_DELAY)
  655. {
  656. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  657. {
  658. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  659. return HAL_TIMEOUT;
  660. }
  661. }
  662. }
  663. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  664. {
  665. /* Clear the TIMESTAMP OverRun Flag */
  666. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  667. /* Change TIMESTAMP state */
  668. hrtc->State = HAL_RTC_STATE_ERROR;
  669. return HAL_ERROR;
  670. }
  671. /* Change RTC state */
  672. hrtc->State = HAL_RTC_STATE_READY;
  673. return HAL_OK;
  674. }
  675. /**
  676. * @brief This function handles Tamper1 Polling.
  677. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  678. * the configuration information for RTC.
  679. * @param Timeout: Timeout duration
  680. * @retval HAL status
  681. */
  682. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  683. {
  684. uint32_t tickstart = 0;
  685. /* Get tick */
  686. tickstart = HAL_GetTick();
  687. /* Get the status of the Interrupt */
  688. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F)== RESET)
  689. {
  690. if(Timeout != HAL_MAX_DELAY)
  691. {
  692. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  693. {
  694. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  695. return HAL_TIMEOUT;
  696. }
  697. }
  698. }
  699. /* Clear the Tamper Flag */
  700. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  701. /* Change RTC state */
  702. hrtc->State = HAL_RTC_STATE_READY;
  703. return HAL_OK;
  704. }
  705. /**
  706. * @brief This function handles Tamper2 Polling.
  707. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  708. * the configuration information for RTC.
  709. * @param Timeout: Timeout duration
  710. * @retval HAL status
  711. */
  712. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  713. {
  714. uint32_t tickstart = 0;
  715. /* Get tick */
  716. tickstart = HAL_GetTick();
  717. /* Get the status of the Interrupt */
  718. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == RESET)
  719. {
  720. if(Timeout != HAL_MAX_DELAY)
  721. {
  722. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  723. {
  724. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  725. return HAL_TIMEOUT;
  726. }
  727. }
  728. }
  729. /* Clear the Tamper Flag */
  730. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
  731. /* Change RTC state */
  732. hrtc->State = HAL_RTC_STATE_READY;
  733. return HAL_OK;
  734. }
  735. /**
  736. * @brief This function handles Tamper3 Polling.
  737. * @param hrtc: RTC handle
  738. * @param Timeout: Timeout duration
  739. * @retval HAL status
  740. */
  741. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  742. {
  743. uint32_t tickstart = HAL_GetTick();
  744. /* Get the status of the Interrupt */
  745. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == RESET)
  746. {
  747. if(Timeout != HAL_MAX_DELAY)
  748. {
  749. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  750. {
  751. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  752. return HAL_TIMEOUT;
  753. }
  754. }
  755. }
  756. /* Clear the Tamper Flag */
  757. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
  758. /* Change RTC state */
  759. hrtc->State = HAL_RTC_STATE_READY;
  760. return HAL_OK;
  761. }
  762. /**
  763. * @}
  764. */
  765. /** @defgroup RTCEx_Group2 RTC Wake-up functions
  766. * @brief RTC Wake-up functions
  767. *
  768. @verbatim
  769. ===============================================================================
  770. ##### RTC Wake-up functions #####
  771. ===============================================================================
  772. [..] This section provides functions allowing to configure Wake-up feature
  773. @endverbatim
  774. * @{
  775. */
  776. /**
  777. * @brief Sets wake up timer.
  778. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  779. * the configuration information for RTC.
  780. * @param WakeUpCounter: Wake up counter
  781. * @param WakeUpClock: Wake up clock
  782. * @retval HAL status
  783. */
  784. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  785. {
  786. uint32_t tickstart = 0;
  787. /* Check the parameters */
  788. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  789. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  790. /* Process Locked */
  791. __HAL_LOCK(hrtc);
  792. hrtc->State = HAL_RTC_STATE_BUSY;
  793. /* Disable the write protection for RTC registers */
  794. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  795. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  796. /* Get tick */
  797. tickstart = HAL_GetTick();
  798. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  799. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  800. {
  801. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  802. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  803. {
  804. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  805. {
  806. /* Enable the write protection for RTC registers */
  807. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  808. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  809. /* Process Unlocked */
  810. __HAL_UNLOCK(hrtc);
  811. return HAL_TIMEOUT;
  812. }
  813. }
  814. }
  815. /* Clear the Wakeup Timer clock source bits in CR register */
  816. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  817. /* Configure the clock source */
  818. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  819. /* Configure the Wakeup Timer counter */
  820. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  821. /* Enable the Wakeup Timer */
  822. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  823. /* Enable the write protection for RTC registers */
  824. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  825. hrtc->State = HAL_RTC_STATE_READY;
  826. /* Process Unlocked */
  827. __HAL_UNLOCK(hrtc);
  828. return HAL_OK;
  829. }
  830. /**
  831. * @brief Sets wake up timer with interrupt
  832. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  833. * the configuration information for RTC.
  834. * @param WakeUpCounter: Wake up counter
  835. * @param WakeUpClock: Wake up clock
  836. * @retval HAL status
  837. */
  838. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  839. {
  840. uint32_t tickstart = 0;
  841. /* Check the parameters */
  842. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  843. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  844. /* Process Locked */
  845. __HAL_LOCK(hrtc);
  846. hrtc->State = HAL_RTC_STATE_BUSY;
  847. /* Disable the write protection for RTC registers */
  848. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  849. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  850. /* Get tick */
  851. tickstart = HAL_GetTick();
  852. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  853. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
  854. {
  855. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  856. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  857. {
  858. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  859. {
  860. /* Enable the write protection for RTC registers */
  861. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  862. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(hrtc);
  865. return HAL_TIMEOUT;
  866. }
  867. }
  868. }
  869. /* Configure the Wakeup Timer counter */
  870. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  871. /* Clear the Wakeup Timer clock source bits in CR register */
  872. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  873. /* Configure the clock source */
  874. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  875. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  876. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  877. EXTI->RTSR |= RTC_EXTI_LINE_WAKEUPTIMER_EVENT;
  878. /* Configure the Interrupt in the RTC_CR register */
  879. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
  880. /* Enable the Wakeup Timer */
  881. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  882. /* Enable the write protection for RTC registers */
  883. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  884. hrtc->State = HAL_RTC_STATE_READY;
  885. /* Process Unlocked */
  886. __HAL_UNLOCK(hrtc);
  887. return HAL_OK;
  888. }
  889. /**
  890. * @brief Deactivates wake up timer counter.
  891. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  892. * the configuration information for RTC.
  893. * @retval HAL status
  894. */
  895. uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  896. {
  897. uint32_t tickstart = 0;
  898. /* Process Locked */
  899. __HAL_LOCK(hrtc);
  900. hrtc->State = HAL_RTC_STATE_BUSY;
  901. /* Disable the write protection for RTC registers */
  902. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  903. /* Disable the Wakeup Timer */
  904. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  905. /* In case of interrupt mode is used, the interrupt source must disabled */
  906. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
  907. /* Get tick */
  908. tickstart = HAL_GetTick();
  909. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  910. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  911. {
  912. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  913. {
  914. /* Enable the write protection for RTC registers */
  915. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  916. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  917. /* Process Unlocked */
  918. __HAL_UNLOCK(hrtc);
  919. return HAL_TIMEOUT;
  920. }
  921. }
  922. /* Enable the write protection for RTC registers */
  923. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  924. hrtc->State = HAL_RTC_STATE_READY;
  925. /* Process Unlocked */
  926. __HAL_UNLOCK(hrtc);
  927. return HAL_OK;
  928. }
  929. /**
  930. * @brief Gets wake up timer counter.
  931. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  932. * the configuration information for RTC.
  933. * @retval Counter value
  934. */
  935. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  936. {
  937. /* Get the counter value */
  938. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  939. }
  940. /**
  941. * @brief This function handles Wake Up Timer interrupt request.
  942. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  943. * the configuration information for RTC.
  944. * @retval None
  945. */
  946. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  947. {
  948. if(__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc, RTC_IT_WUT))
  949. {
  950. /* Get the status of the Interrupt */
  951. if((uint32_t)(hrtc->Instance->CR & RTC_IT_WUT) != (uint32_t)RESET)
  952. {
  953. /* WAKEUPTIMER callback */
  954. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  955. /* Clear the WAKEUPTIMER interrupt pending bit */
  956. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  957. }
  958. }
  959. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  960. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  961. /* Change RTC state */
  962. hrtc->State = HAL_RTC_STATE_READY;
  963. }
  964. /**
  965. * @brief Wake Up Timer callback.
  966. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  967. * the configuration information for RTC.
  968. * @retval None
  969. */
  970. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  971. {
  972. /* NOTE : This function Should not be modified, when the callback is needed,
  973. the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
  974. */
  975. }
  976. /**
  977. * @brief This function handles Wake Up Timer Polling.
  978. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  979. * the configuration information for RTC.
  980. * @param Timeout: Timeout duration
  981. * @retval HAL status
  982. */
  983. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  984. {
  985. uint32_t tickstart = 0;
  986. /* Get tick */
  987. tickstart = HAL_GetTick();
  988. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  989. {
  990. if(Timeout != HAL_MAX_DELAY)
  991. {
  992. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  993. {
  994. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  995. return HAL_TIMEOUT;
  996. }
  997. }
  998. }
  999. /* Clear the WAKEUPTIMER Flag */
  1000. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1001. /* Change RTC state */
  1002. hrtc->State = HAL_RTC_STATE_READY;
  1003. return HAL_OK;
  1004. }
  1005. /**
  1006. * @}
  1007. */
  1008. /** @defgroup RTCEx_Group3 Extension Peripheral Control functions
  1009. * @brief Extension Peripheral Control functions
  1010. *
  1011. @verbatim
  1012. ===============================================================================
  1013. ##### Extension Peripheral Control functions #####
  1014. ===============================================================================
  1015. [..]
  1016. This subsection provides functions allowing to
  1017. (+) Write a data in a specified RTC Backup data register
  1018. (+) Read a data in a specified RTC Backup data register
  1019. (+) Set the Coarse calibration parameters.
  1020. (+) Deactivate the Coarse calibration parameters
  1021. (+) Set the Smooth calibration parameters.
  1022. (+) Configure the Synchronization Shift Control Settings.
  1023. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1024. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1025. (+) Enable the RTC reference clock detection.
  1026. (+) Disable the RTC reference clock detection.
  1027. (+) Enable the Bypass Shadow feature.
  1028. (+) Disable the Bypass Shadow feature.
  1029. @endverbatim
  1030. * @{
  1031. */
  1032. /**
  1033. * @brief Writes a data in a specified RTC Backup data register.
  1034. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1035. * the configuration information for RTC.
  1036. * @param BackupRegister: RTC Backup data Register number.
  1037. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1038. * specify the register.
  1039. * @param Data: Data to be written in the specified RTC Backup data register.
  1040. * @retval None
  1041. */
  1042. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1043. {
  1044. uint32_t tmp = 0;
  1045. /* Check the parameters */
  1046. assert_param(IS_RTC_BKP(BackupRegister));
  1047. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  1048. tmp += (BackupRegister * 4);
  1049. /* Write the specified register */
  1050. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1051. }
  1052. /**
  1053. * @brief Reads data from the specified RTC Backup data Register.
  1054. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1055. * the configuration information for RTC.
  1056. * @param BackupRegister: RTC Backup data Register number.
  1057. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1058. * specify the register.
  1059. * @retval Read value
  1060. */
  1061. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1062. {
  1063. uint32_t tmp = 0;
  1064. /* Check the parameters */
  1065. assert_param(IS_RTC_BKP(BackupRegister));
  1066. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  1067. tmp += (BackupRegister * 4);
  1068. /* Read the specified register */
  1069. return (*(__IO uint32_t *)tmp);
  1070. }
  1071. /**
  1072. * @brief Sets the Smooth calibration parameters.
  1073. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1074. * the configuration information for RTC.
  1075. * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
  1076. * This parameter can be can be one of the following values :
  1077. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  1078. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  1079. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  1080. * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
  1081. * This parameter can be one of the following values:
  1082. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulses every 2*11 pulses.
  1083. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1084. * @param SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  1085. * This parameter can be one any value from 0 to 0x000001FF.
  1086. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1087. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1088. * SmouthCalibMinusPulsesValue must be equal to 0.
  1089. * @retval HAL status
  1090. */
  1091. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  1092. {
  1093. uint32_t tickstart = 0;
  1094. /* Check the parameters */
  1095. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1096. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1097. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  1098. /* Process Locked */
  1099. __HAL_LOCK(hrtc);
  1100. hrtc->State = HAL_RTC_STATE_BUSY;
  1101. /* Disable the write protection for RTC registers */
  1102. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1103. /* check if a calibration is pending*/
  1104. if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1105. {
  1106. /* Get tick */
  1107. tickstart = HAL_GetTick();
  1108. /* check if a calibration is pending*/
  1109. while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1110. {
  1111. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1112. {
  1113. /* Enable the write protection for RTC registers */
  1114. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1115. /* Change RTC state */
  1116. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1117. /* Process Unlocked */
  1118. __HAL_UNLOCK(hrtc);
  1119. return HAL_TIMEOUT;
  1120. }
  1121. }
  1122. }
  1123. /* Configure the Smooth calibration settings */
  1124. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmouthCalibMinusPulsesValue);
  1125. /* Enable the write protection for RTC registers */
  1126. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1127. /* Change RTC state */
  1128. hrtc->State = HAL_RTC_STATE_READY;
  1129. /* Process Unlocked */
  1130. __HAL_UNLOCK(hrtc);
  1131. return HAL_OK;
  1132. }
  1133. /**
  1134. * @brief Configures the Synchronization Shift Control Settings.
  1135. * @note When REFCKON is set, firmware must not write to Shift control register.
  1136. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1137. * the configuration information for RTC.
  1138. * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
  1139. * This parameter can be one of the following values :
  1140. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1141. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1142. * @param ShiftSubFS: Select the number of Second Fractions to substitute.
  1143. * This parameter can be one any value from 0 to 0x7FFF.
  1144. * @retval HAL status
  1145. */
  1146. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1147. {
  1148. uint32_t tickstart = 0;
  1149. /* Check the parameters */
  1150. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1151. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1152. /* Process Locked */
  1153. __HAL_LOCK(hrtc);
  1154. hrtc->State = HAL_RTC_STATE_BUSY;
  1155. /* Disable the write protection for RTC registers */
  1156. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1157. /* Get tick */
  1158. tickstart = HAL_GetTick();
  1159. /* Wait until the shift is completed*/
  1160. while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
  1161. {
  1162. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1163. {
  1164. /* Enable the write protection for RTC registers */
  1165. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1166. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1167. /* Process Unlocked */
  1168. __HAL_UNLOCK(hrtc);
  1169. return HAL_TIMEOUT;
  1170. }
  1171. }
  1172. /* Check if the reference clock detection is disabled */
  1173. if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
  1174. {
  1175. /* Configure the Shift settings */
  1176. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1177. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1178. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  1179. {
  1180. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1181. {
  1182. /* Enable the write protection for RTC registers */
  1183. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1184. hrtc->State = HAL_RTC_STATE_ERROR;
  1185. /* Process Unlocked */
  1186. __HAL_UNLOCK(hrtc);
  1187. return HAL_ERROR;
  1188. }
  1189. }
  1190. }
  1191. else
  1192. {
  1193. /* Enable the write protection for RTC registers */
  1194. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1195. /* Change RTC state */
  1196. hrtc->State = HAL_RTC_STATE_ERROR;
  1197. /* Process Unlocked */
  1198. __HAL_UNLOCK(hrtc);
  1199. return HAL_ERROR;
  1200. }
  1201. /* Enable the write protection for RTC registers */
  1202. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1203. /* Change RTC state */
  1204. hrtc->State = HAL_RTC_STATE_READY;
  1205. /* Process Unlocked */
  1206. __HAL_UNLOCK(hrtc);
  1207. return HAL_OK;
  1208. }
  1209. /**
  1210. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1211. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1212. * the configuration information for RTC.
  1213. * @param CalibOutput: Select the Calibration output Selection .
  1214. * This parameter can be one of the following values:
  1215. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1216. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1217. * @retval HAL status
  1218. */
  1219. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
  1220. {
  1221. /* Check the parameters */
  1222. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1223. /* Process Locked */
  1224. __HAL_LOCK(hrtc);
  1225. hrtc->State = HAL_RTC_STATE_BUSY;
  1226. /* Disable the write protection for RTC registers */
  1227. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1228. /* Clear flags before config */
  1229. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1230. /* Configure the RTC_CR register */
  1231. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1232. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1233. /* Enable the write protection for RTC registers */
  1234. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1235. /* Change RTC state */
  1236. hrtc->State = HAL_RTC_STATE_READY;
  1237. /* Process Unlocked */
  1238. __HAL_UNLOCK(hrtc);
  1239. return HAL_OK;
  1240. }
  1241. /**
  1242. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1243. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1244. * the configuration information for RTC.
  1245. * @retval HAL status
  1246. */
  1247. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1248. {
  1249. /* Process Locked */
  1250. __HAL_LOCK(hrtc);
  1251. hrtc->State = HAL_RTC_STATE_BUSY;
  1252. /* Disable the write protection for RTC registers */
  1253. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1254. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1255. /* Enable the write protection for RTC registers */
  1256. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1257. /* Change RTC state */
  1258. hrtc->State = HAL_RTC_STATE_READY;
  1259. /* Process Unlocked */
  1260. __HAL_UNLOCK(hrtc);
  1261. return HAL_OK;
  1262. }
  1263. /**
  1264. * @brief Enables the RTC reference clock detection.
  1265. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1266. * the configuration information for RTC.
  1267. * @retval HAL status
  1268. */
  1269. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
  1270. {
  1271. /* Process Locked */
  1272. __HAL_LOCK(hrtc);
  1273. hrtc->State = HAL_RTC_STATE_BUSY;
  1274. /* Disable the write protection for RTC registers */
  1275. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1276. /* Set Initialization mode */
  1277. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1278. {
  1279. /* Enable the write protection for RTC registers */
  1280. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1281. /* Set RTC state*/
  1282. hrtc->State = HAL_RTC_STATE_ERROR;
  1283. /* Process Unlocked */
  1284. __HAL_UNLOCK(hrtc);
  1285. return HAL_ERROR;
  1286. }
  1287. else
  1288. {
  1289. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1290. /* Exit Initialization mode */
  1291. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1292. }
  1293. /* Enable the write protection for RTC registers */
  1294. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1295. /* Change RTC state */
  1296. hrtc->State = HAL_RTC_STATE_READY;
  1297. /* Process Unlocked */
  1298. __HAL_UNLOCK(hrtc);
  1299. return HAL_OK;
  1300. }
  1301. /**
  1302. * @brief Disable the RTC reference clock detection.
  1303. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1304. * the configuration information for RTC.
  1305. * @retval HAL status
  1306. */
  1307. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
  1308. {
  1309. /* Process Locked */
  1310. __HAL_LOCK(hrtc);
  1311. hrtc->State = HAL_RTC_STATE_BUSY;
  1312. /* Disable the write protection for RTC registers */
  1313. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1314. /* Set Initialization mode */
  1315. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1316. {
  1317. /* Enable the write protection for RTC registers */
  1318. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1319. /* Set RTC state*/
  1320. hrtc->State = HAL_RTC_STATE_ERROR;
  1321. /* Process Unlocked */
  1322. __HAL_UNLOCK(hrtc);
  1323. return HAL_ERROR;
  1324. }
  1325. else
  1326. {
  1327. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1328. /* Exit Initialization mode */
  1329. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1330. }
  1331. /* Enable the write protection for RTC registers */
  1332. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1333. /* Change RTC state */
  1334. hrtc->State = HAL_RTC_STATE_READY;
  1335. /* Process Unlocked */
  1336. __HAL_UNLOCK(hrtc);
  1337. return HAL_OK;
  1338. }
  1339. /**
  1340. * @brief Enables the Bypass Shadow feature.
  1341. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1342. * the configuration information for RTC.
  1343. * @note When the Bypass Shadow is enabled the calendar value are taken
  1344. * directly from the Calendar counter.
  1345. * @retval HAL status
  1346. */
  1347. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
  1348. {
  1349. /* Process Locked */
  1350. __HAL_LOCK(hrtc);
  1351. hrtc->State = HAL_RTC_STATE_BUSY;
  1352. /* Disable the write protection for RTC registers */
  1353. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1354. /* Set the BYPSHAD bit */
  1355. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1356. /* Enable the write protection for RTC registers */
  1357. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1358. /* Change RTC state */
  1359. hrtc->State = HAL_RTC_STATE_READY;
  1360. /* Process Unlocked */
  1361. __HAL_UNLOCK(hrtc);
  1362. return HAL_OK;
  1363. }
  1364. /**
  1365. * @brief Disables the Bypass Shadow feature.
  1366. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1367. * the configuration information for RTC.
  1368. * @note When the Bypass Shadow is enabled the calendar value are taken
  1369. * directly from the Calendar counter.
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
  1373. {
  1374. /* Process Locked */
  1375. __HAL_LOCK(hrtc);
  1376. hrtc->State = HAL_RTC_STATE_BUSY;
  1377. /* Disable the write protection for RTC registers */
  1378. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1379. /* Reset the BYPSHAD bit */
  1380. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1381. /* Enable the write protection for RTC registers */
  1382. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1383. /* Change RTC state */
  1384. hrtc->State = HAL_RTC_STATE_READY;
  1385. /* Process Unlocked */
  1386. __HAL_UNLOCK(hrtc);
  1387. return HAL_OK;
  1388. }
  1389. /**
  1390. * @}
  1391. */
  1392. /** @defgroup RTCEx_Group4 Extended features functions
  1393. * @brief Extended features functions
  1394. *
  1395. @verbatim
  1396. ===============================================================================
  1397. ##### Extended features functions #####
  1398. ===============================================================================
  1399. [..] This section provides functions allowing to:
  1400. (+) RTC Alram B callback
  1401. (+) RTC Poll for Alarm B request
  1402. @endverbatim
  1403. * @{
  1404. */
  1405. /**
  1406. * @brief Alarm B callback.
  1407. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1408. * the configuration information for RTC.
  1409. * @retval None
  1410. */
  1411. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1412. {
  1413. /* NOTE : This function Should not be modified, when the callback is needed,
  1414. the HAL_RTC_AlarmBEventCallback could be implemented in the user file
  1415. */
  1416. }
  1417. /**
  1418. * @brief This function handles AlarmB Polling request.
  1419. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1420. * the configuration information for RTC.
  1421. * @param Timeout: Timeout duration
  1422. * @retval HAL status
  1423. */
  1424. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1425. {
  1426. uint32_t tickstart = 0;
  1427. /* Get tick */
  1428. tickstart = HAL_GetTick();
  1429. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  1430. {
  1431. if(Timeout != HAL_MAX_DELAY)
  1432. {
  1433. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1434. {
  1435. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1436. return HAL_TIMEOUT;
  1437. }
  1438. }
  1439. }
  1440. /* Clear the Alarm Flag */
  1441. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1442. /* Change RTC state */
  1443. hrtc->State = HAL_RTC_STATE_READY;
  1444. return HAL_OK;
  1445. }
  1446. /**
  1447. * @}
  1448. */
  1449. /**
  1450. * @}
  1451. */
  1452. #endif /* HAL_RTC_MODULE_ENABLED */
  1453. /**
  1454. * @}
  1455. */
  1456. /**
  1457. * @}
  1458. */
  1459. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/