soft_rtc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. int rt_soft_rtc_init(void)
  60. {
  61. static rt_bool_t init_ok = RT_FALSE;
  62. struct tm time_new = SOFT_RTC_TIME_DEFAULT;
  63. if (init_ok)
  64. {
  65. return 0;
  66. }
  67. /* make sure only one 'rtc' device */
  68. RT_ASSERT(!rt_device_find("rtc"));
  69. init_tick = rt_tick_get();
  70. init_time = mktime(&time_new);
  71. soft_rtc_dev.type = RT_Device_Class_RTC;
  72. /* register rtc device */
  73. soft_rtc_dev.init = RT_NULL;
  74. soft_rtc_dev.open = RT_NULL;
  75. soft_rtc_dev.close = RT_NULL;
  76. soft_rtc_dev.read = RT_NULL;
  77. soft_rtc_dev.write = RT_NULL;
  78. soft_rtc_dev.control = soft_rtc_control;
  79. /* no private */
  80. soft_rtc_dev.user_data = RT_NULL;
  81. rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR);
  82. init_ok = RT_TRUE;
  83. return 0;
  84. }
  85. INIT_DEVICE_EXPORT(rt_soft_rtc_init);
  86. #endif /* RT_USING_SOFT_RTC */