rtc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  44. }
  45. /* if t is not NULL, write timestamp to *t */
  46. if (t != RT_NULL)
  47. {
  48. *t = time_now;
  49. }
  50. return time_now;
  51. }
  52. /** \brief set system date(time not modify).
  53. *
  54. * \param rt_uint32_t year e.g: 2012.
  55. * \param rt_uint32_t month e.g: 12 (1~12).
  56. * \param rt_uint32_t day e.g: e.g: 31.
  57. * \return rt_err_t if set success, return RT_EOK.
  58. *
  59. */
  60. rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  61. {
  62. time_t now;
  63. struct tm * p_tm;
  64. struct tm tm_new;
  65. rt_device_t device;
  66. /* get current time */
  67. now = time(RT_NULL);
  68. /* lock scheduler. */
  69. rt_enter_critical();
  70. /* converts calendar time time into local time. */
  71. p_tm = localtime(&now);
  72. /* copy the statically located variable */
  73. memcpy(&tm_new, p_tm, sizeof(struct tm));
  74. /* unlock scheduler. */
  75. rt_exit_critical();
  76. /* update date. */
  77. tm_new.tm_year = year - 1900;
  78. tm_new.tm_mon = month - 1; /* tm_mon: 0~11 */
  79. tm_new.tm_mday = day;
  80. /* converts the local time in time to calendar time. */
  81. now = mktime(&tm_new);
  82. device = rt_device_find("rtc");
  83. if (device == RT_NULL)
  84. {
  85. return -RT_ERROR;
  86. }
  87. /* update to RTC device. */
  88. return rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  89. }
  90. /** \brief set system time(date not modify).
  91. *
  92. * \param rt_uint32_t hour e.g: 0~23.
  93. * \param rt_uint32_t minute e.g: 0~59.
  94. * \param rt_uint32_t second e.g: 0~59.
  95. * \return rt_err_t if set success, return RT_EOK.
  96. *
  97. */
  98. rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  99. {
  100. time_t now;
  101. struct tm * p_tm;
  102. struct tm tm_new;
  103. rt_device_t device;
  104. /* get current time */
  105. now = time(RT_NULL);
  106. /* lock scheduler. */
  107. rt_enter_critical();
  108. /* converts calendar time time into local time. */
  109. p_tm = localtime(&now);
  110. /* copy the statically located variable */
  111. memcpy(&tm_new, p_tm, sizeof(struct tm));
  112. /* unlock scheduler. */
  113. rt_exit_critical();
  114. /* update time. */
  115. tm_new.tm_hour = hour;
  116. tm_new.tm_min = minute;
  117. tm_new.tm_sec = second;
  118. /* converts the local time in time to calendar time. */
  119. now = mktime(&tm_new);
  120. device = rt_device_find("rtc");
  121. if (device == RT_NULL)
  122. {
  123. return RT_ERROR;
  124. }
  125. /* update to RTC device. */
  126. return rt_device_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  127. }
  128. #ifdef RT_USING_FINSH
  129. #include <finsh.h>
  130. void list_date(void)
  131. {
  132. time_t now;
  133. now = time(RT_NULL);
  134. rt_kprintf("%s\n", ctime(&now));
  135. }
  136. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  137. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  138. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  139. #endif