drv_rtc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-13 armink the first version
  9. */
  10. #include <sys/time.h>
  11. #include <string.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #ifdef RT_USING_RTC
  15. static struct rt_device rtc_dev;
  16. #ifdef RT_USING_ALARM
  17. static struct rt_rtc_wkalarm wkalarm;
  18. static struct rt_timer alarm_time;
  19. static void alarm_timeout(void *param)
  20. {
  21. rt_alarm_update(param, 1);
  22. }
  23. static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm)
  24. {
  25. rt_tick_t next_tick;
  26. if (palarm->enable)
  27. {
  28. next_tick = RT_TICK_PER_SECOND;
  29. rt_timer_control(&alarm_time, RT_TIMER_CTRL_SET_TIME, &next_tick);
  30. rt_timer_start(&alarm_time);
  31. }
  32. else
  33. {
  34. rt_timer_stop(&alarm_time);
  35. }
  36. }
  37. #endif
  38. static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
  39. {
  40. __time32_t *t;
  41. struct tm newtime;
  42. RT_ASSERT(dev != RT_NULL);
  43. switch (cmd)
  44. {
  45. case RT_DEVICE_CTRL_RTC_GET_TIME:
  46. {
  47. t = (__time32_t *)args;
  48. SYSTEMTIME sys_time;
  49. GetSystemTime(&sys_time);
  50. newtime.tm_year = sys_time.wYear - 1900;
  51. newtime.tm_mon = sys_time.wMonth - 1;
  52. newtime.tm_mday = sys_time.wDay;
  53. newtime.tm_hour = sys_time.wHour;
  54. newtime.tm_min = sys_time.wMinute;
  55. newtime.tm_sec = sys_time.wSecond;
  56. *t = timegm(&newtime);
  57. break;
  58. }
  59. case RT_DEVICE_CTRL_RTC_SET_TIME:
  60. {
  61. #ifdef RT_USING_ALARM
  62. soft_rtc_alarm_update(&wkalarm);
  63. #endif
  64. break;
  65. }
  66. #ifdef RT_USING_ALARM
  67. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  68. *((struct rt_rtc_wkalarm *)args) = wkalarm;
  69. break;
  70. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  71. wkalarm = *((struct rt_rtc_wkalarm *)args);
  72. soft_rtc_alarm_update(&wkalarm);
  73. break;
  74. #endif
  75. case RT_DEVICE_CTRL_RTC_GET_TIME_US:
  76. {
  77. long *tv_usec = (long *)args;
  78. SYSTEMTIME sys_time;
  79. GetSystemTime(&sys_time);
  80. *tv_usec = sys_time.wMilliseconds * 1000UL;
  81. break;
  82. }
  83. default:
  84. return -RT_ERROR;
  85. }
  86. return RT_EOK;
  87. }
  88. #ifdef RT_USING_DEVICE_OPS
  89. const static struct rt_device_ops soft_rtc_ops =
  90. {
  91. RT_NULL,
  92. RT_NULL,
  93. RT_NULL,
  94. RT_NULL,
  95. RT_NULL,
  96. soft_rtc_control
  97. };
  98. #endif
  99. int rt_win_rtc_init(void)
  100. {
  101. static rt_bool_t init_ok = RT_FALSE;
  102. if (init_ok)
  103. {
  104. return 0;
  105. }
  106. /* make sure only one 'rtc' device */
  107. RT_ASSERT(!rt_device_find("rtc"));
  108. #ifdef RT_USING_ALARM
  109. rt_timer_init(&alarm_time,
  110. "alarm",
  111. alarm_timeout,
  112. &rtc_dev,
  113. 0,
  114. RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
  115. #endif
  116. rtc_dev.type = RT_Device_Class_RTC;
  117. /* register rtc device */
  118. #ifdef RT_USING_DEVICE_OPS
  119. rtc_dev.ops = &soft_rtc_ops;
  120. #else
  121. rtc_dev.init = RT_NULL;
  122. rtc_dev.open = RT_NULL;
  123. rtc_dev.close = RT_NULL;
  124. rtc_dev.read = RT_NULL;
  125. rtc_dev.write = RT_NULL;
  126. rtc_dev.control = soft_rtc_control;
  127. #endif
  128. /* no private */
  129. rtc_dev.user_data = RT_NULL;
  130. rt_device_register(&rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  131. init_ok = RT_TRUE;
  132. return 0;
  133. }
  134. INIT_BOARD_EXPORT(rt_win_rtc_init);
  135. #endif /* RT_USING_RTC */