can_app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: can_app.c
  4. 文件版本: 01.01
  5. 创建作者: sunxi
  6. 创建日期: 2013-02-28
  7. 功能说明: can传输应用层
  8. 其它说明:
  9. 修改记录:
  10. */
  11. /*------------------------------- 头文件 --------------------------------------
  12. */
  13. #include "head.h"
  14. #ifdef BSP_CAN_ENABLE
  15. /*------------------------------- 宏定义 --------------------------------------
  16. */
  17. /*------------------------------ 类型结构 -------------------------------------
  18. */
  19. /*------------------------------ 全局变量 -------------------------------------
  20. */
  21. int g_sb_monitor;
  22. /*------------------------------ 函数声明 -------------------------------------
  23. */
  24. int can_app_callback(u32 can_bus,u8 * buf);
  25. /*------------------------------ 外部函数 -------------------------------------
  26. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  27. */
  28. int can_app_init(void)
  29. {
  30. can_regester_recv_callback(can_app_callback);
  31. return 0;
  32. }
  33. int can_app_exit(void)
  34. {
  35. can_regester_recv_callback(NULL);
  36. return 0;
  37. }
  38. int can_app_callback(u32 can_bus,u8 * buf)
  39. {
  40. struct can_frame_head *cfh;
  41. // 检查参数
  42. if(can_bus >= CAN_BUS_NUM)
  43. {
  44. return -1;
  45. }
  46. if(buf == NULL)
  47. {
  48. return -2;
  49. }
  50. cfh = (struct can_frame_head *)buf;
  51. switch(cfh->type)
  52. {
  53. case CAN_FRAME_TYPE_DI_INIT:
  54. case CAN_FRAME_TYPE_DI_ON:
  55. case CAN_FRAME_TYPE_DI_OFF:
  56. case CAN_FRAME_TYPE_DI_INIT1:
  57. case CAN_FRAME_TYPE_DI_ON1:
  58. case CAN_FRAME_TYPE_DI_OFF1:
  59. #if defined(BSP_DTU3) || defined(BSP_DTU2)
  60. dido_di_update(buf);
  61. #endif
  62. break;
  63. case CAN_FRAME_TYPE_SB_INFO:
  64. #if defined(BSP_DTU3) || defined(BSP_DTU2) || defined(CAN_SLAVE_BOARD)
  65. equ_board_info_update(can_bus,buf);
  66. #endif
  67. break;
  68. default:
  69. return 0;
  70. break;
  71. }
  72. return 1;
  73. }
  74. u8 can_app_checksum(u8 *buf,u8 len)
  75. {
  76. u8 i,sum;
  77. sum = 0;
  78. for(i=0; i<len; i++)
  79. {
  80. sum += *buf++;
  81. }
  82. return sum;
  83. }
  84. int can_app_timing(void)
  85. {
  86. static long g_timing_sec = 0;
  87. int i,ret;
  88. u8 *buf;
  89. struct timespec ts;
  90. clk_time_get(&ts);
  91. // 对时帧必须在内部对时秒脉冲发出10ms后再发送,每秒发送一次。
  92. if(ts.tv_sec == g_timing_sec || ts.tv_nsec < 10*NSEC_PER_MS)
  93. {
  94. return 0;
  95. }
  96. // 帧头
  97. buf = can_request_tx_buf(CAN_FRAME_TYPE_TIMING);
  98. buf[0] = CAN_FRAME_TYPE_TIMING;
  99. buf[1] = CAN_BUS_ADDR_BCAST;
  100. buf[2] = 0;
  101. buf[3] = sizeof(long);
  102. // 得到时间
  103. memcpy(buf+4,&ts.tv_sec,sizeof(long));//noted by sunxi: 这里需要注意大小端问题
  104. // 发送
  105. for(i=0;i<CAN_BUS_NUM;i++)
  106. {
  107. ret = can_send(i,buf);
  108. if(ret < 0)
  109. {
  110. // rt_printf("can_app_timing:ret=%d\r\n",ret);
  111. }
  112. }
  113. g_timing_sec = ts.tv_sec;
  114. return 0;
  115. }
  116. int can_app_reset(int is_watchdog)
  117. {
  118. int i ;
  119. u8 *buf;
  120. // 帧头
  121. buf = can_request_tx_buf(CAN_FRAME_TYPE_RESET);
  122. buf[0] = CAN_FRAME_TYPE_RESET;
  123. buf[1] = CAN_BUS_ADDR_BCAST;
  124. buf[2] = 0;
  125. buf[3] = 1;
  126. buf[4] = (u8)is_watchdog;
  127. // 发送
  128. for(i=0;i<CAN_BUS_NUM;i++)
  129. {
  130. can_send(i,buf);
  131. }
  132. // 子板复位时更新时间,避免产生子板通讯异常错误。
  133. for(i=0;i<EQU_SLOT_NUM_MAX;i++)
  134. {
  135. g_board_info[i].us0 = ustimer_get_origin();
  136. }
  137. return 0;
  138. }
  139. int can_app_sb_monitor(void)
  140. {
  141. g_sb_monitor = 1;
  142. return 0;
  143. }
  144. int can_app_sb_monitor_send(void)
  145. {
  146. u8 buf[4];
  147. buf[0] = CAN_FRAME_TYPE_SB_MONITOR;
  148. buf[1] = CAN_BUS_ADDR_BCAST;
  149. buf[2] = 0;
  150. buf[3] = 0;
  151. // 发送
  152. can_send(0,buf);
  153. can_send(1,buf);
  154. return 0;
  155. }
  156. //can子板查询 oujie add 20200420
  157. int can_app_sb_discover(void)
  158. {
  159. u8 buf[4];
  160. u32 slot;
  161. #if defined(BSP_DTU3) || defined(BSP_DTU2)
  162. slot = equ_get_slot_by_type(BOARD_TYPE_METERING);
  163. #else
  164. slot = 1;
  165. #endif
  166. if(slot >0)
  167. {
  168. buf[0] = CAN_FRAME_TYPE_DISCOVER;
  169. buf[1] = CAN_BUS_ADDR_BCAST;
  170. buf[2] = 0;
  171. buf[3] = 0;
  172. #ifdef CAN_BOARD_DEBUG
  173. rt_printf("查询子板\r\n");
  174. #endif
  175. // 发送
  176. can_send(0,buf);
  177. //can_send(1,buf);
  178. }
  179. return 0;
  180. }
  181. #if 1
  182. //分配can子板卡槽号 oujie add 20200420
  183. int can_app_sb_give_slot(u32 no, u8* buf)
  184. {
  185. u8 send_buf[32];
  186. char id_string[64] = {0};
  187. struct can_board_res* can_brd;
  188. Hex2Str(&buf[5], id_string, CHIP_ID_SIZE*4);
  189. #ifdef CAN_BOARD_DEBUG
  190. rt_printf("子板id字符串 = %s\r\n", id_string);
  191. rt_printf("获取卡槽号\r\n");
  192. #endif
  193. can_brd = equ_get_can_slot(buf[4], buf[5], &buf[6], sizeof(can_brd->chip_id));
  194. if(can_brd != NULL)
  195. {
  196. can_brd->type = buf[5];
  197. send_buf[0] = CAN_FRAME_TYPE_GIVE_CAN_SLOT;
  198. send_buf[1] = CAN_BUS_ADDR_BCAST;
  199. send_buf[2] = 0;
  200. send_buf[3] = sizeof(can_brd->chip_id)+1;
  201. send_buf[4] = can_brd->slot;
  202. memcpy(&send_buf[5], can_brd->chip_id, sizeof(can_brd->chip_id));
  203. #ifdef CAN_BOARD_DEBUG
  204. rt_printf("CAN发送卡槽号 slot = %d\r\n", can_brd->slot);
  205. rt_printf("CAN卡类型 type = %d\r\n", can_brd->type);
  206. #endif
  207. // 发送
  208. can_send(no, send_buf);
  209. }
  210. else
  211. {
  212. #ifdef CAN_BOARD_DEBUG
  213. rt_printf("卡槽号已存在,不再发送\r\n");
  214. #endif
  215. }
  216. return 0;
  217. }
  218. #endif
  219. struct sb_monitor
  220. {
  221. // 系统
  222. u32 slot;
  223. u32 type;
  224. u32 start_sec;
  225. //中断时间
  226. u32 us_gps_min;
  227. u32 us_gps_max;
  228. u32 us_100us_min;
  229. u32 us_100us_max;
  230. u32 us_can_min;
  231. u32 us_can_max;
  232. // 开出
  233. u32 do_delay_min;
  234. u32 do_delay_max;
  235. // CAN总线
  236. u32 can_rx_dropped;
  237. u32 can_tx_dropped;
  238. u32 can_hw_bus_errors;
  239. u32 can_hw_errors;
  240. u32 can_hw_overrun;
  241. u32 can_overrun;
  242. };
  243. extern struct rt_stat g_stat_do_delay;
  244. int can_app_sb_monitor_recv(u8 * buf)
  245. {
  246. struct sb_monitor sm;
  247. struct timespec ts;
  248. memcpy(&sm,&buf[4],sizeof(sm));
  249. rt_printf("\r\n子板监视信息[slot=%02d,type=%02d]:\r\n",sm.slot,sm.type);
  250. ts.tv_sec = sm.start_sec;
  251. ts.tv_nsec = 0;
  252. rt_printf("上电时间: ");
  253. rt_printf_time2(ts);
  254. rt_printf(" GPS中断时间:\tmin=%d,\tmax=%d\r\n",sm.us_gps_min,sm.us_gps_max);
  255. rt_printf("100us中断时间:\tmin=%d,\tmax=%d\r\n",sm.us_100us_min,sm.us_100us_max);
  256. rt_printf(" CAN中断时间:\tmin=%d,\tmax=%d\r\n",sm.us_can_min,sm.us_can_max);
  257. rt_printf(" 开出延时时间:\tmin=%d,\tmax=%d\r\n",sm.do_delay_min,sm.do_delay_max);
  258. rt_stat_in(&g_stat_do_delay,sm.do_delay_max);
  259. rt_printf("CAN总线接收丢包:%d\r\n",sm.can_rx_dropped);
  260. rt_printf("CAN总线发送丢包:%d\r\n",sm.can_tx_dropped);
  261. rt_printf("CAN总线总线错误:%d\r\n",sm.can_hw_bus_errors);
  262. rt_printf("CAN总线硬件错误:%d\r\n",sm.can_hw_errors);
  263. rt_printf("CAN总线硬件溢出:%d\r\n",sm.can_hw_overrun);
  264. rt_printf("CAN总线接收溢出:%d\r\n",sm.can_overrun);
  265. return 0;
  266. }
  267. int can_app_sb_discover_recv(u8 * buf)
  268. {
  269. struct can_board_res* can_brd;
  270. #ifdef CAN_BOARD_DEBUG
  271. uint8_t i;
  272. #endif
  273. #if !defined(BSP_DTU3) || !defined(BSP_DTU2)
  274. memcpy((u8*)can_board[0].chip_id, &buf[6], sizeof(can_brd->chip_id));
  275. #endif
  276. #ifdef CAN_BOARD_DEBUG
  277. rt_printf("\r\n接收到子板chip_id: ");
  278. for(i=0; i<sizeof(can_brd->chip_id); i++)
  279. {
  280. rt_printf("%x ", buf[6+i]);
  281. }
  282. rt_printf("\r\n");
  283. #endif
  284. return 0;
  285. }
  286. int can_app_sb_slot_recv(u8 * buf)
  287. {
  288. #ifdef CAN_BOARD_DEBUG
  289. rt_printf("接收到子板分配卡槽的应答\r\n");
  290. #endif
  291. #if defined(BSP_DTU3) || defined(BSP_DTU2)
  292. uint8_t i;
  293. for(i=0; i<CAN_BOARD_NUM; i++)
  294. {
  295. if(can_board[i].file_have)
  296. {
  297. if((memcmp((u8*)can_board[i].chip_id, &buf[5], sizeof(can_board[i].chip_id)) == 0)
  298. && (can_board[i].slot == buf[4]))
  299. {
  300. #ifdef CAN_BOARD_DEBUG
  301. rt_printf("分配子板号完成\r\n");
  302. #endif
  303. can_board[i].given_slot = 1;
  304. }
  305. }
  306. }
  307. #else
  308. if((memcmp((u8*)can_board[0].chip_id, &buf[5], sizeof(can_board[0].chip_id)) == 0)
  309. && (can_board[0].slot == buf[4]))
  310. {
  311. #ifdef CAN_BOARD_DEBUG
  312. rt_printf("分配子板号完成\r\n");
  313. #endif
  314. can_board[0].given_slot = 1;
  315. }
  316. #endif
  317. return 0;
  318. }
  319. int errcount_crc=0;
  320. int errcount_sb=0;
  321. int errcount_no=0;
  322. int errcount_sb_no=0;
  323. int errcount_check=0;
  324. static u32 g_no_send[CAN_BUS_NUM];
  325. static u32 g_no_recv[EQU_SLOT_NUM_MAX];
  326. int can_app_test_send(void)
  327. {
  328. u8 *buf_tx;
  329. int i,can,no,len,ret;
  330. len = 200;
  331. buf_tx = can_request_tx_buf(CAN_FRAME_TYPE_TEST);
  332. for(can=0;can<CAN_BUS_NUM;can++)
  333. {
  334. no = g_no_send[can];
  335. buf_tx[0] = CAN_FRAME_TYPE_TEST;
  336. buf_tx[1] = CAN_BUS_ADDR_BCAST;
  337. buf_tx[2] = 0;
  338. buf_tx[3] = len;
  339. buf_tx[4] = 0;
  340. buf_tx[5] = no;
  341. buf_tx[6] = no>>8;
  342. buf_tx[7] = no>>16;
  343. buf_tx[8] = no>>24;
  344. for(i=0;i<(len-6);i++)
  345. {
  346. buf_tx[i+9] = i;
  347. }
  348. buf_tx[len+3] = can_app_checksum(buf_tx+4,len-1);
  349. // 发送
  350. ret = can_send(can,buf_tx);
  351. if(ret < 0)
  352. {
  353. if(ret != -7)
  354. {
  355. rt_printf("can_app_test error:ret=%d\r\n",ret);
  356. }
  357. }
  358. else
  359. {
  360. g_no_send[can]++;
  361. }
  362. }
  363. return 0;
  364. }
  365. int can_app_test_recv(u8 * buf)
  366. {
  367. u8 len,crc,crc_check;
  368. int i,no,slot;
  369. slot = buf[2];
  370. len = buf[3];
  371. crc = buf[len+3];
  372. crc_check = can_app_checksum(buf+4,len-1);
  373. no = buf[5] | ((int)buf[6])<<8 | ((int)buf[7])<<16 | ((int)buf[8])<<24;
  374. // 检查CRC
  375. if(crc != crc_check)
  376. {
  377. errcount_crc++;
  378. rt_printf("can_app_test_recv:crc error!slot=%d,type=%d,no=%d, crc=%x,crc_check=%x,errcount_crc=%d\r\n",slot,buf[4],no,crc,crc_check,errcount_crc);
  379. print_msg("RX_CAN:",buf,buf[3] + 4);
  380. return -1;
  381. }
  382. // 检查子板接收错误
  383. if(buf[4] != 0)
  384. {
  385. errcount_sb++;
  386. rt_printf("can_app_test_recv:sb error! slot=%d,type=%d,no=%d, errcount_sb=%d\r\n",slot,buf[4],no,errcount_sb);
  387. print_msg("RX_CAN:",buf,buf[3] + 4);
  388. return -2;
  389. }
  390. // 检查内容
  391. for(i=0;i<(len-6);i++)
  392. {
  393. if(buf[i+9] != i)
  394. {
  395. break;
  396. }
  397. }
  398. if(i != (len-6))
  399. {
  400. errcount_check++;
  401. rt_printf("can_app_test_recv:content error!slot=%d,no=%d,errcount_check=%d\r\n",slot,no,errcount_check);
  402. print_msg("RX_CAN:",buf,buf[3] + 4);
  403. return -4;
  404. }
  405. // 检查序号
  406. if(no != g_no_recv[slot])
  407. {
  408. errcount_sb_no++;
  409. rt_printf("can_app_test_recv:no error!slot=%d,no=%d,g_no_recv[slot]=%d,errcount_sb_no=%d\r\n",slot,no,g_no_recv[slot],errcount_sb_no);
  410. print_msg("RX_CAN:",buf,buf[3] + 4);
  411. return -5;
  412. }
  413. // 检查ok,序号加一
  414. g_no_recv[slot]++;
  415. return 0;
  416. }
  417. int can_task(void)
  418. {
  419. int i;
  420. u8 * buf;
  421. struct can_frame_head *cfh;
  422. static unsigned char send_discover = 0;
  423. #ifdef CAN_SLAVE_BOARD
  424. static unsigned long us0 = 0;
  425. static unsigned long us1 = 0;
  426. static unsigned char send_discover = 0;
  427. #endif
  428. // static unsigned long us1 = 0;
  429. u8 can_no = 0;
  430. if(g_sb_monitor)
  431. {
  432. can_app_sb_monitor_send();
  433. g_sb_monitor = 0;
  434. }
  435. buf = can_request_tx_buf(CAN_FRAME_TYPE_PROGRAM_UPDATE);
  436. // 最多一次接收8帧
  437. for(i=0; i<128; i++)
  438. {
  439. if(can_recv(0,buf,CAN_FRAME_LEN_MAX) <= 0)
  440. {
  441. if(can_recv(1,buf,CAN_FRAME_LEN_MAX) <= 0)
  442. {
  443. prog_recv_timeout();
  444. break;
  445. }
  446. else
  447. can_no = 1;
  448. }
  449. else
  450. can_no = 0;
  451. // 检查帧内容
  452. cfh = (struct can_frame_head *)buf;
  453. #if 0
  454. if(cfh->src >= EQU_SLOT_NUM_MAX)
  455. {
  456. continue;
  457. }
  458. #endif
  459. if(cfh->src >= (EQU_SLOT_NUM_MAX+CAN_BOARD_NUM))
  460. {
  461. continue;
  462. }
  463. // 处理不同类型的帧
  464. switch(cfh->type)
  465. {
  466. case CAN_FRAME_TYPE_PROGRAM_UPDATE:
  467. prog_recv(buf);
  468. break;
  469. case CAN_FRAME_TYPE_SB_MONITOR:
  470. can_app_sb_monitor_recv(buf);
  471. break;
  472. case CAN_FRAME_TYPE_TEST:
  473. if(can_app_test_recv(buf) != 0)
  474. {
  475. g_test_on = 0;
  476. g_print_can = 0;
  477. }
  478. break;
  479. case CAN_FRAME_TYPE_DISCOVER:
  480. can_app_sb_discover_recv(buf);
  481. can_app_sb_give_slot(can_no, buf);
  482. send_discover = 0;
  483. can_brd_error_clear(buf[4]);
  484. break;
  485. case CAN_FRAME_TYPE_GIVE_CAN_SLOT: //收到子板应答
  486. can_app_sb_slot_recv(buf);
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. // 子板程序更新发送
  493. prog_send();
  494. #ifdef CAN_SLAVE_BOARD
  495. if(!is_prog_down())
  496. {
  497. if(us0 == 0)
  498. us0 = ustimer_get_origin();
  499. if(ustimer_delay_origin2(&us0, 3*USTIMER_SEC))
  500. {
  501. us0 = 0;
  502. can_app_sb_discover();
  503. send_discover++;
  504. us1 = ustimer_get_origin();
  505. }
  506. if(send_discover>=2)
  507. {
  508. if(ustimer_delay_origin2(&us1, 2*USTIMER_SEC))
  509. {
  510. send_discover = 0;
  511. us1 = ustimer_get_origin();
  512. can_brd_error_set();
  513. // rt_printf("can_brd_error_set\r\n");
  514. }
  515. }
  516. }
  517. #endif
  518. #if defined(METERING_ENERGY) && defined(CAN_SLAVE_BOARD)
  519. if(!is_prog_down()&&!send_discover)
  520. can_metering_send_app();
  521. #endif
  522. if(g_test_on)
  523. {
  524. // can_app_test_send();
  525. }
  526. return 0;
  527. }
  528. int can_app_test_printf(void)
  529. {
  530. int i;
  531. for(i=0;i<CAN_BUS_NUM;i++)
  532. {
  533. rt_printf("slot%02d:%d.\r\n",i,g_no_send[i]);
  534. }
  535. rt_printf("g_no_recv:\r\n");
  536. for(i=0;i<EQU_SLOT_NUM_MAX;i++)
  537. {
  538. rt_printf("slot%02d:%d.\r\n",i,g_no_recv[i]);
  539. }
  540. return 0;
  541. }
  542. #endif
  543. /*------------------------------ 内部函数 -------------------------------------
  544. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  545. */
  546. /*------------------------------ 测试函数 -------------------------------------
  547. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  548. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  549. */
  550. /*------------------------------ 文件结束 -------------------------------------
  551. */