drv_rtc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include <string.h>
  15. #include <time.h>
  16. /**
  17. * This function will get the weed day from a date.
  18. *
  19. * @param year the year of time
  20. * @param month the month of time
  21. * @param date the date of time
  22. *
  23. * @return the week day 0 ~ 6 : sun ~ sat
  24. *
  25. * @note No
  26. */
  27. static uint32_t calcWeekDay(uint32_t year, uint32_t month, uint32_t date)
  28. {
  29. uint32_t i, cnt = 0;
  30. const uint32_t daysOfMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  31. for (i = 1; i < month; i++)
  32. cnt += daysOfMonth[i];
  33. cnt += date;
  34. if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) &&
  35. (month >= 3))
  36. cnt += 1;
  37. cnt += (year - 1901) * 365;
  38. for (i = 1901; i < year; i++)
  39. {
  40. if ((i % 4 == 0) && ((i % 100 != 0) || (i % 400 == 0)))
  41. cnt += 1;
  42. }
  43. return (cnt + 1) % 7;
  44. }
  45. static void RTC_SetDateTime(RTC_TypeDef *RTCx, RTC_DateTime *dateTime)
  46. {
  47. RTC_Stop(RTCx);
  48. while (RTCx->CFGABLE == 0);
  49. RTCx->MINSEC = (dateTime->Second << RTC_MINSEC_SEC_Pos) |
  50. (dateTime->Minute << RTC_MINSEC_MIN_Pos);
  51. RTCx->DATHUR = (dateTime->Hour << RTC_DATHUR_HOUR_Pos) |
  52. ((dateTime->Date - 1) << RTC_DATHUR_DATE_Pos);
  53. RTCx->MONDAY = (calcWeekDay(dateTime->Year, dateTime->Month, dateTime->Date)
  54. << RTC_MONDAY_DAY_Pos) |
  55. ((dateTime->Month - 1) << RTC_MONDAY_MON_Pos);
  56. RTCx->YEAR = dateTime->Year - 1901;
  57. RTCx->LOAD = 1 << RTC_LOAD_TIME_Pos;
  58. RTC_Start(RTC);
  59. }
  60. static rt_err_t swm320_rtc_control(rt_device_t dev, int cmd, void *args)
  61. {
  62. rt_err_t result = RT_EOK;
  63. struct tm time_temp;
  64. struct tm *pNow;
  65. RTC_DateTime dateTime;
  66. switch (cmd)
  67. {
  68. case RT_DEVICE_CTRL_RTC_GET_TIME:
  69. RTC_GetDateTime(RTC, &dateTime);
  70. time_temp.tm_sec = dateTime.Second;
  71. time_temp.tm_min = dateTime.Minute;
  72. time_temp.tm_hour = dateTime.Hour;
  73. time_temp.tm_mday = dateTime.Date;
  74. time_temp.tm_mon = dateTime.Month - 1;
  75. time_temp.tm_year = dateTime.Year - 1900;
  76. *((time_t *)args) = mktime(&time_temp);
  77. break;
  78. case RT_DEVICE_CTRL_RTC_SET_TIME:
  79. rt_enter_critical();
  80. /* converts calendar time time into local time. */
  81. pNow = localtime((const time_t *)args);
  82. /* copy the statically located variable */
  83. memcpy(&time_temp, pNow, sizeof(struct tm));
  84. /* unlock scheduler. */
  85. rt_exit_critical();
  86. dateTime.Hour = time_temp.tm_hour;
  87. dateTime.Minute = time_temp.tm_min;
  88. dateTime.Second = time_temp.tm_sec;
  89. dateTime.Year = time_temp.tm_year + 1900;
  90. dateTime.Month = time_temp.tm_mon + 1;
  91. dateTime.Date = time_temp.tm_mday;
  92. RTC_SetDateTime(RTC, &dateTime);
  93. break;
  94. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  95. break;
  96. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  97. break;
  98. default:
  99. break;
  100. }
  101. return result;
  102. }
  103. #ifdef RT_USING_DEVICE_OPS
  104. const static struct rt_device_ops swm320_rtc_ops =
  105. {
  106. RT_NULL,
  107. RT_NULL,
  108. RT_NULL,
  109. RT_NULL,
  110. RT_NULL,
  111. swm320_rtc_control
  112. };
  113. #endif
  114. int rt_hw_rtc_init(void)
  115. {
  116. rt_err_t ret = RT_EOK;
  117. static struct rt_device rtc_dev;
  118. RTC_InitStructure RTC_initStruct;
  119. RTC_initStruct.Year = 2018;
  120. RTC_initStruct.Month = 1;
  121. RTC_initStruct.Date = 1;
  122. RTC_initStruct.Hour = 12;
  123. RTC_initStruct.Minute = 0;
  124. RTC_initStruct.Second = 0;
  125. RTC_initStruct.SecondIEn = 0;
  126. RTC_initStruct.MinuteIEn = 0;
  127. RTC_Init(RTC, &RTC_initStruct);
  128. RTC_Start(RTC);
  129. rtc_dev.type = RT_Device_Class_RTC;
  130. rtc_dev.rx_indicate = RT_NULL;
  131. rtc_dev.tx_complete = RT_NULL;
  132. #ifdef RT_USING_DEVICE_OPS
  133. rtc_dev.ops = &swm320_rtc_ops;
  134. #else
  135. rtc_dev.init = RT_NULL;
  136. rtc_dev.open = RT_NULL;
  137. rtc_dev.close = RT_NULL;
  138. rtc_dev.read = RT_NULL;
  139. rtc_dev.write = RT_NULL;
  140. rtc_dev.control = swm320_rtc_control;
  141. #endif
  142. rtc_dev.user_data = RTC;
  143. ret = rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  144. return ret;
  145. }
  146. INIT_DEVICE_EXPORT(rt_hw_rtc_init);