1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_ll_rtc.c
4   * @author  MCD Application Team
5   * @brief   RTC LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32mp1xx_ll_rtc.h"
23 #include "stm32mp1xx_ll_cortex.h"
24 #ifdef  USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
29 
30 /** @addtogroup STM32MP1xx_LL_Driver
31   * @{
32   */
33 
34 #if defined(RTC)
35 
36 /** @addtogroup RTC_LL
37   * @{
38   */
39 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /** @addtogroup RTC_LL_Private_Constants
44   * @{
45   */
46 /* Default values used for prescaler */
47 #define RTC_ASYNCH_PRESC_DEFAULT     ((uint32_t) 0x0000007FU)
48 #define RTC_SYNCH_PRESC_DEFAULT      ((uint32_t) 0x000000FFU)
49 
50 /* Values used for timeout */
51 #define RTC_INITMODE_TIMEOUT         ((uint32_t) 1000U) /* 1s when tick set to 1ms */
52 #define RTC_SYNCHRO_TIMEOUT          ((uint32_t) 1000U) /* 1s when tick set to 1ms */
53 /**
54   * @}
55   */
56 
57 /* Private macros ------------------------------------------------------------*/
58 /** @addtogroup RTC_LL_Private_Macros
59   * @{
60   */
61 
62 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
63                                       || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
64 
65 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
66 
67 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x7FFFU)
68 
69 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
70                                   || ((__VALUE__) == LL_RTC_FORMAT_BCD))
71 
72 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
73                                        || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
74 
75 #define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
76 #define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
77 #define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
78 #define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
79 
80 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
81                                    || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
82                                    || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
83                                    || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
84                                    || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
85                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
86                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
87 
88 #define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= (uint32_t)1U) && ((__DAY__) <= (uint32_t)31U))
89 
90 #define IS_LL_RTC_MONTH(__VALUE__) (((__VALUE__) == LL_RTC_MONTH_JANUARY) \
91                                  || ((__VALUE__) == LL_RTC_MONTH_FEBRUARY) \
92                                  || ((__VALUE__) == LL_RTC_MONTH_MARCH) \
93                                  || ((__VALUE__) == LL_RTC_MONTH_APRIL) \
94                                  || ((__VALUE__) == LL_RTC_MONTH_MAY) \
95                                  || ((__VALUE__) == LL_RTC_MONTH_JUNE) \
96                                  || ((__VALUE__) == LL_RTC_MONTH_JULY) \
97                                  || ((__VALUE__) == LL_RTC_MONTH_AUGUST) \
98                                  || ((__VALUE__) == LL_RTC_MONTH_SEPTEMBER) \
99                                  || ((__VALUE__) == LL_RTC_MONTH_OCTOBER) \
100                                  || ((__VALUE__) == LL_RTC_MONTH_NOVEMBER) \
101                                  || ((__VALUE__) == LL_RTC_MONTH_DECEMBER))
102 
103 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
104 
105 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
106                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
107                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
108                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
109                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
110                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
111 
112 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
113                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
114                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
115                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
116                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
117                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
118 
119 
120 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
121                                                   ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
122 
123 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
124                                                   ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
125 
126 
127 /**
128   * @}
129   */
130 /* Private function prototypes -----------------------------------------------*/
131 /* Exported functions --------------------------------------------------------*/
132 /** @addtogroup RTC_LL_Exported_Functions
133   * @{
134   */
135 
136 /** @addtogroup RTC_LL_EF_Init
137   * @{
138   */
139 
140 /**
141   * @brief  De-Initializes the RTC registers to their default reset values.
142   * @note   This function does not reset the RTC Clock source and RTC Backup Data
143   *         registers.
144   * @param  RTCx RTC Instance
145   * @retval An ErrorStatus enumeration value:
146   *          - SUCCESS: RTC registers are de-initialized
147   *          - ERROR: RTC registers are not de-initialized
148   */
LL_RTC_DeInit(RTC_TypeDef * RTCx)149 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
150 {
151   ErrorStatus status = ERROR;
152 
153   /* Check the parameter */
154   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
155 
156   /* Disable the write protection for RTC registers */
157   LL_RTC_DisableWriteProtection(RTCx);
158 
159   /* Set Initialization mode */
160   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
161   {
162     /* Reset TR, DR and CR registers */
163     WRITE_REG(RTCx->TR,       0x00000000U);
164     WRITE_REG(RTCx->WUTR,     RTC_WUTR_WUT);
165     WRITE_REG(RTCx->DR, (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
166     /* Reset All CR bits except CR[2:0] */
167     WRITE_REG(RTCx->CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
168     WRITE_REG(RTCx->PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
169     WRITE_REG(RTCx->ALRMAR,   0x00000000U);
170     WRITE_REG(RTCx->ALRMBR,   0x00000000U);
171     WRITE_REG(RTCx->SHIFTR,   0x00000000U);
172     WRITE_REG(RTCx->CALR,     0x00000000U);
173     WRITE_REG(RTCx->ALRMASSR, 0x00000000U);
174     WRITE_REG(RTCx->ALRMBSSR, 0x00000000U);
175 
176     /* Exit Initialization mode */
177     LL_RTC_DisableInitMode(RTCx);
178 
179     /* Wait till the RTC RSF flag is set */
180     status = LL_RTC_WaitForSynchro(RTCx);
181   }
182 
183   /* Enable the write protection for RTC registers */
184   LL_RTC_EnableWriteProtection(RTCx);
185 
186   /* DeInitialization of the TAMP */
187   /* Reset TAMP CR1 and CR2 registers */
188   WRITE_REG(TAMP->CR1,      0xFFFF0000U);
189   WRITE_REG(TAMP->CR2,     0x00000000U);
190   WRITE_REG(TAMP->SMCR,     0x00000000U);
191   WRITE_REG(TAMP->FLTCR,    0x00000000U);
192   WRITE_REG(TAMP->ATCR1,    0x00000000U);
193   WRITE_REG(TAMP->IER,      0x00000000U);
194   WRITE_REG(TAMP->SCR,      0xFFFFFFFFU);
195   WRITE_REG(TAMP->CFGR,     0x00000000U);
196 
197   return status;
198 }
199 
200 /**
201   * @brief  Initializes the RTC registers according to the specified parameters
202   *         in RTC_InitStruct.
203   * @param  RTCx RTC Instance
204   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
205   *         the configuration information for the RTC peripheral.
206   * @note   The RTC Prescaler register is write protected and can be written in
207   *         initialization mode only.
208   * @retval An ErrorStatus enumeration value:
209   *          - SUCCESS: RTC registers are initialized
210   *          - ERROR: RTC registers are not initialized
211   */
LL_RTC_Init(RTC_TypeDef * RTCx,LL_RTC_InitTypeDef * RTC_InitStruct)212 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
213 {
214   ErrorStatus status = ERROR;
215 
216   /* Check the parameters */
217   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
218   assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
219   assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
220   assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
221 
222   /* Disable the write protection for RTC registers */
223   LL_RTC_DisableWriteProtection(RTCx);
224 
225   /* Set Initialization mode */
226   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
227   {
228     /* Set Hour Format */
229     LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
230 
231     /* Configure Synchronous and Asynchronous prescaler factor */
232     LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
233     LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
234 
235     /* Exit Initialization mode */
236     LL_RTC_DisableInitMode(RTCx);
237 
238     status = SUCCESS;
239   }
240   /* Enable the write protection for RTC registers */
241   LL_RTC_EnableWriteProtection(RTCx);
242 
243   return status;
244 }
245 
246 /**
247   * @brief  Set each @ref LL_RTC_InitTypeDef field to default value.
248   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
249   * @retval None
250   */
LL_RTC_StructInit(LL_RTC_InitTypeDef * RTC_InitStruct)251 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
252 {
253   /* Set RTC_InitStruct fields to default values */
254   RTC_InitStruct->HourFormat      = LL_RTC_HOURFORMAT_24HOUR;
255   RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
256   RTC_InitStruct->SynchPrescaler  = RTC_SYNCH_PRESC_DEFAULT;
257 }
258 
259 /**
260   * @brief  Set the RTC current time.
261   * @param  RTCx RTC Instance
262   * @param  RTC_Format This parameter can be one of the following values:
263   *         @arg @ref LL_RTC_FORMAT_BIN
264   *         @arg @ref LL_RTC_FORMAT_BCD
265   * @param  RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
266   *                        the time configuration information for the RTC.
267   * @retval An ErrorStatus enumeration value:
268   *          - SUCCESS: RTC Time register is configured
269   *          - ERROR: RTC Time register is not configured
270   */
LL_RTC_TIME_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_TimeTypeDef * RTC_TimeStruct)271 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
272 {
273   ErrorStatus status = ERROR;
274 
275   /* Check the parameters */
276   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
277   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
278 
279   if (RTC_Format == LL_RTC_FORMAT_BIN)
280   {
281     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
282     {
283       assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
284       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
285     }
286     else
287     {
288       RTC_TimeStruct->TimeFormat = 0x00U;
289       assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
290     }
291     assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
292     assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
293   }
294   else
295   {
296     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
297     {
298       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
299       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
300     }
301     else
302     {
303       RTC_TimeStruct->TimeFormat = 0x00U;
304       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
305     }
306     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
307     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
308   }
309 
310   /* Disable the write protection for RTC registers */
311   LL_RTC_DisableWriteProtection(RTCx);
312 
313   /* Set Initialization mode */
314   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
315   {
316     /* Check the input parameters format */
317     if (RTC_Format != LL_RTC_FORMAT_BIN)
318     {
319       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
320                          RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
321     }
322     else
323     {
324       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
325                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
326                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
327     }
328 
329     /* Exit Initialization mode */
330     LL_RTC_DisableInitMode(RTC);
331 
332     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
333     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
334     {
335       status = LL_RTC_WaitForSynchro(RTCx);
336     }
337     else
338     {
339       status = SUCCESS;
340     }
341   }
342   /* Enable the write protection for RTC registers */
343   LL_RTC_EnableWriteProtection(RTCx);
344 
345   return status;
346 }
347 
348 /**
349   * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
350   * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
351   * @retval None
352   */
LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef * RTC_TimeStruct)353 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
354 {
355   /* Time = 00h:00min:00sec */
356   RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
357   RTC_TimeStruct->Hours      = 0U;
358   RTC_TimeStruct->Minutes    = 0U;
359   RTC_TimeStruct->Seconds    = 0U;
360 }
361 
362 /**
363   * @brief  Set the RTC current date.
364   * @param  RTCx RTC Instance
365   * @param  RTC_Format This parameter can be one of the following values:
366   *         @arg @ref LL_RTC_FORMAT_BIN
367   *         @arg @ref LL_RTC_FORMAT_BCD
368   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
369   *                         the date configuration information for the RTC.
370   * @retval An ErrorStatus enumeration value:
371   *          - SUCCESS: RTC Day register is configured
372   *          - ERROR: RTC Day register is not configured
373   */
LL_RTC_DATE_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_DateTypeDef * RTC_DateStruct)374 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
375 {
376   ErrorStatus status = ERROR;
377 
378   /* Check the parameters */
379   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
380   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
381 
382   if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
383   {
384     RTC_DateStruct->Month = (uint8_t)((uint32_t) RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU;
385   }
386   if (RTC_Format == LL_RTC_FORMAT_BIN)
387   {
388     assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
389     assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
390     assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
391   }
392   else
393   {
394     assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
395     assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
396     assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
397   }
398   assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
399 
400   /* Disable the write protection for RTC registers */
401   LL_RTC_DisableWriteProtection(RTCx);
402 
403   /* Set Initialization mode */
404   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
405   {
406     /* Check the input parameters format */
407     if (RTC_Format != LL_RTC_FORMAT_BIN)
408     {
409       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
410     }
411     else
412     {
413       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
414                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
415     }
416 
417     /* Exit Initialization mode */
418     LL_RTC_DisableInitMode(RTC);
419 
420     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
421     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
422     {
423       status = LL_RTC_WaitForSynchro(RTCx);
424     }
425     else
426     {
427       status = SUCCESS;
428     }
429   }
430   /* Enable the write protection for RTC registers */
431   LL_RTC_EnableWriteProtection(RTCx);
432 
433   return status;
434 }
435 
436 /**
437   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
438   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
439   * @retval None
440   */
LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef * RTC_DateStruct)441 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
442 {
443   /* Monday, January 01 xx00 */
444   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
445   RTC_DateStruct->Day     = 1U;
446   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
447   RTC_DateStruct->Year    = 0U;
448 }
449 
450 /**
451   * @brief  Set the RTC Alarm A.
452   * @note   The Alarm register can only be written when the corresponding Alarm
453   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
454   * @param  RTCx RTC Instance
455   * @param  RTC_Format This parameter can be one of the following values:
456   *         @arg @ref LL_RTC_FORMAT_BIN
457   *         @arg @ref LL_RTC_FORMAT_BCD
458   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
459   *                         contains the alarm configuration parameters.
460   * @retval An ErrorStatus enumeration value:
461   *          - SUCCESS: ALARMA registers are configured
462   *          - ERROR: ALARMA registers are not configured
463   */
LL_RTC_ALMA_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)464 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
465 {
466   /* Check the parameters */
467   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
468   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
469   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
470   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
471 
472   if (RTC_Format == LL_RTC_FORMAT_BIN)
473   {
474     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
475     {
476       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
477       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
478     }
479     else
480     {
481       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
482       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
483     }
484     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
485     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
486 
487     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
488     {
489       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
490     }
491     else
492     {
493       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
494     }
495   }
496   else
497   {
498     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
499     {
500       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
501       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
502     }
503     else
504     {
505       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
506       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
507     }
508 
509     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
510     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
511 
512     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
513     {
514       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
515     }
516     else
517     {
518       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
519     }
520   }
521 
522   /* Disable the write protection for RTC registers */
523   LL_RTC_DisableWriteProtection(RTCx);
524 
525   /* Select weekday selection */
526   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
527   {
528     /* Set the date for ALARM */
529     LL_RTC_ALMA_DisableWeekday(RTCx);
530     if (RTC_Format != LL_RTC_FORMAT_BIN)
531     {
532       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
533     }
534     else
535     {
536       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
537     }
538   }
539   else
540   {
541     /* Set the week day for ALARM */
542     LL_RTC_ALMA_EnableWeekday(RTCx);
543     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
544   }
545 
546   /* Configure the Alarm register */
547   if (RTC_Format != LL_RTC_FORMAT_BIN)
548   {
549     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
550                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
551   }
552   else
553   {
554     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
555                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
556                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
557                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
558   }
559   /* Set ALARM mask */
560   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
561 
562   /* Enable the write protection for RTC registers */
563   LL_RTC_EnableWriteProtection(RTCx);
564 
565   return SUCCESS;
566 }
567 
568 /**
569   * @brief  Set the RTC Alarm B.
570   * @note   The Alarm register can only be written when the corresponding Alarm
571   *         is disabled (@ref LL_RTC_ALMB_Disable function).
572   * @param  RTCx RTC Instance
573   * @param  RTC_Format This parameter can be one of the following values:
574   *         @arg @ref LL_RTC_FORMAT_BIN
575   *         @arg @ref LL_RTC_FORMAT_BCD
576   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
577   *                         contains the alarm configuration parameters.
578   * @retval An ErrorStatus enumeration value:
579   *          - SUCCESS: ALARMB registers are configured
580   *          - ERROR: ALARMB registers are not configured
581   */
LL_RTC_ALMB_Init(RTC_TypeDef * RTCx,uint32_t RTC_Format,LL_RTC_AlarmTypeDef * RTC_AlarmStruct)582 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
583 {
584   /* Check the parameters */
585   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
586   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
587   assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask));
588   assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
589 
590   if (RTC_Format == LL_RTC_FORMAT_BIN)
591   {
592     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
593     {
594       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
595       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
596     }
597     else
598     {
599       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
600       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
601     }
602     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
603     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
604 
605     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
606     {
607       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
608     }
609     else
610     {
611       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
612     }
613   }
614   else
615   {
616     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
617     {
618       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
619       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
620     }
621     else
622     {
623       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
624       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
625     }
626 
627     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
628     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
629 
630     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
631     {
632       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
633     }
634     else
635     {
636       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
637     }
638   }
639 
640   /* Disable the write protection for RTC registers */
641   LL_RTC_DisableWriteProtection(RTCx);
642 
643   /* Select weekday selection */
644   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
645   {
646     /* Set the date for ALARM */
647     LL_RTC_ALMB_DisableWeekday(RTCx);
648     if (RTC_Format != LL_RTC_FORMAT_BIN)
649     {
650       LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
651     }
652     else
653     {
654       LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
655     }
656   }
657   else
658   {
659     /* Set the week day for ALARM */
660     LL_RTC_ALMB_EnableWeekday(RTCx);
661     LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
662   }
663 
664   /* Configure the Alarm register */
665   if (RTC_Format != LL_RTC_FORMAT_BIN)
666   {
667     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
668                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
669   }
670   else
671   {
672     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
673                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
674                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
675                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
676   }
677   /* Set ALARM mask */
678   LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
679 
680   /* Enable the write protection for RTC registers */
681   LL_RTC_EnableWriteProtection(RTCx);
682 
683   return SUCCESS;
684 }
685 
686 /**
687   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
688   *         Day = 1st day of the month/Mask = all fields are masked).
689   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
690   * @retval None
691   */
LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)692 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
693 {
694   /* Alarm Time Settings : Time = 00h:00mn:00sec */
695   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
696   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
697   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
698   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
699 
700   /* Alarm Day Settings : Day = 1st day of the month */
701   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
702   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
703 
704   /* Alarm Masks Settings : Mask =  all fields are not masked */
705   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
706 }
707 
708 /**
709   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
710   *         Day = 1st day of the month/Mask = all fields are masked).
711   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
712   * @retval None
713   */
LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef * RTC_AlarmStruct)714 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
715 {
716   /* Alarm Time Settings : Time = 00h:00mn:00sec */
717   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM;
718   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
719   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
720   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
721 
722   /* Alarm Day Settings : Day = 1st day of the month */
723   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE;
724   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
725 
726   /* Alarm Masks Settings : Mask =  all fields are not masked */
727   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMB_MASK_NONE;
728 }
729 
730 /**
731   * @brief  Enters the RTC Initialization mode.
732   * @note   The RTC Initialization mode is write protected, use the
733   *         @ref LL_RTC_DisableWriteProtection before calling this function.
734   * @param  RTCx RTC Instance
735   * @retval An ErrorStatus enumeration value:
736   *          - SUCCESS: RTC is in Init mode
737   *          - ERROR: RTC is not in Init mode
738   */
LL_RTC_EnterInitMode(RTC_TypeDef * RTCx)739 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
740 {
741   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
742   ErrorStatus status = SUCCESS;
743   uint32_t tmp;
744 
745   /* Check the parameter */
746   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
747 
748   /* Check if the Initialization mode is set */
749   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
750   {
751     /* Set the Initialization mode */
752     LL_RTC_EnableInitMode(RTCx);
753 
754     /* Wait till RTC is in INIT state and if Time out is reached exit */
755     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
756     while ((timeout != 0U) && (tmp != 1U))
757     {
758       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
759       {
760         timeout --;
761       }
762       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
763       if (timeout == 0U)
764       {
765         status = ERROR;
766       }
767     }
768   }
769   return status;
770 }
771 
772 /**
773   * @brief  Exit the RTC Initialization mode.
774   * @note   When the initialization sequence is complete, the calendar restarts
775   *         counting after 4 RTCCLK cycles.
776   * @note   The RTC Initialization mode is write protected, use the
777   *         @ref LL_RTC_DisableWriteProtection before calling this function.
778   * @param  RTCx RTC Instance
779   * @retval An ErrorStatus enumeration value:
780   *          - SUCCESS: RTC exited from in Init mode
781   *          - ERROR: Not applicable
782   */
LL_RTC_ExitInitMode(RTC_TypeDef * RTCx)783 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
784 {
785   /* Check the parameter */
786   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
787 
788   /* Disable initialization mode */
789   LL_RTC_DisableInitMode(RTCx);
790 
791   return SUCCESS;
792 }
793 
794 /**
795   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
796   *         synchronized with RTC APB clock.
797   * @note   The RTC Resynchronization mode is write protected, use the
798   *         @ref LL_RTC_DisableWriteProtection before calling this function.
799   * @note   To read the calendar through the shadow registers after Calendar
800   *         initialization, calendar update or after wakeup from low power modes
801   *         the software must first clear the RSF flag.
802   *         The software must then wait until it is set again before reading
803   *         the calendar, which means that the calendar registers have been
804   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
805   * @param  RTCx RTC Instance
806   * @retval An ErrorStatus enumeration value:
807   *          - SUCCESS: RTC registers are synchronised
808   *          - ERROR: RTC registers are not synchronised
809   */
LL_RTC_WaitForSynchro(RTC_TypeDef * RTCx)810 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
811 {
812   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
813   ErrorStatus status = SUCCESS;
814   uint32_t tmp;
815 
816   /* Check the parameter */
817   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
818 
819   /* Clear RSF flag */
820   LL_RTC_ClearFlag_RS(RTCx);
821 
822   /* Wait the registers to be synchronised */
823   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
824   while ((timeout != 0U) && (tmp != 0U))
825   {
826     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
827     {
828       timeout--;
829     }
830     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
831     if (timeout == 0U)
832     {
833       status = ERROR;
834     }
835   }
836 
837   if (status != ERROR)
838   {
839     timeout = RTC_SYNCHRO_TIMEOUT;
840     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
841     while ((timeout != 0U) && (tmp != 1U))
842     {
843       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
844       {
845         timeout--;
846       }
847       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
848       if (timeout == 0U)
849       {
850         status = ERROR;
851       }
852     }
853   }
854 
855   return (status);
856 }
857 
858 /**
859   * @}
860   */
861 
862 /**
863   * @}
864   */
865 
866 /**
867   * @}
868   */
869 
870 #endif /* defined(RTC) */
871 
872 /**
873   * @}
874   */
875 
876 #endif /* USE_FULL_LL_DRIVER */
877 
878 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
879