drv_rtc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-25 Rbb666 first version
  9. * 2024-11-06 kurisaw add alarm function
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <sys/time.h>
  14. #include "drv_common.h"
  15. #ifdef BSP_USING_RTC
  16. /*#define DRV_DEBUG*/
  17. #define LOG_TAG "drv.rtc"
  18. #include <drv_log.h>
  19. cyhal_rtc_t rtc_obj;
  20. struct rtc_device_object
  21. {
  22. rt_rtc_dev_t rtc_dev;
  23. #ifdef RT_USING_ALARM
  24. struct rt_rtc_wkalarm wkalarm;
  25. #endif
  26. };
  27. static struct rtc_device_object ifx32_rtc_dev;
  28. static int get_day_of_week(int day, int month, int year)
  29. {
  30. int ret;
  31. int k = 0;
  32. int j = 0;
  33. if (month < CY_RTC_MARCH)
  34. {
  35. month += CY_RTC_MONTHS_PER_YEAR;
  36. year--;
  37. }
  38. k = (year % 100);
  39. j = (year / 100);
  40. ret = (day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
  41. ret = ((ret + 6) % 7);
  42. return ret;
  43. }
  44. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  45. {
  46. struct tm tm = {0};
  47. struct tm new_time = {0};
  48. gmtime_r(&time_stamp, &tm);
  49. if (tm.tm_year < 100)
  50. {
  51. return -RT_ERROR;
  52. }
  53. new_time.tm_sec = tm.tm_sec ;
  54. new_time.tm_min = tm.tm_min ;
  55. new_time.tm_hour = tm.tm_hour;
  56. new_time.tm_mday = tm.tm_mday;
  57. new_time.tm_mon = tm.tm_mon;
  58. new_time.tm_year = tm.tm_year;
  59. new_time.tm_wday = get_day_of_week(tm.tm_mday, tm.tm_mon, tm.tm_year);
  60. if (cyhal_rtc_write(&rtc_obj, &new_time) != RT_EOK)
  61. {
  62. return -RT_ERROR;
  63. }
  64. LOG_D("set rtc time.");
  65. return RT_EOK;
  66. }
  67. static rt_err_t ifx_rtc_get_timeval(struct timeval *tv)
  68. {
  69. struct tm tm_new = {0};
  70. struct tm date_time = {0};
  71. cyhal_rtc_read(&rtc_obj, &date_time);
  72. tm_new.tm_sec = date_time.tm_sec;
  73. tm_new.tm_min = date_time.tm_min;
  74. tm_new.tm_hour = date_time.tm_hour;
  75. tm_new.tm_mday = date_time.tm_mday;
  76. tm_new.tm_mon = date_time.tm_mon;
  77. tm_new.tm_year = date_time.tm_year;
  78. tv->tv_sec = timegm(&tm_new);
  79. return RT_EOK;
  80. }
  81. #ifdef RT_USING_ALARM
  82. void rtc_alarm_callback(void *callback_arg, cyhal_rtc_event_t event);
  83. #endif
  84. static rt_err_t _rtc_init(void)
  85. {
  86. #ifdef BSP_RTC_USING_LSE
  87. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_WCO);
  88. #else
  89. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_ILO);
  90. #endif /* BSP_RTC_USING_LSE */
  91. if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
  92. {
  93. LOG_E("rtc init failed.");
  94. return -RT_ERROR;
  95. }
  96. #ifdef RT_USING_ALARM
  97. cyhal_rtc_register_callback(&rtc_obj, rtc_alarm_callback, NULL);
  98. cyhal_rtc_enable_event(&rtc_obj, CYHAL_RTC_ALARM, 3u, true);
  99. #endif
  100. return RT_EOK;
  101. }
  102. static rt_err_t _rtc_get_secs(time_t *sec)
  103. {
  104. struct timeval tv;
  105. ifx_rtc_get_timeval(&tv);
  106. *(time_t *) sec = tv.tv_sec;
  107. LOG_D("RTC: get rtc_time %d", *sec);
  108. return RT_EOK;
  109. }
  110. static rt_err_t _rtc_set_secs(time_t *sec)
  111. {
  112. rt_err_t result = RT_EOK;
  113. if (set_rtc_time_stamp(*sec))
  114. {
  115. result = -RT_ERROR;
  116. }
  117. LOG_D("RTC: set rtc_time %d", *sec);
  118. return result;
  119. }
  120. #if defined(RT_USING_ALARM)
  121. static rt_err_t _rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
  122. {
  123. #ifdef RT_USING_ALARM
  124. *alarm = ifx32_rtc_dev.wkalarm;
  125. LOG_D("GET_ALARM %d:%d:%d",ifx32_rtc_dev.wkalarm.tm_hour,
  126. ifx32_rtc_dev.wkalarm.tm_min,ifx32_rtc_dev.wkalarm.tm_sec);
  127. return RT_EOK;
  128. #else
  129. return -RT_ERROR;
  130. #endif
  131. }
  132. static rt_err_t _rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
  133. {
  134. #ifdef RT_USING_ALARM
  135. LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  136. if (alarm != RT_NULL)
  137. {
  138. ifx32_rtc_dev.wkalarm.enable = alarm->enable;
  139. ifx32_rtc_dev.wkalarm.tm_hour = alarm->tm_hour;
  140. ifx32_rtc_dev.wkalarm.tm_min = alarm->tm_min;
  141. ifx32_rtc_dev.wkalarm.tm_sec = alarm->tm_sec;
  142. cyhal_rtc_set_alarm_by_seconds(&rtc_obj, 1);
  143. }
  144. else
  145. {
  146. LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  147. return -RT_ERROR;
  148. }
  149. LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
  150. alarm->tm_min, alarm->tm_sec);
  151. return RT_EOK;
  152. #else
  153. return -RT_ERROR;
  154. #endif
  155. }
  156. #ifdef RT_USING_ALARM
  157. void rtc_alarm_callback(void *callback_arg, cyhal_rtc_event_t event)
  158. {
  159. RT_UNUSED(callback_arg);
  160. RT_UNUSED(event);
  161. rt_interrupt_enter();
  162. rt_alarm_update(0, 0);
  163. rt_interrupt_leave();
  164. }
  165. #endif
  166. static const struct rt_rtc_ops _rtc_ops =
  167. {
  168. _rtc_init,
  169. _rtc_get_secs,
  170. _rtc_set_secs,
  171. _rtc_get_alarm,
  172. _rtc_set_alarm,
  173. ifx_rtc_get_timeval,
  174. RT_NULL,
  175. };
  176. /**
  177. * @brief RTC initialization function.
  178. *
  179. * @return RT_EOK indicates successful initialization, other value indicates failed;
  180. */
  181. static int rt_hw_rtc_init(void)
  182. {
  183. rt_err_t result = RT_EOK;
  184. ifx32_rtc_dev.rtc_dev.ops = &_rtc_ops;
  185. if (rt_hw_rtc_register(&(ifx32_rtc_dev.rtc_dev), "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  186. {
  187. LOG_E("rtc init failed");
  188. result = -RT_ERROR;
  189. }
  190. else
  191. {
  192. LOG_D("rtc init success");
  193. }
  194. return result;
  195. }
  196. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  197. #endif
  198. #endif