1
0

soft_rtc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * File : soft_rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-01-30 armink the first version
  23. */
  24. #include <time.h>
  25. #include <string.h>
  26. #include <rtthread.h>
  27. #include <drivers/rtc.h>
  28. #ifdef RT_USING_SOFT_RTC
  29. /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */
  30. #define RTC_TIME_INIT(year, month, day, hour, minute, second) \
  31. {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second}
  32. #ifndef SOFT_RTC_TIME_DEFAULT
  33. #define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0)
  34. #endif
  35. static struct rt_device soft_rtc_dev;
  36. static rt_tick_t init_tick;
  37. static time_t init_time;
  38. static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args)
  39. {
  40. time_t *time;
  41. struct tm time_temp;
  42. RT_ASSERT(dev != RT_NULL);
  43. memset(&time_temp, 0, sizeof(struct tm));
  44. switch (cmd)
  45. {
  46. case RT_DEVICE_CTRL_RTC_GET_TIME:
  47. time = (time_t *) args;
  48. *time = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  49. break;
  50. case RT_DEVICE_CTRL_RTC_SET_TIME:
  51. {
  52. time = (time_t *) args;
  53. init_time = *time - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND;
  54. break;
  55. }
  56. }
  57. return RT_EOK;
  58. }
  59. #ifdef RT_USING_DEVICE_OPS
  60. const static struct rt_device_ops soft_rtc_ops =
  61. {
  62. RT_NULL,
  63. RT_NULL,
  64. RT_NULL,
  65. RT_NULL,
  66. RT_NULL,
  67. soft_rtc_control
  68. };
  69. #endif
  70. int rt_soft_rtc_init(void)
  71. {
  72. static rt_bool_t init_ok = RT_FALSE;
  73. struct tm time_new = SOFT_RTC_TIME_DEFAULT;
  74. if (init_ok)
  75. {
  76. return 0;
  77. }
  78. /* make sure only one 'rtc' device */
  79. RT_ASSERT(!rt_device_find("rtc"));
  80. init_tick = rt_tick_get();
  81. init_time = mktime(&time_new);
  82. soft_rtc_dev.type = RT_Device_Class_RTC;
  83. /* register rtc device */
  84. #ifdef RT_USING_DEVICE_OPS
  85. soft_rtc_dev.ops = &soft_rtc_ops;
  86. #else
  87. soft_rtc_dev.init = RT_NULL;
  88. soft_rtc_dev.open = RT_NULL;
  89. soft_rtc_dev.close = RT_NULL;
  90. soft_rtc_dev.read = RT_NULL;
  91. soft_rtc_dev.write = RT_NULL;
  92. soft_rtc_dev.control = soft_rtc_control;
  93. #endif
  94. /* no private */
  95. soft_rtc_dev.user_data = RT_NULL;
  96. rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  97. init_ok = RT_TRUE;
  98. return 0;
  99. }
  100. INIT_DEVICE_EXPORT(rt_soft_rtc_init);
  101. #endif /* RT_USING_SOFT_RTC */