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