rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. * 2012-01-29 aozima first version.
  23. * 2012-04-12 aozima optimization: find rtc device only first.
  24. * 2012-04-16 aozima add scheduler lock for set_date and set_time.
  25. */
  26. #include <time.h>
  27. #include <string.h>
  28. #include <rtthread.h>
  29. /** \brief returns the current time.
  30. *
  31. * \param time_t * t the timestamp pointer, if not used, keep NULL.
  32. * \return time_t return timestamp current.
  33. *
  34. */
  35. /* for IAR 6.2 later Compiler */
  36. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  37. #pragma module_name = "?time"
  38. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  39. #else
  40. time_t time(time_t *t)
  41. #endif
  42. {
  43. static rt_device_t device = RT_NULL;
  44. time_t time_now = 0;
  45. /* optimization: find rtc device only first. */
  46. if (device == RT_NULL)
  47. {
  48. device = rt_device_find("rtc");
  49. }
  50. /* read timestamp from RTC device. */
  51. if (device != RT_NULL)
  52. {
  53. if (rt_device_open(device, 0) == RT_EOK)
  54. {
  55. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  56. rt_device_close(device);
  57. }
  58. }
  59. /* if t is not NULL, write timestamp to *t */
  60. if (t != RT_NULL)
  61. {
  62. *t = time_now;
  63. }
  64. return time_now;
  65. }
  66. /** \brief set system date(time not modify).
  67. *
  68. * \param rt_uint32_t year e.g: 2012.
  69. * \param rt_uint32_t month e.g: 12 (1~12).
  70. * \param rt_uint32_t day e.g: e.g: 31.
  71. * \return rt_err_t if set success, return RT_EOK.
  72. *
  73. */
  74. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  75. {
  76. time_t now;
  77. struct tm *p_tm;
  78. struct tm tm_new;
  79. rt_device_t device;
  80. rt_err_t ret = -RT_ERROR;
  81. /* get current time */
  82. now = time(RT_NULL);
  83. /* lock scheduler. */
  84. rt_enter_critical();
  85. /* converts calendar time time into local time. */
  86. p_tm = localtime(&now);
  87. /* copy the statically located variable */
  88. memcpy(&tm_new, p_tm, sizeof(struct tm));
  89. /* unlock scheduler. */
  90. rt_exit_critical();
  91. /* update date. */
  92. tm_new.tm_year = year - 1900;
  93. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  94. tm_new.tm_mday = day;
  95. /* converts the local time in time to calendar time. */
  96. now = mktime(&tm_new);
  97. device = rt_device_find("rtc");
  98. if (device == RT_NULL)
  99. {
  100. return -RT_ERROR;
  101. }
  102. /* update to RTC device. */
  103. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  104. return ret;
  105. }
  106. /** \brief set system time(date not modify).
  107. *
  108. * \param rt_uint32_t hour e.g: 0~23.
  109. * \param rt_uint32_t minute e.g: 0~59.
  110. * \param rt_uint32_t second e.g: 0~59.
  111. * \return rt_err_t if set success, return RT_EOK.
  112. *
  113. */
  114. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  115. {
  116. time_t now;
  117. struct tm *p_tm;
  118. struct tm tm_new;
  119. rt_device_t device;
  120. rt_err_t ret = -RT_ERROR;
  121. /* get current time */
  122. now = time(RT_NULL);
  123. /* lock scheduler. */
  124. rt_enter_critical();
  125. /* converts calendar time time into local time. */
  126. p_tm = localtime(&now);
  127. /* copy the statically located variable */
  128. memcpy(&tm_new, p_tm, sizeof(struct tm));
  129. /* unlock scheduler. */
  130. rt_exit_critical();
  131. /* update time. */
  132. tm_new.tm_hour = hour;
  133. tm_new.tm_min = minute;
  134. tm_new.tm_sec = second;
  135. /* converts the local time in time to calendar time. */
  136. now = mktime(&tm_new);
  137. device = rt_device_find("rtc");
  138. if (device == RT_NULL)
  139. {
  140. return -RT_ERROR;
  141. }
  142. /* update to RTC device. */
  143. ret = rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  144. return ret;
  145. }
  146. #ifdef RT_USING_FINSH
  147. #include <finsh.h>
  148. #include <rtdevice.h>
  149. void list_date(void)
  150. {
  151. time_t now;
  152. now = time(RT_NULL);
  153. rt_kprintf("%s\n", ctime(&now));
  154. }
  155. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  156. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  157. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  158. #endif