rtc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /**
  16. * @file
  17. * @brief A real-time clock (RTC) is a computer clock that keeps track of
  18. * the current time.
  19. */
  20. #ifndef _DRIVER_RTC_H
  21. #define _DRIVER_RTC_H
  22. #include <plic.h>
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "platform.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * @brief RTC timer mode
  33. *
  34. * Timer mode selector
  35. * | Mode | Description |
  36. * |------|------------------------|
  37. * | 0 | Timer pause |
  38. * | 1 | Timer time running |
  39. * | 2 | Timer time setting |
  40. */
  41. typedef enum _rtc_timer_mode_e
  42. {
  43. RTC_TIMER_PAUSE, /*!< 0: Timer pause */
  44. RTC_TIMER_RUNNING, /*!< 1: Timer time running */
  45. RTC_TIMER_SETTING, /*!< 2: Timer time setting */
  46. RTC_TIMER_MAX /*!< Max count of this enum*/
  47. } rtc_timer_mode_t;
  48. /*
  49. * @brief RTC tick interrupt mode
  50. *
  51. * Tick interrupt mode selector
  52. * | Mode | Description |
  53. * |------|------------------------|
  54. * | 0 | Interrupt every second |
  55. * | 1 | Interrupt every minute |
  56. * | 2 | Interrupt every hour |
  57. * | 3 | Interrupt every day |
  58. */
  59. typedef enum _rtc_tick_interrupt_mode_e
  60. {
  61. RTC_INT_SECOND, /*!< 0: Interrupt every second */
  62. RTC_INT_MINUTE, /*!< 1: Interrupt every minute */
  63. RTC_INT_HOUR, /*!< 2: Interrupt every hour */
  64. RTC_INT_DAY, /*!< 3: Interrupt every day */
  65. RTC_INT_MAX /*!< Max count of this enum*/
  66. } rtc_tick_interrupt_mode_t;
  67. /**
  68. * @brief RTC mask structure
  69. *
  70. * RTC mask structure for common use
  71. */
  72. typedef struct _rtc_mask
  73. {
  74. uint32_t resv : 1; /*!< Reserved */
  75. uint32_t second : 1; /*!< Second mask */
  76. uint32_t minute : 1; /*!< Minute mask */
  77. uint32_t hour : 1; /*!< Hour mask */
  78. uint32_t week : 1; /*!< Week mask */
  79. uint32_t day : 1; /*!< Day mask */
  80. uint32_t month : 1; /*!< Month mask */
  81. uint32_t year : 1; /*!< Year mask */
  82. } __attribute__((packed, aligned(1))) rtc_mask_t;
  83. /**
  84. * @brief RTC register
  85. *
  86. * @note RTC register table
  87. *
  88. * | Offset | Name | Description |
  89. * |-----------|----------------|-------------------------------------|
  90. * | 0x00 | date | Timer date information |
  91. * | 0x04 | time | Timer time information |
  92. * | 0x08 | alarm_date | Alarm date information |
  93. * | 0x0c | alarm_time | Alarm time information |
  94. * | 0x10 | initial_count | Timer counter initial value |
  95. * | 0x14 | current_count | Timer counter current value |
  96. * | 0x18 | interrupt_ctrl | RTC interrupt settings |
  97. * | 0x1c | register_ctrl | RTC register settings |
  98. * | 0x20 | reserved0 | Reserved |
  99. * | 0x24 | reserved1 | Reserved |
  100. * | 0x28 | extended | Timer extended information |
  101. *
  102. */
  103. /**
  104. * @brief Timer date information
  105. *
  106. * No. 0 Register (0x00)
  107. */
  108. typedef struct _rtc_date
  109. {
  110. uint32_t week : 3; /*!< Week. Range [0,6]. 0 is Sunday. */
  111. uint32_t resv0 : 5; /*!< Reserved */
  112. uint32_t day : 5; /*!< Day. Range [1,31] or [1,30] or [1,29] or [1,28] */
  113. uint32_t resv1 : 3; /*!< Reserved */
  114. uint32_t month : 4; /*!< Month. Range [1,12] */
  115. uint32_t year : 12; /*!< Year. Range [0,99] */
  116. } __attribute__((packed, aligned(4))) rtc_date_t;
  117. /**
  118. * @brief Timer time information
  119. *
  120. * No. 1 Register (0x04)
  121. */
  122. typedef struct _rtc_time
  123. {
  124. uint32_t resv0 : 10; /*!< Reserved */
  125. uint32_t second : 6; /*!< Second. Range [0,59] */
  126. uint32_t minute : 6; /*!< Minute. Range [0,59] */
  127. uint32_t resv1 : 2; /*!< Reserved */
  128. uint32_t hour : 5; /*!< Hour. Range [0,23] */
  129. uint32_t resv2 : 3; /*!< Reserved */
  130. } __attribute__((packed, aligned(4))) rtc_time_t;
  131. typedef struct _rtc_date_time
  132. {
  133. uint32_t sec : 6;
  134. uint32_t min : 6;
  135. uint32_t hour : 5;
  136. uint32_t week : 3;
  137. uint32_t day : 5;
  138. uint32_t month : 4;
  139. uint16_t year;
  140. } rtc_date_time_t;
  141. /**
  142. * @brief Alarm date information
  143. *
  144. * No. 2 Register (0x08)
  145. */
  146. typedef struct _rtc_alarm_date
  147. {
  148. uint32_t week : 3; /*!< Alarm Week. Range [0,6]. 0 is Sunday. */
  149. uint32_t resv0 : 5; /*!< Reserved */
  150. uint32_t day : 5; /*!< Alarm Day. Range [1,31] or [1,30] or [1,29] or [1,28] */
  151. uint32_t resv1 : 3; /*!< Reserved */
  152. uint32_t month : 4; /*!< Alarm Month. Range [1,12] */
  153. uint32_t year : 12; /*!< Alarm Year. Range [0,99] */
  154. } __attribute__((packed, aligned(4))) rtc_alarm_date_t;
  155. /**
  156. * @brief Alarm time information
  157. *
  158. * No. 3 Register (0x0c)
  159. */
  160. typedef struct _rtc_alarm_time
  161. {
  162. uint32_t resv0 : 10; /*!< Reserved */
  163. uint32_t second : 6; /*!< Alarm Second. Range [0,59] */
  164. uint32_t minute : 6; /*!< Alarm Minute. Range [0,59] */
  165. uint32_t resv1 : 2; /*!< Reserved */
  166. uint32_t hour : 5; /*!< Alarm Hour. Range [0,23] */
  167. uint32_t resv2 : 3; /*!< Reserved */
  168. } __attribute__((packed, aligned(4))) rtc_alarm_time_t;
  169. /**
  170. * @brief Timer counter initial value
  171. *
  172. * No. 4 Register (0x10)
  173. */
  174. typedef struct _rtc_initial_count
  175. {
  176. uint32_t count : 32; /*!< RTC counter initial value */
  177. } __attribute__((packed, aligned(4))) rtc_initial_count_t;
  178. /**
  179. * @brief Timer counter current value
  180. *
  181. * No. 5 Register (0x14)
  182. */
  183. typedef struct _rtc_current_count
  184. {
  185. uint32_t count : 32; /*!< RTC counter current value */
  186. } __attribute__((packed, aligned(4))) rtc_current_count_t;
  187. /**
  188. * @brief RTC interrupt settings
  189. *
  190. * No. 6 Register (0x18)
  191. */
  192. typedef struct _rtc_interrupt_ctrl
  193. {
  194. uint32_t tick_enable : 1; /*!< Reserved */
  195. uint32_t alarm_enable : 1; /*!< Alarm interrupt enable */
  196. uint32_t tick_int_mode : 2; /*!< Tick interrupt enable */
  197. uint32_t resv : 20; /*!< Reserved */
  198. uint32_t alarm_compare_mask : 8; /*!< Alarm compare mask for interrupt */
  199. } __attribute__((packed, aligned(4))) rtc_interrupt_ctrl_t;
  200. /**
  201. * @brief RTC register settings
  202. *
  203. * No. 7 Register (0x1c)
  204. */
  205. typedef struct _rtc_register_ctrl
  206. {
  207. uint32_t read_enable : 1; /*!< RTC timer read enable */
  208. uint32_t write_enable : 1; /*!< RTC timer write enable */
  209. uint32_t resv0 : 11; /*!< Reserved */
  210. uint32_t timer_mask : 8; /*!< RTC timer mask */
  211. uint32_t alarm_mask : 8; /*!< RTC alarm mask */
  212. uint32_t initial_count_mask : 1; /*!< RTC counter initial count value mask */
  213. uint32_t interrupt_register_mask : 1; /*!< RTC interrupt register mask */
  214. uint32_t resv1 : 1; /*!< Reserved */
  215. } __attribute__((packed, aligned(4))) rtc_register_ctrl_t;
  216. /**
  217. * @brief Reserved
  218. *
  219. * No. 8 Register (0x20)
  220. */
  221. typedef struct _rtc_reserved0
  222. {
  223. uint32_t resv : 32; /*!< Reserved */
  224. } __attribute__((packed, aligned(4))) rtc_reserved0_t;
  225. /**
  226. * @brief Reserved
  227. *
  228. * No. 9 Register (0x24)
  229. */
  230. typedef struct _rtc_reserved1
  231. {
  232. uint32_t resv : 32; /*!< Reserved */
  233. } __attribute__((packed, aligned(4))) rtc_reserved1_t;
  234. /**
  235. * @brief Timer extended information
  236. *
  237. * No. 10 Register (0x28)
  238. */
  239. typedef struct _rtc_extended
  240. {
  241. uint32_t century : 5; /*!< Century. Range [0,31] */
  242. uint32_t leap_year : 1; /*!< Is leap year. 1 is leap year, 0 is not leap year */
  243. uint32_t resv : 26; /*!< Reserved */
  244. ;
  245. } __attribute__((packed, aligned(4))) rtc_extended_t;
  246. /**
  247. * @brief Real-time clock struct
  248. *
  249. * A real-time clock (RTC) is a computer clock that keeps track of
  250. * the current time.
  251. */
  252. typedef struct _rtc
  253. {
  254. rtc_date_t date; /*!< No. 0 (0x00): Timer date information */
  255. rtc_time_t time; /*!< No. 1 (0x04): Timer time information */
  256. rtc_alarm_date_t alarm_date; /*!< No. 2 (0x08): Alarm date information */
  257. rtc_alarm_time_t alarm_time; /*!< No. 3 (0x0c): Alarm time information */
  258. rtc_initial_count_t initial_count; /*!< No. 4 (0x10): Timer counter initial value */
  259. rtc_current_count_t current_count; /*!< No. 5 (0x14): Timer counter current value */
  260. rtc_interrupt_ctrl_t interrupt_ctrl; /*!< No. 6 (0x18): RTC interrupt settings */
  261. rtc_register_ctrl_t register_ctrl; /*!< No. 7 (0x1c): RTC register settings */
  262. rtc_reserved0_t reserved0; /*!< No. 8 (0x20): Reserved */
  263. rtc_reserved1_t reserved1; /*!< No. 9 (0x24): Reserved */
  264. rtc_extended_t extended; /*!< No. 10 (0x28): Timer extended information */
  265. } __attribute__((packed, aligned(4))) rtc_t;
  266. /**
  267. * @brief Real-time clock object
  268. */
  269. extern volatile rtc_t *const rtc;
  270. extern volatile uint32_t *const rtc_base;
  271. /**
  272. * @brief Set RTC timer mode
  273. *
  274. * @param[in] timer_mode The timer mode
  275. *
  276. * @return Result
  277. * - 0 Success
  278. * - Other Fail
  279. */
  280. int rtc_timer_set_mode(rtc_timer_mode_t timer_mode);
  281. /**
  282. * @brief Get RTC timer mode
  283. *
  284. * @return The timer mode
  285. */
  286. rtc_timer_mode_t rtc_timer_get_mode(void);
  287. /**
  288. * @brief Set date time to RTC
  289. *
  290. * @param[in] tm The Broken-down date time
  291. *
  292. * @return Result
  293. * - 0 Success
  294. * - Other Fail
  295. */
  296. int rtc_timer_set_tm(const struct tm *tm);
  297. /**
  298. * @brief Get date time from RTC
  299. *
  300. * @return The Broken-down date time
  301. */
  302. struct tm *rtc_timer_get_tm(void);
  303. /**
  304. * @brief Set date time to Alarm
  305. *
  306. * @param[in] tm The Broken-down date time
  307. *
  308. * @return Result
  309. * - 0 Success
  310. * - Other Fail
  311. */
  312. int rtc_timer_set_alarm_tm(const struct tm *tm);
  313. /**
  314. * @brief Get date time from Alarm
  315. *
  316. * @return The Broken-down date time
  317. */
  318. struct tm *rtc_timer_get_alarm_tm(void);
  319. /**
  320. * @brief Check if it is a leap year
  321. *
  322. * @param[in] year The year
  323. *
  324. * @return Result
  325. * - 0 Not leap year
  326. * - Other Leap year
  327. */
  328. int rtc_year_is_leap(int year);
  329. /**
  330. * @brief Get day of year from date
  331. *
  332. * @param[in] year The year
  333. * @param[in] month The month
  334. * @param[in] day The day
  335. *
  336. * @return The day of year from date
  337. */
  338. int rtc_get_yday(int year, int month, int day);
  339. /**
  340. * @brief Get the day of the week from date
  341. *
  342. * @param[in] year The year
  343. * @param[in] month The month
  344. * @param[in] day The day
  345. *
  346. * @return The day of the week.
  347. * Where Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3,
  348. * Thursday = 4, Friday = 5, Saturday = 6.
  349. */
  350. int rtc_get_wday(int year, int month, int day);
  351. /**
  352. * @brief Set date time to RTC
  353. *
  354. * @param[in] year The year
  355. * @param[in] month The month
  356. * @param[in] day The day
  357. * @param[in] hour The hour
  358. * @param[in] minute The minute
  359. * @param[in] second The second
  360. *
  361. * @return Result
  362. * - 0 Success
  363. * - Other Fail
  364. */
  365. int rtc_timer_set(int year, int month, int day, int hour, int minute, int second);
  366. /**
  367. * @brief Get date time from RTC
  368. *
  369. * @param year The year
  370. * @param month The month
  371. * @param day The day
  372. * @param hour The hour
  373. * @param minute The minute
  374. * @param second The second
  375. *
  376. * @return Result
  377. * - 0 Success
  378. * - Other Fail
  379. */
  380. int rtc_timer_get(int *year, int *month, int *day, int *hour, int *minute, int *second);
  381. /**
  382. * @brief Set date time to Alarm
  383. *
  384. * @param[in] year The year
  385. * @param[in] month The month
  386. * @param[in] day The day
  387. * @param[in] hour The hour
  388. * @param[in] minute The minute
  389. * @param[in] second The second
  390. *
  391. * @return Result
  392. * - 0 Success
  393. * - Other Fail
  394. */
  395. int rtc_alarm_set(int year, int month, int day, int hour, int minute, int second);
  396. /**
  397. * @brief Get date time from Alarm
  398. *
  399. * @param year The year
  400. * @param month The month
  401. * @param day The day
  402. * @param hour The hour
  403. * @param minute The minute
  404. * @param second The second
  405. *
  406. * @return Result
  407. * - 0 Success
  408. * - Other Fail
  409. */
  410. int rtc_alarm_get(int *year, int *month, int *day, int *hour, int *minute, int *second);
  411. /**
  412. * @brief Get RTC timer clock count value
  413. *
  414. * @return unsigned int, the value of counter
  415. */
  416. unsigned int rtc_timer_get_clock_count_value(void);
  417. /**
  418. * @brief Enable or disable RTC tick interrupt
  419. *
  420. * @param enable Enable or disable RTC tick interrupt
  421. * @return Result
  422. * - 0 Success
  423. * - Other Fail
  424. */
  425. int rtc_tick_interrupt_set(int enable);
  426. /**
  427. * @brief Get the enable status of RTC tick interrupt
  428. *
  429. * @return The enable status of RTC tick interrupt
  430. */
  431. int rtc_tick_interrupt_get(void);
  432. /**
  433. * @brief Set the interrupt mode of RTC tick interrupt
  434. *
  435. * @param mode The mode of RTC tick interrupt
  436. * - RTC_INT_SECOND, 0: Interrupt every second
  437. * - RTC_INT_MINUTE, 1: Interrupt every minute
  438. * - RTC_INT_HOUR, 2: Interrupt every hour
  439. * - RTC_INT_DAY, 3: Interrupt every day
  440. * @return Result
  441. * - 0 Success
  442. * - Other Fail
  443. */
  444. int rtc_tick_set_interrupt_mode(rtc_tick_interrupt_mode_t mode);
  445. /**
  446. * @brief Get the interrupt mode of RTC tick interrupt
  447. *
  448. * @return mode The mode of RTC tick interrupt
  449. * - RTC_INT_SECOND, 0: Interrupt every second
  450. * - RTC_INT_MINUTE, 1: Interrupt every minute
  451. * - RTC_INT_HOUR, 2: Interrupt every hour
  452. * - RTC_INT_DAY, 3: Interrupt every day
  453. */
  454. rtc_tick_interrupt_mode_t rtc_tick_get_interrupt_mode(void);
  455. /**
  456. * @brief Enable or disable RTC alarm interrupt
  457. *
  458. * @param enable Enable or disable RTC alarm interrupt
  459. * @return Result
  460. * - 0 Success
  461. * - Other Fail
  462. */
  463. int rtc_alarm_set_interrupt(int enable);
  464. /**
  465. * @brief Get the enable status of RTC alarm interrupt
  466. *
  467. * @return The enable status of RTC alarm interrupt
  468. */
  469. int rtc_alarm_get_interrupt(void);
  470. /**
  471. * @brief Set the compare mask for RTC alarm interrupt
  472. *
  473. * @param mask The alarm compare mask for RTC alarm interrupt
  474. * (rtc_mask_t) {
  475. * .second = 1, Set this mask to compare Second
  476. * .minute = 0, Set this mask to compare Minute
  477. * .hour = 0, Set this mask to compare Hour
  478. * .week = 0, Set this mask to compare Week
  479. * .day = 0, Set this mask to compare Day
  480. * .month = 0, Set this mask to compare Month
  481. * .year = 0, Set this mask to compare Year
  482. * }
  483. * @return Result
  484. * - 0 Success
  485. * - Other Fail
  486. */
  487. int rtc_alarm_set_mask(rtc_mask_t mask);
  488. /**
  489. * @brief Get the compare mask for RTC alarm interrupt
  490. *
  491. * @return mask The alarm compare mask for RTC alarm interrupt
  492. * (rtc_mask_t) {
  493. * .second = 1, Set this mask to compare Second
  494. * .minute = 0, Set this mask to compare Minute
  495. * .hour = 0, Set this mask to compare Hour
  496. * .week = 0, Set this mask to compare Week
  497. * .day = 0, Set this mask to compare Day
  498. * .month = 0, Set this mask to compare Month
  499. * .year = 0, Set this mask to compare Year
  500. * }
  501. */
  502. rtc_mask_t rtc_alarm_get_mask(void);
  503. /**
  504. * @brief Initialize RTC
  505. *
  506. * @return Result
  507. * - 0 Success
  508. * - Other Fail
  509. */
  510. int rtc_init(void);
  511. /**
  512. * @brief Register callback of tick interrupt
  513. *
  514. * @param is_single_shot Indicates if single shot
  515. * @param mode Tick interrupt mode
  516. 0:second
  517. 1:minute
  518. 2:hour
  519. 3:day
  520. * @param callback Callback of tick interrupt
  521. * @param ctx Param of callback
  522. * @param priority Priority of tick interrupt
  523. *
  524. * @return result
  525. * - 0 Success
  526. * - Other Fail
  527. */
  528. int rtc_tick_irq_register(bool is_single_shot, rtc_tick_interrupt_mode_t mode, plic_irq_callback_t callback, void *ctx, uint8_t priority);
  529. /**
  530. * @brief Unregister tick interrupt
  531. *
  532. * @return Result
  533. * - 0 Success
  534. * - Other Fail
  535. */
  536. void rtc_tick_irq_unregister(void);
  537. /**
  538. * @brief Register callback of alarm interrupt
  539. *
  540. * @param is_single_shot Indicates if single shot
  541. * @param mask The alarm compare mask for RTC alarm interrupt
  542. * (rtc_mask_t) {
  543. * .second = 1, Set this mask to compare Second
  544. * .minute = 0, Set this mask to compare Minute
  545. * .hour = 0, Set this mask to compare Hour
  546. * .week = 0, Set this mask to compare Week
  547. * .day = 0, Set this mask to compare Day
  548. * .month = 0, Set this mask to compare Month
  549. * .year = 0, Set this mask to compare Year
  550. * }
  551. * @param callback Callback of tick interrupt
  552. * @param ctx Param of callback
  553. * @param priority Priority of tick interrupt
  554. *
  555. * @return result
  556. * - 0 Success
  557. * - Other Fail
  558. */
  559. int rtc_alarm_irq_register(bool is_single_shot, rtc_mask_t mask, plic_irq_callback_t callback, void *ctx, uint8_t priority);
  560. /**
  561. * @brief Unregister alarm interrupt
  562. *
  563. * @return Result
  564. * - 0 Success
  565. * - Other Fail
  566. */
  567. void rtc_alarm_irq_unregister(void);
  568. #ifdef __cplusplus
  569. }
  570. #endif
  571. #endif /* _DRIVER_RTC_H */