gps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: rt_gps.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2008-06-04
  7. 功能说明: GPS时钟模块驱动程序。
  8. 其它说明: 1. GPS卫星同步时钟只保证整秒时PR上升沿的精度(一般1us之内),
  9. 其它bit的上升沿精度并不保证。
  10. 2. GPS接收模块只给出秒脉冲和串口数据。
  11. 3. 使用B码对时的时候,B码给的数据中可能包含闰秒(负闰秒:58->0;
  12. 正闰秒:60->0),所以秒的有效数据范围为0~60。但是系统中其它
  13. 计时模块没有闰秒的概念,单独在B码模块使用闰秒有可能造成整个计时
  14. 系统的混乱,所以最终决定将秒的有效数据范围还是限制在0~59,这样
  15. 当正闰秒出现时,系统时间将是:58->59->0->0-1。初步估计,这种影响
  16. 在可接受的范围之内。
  17. 4. 目前在冷火Linux平台上对时误差<8US.(2010-11-23)
  18. 修改记录:
  19. */
  20. /*------------------------------- 头文件 --------------------------------------
  21. */
  22. #include "bspconfig.h"
  23. #include "my_rtc.h"
  24. #include "rt.h"
  25. #include "bsp_ccu.h"
  26. #include "bsp_ustimer.h"
  27. #include "rt_printf.h"
  28. #include "rt_types.h"
  29. #include <sys/mman.h>
  30. #include "gps.h"
  31. #include <sys/prctl.h>
  32. #include <sys/syscall.h>
  33. #include <pthread.h>
  34. #include <sched.h>
  35. /*------------------------------- 宏定义 --------------------------------------
  36. */
  37. #if CFG_BSP_DEBUG
  38. #define _DEBUG_GPS
  39. #endif
  40. #define DTIM_CLOCK_PER_US (CFG_CPU_CLK/2/1000000)
  41. #define GPS_SIGAL_DIFF 1 //GPS信号宽度误差
  42. #define GPS_IRIG_CODE_0 0
  43. #define GPS_IRIG_CODE_1 1
  44. #define GPS_IRIG_CODE_P 2
  45. /*------------------------------ 全局变量 -------------------------------------
  46. */
  47. extern int g_clock_mode;
  48. struct timespec g_sys_time;
  49. static struct rtc_time_t tmp_sys_time;
  50. //毫秒中断的参考值
  51. unsigned int g_1s_refence;
  52. //输入信号是否合法,合法为1,不合法为0
  53. int g_signal_valid;
  54. //GPS对时使用的保活计数值
  55. unsigned int g_keep_alive_gps;
  56. //处理B码的全局变量
  57. unsigned int g_irig_old_value;
  58. unsigned int g_irig_old_interval;
  59. unsigned int g_irig_code_index;
  60. unsigned char g_irig_code[100];
  61. int g_gps_fd = -1;
  62. unsigned g_gps_mapped_size;
  63. void *g_gps_map_base, *g_gps_virt_addr;
  64. static void* gpio_base = NULL;
  65. /*------------------------------ 函数声明 -------------------------------------
  66. */
  67. int _gps_irig_decode(unsigned char *code,struct rtc_time_t * p_tm );
  68. void _ms_update_time(void);
  69. void _gps_capture_isr(void);
  70. void _gps_1s_isr(void);
  71. void gps_isr(void);
  72. /*------------------------------ 外部函数 -------------------------------------
  73. */
  74. /******************************************************************************
  75. 函数名称: gps_init
  76. 函数版本: 01.01
  77. 创建作者: ocean
  78. 创建日期: 2025-11-21
  79. 函数说明: gps初始化。
  80. 参数说明: 无
  81. 返回值: 成功返回0.
  82. 修改记录:
  83. */
  84. int gps_init(void)
  85. {
  86. struct timespec ts;
  87. struct rtc_time_t ct;
  88. void *temp_ccu_base, *temp_virt_l_base, *temp_virt_h_base, *temp_virt_ctl_base, *temp_virt_irq_base;
  89. unsigned long read_result;
  90. off_t target;
  91. unsigned page_size, offset_in_page;
  92. unsigned width = 8 * sizeof(int);
  93. target = TIMER_BASE;
  94. // 获取页面大小
  95. g_gps_mapped_size = page_size = sysconf(_SC_PAGESIZE);
  96. offset_in_page = (unsigned)target & (page_size - 1);
  97. if (offset_in_page + width > page_size) {
  98. /* This access spans pages.
  99. * Must map two pages to make it possible: */
  100. g_gps_mapped_size *= 2;
  101. }
  102. g_gps_fd = open("/dev/mem", O_RDWR | O_SYNC);
  103. if (g_gps_fd < 0)
  104. {
  105. printf("open(/dev/mem) failed.\n");
  106. return -1;
  107. }
  108. fflush(stdout);
  109. g_gps_map_base = mmap (NULL, g_gps_mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, g_gps_fd, target & ~(off_t)(page_size - 1));
  110. if (g_gps_map_base == (void *)-1)
  111. {
  112. printf ("NULL pointer!\n");
  113. }
  114. else
  115. {
  116. printf ("BSP gps map Successfull!\n");
  117. }
  118. fflush(stdout);
  119. g_gps_virt_addr = (char*)g_gps_map_base + offset_in_page;
  120. // printf("g_gps_virt_addr: %p %p %x\n", g_gps_virt_addr, g_gps_map_base, offset_in_page);
  121. // Init timer ccu clock
  122. /* set TMR3 clock source to HOSC, 8 pre-division */
  123. temp_ccu_base = g_ccu_map_base + 0x080C;
  124. read_result = *(volatile u_int32_t*)(temp_ccu_base);
  125. read_result &= ~(0x01 << 24);
  126. read_result |= (1 << 31) | 3;
  127. *(volatile u_int32_t*)(temp_ccu_base) = read_result;
  128. // Enable timer3
  129. /* set timer intervalue */
  130. temp_virt_l_base = (char*)g_gps_map_base + 0x84;
  131. temp_virt_h_base = (char*)g_gps_map_base + 0x8C;
  132. *(volatile u_int32_t*)(temp_virt_l_base) = 3000000;
  133. *(volatile u_int32_t*)(temp_virt_h_base) = 0x0;
  134. /* set mode to auto reload */
  135. temp_virt_ctl_base = (char*)g_gps_map_base + 0x80;
  136. read_result = *(volatile u_int32_t*)(temp_virt_ctl_base);
  137. read_result &= TIMER_CTL_PERIODIC;
  138. *(volatile u_int32_t*)(temp_virt_ctl_base) = (read_result | TIMER_CTL_AUTORELOAD | TIMER_CTL_ENABLE);
  139. /* Enable timer interrupt */
  140. temp_virt_irq_base = (char*)g_gps_map_base + 0x00;
  141. read_result = *(volatile u_int32_t*)(temp_virt_irq_base);
  142. *(volatile u_int32_t*)(temp_virt_irq_base) = (read_result | TIMER_IRQ_EN(3));
  143. /* Get linux system current time */
  144. clock_gettime(CLOCK_REALTIME, &ts);
  145. timespec_to_rtc(ts, &ct, 1);
  146. if(ct.hour>=24||ct.min>=60||ct.month>12||ct.day>31||ct.day==0||ct.month==0) // 读取系统时钟错误
  147. {
  148. /* Reset linux system time */
  149. ct.year = 8;
  150. ct.month = 8;
  151. ct.day = 8;
  152. ct.hour = 8;
  153. ct.min = 8;
  154. rtc_to_timespec(&ct, &ts);
  155. rt_printf("gps read system date data error!!!");
  156. }
  157. g_sys_time.tv_sec = ts.tv_sec;
  158. g_sys_time.tv_nsec = ts.tv_nsec;
  159. init_timer_1ms_thread();
  160. return 0;
  161. }
  162. int timer_1ms(void* arg){
  163. cpu_set_t set;
  164. CPU_ZERO(&set);
  165. CPU_SET(3, &set);
  166. pid_t tid = syscall(SYS_gettid);
  167. sched_setaffinity(tid, sizeof(cpu_set_t), &set);
  168. printf("Thread created successfully: %s, PID: %d LWP: %d\r\n",
  169. "timer_1ms", (int)syscall(SYS_gettid), (int)syscall(SYS_gettid));
  170. while(1)
  171. {
  172. gps_isr();
  173. usleep(500);// 扫描间隔改为500us EWen
  174. }
  175. return 0;
  176. }
  177. void init_timer_1ms_thread(void)
  178. {
  179. // 设置实时性
  180. pthread_attr_t attr;
  181. struct sched_param param;
  182. pthread_t thread;
  183. if (pthread_attr_init(&attr) != 0) {
  184. perror("pthread_attr_init failed");
  185. return;
  186. }
  187. if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0) {
  188. perror("pthread_attr_setschedpolicy failed");
  189. goto cleanup;
  190. }
  191. param.sched_priority = 99;
  192. if (pthread_attr_setschedparam(&attr, &param) != 0) {
  193. perror("pthread_attr_setschedparam failed");
  194. goto cleanup;
  195. }
  196. if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0) {
  197. perror("pthread_attr_setinheritsched failed");
  198. goto cleanup;
  199. }
  200. if (pthread_create(&thread, &attr, timer_1ms, NULL) != 0) {
  201. perror("pthread_create failed");
  202. goto cleanup;
  203. }
  204. // 分离线程
  205. pthread_detach(thread);
  206. printf("PPS线程已启动并在后台运行\n");
  207. cleanup:
  208. pthread_attr_destroy(&attr);
  209. }
  210. int gps_exit(void)
  211. {
  212. unsigned long read_result;
  213. void *temp_virt_ctl_base;
  214. if (g_gps_fd >= 0)
  215. {
  216. close(g_gps_fd);
  217. g_gps_fd = -1;
  218. }
  219. temp_virt_ctl_base = (char*)g_gps_map_base + 0x80;
  220. read_result = *(volatile u_int32_t*)(temp_virt_ctl_base);
  221. read_result &= ~(TIMER_CTL_ENABLE);
  222. *(volatile u_int32_t*)(temp_virt_ctl_base) = (read_result);
  223. if (munmap(g_gps_map_base, g_gps_mapped_size) == -1) {
  224. printf("munmap failed!");
  225. return -1;
  226. }
  227. return 0;
  228. }
  229. int gps_get_time(struct timespec *p_ts)
  230. {
  231. unsigned long flags;
  232. register long tv_sec,tv_nsec;
  233. unsigned long read_result;
  234. void *temp_virt_cvl_base, *temp_virt_irq_base;
  235. //检查参数
  236. if(p_ts == 0)
  237. return -1;
  238. //关中断
  239. rt_irq_save(flags);
  240. tv_sec = g_sys_time.tv_sec;
  241. temp_virt_cvl_base = (char*)g_gps_map_base + 0x88;
  242. temp_virt_irq_base = (char*)g_gps_map_base + 0x04;
  243. // read_result = *(volatile u_int32_t*)(temp_virt_cvl_base);
  244. tv_nsec = 3000000 - (*(volatile u_int32_t*)(temp_virt_cvl_base));
  245. if (((*(volatile u_int32_t*)(temp_virt_irq_base)) & (1 << 3)) != 0)
  246. {
  247. tv_sec += 1;
  248. tv_nsec = 3000000 - (*(volatile u_int32_t*)(temp_virt_cvl_base));
  249. }
  250. //恢复中断级别
  251. rt_irq_restore(flags);
  252. p_ts->tv_sec = tv_sec;
  253. p_ts->tv_nsec = (tv_nsec / 3) * 1000;
  254. return 0;
  255. }
  256. int gps_set_time(struct timespec *p_ts)
  257. {
  258. unsigned int flags;
  259. unsigned int tmp;
  260. unsigned int read_result;
  261. void *temp_virt_cvl_base, *temp_virt_irq_base,*temp_virt_ctrl_base;
  262. if (!p_ts) {
  263. return -1;
  264. }
  265. rt_irq_save(flags);
  266. g_sys_time.tv_nsec = p_ts->tv_nsec;
  267. g_sys_time.tv_sec = p_ts->tv_sec;
  268. tmp = 3000000 - ((g_sys_time.tv_nsec / 1000) * 3);
  269. if (tmp > 3000000)
  270. tmp = 3000000;
  271. temp_virt_ctrl_base= (char*)g_gps_map_base + 0x80;
  272. temp_virt_cvl_base = (char*)g_gps_map_base + 0x88;
  273. read_result = *(volatile u_int32_t*)temp_virt_ctrl_base;
  274. read_result &= ~TIMER_CTL_ENABLE;
  275. *(volatile u_int32_t*)temp_virt_ctrl_base = read_result;
  276. *(volatile u_int32_t*)(temp_virt_cvl_base) = tmp;
  277. read_result |= (TIMER_CTL_ENABLE|TIMER_CTL_AUTORELOAD);
  278. *(volatile u_int32_t*)temp_virt_ctrl_base = read_result;
  279. // ((TIMER_TypeDef *) g_timer_base)->TMR3_CVH_REG = 0x0;
  280. rt_irq_restore(flags);
  281. return 0;
  282. }
  283. /******************************************************************************
  284. 函数名称: gps_disable
  285. 函数版本: 01.01
  286. 创建作者:
  287. 创建日期: 2014-12-02
  288. 函数说明: 禁止GPS输入功能。
  289. 参数说明: 无
  290. 返回值: 无.
  291. 修改记录:
  292. */
  293. void gps_disable(void)
  294. {
  295. MCF_REG16(MCF_DTIM_DTMR(CFG_DTIM_GPS_CAP)) &= ~MCF_DTIM_DTMR_CE_ANY;
  296. }
  297. /******************************************************************************
  298. 函数名称: gps_enable
  299. 函数版本: 01.01
  300. 创建作者:
  301. 创建日期: 2014-12-02
  302. 函数说明: 使能GPS输入功能。
  303. 参数说明: 无
  304. 返回值: 无.
  305. 修改记录:
  306. */
  307. void gps_enable(void)
  308. {
  309. MCF_REG16(MCF_DTIM_DTMR(CFG_DTIM_GPS_CAP)) |= MCF_DTIM_DTMR_CE_ANY;
  310. }
  311. /******************************************************************************
  312. 函数名称: gps_reset
  313. 函数版本: 01.01
  314. 创建作者: sunxi
  315. 创建日期: 2008-08-06
  316. 函数说明: gps对时模块复位。
  317. 参数说明: 无
  318. 返回值: 成功返回0.
  319. 修改记录:
  320. */
  321. int gps_reset(void)
  322. {
  323. //全局变量清零
  324. g_signal_valid = 0;
  325. g_keep_alive_gps = 0;
  326. g_irig_old_value = 0;
  327. g_irig_old_interval = 0;
  328. g_irig_code_index = 0;
  329. return 0;
  330. }
  331. /******************************************************************************
  332. 函数名称: gps_pulse_isr
  333. 函数版本: 01.01
  334. 创建作者: ocean
  335. 创建日期: 2023-11-24
  336. 函数说明: gps脉冲中断
  337. 参数说明: 无
  338. 返回值: 成功返回0.
  339. 修改记录:
  340. */
  341. void gps_pulse_isr(void *data)
  342. {
  343. unsigned int flags;
  344. rt_irq_save(flags);
  345. g_sys_time.tv_sec++;
  346. rt_irq_restore(flags);
  347. }
  348. /******************************************************************************
  349. 函数名称: gps_isr
  350. 函数版本: 01.01
  351. 创建作者: sunxi
  352. 创建日期: 2008-08-06
  353. 函数说明: gps中断处理函数。
  354. 参数说明: 无
  355. 返回值: 成功返回0.
  356. 修改记录:
  357. */
  358. void gps_isr(void)
  359. {
  360. void *temp_virt_cvl_base, *temp_virt_irq_base;
  361. // clear irq flags
  362. if (g_gps_map_base) {
  363. temp_virt_irq_base = (char*)g_gps_map_base + 0x04;
  364. if (((*(volatile u_int32_t*)(temp_virt_irq_base)) & (1 << 3)) != 0)
  365. {
  366. gps_pulse_isr(NULL);
  367. *(volatile u_int32_t*)(temp_virt_irq_base) = (1 << 3);
  368. }
  369. }
  370. }
  371. /*------------------------------ 内部函数 -------------------------------------
  372. */
  373. //闰年判断
  374. int _is_leap_year(int year)
  375. {
  376. if ((((year & 0x3) == 0) && (year % 100 != 0)) ||(year % 400 == 0))
  377. return 1;
  378. else
  379. return 0;
  380. }
  381. //日期转换,给出一年中的第几天(从1开始,不是从0开始),算出是这一年的几月几日
  382. int _days_translate(struct rtc_time_t * pt,int year,int day)
  383. {
  384. static int month_last_day[2][13]=
  385. {
  386. {0,31,59,90,120,151,181,212,243,273,304,334,365}, //平年
  387. {0,31,60,91,121,152,182,213,244,274,305,335,366}, //闰年
  388. };
  389. int i;
  390. int *last_day;
  391. if(pt == 0 || day == 0 || day > 366)
  392. return -1;
  393. last_day = month_last_day[_is_leap_year(year)];
  394. for(i=0;i<13;i++)
  395. {
  396. if(day<=last_day[i])
  397. {
  398. break;
  399. }
  400. }
  401. pt->month = (unsigned char)i;
  402. pt->day = (unsigned char)(day-last_day[i-1]);
  403. return 0;
  404. }
  405. int _gps_irig_decode(unsigned char *code,struct rtc_time_t * p_tm )
  406. {
  407. #if 0
  408. int tmp;
  409. //检查P码位置
  410. if( code[0]!=GPS_IRIG_CODE_P
  411. ||code[9]!=GPS_IRIG_CODE_P
  412. ||code[19]!=GPS_IRIG_CODE_P
  413. ||code[29]!=GPS_IRIG_CODE_P
  414. ||code[39]!=GPS_IRIG_CODE_P
  415. ||code[49]!=GPS_IRIG_CODE_P
  416. ||code[59]!=GPS_IRIG_CODE_P
  417. ||code[69]!=GPS_IRIG_CODE_P
  418. ||code[79]!=GPS_IRIG_CODE_P
  419. ||code[89]!=GPS_IRIG_CODE_P)
  420. return -1;
  421. //秒
  422. tmp = ((code[8]<<2)+(code[7]<<1)+code[6])*10
  423. +(code[4]<<3)+(code[3]<<2)+(code[2]<<1)+(code[1]);
  424. if(tmp>=60)
  425. return -2;
  426. p_tm->ms = (unsigned short)(tmp*1000 + p_tm->ms%1000);
  427. //分
  428. tmp = ((code[17]<<2)+(code[16]<<1)+code[15])*10
  429. +(code[13]<<3)+(code[12]<<2)+(code[11]<<1)+(code[10]<<0);
  430. if(tmp>=60)
  431. return -3;
  432. p_tm->min=(unsigned char)tmp;
  433. //时
  434. tmp = ((code[26]<<1)+code[25])*10
  435. +(code[23]<<3)+(code[22]<<GPS_IRIG_CODE_P)+(code[21]<<1)+(code[20]<<0);
  436. if(tmp>=24)
  437. return -4;
  438. p_tm->hour=(unsigned char)tmp;
  439. if(g_clock_mode == CLOCK_MODE_GPS_B_YEAR_NONE)
  440. {
  441. //不带年的B码对时
  442. //日
  443. tmp = ((code[41]<<1)+code[40])*100
  444. +((code[38]<<3)+(code[37]<<2)+(code[36]<<1)+code[35])*10
  445. + (code[33]<<3)+(code[32]<<2)+(code[31]<<1)+code[30];
  446. if(tmp>366 || tmp == 0)
  447. return -5;
  448. p_tm->year = tmp_sys_time.year;
  449. _days_translate(p_tm,p_tm->year + 2000,tmp);
  450. }
  451. else if(g_clock_mode == CLOCK_MODE_GPS_B_YEAR)
  452. {
  453. //带年的B码对时
  454. //日
  455. tmp=((code[38]<<3)+(code[37]<<2)+(code[36]<<1)+code[35])*10+
  456. (code[33]<<3)+(code[32]<<2)+(code[31]<<1)+code[30];
  457. if(tmp>31||tmp==0)
  458. return -6;
  459. p_tm->day = (unsigned char)tmp;
  460. //月
  461. tmp=(code[43]<<3)+(code[42]<<2)+(code[41]<<1)+code[40];
  462. if(tmp > 12)
  463. return -7;
  464. p_tm->month = (unsigned char)tmp;
  465. //年
  466. tmp=((code[58]<<3)+(code[57]<<2)+(code[56]<<1)+code[55])*10+
  467. (code[53]<<3)+(code[52]<<2)+(code[51]<<1)+code[50];
  468. p_tm->year = (unsigned short)tmp;
  469. }
  470. else if(g_clock_mode == CLOCK_MODE_GPS_B_2009)
  471. {
  472. //年
  473. tmp=((code[58]<<3)+(code[57]<<2)+(code[56]<<1)+code[55])*10+
  474. (code[53]<<3)+(code[52]<<2)+(code[51]<<1)+code[50];
  475. p_tm->year = (unsigned short)tmp;
  476. //日
  477. tmp = ((code[41]<<1)+code[40])*100
  478. +((code[38]<<3)+(code[37]<<2)+(code[36]<<1)+code[35])*10
  479. + (code[33]<<3)+(code[32]<<2)+(code[31]<<1)+code[30];
  480. if(tmp>366 || tmp == 0)
  481. return -8;
  482. _days_translate(p_tm,p_tm->year + 2000,tmp);
  483. }
  484. #endif
  485. return 0;
  486. }
  487. void _ms_update_time(void)
  488. {
  489. struct rtc_time_t *pt = &tmp_sys_time;
  490. int day_flag = 0;
  491. if(pt->ms<60000)
  492. return;
  493. pt->ms = 0;
  494. pt->min++;
  495. if(pt->min>=60)
  496. {
  497. pt->min=0;
  498. pt->hour++;
  499. if(pt->hour>=24)
  500. {
  501. pt->hour=0;
  502. pt->day++;
  503. if(pt->month==2)
  504. {
  505. // 2月
  506. if(pt->day>28)
  507. {
  508. if(_is_leap_year(pt->year+2000))
  509. {
  510. //闰年 2月29天?
  511. if(pt->day>29)
  512. {
  513. day_flag=1;
  514. }
  515. }
  516. else
  517. {
  518. //其他28天
  519. day_flag=1;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. //非2月
  526. if(pt->day>30)
  527. {
  528. if(pt->month==4||pt->month==6||pt->month==9||pt->month==11)
  529. {
  530. day_flag=1;
  531. }
  532. }
  533. if(pt->day>31)
  534. {
  535. day_flag=1;
  536. }
  537. }
  538. if(day_flag)
  539. {
  540. pt->day = 1;
  541. pt->month++;
  542. if(pt->month>12)
  543. {
  544. pt->month=1;
  545. pt->year++;
  546. }
  547. }
  548. }
  549. }
  550. }
  551. void _gps_1s_isr(void)
  552. {
  553. //unsigned int now;
  554. //uint32_t flags;
  555. #if 0
  556. if(tmp_sys_time.ms%2 == 0)
  557. {
  558. // DIO_KOUT6_OFF();
  559. }
  560. else
  561. {
  562. // DIO_KOUT6_ON();
  563. }
  564. //计数
  565. tmp_sys_time.ms ++;
  566. //输出对时脉冲(10ms,下降沿)
  567. if((tmp_sys_time.ms%1000) >= 10 )
  568. {
  569. //GPIO_GPS_HIGH();
  570. }
  571. else if(tmp_sys_time.ms%1000 != 0)
  572. {
  573. //GPIO_GPS_LOW();
  574. }
  575. else
  576. {
  577. //tmp_sys_time.ms%1000 == 0的情况
  578. if(g_clock_mode == CLOCK_MODE_EXT || g_clock_mode == CLOCK_MODE_GPS_MINUTE)
  579. {
  580. //当对时模式为外部对时或分对时的时候,在这儿输出下降沿。
  581. //GPIO_GPS_LOW();
  582. }
  583. else if(g_signal_valid == 0)
  584. {
  585. //输入信号不合法,在此输出下降沿
  586. //GPIO_GPS_LOW();
  587. }
  588. }
  589. #endif
  590. rt_irq_save(flags);
  591. g_sys_time.tv_sec++;
  592. g_1s_refence += 1000000;
  593. MCF_REG32(MCF_DTIM_DTRR(CFG_DTIM_GPS_CAP)) = g_1s_refence;
  594. rt_irq_restore(flags);
  595. //更新系统时间
  596. //_ms_update_time();
  597. //GPS保活处理
  598. if(g_keep_alive_gps)
  599. {
  600. // 减1s
  601. g_keep_alive_gps -= 1000;
  602. }
  603. return;
  604. }
  605. void _gps_capture_isr(void)
  606. {
  607. #if 0
  608. unsigned int interval,capture_value,n;
  609. //如果不是GPS对时模式,不进行任何处理
  610. // if(g_clock_mode == CLOCK_MODE_EXT)
  611. return;
  612. //得到计数值,并计算和上一次计数器之差,也就是两次中断之间的计数值.
  613. capture_value = MCF_REG32(MCF_DTIM_DTCR(CFG_DTIM_GPS_CAP));
  614. interval = (capture_value - g_irig_old_value + 500L)/1000;
  615. //消除干扰信号
  616. if(interval < 2)
  617. {
  618. rt_printf("gps:interval = %d\r\n",interval);
  619. //干扰信号,直接返回
  620. return;
  621. }
  622. #if 0
  623. if(GPIO_GPS_INT_STATUS())
  624. rt_printf("H:");
  625. else
  626. rt_printf("L:");
  627. rt_printf("%d,%d\r\n",capture_value - g_irig_old_value,capture_value);
  628. goto __ret;
  629. #endif
  630. if(g_clock_mode == CLOCK_MODE_GPS_SECOND)
  631. {
  632. //输入信号不为低电平时不进行逻辑判断。
  633. //注意:目前输入信号经过了两个反相器。
  634. if(GPIO_GPS_INT_STATUS() == 0)
  635. {
  636. goto __ret;
  637. }
  638. //检查输入信号正负脉冲之和是否为1000ms.
  639. n = interval + g_irig_old_interval;
  640. //if( n < 999 || n > 1001)
  641. if( abs(n - 1000) > GPS_SIGAL_DIFF)
  642. { //输入信号不正常,重新搜索秒开始位置
  643. rt_printf("gps:%d,%d\r\n",interval,g_irig_old_interval);
  644. g_signal_valid = 0;
  645. goto __ret;
  646. }
  647. //分板对时下降沿
  648. //GPIO_GPS_LOW();
  649. //修正时间
  650. tmp_sys_time.ms = (unsigned short)((tmp_sys_time.ms+500)/1000*1000);
  651. _ms_update_time();
  652. //修正边沿
  653. MCF_REG08(MCF_DTIM_DTER(CFG_DTIM_GPS_CAP)) = MCF_DTIM_DTER_REF;
  654. g_1s_refence = capture_value + 1000000;
  655. MCF_REG32(MCF_DTIM_DTRR(CFG_DTIM_GPS_CAP)) = g_1s_refence;
  656. //GPS保活
  657. g_keep_alive_gps = CLOCK_ALIVE_GPS;
  658. g_signal_valid = 1;
  659. goto __ret;
  660. }
  661. if(g_clock_mode == CLOCK_MODE_GPS_MINUTE)
  662. {
  663. //输入信号不为低电平时不进行逻辑判断。
  664. //注意:目前输入信号经过了两个反相器。
  665. if(GPIO_GPS_INT_STATUS() == 0)
  666. {
  667. goto __ret;
  668. }
  669. //检查输入信号正负脉冲之和是否为1分钟。
  670. //由于我们使用的晶体是50ppm的,在一分钟之内可能有3ms的误差,
  671. //所以我们以5ms作为指标判断。
  672. n = interval + g_irig_old_interval;
  673. if( n < (60*1000 - 5) || n > (60*1000 + 5))
  674. { //输入信号不正常,重新搜索分开始位置
  675. rt_printf("gps:%d,%d\r\n",interval,g_irig_old_interval);
  676. g_signal_valid = 0;
  677. goto __ret;;
  678. }
  679. //分板对时下降沿
  680. //GPIO_GPS_LOW();
  681. //修正时间
  682. if(tmp_sys_time.ms > 30000)
  683. {
  684. tmp_sys_time.ms = 60000;
  685. _ms_update_time();
  686. }
  687. else
  688. {
  689. tmp_sys_time.ms = 0;
  690. }
  691. //修正边沿
  692. MCF_REG08(MCF_DTIM_DTER(CFG_DTIM_GPS_CAP)) = MCF_DTIM_DTER_REF;
  693. g_1s_refence = capture_value + 1000000;
  694. MCF_REG32(MCF_DTIM_DTRR(CFG_DTIM_GPS_CAP)) = g_1s_refence;
  695. //GPS保活
  696. g_keep_alive_gps = CLOCK_ALIVE_GPS + 60000;
  697. g_signal_valid = 1;
  698. goto __ret;
  699. }
  700. //!!!以下为B码对时
  701. //检查输入信号
  702. if(abs(interval - 2)> GPS_SIGAL_DIFF && abs(interval - 5) > GPS_SIGAL_DIFF && abs(interval - 8) > GPS_SIGAL_DIFF)
  703. {
  704. rt_printf("gps:%d\r\n",interval);
  705. //输入信号不正常,重新搜索秒开始位置
  706. g_signal_valid = 0;
  707. g_irig_code_index = 0;
  708. goto __ret;
  709. }
  710. //输入信号不为低电平时不进行逻辑判断。
  711. //注意:目前输入信号经过了两个反相器。
  712. if(GPIO_GPS_INT_STATUS() == 0)
  713. {
  714. goto __ret;
  715. }
  716. //检查输入信号正负脉冲之和是否为10ms。
  717. if(abs(interval + g_irig_old_interval - 10) > GPS_SIGAL_DIFF)
  718. { //输入信号不正常,重新搜索秒开始位置
  719. rt_printf("gps:%d,%d\r\n",interval,g_irig_old_interval);
  720. g_signal_valid = 0;
  721. g_irig_code_index = 0;
  722. goto __ret;;
  723. }
  724. //如果g_irig_code_index<2,需要搜索秒开始位置
  725. if(g_irig_code_index <2)
  726. {
  727. //如果不是P码,放弃处理,重新搜索。
  728. if(abs(g_irig_old_interval - 8) > GPS_SIGAL_DIFF)
  729. {
  730. g_signal_valid = 0;
  731. g_irig_code_index = 0;
  732. goto __ret;
  733. }
  734. //保存P码
  735. g_irig_code[g_irig_code_index++] = GPS_IRIG_CODE_P;
  736. if(g_irig_code_index == 1 && g_signal_valid == 1)
  737. {
  738. //分板对时下降沿
  739. //GPIO_GPS_LOW();
  740. //修正时间
  741. tmp_sys_time.ms = (unsigned short)((tmp_sys_time.ms+500)/1000*1000);
  742. _ms_update_time();
  743. //修正边沿
  744. MCF_REG32(MCF_DTIM_DTER(CFG_DTIM_GPS_CAP)) = MCF_DTIM_DTER_REF;
  745. g_1s_refence = capture_value + 1000000;
  746. MCF_REG32(MCF_DTIM_DTRR(CFG_DTIM_GPS_CAP)) = g_1s_refence;
  747. }
  748. goto __ret;
  749. }
  750. //保存编码
  751. switch(g_irig_old_interval)
  752. {
  753. case 1:
  754. case 2:
  755. case 3:
  756. g_irig_code[g_irig_code_index++] = GPS_IRIG_CODE_0;
  757. break;
  758. case 4:
  759. case 5:
  760. case 6:
  761. g_irig_code[g_irig_code_index++] = GPS_IRIG_CODE_1;
  762. break;
  763. case 7:
  764. case 8:
  765. case 9:
  766. g_irig_code[g_irig_code_index++] = GPS_IRIG_CODE_P;
  767. break;
  768. default:
  769. rt_printf("gps:g_irig_old_interval=%d!\r\n",g_irig_old_interval);
  770. g_signal_valid = 0;
  771. g_irig_code_index = 0;
  772. goto __ret;
  773. break;
  774. }
  775. //如果接收完时间信息,就开始处理
  776. if(g_irig_code_index >= 100)
  777. {
  778. if(_gps_irig_decode(&g_irig_code[1],&tmp_sys_time) == 0)
  779. {
  780. g_signal_valid = 1;
  781. g_keep_alive_gps = CLOCK_ALIVE_GPS;
  782. }
  783. else
  784. g_signal_valid = 0;
  785. g_irig_code_index = 0;
  786. }
  787. __ret:
  788. g_irig_old_value = capture_value;
  789. g_irig_old_interval = interval;
  790. #endif
  791. }
  792. /*------------------------------ 测试函数 -------------------------------------
  793. */
  794. #ifdef _DEBUG_GPS
  795. #include "ustimer.h"
  796. int gps_test(void)
  797. {
  798. unsigned int us0,t;
  799. rt_printf("gps_test start...\r\n");
  800. // rt_irq_level(0);
  801. t = 0;
  802. us0 = ustimer_get_origin();
  803. // sunxi 20220424 while(1) ;
  804. while(t < 10)
  805. {
  806. // rt_printf("pit(t=%03dsec):g_gps_ms:%06d,diff(g_gps_ms-t*1000):%d!\r\n",t,g_gps_ms,g_gps_ms -t*1000);
  807. rt_printf("g_sys_time: %04d-%02d-%02d time: %02d:%02d:%05d!\r\n",
  808. tmp_sys_time.year + 2000,
  809. tmp_sys_time.month,
  810. tmp_sys_time.day,
  811. tmp_sys_time.hour,
  812. tmp_sys_time.min,
  813. tmp_sys_time.ms
  814. );
  815. ustimer_delay_origin(us0,++t*USTIMER_SEC);
  816. }
  817. rt_printf("gps_test end...\r\n");
  818. return 0;
  819. }
  820. #endif