drv_rtc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-14 Mr.Tiger first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include <sys/time.h>
  14. #include "hal_data.h"
  15. #ifdef BSP_USING_ONCHIP_RTC
  16. #define DBG_TAG "drv.rtc"
  17. #ifdef DRV_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif /* DRV_DEBUG */
  22. #include <rtdbg.h>
  23. static rt_err_t ra_rtc_init(void)
  24. {
  25. rt_err_t result = RT_EOK;
  26. if (R_RTC_Open(&g_rtc_ctrl, &g_rtc_cfg) != RT_EOK)
  27. {
  28. LOG_E("rtc init failed.");
  29. result = -RT_ERROR;
  30. }
  31. rtc_time_t default_set_time =
  32. {
  33. .tm_sec = 0,
  34. .tm_min = 0,
  35. .tm_hour = 0,
  36. .tm_mday = 1,
  37. .tm_wday = 1,
  38. .tm_mon = 1,
  39. .tm_year = 1900,
  40. };
  41. R_RTC_CalendarTimeSet(&g_rtc_ctrl, &default_set_time);
  42. return result;
  43. }
  44. static time_t get_rtc_timestamp(void)
  45. {
  46. struct tm tm_new = {0};
  47. rtc_time_t g_current_time = {0};
  48. R_RTC_CalendarTimeGet(&g_rtc_ctrl, &g_current_time);
  49. tm_new.tm_year = g_current_time.tm_year;
  50. tm_new.tm_mon = g_current_time.tm_mon;
  51. tm_new.tm_mday = g_current_time.tm_mday;
  52. tm_new.tm_hour = g_current_time.tm_hour;
  53. tm_new.tm_min = g_current_time.tm_min;
  54. tm_new.tm_sec = g_current_time.tm_sec;
  55. tm_new.tm_wday = g_current_time.tm_wday;
  56. tm_new.tm_yday = g_current_time.tm_yday;
  57. tm_new.tm_isdst = g_current_time.tm_isdst;
  58. return timegm(&tm_new);
  59. }
  60. static rt_err_t ra_get_secs(time_t *sec)
  61. {
  62. *(rt_uint32_t *)sec = get_rtc_timestamp();
  63. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)sec);
  64. return RT_EOK;
  65. }
  66. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  67. {
  68. struct tm now;
  69. rtc_time_t g_current_time = {0};
  70. gmtime_r(&time_stamp, &now);
  71. if (now.tm_year < 100)
  72. {
  73. return -RT_ERROR;
  74. }
  75. g_current_time.tm_sec = now.tm_sec ;
  76. g_current_time.tm_min = now.tm_min ;
  77. g_current_time.tm_hour = now.tm_hour;
  78. g_current_time.tm_mday = now.tm_mday;
  79. g_current_time.tm_mon = now.tm_mon;
  80. g_current_time.tm_year = now.tm_year;
  81. g_current_time.tm_wday = now.tm_wday;
  82. g_current_time.tm_yday = now.tm_yday;
  83. if (R_RTC_CalendarTimeSet(&g_rtc_ctrl, &g_current_time) != FSP_SUCCESS)
  84. {
  85. LOG_E("set rtc time failed.");
  86. return -RT_ERROR;
  87. }
  88. return RT_EOK;
  89. }
  90. static rt_err_t ra_set_secs(time_t *sec)
  91. {
  92. rt_err_t result = RT_EOK;
  93. if (set_rtc_time_stamp(*(rt_uint32_t *)sec))
  94. {
  95. result = -RT_ERROR;
  96. }
  97. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)sec);
  98. return result;
  99. }
  100. #ifdef RT_USING_ALARM
  101. static rt_err_t ra_get_alarm(struct rt_rtc_wkalarm *alarm)
  102. {
  103. rt_err_t result = RT_EOK;
  104. struct rt_rtc_wkalarm *wkalarm = alarm;
  105. rtc_alarm_time_t alarm_time_get =
  106. {
  107. .sec_match = RT_FALSE,
  108. .min_match = RT_FALSE,
  109. .hour_match = RT_FALSE,
  110. .mday_match = RT_FALSE,
  111. .mon_match = RT_FALSE,
  112. .year_match = RT_FALSE,
  113. .dayofweek_match = RT_FALSE,
  114. };
  115. if (RT_EOK == R_RTC_CalendarAlarmGet(&g_rtc_ctrl, &alarm_time_get))
  116. {
  117. wkalarm->tm_hour = alarm_time_get.time.tm_hour;
  118. wkalarm->tm_min = alarm_time_get.time.tm_min;
  119. wkalarm->tm_sec = alarm_time_get.time.tm_sec;
  120. }
  121. else
  122. {
  123. LOG_E("Calendar alarm Get failed.");
  124. }
  125. return result;
  126. }
  127. static rt_err_t ra_set_alarm(struct rt_rtc_wkalarm *alarm)
  128. {
  129. rt_err_t result = RT_EOK;
  130. struct rt_rtc_wkalarm *wkalarm = alarm;
  131. rtc_alarm_time_t alarm_time_set =
  132. {
  133. .sec_match = RT_TRUE,
  134. .min_match = RT_TRUE,
  135. .hour_match = RT_TRUE,
  136. .mday_match = RT_FALSE,
  137. .mon_match = RT_FALSE,
  138. .year_match = RT_FALSE,
  139. .dayofweek_match = RT_FALSE,
  140. };
  141. alarm_time_set.time.tm_hour = wkalarm->tm_hour;
  142. alarm_time_set.time.tm_min = wkalarm->tm_min;
  143. alarm_time_set.time.tm_sec = wkalarm->tm_sec;
  144. if (1 == wkalarm->enable)
  145. {
  146. if (RT_EOK != R_RTC_CalendarAlarmSet(&g_rtc_ctrl, &alarm_time_set))
  147. {
  148. LOG_E("Calendar alarm Set failed.");
  149. result = -RT_ERROR;
  150. }
  151. }
  152. else
  153. {
  154. alarm_time_set.sec_match = RT_FALSE;
  155. alarm_time_set.min_match = RT_FALSE;
  156. alarm_time_set.hour_match = RT_FALSE;
  157. if (RT_EOK != R_RTC_CalendarAlarmSet(&g_rtc_ctrl, &alarm_time_set))
  158. {
  159. LOG_E("Calendar alarm Stop failed.");
  160. result = -RT_ERROR;
  161. }
  162. }
  163. return result;
  164. }
  165. #endif /* RT_USING_ALARM */
  166. void rtc_callback(rtc_callback_args_t *p_args)
  167. {
  168. #ifdef RT_USING_ALARM
  169. static rt_device_t ra_device;
  170. if (RTC_EVENT_ALARM_IRQ == p_args->event)
  171. {
  172. rt_alarm_update(ra_device, 1);
  173. }
  174. #endif
  175. }
  176. static const struct rt_rtc_ops ra_rtc_ops =
  177. {
  178. .init = ra_rtc_init,
  179. .get_secs = ra_get_secs,
  180. .set_secs = ra_set_secs,
  181. #ifdef RT_USING_ALARM
  182. .set_alarm = ra_set_alarm,
  183. .get_alarm = ra_get_alarm,
  184. #endif
  185. };
  186. static rt_rtc_dev_t ra_rtc_dev;
  187. static int rt_hw_rtc_init(void)
  188. {
  189. rt_err_t result;
  190. ra_rtc_dev.ops = &ra_rtc_ops;
  191. result = rt_hw_rtc_register(&ra_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  192. if (result != RT_EOK)
  193. {
  194. LOG_E("rtc register err code: %d", result);
  195. return result;
  196. }
  197. LOG_D("rtc init success");
  198. return RT_EOK;
  199. }
  200. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  201. #endif