drv_rtc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2006-2025, 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. #if defined(SOC_SERIES_R9A07G0)
  32. rtc_time_t default_set_time =
  33. {
  34. .tm_sec = 0,
  35. .tm_min = 0,
  36. .tm_hour = 0,
  37. .tm_mday = 1,
  38. .tm_wday = 1,
  39. .tm_mon = 1,
  40. .tm_year = 1900,
  41. };
  42. R_RTC_CalendarTimeSet(&g_rtc_ctrl, &default_set_time);
  43. #endif
  44. return result;
  45. }
  46. static time_t get_rtc_timestamp(void)
  47. {
  48. struct tm tm_new = {0};
  49. rtc_time_t g_current_time = {0};
  50. R_RTC_CalendarTimeGet(&g_rtc_ctrl, &g_current_time);
  51. tm_new.tm_year = g_current_time.tm_year;
  52. tm_new.tm_mon = g_current_time.tm_mon;
  53. tm_new.tm_mday = g_current_time.tm_mday;
  54. tm_new.tm_hour = g_current_time.tm_hour;
  55. tm_new.tm_min = g_current_time.tm_min;
  56. tm_new.tm_sec = g_current_time.tm_sec;
  57. tm_new.tm_wday = g_current_time.tm_wday;
  58. tm_new.tm_yday = g_current_time.tm_yday;
  59. tm_new.tm_isdst = g_current_time.tm_isdst;
  60. return timegm(&tm_new);
  61. }
  62. static rt_err_t ra_get_secs(time_t *sec)
  63. {
  64. *(rt_uint32_t *)sec = get_rtc_timestamp();
  65. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)sec);
  66. return RT_EOK;
  67. }
  68. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  69. {
  70. struct tm now;
  71. rtc_time_t g_current_time = {0};
  72. gmtime_r(&time_stamp, &now);
  73. if (now.tm_year < 100)
  74. {
  75. return -RT_ERROR;
  76. }
  77. g_current_time.tm_sec = now.tm_sec ;
  78. g_current_time.tm_min = now.tm_min ;
  79. g_current_time.tm_hour = now.tm_hour;
  80. g_current_time.tm_mday = now.tm_mday;
  81. g_current_time.tm_mon = now.tm_mon;
  82. g_current_time.tm_year = now.tm_year;
  83. g_current_time.tm_wday = now.tm_wday;
  84. g_current_time.tm_yday = now.tm_yday;
  85. if (R_RTC_CalendarTimeSet(&g_rtc_ctrl, &g_current_time) != FSP_SUCCESS)
  86. {
  87. LOG_E("set rtc time failed.");
  88. return -RT_ERROR;
  89. }
  90. return RT_EOK;
  91. }
  92. static rt_err_t ra_set_secs(time_t *sec)
  93. {
  94. rt_err_t result = RT_EOK;
  95. if (set_rtc_time_stamp(*(rt_uint32_t *)sec))
  96. {
  97. result = -RT_ERROR;
  98. }
  99. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)sec);
  100. return result;
  101. }
  102. #ifdef RT_USING_ALARM
  103. static rt_err_t ra_get_alarm(struct rt_rtc_wkalarm *alarm)
  104. {
  105. rt_err_t result = RT_EOK;
  106. struct rt_rtc_wkalarm *wkalarm = alarm;
  107. rtc_alarm_time_t alarm_time_get =
  108. {
  109. .sec_match = RT_FALSE,
  110. .min_match = RT_FALSE,
  111. .hour_match = RT_FALSE,
  112. .mday_match = RT_FALSE,
  113. .mon_match = RT_FALSE,
  114. .year_match = RT_FALSE,
  115. .dayofweek_match = RT_FALSE,
  116. };
  117. if (RT_EOK == R_RTC_CalendarAlarmGet(&g_rtc_ctrl, &alarm_time_get))
  118. {
  119. wkalarm->tm_hour = alarm_time_get.time.tm_hour;
  120. wkalarm->tm_min = alarm_time_get.time.tm_min;
  121. wkalarm->tm_sec = alarm_time_get.time.tm_sec;
  122. }
  123. else
  124. {
  125. LOG_E("Calendar alarm Get failed.");
  126. }
  127. return result;
  128. }
  129. static rt_err_t ra_set_alarm(struct rt_rtc_wkalarm *alarm)
  130. {
  131. rt_err_t result = RT_EOK;
  132. struct rt_rtc_wkalarm *wkalarm = alarm;
  133. rtc_alarm_time_t alarm_time_set =
  134. {
  135. .sec_match = RT_TRUE,
  136. .min_match = RT_TRUE,
  137. .hour_match = RT_TRUE,
  138. .mday_match = RT_FALSE,
  139. .mon_match = RT_FALSE,
  140. .year_match = RT_FALSE,
  141. .dayofweek_match = RT_FALSE,
  142. };
  143. alarm_time_set.time.tm_hour = wkalarm->tm_hour;
  144. alarm_time_set.time.tm_min = wkalarm->tm_min;
  145. alarm_time_set.time.tm_sec = wkalarm->tm_sec;
  146. if (1 == wkalarm->enable)
  147. {
  148. if (RT_EOK != R_RTC_CalendarAlarmSet(&g_rtc_ctrl, &alarm_time_set))
  149. {
  150. LOG_E("Calendar alarm Set failed.");
  151. result = -RT_ERROR;
  152. }
  153. }
  154. else
  155. {
  156. alarm_time_set.sec_match = RT_FALSE;
  157. alarm_time_set.min_match = RT_FALSE;
  158. alarm_time_set.hour_match = RT_FALSE;
  159. if (RT_EOK != R_RTC_CalendarAlarmSet(&g_rtc_ctrl, &alarm_time_set))
  160. {
  161. LOG_E("Calendar alarm Stop failed.");
  162. result = -RT_ERROR;
  163. }
  164. }
  165. return result;
  166. }
  167. #endif /* RT_USING_ALARM */
  168. void rtc_callback(rtc_callback_args_t *p_args)
  169. {
  170. #ifdef RT_USING_ALARM
  171. static rt_device_t ra_device;
  172. if (RTC_EVENT_ALARM_IRQ == p_args->event)
  173. {
  174. rt_alarm_update(ra_device, 1);
  175. }
  176. #endif
  177. }
  178. static const struct rt_rtc_ops ra_rtc_ops =
  179. {
  180. .init = ra_rtc_init,
  181. .get_secs = ra_get_secs,
  182. .set_secs = ra_set_secs,
  183. #ifdef RT_USING_ALARM
  184. .set_alarm = ra_set_alarm,
  185. .get_alarm = ra_get_alarm,
  186. #endif
  187. };
  188. static rt_rtc_dev_t ra_rtc_dev;
  189. static int rt_hw_rtc_init(void)
  190. {
  191. rt_err_t result;
  192. ra_rtc_dev.ops = &ra_rtc_ops;
  193. result = rt_hw_rtc_register(&ra_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
  194. if (result != RT_EOK)
  195. {
  196. LOG_E("rtc register err code: %d", result);
  197. return result;
  198. }
  199. LOG_D("rtc init success");
  200. return RT_EOK;
  201. }
  202. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  203. #endif