drv_rtc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025 RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #ifndef __DRV_RTC_H__
  31. #define __DRV_RTC_H__
  32. #include <stdint.h>
  33. #include <drivers/dev_rtc.h>
  34. #define RT_DEVICE_CTRL_RTC_SET_CALLBACK 0x44
  35. #define RT_DEVICE_CTRL_RTC_STOP_ALARM 0x45
  36. #define RT_DEVICE_CTRL_RTC_STOP_TICK 0x46
  37. #define BIT(n) (1 << n)
  38. /* date register(reset value 0x10101, offset address 0x00) */
  39. typedef struct _rtc_date
  40. {
  41. uint32_t day : 5;
  42. uint32_t resv0 : 3;
  43. uint32_t month : 4;
  44. uint32_t resv1 : 4;
  45. uint32_t year_l : 7;
  46. uint32_t leap_year : 1;
  47. uint32_t year_h : 7;
  48. uint32_t resv2 : 1;
  49. } __attribute__((packed, aligned(4))) rtc_date_t;
  50. /* time register(reset value 0x00, offset address 0x04) */
  51. typedef struct _rtc_time
  52. {
  53. uint32_t second : 6;
  54. uint32_t resv0 : 2;
  55. uint32_t minute : 6;
  56. uint32_t resv1 : 2;
  57. uint32_t hour : 5;
  58. uint32_t resv2 : 3;
  59. uint32_t week : 3;
  60. uint32_t resv3 : 5;
  61. } __attribute__((packed, aligned(4))) rtc_time_t;
  62. /* alarm date register(reset value 0x10101, offset address 0x08) */
  63. typedef struct _rtc_alarm_date
  64. {
  65. uint32_t alarm_day : 5;
  66. uint32_t resv0 : 3;
  67. uint32_t alarm_month : 4;
  68. uint32_t resv1 : 4;
  69. uint32_t alarm_year_l : 7;
  70. uint32_t resv2 : 1;
  71. uint32_t alarm_year_h : 7;
  72. uint32_t resv3 : 1;
  73. } __attribute__((packed, aligned(4))) rtc_alarm_date_t;
  74. /* alarm time register(reset value 0x00, offset address 0x0C) */
  75. typedef struct _rtc_alarm_time
  76. {
  77. uint32_t alarm_second : 6;
  78. uint32_t resv0 : 2;
  79. uint32_t alarm_minute : 6;
  80. uint32_t resv1 : 2;
  81. uint32_t alarm_hour : 5;
  82. uint32_t resv2 : 3;
  83. uint32_t alarm_week : 3;
  84. uint32_t resv3 : 5;
  85. } __attribute__((packed, aligned(4))) rtc_alarm_time_t;
  86. /* count register(reset value 0x7FFF0000, offset address 0x10) */
  87. typedef struct _rtc_count
  88. {
  89. uint32_t curr_count : 15; /*!< RTC counter currunt value */
  90. uint32_t resv0 : 1;
  91. uint32_t sum_count : 15; /*!< RTC counter max value */
  92. uint32_t resv1 : 1;
  93. } __attribute__((packed, aligned(4))) rtc_count_t;
  94. /* interrupt control register(reset value 0x00, offset address 0x14) */
  95. typedef struct _rtc_int_ctrl
  96. {
  97. uint32_t timer_w_en : 1;
  98. uint32_t timer_r_en : 1;
  99. uint32_t resv0 : 6;
  100. uint32_t tick_en : 1;
  101. uint32_t tick_sel : 4;
  102. uint32_t resv1 : 3;
  103. uint32_t alarm_en : 1;
  104. uint32_t alarm_clr : 1;
  105. uint32_t resv2 : 6;
  106. uint32_t second_cmp : 1;
  107. uint32_t minute_cmp : 1;
  108. uint32_t hour_cmp : 1;
  109. uint32_t week_cmp : 1;
  110. uint32_t day_cmp : 1;
  111. uint32_t month_cmp : 1;
  112. uint32_t year_cmp : 1;
  113. uint32_t resv3 : 1;
  114. } __attribute__((packed, aligned(4))) rtc_int_ctrl_t;
  115. /* rtc register */
  116. typedef struct _rtc
  117. {
  118. rtc_date_t date;
  119. rtc_time_t time;
  120. rtc_alarm_date_t alarm_date;
  121. rtc_alarm_time_t alarm_time;
  122. rtc_count_t count;
  123. rtc_int_ctrl_t int_ctrl;
  124. } __attribute__((packed, aligned(4))) rtc_t;
  125. typedef enum _rtc_tick_interrupt_mode_e
  126. {
  127. RTC_INT_ALARM_YEAR = BIT(0),
  128. RTC_INT_ALARM_MONTH = BIT(1),
  129. RTC_INT_ALARM_DAY = BIT(2),
  130. RTC_INT_ALARM_WEEK = BIT(3),
  131. RTC_INT_ALARM_HOUR = BIT(4),
  132. RTC_INT_ALARM_MINUTE = BIT(5),
  133. RTC_INT_ALARM_SECOND = BIT(6),
  134. RTC_INT_TICK_YEAR = BIT(7),
  135. RTC_INT_TICK_MONTH,
  136. RTC_INT_TICK_DAY,
  137. RTC_INT_TICK_WEEK,
  138. RTC_INT_TICK_HOUR,
  139. RTC_INT_TICK_MINUTE,
  140. RTC_INT_TICK_SECOND,
  141. RTC_INT_TICK_S8,
  142. RTC_INT_TICK_S64,
  143. } rtc_interrupt_mode_t;
  144. typedef struct _rtc_alarm_setup
  145. {
  146. rt_uint32_t flag; /* alarm flag */
  147. struct tm tm; /* when will the alarm wake up user */
  148. } rtc_alarm_setup_t;
  149. #endif /* __DRV_RTC_H__ */