drv_rtc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * 2022-07-25 Rbb666 first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <sys/time.h>
  13. #include "drv_common.h"
  14. #ifdef BSP_USING_RTC
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.rtc"
  17. #include <drv_log.h>
  18. cyhal_rtc_t rtc_obj;
  19. static rt_rtc_dev_t ifx32_rtc_dev;
  20. static int get_day_of_week(int day, int month, int year)
  21. {
  22. int ret;
  23. int k = 0;
  24. int j = 0;
  25. if (month < CY_RTC_MARCH)
  26. {
  27. month += CY_RTC_MONTHS_PER_YEAR;
  28. year--;
  29. }
  30. k = (year % 100);
  31. j = (year / 100);
  32. ret = (day + (13 * (month + 1) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
  33. ret = ((ret + 6) % 7);
  34. return ret;
  35. }
  36. static rt_err_t set_rtc_time_stamp(time_t time_stamp)
  37. {
  38. struct tm tm = {0};
  39. struct tm new_time = {0};
  40. gmtime_r(&time_stamp, &tm);
  41. if (tm.tm_year < 100)
  42. {
  43. return -RT_ERROR;
  44. }
  45. new_time.tm_sec = tm.tm_sec ;
  46. new_time.tm_min = tm.tm_min ;
  47. new_time.tm_hour = tm.tm_hour;
  48. new_time.tm_mday = tm.tm_mday;
  49. new_time.tm_mon = tm.tm_mon;
  50. new_time.tm_year = tm.tm_year;
  51. new_time.tm_wday = get_day_of_week(tm.tm_mday, tm.tm_mon, tm.tm_year);
  52. if (cyhal_rtc_write(&rtc_obj, &new_time) != RT_EOK)
  53. {
  54. return -RT_ERROR;
  55. }
  56. LOG_D("set rtc time.");
  57. return RT_EOK;
  58. }
  59. static rt_err_t ifx_rtc_get_timeval(struct timeval *tv)
  60. {
  61. struct tm tm_new = {0};
  62. struct tm date_time = {0};
  63. cyhal_rtc_read(&rtc_obj, &date_time);
  64. tm_new.tm_sec = date_time.tm_sec;
  65. tm_new.tm_min = date_time.tm_min;
  66. tm_new.tm_hour = date_time.tm_hour;
  67. tm_new.tm_mday = date_time.tm_mday;
  68. tm_new.tm_mon = date_time.tm_mon;
  69. tm_new.tm_year = date_time.tm_year;
  70. tv->tv_sec = timegm(&tm_new);
  71. return RT_EOK;
  72. }
  73. static rt_err_t _rtc_init(void)
  74. {
  75. #ifdef BSP_RTC_USING_LSE
  76. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_WCO);
  77. #else
  78. Cy_RTC_SelectClockSource(CY_RTC_CLK_SELECT_ILO);
  79. #endif /* BSP_RTC_USING_LSE */
  80. if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
  81. {
  82. LOG_E("rtc init failed.");
  83. return -RT_ERROR;
  84. }
  85. return RT_EOK;
  86. }
  87. static rt_err_t _rtc_get_secs(time_t *sec)
  88. {
  89. struct timeval tv;
  90. ifx_rtc_get_timeval(&tv);
  91. *(time_t *) sec = tv.tv_sec;
  92. LOG_D("RTC: get rtc_time %d", *sec);
  93. return RT_EOK;
  94. }
  95. static rt_err_t _rtc_set_secs(time_t *sec)
  96. {
  97. rt_err_t result = RT_EOK;
  98. if (set_rtc_time_stamp(*sec))
  99. {
  100. result = -RT_ERROR;
  101. }
  102. LOG_D("RTC: set rtc_time %d", *sec);
  103. return result;
  104. }
  105. static const struct rt_rtc_ops _rtc_ops =
  106. {
  107. _rtc_init,
  108. _rtc_get_secs,
  109. _rtc_set_secs,
  110. RT_NULL,
  111. RT_NULL,
  112. ifx_rtc_get_timeval,
  113. RT_NULL,
  114. };
  115. /**
  116. * @brief RTC initialization function.
  117. *
  118. * @return RT_EOK indicates successful initialization, other value indicates failed;
  119. */
  120. static int rt_hw_rtc_init(void)
  121. {
  122. rt_err_t result = RT_EOK;
  123. ifx32_rtc_dev.ops = &_rtc_ops;
  124. if (rt_hw_rtc_register(&ifx32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  125. {
  126. LOG_E("rtc init failed");
  127. result = -RT_ERROR;
  128. }
  129. else
  130. {
  131. LOG_D("rtc init success");
  132. }
  133. return result;
  134. }
  135. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  136. #endif