rtc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 <stm32f2xx.h>
  16. #include <time.h>
  17. __IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
  18. RTC_TimeTypeDef RTC_TimeStructure;
  19. RTC_InitTypeDef RTC_InitStructure;
  20. RTC_AlarmTypeDef RTC_AlarmStructure;
  21. RTC_DateTypeDef RTC_DateStructure;
  22. #define MINUTE 60
  23. #define HOUR (60*MINUTE)
  24. #define DAY (24*HOUR)
  25. #define YEAR (365*DAY)
  26. static int month[12] =
  27. {
  28. 0,
  29. DAY*(31),
  30. DAY*(31+29),
  31. DAY*(31+29+31),
  32. DAY*(31+29+31+30),
  33. DAY*(31+29+31+30+31),
  34. DAY*(31+29+31+30+31+30),
  35. DAY*(31+29+31+30+31+30+31),
  36. DAY*(31+29+31+30+31+30+31+31),
  37. DAY*(31+29+31+30+31+30+31+31+30),
  38. DAY*(31+29+31+30+31+30+31+31+30+31),
  39. DAY*(31+29+31+30+31+30+31+31+30+31+30)
  40. };
  41. static struct rt_device rtc;
  42. static time_t rt_mktime(struct tm *tm)
  43. {
  44. long res;
  45. int year;
  46. year = tm->tm_year - 70;
  47. res = YEAR * year + DAY * ((year + 1) / 4);
  48. res += month[tm->tm_mon];
  49. if (tm->tm_mon > 1 && ((year + 2) % 4))
  50. res -= DAY;
  51. res += DAY * (tm->tm_mday - 1);
  52. res += HOUR * tm->tm_hour;
  53. res += MINUTE * tm->tm_min;
  54. res += tm->tm_sec;
  55. return res;
  56. }
  57. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  58. {
  59. if (dev->rx_indicate != RT_NULL)
  60. {
  61. /* Open Interrupt */
  62. }
  63. return RT_EOK;
  64. }
  65. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  66. {
  67. return 0;
  68. }
  69. static rt_err_t rt_rtc_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  70. {
  71. time_t *time;
  72. struct tm ti,*to;
  73. RT_ASSERT(dev != RT_NULL);
  74. switch (cmd)
  75. {
  76. case RT_DEVICE_CTRL_RTC_GET_TIME:
  77. time = (time_t *)args;
  78. /* read device */
  79. //RTC_GetTimeStamp(RTC_Format_BIN, &RTC_TimeStructure, &RTC_DateStructure);
  80. RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
  81. RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
  82. ti.tm_sec = RTC_TimeStructure.RTC_Seconds;
  83. ti.tm_min = RTC_TimeStructure.RTC_Minutes;
  84. ti.tm_hour = RTC_TimeStructure.RTC_Hours;
  85. //ti.tm_wday = (RTC_DateStructure.RTC_WeekDay==7)?0:RTC_DateStructure.RTC_WeekDay;
  86. ti.tm_mon = RTC_DateStructure.RTC_Month -1;
  87. ti.tm_mday = RTC_DateStructure.RTC_Date;
  88. ti.tm_year = RTC_DateStructure.RTC_Year + 70;
  89. *time = rt_mktime(&ti);
  90. //*time = RTC_GetCounter();
  91. break;
  92. case RT_DEVICE_CTRL_RTC_SET_TIME:
  93. {
  94. time = (time_t *)args;
  95. /* Enable the PWR clock */
  96. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  97. /* Allow access to RTC */
  98. PWR_BackupAccessCmd(ENABLE);
  99. /* Wait until last write operation on RTC registers has finished */
  100. //RTC_WaitForLastTask();
  101. /* Change the current time */
  102. //RTC_SetCounter(*time);
  103. to = localtime(time);
  104. RTC_TimeStructure.RTC_Seconds = to->tm_sec;
  105. RTC_TimeStructure.RTC_Minutes = to->tm_min;
  106. RTC_TimeStructure.RTC_Hours = to->tm_hour;
  107. //RTC_DateStructure.RTC_WeekDay =(ti->tm_wday==0)?7:ti->tm_wday;
  108. RTC_DateStructure.RTC_Month = to->tm_mon + 1;
  109. RTC_DateStructure.RTC_Date = to->tm_mday;
  110. RTC_DateStructure.RTC_Year = to->tm_year - 70;
  111. RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
  112. RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure);
  113. /* Wait until last write operation on RTC registers has finished */
  114. //RTC_WaitForLastTask();
  115. RTC_WriteBackupRegister(RTC_BKP_DR1, 0xA5A5);
  116. //BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  117. }
  118. break;
  119. }
  120. return RT_EOK;
  121. }
  122. /*******************************************************************************
  123. * Function Name : RTC_Configuration
  124. * Description : Configures the RTC.
  125. * Input : None
  126. * Output : None
  127. * Return : 0 reday,-1 error.
  128. *******************************************************************************/
  129. int RTC_Config(void)
  130. {
  131. u32 count=0x200000;
  132. /* Enable the PWR clock */
  133. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  134. /* Allow access to RTC */
  135. PWR_BackupAccessCmd(ENABLE);
  136. RCC_LSEConfig(RCC_LSE_ON);
  137. /* Wait till LSE is ready */
  138. while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
  139. if ( count == 0 )
  140. {
  141. return -1;
  142. }
  143. /* Select the RTC Clock Source */
  144. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  145. SynchPrediv = 0xFF;
  146. AsynchPrediv = 0x7F;
  147. /* Enable the RTC Clock */
  148. RCC_RTCCLKCmd(ENABLE);
  149. /* Wait for RTC APB registers synchronisation */
  150. RTC_WaitForSynchro();
  151. /* Enable The TimeStamp */
  152. //RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE);
  153. return 0;
  154. }
  155. int RTC_Configuration(void)
  156. {
  157. if(RTC_Config() < 0 )
  158. return -1;
  159. /* Set the Time */
  160. RTC_TimeStructure.RTC_Hours = 0;
  161. RTC_TimeStructure.RTC_Minutes = 0;
  162. RTC_TimeStructure.RTC_Seconds = 0;
  163. /* Set the Date */
  164. RTC_DateStructure.RTC_Month = 1;
  165. RTC_DateStructure.RTC_Date = 1;
  166. RTC_DateStructure.RTC_Year = 0;
  167. RTC_DateStructure.RTC_WeekDay = 4;
  168. /* Calendar Configuration */
  169. RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
  170. RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
  171. RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
  172. RTC_Init(&RTC_InitStructure);
  173. /* Set Current Time and Date */
  174. RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
  175. RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
  176. if (RTC_Init(&RTC_InitStructure) == ERROR)
  177. return -1;
  178. return 0;
  179. }
  180. void rt_hw_rtc_init(void)
  181. {
  182. rtc.type = RT_Device_Class_RTC;
  183. if (RTC_ReadBackupRegister(RTC_BKP_DR1) != 0xA5A5)
  184. {
  185. rt_kprintf("rtc is not configured\n");
  186. rt_kprintf("please configure with set_date and set_time\n");
  187. if ( RTC_Configuration() != 0)
  188. {
  189. rt_kprintf("rtc configure fail...\r\n");
  190. return ;
  191. }
  192. }
  193. else
  194. {
  195. /* Wait for RTC registers synchronization */
  196. RTC_WaitForSynchro();
  197. }
  198. /* register rtc device */
  199. rtc.init = RT_NULL;
  200. rtc.open = rt_rtc_open;
  201. rtc.close = RT_NULL;
  202. rtc.read = rt_rtc_read;
  203. rtc.write = RT_NULL;
  204. rtc.control = rt_rtc_control;
  205. /* no private */
  206. rtc.user_data = RT_NULL;
  207. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  208. return;
  209. }
  210. #ifdef RT_USING_FINSH
  211. #include <finsh.h>
  212. #include <time.h>
  213. time_t time(time_t* t)
  214. {
  215. rt_device_t device;
  216. time_t time;
  217. device = rt_device_find("rtc");
  218. if (device != RT_NULL)
  219. {
  220. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  221. if (t != RT_NULL) *t = time;
  222. }
  223. return time;
  224. }
  225. void set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
  226. {
  227. time_t now;
  228. struct tm* ti;
  229. rt_device_t device;
  230. ti = RT_NULL;
  231. /* get current time */
  232. time(&now);
  233. ti = localtime(&now);
  234. if (ti != RT_NULL)
  235. {
  236. ti->tm_year = year - 1900;
  237. ti->tm_mon = month - 1; /* ti->tm_mon = month; */
  238. ti->tm_mday = day;
  239. }
  240. now = mktime(ti);
  241. device = rt_device_find("rtc");
  242. if (device != RT_NULL)
  243. {
  244. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  245. }
  246. }
  247. FINSH_FUNCTION_EXPORT(set_date, set date. e.g: set_date(2010,2,28))
  248. void set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
  249. {
  250. time_t now;
  251. struct tm* ti;
  252. rt_device_t device;
  253. ti = RT_NULL;
  254. /* get current time */
  255. time(&now);
  256. ti = localtime(&now);
  257. if (ti != RT_NULL)
  258. {
  259. ti->tm_hour = hour;
  260. ti->tm_min = minute;
  261. ti->tm_sec = second;
  262. }
  263. now = mktime(ti);
  264. device = rt_device_find("rtc");
  265. if (device != RT_NULL)
  266. {
  267. rt_rtc_control(device, RT_DEVICE_CTRL_RTC_SET_TIME, &now);
  268. }
  269. }
  270. FINSH_FUNCTION_EXPORT(set_time, set time. e.g: set_time(23,59,59))
  271. void list_date()
  272. {
  273. time_t now;
  274. time(&now);
  275. rt_kprintf("%s\n", ctime(&now));
  276. }
  277. FINSH_FUNCTION_EXPORT(list_date, show date and time.)
  278. #endif