stm32f1xx_hal_rtc_ex.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-2016
  7. * @brief Extended 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 Tamper functions
  11. * + Extension Control functions
  12. * + Extension RTC features functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  18. *
  19. * Redistribution and use in source and binary forms, with or without modification,
  20. * are permitted provided that the following conditions are met:
  21. * 1. Redistributions of source code must retain the above copyright notice,
  22. * this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  27. * may be used to endorse or promote products derived from this software
  28. * without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  34. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f1xx_hal.h"
  45. /** @addtogroup STM32F1xx_HAL_Driver
  46. * @{
  47. */
  48. #ifdef HAL_RTC_MODULE_ENABLED
  49. /** @defgroup RTCEx RTCEx
  50. * @brief RTC Extended HAL module driver
  51. * @{
  52. */
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private define ------------------------------------------------------------*/
  55. /* Private macro -------------------------------------------------------------*/
  56. /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
  57. * @{
  58. */
  59. /**
  60. * @}
  61. */
  62. /* Private variables ---------------------------------------------------------*/
  63. /* Private function prototypes -----------------------------------------------*/
  64. /* Private functions ---------------------------------------------------------*/
  65. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  66. * @{
  67. */
  68. /** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
  69. * @brief RTC Tamper functions
  70. *
  71. @verbatim
  72. ===============================================================================
  73. ##### RTC Tamper functions #####
  74. ===============================================================================
  75. [..] This section provides functions allowing to configure Tamper feature
  76. @endverbatim
  77. * @{
  78. */
  79. /**
  80. * @brief Sets Tamper
  81. * @note By calling this API we disable the tamper interrupt for all tampers.
  82. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  83. * the configuration information for RTC.
  84. * @param sTamper: Pointer to Tamper Structure.
  85. * @note Tamper can be enabled only if ASOE and CCO bit are reset
  86. * @retval HAL status
  87. */
  88. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  89. {
  90. /* Check input parameters */
  91. if((hrtc == NULL) || (sTamper == NULL))
  92. {
  93. return HAL_ERROR;
  94. }
  95. /* Check the parameters */
  96. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  97. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  98. /* Process Locked */
  99. __HAL_LOCK(hrtc);
  100. hrtc->State = HAL_RTC_STATE_BUSY;
  101. if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
  102. {
  103. hrtc->State = HAL_RTC_STATE_ERROR;
  104. /* Process Unlocked */
  105. __HAL_UNLOCK(hrtc);
  106. return HAL_ERROR;
  107. }
  108. MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
  109. hrtc->State = HAL_RTC_STATE_READY;
  110. /* Process Unlocked */
  111. __HAL_UNLOCK(hrtc);
  112. return HAL_OK;
  113. }
  114. /**
  115. * @brief Sets Tamper with interrupt.
  116. * @note By calling this API we force the tamper interrupt for all tampers.
  117. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  118. * the configuration information for RTC.
  119. * @param sTamper: Pointer to RTC Tamper.
  120. * @note Tamper can be enabled only if ASOE and CCO bit are reset
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  124. {
  125. /* Check input parameters */
  126. if((hrtc == NULL) || (sTamper == NULL))
  127. {
  128. return HAL_ERROR;
  129. }
  130. /* Check the parameters */
  131. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  132. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  133. /* Process Locked */
  134. __HAL_LOCK(hrtc);
  135. hrtc->State = HAL_RTC_STATE_BUSY;
  136. if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
  137. {
  138. hrtc->State = HAL_RTC_STATE_ERROR;
  139. /* Process Unlocked */
  140. __HAL_UNLOCK(hrtc);
  141. return HAL_ERROR;
  142. }
  143. MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
  144. /* Configure the Tamper Interrupt in the BKP->CSR */
  145. __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
  146. hrtc->State = HAL_RTC_STATE_READY;
  147. /* Process Unlocked */
  148. __HAL_UNLOCK(hrtc);
  149. return HAL_OK;
  150. }
  151. /**
  152. * @brief Deactivates Tamper.
  153. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  154. * the configuration information for RTC.
  155. * @param Tamper: Selected tamper pin.
  156. * This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
  157. * @retval HAL status
  158. */
  159. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  160. {
  161. /* Check input parameters */
  162. if(hrtc == NULL)
  163. {
  164. return HAL_ERROR;
  165. }
  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 == 0)||((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 = 0;
  407. /* Check the parameters */
  408. assert_param(IS_RTC_BKP(BackupRegister));
  409. tmp = (uint32_t)BKP_BASE;
  410. tmp += (BackupRegister * 4);
  411. *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
  412. }
  413. /**
  414. * @brief Reads data from the specified RTC Backup data Register.
  415. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  416. * the configuration information for RTC.
  417. * @param BackupRegister: RTC Backup data Register number.
  418. * This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
  419. * specify the register (depending devices).
  420. * @retval Read value
  421. */
  422. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  423. {
  424. uint32_t backupregister = 0;
  425. uint32_t pvalue = 0;
  426. /* Check the parameters */
  427. assert_param(IS_RTC_BKP(BackupRegister));
  428. backupregister = (uint32_t)BKP_BASE;
  429. backupregister += (BackupRegister * 4);
  430. pvalue = (*(__IO uint32_t *)(backupregister)) & BKP_DR1_D;
  431. /* Read the specified register */
  432. return pvalue;
  433. }
  434. /**
  435. * @brief Sets the Smooth calibration parameters.
  436. * @param hrtc: RTC handle
  437. * @param SmoothCalibPeriod: Not used (only present for compatibility with another families)
  438. * @param SmoothCalibPlusPulses: Not used (only present for compatibility with another families)
  439. * @param SmouthCalibMinusPulsesValue: specifies the RTC Clock Calibration value.
  440. * This parameter must be a number between 0 and 0x7F.
  441. * @retval HAL status
  442. */
  443. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
  444. {
  445. /* Check input parameters */
  446. if(hrtc == NULL)
  447. {
  448. return HAL_ERROR;
  449. }
  450. /* Check the parameters */
  451. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
  452. /* Process Locked */
  453. __HAL_LOCK(hrtc);
  454. hrtc->State = HAL_RTC_STATE_BUSY;
  455. /* Sets RTC Clock Calibration value.*/
  456. MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
  457. /* Change RTC state */
  458. hrtc->State = HAL_RTC_STATE_READY;
  459. /* Process Unlocked */
  460. __HAL_UNLOCK(hrtc);
  461. return HAL_OK;
  462. }
  463. /**
  464. * @}
  465. */
  466. /**
  467. * @}
  468. */
  469. /**
  470. * @}
  471. */
  472. #endif /* HAL_RTC_MODULE_ENABLED */
  473. /**
  474. * @}
  475. */
  476. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/