stm32f1xx_hal_rtc_ex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) Extension peripheral:
  8. * + RTC Tamper functions
  9. * + Extension Control functions
  10. * + Extension RTC features functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  16. * All rights reserved.</center></h2>
  17. *
  18. * This software component is licensed by ST under BSD 3-Clause license,
  19. * the "License"; You may not use this file except in compliance with the
  20. * License. You may obtain a copy of the License at:
  21. * opensource.org/licenses/BSD-3-Clause
  22. *
  23. ******************************************************************************
  24. */
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f1xx_hal.h"
  27. /** @addtogroup STM32F1xx_HAL_Driver
  28. * @{
  29. */
  30. #ifdef HAL_RTC_MODULE_ENABLED
  31. /** @defgroup RTCEx RTCEx
  32. * @brief RTC Extended HAL module driver
  33. * @{
  34. */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
  39. * @{
  40. */
  41. /**
  42. * @}
  43. */
  44. /* Private variables ---------------------------------------------------------*/
  45. /* Private function prototypes -----------------------------------------------*/
  46. /* Private functions ---------------------------------------------------------*/
  47. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  48. * @{
  49. */
  50. /** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
  51. * @brief RTC Tamper functions
  52. *
  53. @verbatim
  54. ===============================================================================
  55. ##### RTC Tamper functions #####
  56. ===============================================================================
  57. [..] This section provides functions allowing to configure Tamper feature
  58. @endverbatim
  59. * @{
  60. */
  61. /**
  62. * @brief Sets Tamper
  63. * @note By calling this API we disable the tamper interrupt for all tampers.
  64. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  65. * the configuration information for RTC.
  66. * @param sTamper: Pointer to Tamper Structure.
  67. * @note Tamper can be enabled only if ASOE and CCO bit are reset
  68. * @retval HAL status
  69. */
  70. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  71. {
  72. /* Check input parameters */
  73. if ((hrtc == NULL) || (sTamper == NULL))
  74. {
  75. return HAL_ERROR;
  76. }
  77. /* Check the parameters */
  78. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  79. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  80. /* Process Locked */
  81. __HAL_LOCK(hrtc);
  82. hrtc->State = HAL_RTC_STATE_BUSY;
  83. if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
  84. {
  85. hrtc->State = HAL_RTC_STATE_ERROR;
  86. /* Process Unlocked */
  87. __HAL_UNLOCK(hrtc);
  88. return HAL_ERROR;
  89. }
  90. MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
  91. hrtc->State = HAL_RTC_STATE_READY;
  92. /* Process Unlocked */
  93. __HAL_UNLOCK(hrtc);
  94. return HAL_OK;
  95. }
  96. /**
  97. * @brief Sets Tamper with interrupt.
  98. * @note By calling this API we force the tamper interrupt for all tampers.
  99. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  100. * the configuration information for RTC.
  101. * @param sTamper: Pointer to RTC Tamper.
  102. * @note Tamper can be enabled only if ASOE and CCO bit are reset
  103. * @retval HAL status
  104. */
  105. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  106. {
  107. /* Check input parameters */
  108. if ((hrtc == NULL) || (sTamper == NULL))
  109. {
  110. return HAL_ERROR;
  111. }
  112. /* Check the parameters */
  113. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  114. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  115. /* Process Locked */
  116. __HAL_LOCK(hrtc);
  117. hrtc->State = HAL_RTC_STATE_BUSY;
  118. if (HAL_IS_BIT_SET(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
  119. {
  120. hrtc->State = HAL_RTC_STATE_ERROR;
  121. /* Process Unlocked */
  122. __HAL_UNLOCK(hrtc);
  123. return HAL_ERROR;
  124. }
  125. MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
  126. /* Configure the Tamper Interrupt in the BKP->CSR */
  127. __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
  128. hrtc->State = HAL_RTC_STATE_READY;
  129. /* Process Unlocked */
  130. __HAL_UNLOCK(hrtc);
  131. return HAL_OK;
  132. }
  133. /**
  134. * @brief Deactivates Tamper.
  135. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  136. * the configuration information for RTC.
  137. * @param Tamper: Selected tamper pin.
  138. * This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
  139. * @retval HAL status
  140. */
  141. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  142. {
  143. /* Check input parameters */
  144. if (hrtc == NULL)
  145. {
  146. return HAL_ERROR;
  147. }
  148. /* Prevent unused argument(s) compilation warning */
  149. UNUSED(Tamper);
  150. assert_param(IS_RTC_TAMPER(Tamper));
  151. /* Process Locked */
  152. __HAL_LOCK(hrtc);
  153. hrtc->State = HAL_RTC_STATE_BUSY;
  154. /* Disable the selected Tamper pin */
  155. CLEAR_BIT(BKP->CR, BKP_CR_TPE);
  156. /* Disable the Tamper Interrupt in the BKP->CSR */
  157. /* Configure the Tamper Interrupt in the BKP->CSR */
  158. __HAL_RTC_TAMPER_DISABLE_IT(hrtc, RTC_IT_TAMP1);
  159. /* Clear the Tamper interrupt pending bit */
  160. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  161. SET_BIT(BKP->CSR, BKP_CSR_CTE);
  162. hrtc->State = HAL_RTC_STATE_READY;
  163. /* Process Unlocked */
  164. __HAL_UNLOCK(hrtc);
  165. return HAL_OK;
  166. }
  167. /**
  168. * @brief This function handles Tamper interrupt request.
  169. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  170. * the configuration information for RTC.
  171. * @retval None
  172. */
  173. void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
  174. {
  175. /* Get the status of the Interrupt */
  176. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP1))
  177. {
  178. /* Get the TAMPER Interrupt enable bit and pending bit */
  179. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
  180. {
  181. /* Tamper callback */
  182. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  183. hrtc->Tamper1EventCallback(hrtc);
  184. #else
  185. HAL_RTCEx_Tamper1EventCallback(hrtc);
  186. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  187. /* Clear the Tamper interrupt pending bit */
  188. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  189. }
  190. }
  191. /* Change RTC state */
  192. hrtc->State = HAL_RTC_STATE_READY;
  193. }
  194. /**
  195. * @brief Tamper 1 callback.
  196. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  197. * the configuration information for RTC.
  198. * @retval None
  199. */
  200. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  201. {
  202. /* Prevent unused argument(s) compilation warning */
  203. UNUSED(hrtc);
  204. /* NOTE : This function Should not be modified, when the callback is needed,
  205. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  206. */
  207. }
  208. /**
  209. * @brief This function handles Tamper1 Polling.
  210. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  211. * the configuration information for RTC.
  212. * @param Timeout: Timeout duration
  213. * @retval HAL status
  214. */
  215. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  216. {
  217. uint32_t tickstart = HAL_GetTick();
  218. /* Check input parameters */
  219. if (hrtc == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Get the status of the Interrupt */
  224. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == RESET)
  225. {
  226. if (Timeout != HAL_MAX_DELAY)
  227. {
  228. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  229. {
  230. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  231. return HAL_TIMEOUT;
  232. }
  233. }
  234. }
  235. /* Clear the Tamper Flag */
  236. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  237. /* Change RTC state */
  238. hrtc->State = HAL_RTC_STATE_READY;
  239. return HAL_OK;
  240. }
  241. /**
  242. * @}
  243. */
  244. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Second functions
  245. * @brief RTC Second functions
  246. *
  247. @verbatim
  248. ===============================================================================
  249. ##### RTC Second functions #####
  250. ===============================================================================
  251. [..] This section provides functions implementing second interupt handlers
  252. @endverbatim
  253. * @{
  254. */
  255. /**
  256. * @brief Sets Interrupt for second
  257. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  258. * the configuration information for RTC.
  259. * @retval HAL status
  260. */
  261. HAL_StatusTypeDef HAL_RTCEx_SetSecond_IT(RTC_HandleTypeDef *hrtc)
  262. {
  263. /* Check input parameters */
  264. if (hrtc == NULL)
  265. {
  266. return HAL_ERROR;
  267. }
  268. /* Process Locked */
  269. __HAL_LOCK(hrtc);
  270. hrtc->State = HAL_RTC_STATE_BUSY;
  271. /* Enable Second interuption */
  272. __HAL_RTC_SECOND_ENABLE_IT(hrtc, RTC_IT_SEC);
  273. hrtc->State = HAL_RTC_STATE_READY;
  274. /* Process Unlocked */
  275. __HAL_UNLOCK(hrtc);
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Deactivates Second.
  280. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  281. * the configuration information for RTC.
  282. * @retval HAL status
  283. */
  284. HAL_StatusTypeDef HAL_RTCEx_DeactivateSecond(RTC_HandleTypeDef *hrtc)
  285. {
  286. /* Check input parameters */
  287. if (hrtc == NULL)
  288. {
  289. return HAL_ERROR;
  290. }
  291. /* Process Locked */
  292. __HAL_LOCK(hrtc);
  293. hrtc->State = HAL_RTC_STATE_BUSY;
  294. /* Deactivate Second interuption*/
  295. __HAL_RTC_SECOND_DISABLE_IT(hrtc, RTC_IT_SEC);
  296. hrtc->State = HAL_RTC_STATE_READY;
  297. /* Process Unlocked */
  298. __HAL_UNLOCK(hrtc);
  299. return HAL_OK;
  300. }
  301. /**
  302. * @brief This function handles second interrupt request.
  303. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  304. * the configuration information for RTC.
  305. * @retval None
  306. */
  307. void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef *hrtc)
  308. {
  309. if (__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
  310. {
  311. /* Get the status of the Interrupt */
  312. if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
  313. {
  314. /* Check if Overrun occurred */
  315. if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
  316. {
  317. /* Second error callback */
  318. HAL_RTCEx_RTCEventErrorCallback(hrtc);
  319. /* Clear flag Second */
  320. __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
  321. /* Change RTC state */
  322. hrtc->State = HAL_RTC_STATE_ERROR;
  323. }
  324. else
  325. {
  326. /* Second callback */
  327. HAL_RTCEx_RTCEventCallback(hrtc);
  328. /* Change RTC state */
  329. hrtc->State = HAL_RTC_STATE_READY;
  330. }
  331. /* Clear flag Second */
  332. __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
  333. }
  334. }
  335. }
  336. /**
  337. * @brief Second event callback.
  338. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  339. * the configuration information for RTC.
  340. * @retval None
  341. */
  342. __weak void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
  343. {
  344. /* Prevent unused argument(s) compilation warning */
  345. UNUSED(hrtc);
  346. /* NOTE : This function Should not be modified, when the callback is needed,
  347. the HAL_RTCEx_RTCEventCallback could be implemented in the user file
  348. */
  349. }
  350. /**
  351. * @brief Second event error callback.
  352. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  353. * the configuration information for RTC.
  354. * @retval None
  355. */
  356. __weak void HAL_RTCEx_RTCEventErrorCallback(RTC_HandleTypeDef *hrtc)
  357. {
  358. /* Prevent unused argument(s) compilation warning */
  359. UNUSED(hrtc);
  360. /* NOTE : This function Should not be modified, when the callback is needed,
  361. the HAL_RTCEx_RTCEventErrorCallback could be implemented in the user file
  362. */
  363. }
  364. /**
  365. * @}
  366. */
  367. /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
  368. * @brief Extended Peripheral Control functions
  369. *
  370. @verbatim
  371. ===============================================================================
  372. ##### Extension Peripheral Control functions #####
  373. ===============================================================================
  374. [..]
  375. This subsection provides functions allowing to
  376. (+) Writes a data in a specified RTC Backup data register
  377. (+) Read a data in a specified RTC Backup data register
  378. (+) Sets the Smooth calibration parameters.
  379. @endverbatim
  380. * @{
  381. */
  382. /**
  383. * @brief Writes a data in a specified RTC Backup data register.
  384. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  385. * the configuration information for RTC.
  386. * @param BackupRegister: RTC Backup data Register number.
  387. * This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
  388. * specify the register (depending devices).
  389. * @param Data: Data to be written in the specified RTC Backup data register.
  390. * @retval None
  391. */
  392. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  393. {
  394. uint32_t tmp = 0U;
  395. /* Prevent unused argument(s) compilation warning */
  396. UNUSED(hrtc);
  397. /* Check the parameters */
  398. assert_param(IS_RTC_BKP(BackupRegister));
  399. tmp = (uint32_t)BKP_BASE;
  400. tmp += (BackupRegister * 4U);
  401. *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
  402. }
  403. /**
  404. * @brief Reads data from the specified RTC Backup data Register.
  405. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  406. * the configuration information for RTC.
  407. * @param BackupRegister: RTC Backup data Register number.
  408. * This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
  409. * specify the register (depending devices).
  410. * @retval Read value
  411. */
  412. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  413. {
  414. uint32_t backupregister = 0U;
  415. uint32_t pvalue = 0U;
  416. /* Prevent unused argument(s) compilation warning */
  417. UNUSED(hrtc);
  418. /* Check the parameters */
  419. assert_param(IS_RTC_BKP(BackupRegister));
  420. backupregister = (uint32_t)BKP_BASE;
  421. backupregister += (BackupRegister * 4U);
  422. pvalue = (*(__IO uint32_t *)(backupregister)) & BKP_DR1_D;
  423. /* Read the specified register */
  424. return pvalue;
  425. }
  426. /**
  427. * @brief Sets the Smooth calibration parameters.
  428. * @param hrtc: RTC handle
  429. * @param SmoothCalibPeriod: Not used (only present for compatibility with another families)
  430. * @param SmoothCalibPlusPulses: Not used (only present for compatibility with another families)
  431. * @param SmouthCalibMinusPulsesValue: specifies the RTC Clock Calibration value.
  432. * This parameter must be a number between 0 and 0x7F.
  433. * @retval HAL status
  434. */
  435. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  436. {
  437. /* Check input parameters */
  438. if (hrtc == NULL)
  439. {
  440. return HAL_ERROR;
  441. }
  442. /* Prevent unused argument(s) compilation warning */
  443. UNUSED(SmoothCalibPeriod);
  444. UNUSED(SmoothCalibPlusPulses);
  445. /* Check the parameters */
  446. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  447. /* Process Locked */
  448. __HAL_LOCK(hrtc);
  449. hrtc->State = HAL_RTC_STATE_BUSY;
  450. /* Sets RTC Clock Calibration value.*/
  451. MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
  452. /* Change RTC state */
  453. hrtc->State = HAL_RTC_STATE_READY;
  454. /* Process Unlocked */
  455. __HAL_UNLOCK(hrtc);
  456. return HAL_OK;
  457. }
  458. /**
  459. * @}
  460. */
  461. /**
  462. * @}
  463. */
  464. /**
  465. * @}
  466. */
  467. #endif /* HAL_RTC_MODULE_ENABLED */
  468. /**
  469. * @}
  470. */
  471. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/