Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:10

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_ll_rtc.c
0004   * @author  MCD Application Team
0005   * @brief   RTC LL module driver.
0006   ******************************************************************************
0007   * @attention
0008   *
0009   * Copyright (c) 2017 STMicroelectronics.
0010   * All rights reserved.
0011   *
0012   * This software is licensed under terms that can be found in the LICENSE file
0013   * in the root directory of this software component.
0014   * If no LICENSE file comes with this software, it is provided AS-IS.
0015   *
0016   ******************************************************************************
0017   */
0018 #if defined(USE_FULL_LL_DRIVER) || defined(__rtems__)
0019 
0020 /* Includes ------------------------------------------------------------------*/
0021 #include "stm32h7xx_ll_rtc.h"
0022 #include "stm32h7xx_ll_cortex.h"
0023 #ifdef  USE_FULL_ASSERT
0024 #include "stm32_assert.h"
0025 #else
0026 #define assert_param(expr) ((void)0U)
0027 #endif
0028 
0029 /** @addtogroup STM32H7xx_LL_Driver
0030   * @{
0031   */
0032 
0033 #if defined(RTC)
0034 
0035 /** @addtogroup RTC_LL
0036   * @{
0037   */
0038 
0039 /* Private types -------------------------------------------------------------*/
0040 /* Private variables ---------------------------------------------------------*/
0041 /* Private constants ---------------------------------------------------------*/
0042 /** @addtogroup RTC_LL_Private_Constants
0043   * @{
0044   */
0045 /* Default values used for prescaler */
0046 #define RTC_ASYNCH_PRESC_DEFAULT     0x0000007FU
0047 #define RTC_SYNCH_PRESC_DEFAULT      0x000000FFU
0048 
0049 /* Values used for timeout */
0050 #define RTC_INITMODE_TIMEOUT         1000U /* 1s when tick set to 1ms */
0051 #define RTC_SYNCHRO_TIMEOUT          1000U /* 1s when tick set to 1ms */
0052 /**
0053   * @}
0054   */
0055 
0056 /* Private macros ------------------------------------------------------------*/
0057 /** @addtogroup RTC_LL_Private_Macros
0058   * @{
0059   */
0060 
0061 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
0062                                       || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
0063 
0064 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
0065 
0066 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x7FFFU)
0067 
0068 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
0069                                   || ((__VALUE__) == LL_RTC_FORMAT_BCD))
0070 
0071 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
0072                                        || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
0073 
0074 #define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
0075 #define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
0076 #define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
0077 #define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
0078 
0079 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
0080                                    || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
0081                                    || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
0082                                    || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
0083                                    || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
0084                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
0085                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
0086 
0087 #define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
0088 
0089 #define IS_LL_RTC_MONTH(__MONTH__) (((__MONTH__) >= 1U) && ((__MONTH__) <= 12U))
0090 
0091 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
0092 
0093 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
0094                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
0095                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
0096                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
0097                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
0098                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
0099 
0100 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
0101                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
0102                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
0103                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
0104                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
0105                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
0106 
0107 
0108 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
0109                                                   ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
0110 
0111 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
0112                                                   ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
0113 
0114 
0115 /**
0116   * @}
0117   */
0118 /* Private function prototypes -----------------------------------------------*/
0119 /* Exported functions --------------------------------------------------------*/
0120 /** @addtogroup RTC_LL_Exported_Functions
0121   * @{
0122   */
0123 
0124 /** @addtogroup RTC_LL_EF_Init
0125   * @{
0126   */
0127 
0128 /**
0129   * @brief  De-Initializes the RTC registers to their default reset values.
0130   * @note   This function does not reset the RTC Clock source and RTC Backup Data
0131   *         registers.
0132   * @param  RTCx RTC Instance
0133   * @retval An ErrorStatus enumeration value:
0134   *          - SUCCESS: RTC registers are de-initialized
0135   *          - ERROR: RTC registers are not de-initialized
0136   */
0137 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
0138 {
0139   ErrorStatus status = ERROR;
0140 
0141   /* Check the parameter */
0142   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0143 
0144   /* Disable the write protection for RTC registers */
0145   LL_RTC_DisableWriteProtection(RTCx);
0146 
0147   /* Set Initialization mode */
0148   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
0149   {
0150     /* Reset TR, DR and CR registers */
0151     LL_RTC_WriteReg(RTCx, TR,       0x00000000U);
0152     LL_RTC_WriteReg(RTCx, DR, (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
0153 
0154     /* Reset All CR bits except CR[2:0] */
0155     LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
0156 
0157     LL_RTC_WriteReg(RTCx, WUTR,     RTC_WUTR_WUT);
0158     LL_RTC_WriteReg(RTCx, PRER, (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
0159     LL_RTC_WriteReg(RTCx, ALRMAR,   0x00000000U);
0160     LL_RTC_WriteReg(RTCx, ALRMBR,   0x00000000U);
0161     LL_RTC_WriteReg(RTCx, SHIFTR,   0x00000000U);
0162     LL_RTC_WriteReg(RTCx, CALR,     0x00000000U);
0163     LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U);
0164     LL_RTC_WriteReg(RTCx, ALRMBSSR, 0x00000000U);
0165 
0166 #if defined(TAMP)
0167     /* Reset ICSR register and exit initialization mode */
0168     LL_RTC_WriteReg(RTCx, ICSR,   0x00000000U);
0169 #else
0170     /* Reset ISR register and exit initialization mode */
0171     LL_RTC_WriteReg(RTCx, ISR,    0x00000000U);
0172 
0173     /* Reset Tamper and alternate functions configuration register */
0174     LL_RTC_WriteReg(RTCx, TAMPCR, 0x00000000U);
0175 
0176     /* Reset Option register */
0177     LL_RTC_WriteReg(RTCx, OR,     0x00000000U);
0178 #endif /* TAMP */
0179 
0180     /* Wait till the RTC RSF flag is set */
0181     status = LL_RTC_WaitForSynchro(RTCx);
0182   }
0183 
0184   /* Enable the write protection for RTC registers */
0185   LL_RTC_EnableWriteProtection(RTCx);
0186 
0187 #if defined(TAMP)
0188   /* DeInitialization of the TAMP */
0189   LL_RTC_WriteReg(TAMP, CR1,      0xFFFF0000U);
0190   LL_RTC_WriteReg(TAMP, FLTCR,    0x00000000U);
0191   LL_RTC_WriteReg(TAMP, ATCR1,    0x00000000U);
0192   LL_RTC_WriteReg(TAMP, IER,      0x00000000U);
0193   LL_RTC_WriteReg(TAMP, SCR,      0xFFFFFFFFU);
0194 #endif /* TAMP */
0195 
0196   return status;
0197 }
0198 
0199 /**
0200   * @brief  Initializes the RTC registers according to the specified parameters
0201   *         in RTC_InitStruct.
0202   * @param  RTCx RTC Instance
0203   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
0204   *         the configuration information for the RTC peripheral.
0205   * @note   The RTC Prescaler register is write protected and can be written in
0206   *         initialization mode only.
0207   * @retval An ErrorStatus enumeration value:
0208   *          - SUCCESS: RTC registers are initialized
0209   *          - ERROR: RTC registers are not initialized
0210   */
0211 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
0212 {
0213   ErrorStatus status = ERROR;
0214 
0215   /* Check the parameters */
0216   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0217   assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
0218   assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
0219   assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
0220 
0221   /* Disable the write protection for RTC registers */
0222   LL_RTC_DisableWriteProtection(RTCx);
0223 
0224   /* Set Initialization mode */
0225   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
0226   {
0227     /* Set Hour Format */
0228     LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
0229 
0230     /* Configure Synchronous and Asynchronous prescaler factor */
0231     LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
0232     LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
0233 
0234     /* Exit Initialization mode */
0235     LL_RTC_DisableInitMode(RTCx);
0236 
0237     status = SUCCESS;
0238   }
0239   /* Enable the write protection for RTC registers */
0240   LL_RTC_EnableWriteProtection(RTCx);
0241 
0242   return status;
0243 }
0244 
0245 /**
0246   * @brief  Set each @ref LL_RTC_InitTypeDef field to default value.
0247   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
0248   * @retval None
0249   */
0250 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
0251 {
0252   /* Set RTC_InitStruct fields to default values */
0253   RTC_InitStruct->HourFormat      = LL_RTC_HOURFORMAT_24HOUR;
0254   RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
0255   RTC_InitStruct->SynchPrescaler  = RTC_SYNCH_PRESC_DEFAULT;
0256 }
0257 
0258 /**
0259   * @brief  Set the RTC current time.
0260   * @param  RTCx RTC Instance
0261   * @param  RTC_Format This parameter can be one of the following values:
0262   *         @arg @ref LL_RTC_FORMAT_BIN
0263   *         @arg @ref LL_RTC_FORMAT_BCD
0264   * @param  RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
0265   *                        the time configuration information for the RTC.
0266   * @retval An ErrorStatus enumeration value:
0267   *          - SUCCESS: RTC Time register is configured
0268   *          - ERROR: RTC Time register is not configured
0269   */
0270 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
0271 {
0272   ErrorStatus status = ERROR;
0273 
0274   /* Check the parameters */
0275   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0276   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
0277 
0278   if (RTC_Format == LL_RTC_FORMAT_BIN)
0279   {
0280     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0281     {
0282       assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
0283       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
0284     }
0285     else
0286     {
0287       RTC_TimeStruct->TimeFormat = 0x00U;
0288       assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
0289     }
0290     assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
0291     assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
0292   }
0293   else
0294   {
0295     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0296     {
0297       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
0298       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
0299     }
0300     else
0301     {
0302       RTC_TimeStruct->TimeFormat = 0x00U;
0303       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
0304     }
0305     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
0306     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
0307   }
0308 
0309   /* Disable the write protection for RTC registers */
0310   LL_RTC_DisableWriteProtection(RTCx);
0311 
0312   /* Set Initialization mode */
0313   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
0314   {
0315     /* Check the input parameters format */
0316     if (RTC_Format != LL_RTC_FORMAT_BIN)
0317     {
0318       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
0319                          RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
0320     }
0321     else
0322     {
0323       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
0324                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
0325                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
0326     }
0327 
0328     /* Exit Initialization mode */
0329     LL_RTC_DisableInitMode(RTCx);
0330 
0331     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
0332     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
0333     {
0334       status = LL_RTC_WaitForSynchro(RTCx);
0335     }
0336     else
0337     {
0338       status = SUCCESS;
0339     }
0340   }
0341   /* Enable the write protection for RTC registers */
0342   LL_RTC_EnableWriteProtection(RTCx);
0343 
0344   return status;
0345 }
0346 
0347 /**
0348   * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
0349   * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
0350   * @retval None
0351   */
0352 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
0353 {
0354   /* Time = 00h:00min:00sec */
0355   RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
0356   RTC_TimeStruct->Hours      = 0U;
0357   RTC_TimeStruct->Minutes    = 0U;
0358   RTC_TimeStruct->Seconds    = 0U;
0359 }
0360 
0361 /**
0362   * @brief  Set the RTC current date.
0363   * @param  RTCx RTC Instance
0364   * @param  RTC_Format This parameter can be one of the following values:
0365   *         @arg @ref LL_RTC_FORMAT_BIN
0366   *         @arg @ref LL_RTC_FORMAT_BCD
0367   * @param  RTC_DateStruct: pointer to a RTC_DateTypeDef structure that contains
0368   *                         the date configuration information for the RTC.
0369   * @retval An ErrorStatus enumeration value:
0370   *          - SUCCESS: RTC Day register is configured
0371   *          - ERROR: RTC Day register is not configured
0372   */
0373 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
0374 {
0375   ErrorStatus status = ERROR;
0376 
0377   /* Check the parameters */
0378   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0379   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
0380 
0381   if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
0382   {
0383     RTC_DateStruct->Month = (uint8_t)((RTC_DateStruct->Month & (uint8_t)~(0x10U)) + 0x0AU);
0384   }
0385   if (RTC_Format == LL_RTC_FORMAT_BIN)
0386   {
0387     assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
0388     assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
0389     assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
0390   }
0391   else
0392   {
0393     assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
0394     assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
0395     assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
0396   }
0397   assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
0398 
0399   /* Disable the write protection for RTC registers */
0400   LL_RTC_DisableWriteProtection(RTCx);
0401 
0402   /* Set Initialization mode */
0403   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
0404   {
0405     /* Check the input parameters format */
0406     if (RTC_Format != LL_RTC_FORMAT_BIN)
0407     {
0408       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
0409     }
0410     else
0411     {
0412       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
0413                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
0414     }
0415 
0416     /* Exit Initialization mode */
0417     LL_RTC_DisableInitMode(RTCx);
0418 
0419     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
0420     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
0421     {
0422       status = LL_RTC_WaitForSynchro(RTCx);
0423     }
0424     else
0425     {
0426       status = SUCCESS;
0427     }
0428   }
0429   /* Enable the write protection for RTC registers */
0430   LL_RTC_EnableWriteProtection(RTCx);
0431 
0432   return status;
0433 }
0434 
0435 /**
0436   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
0437   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
0438   * @retval None
0439   */
0440 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
0441 {
0442   /* Monday, January 01 xx00 */
0443   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
0444   RTC_DateStruct->Day     = 1U;
0445   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
0446   RTC_DateStruct->Year    = 0U;
0447 }
0448 
0449 /**
0450   * @brief  Set the RTC Alarm A.
0451   * @note   The Alarm register can only be written when the corresponding Alarm
0452   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
0453   * @param  RTCx RTC Instance
0454   * @param  RTC_Format This parameter can be one of the following values:
0455   *         @arg @ref LL_RTC_FORMAT_BIN
0456   *         @arg @ref LL_RTC_FORMAT_BCD
0457   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
0458   *                         contains the alarm configuration parameters.
0459   * @retval An ErrorStatus enumeration value:
0460   *          - SUCCESS: ALARMA registers are configured
0461   *          - ERROR: ALARMA registers are not configured
0462   */
0463 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
0464 {
0465   /* Check the parameters */
0466   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0467   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
0468   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
0469   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
0470 
0471   if (RTC_Format == LL_RTC_FORMAT_BIN)
0472   {
0473     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0474     {
0475       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
0476       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
0477     }
0478     else
0479     {
0480       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
0481       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
0482     }
0483     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
0484     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
0485 
0486     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
0487     {
0488       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
0489     }
0490     else
0491     {
0492       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
0493     }
0494   }
0495   else
0496   {
0497     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0498     {
0499       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
0500       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
0501     }
0502     else
0503     {
0504       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
0505       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
0506     }
0507 
0508     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
0509     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
0510 
0511     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
0512     {
0513       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
0514     }
0515     else
0516     {
0517       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
0518     }
0519   }
0520 
0521   /* Disable the write protection for RTC registers */
0522   LL_RTC_DisableWriteProtection(RTCx);
0523 
0524   /* Select weekday selection */
0525   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
0526   {
0527     /* Set the date for ALARM */
0528     LL_RTC_ALMA_DisableWeekday(RTCx);
0529     if (RTC_Format != LL_RTC_FORMAT_BIN)
0530     {
0531       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
0532     }
0533     else
0534     {
0535       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
0536     }
0537   }
0538   else
0539   {
0540     /* Set the week day for ALARM */
0541     LL_RTC_ALMA_EnableWeekday(RTCx);
0542     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
0543   }
0544 
0545   /* Configure the Alarm register */
0546   if (RTC_Format != LL_RTC_FORMAT_BIN)
0547   {
0548     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
0549                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
0550   }
0551   else
0552   {
0553     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
0554                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
0555                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
0556                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
0557   }
0558   /* Set ALARM mask */
0559   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
0560 
0561   /* Enable the write protection for RTC registers */
0562   LL_RTC_EnableWriteProtection(RTCx);
0563 
0564   return SUCCESS;
0565 }
0566 
0567 /**
0568   * @brief  Set the RTC Alarm B.
0569   * @note   The Alarm register can only be written when the corresponding Alarm
0570   *         is disabled (@ref LL_RTC_ALMB_Disable function).
0571   * @param  RTCx RTC Instance
0572   * @param  RTC_Format This parameter can be one of the following values:
0573   *         @arg @ref LL_RTC_FORMAT_BIN
0574   *         @arg @ref LL_RTC_FORMAT_BCD
0575   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
0576   *                         contains the alarm configuration parameters.
0577   * @retval An ErrorStatus enumeration value:
0578   *          - SUCCESS: ALARMB registers are configured
0579   *          - ERROR: ALARMB registers are not configured
0580   */
0581 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
0582 {
0583   /* Check the parameters */
0584   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0585   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
0586   assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask));
0587   assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
0588 
0589   if (RTC_Format == LL_RTC_FORMAT_BIN)
0590   {
0591     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0592     {
0593       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
0594       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
0595     }
0596     else
0597     {
0598       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
0599       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
0600     }
0601     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
0602     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
0603 
0604     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
0605     {
0606       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
0607     }
0608     else
0609     {
0610       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
0611     }
0612   }
0613   else
0614   {
0615     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
0616     {
0617       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
0618       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
0619     }
0620     else
0621     {
0622       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
0623       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
0624     }
0625 
0626     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
0627     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
0628 
0629     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
0630     {
0631       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
0632     }
0633     else
0634     {
0635       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
0636     }
0637   }
0638 
0639   /* Disable the write protection for RTC registers */
0640   LL_RTC_DisableWriteProtection(RTCx);
0641 
0642   /* Select weekday selection */
0643   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
0644   {
0645     /* Set the date for ALARM */
0646     LL_RTC_ALMB_DisableWeekday(RTCx);
0647     if (RTC_Format != LL_RTC_FORMAT_BIN)
0648     {
0649       LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
0650     }
0651     else
0652     {
0653       LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
0654     }
0655   }
0656   else
0657   {
0658     /* Set the week day for ALARM */
0659     LL_RTC_ALMB_EnableWeekday(RTCx);
0660     LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
0661   }
0662 
0663   /* Configure the Alarm register */
0664   if (RTC_Format != LL_RTC_FORMAT_BIN)
0665   {
0666     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
0667                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
0668   }
0669   else
0670   {
0671     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
0672                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
0673                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
0674                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
0675   }
0676   /* Set ALARM mask */
0677   LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
0678 
0679   /* Enable the write protection for RTC registers */
0680   LL_RTC_EnableWriteProtection(RTCx);
0681 
0682   return SUCCESS;
0683 }
0684 
0685 /**
0686   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
0687   *         Day = 1st day of the month/Mask = all fields are masked).
0688   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
0689   * @retval None
0690   */
0691 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
0692 {
0693   /* Alarm Time Settings : Time = 00h:00mn:00sec */
0694   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
0695   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
0696   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
0697   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
0698 
0699   /* Alarm Day Settings : Day = 1st day of the month */
0700   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
0701   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
0702 
0703   /* Alarm Masks Settings : Mask =  all fields are not masked */
0704   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
0705 }
0706 
0707 /**
0708   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
0709   *         Day = 1st day of the month/Mask = all fields are masked).
0710   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
0711   * @retval None
0712   */
0713 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
0714 {
0715   /* Alarm Time Settings : Time = 00h:00mn:00sec */
0716   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM;
0717   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
0718   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
0719   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
0720 
0721   /* Alarm Day Settings : Day = 1st day of the month */
0722   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE;
0723   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
0724 
0725   /* Alarm Masks Settings : Mask =  all fields are not masked */
0726   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMB_MASK_NONE;
0727 }
0728 
0729 /**
0730   * @brief  Enters the RTC Initialization mode.
0731   * @note   The RTC Initialization mode is write protected, use the
0732   *         @ref LL_RTC_DisableWriteProtection before calling this function.
0733   * @param  RTCx RTC Instance
0734   * @retval An ErrorStatus enumeration value:
0735   *          - SUCCESS: RTC is in Init mode
0736   *          - ERROR: RTC is not in Init mode
0737   */
0738 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
0739 {
0740   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
0741   ErrorStatus status = SUCCESS;
0742   uint32_t tmp;
0743 
0744   /* Check the parameter */
0745   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0746 
0747   /* Check if the Initialization mode is set */
0748   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
0749   {
0750     /* Set the Initialization mode */
0751     LL_RTC_EnableInitMode(RTCx);
0752 
0753     /* Wait till RTC is in INIT state and if Time out is reached exit */
0754     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
0755     while ((timeout != 0U) && (tmp != 1U))
0756     {
0757       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
0758       {
0759         timeout --;
0760       }
0761       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
0762       if (timeout == 0U)
0763       {
0764         status = ERROR;
0765       }
0766     }
0767   }
0768   return status;
0769 }
0770 
0771 /**
0772   * @brief  Exit the RTC Initialization mode.
0773   * @note   When the initialization sequence is complete, the calendar restarts
0774   *         counting after 4 RTCCLK cycles.
0775   * @note   The RTC Initialization mode is write protected, use the
0776   *         @ref LL_RTC_DisableWriteProtection before calling this function.
0777   * @param  RTCx RTC Instance
0778   * @retval An ErrorStatus enumeration value:
0779   *          - SUCCESS: RTC exited from in Init mode
0780   *          - ERROR: Not applicable
0781   */
0782 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
0783 {
0784   /* Check the parameter */
0785   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0786 
0787   /* Disable initialization mode */
0788   LL_RTC_DisableInitMode(RTCx);
0789 
0790   return SUCCESS;
0791 }
0792 
0793 /**
0794   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
0795   *         synchronized with RTC APB clock.
0796   * @note   The RTC Resynchronization mode is write protected, use the
0797   *         @ref LL_RTC_DisableWriteProtection before calling this function.
0798   * @note   To read the calendar through the shadow registers after Calendar
0799   *         initialization, calendar update or after wakeup from low power modes
0800   *         the software must first clear the RSF flag.
0801   *         The software must then wait until it is set again before reading
0802   *         the calendar, which means that the calendar registers have been
0803   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
0804   * @param  RTCx RTC Instance
0805   * @retval An ErrorStatus enumeration value:
0806   *          - SUCCESS: RTC registers are synchronised
0807   *          - ERROR: RTC registers are not synchronised
0808   */
0809 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
0810 {
0811   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
0812   ErrorStatus status = SUCCESS;
0813   uint32_t tmp;
0814 
0815   /* Check the parameter */
0816   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
0817 
0818   /* Clear RSF flag */
0819   LL_RTC_ClearFlag_RS(RTCx);
0820 
0821   /* Wait the registers to be synchronised */
0822   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
0823   while ((timeout != 0U) && (tmp != 0U))
0824   {
0825     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
0826     {
0827       timeout--;
0828     }
0829     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
0830     if (timeout == 0U)
0831     {
0832       status = ERROR;
0833     }
0834   }
0835 
0836   if (status != ERROR)
0837   {
0838     timeout = RTC_SYNCHRO_TIMEOUT;
0839     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
0840     while ((timeout != 0U) && (tmp != 1U))
0841     {
0842       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
0843       {
0844         timeout--;
0845       }
0846       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
0847       if (timeout == 0U)
0848       {
0849         status = ERROR;
0850       }
0851     }
0852   }
0853 
0854   return (status);
0855 }
0856 
0857 /**
0858   * @}
0859   */
0860 
0861 /**
0862   * @}
0863   */
0864 
0865 /**
0866   * @}
0867   */
0868 
0869 #endif /* defined(RTC) */
0870 
0871 /**
0872   * @}
0873   */
0874 
0875 #endif /* USE_FULL_LL_DRIVER */
0876