1
0

rtc.c 4.2 KB

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