2 ******************************************************************************
3 * @file stm32f1xx_hal_rtc_ex.c
4 * @author MCD Application Team
6 * @date 15-December-2014
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
14 ******************************************************************************
17 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
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.
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.
41 ******************************************************************************
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32f1xx_hal.h"
47 /** @addtogroup STM32F1xx_HAL_Driver
51 #ifdef HAL_RTC_MODULE_ENABLED
53 /** @defgroup RTCEx RTCEx
54 * @brief RTC Extended HAL module driver
58 /* Private typedef -----------------------------------------------------------*/
59 /* Private define ------------------------------------------------------------*/
60 /* Private macro -------------------------------------------------------------*/
61 /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Private functions ---------------------------------------------------------*/
72 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
76 /** @defgroup RTCEx_Exported_Functions_Group1 RTC Tamper functions
77 * @brief RTC Tamper functions
80 ===============================================================================
81 ##### RTC Tamper functions #####
82 ===============================================================================
84 [..] This section provides functions allowing to configure Tamper feature
92 * @note By calling this API we disable the tamper interrupt for all tampers.
93 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
94 * the configuration information for RTC.
95 * @param sTamper: Pointer to Tamper Structure.
96 * @note Tamper can be enabled only if ASOE and CCO bit are reset
99 HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
101 /* Check input parameters */
102 if((hrtc == NULL) || (sTamper == NULL))
107 /* Check the parameters */
108 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
109 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
114 hrtc->State = HAL_RTC_STATE_BUSY;
116 if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
118 hrtc->State = HAL_RTC_STATE_ERROR;
120 /* Process Unlocked */
126 MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
128 hrtc->State = HAL_RTC_STATE_READY;
130 /* Process Unlocked */
137 * @brief Sets Tamper with interrupt.
138 * @note By calling this API we force the tamper interrupt for all tampers.
139 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
140 * the configuration information for RTC.
141 * @param sTamper: Pointer to RTC Tamper.
142 * @note Tamper can be enabled only if ASOE and CCO bit are reset
145 HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
147 /* Check input parameters */
148 if((hrtc == NULL) || (sTamper == NULL))
153 /* Check the parameters */
154 assert_param(IS_RTC_TAMPER(sTamper->Tamper));
155 assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
160 hrtc->State = HAL_RTC_STATE_BUSY;
162 if (HAL_IS_BIT_SET(BKP->RTCCR,(BKP_RTCCR_CCO | BKP_RTCCR_ASOE)))
164 hrtc->State = HAL_RTC_STATE_ERROR;
166 /* Process Unlocked */
172 MODIFY_REG(BKP->CR, (BKP_CR_TPE | BKP_CR_TPAL), (sTamper->Tamper | (sTamper->Trigger)));
174 /* Configure the Tamper Interrupt in the BKP->CSR */
175 __HAL_RTC_TAMPER_ENABLE_IT(hrtc, RTC_IT_TAMP1);
177 hrtc->State = HAL_RTC_STATE_READY;
179 /* Process Unlocked */
186 * @brief Deactivates Tamper.
187 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
188 * the configuration information for RTC.
189 * @param Tamper: Selected tamper pin.
190 * This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
193 HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
195 /* Check input parameters */
201 assert_param(IS_RTC_TAMPER(Tamper));
206 hrtc->State = HAL_RTC_STATE_BUSY;
208 /* Disable the selected Tamper pin */
209 CLEAR_BIT(BKP->CR, BKP_CR_TPE);
211 /* Disable the Tamper Interrupt in the BKP->CSR */
212 /* Configure the Tamper Interrupt in the BKP->CSR */
213 __HAL_RTC_TAMPER_DISABLE_IT(hrtc, RTC_IT_TAMP1);
215 /* Clear the Tamper interrupt pending bit */
216 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
217 SET_BIT(BKP->CSR, BKP_CSR_CTE);
219 hrtc->State = HAL_RTC_STATE_READY;
221 /* Process Unlocked */
228 * @brief This function handles Tamper interrupt request.
229 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
230 * the configuration information for RTC.
233 void HAL_RTCEx_TamperIRQHandler(RTC_HandleTypeDef *hrtc)
235 /* Get the status of the Interrupt */
236 if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP1))
238 /* Get the TAMPER Interrupt enable bit and pending bit */
239 if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != (uint32_t)RESET)
241 /* Tamper callback */
242 HAL_RTCEx_Tamper1EventCallback(hrtc);
244 /* Clear the Tamper interrupt pending bit */
245 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
249 /* Change RTC state */
250 hrtc->State = HAL_RTC_STATE_READY;
254 * @brief Tamper 1 callback.
255 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
256 * the configuration information for RTC.
259 __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
261 /* NOTE : This function Should not be modified, when the callback is needed,
262 the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
267 * @brief This function handles Tamper1 Polling.
268 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
269 * the configuration information for RTC.
270 * @param Timeout: Timeout duration
273 HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
275 uint32_t tickstart = HAL_GetTick();
277 /* Check input parameters */
283 /* Get the status of the Interrupt */
284 while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP1F)== RESET)
286 if(Timeout != HAL_MAX_DELAY)
288 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
290 hrtc->State = HAL_RTC_STATE_TIMEOUT;
296 /* Clear the Tamper Flag */
297 __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
299 /* Change RTC state */
300 hrtc->State = HAL_RTC_STATE_READY;
309 /** @defgroup RTCEx_Exported_Functions_Group2 RTC Second functions
310 * @brief RTC Second functions
313 ===============================================================================
314 ##### RTC Second functions #####
315 ===============================================================================
317 [..] This section provides functions implementing second interupt handlers
324 * @brief Sets Interrupt for second
325 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
326 * the configuration information for RTC.
329 HAL_StatusTypeDef HAL_RTCEx_SetSecond_IT(RTC_HandleTypeDef *hrtc)
331 /* Check input parameters */
340 hrtc->State = HAL_RTC_STATE_BUSY;
342 /* Enable Second interuption */
343 __HAL_RTC_SECOND_ENABLE_IT(hrtc, RTC_IT_SEC);
345 hrtc->State = HAL_RTC_STATE_READY;
347 /* Process Unlocked */
354 * @brief Deactivates Second.
355 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
356 * the configuration information for RTC.
359 HAL_StatusTypeDef HAL_RTCEx_DeactivateSecond(RTC_HandleTypeDef *hrtc)
361 /* Check input parameters */
370 hrtc->State = HAL_RTC_STATE_BUSY;
372 /* Deactivate Second interuption*/
373 __HAL_RTC_SECOND_DISABLE_IT(hrtc, RTC_IT_SEC);
375 hrtc->State = HAL_RTC_STATE_READY;
377 /* Process Unlocked */
384 * @brief This function handles second interrupt request.
385 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
386 * the configuration information for RTC.
389 void HAL_RTCEx_RTCIRQHandler(RTC_HandleTypeDef* hrtc)
391 if(__HAL_RTC_SECOND_GET_IT_SOURCE(hrtc, RTC_IT_SEC))
393 /* Get the status of the Interrupt */
394 if(__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_SEC))
396 /* Check if Overrun occurred */
397 if (__HAL_RTC_SECOND_GET_FLAG(hrtc, RTC_FLAG_OW))
399 /* Second error callback */
400 HAL_RTCEx_RTCEventErrorCallback(hrtc);
402 /* Clear flag Second */
403 __HAL_RTC_OVERFLOW_CLEAR_FLAG(hrtc, RTC_FLAG_OW);
405 /* Change RTC state */
406 hrtc->State = HAL_RTC_STATE_ERROR;
410 /* Second callback */
411 HAL_RTCEx_RTCEventCallback(hrtc);
413 /* Change RTC state */
414 hrtc->State = HAL_RTC_STATE_READY;
417 /* Clear flag Second */
418 __HAL_RTC_SECOND_CLEAR_FLAG(hrtc, RTC_FLAG_SEC);
424 * @brief Second event callback.
425 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
426 * the configuration information for RTC.
429 __weak void HAL_RTCEx_RTCEventCallback(RTC_HandleTypeDef *hrtc)
431 /* NOTE : This function Should not be modified, when the callback is needed,
432 the HAL_RTCEx_RTCEventCallback could be implemented in the user file
437 * @brief Second event error callback.
438 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
439 * the configuration information for RTC.
442 __weak void HAL_RTCEx_RTCEventErrorCallback(RTC_HandleTypeDef *hrtc)
444 /* NOTE : This function Should not be modified, when the callback is needed,
445 the HAL_RTCEx_RTCEventErrorCallback could be implemented in the user file
453 /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
454 * @brief Extended Peripheral Control functions
457 ===============================================================================
458 ##### Extension Peripheral Control functions #####
459 ===============================================================================
461 This subsection provides functions allowing to
462 (+) Writes a data in a specified RTC Backup data register
463 (+) Read a data in a specified RTC Backup data register
464 (+) Sets the Smooth calibration parameters.
471 * @brief Writes a data in a specified RTC Backup data register.
472 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
473 * the configuration information for RTC.
474 * @param BackupRegister: RTC Backup data Register number.
475 * This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
476 * specify the register (depending devices).
477 * @param Data: Data to be written in the specified RTC Backup data register.
480 void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
484 /* Check the parameters */
485 assert_param(IS_RTC_BKP(BackupRegister));
487 tmp = (uint32_t)BKP_BASE;
488 tmp += (BackupRegister * 4);
490 *(__IO uint32_t *) tmp = (Data & BKP_DR1_D);
494 * @brief Reads data from the specified RTC Backup data Register.
495 * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
496 * the configuration information for RTC.
497 * @param BackupRegister: RTC Backup data Register number.
498 * This parameter can be: RTC_BKP_DRx where x can be from 1 to 10 (or 42) to
499 * specify the register (depending devices).
502 uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
504 uint32_t backupregister = 0;
507 /* Check the parameters */
508 assert_param(IS_RTC_BKP(BackupRegister));
510 backupregister = (uint32_t)BKP_BASE;
511 backupregister += (BackupRegister * 4);
513 pvalue = (*(__IO uint32_t *)(backupregister)) & BKP_DR1_D;
515 /* Read the specified register */
521 * @brief Sets the Smooth calibration parameters.
522 * @param hrtc: RTC handle
523 * @param SmoothCalibPeriod: Not used (only present for compatibility with another families)
524 * @param SmoothCalibPlusPulses: Not used (only present for compatibility with another families)
525 * @param SmouthCalibMinusPulsesValue: specifies the RTC Clock Calibration value.
526 * This parameter must be a number between 0 and 0x7F.
529 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmouthCalibMinusPulsesValue)
531 /* Check input parameters */
537 /* Check the parameters */
538 assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmouthCalibMinusPulsesValue));
543 hrtc->State = HAL_RTC_STATE_BUSY;
545 /* Sets RTC Clock Calibration value.*/
546 MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, SmouthCalibMinusPulsesValue);
548 /* Change RTC state */
549 hrtc->State = HAL_RTC_STATE_READY;
551 /* Process Unlocked */
569 #endif /* HAL_RTC_MODULE_ENABLED */
575 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/