You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

741 lines
26 KiB

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