comparison Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c @ 128:c78bcbd5deda FlipDisplay

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