hc32_ll_rtc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_rtc.h
  4. * @brief This file contains all the functions prototypes of the RTC driver
  5. * library.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32_LL_RTC_H__
  22. #define __HC32_LL_RTC_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include "hc32_ll_def.h"
  32. #include "hc32f4xx.h"
  33. #include "hc32f4xx_conf.h"
  34. /**
  35. * @addtogroup LL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup LL_RTC
  40. * @{
  41. */
  42. #if (LL_RTC_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup RTC_Global_Types RTC Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief RTC Init structure definition
  52. */
  53. typedef struct {
  54. uint8_t u8ClockSrc; /*!< Specifies the RTC clock source.
  55. This parameter can be a value of @ref RTC_Clock_Source */
  56. uint8_t u8HourFormat; /*!< Specifies the RTC hour format.
  57. This parameter can be a value of @ref RTC_Hour_Format */
  58. uint8_t u8IntPeriod; /*!< Specifies the RTC interrupt period.
  59. This parameter can be a value of @ref RTC_Interrupt_Period */
  60. uint8_t u8ClockCompen; /*!< Specifies the validity of RTC clock compensation.
  61. This parameter can be a value of @ref RTC_Clock_Compensation */
  62. uint8_t u8CompenMode; /*!< Specifies the mode of RTC clock compensation.
  63. This parameter can be a value of @ref RTC_Clock_Compensation_Mode */
  64. uint16_t u16CompenValue; /*!< Specifies the value of RTC clock compensation.
  65. This parameter can be a number between Min_Data = 0 and Max_Data = 0x1FF */
  66. } stc_rtc_init_t;
  67. /**
  68. * @brief RTC Date structure definition
  69. */
  70. typedef struct {
  71. uint8_t u8Year; /*!< Specifies the RTC Year.
  72. This parameter can be a number between Min_Data = 0 and Max_Data = 99 */
  73. uint8_t u8Month; /*!< Specifies the RTC Month (in Decimal format).
  74. This parameter can be a value of @ref RTC_Month */
  75. uint8_t u8Day; /*!< Specifies the RTC Day.
  76. This parameter can be a number between Min_Data = 1 and Max_Data = 31 */
  77. uint8_t u8Weekday; /*!< Specifies the RTC Weekday.
  78. This parameter can be a value of @ref RTC_Weekday */
  79. } stc_rtc_date_t;
  80. /**
  81. * @brief RTC Time structure definition
  82. */
  83. typedef struct {
  84. uint8_t u8Hour; /*!< Specifies the RTC Hour.
  85. This parameter can be a number between Min_Data = 1 and Max_Data = 12 if the RTC_HOUR_FMT_12H is selected.
  86. This parameter can be a number between Min_Data = 0 and Max_Data = 23 if the RTC_HOUR_FMT_24H is selected */
  87. uint8_t u8Minute; /*!< Specifies the RTC Minute.
  88. This parameter can be a number between Min_Data = 0 and Max_Data = 59 */
  89. uint8_t u8Second; /*!< Specifies the RTC Second.
  90. This parameter can be a number between Min_Data = 0 and Max_Data = 59 */
  91. uint8_t u8AmPm; /*!< Specifies the RTC Am/Pm Time (in RTC_HOUR_FMT_12H mode).
  92. This parameter can be a value of @ref RTC_Hour12_AM_PM */
  93. } stc_rtc_time_t;
  94. /**
  95. * @brief RTC Alarm structure definition
  96. */
  97. typedef struct {
  98. uint8_t u8AlarmHour; /*!< Specifies the RTC Alarm Hour.
  99. This parameter can be a number between Min_Data = 1 and Max_Data = 12 if the RTC_HOUR_FMT_12H is selected.
  100. This parameter can be a number between Min_Data = 0 and Max_Data = 23 if the RTC_HOUR_FMT_24H is selected */
  101. uint8_t u8AlarmMinute; /*!< Specifies the RTC Alarm Minute.
  102. This parameter can be a number between Min_Data = 0 and Max_Data = 59 */
  103. uint8_t u8AlarmWeekday; /*!< Specifies the RTC Alarm Weekday.
  104. This parameter can be a value of @ref RTC_Alarm_Weekday */
  105. uint8_t u8AlarmAmPm; /*!< Specifies the RTC Alarm Am/Pm Time (in RTC_HOUR_FMT_12H mode).
  106. This parameter can be a value of @ref RTC_Hour12_AM_PM */
  107. } stc_rtc_alarm_t;
  108. /**
  109. * @}
  110. */
  111. /*******************************************************************************
  112. * Global pre-processor symbols/macros ('#define')
  113. ******************************************************************************/
  114. /**
  115. * @defgroup RTC_Global_Macros RTC Global Macros
  116. * @{
  117. */
  118. /**
  119. * @defgroup RTC_Data_Format RTC Data Format
  120. * @{
  121. */
  122. #define RTC_DATA_FMT_DEC (0x00U) /*!< Decimal data format */
  123. #define RTC_DATA_FMT_BCD (0x01U) /*!< BCD data format */
  124. /**
  125. * @}
  126. */
  127. /**
  128. * @defgroup RTC_Decimal_BCD_Conversion RTC Decimal BCD Conversion
  129. * @{
  130. */
  131. #define RTC_DEC2BCD(__DATA__) ((((__DATA__) / 10U) << 4U) + ((__DATA__) % 10U))
  132. #define RTC_BCD2DEC(__DATA__) ((((__DATA__) >> 4U) * 10U) + ((__DATA__) & 0x0FU))
  133. /**
  134. * @}
  135. */
  136. /**
  137. * @defgroup RTC_Clock_Source RTC Clock Source
  138. * @{
  139. */
  140. #define RTC_CLK_SRC_XTAL32 (0U) /*!< XTAL32 Clock */
  141. #define RTC_CLK_SRC_LRC (RTC_CR3_RCKSEL | RTC_CR3_LRCEN) /*!< RTC LRC Clock */
  142. #define RTC_CLK_SRC_XTAL_DIV (RTC_CR3_RCKSEL | PWC_PWRC6_RTCCKSEL_0) /*!< XTAL division to 32768 */
  143. /**
  144. * @}
  145. */
  146. /**
  147. * @defgroup RTC_Hour_Format RTC Hour Format
  148. * @{
  149. */
  150. #define RTC_HOUR_FMT_12H (0U) /*!< 12 hour time system */
  151. #define RTC_HOUR_FMT_24H (RTC_CR1_AMPM) /*!< 24 hour time system */
  152. /**
  153. * @}
  154. */
  155. /**
  156. * @defgroup RTC_Interrupt_Period RTC Interrupt Period
  157. * @{
  158. */
  159. #define RTC_INT_PERIOD_INVD (0U) /*!< Interrupt period invalid */
  160. #define RTC_INT_PERIOD_PER_HALF_SEC (0x01U << RTC_CR1_PRDS_POS) /*!< Interrupt period per half second */
  161. #define RTC_INT_PERIOD_PER_SEC (0x02U << RTC_CR1_PRDS_POS) /*!< Interrupt period per second */
  162. #define RTC_INT_PERIOD_PER_MINUTE (0x03U << RTC_CR1_PRDS_POS) /*!< Interrupt period per minute */
  163. #define RTC_INT_PERIOD_PER_HOUR (0x04U << RTC_CR1_PRDS_POS) /*!< Interrupt period per hour */
  164. #define RTC_INT_PERIOD_PER_DAY (0x05U << RTC_CR1_PRDS_POS) /*!< Interrupt period per day */
  165. #define RTC_INT_PERIOD_PER_MONTH (0x06U << RTC_CR1_PRDS_POS) /*!< Interrupt period per month */
  166. /**
  167. * @}
  168. */
  169. /**
  170. * @defgroup RTC_Clock_Compensation RTC Clock Compensation
  171. * @{
  172. */
  173. #define RTC_CLK_COMPEN_DISABLE (0U)
  174. #define RTC_CLK_COMPEN_ENABLE (RTC_ERRCRH_COMPEN)
  175. /**
  176. * @}
  177. */
  178. /**
  179. * @defgroup RTC_Clock_Compensation_Mode RTC Clock Compensation Mode
  180. * @{
  181. */
  182. #define RTC_CLK_COMPEN_MD_DISTRIBUTED (0U) /*!< Distributed compensation 1Hz output */
  183. #define RTC_CLK_COMPEN_MD_UNIFORM (RTC_CR1_ONEHZSEL) /*!< Uniform compensation 1Hz output */
  184. /**
  185. * @}
  186. */
  187. /**
  188. * @defgroup RTC_Hour12_AM_PM RTC Hour12 AM/PM
  189. * @{
  190. */
  191. #define RTC_HOUR_24H (0U) /*!< 24-hour format */
  192. #define RTC_HOUR_12H_AM (0U) /*!< AM in 12-hour */
  193. #define RTC_HOUR_12H_PM (RTC_HOUR_HOURD_1) /*!< PM in 12-hour */
  194. /**
  195. * @}
  196. */
  197. /**
  198. * @defgroup RTC_Month RTC Month
  199. * @{
  200. */
  201. #define RTC_MONTH_JANUARY (0x01U)
  202. #define RTC_MONTH_FEBRUARY (0x02U)
  203. #define RTC_MONTH_MARCH (0x03U)
  204. #define RTC_MONTH_APRIL (0x04U)
  205. #define RTC_MONTH_MAY (0x05U)
  206. #define RTC_MONTH_JUNE (0x06U)
  207. #define RTC_MONTH_JULY (0x07U)
  208. #define RTC_MONTH_AUGUST (0x08U)
  209. #define RTC_MONTH_SEPTEMBER (0x09U)
  210. #define RTC_MONTH_OCTOBER (0x0AU)
  211. #define RTC_MONTH_NOVEMBER (0x0BU)
  212. #define RTC_MONTH_DECEMBER (0x0CU)
  213. /**
  214. * @}
  215. */
  216. /**
  217. * @defgroup RTC_Weekday RTC Weekday
  218. * @{
  219. */
  220. #define RTC_WEEKDAY_SUNDAY (0x00U)
  221. #define RTC_WEEKDAY_MONDAY (0x01U)
  222. #define RTC_WEEKDAY_TUESDAY (0x02U)
  223. #define RTC_WEEKDAY_WEDNESDAY (0x03U)
  224. #define RTC_WEEKDAY_THURSDAY (0x04U)
  225. #define RTC_WEEKDAY_FRIDAY (0x05U)
  226. #define RTC_WEEKDAY_SATURDAY (0x06U)
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @defgroup RTC_Alarm_Weekday RTC Alarm Weekday
  232. * @{
  233. */
  234. #define RTC_ALARM_WEEKDAY_SUNDAY (0x01U)
  235. #define RTC_ALARM_WEEKDAY_MONDAY (0x02U)
  236. #define RTC_ALARM_WEEKDAY_TUESDAY (0x04U)
  237. #define RTC_ALARM_WEEKDAY_WEDNESDAY (0x08U)
  238. #define RTC_ALARM_WEEKDAY_THURSDAY (0x10U)
  239. #define RTC_ALARM_WEEKDAY_FRIDAY (0x20U)
  240. #define RTC_ALARM_WEEKDAY_SATURDAY (0x40U)
  241. #define RTC_ALARM_WEEKDAY_EVERYDAY (0x7FU)
  242. /**
  243. * @}
  244. */
  245. /**
  246. * @defgroup RTC_Flag RTC Flag
  247. * @{
  248. */
  249. #define RTC_FLAG_RD_WR (RTC_CR2_RWEN) /*!< Read and write permission flag */
  250. #define RTC_FLAG_ALARM (RTC_CR2_ALMF) /*!< Alarm flag */
  251. #define RTC_FLAG_PERIOD (RTC_CR2_PRDF) /*!< Period flag */
  252. #define RTC_FLAG_ALL (RTC_FLAG_RD_WR | RTC_FLAG_ALARM | RTC_FLAG_PERIOD)
  253. #define RTC_FLAG_CLR_ALL (RTC_FLAG_ALARM | RTC_FLAG_PERIOD)
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @defgroup RTC_Interrupt RTC Interrupt
  259. * @{
  260. */
  261. #define RTC_INT_PERIOD (RTC_CR2_PRDIE) /*!< Period interrupt */
  262. #define RTC_INT_ALARM (RTC_CR2_ALMIE) /*!< Alarm interrupt */
  263. #define RTC_INT_ALL (RTC_INT_PERIOD | RTC_INT_ALARM)
  264. /**
  265. * @}
  266. */
  267. /**
  268. * @}
  269. */
  270. /*******************************************************************************
  271. * Global variable definitions ('extern')
  272. ******************************************************************************/
  273. /*******************************************************************************
  274. Global function prototypes (definition in C source)
  275. ******************************************************************************/
  276. /**
  277. * @addtogroup RTC_Global_Functions
  278. * @{
  279. */
  280. /* Initialization and configuration functions */
  281. int32_t RTC_DeInit(void);
  282. int32_t RTC_Init(const stc_rtc_init_t *pstcRtcInit);
  283. int32_t RTC_StructInit(stc_rtc_init_t *pstcRtcInit);
  284. int32_t RTC_EnterRwMode(void);
  285. int32_t RTC_ExitRwMode(void);
  286. /* Control configuration */
  287. int32_t RTC_ConfirmLPMCond(void);
  288. void RTC_SetIntPeriod(uint8_t u8Period);
  289. void RTC_SetClockSrc(uint8_t u8Src);
  290. void RTC_SetClockCompenValue(uint16_t u16Value);
  291. en_functional_state_t RTC_GetCounterState(void);
  292. void RTC_Cmd(en_functional_state_t enNewState);
  293. void RTC_LrcCmd(en_functional_state_t enNewState);
  294. void RTC_OneHzOutputCmd(en_functional_state_t enNewState);
  295. void RTC_ClockCompenCmd(en_functional_state_t enNewState);
  296. /* Date and time functions */
  297. int32_t RTC_SetDate(uint8_t u8Format, stc_rtc_date_t *pstcRtcDate);
  298. int32_t RTC_GetDate(uint8_t u8Format, stc_rtc_date_t *pstcRtcDate);
  299. int32_t RTC_SetTime(uint8_t u8Format, stc_rtc_time_t *pstcRtcTime);
  300. int32_t RTC_GetTime(uint8_t u8Format, stc_rtc_time_t *pstcRtcTime);
  301. /* Alarm configuration functions */
  302. int32_t RTC_SetAlarm(uint8_t u8Format, stc_rtc_alarm_t *pstcRtcAlarm);
  303. int32_t RTC_GetAlarm(uint8_t u8Format, stc_rtc_alarm_t *pstcRtcAlarm);
  304. void RTC_AlarmCmd(en_functional_state_t enNewState);
  305. /* Interrupt and flag management functions */
  306. void RTC_IntCmd(uint32_t u32IntType, en_functional_state_t enNewState);
  307. en_flag_status_t RTC_GetStatus(uint32_t u32Flag);
  308. void RTC_ClearStatus(uint32_t u32Flag);
  309. /**
  310. * @}
  311. */
  312. #endif /* LL_RTC_ENABLE */
  313. /**
  314. * @}
  315. */
  316. /**
  317. * @}
  318. */
  319. #ifdef __cplusplus
  320. }
  321. #endif
  322. #endif /* __HC32_LL_RTC_H__ */
  323. /*******************************************************************************
  324. * EOF (not truncated)
  325. ******************************************************************************/