2 ******************************************************************************
3 * @file stm32g4xx_ll_rtc.c
4 * @author MCD Application Team
5 * @brief RTC LL module driver.
6 ******************************************************************************
9 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
11 * This software component is licensed by ST under BSD 3-Clause license,
12 * the "License"; You may not use this file except in compliance with the
13 * License. You may obtain a copy of the License at:
14 * opensource.org/licenses/BSD-3-Clause
16 ******************************************************************************
18 #if defined(USE_FULL_LL_DRIVER)
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32g4xx_ll_rtc.h"
22 #include "stm32g4xx_ll_cortex.h"
23 #ifdef USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else /* USE_FULL_ASSERT */
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
29 /** @addtogroup STM32G4xx_LL_Driver
35 /** @addtogroup RTC_LL
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /** @addtogroup RTC_LL_Private_Constants
45 /* Default values used for prescaler */
46 #define RTC_ASYNCH_PRESC_DEFAULT ((uint32_t) 0x0000007FU)
47 #define RTC_SYNCH_PRESC_DEFAULT ((uint32_t) 0x000000FFU)
49 /* Values used for timeout */
50 #define RTC_INITMODE_TIMEOUT ((uint32_t) 1000U) /* 1s when tick set to 1ms */
51 #define RTC_SYNCHRO_TIMEOUT ((uint32_t) 1000U) /* 1s when tick set to 1ms */
56 /* Private macros ------------------------------------------------------------*/
57 /** @addtogroup RTC_LL_Private_Macros
60 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
61 || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
63 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FU)
65 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__) ((__VALUE__) <= 0x7FFFU)
67 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
68 || ((__VALUE__) == LL_RTC_FORMAT_BCD))
70 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
71 || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
73 #define IS_LL_RTC_HOUR12(__HOUR__) (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
74 #define IS_LL_RTC_HOUR24(__HOUR__) ((__HOUR__) <= 23U)
75 #define IS_LL_RTC_MINUTES(__MINUTES__) ((__MINUTES__) <= 59U)
76 #define IS_LL_RTC_SECONDS(__SECONDS__) ((__SECONDS__) <= 59U)
78 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
79 || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
80 || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
81 || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
82 || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
83 || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
84 || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
86 #define IS_LL_RTC_DAY(__DAY__) (((__DAY__) >= (uint32_t)1U) && ((__DAY__) <= (uint32_t)31U))
88 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
89 || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
90 || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
91 || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
92 || ((__VALUE__) == LL_RTC_MONTH_MAY) \
93 || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
94 || ((__VALUE__) == LL_RTC_MONTH_JULY) \
95 || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
96 || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
97 || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
98 || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
99 || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
101 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
103 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
104 || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
105 || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
106 || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
107 || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
108 || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
110 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
111 || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
112 || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
113 || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
114 || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
115 || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
118 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
119 ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
121 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
122 ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
126 /* Private function prototypes -----------------------------------------------*/
127 /* Exported functions --------------------------------------------------------*/
128 /** @addtogroup RTC_LL_Exported_Functions
132 /** @addtogroup RTC_LL_EF_Init
137 * @brief De-Initializes the RTC registers to their default reset values.
138 * @note This function does not reset the RTC Clock source and RTC Backup Data
140 * @param RTCx RTC Instance
141 * @retval An ErrorStatus enumeration value:
142 * - SUCCESS: RTC registers are de-initialized
143 * - ERROR: RTC registers are not de-initialized
145 ErrorStatus
LL_RTC_DeInit(RTC_TypeDef
*RTCx
)
147 ErrorStatus status
= ERROR
;
149 /* Check the parameter */
150 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
151 assert_param(IS_TAMP_ALL_INSTANCE(TAMP
));
153 /* Disable the write protection for RTC registers */
154 LL_RTC_DisableWriteProtection(RTCx
);
156 /* Set Initialization mode */
157 if (LL_RTC_EnterInitMode(RTCx
) != ERROR
)
159 /* Reset TR, DR and CR registers */
160 WRITE_REG(RTCx
->TR
, 0x00000000U
);
161 #if defined(RTC_WAKEUP_SUPPORT)
162 WRITE_REG(RTCx
->WUTR
, RTC_WUTR_WUT
);
163 #endif /* RTC_WAKEUP_SUPPORT */
164 WRITE_REG(RTCx
->DR
, (RTC_DR_WDU_0
| RTC_DR_MU_0
| RTC_DR_DU_0
));
165 /* Reset All CR bits except CR[2:0] */
166 #if defined(RTC_WAKEUP_SUPPORT)
167 WRITE_REG(RTCx
->CR
, (READ_REG(RTCx
->CR
) & RTC_CR_WUCKSEL
));
169 WRITE_REG(RTCx
->CR
, 0x00000000U
);
170 #endif /* RTC_WAKEUP_SUPPORT */
171 WRITE_REG(RTCx
->PRER
, (RTC_PRER_PREDIV_A
| RTC_SYNCH_PRESC_DEFAULT
));
172 WRITE_REG(RTCx
->ALRMAR
, 0x00000000U
);
173 WRITE_REG(RTCx
->ALRMBR
, 0x00000000U
);
174 WRITE_REG(RTCx
->SHIFTR
, 0x00000000U
);
175 WRITE_REG(RTCx
->CALR
, 0x00000000U
);
176 WRITE_REG(RTCx
->ALRMASSR
, 0x00000000U
);
177 WRITE_REG(RTCx
->ALRMBSSR
, 0x00000000U
);
179 /* Exit Initialization mode */
180 LL_RTC_DisableInitMode(RTCx
);
182 /* Wait till the RTC RSF flag is set */
183 status
= LL_RTC_WaitForSynchro(RTCx
);
186 /* Enable the write protection for RTC registers */
187 LL_RTC_EnableWriteProtection(RTCx
);
189 /* DeInitialization of the TAMP */
190 /* Reset TAMP CR1 and CR2 registers */
191 WRITE_REG(TAMP
->CR1
, 0xFFFF0000U
);
192 WRITE_REG(TAMP
->CR2
, 0x00000000U
);
193 #if defined (RTC_OTHER_SUPPORT)
194 WRITE_REG(TAMP
->CR3
, 0x00000000U
);
195 WRITE_REG(TAMP
->SMCR
, 0x00000000U
);
196 WRITE_REG(TAMP
->PRIVCR
, 0x00000000U
);
197 #endif /* RTC_OTHER_SUPPORT */
198 WRITE_REG(TAMP
->FLTCR
, 0x00000000U
);
199 #if defined (RTC_ACTIVE_TAMPER_SUPPORT)
200 WRITE_REG(TAMP
->ATCR1
, 0x00000000U
);
201 WRITE_REG(TAMP
->ATCR2
, 0x00000000U
);
202 #endif /* RTC_ACTIVE_TAMPER_SUPPORT */
203 WRITE_REG(TAMP
->IER
, 0x00000000U
);
204 WRITE_REG(TAMP
->SCR
, 0xFFFFFFFFU
);
205 #if defined (RTC_OPTION_REG_SUPPORT)
206 WRITE_REG(TAMP
->OR
, 0x00000000U
);
207 #endif /* RTC_OPTION_REG_SUPPORT */
213 * @brief Initializes the RTC registers according to the specified parameters
215 * @param RTCx RTC Instance
216 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
217 * the configuration information for the RTC peripheral.
218 * @note The RTC Prescaler register is write protected and can be written in
219 * initialization mode only.
220 * @retval An ErrorStatus enumeration value:
221 * - SUCCESS: RTC registers are initialized
222 * - ERROR: RTC registers are not initialized
224 ErrorStatus
LL_RTC_Init(RTC_TypeDef
*RTCx
, LL_RTC_InitTypeDef
*RTC_InitStruct
)
226 ErrorStatus status
= ERROR
;
228 /* Check the parameters */
229 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
230 assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct
->HourFormat
));
231 assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct
->AsynchPrescaler
));
232 assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct
->SynchPrescaler
));
234 /* Disable the write protection for RTC registers */
235 LL_RTC_DisableWriteProtection(RTCx
);
237 /* Set Initialization mode */
238 if (LL_RTC_EnterInitMode(RTCx
) != ERROR
)
240 /* Set Hour Format */
241 LL_RTC_SetHourFormat(RTCx
, RTC_InitStruct
->HourFormat
);
243 /* Configure Synchronous prescaler factor */
244 LL_RTC_SetSynchPrescaler(RTCx
, RTC_InitStruct
->SynchPrescaler
);
245 /* Configure Asynchronous prescaler factor */
246 LL_RTC_SetAsynchPrescaler(RTCx
, RTC_InitStruct
->AsynchPrescaler
);
248 /* Exit Initialization mode */
249 LL_RTC_DisableInitMode(RTCx
);
253 /* Enable the write protection for RTC registers */
254 LL_RTC_EnableWriteProtection(RTCx
);
260 * @brief Set each @ref LL_RTC_InitTypeDef field to default value.
261 * @param RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
264 void LL_RTC_StructInit(LL_RTC_InitTypeDef
*RTC_InitStruct
)
266 /* Set RTC_InitStruct fields to default values */
267 RTC_InitStruct
->HourFormat
= LL_RTC_HOURFORMAT_24HOUR
;
268 RTC_InitStruct
->AsynchPrescaler
= RTC_ASYNCH_PRESC_DEFAULT
;
269 RTC_InitStruct
->SynchPrescaler
= RTC_SYNCH_PRESC_DEFAULT
;
273 * @brief Set the RTC current time.
274 * @param RTCx RTC Instance
275 * @param RTC_Format This parameter can be one of the following values:
276 * @arg @ref LL_RTC_FORMAT_BIN
277 * @arg @ref LL_RTC_FORMAT_BCD
278 * @param RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
279 * the time configuration information for the RTC.
280 * @retval An ErrorStatus enumeration value:
281 * - SUCCESS: RTC Time register is configured
282 * - ERROR: RTC Time register is not configured
284 ErrorStatus
LL_RTC_TIME_Init(RTC_TypeDef
*RTCx
, uint32_t RTC_Format
, LL_RTC_TimeTypeDef
*RTC_TimeStruct
)
286 ErrorStatus status
= ERROR
;
288 /* Check the parameters */
289 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
290 assert_param(IS_LL_RTC_FORMAT(RTC_Format
));
292 if (RTC_Format
== LL_RTC_FORMAT_BIN
)
294 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
296 assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct
->Hours
));
297 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct
->TimeFormat
));
301 RTC_TimeStruct
->TimeFormat
= 0x00U
;
302 assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct
->Hours
));
304 assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct
->Minutes
));
305 assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct
->Seconds
));
309 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
311 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct
->Hours
)));
312 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct
->TimeFormat
));
316 RTC_TimeStruct
->TimeFormat
= 0x00U
;
317 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct
->Hours
)));
319 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct
->Minutes
)));
320 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct
->Seconds
)));
323 /* Disable the write protection for RTC registers */
324 LL_RTC_DisableWriteProtection(RTCx
);
326 /* Set Initialization mode */
327 if (LL_RTC_EnterInitMode(RTCx
) != ERROR
)
329 /* Check the input parameters format */
330 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
332 LL_RTC_TIME_Config(RTCx
, RTC_TimeStruct
->TimeFormat
, RTC_TimeStruct
->Hours
,
333 RTC_TimeStruct
->Minutes
, RTC_TimeStruct
->Seconds
);
337 LL_RTC_TIME_Config(RTCx
, RTC_TimeStruct
->TimeFormat
, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct
->Hours
),
338 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct
->Minutes
),
339 __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct
->Seconds
));
342 /* Exit Initialization mode */
343 LL_RTC_DisableInitMode(RTC
);
345 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
346 if (LL_RTC_IsShadowRegBypassEnabled(RTCx
) == 0U)
348 status
= LL_RTC_WaitForSynchro(RTCx
);
355 /* Enable the write protection for RTC registers */
356 LL_RTC_EnableWriteProtection(RTCx
);
362 * @brief Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
363 * @param RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
366 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef
*RTC_TimeStruct
)
368 /* Time = 00h:00min:00sec */
369 RTC_TimeStruct
->TimeFormat
= LL_RTC_TIME_FORMAT_AM_OR_24
;
370 RTC_TimeStruct
->Hours
= 0U;
371 RTC_TimeStruct
->Minutes
= 0U;
372 RTC_TimeStruct
->Seconds
= 0U;
376 * @brief Set the RTC current date.
377 * @param RTCx RTC Instance
378 * @param RTC_Format This parameter can be one of the following values:
379 * @arg @ref LL_RTC_FORMAT_BIN
380 * @arg @ref LL_RTC_FORMAT_BCD
381 * @param RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
382 * the date configuration information for the RTC.
383 * @retval An ErrorStatus enumeration value:
384 * - SUCCESS: RTC Day register is configured
385 * - ERROR: RTC Day register is not configured
387 ErrorStatus
LL_RTC_DATE_Init(RTC_TypeDef
*RTCx
, uint32_t RTC_Format
, LL_RTC_DateTypeDef
*RTC_DateStruct
)
389 ErrorStatus status
= ERROR
;
391 /* Check the parameters */
392 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
393 assert_param(IS_LL_RTC_FORMAT(RTC_Format
));
395 if ((RTC_Format
== LL_RTC_FORMAT_BIN
) && ((RTC_DateStruct
->Month
& 0x10U
) == 0x10U
))
397 RTC_DateStruct
->Month
= (uint8_t)(RTC_DateStruct
->Month
& (uint32_t)~(0x10U
)) + 0x0AU
;
399 if (RTC_Format
== LL_RTC_FORMAT_BIN
)
401 assert_param(IS_LL_RTC_YEAR(RTC_DateStruct
->Year
));
402 assert_param(IS_LL_RTC_MONTH(RTC_DateStruct
->Month
));
403 assert_param(IS_LL_RTC_DAY(RTC_DateStruct
->Day
));
407 assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct
->Year
)));
408 assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct
->Month
)));
409 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct
->Day
)));
411 assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct
->WeekDay
));
413 /* Disable the write protection for RTC registers */
414 LL_RTC_DisableWriteProtection(RTCx
);
416 /* Set Initialization mode */
417 if (LL_RTC_EnterInitMode(RTCx
) != ERROR
)
419 /* Check the input parameters format */
420 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
422 LL_RTC_DATE_Config(RTCx
, RTC_DateStruct
->WeekDay
, RTC_DateStruct
->Day
, RTC_DateStruct
->Month
, RTC_DateStruct
->Year
);
426 LL_RTC_DATE_Config(RTCx
, RTC_DateStruct
->WeekDay
, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct
->Day
),
427 __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct
->Month
), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct
->Year
));
430 /* Exit Initialization mode */
431 LL_RTC_DisableInitMode(RTC
);
433 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
434 if (LL_RTC_IsShadowRegBypassEnabled(RTCx
) == 0U)
436 status
= LL_RTC_WaitForSynchro(RTCx
);
443 /* Enable the write protection for RTC registers */
444 LL_RTC_EnableWriteProtection(RTCx
);
450 * @brief Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
451 * @param RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
454 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef
*RTC_DateStruct
)
456 /* Monday, January 01 xx00 */
457 RTC_DateStruct
->WeekDay
= LL_RTC_WEEKDAY_MONDAY
;
458 RTC_DateStruct
->Day
= 1U;
459 RTC_DateStruct
->Month
= LL_RTC_MONTH_JANUARY
;
460 RTC_DateStruct
->Year
= 0U;
464 * @brief Set the RTC Alarm A.
465 * @note The Alarm register can only be written when the corresponding Alarm
466 * is disabled (Use @ref LL_RTC_ALMA_Disable function).
467 * @param RTCx RTC Instance
468 * @param RTC_Format This parameter can be one of the following values:
469 * @arg @ref LL_RTC_FORMAT_BIN
470 * @arg @ref LL_RTC_FORMAT_BCD
471 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
472 * contains the alarm configuration parameters.
473 * @retval An ErrorStatus enumeration value:
474 * - SUCCESS: ALARMA registers are configured
475 * - ERROR: ALARMA registers are not configured
477 ErrorStatus
LL_RTC_ALMA_Init(RTC_TypeDef
*RTCx
, uint32_t RTC_Format
, LL_RTC_AlarmTypeDef
*RTC_AlarmStruct
)
479 /* Check the parameters */
480 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
481 assert_param(IS_LL_RTC_FORMAT(RTC_Format
));
482 assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct
->AlarmMask
));
483 assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct
->AlarmDateWeekDaySel
));
485 if (RTC_Format
== LL_RTC_FORMAT_BIN
)
487 /* initialize the AlarmTime for Binary format */
488 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
490 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct
->AlarmTime
.Hours
));
491 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct
->AlarmTime
.TimeFormat
));
495 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= 0x00U
;
496 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct
->AlarmTime
.Hours
));
498 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct
->AlarmTime
.Minutes
));
499 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct
->AlarmTime
.Seconds
));
501 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMA_DATEWEEKDAYSEL_DATE
)
503 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct
->AlarmDateWeekDay
));
507 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct
->AlarmDateWeekDay
));
512 /* initialize the AlarmTime for BCD format */
513 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
515 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Hours
)));
516 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct
->AlarmTime
.TimeFormat
));
520 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= 0x00U
;
521 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Hours
)));
524 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Minutes
)));
525 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Seconds
)));
527 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMA_DATEWEEKDAYSEL_DATE
)
529 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmDateWeekDay
)));
533 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmDateWeekDay
)));
537 /* Disable the write protection for RTC registers */
538 LL_RTC_DisableWriteProtection(RTCx
);
540 /* Select weekday selection */
541 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMA_DATEWEEKDAYSEL_DATE
)
543 /* Set the date for ALARM */
544 LL_RTC_ALMA_DisableWeekday(RTCx
);
545 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
547 LL_RTC_ALMA_SetDay(RTCx
, RTC_AlarmStruct
->AlarmDateWeekDay
);
551 LL_RTC_ALMA_SetDay(RTCx
, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmDateWeekDay
));
556 /* Set the week day for ALARM */
557 LL_RTC_ALMA_EnableWeekday(RTCx
);
558 LL_RTC_ALMA_SetWeekDay(RTCx
, RTC_AlarmStruct
->AlarmDateWeekDay
);
561 /* Configure the Alarm register */
562 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
564 LL_RTC_ALMA_ConfigTime(RTCx
, RTC_AlarmStruct
->AlarmTime
.TimeFormat
, RTC_AlarmStruct
->AlarmTime
.Hours
,
565 RTC_AlarmStruct
->AlarmTime
.Minutes
, RTC_AlarmStruct
->AlarmTime
.Seconds
);
569 LL_RTC_ALMA_ConfigTime(RTCx
, RTC_AlarmStruct
->AlarmTime
.TimeFormat
,
570 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Hours
),
571 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Minutes
),
572 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Seconds
));
575 LL_RTC_ALMA_SetMask(RTCx
, RTC_AlarmStruct
->AlarmMask
);
577 /* Enable the write protection for RTC registers */
578 LL_RTC_EnableWriteProtection(RTCx
);
584 * @brief Set the RTC Alarm B.
585 * @note The Alarm register can only be written when the corresponding Alarm
586 * is disabled (@ref LL_RTC_ALMB_Disable function).
587 * @param RTCx RTC Instance
588 * @param RTC_Format This parameter can be one of the following values:
589 * @arg @ref LL_RTC_FORMAT_BIN
590 * @arg @ref LL_RTC_FORMAT_BCD
591 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
592 * contains the alarm configuration parameters.
593 * @retval An ErrorStatus enumeration value:
594 * - SUCCESS: ALARMB registers are configured
595 * - ERROR: ALARMB registers are not configured
597 ErrorStatus
LL_RTC_ALMB_Init(RTC_TypeDef
*RTCx
, uint32_t RTC_Format
, LL_RTC_AlarmTypeDef
*RTC_AlarmStruct
)
599 /* Check the parameters */
600 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
601 assert_param(IS_LL_RTC_FORMAT(RTC_Format
));
602 assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct
->AlarmMask
));
603 assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct
->AlarmDateWeekDaySel
));
605 if (RTC_Format
== LL_RTC_FORMAT_BIN
)
607 /* initialize the AlarmTime for Binary format */
608 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
610 assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct
->AlarmTime
.Hours
));
611 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct
->AlarmTime
.TimeFormat
));
615 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= 0x00U
;
616 assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct
->AlarmTime
.Hours
));
618 assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct
->AlarmTime
.Minutes
));
619 assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct
->AlarmTime
.Seconds
));
621 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMB_DATEWEEKDAYSEL_DATE
)
623 assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct
->AlarmDateWeekDay
));
627 assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct
->AlarmDateWeekDay
));
632 /* initialize the AlarmTime for BCD format */
633 if (LL_RTC_GetHourFormat(RTCx
) != LL_RTC_HOURFORMAT_24HOUR
)
635 assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Hours
)));
636 assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct
->AlarmTime
.TimeFormat
));
640 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= 0x00U
;
641 assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Hours
)));
644 assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Minutes
)));
645 assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmTime
.Seconds
)));
647 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMB_DATEWEEKDAYSEL_DATE
)
649 assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmDateWeekDay
)));
653 assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct
->AlarmDateWeekDay
)));
657 /* Disable the write protection for RTC registers */
658 LL_RTC_DisableWriteProtection(RTCx
);
660 /* Select weekday selection */
661 if (RTC_AlarmStruct
->AlarmDateWeekDaySel
== LL_RTC_ALMB_DATEWEEKDAYSEL_DATE
)
663 /* Set the date for ALARM */
664 LL_RTC_ALMB_DisableWeekday(RTCx
);
665 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
667 LL_RTC_ALMB_SetDay(RTCx
, RTC_AlarmStruct
->AlarmDateWeekDay
);
671 LL_RTC_ALMB_SetDay(RTCx
, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmDateWeekDay
));
676 /* Set the week day for ALARM */
677 LL_RTC_ALMB_EnableWeekday(RTCx
);
678 LL_RTC_ALMB_SetWeekDay(RTCx
, RTC_AlarmStruct
->AlarmDateWeekDay
);
681 /* Configure the Alarm register */
682 if (RTC_Format
!= LL_RTC_FORMAT_BIN
)
684 LL_RTC_ALMB_ConfigTime(RTCx
, RTC_AlarmStruct
->AlarmTime
.TimeFormat
, RTC_AlarmStruct
->AlarmTime
.Hours
,
685 RTC_AlarmStruct
->AlarmTime
.Minutes
, RTC_AlarmStruct
->AlarmTime
.Seconds
);
689 LL_RTC_ALMB_ConfigTime(RTCx
, RTC_AlarmStruct
->AlarmTime
.TimeFormat
,
690 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Hours
),
691 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Minutes
),
692 __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct
->AlarmTime
.Seconds
));
695 LL_RTC_ALMB_SetMask(RTCx
, RTC_AlarmStruct
->AlarmMask
);
697 /* Enable the write protection for RTC registers */
698 LL_RTC_EnableWriteProtection(RTCx
);
704 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
705 * Day = 1st day of the month/Mask = all fields are masked).
706 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
709 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef
*RTC_AlarmStruct
)
711 /* Alarm Time Settings : Time = 00h:00mn:00sec */
712 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= LL_RTC_ALMA_TIME_FORMAT_AM
;
713 RTC_AlarmStruct
->AlarmTime
.Hours
= 0U;
714 RTC_AlarmStruct
->AlarmTime
.Minutes
= 0U;
715 RTC_AlarmStruct
->AlarmTime
.Seconds
= 0U;
717 /* Alarm Day Settings : Day = 1st day of the month */
718 RTC_AlarmStruct
->AlarmDateWeekDaySel
= LL_RTC_ALMA_DATEWEEKDAYSEL_DATE
;
719 RTC_AlarmStruct
->AlarmDateWeekDay
= 1U;
721 /* Alarm Masks Settings : Mask = all fields are not masked */
722 RTC_AlarmStruct
->AlarmMask
= LL_RTC_ALMA_MASK_NONE
;
726 * @brief Set each @ref LL_RTC_AlarmTypeDef of ALARMB field to default value (Time = 00h:00mn:00sec /
727 * Day = 1st day of the month/Mask = all fields are masked).
728 * @param RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
731 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef
*RTC_AlarmStruct
)
733 /* Alarm Time Settings : Time = 00h:00mn:00sec */
734 RTC_AlarmStruct
->AlarmTime
.TimeFormat
= LL_RTC_ALMB_TIME_FORMAT_AM
;
735 RTC_AlarmStruct
->AlarmTime
.Hours
= 0U;
736 RTC_AlarmStruct
->AlarmTime
.Minutes
= 0U;
737 RTC_AlarmStruct
->AlarmTime
.Seconds
= 0U;
739 /* Alarm Day Settings : Day = 1st day of the month */
740 RTC_AlarmStruct
->AlarmDateWeekDaySel
= LL_RTC_ALMB_DATEWEEKDAYSEL_DATE
;
741 RTC_AlarmStruct
->AlarmDateWeekDay
= 1U;
743 /* Alarm Masks Settings : Mask = all fields are not masked */
744 RTC_AlarmStruct
->AlarmMask
= LL_RTC_ALMB_MASK_NONE
;
748 * @brief Enters the RTC Initialization mode.
749 * @note The RTC Initialization mode is write protected, use the
750 * @ref LL_RTC_DisableWriteProtection before calling this function.
751 * @param RTCx RTC Instance
752 * @retval An ErrorStatus enumeration value:
753 * - SUCCESS: RTC is in Init mode
754 * - ERROR: RTC is not in Init mode
756 ErrorStatus
LL_RTC_EnterInitMode(RTC_TypeDef
*RTCx
)
758 __IO
uint32_t timeout
= RTC_INITMODE_TIMEOUT
;
759 ErrorStatus status
= SUCCESS
;
762 /* Check the parameter */
763 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
765 /* Check if the Initialization mode is set */
766 if (LL_RTC_IsActiveFlag_INIT(RTCx
) == 0U)
768 /* Set the Initialization mode */
769 LL_RTC_EnableInitMode(RTCx
);
771 /* Wait till RTC is in INIT state and if Time out is reached exit */
772 tmp
= LL_RTC_IsActiveFlag_INIT(RTCx
);
773 while ((timeout
!= 0U) && (tmp
!= 1U))
775 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
779 tmp
= LL_RTC_IsActiveFlag_INIT(RTCx
);
790 * @brief Exit the RTC Initialization mode.
791 * @note When the initialization sequence is complete, the calendar restarts
792 * counting after 4 RTCCLK cycles.
793 * @note The RTC Initialization mode is write protected, use the
794 * @ref LL_RTC_DisableWriteProtection before calling this function.
795 * @param RTCx RTC Instance
796 * @retval An ErrorStatus enumeration value:
797 * - SUCCESS: RTC exited from in Init mode
798 * - ERROR: Not applicable
800 ErrorStatus
LL_RTC_ExitInitMode(RTC_TypeDef
*RTCx
)
802 /* Check the parameter */
803 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
805 /* Disable initialization mode */
806 LL_RTC_DisableInitMode(RTCx
);
812 * @brief Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
813 * synchronized with RTC APB clock.
814 * @note The RTC Resynchronization mode is write protected, use the
815 * @ref LL_RTC_DisableWriteProtection before calling this function.
816 * @note To read the calendar through the shadow registers after Calendar
817 * initialization, calendar update or after wakeup from low power modes
818 * the software must first clear the RSF flag.
819 * The software must then wait until it is set again before reading
820 * the calendar, which means that the calendar registers have been
821 * correctly copied into the RTC_TR and RTC_DR shadow registers.
822 * @param RTCx RTC Instance
823 * @retval An ErrorStatus enumeration value:
824 * - SUCCESS: RTC registers are synchronised
825 * - ERROR: RTC registers are not synchronised
827 ErrorStatus
LL_RTC_WaitForSynchro(RTC_TypeDef
*RTCx
)
829 __IO
uint32_t timeout
= RTC_SYNCHRO_TIMEOUT
;
830 ErrorStatus status
= SUCCESS
;
833 /* Check the parameter */
834 assert_param(IS_RTC_ALL_INSTANCE(RTCx
));
837 LL_RTC_ClearFlag_RS(RTCx
);
839 /* Wait the registers to be synchronised */
840 tmp
= LL_RTC_IsActiveFlag_RS(RTCx
);
841 while ((timeout
!= 0U) && (tmp
!= 0U))
843 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
847 tmp
= LL_RTC_IsActiveFlag_RS(RTCx
);
856 timeout
= RTC_SYNCHRO_TIMEOUT
;
857 tmp
= LL_RTC_IsActiveFlag_RS(RTCx
);
858 while ((timeout
!= 0U) && (tmp
!= 1U))
860 if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
864 tmp
= LL_RTC_IsActiveFlag_RS(RTCx
);
887 #endif /* defined(RTC) */
893 #endif /* USE_FULL_LL_DRIVER */
895 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/