rtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * File : rtc.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, 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. * 2009-01-05 Bernard the first version.
  13. * 2011-11-26 aozima implementation time.
  14. */
  15. #include <rtthread.h>
  16. #include <stm32f10x.h>
  17. #include "rtc.h"
  18. static struct rt_device rtc;
  19. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  20. {
  21. if (dev->rx_indicate != RT_NULL)
  22. {
  23. /* Open Interrupt */
  24. }
  25. return RT_EOK;
  26. }
  27. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  28. {
  29. return 0;
  30. }
  31. static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  32. {
  33. rt_time_t *time;
  34. RT_ASSERT(dev != RT_NULL);
  35. switch (cmd)
  36. {
  37. case RT_DEVICE_CTRL_RTC_GET_TIME:
  38. time = (rt_time_t *)args;
  39. /* read device */
  40. *time = RTC_GetCounter();
  41. break;
  42. case RT_DEVICE_CTRL_RTC_SET_TIME:
  43. {
  44. time = (rt_time_t *)args;
  45. /* Enable PWR and BKP clocks */
  46. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  47. /* Allow access to BKP Domain */
  48. PWR_BackupAccessCmd(ENABLE);
  49. /* Wait until last write operation on RTC registers has finished */
  50. RTC_WaitForLastTask();
  51. /* Change the current time */
  52. RTC_SetCounter(*time);
  53. /* Wait until last write operation on RTC registers has finished */
  54. RTC_WaitForLastTask();
  55. BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  56. }
  57. break;
  58. }
  59. return RT_EOK;
  60. }
  61. /*******************************************************************************
  62. * Function Name : RTC_Configuration
  63. * Description : Configures the RTC.
  64. * Input : None
  65. * Output : None
  66. * Return : 0 reday,-1 error.
  67. *******************************************************************************/
  68. int RTC_Configuration(void)
  69. {
  70. u32 count=0x200000;
  71. /* Enable PWR and BKP clocks */
  72. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  73. /* Allow access to BKP Domain */
  74. PWR_BackupAccessCmd(ENABLE);
  75. /* Reset Backup Domain */
  76. BKP_DeInit();
  77. /* Enable LSE */
  78. RCC_LSEConfig(RCC_LSE_ON);
  79. /* Wait till LSE is ready */
  80. while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
  81. if ( count == 0 )
  82. {
  83. return -1;
  84. }
  85. /* Select LSE as RTC Clock Source */
  86. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  87. /* Enable RTC Clock */
  88. RCC_RTCCLKCmd(ENABLE);
  89. /* Wait for RTC registers synchronization */
  90. RTC_WaitForSynchro();
  91. /* Wait until last write operation on RTC registers has finished */
  92. RTC_WaitForLastTask();
  93. /* Set RTC prescaler: set RTC period to 1sec */
  94. RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  95. /* Wait until last write operation on RTC registers has finished */
  96. RTC_WaitForLastTask();
  97. return 0;
  98. }
  99. void rt_hw_rtc_init(void)
  100. {
  101. rtc.type = RT_Device_Class_RTC;
  102. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  103. {
  104. rt_kprintf("rtc is not configured\n");
  105. rt_kprintf("please configure with set_date and set_time\n");
  106. if ( RTC_Configuration() != 0)
  107. {
  108. rt_kprintf("rtc configure fail...\r\n");
  109. return ;
  110. }
  111. }
  112. else
  113. {
  114. /* Wait for RTC registers synchronization */
  115. RTC_WaitForSynchro();
  116. }
  117. /* register rtc device */
  118. rtc.init = RT_NULL;
  119. rtc.open = rt_rtc_open;
  120. rtc.close = RT_NULL;
  121. rtc.read = rt_rtc_read;
  122. rtc.write = RT_NULL;
  123. rtc.control = rt_rtc_control;
  124. /* no private */
  125. rtc.user_data = RT_NULL;
  126. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  127. #ifdef RT_USING_FINSH
  128. list_date();
  129. #endif
  130. return;
  131. }
  132. #include <time.h>
  133. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000 /* for IAR 6.2 later Compiler */
  134. #pragma module_name = "?time"
  135. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  136. #else
  137. time_t time(time_t* t)
  138. #endif
  139. {
  140. rt_device_t device;
  141. time_t time=0;
  142. device = rt_device_find("rtc");
  143. if (device != RT_NULL)
  144. {
  145. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  146. if (t != RT_NULL) *t = time;
  147. }
  148. return time;
  149. }
  150. #ifdef RT_USING_FINSH
  151. #include <finsh.h>
  152. void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  153. {
  154. time_t now;
  155. struct tm* ti;
  156. rt_device_t device;
  157. ti = RT_NULL;
  158. /* get current time */
  159. time(&now);
  160. ti = localtime(&now);
  161. if (ti != RT_NULL)
  162. {
  163. ti->tm_year = year - 1900;
  164. ti->tm_mon = month - 1; /* ti->tm_mon = month; 0~11 */
  165. ti->tm_mday = day;
  166. }
  167. now = mktime(ti);
  168. device = rt_device_find("rtc");
  169. if (device != RT_NULL)
  170. {
  171. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  172. }
  173. }
  174. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  175. void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  176. {
  177. time_t now;
  178. struct tm* ti;
  179. rt_device_t device;
  180. ti = RT_NULL;
  181. /* get current time */
  182. time(&now);
  183. ti = localtime(&now);
  184. if (ti != RT_NULL)
  185. {
  186. ti->tm_hour = hour;
  187. ti->tm_min = minute;
  188. ti->tm_sec = second;
  189. }
  190. now = mktime(ti);
  191. device = rt_device_find("rtc");
  192. if (device != RT_NULL)
  193. {
  194. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  195. }
  196. }
  197. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  198. void list_date(void)
  199. {
  200. time_t now;
  201. time(&now);
  202. rt_kprintf("%s\n", ctime(&now));
  203. }
  204. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  205. #endif