1
0

rtc.c 8.2 KB

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