drv_rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. static rt_err_t _rtc_init(void)
  82. {
  83. #ifdef BSP_RTC_USING_LSE
  84. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_WCO);
  85. #else
  86. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_ILO);
  87. #endif /* BSP_RTC_USING_LSE */
  88. if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
  89. {
  90. LOG_E("rtc init failed.");
  91. return -RT_ERROR;
  92. }
  93. #ifdef RT_USING_ALARM
  94. cyhal_rtc_register_callback(&rtc_obj, rtc_alarm_callback, NULL);
  95. cyhal_rtc_enable_event(&rtc_obj, CYHAL_RTC_ALARM, 3u, true);
  96. #endif
  97. return RT_EOK;
  98. }
  99. static rt_err_t _rtc_get_secs(time_t *sec)
  100. {
  101. struct timeval tv;
  102. ifx_rtc_get_timeval(&tv);
  103. *(time_t *) sec = tv.tv_sec;
  104. LOG_D("RTC: get rtc_time %d", *sec);
  105. return RT_EOK;
  106. }
  107. static rt_err_t _rtc_set_secs(time_t *sec)
  108. {
  109. rt_err_t result = RT_EOK;
  110. if (set_rtc_time_stamp(*sec))
  111. {
  112. result = -RT_ERROR;
  113. }
  114. LOG_D("RTC: set rtc_time %d", *sec);
  115. return result;
  116. }
  117. #if defined(RT_USING_ALARM)
  118. static rt_err_t _rtc_get_alarm(struct rt_rtc_wkalarm *alarm)
  119. {
  120. #ifdef RT_USING_ALARM
  121. *alarm = ifx32_rtc_dev.wkalarm;
  122. LOG_D("GET_ALARM %d:%d:%d",ifx32_rtc_dev.wkalarm.tm_hour,
  123. ifx32_rtc_dev.wkalarm.tm_min,ifx32_rtc_dev.wkalarm.tm_sec);
  124. return RT_EOK;
  125. #else
  126. return -RT_ERROR;
  127. #endif
  128. }
  129. static rt_err_t _rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
  130. {
  131. #ifdef RT_USING_ALARM
  132. LOG_D("RT_DEVICE_CTRL_RTC_SET_ALARM");
  133. if (alarm != RT_NULL)
  134. {
  135. ifx32_rtc_dev.wkalarm.enable = alarm->enable;
  136. ifx32_rtc_dev.wkalarm.tm_hour = alarm->tm_hour;
  137. ifx32_rtc_dev.wkalarm.tm_min = alarm->tm_min;
  138. ifx32_rtc_dev.wkalarm.tm_sec = alarm->tm_sec;
  139. cyhal_rtc_set_alarm_by_seconds(&rtc_obj, 1);
  140. }
  141. else
  142. {
  143. LOG_E("RT_DEVICE_CTRL_RTC_SET_ALARM error!!");
  144. return -RT_ERROR;
  145. }
  146. LOG_D("SET_ALARM %d:%d:%d",alarm->tm_hour,
  147. alarm->tm_min, alarm->tm_sec);
  148. return RT_EOK;
  149. #else
  150. return -RT_ERROR;
  151. #endif
  152. }
  153. #ifdef RT_USING_ALARM
  154. void rtc_alarm_callback(void)
  155. {
  156. rt_interrupt_enter();
  157. rt_alarm_update(0, 0);
  158. rt_interrupt_leave();
  159. }
  160. #endif
  161. static const struct rt_rtc_ops _rtc_ops =
  162. {
  163. _rtc_init,
  164. _rtc_get_secs,
  165. _rtc_set_secs,
  166. _rtc_get_alarm,
  167. _rtc_set_alarm,
  168. ifx_rtc_get_timeval,
  169. RT_NULL,
  170. };
  171. /**
  172. * @brief RTC initialization function.
  173. *
  174. * @return RT_EOK indicates successful initialization, other value indicates failed;
  175. */
  176. static int rt_hw_rtc_init(void)
  177. {
  178. rt_err_t result = RT_EOK;
  179. ifx32_rtc_dev.rtc_dev.ops = &_rtc_ops;
  180. if (rt_hw_rtc_register(&(ifx32_rtc_dev.rtc_dev), "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  181. {
  182. LOG_E("rtc init failed");
  183. result = -RT_ERROR;
  184. }
  185. else
  186. {
  187. LOG_D("rtc init success");
  188. }
  189. return result;
  190. }
  191. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  192. #endif