stm32f1xx_hal_rtc_ex.c 17 KB

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