drv_rtc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #ifdef RT_USING_RTC
  13. #ifdef BSP_USING_RTC
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.rtc"
  16. #include <drv_log.h>
  17. static struct rt_device rtc_device;
  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 - 1;
  45. tm_new.tm_year = get_datetime.Year - 1900;
  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 *p_tm;
  53. p_tm = gmtime(&time_stamp);
  54. set_datetime.Second = p_tm->tm_sec;
  55. set_datetime.Minute = p_tm->tm_min;
  56. set_datetime.Hour = p_tm->tm_hour;
  57. set_datetime.Date = p_tm->tm_mday;
  58. set_datetime.Month = p_tm->tm_mon + 1;
  59. set_datetime.Year = p_tm->tm_year + 1900;
  60. // datetime.Day = p_tm->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 - 1901;
  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_control(rt_device_t rtc_device, int cmd, void *args)
  78. {
  79. rt_err_t result = RT_EOK;
  80. RT_ASSERT(rtc_device != RT_NULL);
  81. switch (cmd)
  82. {
  83. case RT_DEVICE_CTRL_RTC_GET_TIME:
  84. *(rt_uint32_t *)args = swm_get_rtc_time_stamp();
  85. LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
  86. break;
  87. case RT_DEVICE_CTRL_RTC_SET_TIME:
  88. if (swm_set_rtc_time_stamp(*(rt_uint32_t *)args))
  89. {
  90. result = -RT_ERROR;
  91. }
  92. LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
  93. break;
  94. default:
  95. break;
  96. }
  97. return result;
  98. }
  99. #ifdef RT_USING_DEVICE_OPS
  100. const static struct rt_device_ops swm_rtc_ops =
  101. {
  102. RT_NULL,
  103. RT_NULL,
  104. RT_NULL,
  105. RT_NULL,
  106. RT_NULL,
  107. swm_rtc_control};
  108. #endif
  109. static void swm_rtc_init(void)
  110. {
  111. RTC_InitStructure rtc_initstruct;
  112. rtc_initstruct.Year = 2020;
  113. rtc_initstruct.Month = 6;
  114. rtc_initstruct.Date = 8;
  115. rtc_initstruct.Hour = 12;
  116. rtc_initstruct.Minute = 0;
  117. rtc_initstruct.Second = 0;
  118. rtc_initstruct.SecondIEn = 0;
  119. rtc_initstruct.MinuteIEn = 0;
  120. RTC_Init(RTC, &rtc_initstruct);
  121. RTC_Start(RTC);
  122. }
  123. static rt_err_t rt_hw_rtc_register(rt_device_t rtc_device, const char *name, rt_uint32_t flag)
  124. {
  125. RT_ASSERT(rtc_device != RT_NULL);
  126. swm_rtc_init();
  127. #ifdef RT_USING_DEVICE_OPS
  128. rtc_device->ops = &swm_rtc_ops;
  129. #else
  130. rtc_device->init = RT_NULL;
  131. rtc_device->open = RT_NULL;
  132. rtc_device->close = RT_NULL;
  133. rtc_device->read = RT_NULL;
  134. rtc_device->write = RT_NULL;
  135. rtc_device->control = swm_rtc_control;
  136. #endif
  137. rtc_device->type = RT_Device_Class_RTC;
  138. rtc_device->rx_indicate = RT_NULL;
  139. rtc_device->tx_complete = RT_NULL;
  140. rtc_device->user_data = RT_NULL;
  141. /* register a character device */
  142. return rt_device_register(rtc_device, name, flag);
  143. }
  144. int rt_hw_rtc_init(void)
  145. {
  146. rt_err_t result;
  147. result = rt_hw_rtc_register(&rtc_device, "rtc", RT_DEVICE_FLAG_RDWR);
  148. if (result != RT_EOK)
  149. {
  150. LOG_E("rtc register err code: %d", result);
  151. return result;
  152. }
  153. LOG_D("rtc init success");
  154. return RT_EOK;
  155. }
  156. INIT_DEVICE_EXPORT(rt_hw_rtc_init);
  157. #endif /* BSP_USING_RTC */
  158. #endif /* RT_USING_RTC */