rtc.c 5.7 KB

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