Subversion Repositories ScreenTimer

Rev

Blame | Last modification | View Log | Download | RSS feed

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