rtc.c 5.3 KB

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