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