gd32e23x_rtc.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*!
  2. \file gd32e23x_rtc.h
  3. \brief definitions for the RTC
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #ifndef GD32E23X_RTC_H
  31. #define GD32E23X_RTC_H
  32. #include "gd32e23x.h"
  33. /* RTC definitions */
  34. #define RTC RTC_BASE
  35. /* registers definitions */
  36. #define RTC_TIME REG32((RTC) + 0x00000000U) /*!< RTC time of day register */
  37. #define RTC_DATE REG32((RTC) + 0x00000004U) /*!< RTC date register */
  38. #define RTC_CTL REG32((RTC) + 0x00000008U) /*!< RTC control register */
  39. #define RTC_STAT REG32((RTC) + 0x0000000CU) /*!< RTC status register */
  40. #define RTC_PSC REG32((RTC) + 0x00000010U) /*!< RTC time prescaler register */
  41. #define RTC_ALRM0TD REG32((RTC) + 0x0000001CU) /*!< RTC alarm 0 time and date register */
  42. #define RTC_WPK REG32((RTC) + 0x00000024U) /*!< RTC write protection key register */
  43. #define RTC_SS REG32((RTC) + 0x00000028U) /*!< RTC sub second register */
  44. #define RTC_SHIFTCTL REG32((RTC) + 0x0000002CU) /*!< RTC shift function control register */
  45. #define RTC_TTS REG32((RTC) + 0x00000030U) /*!< RTC time of timestamp register */
  46. #define RTC_DTS REG32((RTC) + 0x00000034U) /*!< RTC date of timestamp register */
  47. #define RTC_SSTS REG32((RTC) + 0x00000038U) /*!< RTC sub second of timestamp register */
  48. #define RTC_HRFC REG32((RTC) + 0x0000003CU) /*!< RTC high resolution frequency compensation registor */
  49. #define RTC_TAMP REG32((RTC) + 0x00000040U) /*!< RTC tamper register */
  50. #define RTC_ALRM0SS REG32((RTC) + 0x00000044U) /*!< RTC alarm 0 sub second register */
  51. #define RTC_BKP0 REG32((RTC) + 0x00000050U) /*!< RTC backup 0 register */
  52. #define RTC_BKP1 REG32((RTC) + 0x00000054U) /*!< RTC backup 1 register */
  53. #define RTC_BKP2 REG32((RTC) + 0x00000058U) /*!< RTC backup 2 register */
  54. #define RTC_BKP3 REG32((RTC) + 0x0000005CU) /*!< RTC backup 3 register */
  55. #define RTC_BKP4 REG32((RTC) + 0x00000060U) /*!< RTC backup 4 register */
  56. /* bits definitions */
  57. /* RTC_TIME */
  58. #define RTC_TIME_SCU BITS(0,3) /*!< second units in BCD code */
  59. #define RTC_TIME_SCT BITS(4,6) /*!< second tens in BCD code */
  60. #define RTC_TIME_MNU BITS(8,11) /*!< minute units in BCD code */
  61. #define RTC_TIME_MNT BITS(12,14) /*!< minute tens in BCD code */
  62. #define RTC_TIME_HRU BITS(16,19) /*!< hour units in BCD code */
  63. #define RTC_TIME_HRT BITS(20,21) /*!< hour tens in BCD code */
  64. #define RTC_TIME_PM BIT(22) /*!< AM/PM notation */
  65. /* RTC_DATE */
  66. #define RTC_DATE_DAYU BITS(0,3) /*!< date units in BCD code */
  67. #define RTC_DATE_DAYT BITS(4,5) /*!< date tens in BCD code */
  68. #define RTC_DATE_MONU BITS(8,11) /*!< month units in BCD code */
  69. #define RTC_DATE_MONT BIT(12) /*!< month tens in BCD code */
  70. #define RTC_DATE_DOW BITS(13,15) /*!< day of week units */
  71. #define RTC_DATE_YRU BITS(16,19) /*!< year units in BCD code */
  72. #define RTC_DATE_YRT BITS(20,23) /*!< year tens in BCD code */
  73. /* RTC_CTL */
  74. #define RTC_CTL_TSEG BIT(3) /*!< valid event edge of time-stamp */
  75. #define RTC_CTL_REFEN BIT(4) /*!< reference clock detection function enable */
  76. #define RTC_CTL_BPSHAD BIT(5) /*!< shadow registers bypass control */
  77. #define RTC_CTL_CS BIT(6) /*!< display format of clock system */
  78. #define RTC_CTL_ALRM0EN BIT(8) /*!< alarm function enable */
  79. #define RTC_CTL_TSEN BIT(11) /*!< time-stamp function enable */
  80. #define RTC_CTL_ALRM0IE BIT(12) /*!< RTC alarm interrupt enable */
  81. #define RTC_CTL_TSIE BIT(15) /*!< time-stamp interrupt enable */
  82. #define RTC_CTL_A1H BIT(16) /*!< add 1 hour(summer time change) */
  83. #define RTC_CTL_S1H BIT(17) /*!< subtract 1 hour(winter time change) */
  84. #define RTC_CTL_DSM BIT(18) /*!< daylight saving mark */
  85. #define RTC_CTL_COS BIT(19) /*!< calibration output selection */
  86. #define RTC_CTL_OPOL BIT(20) /*!< output polarity */
  87. #define RTC_CTL_OS BITS(21,22) /*!< output selection */
  88. #define RTC_CTL_COEN BIT(23) /*!< calibration output enable */
  89. /* RTC_STAT */
  90. #define RTC_STAT_ALRM0WF BIT(0) /*!< alarm configuration can be write flag */
  91. #define RTC_STAT_SOPF BIT(3) /*!< shift function operation pending flag */
  92. #define RTC_STAT_YCM BIT(4) /*!< year configuration mark status flag */
  93. #define RTC_STAT_RSYNF BIT(5) /*!< register synchronization flag */
  94. #define RTC_STAT_INITF BIT(6) /*!< initialization state flag */
  95. #define RTC_STAT_INITM BIT(7) /*!< enter initialization mode */
  96. #define RTC_STAT_ALRM0F BIT(8) /*!< alarm occurs flag */
  97. #define RTC_STAT_TSF BIT(11) /*!< time-stamp flag */
  98. #define RTC_STAT_TSOVRF BIT(12) /*!< time-stamp overflow flag */
  99. #define RTC_STAT_TP0F BIT(13) /*!< RTC tamp 0 detected flag */
  100. #define RTC_STAT_TP1F BIT(14) /*!< RTC tamp 1 detected flag */
  101. #define RTC_STAT_SCPF BIT(16) /*!< recalibration pending flag */
  102. /* RTC_PSC */
  103. #define RTC_PSC_FACTOR_S BITS(0,14) /*!< synchronous prescaler factor */
  104. #define RTC_PSC_FACTOR_A BITS(16,22) /*!< asynchronous prescaler factor */
  105. /* RTC_ALRM0TD */
  106. #define RTC_ALRM0TD_SCU BITS(0,3) /*!< second units in BCD code */
  107. #define RTC_ALRM0TD_SCT BITS(4,6) /*!< second tens in BCD code */
  108. #define RTC_ALRM0TD_MSKS BIT(7) /*!< alarm second mask bit */
  109. #define RTC_ALRM0TD_MNU BITS(8,11) /*!< minutes units in BCD code */
  110. #define RTC_ALRM0TD_MNT BITS(12,14) /*!< minutes tens in BCD code */
  111. #define RTC_ALRM0TD_MSKM BIT(15) /*!< alarm minutes mask bit */
  112. #define RTC_ALRM0TD_HRU BITS(16,19) /*!< hour units in BCD code */
  113. #define RTC_ALRM0TD_HRT BITS(20,21) /*!< hour units in BCD code */
  114. #define RTC_ALRM0TD_PM BIT(22) /*!< AM/PM flag */
  115. #define RTC_ALRM0TD_MSKH BIT(23) /*!< alarm hour mask bit */
  116. #define RTC_ALRM0TD_DAYU BITS(24,27) /*!< date units or week day in BCD code */
  117. #define RTC_ALRM0TD_DAYT BITS(28,29) /*!< date tens in BCD code */
  118. #define RTC_ALRM0TD_DOWS BIT(30) /*!< day of week selection */
  119. #define RTC_ALRM0TD_MSKD BIT(31) /*!< alarm date mask bit */
  120. /* RTC_WPK */
  121. #define RTC_WPK_WPK BITS(0,7) /*!< key for write protection */
  122. /* RTC_SS */
  123. #define RTC_SS_SSC BITS(0,15) /*!< sub second value */
  124. /* RTC_SHIFTCTL */
  125. #define RTC_SHIFTCTL_SFS BITS(0,14) /*!< subtract a fraction of a second */
  126. #define RTC_SHIFTCTL_A1S BIT(31) /*!< one second add */
  127. /* RTC_TTS */
  128. #define RTC_TTS_SCU BITS(0,3) /*!< second units in BCD code */
  129. #define RTC_TTS_SCT BITS(4,6) /*!< second units in BCD code */
  130. #define RTC_TTS_MNU BITS(8,11) /*!< minute units in BCD code */
  131. #define RTC_TTS_MNT BITS(12,14) /*!< minute tens in BCD code */
  132. #define RTC_TTS_HRU BITS(16,19) /*!< hour units in BCD code */
  133. #define RTC_TTS_HRT BITS(20,21) /*!< hour tens in BCD code */
  134. #define RTC_TTS_PM BIT(22) /*!< AM/PM notation */
  135. /* RTC_DTS */
  136. #define RTC_DTS_DAYU BITS(0,3) /*!< date units in BCD code */
  137. #define RTC_DTS_DAYT BITS(4,5) /*!< date tens in BCD code */
  138. #define RTC_DTS_MONU BITS(8,11) /*!< month units in BCD code */
  139. #define RTC_DTS_MONT BIT(12) /*!< month tens in BCD code */
  140. #define RTC_DTS_DOW BITS(13,15) /*!< day of week units */
  141. /* RTC_SSTS */
  142. #define RTC_SSTS_SSC BITS(0,15) /*!< timestamp sub second units */
  143. /* RTC_HRFC */
  144. #define RTC_HRFC_CMSK BITS(0,8) /*!< calibration mask number */
  145. #define RTC_HRFC_CWND16 BIT(13) /*!< calibration window select 16 seconds */
  146. #define RTC_HRFC_CWND8 BIT(14) /*!< calibration window select 16 seconds */
  147. #define RTC_HRFC_FREQI BIT(15) /*!< increase RTC frequency by 488.5ppm */
  148. /* RTC_TAMP */
  149. #define RTC_TAMP_TP0EN BIT(0) /*!< tamper 0 detection enable */
  150. #define RTC_TAMP_TP0EG BIT(1) /*!< tamper 0 event trigger edge for RTC tamp 0 input */
  151. #define RTC_TAMP_TPIE BIT(2) /*!< tamper detection interrupt enable */
  152. #define RTC_TAMP_TP1EN BIT(3) /*!< tamper 1 detection enable */
  153. #define RTC_TAMP_TP1EG BIT(4) /*!< tamper 1 event trigger edge for RTC tamp 1 input */
  154. #define RTC_TAMP_TPTS BIT(7) /*!< make tamper function used for timestamp function */
  155. #define RTC_TAMP_FREQ BITS(8,10) /*!< sample frequency of tamper event detection */
  156. #define RTC_TAMP_FLT BITS(11,12) /*!< RTC tamp x filter count setting */
  157. #define RTC_TAMP_PRCH BITS(13,14) /*!< precharge duration time of RTC tamp x */
  158. #define RTC_TAMP_DISPU BIT(15) /*!< RTC tamp x pull up disable bit */
  159. #define RTC_TAMP_PC13VAL BIT(18) /*!< alarm output type control/PC13 output value */
  160. #define RTC_TAMP_PC13MDE BIT(19) /*!< PC13 mode */
  161. #define RTC_TAMP_PC14VAL BIT(20) /*!< PC14 output value */
  162. #define RTC_TAMP_PC14MDE BIT(21) /*!< PC14 mode */
  163. #define RTC_TAMP_PC15VAL BIT(22) /*!< PC15 output value */
  164. #define RTC_TAMP_PC15MDE BIT(23) /*!< PC15 mode */
  165. /* RTC_ALRM0SS */
  166. #define RTC_ALRM0SS_SSC BITS(0,14) /*!< alarm sub second value */
  167. #define RTC_ALRM0SS_MASKSSC BITS(24,27) /*!< mask control bit of SS */
  168. /* RTC_BKP0 */
  169. #define RTC_BKP0_DATA BITS(0,31) /*!< backup domain registers */
  170. /* RTC_BKP1 */
  171. #define RTC_BKP1_DATA BITS(0,31) /*!< backup domain registers */
  172. /* RTC_BKP2 */
  173. #define RTC_BKP2_DATA BITS(0,31) /*!< backup domain registers */
  174. /* RTC_BKP3 */
  175. #define RTC_BKP3_DATA BITS(0,31) /*!< backup domain registers */
  176. /* RTC_BKP4 */
  177. #define RTC_BKP4_DATA BITS(0,31) /*!< backup domain registers */
  178. /* constants definitions */
  179. /* structure for initialization of the RTC */
  180. typedef struct
  181. {
  182. uint8_t rtc_year; /*!< RTC year value: 0x0 - 0x99(BCD format) */
  183. uint8_t rtc_month; /*!< RTC month value */
  184. uint8_t rtc_date; /*!< RTC date value: 0x1 - 0x31(BCD format) */
  185. uint8_t rtc_day_of_week; /*!< RTC weekday value */
  186. uint8_t rtc_hour; /*!< RTC hour value */
  187. uint8_t rtc_minute; /*!< RTC minute value: 0x0 - 0x59(BCD format) */
  188. uint8_t rtc_second; /*!< RTC second value: 0x0 - 0x59(BCD format) */
  189. uint16_t rtc_factor_asyn; /*!< RTC asynchronous prescaler value: 0x0 - 0x7F */
  190. uint16_t rtc_factor_syn; /*!< RTC synchronous prescaler value: 0x0 - 0x7FFF */
  191. uint32_t rtc_am_pm; /*!< RTC AM/PM value */
  192. uint32_t rtc_display_format; /*!< RTC time notation */
  193. }rtc_parameter_struct;
  194. /* structure for RTC alarm configuration */
  195. typedef struct
  196. {
  197. uint32_t rtc_alarm_mask; /*!< RTC alarm mask */
  198. uint32_t rtc_weekday_or_date; /*!< specify RTC alarm is on date or weekday */
  199. uint8_t rtc_alarm_day; /*!< RTC alarm date or weekday value*/
  200. uint8_t rtc_alarm_hour; /*!< RTC alarm hour value */
  201. uint8_t rtc_alarm_minute; /*!< RTC alarm minute value: 0x0 - 0x59(BCD format) */
  202. uint8_t rtc_alarm_second; /*!< RTC alarm second value: 0x0 - 0x59(BCD format) */
  203. uint32_t rtc_am_pm; /*!< RTC alarm AM/PM value */
  204. }rtc_alarm_struct;
  205. /* structure for RTC time-stamp configuration */
  206. typedef struct
  207. {
  208. uint8_t rtc_timestamp_month; /*!< RTC time-stamp month value */
  209. uint8_t rtc_timestamp_date; /*!< RTC time-stamp date value: 0x1 - 0x31(BCD format) */
  210. uint8_t rtc_timestamp_day; /*!< RTC time-stamp weekday value */
  211. uint8_t rtc_timestamp_hour; /*!< RTC time-stamp hour value */
  212. uint8_t rtc_timestamp_minute; /*!< RTC time-stamp minute value: 0x0 - 0x59(BCD format) */
  213. uint8_t rtc_timestamp_second; /*!< RTC time-stamp second value: 0x0 - 0x59(BCD format) */
  214. uint32_t rtc_am_pm; /*!< RTC time-stamp AM/PM value */
  215. }rtc_timestamp_struct;
  216. /* structure for RTC tamper configuration */
  217. typedef struct
  218. {
  219. uint32_t rtc_tamper_source; /*!< RTC tamper source */
  220. uint32_t rtc_tamper_trigger; /*!< RTC tamper trigger */
  221. uint32_t rtc_tamper_filter; /*!< RTC tamper consecutive samples needed during a voltage level detection */
  222. uint32_t rtc_tamper_sample_frequency; /*!< RTC tamper sampling frequency during a voltage level detection */
  223. ControlStatus rtc_tamper_precharge_enable; /*!< RTC tamper precharge feature during a voltage level detection */
  224. uint32_t rtc_tamper_precharge_time; /*!< RTC tamper precharge duration if precharge feature is enabled */
  225. ControlStatus rtc_tamper_with_timestamp; /*!< RTC tamper time-stamp feature */
  226. }rtc_tamper_struct;
  227. /* time register value */
  228. #define TIME_SC(regval) (BITS(0,6) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_TIME_SC bit field */
  229. #define GET_TIME_SC(regval) GET_BITS((regval),0,6) /*!< get value of RTC_TIME_SC bit field */
  230. #define TIME_MN(regval) (BITS(8,14) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_TIME_MN bit field */
  231. #define GET_TIME_MN(regval) GET_BITS((regval),8,14) /*!< get value of RTC_TIME_MN bit field */
  232. #define TIME_HR(regval) (BITS(16,21) & ((uint32_t)(regval) << 16U)) /*!< write value to RTC_TIME_HR bit field */
  233. #define GET_TIME_HR(regval) GET_BITS((regval),16,21) /*!< get value of RTC_TIME_HR bit field */
  234. #define RTC_AM ((uint32_t)0x00000000U) /*!< AM format */
  235. #define RTC_PM RTC_TIME_PM /*!< PM format */
  236. /* date register value */
  237. #define DATE_DAY(regval) (BITS(0,5) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_DATE_DAY bit field */
  238. #define GET_DATE_DAY(regval) GET_BITS((regval),0,5) /*!< get value of RTC_DATE_DAY bit field */
  239. #define DATE_MON(regval) (BITS(8,12) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_DATE_MON bit field */
  240. #define GET_DATE_MON(regval) GET_BITS((regval),8,12) /*!< get value of RTC_DATE_MON bit field */
  241. #define RTC_JAN ((uint8_t)0x01U) /*!< Janurary */
  242. #define RTC_FEB ((uint8_t)0x02U) /*!< February */
  243. #define RTC_MAR ((uint8_t)0x03U) /*!< March */
  244. #define RTC_APR ((uint8_t)0x04U) /*!< April */
  245. #define RTC_MAY ((uint8_t)0x05U) /*!< May */
  246. #define RTC_JUN ((uint8_t)0x06U) /*!< June */
  247. #define RTC_JUL ((uint8_t)0x07U) /*!< July */
  248. #define RTC_AUG ((uint8_t)0x08U) /*!< August */
  249. #define RTC_SEP ((uint8_t)0x09U) /*!< September */
  250. #define RTC_OCT ((uint8_t)0x10U) /*!< October */
  251. #define RTC_NOV ((uint8_t)0x11U) /*!< November */
  252. #define RTC_DEC ((uint8_t)0x12U) /*!< December */
  253. #define DATE_DOW(regval) (BITS(13,15) & ((uint32_t)(regval) << 13U)) /*!< write value to RTC_DATE_DOW bit field */
  254. #define GET_DATE_DOW(regval) GET_BITS((regval),13,15) /*!< get value of RTC_DATE_DOW bit field */
  255. #define RTC_MONDAY ((uint8_t)0x01U) /*!< Monday */
  256. #define RTC_TUESDAY ((uint8_t)0x02U) /*!< Tuesday */
  257. #define RTC_WEDSDAY ((uint8_t)0x03U) /*!< Wednesday */
  258. #define RTC_THURSDAY ((uint8_t)0x04U) /*!< Thursday */
  259. #define RTC_FRIDAY ((uint8_t)0x05U) /*!< Friday */
  260. #define RTC_SATURDAY ((uint8_t)0x06U) /*!< Saturday */
  261. #define RTC_SUNDAY ((uint8_t)0x07U) /*!< Sunday */
  262. #define DATE_YR(regval) (BITS(16,23) & ((uint32_t)(regval) << 16U)) /*!< write value to RTC_DATE_YR bit field */
  263. #define GET_DATE_YR(regval) GET_BITS((regval),16,23) /*!< get value of RTC_DATE_YR bit field */
  264. /* ctl register value */
  265. #define CTL_OS(regval) (BITS(21,22) & ((uint32_t)(regval) << 21U)) /*!< write value to RTC_CTL_OS bit field */
  266. #define RTC_OS_DISABLE CTL_OS(0) /*!< disable output RTC_ALARM */
  267. #define RTC_OS_ENABLE CTL_OS(1) /*!< enable alarm flag output */
  268. #define RTC_CALIBRATION_512HZ RTC_CTL_COEN /*!< calibration output of 512Hz is enable */
  269. #define RTC_CALIBRATION_1HZ (RTC_CTL_COEN | RTC_CTL_COS) /*!< calibration output of 1Hz is enable */
  270. #define RTC_ALARM_HIGH RTC_OS_ENABLE /*!< enable alarm flag output with high level */
  271. #define RTC_ALARM_LOW (RTC_OS_ENABLE | RTC_CTL_OPOL) /*!< enable alarm flag output with low level*/
  272. #define RTC_24HOUR ((uint32_t)0x00000000U) /*!< 24-hour format */
  273. #define RTC_12HOUR RTC_CTL_CS /*!< 12-hour format */
  274. #define RTC_TIMESTAMP_RISING_EDGE ((uint32_t)0x00000000U) /*!< rising edge is valid event edge for time-stamp event */
  275. #define RTC_TIMESTAMP_FALLING_EDGE RTC_CTL_TSEG /*!< falling edge is valid event edge for time-stamp event */
  276. /* psc register value */
  277. #define PSC_FACTOR_S(regval) (BITS(0,14) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_PSC_FACTOR_S bit field */
  278. #define GET_PSC_FACTOR_S(regval) GET_BITS((regval),0,14) /*!< get value of RTC_PSC_FACTOR_S bit field */
  279. #define PSC_FACTOR_A(regval) (BITS(16,22) & ((uint32_t)(regval) << 16U)) /*!< write value to RTC_PSC_FACTOR_A bit field */
  280. #define GET_PSC_FACTOR_A(regval) GET_BITS((regval),16,22) /*!< get value of RTC_PSC_FACTOR_A bit field */
  281. /* alrm0td register value */
  282. #define ALRM0TD_SC(regval) (BITS(0,6) & ((uint32_t)(regval)<< 0U)) /*!< write value to RTC_ALRM0TD_SC bit field */
  283. #define GET_ALRM0TD_SC(regval) GET_BITS((regval),0,6) /*!< get value of RTC_ALRM0TD_SC bit field */
  284. #define ALRM0TD_MN(regval) (BITS(8,14) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_ALRM0TD_MN bit field */
  285. #define GET_ALRM0TD_MN(regval) GET_BITS((regval),8,14) /*!< get value of RTC_ALRM0TD_MN bit field */
  286. #define ALRM0TD_HR(regval) (BITS(16,21) & ((uint32_t)(regval) << 16U)) /*!< write value to RTC_ALRM0TD_HR bit field */
  287. #define GET_ALRM0TD_HR(regval) GET_BITS((regval),16,21) /*!< get value of RTC_ALRM0TD_HR bit field */
  288. #define ALRM0TD_DAY(regval) (BITS(24,29) & ((uint32_t)(regval) << 24U)) /*!< write value to RTC_ALRM0TD_DAY bit field */
  289. #define GET_ALRM0TD_DAY(regval) GET_BITS((regval),24,29) /*!< get value of RTC_ALRM0TD_DAY bit field */
  290. #define RTC_ALARM_NONE_MASK ((uint32_t)0x00000000U) /*!< alarm none mask */
  291. #define RTC_ALARM_DATE_MASK RTC_ALRM0TD_MSKD /*!< alarm date mask */
  292. #define RTC_ALARM_HOUR_MASK RTC_ALRM0TD_MSKH /*!< alarm hour mask */
  293. #define RTC_ALARM_MINUTE_MASK RTC_ALRM0TD_MSKM /*!< alarm minute mask */
  294. #define RTC_ALARM_SECOND_MASK RTC_ALRM0TD_MSKS /*!< alarm second mask */
  295. #define RTC_ALARM_ALL_MASK (RTC_ALRM0TD_MSKD|RTC_ALRM0TD_MSKH|RTC_ALRM0TD_MSKM|RTC_ALRM0TD_MSKS) /*!< alarm all mask */
  296. #define RTC_ALARM_DATE_SELECTED ((uint32_t)0x00000000U) /*!< alarm date format selected */
  297. #define RTC_ALARM_WEEKDAY_SELECTED RTC_ALRM0TD_DOWS /*!< alarm weekday format selected */
  298. /* wpk register value */
  299. #define WPK_WPK(regval) (BITS(0,7) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_WPK_WPK bit field */
  300. /* ss register value */
  301. #define SS_SSC(regval) (BITS(0,15) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_SS_SSC bit field */
  302. /* shiftctl register value */
  303. #define SHIFTCTL_SFS(regval) (BITS(0,14) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_SHIFTCTL_SFS bit field */
  304. #define RTC_SHIFT_ADD1S_RESET ((uint32_t)0x00000000U) /*!< not add 1 second */
  305. #define RTC_SHIFT_ADD1S_SET RTC_SHIFTCTL_A1S /*!< add one second to the clock */
  306. /* tts register value */
  307. #define TTS_SC(regval) (BITS(0,6) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_TTS_SC bit field */
  308. #define GET_TTS_SC(regval) GET_BITS((regval),0,6) /*!< get value of RTC_TTS_SC bit field */
  309. #define TTS_MN(regval) (BITS(8,14) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_TTS_MN bit field */
  310. #define GET_TTS_MN(regval) GET_BITS((regval),8,14) /*!< get value of RTC_TTS_MN bit field */
  311. #define TTS_HR(regval) (BITS(16,21) & ((uint32_t)(regval) << 16U)) /*!< write value to RTC_TTS_HR bit field */
  312. #define GET_TTS_HR(regval) GET_BITS((regval),16,21) /*!< get value of RTC_TTS_HR bit field */
  313. /* dts register value */
  314. #define DTS_DAY(regval) (BITS(0,5) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_DTS_DAY bit field */
  315. #define GET_DTS_DAY(regval) GET_BITS((regval),0,5) /*!< get value of RTC_DTS_DAY bit field */
  316. #define DTS_MON(regval) (BITS(8,12) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_DTS_MON bit field */
  317. #define GET_DTS_MON(regval) GET_BITS((regval),8,12) /*!< get value of RTC_DTS_MON bit field */
  318. #define DTS_DOW(regval) (BITS(13,15) & ((uint32_t)(regval) << 13U)) /*!< write value to RTC_DTS_DOW bit field */
  319. #define GET_DTS_DOW(regval) GET_BITS((regval),13,15) /*!< get value of RTC_DTS_DOW bit field */
  320. /* ssts register value */
  321. #define SSTS_SSC(regval) (BITS(0,15) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_SSTS_SSC bit field */
  322. /* hrfc register value */
  323. #define HRFC_CMSK(regval) (BITS(0,8) & ((uint32_t)(regval) << 0U)) /*!< write value to RTC_HRFC_CMSK bit field */
  324. #define RTC_CALIBRATION_WINDOW_32S ((uint32_t)0x00000000U) /*!< 2exp20 RTCCLK cycles, 32s if RTCCLK = 32768 Hz */
  325. #define RTC_CALIBRATION_WINDOW_16S RTC_HRFC_CWND16 /*!< 2exp19 RTCCLK cycles, 16s if RTCCLK = 32768 Hz */
  326. #define RTC_CALIBRATION_WINDOW_8S RTC_HRFC_CWND8 /*!< 2exp18 RTCCLK cycles, 8s if RTCCLK = 32768 Hz */
  327. #define RTC_CALIBRATION_PLUS_SET RTC_HRFC_FREQI /*!< increase RTC frequency by 488.5ppm */
  328. #define RTC_CALIBRATION_PLUS_RESET ((uint32_t)0x00000000U) /*!< no effect */
  329. /* tamp register value */
  330. #define TAMP_FREQ(regval) (BITS(8,10) & ((uint32_t)(regval) << 8U)) /*!< write value to RTC_TAMP_FREQ bit field */
  331. #define RTC_FREQ_DIV32768 TAMP_FREQ(0) /*!< sample once every 32768 RTCCLK(1Hz if RTCCLK=32.768KHz) */
  332. #define RTC_FREQ_DIV16384 TAMP_FREQ(1) /*!< sample once every 16384 RTCCLK(2Hz if RTCCLK=32.768KHz) */
  333. #define RTC_FREQ_DIV8192 TAMP_FREQ(2) /*!< sample once every 8192 RTCCLK(4Hz if RTCCLK=32.768KHz) */
  334. #define RTC_FREQ_DIV4096 TAMP_FREQ(3) /*!< sample once every 4096 RTCCLK(8Hz if RTCCLK=32.768KHz) */
  335. #define RTC_FREQ_DIV2048 TAMP_FREQ(4) /*!< sample once every 2048 RTCCLK(16Hz if RTCCLK=32.768KHz) */
  336. #define RTC_FREQ_DIV1024 TAMP_FREQ(5) /*!< sample once every 1024 RTCCLK(32Hz if RTCCLK=32.768KHz) */
  337. #define RTC_FREQ_DIV512 TAMP_FREQ(6) /*!< sample once every 512 RTCCLK(64Hz if RTCCLK=32.768KHz) */
  338. #define RTC_FREQ_DIV256 TAMP_FREQ(7) /*!< sample once every 256 RTCCLK(128Hz if RTCCLK=32.768KHz) */
  339. #define TAMP_FLT(regval) (BITS(11,12) & ((uint32_t)(regval) << 11U)) /*!< write value to RTC_TAMP_FLT bit field */
  340. #define RTC_FLT_EDGE TAMP_FLT(0) /*!< detecting tamper event using edge mode. precharge duration is disabled automatically */
  341. #define RTC_FLT_2S TAMP_FLT(1) /*!< detecting tamper event using level mode.2 consecutive valid level samples will make a effective tamper event */
  342. #define RTC_FLT_4S TAMP_FLT(2) /*!< detecting tamper event using level mode.4 consecutive valid level samples will make an effective tamper event */
  343. #define RTC_FLT_8S TAMP_FLT(3) /*!< detecting tamper event using level mode.8 consecutive valid level samples will make a effective tamper event */
  344. #define TAMP_PRCH(regval) (BITS(13,14) & ((uint32_t)(regval) << 13U)) /*!< write value to RTC_TAMP_PRCH bit field */
  345. #define RTC_PRCH_1C TAMP_PRCH(0) /*!< 1 RTC clock prechagre time before each sampling */
  346. #define RTC_PRCH_2C TAMP_PRCH(1) /*!< 2 RTC clock prechagre time before each sampling */
  347. #define RTC_PRCH_4C TAMP_PRCH(2) /*!< 4 RTC clock prechagre time before each sampling */
  348. #define RTC_PRCH_8C TAMP_PRCH(3) /*!< 8 RTC clock prechagre time before each sampling */
  349. #define RTC_TAMPER0 RTC_TAMP_TP0EN /*!< tamper 0 detection enable */
  350. #define RTC_TAMPER1 RTC_TAMP_TP1EN /*!< tamper 1 detection enable */
  351. #define RTC_TAMPER_TRIGGER_EDGE_RISING ((uint32_t)0x00000000U) /*!< tamper detection is in rising edge mode */
  352. #define RTC_TAMPER_TRIGGER_EDGE_FALLING ((uint32_t)0x00000001U) /*!< tamper detection is in falling edge mode */
  353. #define RTC_TAMPER_TRIGGER_LEVEL_LOW ((uint32_t)0x00000000U) /*!< tamper detection is in low level mode */
  354. #define RTC_TAMPER_TRIGGER_LEVEL_HIGH ((uint32_t)0x00000001U) /*!< tamper detection is in high level mode */
  355. #define RTC_TAMPER_TRIGGER_POS ((uint32_t)0x00000001U) /* shift position of trigger relative to source */
  356. #define RTC_ALARM_OUTPUT_OD ((uint32_t)0x00000000U) /*!< RTC alarm output open-drain mode */
  357. #define RTC_ALARM_OUTPUT_PP RTC_TAMP_PC13VAL /*!< RTC alarm output push-pull mode */
  358. /* alrm0ss register value */
  359. #define ALRM0SS_SSC(regval) (BITS(0,14) & ((uint32_t)(regval)<< 0U)) /*!< write value to RTC_ALRM0SS_SSC bit field */
  360. #define ALRM0SS_MASKSSC(regval) (BITS(24,27) & ((uint32_t)(regval) << 24U)) /*!< write value to RTC_ALRM0SS_MASKSSC bit field */
  361. #define RTC_MASKSSC_0_14 ALRM0SS_MASKSSC(0) /*!< mask alarm subsecond configuration */
  362. #define RTC_MASKSSC_1_14 ALRM0SS_MASKSSC(1) /*!< mask RTC_ALRM0SS_SSC[14:1], and RTC_ALRM0SS_SSC[0] is to be compared */
  363. #define RTC_MASKSSC_2_14 ALRM0SS_MASKSSC(2) /*!< mask RTC_ALRM0SS_SSC[14:2], and RTC_ALRM0SS_SSC[1:0] is to be compared */
  364. #define RTC_MASKSSC_3_14 ALRM0SS_MASKSSC(3) /*!< mask RTC_ALRM0SS_SSC[14:3], and RTC_ALRM0SS_SSC[2:0] is to be compared */
  365. #define RTC_MASKSSC_4_14 ALRM0SS_MASKSSC(4) /*!< mask RTC_ALRM0SS_SSC[14:4], and RTC_ALRM0SS_SSC[3:0] is to be compared */
  366. #define RTC_MASKSSC_5_14 ALRM0SS_MASKSSC(5) /*!< mask RTC_ALRM0SS_SSC[14:5], and RTC_ALRM0SS_SSC[4:0] is to be compared */
  367. #define RTC_MASKSSC_6_14 ALRM0SS_MASKSSC(6) /*!< mask RTC_ALRM0SS_SSC[14:6], and RTC_ALRM0SS_SSC[5:0] is to be compared */
  368. #define RTC_MASKSSC_7_14 ALRM0SS_MASKSSC(7) /*!< mask RTC_ALRM0SS_SSC[14:7], and RTC_ALRM0SS_SSC[6:0] is to be compared */
  369. #define RTC_MASKSSC_8_14 ALRM0SS_MASKSSC(8) /*!< mask RTC_ALRM0SS_SSC[14:8], and RTC_ALRM0SS_SSC[7:0] is to be compared */
  370. #define RTC_MASKSSC_9_14 ALRM0SS_MASKSSC(9) /*!< mask RTC_ALRM0SS_SSC[14:9], and RTC_ALRM0SS_SSC[8:0] is to be compared */
  371. #define RTC_MASKSSC_10_14 ALRM0SS_MASKSSC(10) /*!< mask RTC_ALRM0SS_SSC[14:10], and RTC_ALRM0SS_SSC[9:0] is to be compared */
  372. #define RTC_MASKSSC_11_14 ALRM0SS_MASKSSC(11) /*!< mask RTC_ALRM0SS_SSC[14:11], and RTC_ALRM0SS_SSC[10:0] is to be compared */
  373. #define RTC_MASKSSC_12_14 ALRM0SS_MASKSSC(12) /*!< mask RTC_ALRM0SS_SSC[14:12], and RTC_ALRM0SS_SSC[11:0] is to be compared */
  374. #define RTC_MASKSSC_13_14 ALRM0SS_MASKSSC(13) /*!< mask RTC_ALRM0SS_SSC[14:13], and RTC_ALRM0SS_SSC[12:0] is to be compared */
  375. #define RTC_MASKSSC_14 ALRM0SS_MASKSSC(14) /*!< mask RTC_ALRM0SS_SSC[14], and RTC_ALRM0SS_SSC[13:0] is to be compared */
  376. #define RTC_MASKSSC_NONE ALRM0SS_MASKSSC(15) /*!< mask none, and RTC_ALRM0SS_SSC[14:0] is to be compared */
  377. /* RTC interrupt source */
  378. #define RTC_INT_TIMESTAMP RTC_CTL_TSIE /*!< time-stamp interrupt enable */
  379. #define RTC_INT_ALARM RTC_CTL_ALRM0IE /*!< RTC alarm interrupt enable */
  380. #define RTC_INT_TAMP RTC_TAMP_TPIE /*!< tamper detection interrupt enable */
  381. /* write protect key */
  382. #define RTC_UNLOCK_KEY1 ((uint8_t)0xCAU) /*!< RTC unlock key1 */
  383. #define RTC_UNLOCK_KEY2 ((uint8_t)0x53U) /*!< RTC unlock key2 */
  384. #define RTC_LOCK_KEY ((uint8_t)0xFFU) /*!< RTC lock key */
  385. /* registers reset value */
  386. #define RTC_REGISTER_RESET ((uint32_t)0x00000000U) /*!< RTC common register reset value */
  387. #define RTC_DATE_RESET ((uint32_t)0x00002101U) /*!< RTC_DATE register reset value */
  388. #define RTC_STAT_RESET ((uint32_t)0x00000007U) /*!< RTC_STAT register reset value */
  389. #define RTC_PSC_RESET ((uint32_t)0x007F00FFU) /*!< RTC_PSC register reset value */
  390. /* RTC timeout value */
  391. #define RTC_INITM_TIMEOUT ((uint32_t)0x00004000U) /*!< initialization state flag timeout */
  392. #define RTC_RSYNF_TIMEOUT ((uint32_t)0x00008000U) /*!< register synchronization flag timeout */
  393. #define RTC_HRFC_TIMEOUT ((uint32_t)0x00001000U) /*!< recalibration pending flag timeout */
  394. #define RTC_SHIFTCTL_TIMEOUT ((uint32_t)0x00001000U) /*!< shift function operation pending flag timeout */
  395. #define RTC_ALRM0WF_TIMEOUT ((uint32_t)0x00008000U) /*!< alarm configuration can be write flag timeout */
  396. /* RTC flag */
  397. #define RTC_FLAG_RECALIBRATION RTC_STAT_SCPF /*!< recalibration pending flag */
  398. #define RTC_FLAG_TAMP1 RTC_STAT_TP1F /*!< tamper 1 event flag */
  399. #define RTC_FLAG_TAMP0 RTC_STAT_TP0F /*!< tamper 0 event flag */
  400. #define RTC_FLAG_TIMESTAMP_OVERFLOW RTC_STAT_TSOVRF /*!< time-stamp overflow event flag */
  401. #define RTC_FLAG_TIMESTAMP RTC_STAT_TSF /*!< time-stamp event flag */
  402. #define RTC_FLAG_ALARM0 RTC_STAT_ALRM0F /*!< alarm event flag */
  403. #define RTC_FLAG_INIT RTC_STAT_INITF /*!< init mode event flag */
  404. #define RTC_FLAG_RSYN RTC_STAT_RSYNF /*!< registers synchronized flag */
  405. #define RTC_FLAG_YCM RTC_STAT_YCM /*!< year parameter configured event flag */
  406. #define RTC_FLAG_SHIFT RTC_STAT_SOPF /*!< shift operation pending flag */
  407. #define RTC_FLAG_ALARM0_WRITTEN RTC_STAT_ALRM0WF /*!< alarm written available flag */
  408. /* function declarations */
  409. /* reset most of the RTC registers */
  410. ErrStatus rtc_deinit(void);
  411. /* initialize RTC registers */
  412. ErrStatus rtc_init(rtc_parameter_struct* rtc_initpara_struct);
  413. /* enter RTC init mode */
  414. ErrStatus rtc_init_mode_enter(void);
  415. /* exit RTC init mode */
  416. void rtc_init_mode_exit(void);
  417. /* wait until RTC_TIME and RTC_DATE registers are synchronized with APB clock, and the shadow registers are updated */
  418. ErrStatus rtc_register_sync_wait(void);
  419. /* get current time and date */
  420. void rtc_current_time_get(rtc_parameter_struct* rtc_initpara_struct);
  421. /* get current subsecond value */
  422. uint32_t rtc_subsecond_get(void);
  423. /* configure RTC alarm */
  424. void rtc_alarm_config(rtc_alarm_struct* rtc_alarm_time);
  425. /* configure subsecond of RTC alarm */
  426. void rtc_alarm_subsecond_config(uint32_t mask_subsecond, uint32_t subsecond);
  427. /* get RTC alarm */
  428. void rtc_alarm_get(rtc_alarm_struct* rtc_alarm_time);
  429. /* get RTC alarm subsecond */
  430. uint32_t rtc_alarm_subsecond_get(void);
  431. /* enable RTC alarm */
  432. void rtc_alarm_enable(void);
  433. /* disable RTC alarm */
  434. ErrStatus rtc_alarm_disable(void);
  435. /* enable RTC time-stamp */
  436. void rtc_timestamp_enable(uint32_t edge);
  437. /* disable RTC time-stamp */
  438. void rtc_timestamp_disable(void);
  439. /* get RTC timestamp time and date */
  440. void rtc_timestamp_get(rtc_timestamp_struct* rtc_timestamp);
  441. /* get RTC time-stamp subsecond */
  442. uint32_t rtc_timestamp_subsecond_get(void);
  443. /* enable RTC tamper */
  444. void rtc_tamper_enable(rtc_tamper_struct* rtc_tamper);
  445. /* disable RTC tamper */
  446. void rtc_tamper_disable(uint32_t source);
  447. /* enable specified RTC interrupt */
  448. void rtc_interrupt_enable(uint32_t interrupt);
  449. /* disble specified RTC interrupt */
  450. void rtc_interrupt_disable(uint32_t interrupt);
  451. /* check specified flag */
  452. FlagStatus rtc_flag_get(uint32_t flag);
  453. /* clear specified flag */
  454. void rtc_flag_clear(uint32_t flag);
  455. /* configure RTC alternate output source */
  456. void rtc_alter_output_config(uint32_t source, uint32_t mode);
  457. /* configure RTC calibration register */
  458. ErrStatus rtc_calibration_config(uint32_t window, uint32_t plus, uint32_t minus);
  459. /* ajust the daylight saving time by adding or substracting one hour from the current time */
  460. void rtc_hour_adjust(uint32_t operation);
  461. /* ajust RTC second or subsecond value of current time */
  462. ErrStatus rtc_second_adjust(uint32_t add, uint32_t minus);
  463. /* enable RTC bypass shadow registers function */
  464. void rtc_bypass_shadow_enable(void);
  465. /* disable RTC bypass shadow registers function */
  466. void rtc_bypass_shadow_disable(void);
  467. /* enable RTC reference clock detection function */
  468. ErrStatus rtc_refclock_detection_enable(void);
  469. /* disable RTC reference clock detection function */
  470. ErrStatus rtc_refclock_detection_disable(void);
  471. #endif /* GD32E23X_RTC_H */