stm32f1_rtc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "stm32f1_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. return;
  128. }
  129. // #include <time.h>
  130. // #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000 /* for IAR 6.2 later Compiler */
  131. // #pragma module_name = "?time"
  132. // time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  133. // #else
  134. // time_t time(time_t* t)
  135. // #endif
  136. // {
  137. // rt_device_t device;
  138. // time_t time=0;
  139. //
  140. // device = rt_device_find("rtc");
  141. // if (device != RT_NULL)
  142. // {
  143. // rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  144. // if (t != RT_NULL) *t = time;
  145. // }
  146. //
  147. // return time;
  148. // }
  149. // #ifdef RT_USING_FINSH
  150. // #include <finsh.h>
  151. //
  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. //
  158. // ti = RT_NULL;
  159. // /* get current time */
  160. // time(&now);
  161. //
  162. // ti = localtime(&now);
  163. // if (ti != RT_NULL)
  164. // {
  165. // ti->tm_year = year - 1900;
  166. // ti->tm_mon = month - 1; /* ti->tm_mon = month; 0~11 */
  167. // ti->tm_mday = day;
  168. // }
  169. //
  170. // now = mktime(ti);
  171. //
  172. // device = rt_device_find("rtc");
  173. // if (device != RT_NULL)
  174. // {
  175. // rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  176. // }
  177. // }
  178. // FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  179. //
  180. // void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  181. // {
  182. // time_t now;
  183. // struct tm* ti;
  184. // rt_device_t device;
  185. //
  186. // ti = RT_NULL;
  187. // /* get current time */
  188. // time(&now);
  189. //
  190. // ti = localtime(&now);
  191. // if (ti != RT_NULL)
  192. // {
  193. // ti->tm_hour = hour;
  194. // ti->tm_min = minute;
  195. // ti->tm_sec = second;
  196. // }
  197. //
  198. // now = mktime(ti);
  199. // device = rt_device_find("rtc");
  200. // if (device != RT_NULL)
  201. // {
  202. // rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  203. // }
  204. // }
  205. // FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  206. //
  207. // void list_date(void)
  208. // {
  209. // time_t now;
  210. //
  211. // time(&now);
  212. // rt_kprintf("%s\n", ctime(&now));
  213. // }
  214. // FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  215. // #endif