stm32l4xx_ll_rtc.c 29 KB

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