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