source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

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