1 /**
2 ******************************************************************************
3 * @file stm32f0xx_hal_rtc.c
4 * @author MCD Application Team
5 * @brief RTC HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Real Time Clock (RTC) peripheral:
8 * + Initialization and de-initialization functions
9 * + RTC Time and Date functions
10 * + RTC Alarm functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
13 *
14 @verbatim
15 ==============================================================================
16 ##### How to use RTC Driver #####
17 ===================================================================
18 [..]
19 (+) Enable the RTC domain access (see description in the section above).
20 (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
21 format using the HAL_RTC_Init() function.
22
23 *** Time and Date configuration ***
24 ===================================
25 [..]
26 (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
27 and HAL_RTC_SetDate() functions.
28 (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
29
30 *** Alarm configuration ***
31 ===========================
32 [..]
33 (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
34 You can also configure the RTC Alarm with interrupt mode using the
35 HAL_RTC_SetAlarm_IT() function.
36 (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
37
38 ##### RTC and low power modes #####
39 ===================================================================
40 [..] The MCU can be woken up from a low power mode by an RTC alternate
41 function.
42 [..] The RTC alternate functions are the RTC alarm (Alarm A),
43 RTC wake-up, RTC tamper event detection and RTC time stamp event detection.
44 These RTC alternate functions can wake up the system from the Stop and
45 Standby low power modes.
46 [..] The system can also wake up from low power modes without depending
47 on an external interrupt (Auto-wake-up mode), by using the RTC alarm
48 or the RTC wake-up events.
49 [..] The RTC provides a programmable time base for waking up from the
50 Stop or Standby mode at regular intervals.
51 Wake-up from STOP and STANDBY modes is possible only when the RTC clock source
52 is LSE or LSI.
53
54 @endverbatim
55 ******************************************************************************
56 * @attention
57 *
58 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
59 *
60 * Redistribution and use in source and binary forms, with or without modification,
61 * are permitted provided that the following conditions are met:
62 * 1. Redistributions of source code must retain the above copyright notice,
63 * this list of conditions and the following disclaimer.
64 * 2. Redistributions in binary form must reproduce the above copyright notice,
65 * this list of conditions and the following disclaimer in the documentation
66 * and/or other materials provided with the distribution.
67 * 3. Neither the name of STMicroelectronics nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
70 *
71 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
72 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
78 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
79 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81 *
82 ******************************************************************************
83 */
84
85 /* Includes ------------------------------------------------------------------*/
86 #include "stm32f0xx_hal.h"
87
88 /** @addtogroup STM32F0xx_HAL_Driver
89 * @{
90 */
91
92 /** @addtogroup RTC
93 * @brief RTC HAL module driver
94 * @{
95 */
96
97 #ifdef HAL_RTC_MODULE_ENABLED
98
99 /* Private typedef -----------------------------------------------------------*/
100 /* Private define ------------------------------------------------------------*/
101 /* Private macro -------------------------------------------------------------*/
102 /* Private variables ---------------------------------------------------------*/
103 /* Private function prototypes -----------------------------------------------*/
104 /* Exported functions ---------------------------------------------------------*/
105
106 /** @addtogroup RTC_Exported_Functions
107 * @{
108 */
109
110 /** @addtogroup RTC_Exported_Functions_Group1
111 * @brief Initialization and Configuration functions
112 *
113 @verbatim
114 ===============================================================================
115 ##### Initialization and de-initialization functions #####
116 ===============================================================================
117 [..] This section provides functions allowing to initialize and configure the
118 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
119 RTC registers Write protection, enter and exit the RTC initialization mode,
120 RTC registers synchronization check and reference clock detection enable.
121 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
122 It is split into 2 programmable prescalers to minimize power consumption.
123 (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
124 (++) When both prescalers are used, it is recommended to configure the
125 asynchronous prescaler to a high value to minimize power consumption.
126 (#) All RTC registers are Write protected. Writing to the RTC registers
127 is enabled by writing a key into the Write Protection register, RTC_WPR.
128 (#) To configure the RTC Calendar, user application should enter
129 initialization mode. In this mode, the calendar counter is stopped
130 and its value can be updated. When the initialization sequence is
131 complete, the calendar restarts counting after 4 RTCCLK cycles.
132 (#) To read the calendar through the shadow registers after Calendar
133 initialization, calendar update or after wake-up from low power modes
134 the software must first clear the RSF flag. The software must then
135 wait until it is set again before reading the calendar, which means
136 that the calendar registers have been correctly copied into the
137 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
138 implements the above software sequence (RSF clear and RSF check).
139
140 @endverbatim
141 * @{
142 */
143
144 /**
145 * @brief Initialize the RTC according to the specified parameters
146 * in the RTC_InitTypeDef structure and initialize the associated handle.
147 * @param hrtc RTC handle
148 * @retval HAL status
149 */
HAL_RTC_Init(RTC_HandleTypeDef * hrtc)150 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
151 {
152 /* Check the RTC peripheral state */
153 if(hrtc == NULL)
154 {
155 return HAL_ERROR;
156 }
157
158 /* Check the parameters */
159 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
160 assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
161 assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
162 assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
163 assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
164 assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
165 assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
166
167 if(hrtc->State == HAL_RTC_STATE_RESET)
168 {
169 /* Allocate lock resource and initialize it */
170 hrtc->Lock = HAL_UNLOCKED;
171
172 /* Initialize RTC MSP */
173 HAL_RTC_MspInit(hrtc);
174 }
175
176 /* Set RTC state */
177 hrtc->State = HAL_RTC_STATE_BUSY;
178
179 /* Disable the write protection for RTC registers */
180 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
181
182 /* Set Initialization mode */
183 if(RTC_EnterInitMode(hrtc) != HAL_OK)
184 {
185 /* Enable the write protection for RTC registers */
186 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
187
188 /* Set RTC state */
189 hrtc->State = HAL_RTC_STATE_ERROR;
190
191 return HAL_ERROR;
192 }
193 else
194 {
195 /* Clear RTC_CR FMT, OSEL and POL Bits */
196 hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
197 /* Set RTC_CR register */
198 hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
199
200 /* Configure the RTC PRER */
201 hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
202 hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16U);
203
204 /* Exit Initialization mode */
205 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
206
207 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
208 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
209 {
210 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
211 {
212 /* Enable the write protection for RTC registers */
213 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
214
215 hrtc->State = HAL_RTC_STATE_ERROR;
216
217 return HAL_ERROR;
218 }
219 }
220
221 hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
222 hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
223
224 /* Enable the write protection for RTC registers */
225 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
226
227 /* Set RTC state */
228 hrtc->State = HAL_RTC_STATE_READY;
229
230 return HAL_OK;
231 }
232 }
233
234 /**
235 * @brief DeInitialize the RTC peripheral.
236 * @param hrtc RTC handle
237 * @note This function doesn't reset the RTC Backup Data registers.
238 * @retval HAL status
239 */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)240 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
241 {
242 #if defined (STM32F030xC) || defined (STM32F070xB) || \
243 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
244 defined (STM32F091xC) || defined (STM32F098xx)
245 uint32_t tickstart = 0;
246 #endif /* defined (STM32F030xC) || defined (STM32F070xB) ||\
247 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
248 defined (STM32F091xC) || defined (STM32F098xx) ||*/
249
250 /* Check the parameters */
251 assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
252
253 /* Set RTC state */
254 hrtc->State = HAL_RTC_STATE_BUSY;
255
256 /* Disable the write protection for RTC registers */
257 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
258
259 /* Set Initialization mode */
260 if(RTC_EnterInitMode(hrtc) != HAL_OK)
261 {
262 /* Enable the write protection for RTC registers */
263 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
264
265 /* Set RTC state */
266 hrtc->State = HAL_RTC_STATE_ERROR;
267
268 return HAL_ERROR;
269 }
270 else
271 {
272 /* Reset TR, DR and CR registers */
273 hrtc->Instance->TR = 0x00000000U;
274 hrtc->Instance->DR = 0x00002101U;
275
276 #if defined (STM32F030xC) || defined (STM32F070xB) || \
277 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
278 defined (STM32F091xC) || defined (STM32F098xx)
279 /* Reset All CR bits except CR[2:0] */
280 hrtc->Instance->CR &= 0x00000007U;
281
282 tickstart = HAL_GetTick();
283
284 /* Wait till WUTWF flag is set and if Time out is reached exit */
285 while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
286 {
287 if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
288 {
289 /* Enable the write protection for RTC registers */
290 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
291
292 /* Set RTC state */
293 hrtc->State = HAL_RTC_STATE_TIMEOUT;
294
295 return HAL_TIMEOUT;
296 }
297 }
298 #endif /* defined (STM32F030xC) || defined (STM32F070xB) ||\
299 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
300 defined (STM32F091xC) || defined (STM32F098xx) ||*/
301
302 /* Reset all RTC CR register bits */
303 hrtc->Instance->CR &= 0x00000000U;
304 #if defined (STM32F030xC) || defined (STM32F070xB) || \
305 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
306 defined (STM32F091xC) || defined (STM32F098xx)
307 hrtc->Instance->WUTR = 0x0000FFFFU;
308 #endif /* defined (STM32F030xC) || defined (STM32F070xB) ||\
309 defined (STM32F071xB) || defined (STM32F072xB) || defined (STM32F078xx) || \
310 defined (STM32F091xC) || defined (STM32F098xx) ||*/
311 hrtc->Instance->PRER = 0x007F00FFU;
312 hrtc->Instance->ALRMAR = 0x00000000U;
313 hrtc->Instance->SHIFTR = 0x00000000U;
314 hrtc->Instance->CALR = 0x00000000U;
315 hrtc->Instance->ALRMASSR = 0x00000000U;
316
317 /* Reset ISR register and exit initialization mode */
318 hrtc->Instance->ISR = 0x00000000U;
319
320 /* Reset Tamper and alternate functions configuration register */
321 hrtc->Instance->TAFCR = 0x00000000;
322
323 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
324 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
325 {
326 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
327 {
328 /* Enable the write protection for RTC registers */
329 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
330
331 hrtc->State = HAL_RTC_STATE_ERROR;
332
333 return HAL_ERROR;
334 }
335 }
336 }
337
338 /* Enable the write protection for RTC registers */
339 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
340
341 /* De-Initialize RTC MSP */
342 HAL_RTC_MspDeInit(hrtc);
343
344 hrtc->State = HAL_RTC_STATE_RESET;
345
346 /* Release Lock */
347 __HAL_UNLOCK(hrtc);
348
349 return HAL_OK;
350 }
351
352 /**
353 * @brief Initialize the RTC MSP.
354 * @param hrtc RTC handle
355 * @retval None
356 */
HAL_RTC_MspInit(RTC_HandleTypeDef * hrtc)357 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
358 {
359 /* Prevent unused argument(s) compilation warning */
360 UNUSED(hrtc);
361
362 /* NOTE : This function should not be modified, when the callback is needed,
363 the HAL_RTC_MspInit could be implemented in the user file
364 */
365 }
366
367 /**
368 * @brief DeInitialize the RTC MSP.
369 * @param hrtc RTC handle
370 * @retval None
371 */
HAL_RTC_MspDeInit(RTC_HandleTypeDef * hrtc)372 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
373 {
374 /* Prevent unused argument(s) compilation warning */
375 UNUSED(hrtc);
376
377 /* NOTE : This function should not be modified, when the callback is needed,
378 the HAL_RTC_MspDeInit could be implemented in the user file
379 */
380 }
381
382 /**
383 * @}
384 */
385
386 /** @addtogroup RTC_Exported_Functions_Group2
387 * @brief RTC Time and Date functions
388 *
389 @verbatim
390 ===============================================================================
391 ##### RTC Time and Date functions #####
392 ===============================================================================
393
394 [..] This section provides functions allowing to configure Time and Date features
395
396 @endverbatim
397 * @{
398 */
399
400 /**
401 * @brief Set RTC current time.
402 * @param hrtc RTC handle
403 * @param sTime Pointer to Time structure
404 * @param Format Specifies the format of the entered parameters.
405 * This parameter can be one of the following values:
406 * @arg RTC_FORMAT_BIN: Binary data format
407 * @arg RTC_FORMAT_BCD: BCD data format
408 * @retval HAL status
409 */
HAL_RTC_SetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)410 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
411 {
412 uint32_t tmpreg = 0U;
413
414 /* Check the parameters */
415 assert_param(IS_RTC_FORMAT(Format));
416 assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
417 assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
418
419 /* Process Locked */
420 __HAL_LOCK(hrtc);
421
422 hrtc->State = HAL_RTC_STATE_BUSY;
423
424 if(Format == RTC_FORMAT_BIN)
425 {
426 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
427 {
428 assert_param(IS_RTC_HOUR12(sTime->Hours));
429 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
430 }
431 else
432 {
433 sTime->TimeFormat = 0x00U;
434 assert_param(IS_RTC_HOUR24(sTime->Hours));
435 }
436 assert_param(IS_RTC_MINUTES(sTime->Minutes));
437 assert_param(IS_RTC_SECONDS(sTime->Seconds));
438
439 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
440 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
441 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
442 (((uint32_t)sTime->TimeFormat) << 16U));
443 }
444 else
445 {
446 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
447 {
448 tmpreg = RTC_Bcd2ToByte(sTime->Hours);
449 assert_param(IS_RTC_HOUR12(tmpreg));
450 assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
451 }
452 else
453 {
454 sTime->TimeFormat = 0x00U;
455 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
456 }
457 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
458 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
459 tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
460 ((uint32_t)(sTime->Minutes) << 8U) | \
461 ((uint32_t)sTime->Seconds) | \
462 ((uint32_t)(sTime->TimeFormat) << 16U));
463 }
464
465 /* Disable the write protection for RTC registers */
466 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
467
468 /* Set Initialization mode */
469 if(RTC_EnterInitMode(hrtc) != HAL_OK)
470 {
471 /* Enable the write protection for RTC registers */
472 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
473
474 /* Set RTC state */
475 hrtc->State = HAL_RTC_STATE_ERROR;
476
477 /* Process Unlocked */
478 __HAL_UNLOCK(hrtc);
479
480 return HAL_ERROR;
481 }
482 else
483 {
484 /* Set the RTC_TR register */
485 hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
486
487 /* Clear the bits to be configured */
488 hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
489
490 /* Configure the RTC_CR register */
491 hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
492
493 /* Exit Initialization mode */
494 hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
495
496 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
497 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
498 {
499 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
500 {
501 /* Enable the write protection for RTC registers */
502 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
503
504 hrtc->State = HAL_RTC_STATE_ERROR;
505
506 /* Process Unlocked */
507 __HAL_UNLOCK(hrtc);
508
509 return HAL_ERROR;
510 }
511 }
512
513 /* Enable the write protection for RTC registers */
514 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
515
516 hrtc->State = HAL_RTC_STATE_READY;
517
518 __HAL_UNLOCK(hrtc);
519
520 return HAL_OK;
521 }
522 }
523
524 /**
525 * @brief Get RTC current time.
526 * @param hrtc RTC handle
527 * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
528 * with input format (BIN or BCD), also SubSeconds field returning the
529 * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
530 * factor to be used for second fraction ratio computation.
531 * @param Format Specifies the format of the entered parameters.
532 * This parameter can be one of the following values:
533 * @arg RTC_FORMAT_BIN: Binary data format
534 * @arg RTC_FORMAT_BCD: BCD data format
535 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
536 * value in second fraction ratio with time unit following generic formula:
537 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
538 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
539 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
540 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
541 * Reading RTC current time locks the values in calendar shadow registers until Current date is read
542 * to ensure consistency between the time and date values.
543 * @retval HAL status
544 */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)545 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
546 {
547 uint32_t tmpreg = 0;
548
549 /* Check the parameters */
550 assert_param(IS_RTC_FORMAT(Format));
551
552 /* Get subseconds structure field from the corresponding register*/
553 sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
554
555 /* Get SecondFraction structure field from the corresponding register field*/
556 sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
557
558 /* Get the TR register */
559 tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
560
561 /* Fill the structure fields with the read parameters */
562 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
563 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8U);
564 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
565 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
566
567 /* Check the input parameters format */
568 if(Format == RTC_FORMAT_BIN)
569 {
570 /* Convert the time structure parameters to Binary format */
571 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
572 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
573 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
574 }
575
576 return HAL_OK;
577 }
578
579 /**
580 * @brief Set RTC current date.
581 * @param hrtc RTC handle
582 * @param sDate Pointer to date structure
583 * @param Format specifies the format of the entered parameters.
584 * This parameter can be one of the following values:
585 * @arg RTC_FORMAT_BIN: Binary data format
586 * @arg RTC_FORMAT_BCD: BCD data format
587 * @retval HAL status
588 */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)589 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
590 {
591 uint32_t datetmpreg = 0U;
592
593 /* Check the parameters */
594 assert_param(IS_RTC_FORMAT(Format));
595
596 /* Process Locked */
597 __HAL_LOCK(hrtc);
598
599 hrtc->State = HAL_RTC_STATE_BUSY;
600
601 if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
602 {
603 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
604 }
605
606 assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
607
608 if(Format == RTC_FORMAT_BIN)
609 {
610 assert_param(IS_RTC_YEAR(sDate->Year));
611 assert_param(IS_RTC_MONTH(sDate->Month));
612 assert_param(IS_RTC_DATE(sDate->Date));
613
614 datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
615 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
616 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
617 ((uint32_t)sDate->WeekDay << 13U));
618 }
619 else
620 {
621 assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
622 datetmpreg = RTC_Bcd2ToByte(sDate->Month);
623 assert_param(IS_RTC_MONTH(datetmpreg));
624 datetmpreg = RTC_Bcd2ToByte(sDate->Date);
625 assert_param(IS_RTC_DATE(datetmpreg));
626
627 datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
628 (((uint32_t)sDate->Month) << 8U) | \
629 ((uint32_t)sDate->Date) | \
630 (((uint32_t)sDate->WeekDay) << 13U));
631 }
632
633 /* Disable the write protection for RTC registers */
634 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
635
636 /* Set Initialization mode */
637 if(RTC_EnterInitMode(hrtc) != HAL_OK)
638 {
639 /* Enable the write protection for RTC registers */
640 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
641
642 /* Set RTC state*/
643 hrtc->State = HAL_RTC_STATE_ERROR;
644
645 /* Process Unlocked */
646 __HAL_UNLOCK(hrtc);
647
648 return HAL_ERROR;
649 }
650 else
651 {
652 /* Set the RTC_DR register */
653 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
654
655 /* Exit Initialization mode */
656 hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
657
658 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
659 if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
660 {
661 if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
662 {
663 /* Enable the write protection for RTC registers */
664 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
665
666 hrtc->State = HAL_RTC_STATE_ERROR;
667
668 /* Process Unlocked */
669 __HAL_UNLOCK(hrtc);
670
671 return HAL_ERROR;
672 }
673 }
674
675 /* Enable the write protection for RTC registers */
676 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
677
678 hrtc->State = HAL_RTC_STATE_READY ;
679
680 /* Process Unlocked */
681 __HAL_UNLOCK(hrtc);
682
683 return HAL_OK;
684 }
685 }
686
687 /**
688 * @brief Get RTC current date.
689 * @param hrtc RTC handle
690 * @param sDate Pointer to Date structure
691 * @param Format Specifies the format of the entered parameters.
692 * This parameter can be one of the following values:
693 * @arg RTC_FORMAT_BIN : Binary data format
694 * @arg RTC_FORMAT_BCD : BCD data format
695 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
696 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
697 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
698 * @retval HAL status
699 */
HAL_RTC_GetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)700 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
701 {
702 uint32_t datetmpreg = 0;
703
704 /* Check the parameters */
705 assert_param(IS_RTC_FORMAT(Format));
706
707 /* Get the DR register */
708 datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
709
710 /* Fill the structure fields with the read parameters */
711 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
712 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
713 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
714 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
715
716 /* Check the input parameters format */
717 if(Format == RTC_FORMAT_BIN)
718 {
719 /* Convert the date structure parameters to Binary format */
720 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
721 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
722 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
723 }
724 return HAL_OK;
725 }
726
727 /**
728 * @}
729 */
730
731 /** @addtogroup RTC_Exported_Functions_Group3
732 * @brief RTC Alarm functions
733 *
734 @verbatim
735 ===============================================================================
736 ##### RTC Alarm functions #####
737 ===============================================================================
738
739 [..] This section provides functions allowing to configure Alarm feature
740
741 @endverbatim
742 * @{
743 */
744 /**
745 * @brief Set the specified RTC Alarm.
746 * @param hrtc RTC handle
747 * @param sAlarm Pointer to Alarm structure
748 * @param Format Specifies the format of the entered parameters.
749 * This parameter can be one of the following values:
750 * @arg RTC_FORMAT_BIN: Binary data format
751 * @arg RTC_FORMAT_BCD: BCD data format
752 * @retval HAL status
753 */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)754 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
755 {
756 uint32_t tickstart = 0U;
757 uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
758
759 /* Check the parameters */
760 assert_param(IS_RTC_FORMAT(Format));
761 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
762 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
763 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
764 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
765 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
766
767 /* Process Locked */
768 __HAL_LOCK(hrtc);
769
770 hrtc->State = HAL_RTC_STATE_BUSY;
771
772 if(Format == RTC_FORMAT_BIN)
773 {
774 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
775 {
776 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
777 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
778 }
779 else
780 {
781 sAlarm->AlarmTime.TimeFormat = 0x00U;
782 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
783 }
784 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
785 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
786
787 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
788 {
789 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
790 }
791 else
792 {
793 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
794 }
795
796 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
797 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
798 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
799 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
800 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
801 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
802 ((uint32_t)sAlarm->AlarmMask));
803 }
804 else
805 {
806 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
807 {
808 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
809 assert_param(IS_RTC_HOUR12(tmpreg));
810 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
811 }
812 else
813 {
814 sAlarm->AlarmTime.TimeFormat = 0x00U;
815 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
816 }
817
818 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
819 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
820
821 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
822 {
823 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
824 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
825 }
826 else
827 {
828 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
829 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
830 }
831
832 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
833 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
834 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
835 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
836 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
837 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
838 ((uint32_t)sAlarm->AlarmMask));
839 }
840
841 /* Configure the Alarm A Sub Second registers */
842 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
843
844 /* Disable the write protection for RTC registers */
845 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
846
847 /* Disable the Alarm A interrupt */
848 __HAL_RTC_ALARMA_DISABLE(hrtc);
849
850 /* In case of interrupt mode is used, the interrupt source must disabled */
851 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
852
853 tickstart = HAL_GetTick();
854 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
855 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
856 {
857 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
858 {
859 /* Enable the write protection for RTC registers */
860 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
861
862 hrtc->State = HAL_RTC_STATE_TIMEOUT;
863
864 /* Process Unlocked */
865 __HAL_UNLOCK(hrtc);
866
867 return HAL_TIMEOUT;
868 }
869 }
870
871 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
872 /* Configure the Alarm A Sub Second register */
873 hrtc->Instance->ALRMASSR = subsecondtmpreg;
874 /* Configure the Alarm state: Enable Alarm */
875 __HAL_RTC_ALARMA_ENABLE(hrtc);
876
877 /* Enable the write protection for RTC registers */
878 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
879
880 /* Change RTC state */
881 hrtc->State = HAL_RTC_STATE_READY;
882
883 /* Process Unlocked */
884 __HAL_UNLOCK(hrtc);
885
886 return HAL_OK;
887 }
888
889 /**
890 * @brief Set the specified RTC Alarm with Interrupt.
891 * @param hrtc RTC handle
892 * @param sAlarm Pointer to Alarm structure
893 * @param Format Specifies the format of the entered parameters.
894 * This parameter can be one of the following values:
895 * @arg RTC_FORMAT_BIN: Binary data format
896 * @arg RTC_FORMAT_BCD: BCD data format
897 * @note The Alarm register can only be written when the corresponding Alarm
898 * is disabled (Use the HAL_RTC_DeactivateAlarm()).
899 * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
900 * @retval HAL status
901 */
HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)902 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
903 {
904 uint32_t tickstart = 0U;
905 uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
906
907 /* Check the parameters */
908 assert_param(IS_RTC_FORMAT(Format));
909 assert_param(IS_RTC_ALARM(sAlarm->Alarm));
910 assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
911 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
912 assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
913 assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
914
915 /* Process Locked */
916 __HAL_LOCK(hrtc);
917
918 hrtc->State = HAL_RTC_STATE_BUSY;
919
920 if(Format == RTC_FORMAT_BIN)
921 {
922 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
923 {
924 assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
925 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
926 }
927 else
928 {
929 sAlarm->AlarmTime.TimeFormat = 0x00U;
930 assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
931 }
932 assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
933 assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
934
935 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
936 {
937 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
938 }
939 else
940 {
941 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
942 }
943 tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
944 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
945 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
946 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
947 ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
948 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
949 ((uint32_t)sAlarm->AlarmMask));
950 }
951 else
952 {
953 if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
954 {
955 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
956 assert_param(IS_RTC_HOUR12(tmpreg));
957 assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
958 }
959 else
960 {
961 sAlarm->AlarmTime.TimeFormat = 0x00U;
962 assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
963 }
964
965 assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
966 assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
967
968 if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
969 {
970 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
971 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
972 }
973 else
974 {
975 tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
976 assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
977 }
978 tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
979 ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
980 ((uint32_t) sAlarm->AlarmTime.Seconds) | \
981 ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
982 ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
983 ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
984 ((uint32_t)sAlarm->AlarmMask));
985 }
986 /* Configure the Alarm A Sub Second registers */
987 subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
988
989 /* Disable the write protection for RTC registers */
990 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
991
992 /* Disable the Alarm A interrupt */
993 __HAL_RTC_ALARMA_DISABLE(hrtc);
994
995 /* Clear flag alarm A */
996 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
997
998 tickstart = HAL_GetTick();
999
1000 /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1001 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1002 {
1003 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1004 {
1005 /* Enable the write protection for RTC registers */
1006 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1007
1008 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1009
1010 /* Process Unlocked */
1011 __HAL_UNLOCK(hrtc);
1012
1013 return HAL_TIMEOUT;
1014 }
1015 }
1016
1017 hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1018 /* Configure the Alarm A Sub Second register */
1019 hrtc->Instance->ALRMASSR = subsecondtmpreg;
1020 /* Configure the Alarm state: Enable Alarm */
1021 __HAL_RTC_ALARMA_ENABLE(hrtc);
1022 /* Configure the Alarm interrupt */
1023 __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
1024
1025 /* RTC Alarm Interrupt Configuration: EXTI configuration */
1026 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1027
1028 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1029
1030 /* Enable the write protection for RTC registers */
1031 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1032
1033 hrtc->State = HAL_RTC_STATE_READY;
1034
1035 /* Process Unlocked */
1036 __HAL_UNLOCK(hrtc);
1037
1038 return HAL_OK;
1039 }
1040
1041 /**
1042 * @brief Deactivate the specified RTC Alarm.
1043 * @param hrtc RTC handle
1044 * @param Alarm Specifies the Alarm.
1045 * This parameter can be one of the following values:
1046 * @arg RTC_ALARM_A: AlarmA
1047 * @retval HAL status
1048 */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc,uint32_t Alarm)1049 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1050 {
1051 uint32_t tickstart = 0U;
1052
1053 /* Check the parameters */
1054 assert_param(IS_RTC_ALARM(Alarm));
1055
1056 /* Process Locked */
1057 __HAL_LOCK(hrtc);
1058
1059 hrtc->State = HAL_RTC_STATE_BUSY;
1060
1061 /* Disable the write protection for RTC registers */
1062 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
1063
1064 __HAL_RTC_ALARMA_DISABLE(hrtc);
1065
1066 /* In case of interrupt mode is used, the interrupt source must disabled */
1067 __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1068
1069 tickstart = HAL_GetTick();
1070
1071 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1072 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
1073 {
1074 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1075 {
1076 /* Enable the write protection for RTC registers */
1077 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1078
1079 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1080
1081 /* Process Unlocked */
1082 __HAL_UNLOCK(hrtc);
1083
1084 return HAL_TIMEOUT;
1085 }
1086 }
1087 /* Enable the write protection for RTC registers */
1088 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
1089
1090 hrtc->State = HAL_RTC_STATE_READY;
1091
1092 /* Process Unlocked */
1093 __HAL_UNLOCK(hrtc);
1094
1095 return HAL_OK;
1096 }
1097
1098 /**
1099 * @brief Get the RTC Alarm value and masks.
1100 * @param hrtc RTC handle
1101 * @param sAlarm Pointer to Date structure
1102 * @param Alarm Specifies the Alarm.
1103 * This parameter can be one of the following values:
1104 * @arg RTC_ALARM_A: AlarmA
1105 * @param Format Specifies the format of the entered parameters.
1106 * This parameter can be one of the following values:
1107 * @arg RTC_FORMAT_BIN: Binary data format
1108 * @arg RTC_FORMAT_BCD: BCD data format
1109 * @retval HAL status
1110 */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Alarm,uint32_t Format)1111 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1112 {
1113 uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
1114
1115 /* Check the parameters */
1116 assert_param(IS_RTC_FORMAT(Format));
1117 assert_param(IS_RTC_ALARM(Alarm));
1118
1119 sAlarm->Alarm = RTC_ALARM_A;
1120
1121 tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1122 subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
1123
1124 /* Fill the structure with the read parameters */
1125 sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
1126 sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
1127 sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
1128 sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
1129 sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1130 sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24U);
1131 sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1132 sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1133
1134 if(Format == RTC_FORMAT_BIN)
1135 {
1136 sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1137 sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1138 sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1139 sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
1140 }
1141
1142 return HAL_OK;
1143 }
1144
1145 /**
1146 * @brief Handle Alarm interrupt request.
1147 * @param hrtc RTC handle
1148 * @retval None
1149 */
HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef * hrtc)1150 void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
1151 {
1152 /* Get the AlarmA interrupt source enable status */
1153 if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != RESET)
1154 {
1155 /* Get the pending status of the AlarmA Interrupt */
1156 if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != RESET)
1157 {
1158 /* AlarmA callback */
1159 HAL_RTC_AlarmAEventCallback(hrtc);
1160
1161 /* Clear the AlarmA interrupt pending bit */
1162 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1163 }
1164 }
1165
1166 /* Clear the EXTI's line Flag for RTC Alarm */
1167 __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1168
1169 /* Change RTC state */
1170 hrtc->State = HAL_RTC_STATE_READY;
1171 }
1172
1173 /**
1174 * @brief Alarm A callback.
1175 * @param hrtc RTC handle
1176 * @retval None
1177 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)1178 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
1179 {
1180 /* Prevent unused argument(s) compilation warning */
1181 UNUSED(hrtc);
1182
1183 /* NOTE : This function should not be modified, when the callback is needed,
1184 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1185 */
1186 }
1187
1188 /**
1189 * @brief Handle AlarmA Polling request.
1190 * @param hrtc RTC handle
1191 * @param Timeout Timeout duration
1192 * @retval HAL status
1193 */
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)1194 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1195 {
1196
1197 uint32_t tickstart = HAL_GetTick();
1198
1199 while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
1200 {
1201 if(Timeout != HAL_MAX_DELAY)
1202 {
1203 if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
1204 {
1205 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1206 return HAL_TIMEOUT;
1207 }
1208 }
1209 }
1210
1211 /* Clear the Alarm interrupt pending bit */
1212 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1213
1214 /* Change RTC state */
1215 hrtc->State = HAL_RTC_STATE_READY;
1216
1217 return HAL_OK;
1218 }
1219
1220 /**
1221 * @}
1222 */
1223
1224 /** @addtogroup RTC_Exported_Functions_Group4
1225 * @brief Peripheral Control functions
1226 *
1227 @verbatim
1228 ===============================================================================
1229 ##### Peripheral Control functions #####
1230 ===============================================================================
1231 [..]
1232 This subsection provides functions allowing to
1233 (+) Wait for RTC Time and Date Synchronization
1234
1235 @endverbatim
1236 * @{
1237 */
1238
1239 /**
1240 * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
1241 * synchronized with RTC APB clock.
1242 * @note The RTC Resynchronization mode is write protected, use the
1243 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1244 * @note To read the calendar through the shadow registers after Calendar
1245 * initialization, calendar update or after wakeup from low power modes
1246 * the software must first clear the RSF flag.
1247 * The software must then wait until it is set again before reading
1248 * the calendar, which means that the calendar registers have been
1249 * correctly copied into the RTC_TR and RTC_DR shadow registers.
1250 * @param hrtc RTC handle
1251 * @retval HAL status
1252 */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)1253 HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
1254 {
1255 uint32_t tickstart = 0U;
1256
1257 /* Clear RSF flag */
1258 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1259
1260 tickstart = HAL_GetTick();
1261
1262 /* Wait the registers to be synchronised */
1263 while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
1264 {
1265 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1266 {
1267 return HAL_TIMEOUT;
1268 }
1269 }
1270
1271 return HAL_OK;
1272 }
1273
1274 /**
1275 * @}
1276 */
1277
1278 /** @addtogroup RTC_Exported_Functions_Group5
1279 * @brief Peripheral State functions
1280 *
1281 @verbatim
1282 ===============================================================================
1283 ##### Peripheral State functions #####
1284 ===============================================================================
1285 [..]
1286 This subsection provides functions allowing to
1287 (+) Get RTC state
1288
1289 @endverbatim
1290 * @{
1291 */
1292 /**
1293 * @brief Return the RTC handle state.
1294 * @param hrtc RTC handle
1295 * @retval HAL state
1296 */
HAL_RTC_GetState(RTC_HandleTypeDef * hrtc)1297 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
1298 {
1299 /* Return RTC handle state */
1300 return hrtc->State;
1301 }
1302
1303 /**
1304 * @}
1305 */
1306
1307 /**
1308 * @}
1309 */
1310
1311 /** @addtogroup RTC_Private_Functions
1312 * @{
1313 */
1314 /**
1315 * @brief Enter the RTC Initialization mode.
1316 * @note The RTC Initialization mode is write protected, use the
1317 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
1318 * @param hrtc RTC handle
1319 * @retval HAL status
1320 */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)1321 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
1322 {
1323 uint32_t tickstart = 0U;
1324
1325 /* Check if the Initialization mode is set */
1326 if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1327 {
1328 /* Set the Initialization mode */
1329 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1330
1331 tickstart = HAL_GetTick();
1332
1333 /* Wait till RTC is in INIT state and if Time out is reached exit */
1334 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
1335 {
1336 if((HAL_GetTick()-tickstart) > RTC_TIMEOUT_VALUE)
1337 {
1338 return HAL_TIMEOUT;
1339 }
1340 }
1341 }
1342
1343 return HAL_OK;
1344 }
1345
1346
1347 /**
1348 * @brief Convert a 2 digit decimal to BCD format.
1349 * @param Value Byte to be converted
1350 * @retval Converted byte
1351 */
RTC_ByteToBcd2(uint8_t Value)1352 uint8_t RTC_ByteToBcd2(uint8_t Value)
1353 {
1354 uint32_t bcdhigh = 0U;
1355
1356 while(Value >= 10U)
1357 {
1358 bcdhigh++;
1359 Value -= 10U;
1360 }
1361
1362 return ((uint8_t)(bcdhigh << 4U) | Value);
1363 }
1364
1365 /**
1366 * @brief Convert from 2 digit BCD to Binary.
1367 * @param Value BCD value to be converted
1368 * @retval Converted word
1369 */
RTC_Bcd2ToByte(uint8_t Value)1370 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1371 {
1372 uint32_t tmp = 0U;
1373 tmp = ((uint8_t)(Value & (uint8_t)0xF0U) >> (uint8_t)0x4U) * 10U;
1374 return (tmp + (Value & (uint8_t)0x0FU));
1375 }
1376 /**
1377 * @}
1378 */
1379
1380 #endif /* HAL_RTC_MODULE_ENABLED */
1381
1382 /**
1383 * @}
1384 */
1385
1386
1387 /**
1388 * @}
1389 */
1390
1391 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1392