drv_rtc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-10 Zohar_Lee first version
  9. * 2020-07-10 lik format file
  10. */
  11. #include "drv_rtc.h"
  12. #include <sys/time.h>
  13. #ifdef RT_USING_RTC
  14. #ifdef BSP_USING_RTC
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.rtc"
  17. #include <drv_log.h>
  18. static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
  19. {
  20. uint32_t i, cnt = 0;
  21. const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  22. for (i = 1; i < month; i++)
  23. cnt += daysOfMonth[i];
  24. cnt += date;
  25. if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && (month >= 3))
  26. cnt += 1;
  27. cnt += (year - 1901) * 365;
  28. for (i = 1901; i < year; i++)
  29. {
  30. if ((i % 4 == 0) && ((i % 100 != 0) || (i % 400 == 0)))
  31. cnt += 1;
  32. }
  33. return (cnt + 1) % 7;
  34. }
  35. static time_t swm_get_rtc_time_stamp(void)
  36. {
  37. RTC_DateTime get_datetime = {0};
  38. struct tm tm_new;
  39. RTC_GetDateTime(RTC, &get_datetime);
  40. tm_new.tm_sec = get_datetime.Second;
  41. tm_new.tm_min = get_datetime.Minute;
  42. tm_new.tm_hour = get_datetime.Hour;
  43. tm_new.tm_mday = get_datetime.Date;
  44. tm_new.tm_mon = get_datetime.Month;
  45. tm_new.tm_year = get_datetime.Year;
  46. LOG_D("get rtc time.");
  47. return mktime(&tm_new);
  48. }
  49. static rt_err_t swm_set_rtc_time_stamp(time_t time_stamp)
  50. {
  51. RTC_DateTime set_datetime = {0};
  52. struct tm now;
  53. gmtime_r(&time_stamp, &now);
  54. set_datetime.Second = now.tm_sec;
  55. set_datetime.Minute = now.tm_min;
  56. set_datetime.Hour = now.tm_hour;
  57. set_datetime.Date = now.tm_mday;
  58. set_datetime.Month = now.tm_mon;
  59. set_datetime.Year = now.tm_year;
  60. // set_datetime.Day = now.tm_wday;
  61. RTC_Stop(RTC);
  62. while (RTC->CFGABLE == 0)
  63. ;
  64. RTC->MINSEC = (set_datetime.Second << RTC_MINSEC_SEC_Pos) |
  65. (set_datetime.Minute << RTC_MINSEC_MIN_Pos);
  66. RTC->DATHUR = (set_datetime.Hour << RTC_DATHUR_HOUR_Pos) |
  67. ((set_datetime.Date) << RTC_DATHUR_DATE_Pos);
  68. RTC->MONDAY = (calcWeekDay(set_datetime.Year, set_datetime.Month, set_datetime.Date)
  69. << RTC_MONDAY_DAY_Pos) |
  70. ((set_datetime.Month) << RTC_MONDAY_MON_Pos);
  71. RTC->YEAR = set_datetime.Year;
  72. RTC->LOAD = 1 << RTC_LOAD_TIME_Pos;
  73. RTC_Start(RTC);
  74. LOG_D("set rtc time.");
  75. return RT_EOK;
  76. }
  77. static rt_err_t swm_rtc_init(void)
  78. {
  79. RTC_InitStructure rtc_initstruct;
  80. rtc_initstruct.Year = 2020;
  81. rtc_initstruct.Month = 6;
  82. rtc_initstruct.Date = 8;
  83. rtc_initstruct.Hour = 12;
  84. rtc_initstruct.Minute = 0;
  85. rtc_initstruct.Second = 0;
  86. rtc_initstruct.SecondIEn = 0;
  87. rtc_initstruct.MinuteIEn = 0;
  88. RTC_Init(RTC, &rtc_initstruct);
  89. RTC_Start(RTC);
  90. return RT_EOK;
  91. }
  92. static rt_err_t swm_rtc_get_secs(void *args)
  93. {
  94. *(rt_uint32_t *)args = swm_get_rtc_time_stamp();
  95. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  96. return RT_EOK;
  97. }
  98. static rt_err_t swm_rtc_set_secs(void *args)
  99. {
  100. rt_err_t result = RT_EOK;
  101. if (swm_set_rtc_time_stamp(*(rt_uint32_t *)args))
  102. {
  103. result = -RT_ERROR;
  104. }
  105. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  106. return result;
  107. }
  108. static const struct rt_rtc_ops swm_rtc_ops =
  109. {
  110. swm_rtc_init,
  111. swm_rtc_get_secs,
  112. swm_rtc_set_secs,
  113. RT_NULL,
  114. RT_NULL,
  115. RT_NULL,
  116. RT_NULL,
  117. };
  118. static rt_rtc_dev_t swm_rtc_device;
  119. int rt_hw_rtc_init(void)
  120. {
  121. rt_err_t result;
  122. swm_rtc_device.ops = &swm_rtc_ops;
  123. result = rt_hw_rtc_register(&swm_rtc_device, "rtc", RT_DEVICE_FLAG_RDWR,RT_NULL);
  124. if (result != RT_EOK)
  125. {
  126. LOG_E("rtc register err code: %d", result);
  127. return result;
  128. }
  129. LOG_D("rtc init success");
  130. return RT_EOK;
  131. }
  132. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  133. #endif /* BSP_USING_RTC */
  134. #endif /* RT_USING_RTC */