1
0

drv_rtc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if (cyhal_rtc_init(&rtc_obj) != RT_EOK)
  76. {
  77. LOG_E("rtc init failed.");
  78. return -RT_ERROR;
  79. }
  80. return RT_EOK;
  81. }
  82. static rt_err_t _rtc_get_secs(time_t *sec)
  83. {
  84. struct timeval tv;
  85. ifx_rtc_get_timeval(&tv);
  86. *(time_t *) sec = tv.tv_sec;
  87. LOG_D("RTC: get rtc_time %d", *sec);
  88. return RT_EOK;
  89. }
  90. static rt_err_t _rtc_set_secs(time_t *sec)
  91. {
  92. rt_err_t result = RT_EOK;
  93. if (set_rtc_time_stamp(*sec))
  94. {
  95. result = -RT_ERROR;
  96. }
  97. LOG_D("RTC: set rtc_time %d", *sec);
  98. return result;
  99. }
  100. static const struct rt_rtc_ops _rtc_ops =
  101. {
  102. _rtc_init,
  103. _rtc_get_secs,
  104. _rtc_set_secs,
  105. RT_NULL,
  106. RT_NULL,
  107. ifx_rtc_get_timeval,
  108. RT_NULL,
  109. };
  110. /**
  111. * @brief RTC initialization function.
  112. *
  113. * @return RT_EOK indicates successful initialization, other value indicates failed;
  114. */
  115. static int rt_hw_rtc_init(void)
  116. {
  117. rt_err_t result = RT_EOK;
  118. ifx32_rtc_dev.ops = &_rtc_ops;
  119. if (rt_hw_rtc_register(&ifx32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL) != RT_EOK)
  120. {
  121. LOG_E("rtc init failed");
  122. result = -RT_ERROR;
  123. }
  124. else
  125. {
  126. LOG_D("rtc init success");
  127. }
  128. return result;
  129. }
  130. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  131. #endif