stm32f4xx_ll_rtc.c 31 KB

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