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