sysTime.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*******************************************************************************
  2. 版权所有:
  3. 版本号: 1.00
  4. 文件名: sysTime.c
  5. 生成日期: 2008.8.16
  6. 作者:
  7. 功能说明:系统时间维护及GPS对时处理
  8. 修改日志:
  9. 日志1:
  10. 修改者:
  11. 修改日期:
  12. 修改内容:
  13. 修改原因:
  14. ****************************************************************************/
  15. #include "head.h"
  16. #ifdef CPU_FUXI
  17. #include <sys/time.h>
  18. #include <sys/timeb.h>
  19. #include <mb.h>
  20. #include <my_rtc.h>
  21. #endif
  22. extern unsigned int g_keep_alive_gps;
  23. #ifdef CPU_FUXI
  24. int g_rtc_ok;
  25. #endif
  26. void sys_time_get(struct rtc_time_t *pct)
  27. {
  28. struct timespec ts;
  29. clk_time_get(&ts);
  30. timespec_to_rtc(ts,pct,1);
  31. }
  32. #ifdef CPU_FUXI
  33. int sys_time_set(struct rtc_time_t *pct)
  34. {
  35. int ret = 0;
  36. char pchDT[30];
  37. struct timespec ts;
  38. SHM_TS_T shm_ts;
  39. if(rtc_to_timespec(pct,&ts) != 0)
  40. {
  41. return -1;
  42. }
  43. shm_ts.ts = ts;
  44. // shm_comm_packet_write(SHM_ADDR_D_TIME, (uint8_t *)&shm_ts, sizeof(SHM_TS_T));
  45. mb_notice_time_update();
  46. sprintf(pchDT, "date -s '%04d-%02d-%02d %02d:%02d:%02d'", pct->year + 2000, pct->month, pct->day, pct->hour, pct->min, pct->ms / 1000);
  47. system(pchDT);
  48. system("hwclock -w");
  49. system("sync");
  50. rt_printf("设置系统时间: %s\n", pchDT);
  51. ret = gps_set_time(&ts);
  52. return ret;
  53. }
  54. int sys_time_set_kernal(struct rtc_time_t *pct)
  55. {
  56. int ret = 0;
  57. char pchDT[30];
  58. struct timespec ts;
  59. if(rtc_to_timespec(pct,&ts) != 0)
  60. {
  61. return -1;
  62. }
  63. sprintf(pchDT, "date -s '%04d-%02d-%02d %02d:%02d:%02d'", pct->year + 2000, pct->month, pct->day, pct->hour, pct->min, pct->ms / 1000);
  64. system(pchDT);
  65. system("hwclock -w");
  66. system("sync");
  67. printf("设置系统时间: %s\n", pchDT);
  68. ret = gps_set_time(&ts);
  69. return ret;
  70. }
  71. void sys_time_check(void)
  72. {
  73. struct rtc_time_t ct;
  74. SHM_TS_T shm_ts;
  75. int ret;
  76. if(g_rtc_ok < 0)
  77. {
  78. ret = shm_comm_packet_read(SHM_ADDR_U_TIME, sizeof(SHM_TS_T), (uint8_t *)&shm_ts, sizeof(SHM_TS_T));
  79. if(ret >= 0)
  80. {
  81. g_rtc_ok = 0;
  82. timespec_to_rtc(shm_ts.ts, &ct, 1);
  83. if(ct.hour>=24||ct.min>=60||ct.month>12||ct.day>31||ct.day==0||ct.month==0) // 读取时钟错误
  84. {
  85. rt_printf("app read rtc data error.");
  86. return;
  87. }
  88. sys_time_set_kernal(&ct);
  89. rt_printf("app read rtc succeed.");
  90. }
  91. }
  92. }
  93. #else
  94. int sys_time_set(struct rtc_time_t *pct)
  95. {
  96. int ret;
  97. struct timespec ts;
  98. if(rtc_to_timespec(pct,&ts) != 0)
  99. {
  100. return -1;
  101. }
  102. ret = clk_time_set(&ts);
  103. return ret;
  104. }
  105. #endif
  106. void sys_time_set_rmt(struct rtc_time_t *pt)
  107. {
  108. sys_time_set(pt);
  109. }
  110. void sys_time_init_zero(struct rtc_time_t *p_mt)
  111. {
  112. p_mt->year=8;
  113. p_mt->month=8;
  114. p_mt->day=8;
  115. p_mt->hour=8;
  116. p_mt->min=8;
  117. p_mt->ms=8000;
  118. }
  119. // 系统时钟已在clk_init中初始化,此处重新初始化是为了保存SOE.
  120. #ifdef CPU_FUXI
  121. void sys_time_init(void)
  122. {
  123. struct rtc_time_t ct;
  124. SHM_TS_T shm_ts;
  125. bool err=false;
  126. int ret,i;
  127. // GPS已ok,不检查实时时钟
  128. if(g_keep_alive_gps)
  129. {
  130. return;
  131. }
  132. #if 0
  133. if(rtc_time_read((struct rtc_time_t*)&ct)!=0)
  134. {
  135. err=true;
  136. }
  137. #else
  138. i = 0;
  139. while(1)
  140. {
  141. ret = shm_comm_packet_read(SHM_ADDR_U_TIME, sizeof(SHM_TS_T), (uint8_t *)&shm_ts, sizeof(SHM_TS_T));
  142. if(ret >= 0)
  143. {
  144. break;
  145. }
  146. i++;
  147. if(i > 100)
  148. {
  149. err=true;
  150. break;
  151. }
  152. msleep(100);// 延时100ms
  153. }
  154. timespec_to_rtc(shm_ts.ts, &ct, 1);
  155. #endif
  156. if(ct.hour>=24||ct.min>=60||ct.month>12||ct.day>31||ct.day==0||ct.month==0) // 读取时钟错误
  157. {
  158. err=true;
  159. }
  160. if(err)
  161. {
  162. sys_time_init_zero(&ct);
  163. rt_err_set(ERR_CODE_RTC,0);
  164. #if 0
  165. sys_time_set(&ct);
  166. #endif
  167. g_rtc_ok = -1;
  168. return;
  169. }
  170. sys_time_set_kernal(&ct);
  171. }
  172. #else
  173. void sys_time_init(void)
  174. {
  175. struct rtc_time_t ct;
  176. bool err=false;
  177. // GPS已ok,不检查实时时钟
  178. if(g_keep_alive_gps)
  179. {
  180. return;
  181. }
  182. if(rtc_time_read((struct rtc_time_t*)&ct)!=0)
  183. {
  184. err=true;
  185. }
  186. if(ct.hour>=24||ct.min>=60||ct.month>12||ct.day>31||ct.day==0||ct.month==0) // 读取时钟错误
  187. {
  188. err=true;
  189. }
  190. if(err)
  191. {
  192. extern struct timespec g_sys_time;
  193. int ret;
  194. sys_time_init_zero(&ct);
  195. rtc_to_timespec(&ct,&g_sys_time);
  196. ret = rtc_time_write(&ct);
  197. if(ret == 0)
  198. {
  199. rt_err_clr(ERR_CODE_RTC,0);
  200. }
  201. }
  202. }
  203. #endif
  204. void sys_time_change(struct rtc_time_t *p_ct)
  205. {
  206. p_ct->min++;
  207. if(p_ct->min>=60)
  208. {
  209. p_ct->min=0;
  210. p_ct->hour++;
  211. if(p_ct->hour>=24)
  212. {
  213. bool bdayflag=false;
  214. p_ct->hour=0;
  215. p_ct->day++;
  216. if(p_ct->month==2) // 2月
  217. {
  218. if(p_ct->day>28)
  219. {
  220. WORD year=p_ct->year+2000;
  221. if(year%4==0) //闰年 2月29天,其他28天
  222. {
  223. if(p_ct->day>29)
  224. {
  225. bdayflag=true;
  226. }
  227. }
  228. else
  229. {
  230. bdayflag=true;
  231. }
  232. }
  233. }
  234. else
  235. {
  236. if(p_ct->day>30)
  237. {
  238. if(p_ct->month==2||p_ct->month==4||p_ct->month==6||p_ct->month==9||p_ct->month==11)
  239. {
  240. bdayflag=true;
  241. }
  242. if(p_ct->day>31)
  243. {
  244. bdayflag=true;
  245. }
  246. }
  247. }
  248. if(bdayflag)
  249. {
  250. p_ct->day=1;
  251. p_ct->month++;
  252. if(p_ct->month>12)
  253. {
  254. p_ct->month=1;
  255. p_ct->year++;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. #define RTC_WRITE_PERIOD (600UL*USTIMER_SEC)
  262. void clk_app(void)
  263. {
  264. static uint32_t us0 = -(RTC_WRITE_PERIOD -20*USTIMER_SEC); // 启动20S后更新一次
  265. struct timespec ts;
  266. struct rtc_time_t rtc;
  267. // 旧系统的时间更新策略是有GPS对时,每隔10分钟,更新一次时钟芯片时间
  268. // 没有外部对时1000秒从时钟芯片更新一次系统时间。这种策略将导致没有外部
  269. // 对时的时候,系统时间波动较大,导致时间测量不准。
  270. // 新系统的时间策略是,不管什么情况,每隔10分钟,更新一次时钟芯片时间.
  271. // 这样系统时间就不会有较大波动。
  272. // 每隔10分钟,更新一次时钟芯片时间
  273. if(ustimer_delay_origin2(&us0,RTC_WRITE_PERIOD) != 1)
  274. {
  275. return;
  276. }
  277. gps_get_time(&ts);
  278. if(timespec_to_rtc(ts,&rtc,1) != 0)
  279. {
  280. rt_printf("clk_app:timespec_to_rtc err.\r\n");
  281. return;
  282. }
  283. if(rtc_time_write(&rtc) != 0)
  284. {
  285. rt_printf("clk_app:rtc_time_write err.\r\n");
  286. return;
  287. }
  288. rt_err_clr(ERR_CODE_RTC,0);
  289. }