stm32f1xx_hal_rtc_ex.c 16 KB

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