rtc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 : None
  65. *******************************************************************************/
  66. void RTC_Configuration(void)
  67. {
  68. /* Enable PWR and BKP clocks */
  69. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  70. /* Allow access to BKP Domain */
  71. PWR_BackupAccessCmd(ENABLE);
  72. /* Reset Backup Domain */
  73. BKP_DeInit();
  74. /* Enable LSE */
  75. RCC_LSEConfig(RCC_LSE_ON);
  76. /* Wait till LSE is ready */
  77. while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  78. {
  79. }
  80. /* Select LSE as RTC Clock Source */
  81. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  82. /* Enable RTC Clock */
  83. RCC_RTCCLKCmd(ENABLE);
  84. /* Wait for RTC registers synchronization */
  85. RTC_WaitForSynchro();
  86. /* Wait until last write operation on RTC registers has finished */
  87. RTC_WaitForLastTask();
  88. /* Set RTC prescaler: set RTC period to 1sec */
  89. RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  90. /* Wait until last write operation on RTC registers has finished */
  91. RTC_WaitForLastTask();
  92. }
  93. void rt_hw_rtc_init(void)
  94. {
  95. rtc.type = RT_Device_Class_RTC;
  96. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  97. {
  98. rt_kprintf("rtc is not configured\n");
  99. rt_kprintf("please configure with set_date and set_time\n");
  100. RTC_Configuration();
  101. }
  102. else
  103. {
  104. /* Wait for RTC registers synchronization */
  105. RTC_WaitForSynchro();
  106. }
  107. /* register rtc device */
  108. rtc.init = RT_NULL;
  109. rtc.open = rt_rtc_open;
  110. rtc.close = RT_NULL;
  111. rtc.read = rt_rtc_read;
  112. rtc.write = RT_NULL;
  113. rtc.control = rt_rtc_control;
  114. /* no private */
  115. rtc.private = RT_NULL;
  116. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  117. return;
  118. }
  119. #include <time.h>
  120. time_t time(time_t* t)
  121. {
  122. rt_device_t device;
  123. time_t time;
  124. device = rt_device_find("rtc");
  125. if (device != RT_NULL)
  126. {
  127. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  128. if (t != RT_NULL) *t = time;
  129. }
  130. return time;
  131. }
  132. #ifdef RT_USING_FINSH
  133. #include <finsh.h>
  134. void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  135. {
  136. time_t now;
  137. struct tm* ti;
  138. rt_device_t device;
  139. ti = RT_NULL;
  140. /* get current time */
  141. time(&now);
  142. ti = localtime(&now);
  143. if (ti != RT_NULL)
  144. {
  145. ti->tm_year = year - 1900;
  146. ti->tm_mon = month - 1; /* ti->tm_mon = month; */
  147. ti->tm_mday = day;
  148. }
  149. now = mktime(ti);
  150. device = rt_device_find("rtc");
  151. if (device != RT_NULL)
  152. {
  153. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  154. }
  155. }
  156. FINSH_FUNCTION_EXPORT(set_date, set date)
  157. void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  158. {
  159. time_t now;
  160. struct tm* ti;
  161. rt_device_t device;
  162. ti = RT_NULL;
  163. /* get current time */
  164. time(&now);
  165. ti = localtime(&now);
  166. if (ti != RT_NULL)
  167. {
  168. ti->tm_hour = hour;
  169. ti->tm_min = minute;
  170. ti->tm_sec = second;
  171. }
  172. now = mktime(ti);
  173. device = rt_device_find("rtc");
  174. if (device != RT_NULL)
  175. {
  176. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  177. }
  178. }
  179. FINSH_FUNCTION_EXPORT(set_time, set time)
  180. void list_date()
  181. {
  182. time_t now;
  183. time(&now);
  184. rt_kprintf("%s\n", ctime(&now));
  185. }
  186. FINSH_FUNCTION_EXPORT(list_date, list date)
  187. #endif