STM32F479xx HAL User Manual
stm32f4xx_ll_rtc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_ll_rtc.c
00004   * @author  MCD Application Team
00005   * @brief   RTC LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 #if defined(USE_FULL_LL_DRIVER)
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f4xx_ll_rtc.h"
00023 #include "stm32f4xx_ll_cortex.h"
00024 #ifdef  USE_FULL_ASSERT
00025 #include "stm32_assert.h"
00026 #else
00027 #define assert_param(expr) ((void)0U)
00028 #endif
00029 
00030 /** @addtogroup STM32F4xx_LL_Driver
00031   * @{
00032   */
00033 
00034 #if defined(RTC)
00035 
00036 /** @addtogroup RTC_LL
00037   * @{
00038   */
00039 
00040 /* Private types -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 /* Private constants ---------------------------------------------------------*/
00043 /** @addtogroup RTC_LL_Private_Constants
00044   * @{
00045   */
00046 /* Default values used for prescaler */
00047 #define RTC_ASYNCH_PRESC_DEFAULT     0x0000007FU
00048 #define RTC_SYNCH_PRESC_DEFAULT      0x000000FFU
00049 
00050 /* Values used for timeout */
00051 #define RTC_INITMODE_TIMEOUT         1000U /* 1s when tick set to 1ms */
00052 #define RTC_SYNCHRO_TIMEOUT          1000U /* 1s when tick set to 1ms */
00053 /**
00054   * @}
00055   */
00056 
00057 /* Private macros ------------------------------------------------------------*/
00058 /** @addtogroup RTC_LL_Private_Macros
00059   * @{
00060   */
00061 
00062 #define IS_LL_RTC_HOURFORMAT(__VALUE__) (((__VALUE__) == LL_RTC_HOURFORMAT_24HOUR) \
00063                                       || ((__VALUE__) == LL_RTC_HOURFORMAT_AMPM))
00064 
00065 #define IS_LL_RTC_ASYNCH_PREDIV(__VALUE__)   ((__VALUE__) <= 0x7FU)
00066 
00067 #define IS_LL_RTC_SYNCH_PREDIV(__VALUE__)    ((__VALUE__) <= 0x7FFFU)
00068 
00069 #define IS_LL_RTC_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_FORMAT_BIN) \
00070                                   || ((__VALUE__) == LL_RTC_FORMAT_BCD))
00071 
00072 #define IS_LL_RTC_TIME_FORMAT(__VALUE__) (((__VALUE__) == LL_RTC_TIME_FORMAT_AM_OR_24) \
00073                                        || ((__VALUE__) == LL_RTC_TIME_FORMAT_PM))
00074 
00075 #define IS_LL_RTC_HOUR12(__HOUR__)            (((__HOUR__) > 0U) && ((__HOUR__) <= 12U))
00076 #define IS_LL_RTC_HOUR24(__HOUR__)            ((__HOUR__) <= 23U)
00077 #define IS_LL_RTC_MINUTES(__MINUTES__)        ((__MINUTES__) <= 59U)
00078 #define IS_LL_RTC_SECONDS(__SECONDS__)        ((__SECONDS__) <= 59U)
00079 
00080 #define IS_LL_RTC_WEEKDAY(__VALUE__) (((__VALUE__) == LL_RTC_WEEKDAY_MONDAY) \
00081                                    || ((__VALUE__) == LL_RTC_WEEKDAY_TUESDAY) \
00082                                    || ((__VALUE__) == LL_RTC_WEEKDAY_WEDNESDAY) \
00083                                    || ((__VALUE__) == LL_RTC_WEEKDAY_THURSDAY) \
00084                                    || ((__VALUE__) == LL_RTC_WEEKDAY_FRIDAY) \
00085                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SATURDAY) \
00086                                    || ((__VALUE__) == LL_RTC_WEEKDAY_SUNDAY))
00087 
00088 #define IS_LL_RTC_DAY(__DAY__)    (((__DAY__) >= 1U) && ((__DAY__) <= 31U))
00089 
00090 #define IS_LL_RTC_MONTH(__MONTH__) (((__MONTH__) >= 1U) && ((__MONTH__) <= 12U))
00091 
00092 #define IS_LL_RTC_YEAR(__YEAR__) ((__YEAR__) <= 99U)
00093 
00094 #define IS_LL_RTC_ALMA_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMA_MASK_NONE) \
00095                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_DATEWEEKDAY) \
00096                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_HOURS) \
00097                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_MINUTES) \
00098                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_SECONDS) \
00099                                      || ((__VALUE__) == LL_RTC_ALMA_MASK_ALL))
00100 
00101 #define IS_LL_RTC_ALMB_MASK(__VALUE__) (((__VALUE__) == LL_RTC_ALMB_MASK_NONE) \
00102                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_DATEWEEKDAY) \
00103                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_HOURS) \
00104                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_MINUTES) \
00105                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_SECONDS) \
00106                                      || ((__VALUE__) == LL_RTC_ALMB_MASK_ALL))
00107 
00108 
00109 #define IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE) || \
00110                                                   ((__SEL__) == LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY))
00111 
00112 #define IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(__SEL__) (((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE) || \
00113                                                   ((__SEL__) == LL_RTC_ALMB_DATEWEEKDAYSEL_WEEKDAY))
00114 
00115 
00116 /**
00117   * @}
00118   */
00119 /* Private function prototypes -----------------------------------------------*/
00120 /* Exported functions --------------------------------------------------------*/
00121 /** @addtogroup RTC_LL_Exported_Functions
00122   * @{
00123   */
00124 
00125 /** @addtogroup RTC_LL_EF_Init
00126   * @{
00127   */
00128 
00129 /**
00130   * @brief  De-Initializes the RTC registers to their default reset values.
00131   * @note   This function doesn't reset the RTC Clock source and RTC Backup Data
00132   *         registers.
00133   * @param  RTCx RTC Instance
00134   * @retval An ErrorStatus enumeration value:
00135   *          - SUCCESS: RTC registers are de-initialized
00136   *          - ERROR: RTC registers are not de-initialized
00137   */
00138 ErrorStatus LL_RTC_DeInit(RTC_TypeDef *RTCx)
00139 {
00140   ErrorStatus status = ERROR;
00141 
00142   /* Check the parameter */
00143   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00144 
00145   /* Disable the write protection for RTC registers */
00146   LL_RTC_DisableWriteProtection(RTCx);
00147 
00148   /* Set Initialization mode */
00149   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
00150   {
00151     /* Reset TR, DR and CR registers */
00152     LL_RTC_WriteReg(RTCx, TR,       0x00000000U);
00153 #if defined(RTC_WAKEUP_SUPPORT)
00154     LL_RTC_WriteReg(RTCx, WUTR,     RTC_WUTR_WUT);
00155 #endif /* RTC_WAKEUP_SUPPORT */
00156     LL_RTC_WriteReg(RTCx, DR  ,     (RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
00157     /* Reset All CR bits except CR[2:0] */
00158 #if defined(RTC_WAKEUP_SUPPORT)
00159     LL_RTC_WriteReg(RTCx, CR, (LL_RTC_ReadReg(RTCx, CR) & RTC_CR_WUCKSEL));
00160 #else
00161     LL_RTC_WriteReg(RTCx, CR, 0x00000000U);
00162 #endif /* RTC_WAKEUP_SUPPORT */
00163     LL_RTC_WriteReg(RTCx, PRER,     (RTC_PRER_PREDIV_A | RTC_SYNCH_PRESC_DEFAULT));
00164     LL_RTC_WriteReg(RTCx, ALRMAR,   0x00000000U);
00165     LL_RTC_WriteReg(RTCx, ALRMBR,   0x00000000U);
00166     LL_RTC_WriteReg(RTCx, SHIFTR,   0x00000000U);
00167     LL_RTC_WriteReg(RTCx, CALR,     0x00000000U);
00168     LL_RTC_WriteReg(RTCx, ALRMASSR, 0x00000000U);
00169     LL_RTC_WriteReg(RTCx, ALRMBSSR, 0x00000000U);
00170 
00171     /* Reset ISR register and exit initialization mode */
00172     LL_RTC_WriteReg(RTCx, ISR,      0x00000000U);
00173 
00174     /* Reset Tamper and alternate functions configuration register */
00175     LL_RTC_WriteReg(RTCx, TAFCR, 0x00000000U);
00176 
00177     /* Wait till the RTC RSF flag is set */
00178     status = LL_RTC_WaitForSynchro(RTCx);
00179   }
00180 
00181   /* Enable the write protection for RTC registers */
00182   LL_RTC_EnableWriteProtection(RTCx);
00183 
00184   return status;
00185 }
00186 
00187 /**
00188   * @brief  Initializes the RTC registers according to the specified parameters
00189   *         in RTC_InitStruct.
00190   * @param  RTCx RTC Instance
00191   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure that contains
00192   *         the configuration information for the RTC peripheral.
00193   * @note   The RTC Prescaler register is write protected and can be written in
00194   *         initialization mode only.
00195   * @retval An ErrorStatus enumeration value:
00196   *          - SUCCESS: RTC registers are initialized
00197   *          - ERROR: RTC registers are not initialized
00198   */
00199 ErrorStatus LL_RTC_Init(RTC_TypeDef *RTCx, LL_RTC_InitTypeDef *RTC_InitStruct)
00200 {
00201   ErrorStatus status = ERROR;
00202 
00203   /* Check the parameters */
00204   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00205   assert_param(IS_LL_RTC_HOURFORMAT(RTC_InitStruct->HourFormat));
00206   assert_param(IS_LL_RTC_ASYNCH_PREDIV(RTC_InitStruct->AsynchPrescaler));
00207   assert_param(IS_LL_RTC_SYNCH_PREDIV(RTC_InitStruct->SynchPrescaler));
00208 
00209   /* Disable the write protection for RTC registers */
00210   LL_RTC_DisableWriteProtection(RTCx);
00211 
00212   /* Set Initialization mode */
00213   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
00214   {
00215     /* Set Hour Format */
00216     LL_RTC_SetHourFormat(RTCx, RTC_InitStruct->HourFormat);
00217 
00218     /* Configure Synchronous and Asynchronous prescaler factor */
00219     LL_RTC_SetSynchPrescaler(RTCx, RTC_InitStruct->SynchPrescaler);
00220     LL_RTC_SetAsynchPrescaler(RTCx, RTC_InitStruct->AsynchPrescaler);
00221 
00222     /* Exit Initialization mode */
00223     LL_RTC_DisableInitMode(RTCx);
00224 
00225     status = SUCCESS;
00226   }
00227   /* Enable the write protection for RTC registers */
00228   LL_RTC_EnableWriteProtection(RTCx);
00229 
00230   return status;
00231 }
00232 
00233 /**
00234   * @brief  Set each @ref LL_RTC_InitTypeDef field to default value.
00235   * @param  RTC_InitStruct pointer to a @ref LL_RTC_InitTypeDef structure which will be initialized.
00236   * @retval None
00237   */
00238 void LL_RTC_StructInit(LL_RTC_InitTypeDef *RTC_InitStruct)
00239 {
00240   /* Set RTC_InitStruct fields to default values */
00241   RTC_InitStruct->HourFormat      = LL_RTC_HOURFORMAT_24HOUR;
00242   RTC_InitStruct->AsynchPrescaler = RTC_ASYNCH_PRESC_DEFAULT;
00243   RTC_InitStruct->SynchPrescaler  = RTC_SYNCH_PRESC_DEFAULT;
00244 }
00245 
00246 /**
00247   * @brief  Set the RTC current time.
00248   * @param  RTCx RTC Instance
00249   * @param  RTC_Format This parameter can be one of the following values:
00250   *         @arg @ref LL_RTC_FORMAT_BIN
00251   *         @arg @ref LL_RTC_FORMAT_BCD
00252   * @param  RTC_TimeStruct pointer to a RTC_TimeTypeDef structure that contains
00253   *                        the time configuration information for the RTC.
00254   * @retval An ErrorStatus enumeration value:
00255   *          - SUCCESS: RTC Time register is configured
00256   *          - ERROR: RTC Time register is not configured
00257   */
00258 ErrorStatus LL_RTC_TIME_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_TimeTypeDef *RTC_TimeStruct)
00259 {
00260   ErrorStatus status = ERROR;
00261 
00262   /* Check the parameters */
00263   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00264   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
00265 
00266   if (RTC_Format == LL_RTC_FORMAT_BIN)
00267   {
00268     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00269     {
00270       assert_param(IS_LL_RTC_HOUR12(RTC_TimeStruct->Hours));
00271       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
00272     }
00273     else
00274     {
00275       RTC_TimeStruct->TimeFormat = 0x00U;
00276       assert_param(IS_LL_RTC_HOUR24(RTC_TimeStruct->Hours));
00277     }
00278     assert_param(IS_LL_RTC_MINUTES(RTC_TimeStruct->Minutes));
00279     assert_param(IS_LL_RTC_SECONDS(RTC_TimeStruct->Seconds));
00280   }
00281   else
00282   {
00283     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00284     {
00285       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
00286       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_TimeStruct->TimeFormat));
00287     }
00288     else
00289     {
00290       RTC_TimeStruct->TimeFormat = 0x00U;
00291       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Hours)));
00292     }
00293     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Minutes)));
00294     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_TimeStruct->Seconds)));
00295   }
00296 
00297   /* Disable the write protection for RTC registers */
00298   LL_RTC_DisableWriteProtection(RTCx);
00299 
00300   /* Set Initialization mode */
00301   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
00302   {
00303     /* Check the input parameters format */
00304     if (RTC_Format != LL_RTC_FORMAT_BIN)
00305     {
00306       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, RTC_TimeStruct->Hours,
00307                          RTC_TimeStruct->Minutes, RTC_TimeStruct->Seconds);
00308     }
00309     else
00310     {
00311       LL_RTC_TIME_Config(RTCx, RTC_TimeStruct->TimeFormat, __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Hours),
00312                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Minutes),
00313                          __LL_RTC_CONVERT_BIN2BCD(RTC_TimeStruct->Seconds));
00314     }
00315 
00316     /* Exit Initialization mode */
00317     LL_RTC_DisableInitMode(RTCx);
00318 
00319     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
00320     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
00321     {
00322       status = LL_RTC_WaitForSynchro(RTCx);
00323     }
00324     else
00325     {
00326       status = SUCCESS;
00327     }
00328   }
00329   /* Enable the write protection for RTC registers */
00330   LL_RTC_EnableWriteProtection(RTCx);
00331 
00332   return status;
00333 }
00334 
00335 /**
00336   * @brief  Set each @ref LL_RTC_TimeTypeDef field to default value (Time = 00h:00min:00sec).
00337   * @param  RTC_TimeStruct pointer to a @ref LL_RTC_TimeTypeDef structure which will be initialized.
00338   * @retval None
00339   */
00340 void LL_RTC_TIME_StructInit(LL_RTC_TimeTypeDef *RTC_TimeStruct)
00341 {
00342   /* Time = 00h:00min:00sec */
00343   RTC_TimeStruct->TimeFormat = LL_RTC_TIME_FORMAT_AM_OR_24;
00344   RTC_TimeStruct->Hours      = 0U;
00345   RTC_TimeStruct->Minutes    = 0U;
00346   RTC_TimeStruct->Seconds    = 0U;
00347 }
00348 
00349 /**
00350   * @brief  Set the RTC current date.
00351   * @param  RTCx RTC Instance
00352   * @param  RTC_Format This parameter can be one of the following values:
00353   *         @arg @ref LL_RTC_FORMAT_BIN
00354   *         @arg @ref LL_RTC_FORMAT_BCD
00355   * @param  RTC_DateStruct pointer to a RTC_DateTypeDef structure that contains
00356   *                         the date configuration information for the RTC.
00357   * @retval An ErrorStatus enumeration value:
00358   *          - SUCCESS: RTC Day register is configured
00359   *          - ERROR: RTC Day register is not configured
00360   */
00361 ErrorStatus LL_RTC_DATE_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_DateTypeDef *RTC_DateStruct)
00362 {
00363   ErrorStatus status = ERROR;
00364 
00365   /* Check the parameters */
00366   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00367   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
00368 
00369   if ((RTC_Format == LL_RTC_FORMAT_BIN) && ((RTC_DateStruct->Month & 0x10U) == 0x10U))
00370   {
00371     RTC_DateStruct->Month = (RTC_DateStruct->Month & (uint32_t)~(0x10U)) + 0x0AU;
00372   }
00373   if (RTC_Format == LL_RTC_FORMAT_BIN)
00374   {
00375     assert_param(IS_LL_RTC_YEAR(RTC_DateStruct->Year));
00376     assert_param(IS_LL_RTC_MONTH(RTC_DateStruct->Month));
00377     assert_param(IS_LL_RTC_DAY(RTC_DateStruct->Day));
00378   }
00379   else
00380   {
00381     assert_param(IS_LL_RTC_YEAR(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Year)));
00382     assert_param(IS_LL_RTC_MONTH(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Month)));
00383     assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_DateStruct->Day)));
00384   }
00385   assert_param(IS_LL_RTC_WEEKDAY(RTC_DateStruct->WeekDay));
00386 
00387   /* Disable the write protection for RTC registers */
00388   LL_RTC_DisableWriteProtection(RTCx);
00389 
00390   /* Set Initialization mode */
00391   if (LL_RTC_EnterInitMode(RTCx) != ERROR)
00392   {
00393     /* Check the input parameters format */
00394     if (RTC_Format != LL_RTC_FORMAT_BIN)
00395     {
00396       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, RTC_DateStruct->Day, RTC_DateStruct->Month, RTC_DateStruct->Year);
00397     }
00398     else
00399     {
00400       LL_RTC_DATE_Config(RTCx, RTC_DateStruct->WeekDay, __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Day),
00401                          __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Month), __LL_RTC_CONVERT_BIN2BCD(RTC_DateStruct->Year));
00402     }
00403 
00404     /* Exit Initialization mode */
00405     LL_RTC_DisableInitMode(RTCx);
00406 
00407     /* If  RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
00408     if (LL_RTC_IsShadowRegBypassEnabled(RTCx) == 0U)
00409     {
00410       status = LL_RTC_WaitForSynchro(RTCx);
00411     }
00412     else
00413     {
00414       status = SUCCESS;
00415     }
00416   }
00417   /* Enable the write protection for RTC registers */
00418   LL_RTC_EnableWriteProtection(RTCx);
00419 
00420   return status;
00421 }
00422 
00423 /**
00424   * @brief  Set each @ref LL_RTC_DateTypeDef field to default value (date = Monday, January 01 xx00)
00425   * @param  RTC_DateStruct pointer to a @ref LL_RTC_DateTypeDef structure which will be initialized.
00426   * @retval None
00427   */
00428 void LL_RTC_DATE_StructInit(LL_RTC_DateTypeDef *RTC_DateStruct)
00429 {
00430   /* Monday, January 01 xx00 */
00431   RTC_DateStruct->WeekDay = LL_RTC_WEEKDAY_MONDAY;
00432   RTC_DateStruct->Day     = 1U;
00433   RTC_DateStruct->Month   = LL_RTC_MONTH_JANUARY;
00434   RTC_DateStruct->Year    = 0U;
00435 }
00436 
00437 /**
00438   * @brief  Set the RTC Alarm A.
00439   * @note   The Alarm register can only be written when the corresponding Alarm
00440   *         is disabled (Use @ref LL_RTC_ALMA_Disable function).
00441   * @param  RTCx RTC Instance
00442   * @param  RTC_Format This parameter can be one of the following values:
00443   *         @arg @ref LL_RTC_FORMAT_BIN
00444   *         @arg @ref LL_RTC_FORMAT_BCD
00445   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
00446   *                         contains the alarm configuration parameters.
00447   * @retval An ErrorStatus enumeration value:
00448   *          - SUCCESS: ALARMA registers are configured
00449   *          - ERROR: ALARMA registers are not configured
00450   */
00451 ErrorStatus LL_RTC_ALMA_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
00452 {
00453   /* Check the parameters */
00454   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00455   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
00456   assert_param(IS_LL_RTC_ALMA_MASK(RTC_AlarmStruct->AlarmMask));
00457   assert_param(IS_LL_RTC_ALMA_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
00458 
00459   if (RTC_Format == LL_RTC_FORMAT_BIN)
00460   {
00461     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00462     {
00463       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
00464       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
00465     }
00466     else
00467     {
00468       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
00469       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
00470     }
00471     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
00472     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
00473 
00474     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
00475     {
00476       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
00477     }
00478     else
00479     {
00480       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
00481     }
00482   }
00483   else
00484   {
00485     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00486     {
00487       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
00488       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
00489     }
00490     else
00491     {
00492       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
00493       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
00494     }
00495 
00496     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
00497     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
00498 
00499     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
00500     {
00501       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
00502     }
00503     else
00504     {
00505       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
00506     }
00507   }
00508 
00509   /* Disable the write protection for RTC registers */
00510   LL_RTC_DisableWriteProtection(RTCx);
00511 
00512   /* Select weekday selection */
00513   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMA_DATEWEEKDAYSEL_DATE)
00514   {
00515     /* Set the date for ALARM */
00516     LL_RTC_ALMA_DisableWeekday(RTCx);
00517     if (RTC_Format != LL_RTC_FORMAT_BIN)
00518     {
00519       LL_RTC_ALMA_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
00520     }
00521     else
00522     {
00523       LL_RTC_ALMA_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
00524     }
00525   }
00526   else
00527   {
00528     /* Set the week day for ALARM */
00529     LL_RTC_ALMA_EnableWeekday(RTCx);
00530     LL_RTC_ALMA_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
00531   }
00532 
00533   /* Configure the Alarm register */
00534   if (RTC_Format != LL_RTC_FORMAT_BIN)
00535   {
00536     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
00537                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
00538   }
00539   else
00540   {
00541     LL_RTC_ALMA_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
00542                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
00543                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
00544                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
00545   }
00546   /* Set ALARM mask */
00547   LL_RTC_ALMA_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
00548 
00549   /* Enable the write protection for RTC registers */
00550   LL_RTC_EnableWriteProtection(RTCx);
00551 
00552   return SUCCESS;
00553 }
00554 
00555 /**
00556   * @brief  Set the RTC Alarm B.
00557   * @note   The Alarm register can only be written when the corresponding Alarm
00558   *         is disabled (@ref LL_RTC_ALMB_Disable function).
00559   * @param  RTCx RTC Instance
00560   * @param  RTC_Format This parameter can be one of the following values:
00561   *         @arg @ref LL_RTC_FORMAT_BIN
00562   *         @arg @ref LL_RTC_FORMAT_BCD
00563   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure that
00564   *                         contains the alarm configuration parameters.
00565   * @retval An ErrorStatus enumeration value:
00566   *          - SUCCESS: ALARMB registers are configured
00567   *          - ERROR: ALARMB registers are not configured
00568   */
00569 ErrorStatus LL_RTC_ALMB_Init(RTC_TypeDef *RTCx, uint32_t RTC_Format, LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
00570 {
00571   /* Check the parameters */
00572   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00573   assert_param(IS_LL_RTC_FORMAT(RTC_Format));
00574   assert_param(IS_LL_RTC_ALMB_MASK(RTC_AlarmStruct->AlarmMask));
00575   assert_param(IS_LL_RTC_ALMB_DATE_WEEKDAY_SEL(RTC_AlarmStruct->AlarmDateWeekDaySel));
00576 
00577   if (RTC_Format == LL_RTC_FORMAT_BIN)
00578   {
00579     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00580     {
00581       assert_param(IS_LL_RTC_HOUR12(RTC_AlarmStruct->AlarmTime.Hours));
00582       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
00583     }
00584     else
00585     {
00586       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
00587       assert_param(IS_LL_RTC_HOUR24(RTC_AlarmStruct->AlarmTime.Hours));
00588     }
00589     assert_param(IS_LL_RTC_MINUTES(RTC_AlarmStruct->AlarmTime.Minutes));
00590     assert_param(IS_LL_RTC_SECONDS(RTC_AlarmStruct->AlarmTime.Seconds));
00591 
00592     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
00593     {
00594       assert_param(IS_LL_RTC_DAY(RTC_AlarmStruct->AlarmDateWeekDay));
00595     }
00596     else
00597     {
00598       assert_param(IS_LL_RTC_WEEKDAY(RTC_AlarmStruct->AlarmDateWeekDay));
00599     }
00600   }
00601   else
00602   {
00603     if (LL_RTC_GetHourFormat(RTCx) != LL_RTC_HOURFORMAT_24HOUR)
00604     {
00605       assert_param(IS_LL_RTC_HOUR12(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
00606       assert_param(IS_LL_RTC_TIME_FORMAT(RTC_AlarmStruct->AlarmTime.TimeFormat));
00607     }
00608     else
00609     {
00610       RTC_AlarmStruct->AlarmTime.TimeFormat = 0x00U;
00611       assert_param(IS_LL_RTC_HOUR24(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Hours)));
00612     }
00613 
00614     assert_param(IS_LL_RTC_MINUTES(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Minutes)));
00615     assert_param(IS_LL_RTC_SECONDS(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmTime.Seconds)));
00616 
00617     if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
00618     {
00619       assert_param(IS_LL_RTC_DAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
00620     }
00621     else
00622     {
00623       assert_param(IS_LL_RTC_WEEKDAY(__LL_RTC_CONVERT_BCD2BIN(RTC_AlarmStruct->AlarmDateWeekDay)));
00624     }
00625   }
00626 
00627   /* Disable the write protection for RTC registers */
00628   LL_RTC_DisableWriteProtection(RTCx);
00629 
00630   /* Select weekday selection */
00631   if (RTC_AlarmStruct->AlarmDateWeekDaySel == LL_RTC_ALMB_DATEWEEKDAYSEL_DATE)
00632   {
00633     /* Set the date for ALARM */
00634     LL_RTC_ALMB_DisableWeekday(RTCx);
00635     if (RTC_Format != LL_RTC_FORMAT_BIN)
00636     {
00637       LL_RTC_ALMB_SetDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
00638     }
00639     else
00640     {
00641       LL_RTC_ALMB_SetDay(RTCx, __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmDateWeekDay));
00642     }
00643   }
00644   else
00645   {
00646     /* Set the week day for ALARM */
00647     LL_RTC_ALMB_EnableWeekday(RTCx);
00648     LL_RTC_ALMB_SetWeekDay(RTCx, RTC_AlarmStruct->AlarmDateWeekDay);
00649   }
00650 
00651   /* Configure the Alarm register */
00652   if (RTC_Format != LL_RTC_FORMAT_BIN)
00653   {
00654     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat, RTC_AlarmStruct->AlarmTime.Hours,
00655                            RTC_AlarmStruct->AlarmTime.Minutes, RTC_AlarmStruct->AlarmTime.Seconds);
00656   }
00657   else
00658   {
00659     LL_RTC_ALMB_ConfigTime(RTCx, RTC_AlarmStruct->AlarmTime.TimeFormat,
00660                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Hours),
00661                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Minutes),
00662                            __LL_RTC_CONVERT_BIN2BCD(RTC_AlarmStruct->AlarmTime.Seconds));
00663   }
00664   /* Set ALARM mask */
00665   LL_RTC_ALMB_SetMask(RTCx, RTC_AlarmStruct->AlarmMask);
00666 
00667   /* Enable the write protection for RTC registers */
00668   LL_RTC_EnableWriteProtection(RTCx);
00669 
00670   return SUCCESS;
00671 }
00672 
00673 /**
00674   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
00675   *         Day = 1st day of the month/Mask = all fields are masked).
00676   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
00677   * @retval None
00678   */
00679 void LL_RTC_ALMA_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
00680 {
00681   /* Alarm Time Settings : Time = 00h:00mn:00sec */
00682   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMA_TIME_FORMAT_AM;
00683   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
00684   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
00685   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
00686 
00687   /* Alarm Day Settings : Day = 1st day of the month */
00688   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMA_DATEWEEKDAYSEL_DATE;
00689   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
00690 
00691   /* Alarm Masks Settings : Mask =  all fields are not masked */
00692   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMA_MASK_NONE;
00693 }
00694 
00695 /**
00696   * @brief  Set each @ref LL_RTC_AlarmTypeDef of ALARMA field to default value (Time = 00h:00mn:00sec /
00697   *         Day = 1st day of the month/Mask = all fields are masked).
00698   * @param  RTC_AlarmStruct pointer to a @ref LL_RTC_AlarmTypeDef structure which will be initialized.
00699   * @retval None
00700   */
00701 void LL_RTC_ALMB_StructInit(LL_RTC_AlarmTypeDef *RTC_AlarmStruct)
00702 {
00703   /* Alarm Time Settings : Time = 00h:00mn:00sec */
00704   RTC_AlarmStruct->AlarmTime.TimeFormat = LL_RTC_ALMB_TIME_FORMAT_AM;
00705   RTC_AlarmStruct->AlarmTime.Hours      = 0U;
00706   RTC_AlarmStruct->AlarmTime.Minutes    = 0U;
00707   RTC_AlarmStruct->AlarmTime.Seconds    = 0U;
00708 
00709   /* Alarm Day Settings : Day = 1st day of the month */
00710   RTC_AlarmStruct->AlarmDateWeekDaySel = LL_RTC_ALMB_DATEWEEKDAYSEL_DATE;
00711   RTC_AlarmStruct->AlarmDateWeekDay    = 1U;
00712 
00713   /* Alarm Masks Settings : Mask =  all fields are not masked */
00714   RTC_AlarmStruct->AlarmMask           = LL_RTC_ALMB_MASK_NONE;
00715 }
00716 
00717 /**
00718   * @brief  Enters the RTC Initialization mode.
00719   * @note   The RTC Initialization mode is write protected, use the
00720   *         @ref LL_RTC_DisableWriteProtection before calling this function.
00721   * @param  RTCx RTC Instance
00722   * @retval An ErrorStatus enumeration value:
00723   *          - SUCCESS: RTC is in Init mode
00724   *          - ERROR: RTC is not in Init mode
00725   */
00726 ErrorStatus LL_RTC_EnterInitMode(RTC_TypeDef *RTCx)
00727 {
00728   __IO uint32_t timeout = RTC_INITMODE_TIMEOUT;
00729   ErrorStatus status = SUCCESS;
00730   uint32_t tmp = 0U;
00731 
00732   /* Check the parameter */
00733   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00734 
00735   /* Check if the Initialization mode is set */
00736   if (LL_RTC_IsActiveFlag_INIT(RTCx) == 0U)
00737   {
00738     /* Set the Initialization mode */
00739     LL_RTC_EnableInitMode(RTCx);
00740 
00741     /* Wait till RTC is in INIT state and if Time out is reached exit */
00742     tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
00743     while ((timeout != 0U) && (tmp != 1U))
00744     {
00745       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
00746       {
00747         timeout --;
00748       }
00749       tmp = LL_RTC_IsActiveFlag_INIT(RTCx);
00750       if (timeout == 0U)
00751       {
00752         status = ERROR;
00753       }
00754     }
00755   }
00756   return status;
00757 }
00758 
00759 /**
00760   * @brief  Exit the RTC Initialization mode.
00761   * @note   When the initialization sequence is complete, the calendar restarts
00762   *         counting after 4 RTCCLK cycles.
00763   * @note   The RTC Initialization mode is write protected, use the
00764   *         @ref LL_RTC_DisableWriteProtection before calling this function.
00765   * @param  RTCx RTC Instance
00766   * @retval An ErrorStatus enumeration value:
00767   *          - SUCCESS: RTC exited from in Init mode
00768   *          - ERROR: Not applicable
00769   */
00770 ErrorStatus LL_RTC_ExitInitMode(RTC_TypeDef *RTCx)
00771 {
00772   /* Check the parameter */
00773   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00774 
00775   /* Disable initialization mode */
00776   LL_RTC_DisableInitMode(RTCx);
00777 
00778   return SUCCESS;
00779 }
00780 
00781 /**
00782   * @brief  Waits until the RTC Time and Day registers (RTC_TR and RTC_DR) are
00783   *         synchronized with RTC APB clock.
00784   * @note   The RTC Resynchronization mode is write protected, use the
00785   *         @ref LL_RTC_DisableWriteProtection before calling this function.
00786   * @note   To read the calendar through the shadow registers after Calendar
00787   *         initialization, calendar update or after wakeup from low power modes
00788   *         the software must first clear the RSF flag.
00789   *         The software must then wait until it is set again before reading
00790   *         the calendar, which means that the calendar registers have been
00791   *         correctly copied into the RTC_TR and RTC_DR shadow registers.
00792   * @param  RTCx RTC Instance
00793   * @retval An ErrorStatus enumeration value:
00794   *          - SUCCESS: RTC registers are synchronised
00795   *          - ERROR: RTC registers are not synchronised
00796   */
00797 ErrorStatus LL_RTC_WaitForSynchro(RTC_TypeDef *RTCx)
00798 {
00799   __IO uint32_t timeout = RTC_SYNCHRO_TIMEOUT;
00800   ErrorStatus status = SUCCESS;
00801   uint32_t tmp = 0U;
00802 
00803   /* Check the parameter */
00804   assert_param(IS_RTC_ALL_INSTANCE(RTCx));
00805 
00806   /* Clear RSF flag */
00807   LL_RTC_ClearFlag_RS(RTCx);
00808 
00809   /* Wait the registers to be synchronised */
00810   tmp = LL_RTC_IsActiveFlag_RS(RTCx);
00811   while ((timeout != 0U) && (tmp != 0U))
00812   {
00813     if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
00814     {
00815       timeout--;
00816     }
00817     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
00818     if (timeout == 0U)
00819     {
00820       status = ERROR;
00821     }
00822   }
00823 
00824   if (status != ERROR)
00825   {
00826     timeout = RTC_SYNCHRO_TIMEOUT;
00827     tmp = LL_RTC_IsActiveFlag_RS(RTCx);
00828     while ((timeout != 0U) && (tmp != 1U))
00829     {
00830       if (LL_SYSTICK_IsActiveCounterFlag() == 1U)
00831       {
00832         timeout--;
00833       }
00834       tmp = LL_RTC_IsActiveFlag_RS(RTCx);
00835       if (timeout == 0U)
00836       {
00837         status = ERROR;
00838       }
00839     }
00840   }
00841 
00842   return (status);
00843 }
00844 
00845 /**
00846   * @}
00847   */
00848 
00849 /**
00850   * @}
00851   */
00852 
00853 /**
00854   * @}
00855   */
00856 
00857 #endif /* defined(RTC) */
00858 
00859 /**
00860   * @}
00861   */
00862 
00863 #endif /* USE_FULL_LL_DRIVER */
00864 
00865 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/