1
0

drv_rtc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard the first version
  9. * 2011-11-26 aozima implementation time.
  10. */
  11. #include <rtthread.h>
  12. #include <stm32f2xx.h>
  13. #include <time.h>
  14. __IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
  15. RTC_TimeTypeDef RTC_TimeStructure;
  16. RTC_InitTypeDef RTC_InitStructure;
  17. RTC_AlarmTypeDef RTC_AlarmStructure;
  18. RTC_DateTypeDef RTC_DateStructure;
  19. #define MINUTE 60
  20. #define HOUR (60*MINUTE)
  21. #define DAY (24*HOUR)
  22. #define YEAR (365*DAY)
  23. static int month[12] =
  24. {
  25. 0,
  26. DAY*(31),
  27. DAY*(31+29),
  28. DAY*(31+29+31),
  29. DAY*(31+29+31+30),
  30. DAY*(31+29+31+30+31),
  31. DAY*(31+29+31+30+31+30),
  32. DAY*(31+29+31+30+31+30+31),
  33. DAY*(31+29+31+30+31+30+31+31),
  34. DAY*(31+29+31+30+31+30+31+31+30),
  35. DAY*(31+29+31+30+31+30+31+31+30+31),
  36. DAY*(31+29+31+30+31+30+31+31+30+31+30)
  37. };
  38. static struct rt_device rtc;
  39. static time_t rt_mktime(struct tm *tm)
  40. {
  41. long res;
  42. int year;
  43. year = tm->tm_year - 70;
  44. res = YEAR * year + DAY * ((year + 1) / 4);
  45. res += month[tm->tm_mon];
  46. if (tm->tm_mon > 1 && ((year + 2) % 4))
  47. res -= DAY;
  48. res += DAY * (tm->tm_mday - 1);
  49. res += HOUR * tm->tm_hour;
  50. res += MINUTE * tm->tm_min;
  51. res += tm->tm_sec;
  52. return res;
  53. }
  54. static rt_err_t rt_rtc_open(rt_device_t dev, rt_uint16_t oflag)
  55. {
  56. if (dev->rx_indicate != RT_NULL)
  57. {
  58. /* Open Interrupt */
  59. }
  60. return RT_EOK;
  61. }
  62. static rt_size_t rt_rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  63. {
  64. return 0;
  65. }
  66. static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
  67. {
  68. time_t *time;
  69. struct tm ti,*to;
  70. RT_ASSERT(dev != RT_NULL);
  71. switch (cmd)
  72. {
  73. case RT_DEVICE_CTRL_RTC_GET_TIME:
  74. time = (time_t *)args;
  75. /* read device */
  76. //RTC_GetTimeStamp(RTC_Format_BIN, &RTC_TimeStructure, &RTC_DateStructure);
  77. RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
  78. RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
  79. ti.tm_sec = RTC_TimeStructure.RTC_Seconds;
  80. ti.tm_min = RTC_TimeStructure.RTC_Minutes;
  81. ti.tm_hour = RTC_TimeStructure.RTC_Hours;
  82. //ti.tm_wday = (RTC_DateStructure.RTC_WeekDay==7)?0:RTC_DateStructure.RTC_WeekDay;
  83. ti.tm_mon = RTC_DateStructure.RTC_Month -1;
  84. ti.tm_mday = RTC_DateStructure.RTC_Date;
  85. ti.tm_year = RTC_DateStructure.RTC_Year + 70;
  86. *time = rt_mktime(&ti);
  87. //*time = RTC_GetCounter();
  88. break;
  89. case RT_DEVICE_CTRL_RTC_SET_TIME:
  90. {
  91. time = (time_t *)args;
  92. /* Enable the PWR clock */
  93. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  94. /* Allow access to RTC */
  95. PWR_BackupAccessCmd(ENABLE);
  96. /* Wait until last write operation on RTC registers has finished */
  97. //RTC_WaitForLastTask();
  98. /* Change the current time */
  99. //RTC_SetCounter(*time);
  100. to = localtime(time);
  101. RTC_TimeStructure.RTC_Seconds = to->tm_sec;
  102. RTC_TimeStructure.RTC_Minutes = to->tm_min;
  103. RTC_TimeStructure.RTC_Hours = to->tm_hour;
  104. //RTC_DateStructure.RTC_WeekDay =(ti->tm_wday==0)?7:ti->tm_wday;
  105. RTC_DateStructure.RTC_Month = to->tm_mon + 1;
  106. RTC_DateStructure.RTC_Date = to->tm_mday;
  107. RTC_DateStructure.RTC_Year = to->tm_year - 70;
  108. RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure);
  109. RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure);
  110. /* Wait until last write operation on RTC registers has finished */
  111. //RTC_WaitForLastTask();
  112. RTC_WriteBackupRegister(RTC_BKP_DR1, 0xA5A5);
  113. //BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  114. }
  115. break;
  116. }
  117. return RT_EOK;
  118. }
  119. /*******************************************************************************
  120. * Function Name : RTC_Configuration
  121. * Description : Configures the RTC.
  122. * Input : None
  123. * Output : None
  124. * Return : 0 reday,-1 error.
  125. *******************************************************************************/
  126. int RTC_Config(void)
  127. {
  128. u32 count=0x200000;
  129. /* Enable the PWR clock */
  130. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  131. /* Allow access to RTC */
  132. PWR_BackupAccessCmd(ENABLE);
  133. RCC_LSEConfig(RCC_LSE_ON);
  134. /* Wait till LSE is ready */
  135. while ( (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && (--count) );
  136. if ( count == 0 )
  137. {
  138. return -1;
  139. }
  140. /* Select the RTC Clock Source */
  141. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  142. SynchPrediv = 0xFF;
  143. AsynchPrediv = 0x7F;
  144. /* Enable the RTC Clock */
  145. RCC_RTCCLKCmd(ENABLE);
  146. /* Wait for RTC APB registers synchronisation */
  147. RTC_WaitForSynchro();
  148. /* Enable The TimeStamp */
  149. //RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE);
  150. return 0;
  151. }
  152. int RTC_Configuration(void)
  153. {
  154. if(RTC_Config() < 0 )
  155. return -1;
  156. /* Set the Time */
  157. RTC_TimeStructure.RTC_Hours = 0;
  158. RTC_TimeStructure.RTC_Minutes = 0;
  159. RTC_TimeStructure.RTC_Seconds = 0;
  160. /* Set the Date */
  161. RTC_DateStructure.RTC_Month = 1;
  162. RTC_DateStructure.RTC_Date = 1;
  163. RTC_DateStructure.RTC_Year = 0;
  164. RTC_DateStructure.RTC_WeekDay = 4;
  165. /* Calendar Configuration */
  166. RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
  167. RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
  168. RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
  169. RTC_Init(&RTC_InitStructure);
  170. /* Set Current Time and Date */
  171. RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
  172. RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
  173. if (RTC_Init(&RTC_InitStructure) == ERROR)
  174. return -1;
  175. return 0;
  176. }
  177. void rt_hw_rtc_init(void)
  178. {
  179. rtc.type = RT_Device_Class_RTC;
  180. if (RTC_ReadBackupRegister(RTC_BKP_DR1) != 0xA5A5)
  181. {
  182. rt_kprintf("rtc is not configured\n");
  183. rt_kprintf("please configure with set_date and set_time\n");
  184. if ( RTC_Configuration() != 0)
  185. {
  186. rt_kprintf("rtc configure fail...\r\n");
  187. return ;
  188. }
  189. }
  190. else
  191. {
  192. /* Wait for RTC registers synchronization */
  193. RTC_WaitForSynchro();
  194. }
  195. /* register rtc device */
  196. rtc.init = RT_NULL;
  197. rtc.open = rt_rtc_open;
  198. rtc.close = RT_NULL;
  199. rtc.read = rt_rtc_read;
  200. rtc.write = RT_NULL;
  201. rtc.control = rt_rtc_control;
  202. /* no private */
  203. rtc.user_data = RT_NULL;
  204. rt_device_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
  205. return;
  206. }