rtc.c 4.6 KB

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