can_task.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include "can_task.h"
  2. #include "J1939.H"
  3. #include "can_cali.h"
  4. #include "can_id_deal.h"
  5. #include "can_interface.h"
  6. #include "iap.h"
  7. #include "queue.h"
  8. #include "utils.h"
  9. #include <stdint.h>
  10. #include <string.h>
  11. queue_entry(pdu_tag, 40) can_rx_queue;
  12. queue_entry(pdu_tag, 40) can_tx_queue;
  13. uint8_t PGN_00B000H_Ary[20] = {0};
  14. extern void j1939_msg_push_queue(p_pdu_tag rec_msg);
  15. uint8_t recv_can_id = 0;
  16. uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
  17. {
  18. pdu_tag response_msg;
  19. QUEUE_STATUS result_status;
  20. response_msg.id.r = id;
  21. response_msg.reg.dlc = len;
  22. memcpy(&response_msg.data.u8_buf[0], p_data, len);
  23. if (id > 0x7FF)
  24. {
  25. response_msg.reg.ide = CAN_ID_EXT;
  26. }
  27. else
  28. {
  29. response_msg.reg.ide = CAN_ID_STD;
  30. }
  31. __disable_irq();
  32. en_queue(&can_tx_queue, response_msg, result_status);
  33. __enable_irq();
  34. if (result_status != Q_OK)
  35. {
  36. return 0;
  37. }
  38. return 1;
  39. }
  40. static uint8_t can_tx_frame(pdu_tag can_message)
  41. {
  42. can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
  43. return 1;
  44. }
  45. void can_tx_callback(void)
  46. {
  47. pdu_tag tx_data;
  48. QUEUE_STATUS result_status;
  49. de_queue(&can_tx_queue, tx_data, result_status);
  50. // 返回值为1代表读取成功
  51. if (result_status == Q_OK)
  52. {
  53. can_tx_frame(tx_data);
  54. CAN_IT_TME_ENABLE();
  55. }
  56. else
  57. {
  58. CAN_IT_TME_DISABLE();
  59. }
  60. }
  61. void can_rx_callback(CanRxMsg rx_message)
  62. {
  63. pdu_tag data;
  64. uint8_t ps;
  65. uint8_t pf;
  66. QUEUE_STATUS result;
  67. switch (rx_message.ExtId)
  68. {
  69. case 0x18011801:
  70. data.id.r = 0x18011801;
  71. data.reg.dlc = rx_message.DLC;
  72. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  73. en_queue(&can_rx_queue, data, result);
  74. break;
  75. case 0x18011802:
  76. data.id.r = 0x18011802;
  77. data.reg.dlc = rx_message.DLC;
  78. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  79. break;
  80. default:
  81. pf = (rx_message.ExtId & CAN_PGN_PF) >> 16;
  82. if (pf >= 0xF0)
  83. {
  84. data.id.r = rx_message.ExtId;
  85. recv_can_id = data.id.b.sa;
  86. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  87. en_queue(&can_rx_queue, data, result);
  88. }
  89. else
  90. {
  91. ps = (rx_message.ExtId & CAN_PGN_PS) >> 8;
  92. if (ps == 0xF4)
  93. {
  94. data.id.r = rx_message.ExtId;
  95. recv_can_id = data.id.b.sa;
  96. data.reg.dlc = rx_message.DLC;
  97. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  98. if (data.id.b.pf == 0xDF)
  99. {
  100. en_queue(&can_rx_queue, data, result);
  101. }
  102. else
  103. {
  104. en_queue(&can_rx_queue, data, result);
  105. }
  106. }
  107. else
  108. {
  109. en_queue(&can_rx_queue, data, result);
  110. }
  111. }
  112. }
  113. }
  114. uint8_t tx_flag = 0;
  115. void can_rx_ctl(pdu_tag rec_msg)
  116. {
  117. J1939_TP_TX_Message(0x00B300, 0xE1, PGN_00B000H_Ary, 20, 1);
  118. }
  119. uint8_t tx_data[8] = {0};
  120. uint8_t flag = 0;
  121. void can_rx_param(pdu_tag rec_msg)
  122. {
  123. tx_data[0] = rec_msg.data.u8_buf[0];
  124. tx_data[1] = rec_msg.data.u8_buf[1];
  125. tx_data[2] = rec_msg.data.u8_buf[2];
  126. tx_data[3] = rec_msg.data.u8_buf[3];
  127. if (tx_data[1] = 0xFF)
  128. {
  129. tx_flag = 1;
  130. }
  131. else
  132. {
  133. flag = 1;
  134. }
  135. }
  136. void can_rx_update(pdu_tag rec_msg)
  137. {
  138. if ((rec_msg.data.u8_buf[0] == 'A') && (rec_msg.data.u8_buf[1] == 'P') && (rec_msg.data.u8_buf[2] == 'P') && (rec_msg.data.u8_buf[3] == 'L') && (rec_msg.data.u8_buf[4] == 'O') && (rec_msg.data.u8_buf[5] == 'A') && (rec_msg.data.u8_buf[6] == 'D') && (rec_msg.data.u8_buf[7] == 0x01))
  139. {
  140. __set_FAULTMASK(1); // 关闭所有中断
  141. NVIC_SystemReset(); // 复位
  142. }
  143. }
  144. can_rx_tab can_tab[] = {
  145. {0x18011802, can_rx_param},
  146. {0x18011801, can_rx_ctl},
  147. {CAN_IAP, iap_rec_handler},
  148. {CAN_CALI, can_cali_msg_analysis}
  149. };
  150. void can_start_send(void)
  151. {
  152. pdu_tag tx_msg;
  153. QUEUE_STATUS result_status;
  154. if (0 == ((CAN_IER) & 0x1))
  155. {
  156. de_queue(&can_tx_queue, tx_msg, result_status);
  157. if (result_status == Q_OK) // 返回值为1代表读取成功
  158. {
  159. can_tx_frame(tx_msg);
  160. CAN_IT_TME_ENABLE();
  161. }
  162. }
  163. }
  164. static void can_rx_process(void)
  165. {
  166. uint8_t i;
  167. uint8_t flg = 0;
  168. pdu_tag rec_msg;
  169. QUEUE_STATUS result;
  170. for (uint8_t j = 0; j < 15; j++)
  171. {
  172. __disable_irq();
  173. de_queue(&can_rx_queue, rec_msg, result);
  174. __enable_irq();
  175. if (Q_OK == result)
  176. {
  177. for (i = 0; i < ARR_SIZE(can_tab); i++)
  178. {
  179. if (can_tab[i].id == rec_msg.id.r)
  180. {
  181. can_tab[i].p_func(rec_msg);
  182. flg = 1;
  183. break;
  184. }
  185. else if (can_tab[i].id == (uint32_t)(rec_msg.id.b.pf << 8 | rec_msg.id.b.ps))
  186. {
  187. can_tab[i].p_func(rec_msg);
  188. flg = 1;
  189. break;
  190. }
  191. else if (can_tab[i].id == rec_msg.id.b.pf)
  192. {
  193. can_tab[i].p_func(rec_msg);
  194. flg = 1;
  195. break;
  196. }
  197. }
  198. if (flg == 0)
  199. {
  200. j1939_msg_push_queue(&rec_msg);
  201. }
  202. flg = 0;
  203. }
  204. }
  205. }
  206. void can_tx_0x18010000_message(void)
  207. {
  208. uint8_t data[8] = {0};
  209. data[2] = 0xFF;
  210. push_can_message_to_queue(0x18010000, 8, data);
  211. }
  212. void can_tx_0x18010001_message(void)
  213. {
  214. uint8_t data[8] = {0};
  215. push_can_message_to_queue(0x18010001, 8, data);
  216. }
  217. void can_tx_0x18030001_message(void)
  218. {
  219. uint8_t data[8] = {0};
  220. data[1] = 8;
  221. push_can_message_to_queue(0x18030001, 8, data);
  222. }
  223. void can_tx_0x18010002_message(void)
  224. {
  225. uint8_t data[8] = {0};
  226. data[0] = 0x02;
  227. push_can_message_to_queue(0x18010002, 8, data);
  228. }
  229. void can_tx_0x18030003_message(void)
  230. {
  231. uint8_t data[8] = {0};
  232. data[0] = 0x02;
  233. push_can_message_to_queue(0x18030003, 8, data);
  234. }
  235. void can_tx_0x18010004_message(void)
  236. {
  237. uint8_t data[8] = {0};
  238. data[4] = 33;
  239. data[5] = 22;
  240. push_can_message_to_queue(0x18010004, 8, data);
  241. }
  242. void can_tx_0x18010005_message(void)
  243. {
  244. uint8_t data[8] = {0};
  245. data[4] = 33;
  246. data[5] = 22;
  247. push_can_message_to_queue(0x18010005, 8, data);
  248. }
  249. void can_tx_0x18010006_message(void)
  250. {
  251. uint8_t data[8] = {0};
  252. data[4] = 33;
  253. data[5] = 22;
  254. push_can_message_to_queue(0x18010006, 8, data);
  255. }
  256. void can_tx_0x18010007_message(void)
  257. {
  258. uint8_t data[8] = {0};
  259. data[4] = 33;
  260. data[5] = 22;
  261. push_can_message_to_queue(0x18010007, 8, data);
  262. }
  263. void can_tx_0x18010008_message(void)
  264. {
  265. uint8_t data[8] = {0};
  266. data[4] = 33;
  267. data[5] = 22;
  268. push_can_message_to_queue(0x18010008, 8, data);
  269. }
  270. void can_tx_0x18010009_message(void)
  271. {
  272. uint8_t data[8] = {0};
  273. data[4] = 33;
  274. data[5] = 22;
  275. push_can_message_to_queue(0x18010009, 8, data);
  276. }
  277. void can_tx_0x1801000F_message(void)
  278. {
  279. uint8_t data[8] = {0};
  280. data[0] = 1;
  281. data[1] = 5;
  282. data[2] = 3000 & 0xFF;
  283. data[3] = 3000 >> 8;
  284. data[4] = 3000 & 0xFF;
  285. data[5] = 3000 >> 8;
  286. push_can_message_to_queue(0x1801000F, 8, data);
  287. }
  288. void can_tx_0x18010010_message(void)
  289. {
  290. uint8_t data[8] = {0};
  291. data[0] = 1;
  292. data[1] = 1;
  293. data[4] = 0;
  294. data[5] = 0;
  295. push_can_message_to_queue(0x18010010, 8, data);
  296. }
  297. void can_tx_0x1801000A_message(void)
  298. {
  299. uint8_t data[8] = {0};
  300. data[4] = 33;
  301. data[5] = 22;
  302. push_can_message_to_queue(0x1801000A, 8, data);
  303. }
  304. void can_tx_0x1801000B_message(void)
  305. {
  306. uint8_t data[8] = {0};
  307. data[4] = 33;
  308. data[5] = 22;
  309. push_can_message_to_queue(0x1801000B, 8, data);
  310. }
  311. void can_tx_0x1801000C_message(void)
  312. {
  313. uint8_t data[8] = {0};
  314. data[4] = 33;
  315. data[5] = 22;
  316. push_can_message_to_queue(0x1801000C, 8, data);
  317. }
  318. void can_tx_0x1801000D_message(void)
  319. {
  320. uint8_t data[8] = {0};
  321. data[4] = 33;
  322. data[5] = 22;
  323. push_can_message_to_queue(0x1801000D, 8, data);
  324. }
  325. void can_tx_0x18010012_message(void)
  326. {
  327. uint8_t data[8] = {0};
  328. data[0] = 24;
  329. data[1] = 1;
  330. data[2] = 3;
  331. data[3] = 11;
  332. data[4] = 12;
  333. data[5] = 13;
  334. data[6] = 12;
  335. data[7] = 13;
  336. push_can_message_to_queue(0x18010012, 8, data);
  337. }
  338. void can_tx_0x18010017_message(void)
  339. {
  340. uint8_t data[8] = {0};
  341. data[0] = 24;
  342. data[1] = 1;
  343. data[0] = 3;
  344. data[1] = 11;
  345. data[0] = 12;
  346. data[1] = 13;
  347. push_can_message_to_queue(0x18010017, 8, data);
  348. }
  349. void can_tx_0x18010018_message(void)
  350. {
  351. uint8_t data[8] = {0};
  352. data[0] = 2;
  353. data[1] = 0xFF;
  354. push_can_message_to_queue(0x18010018, 8, data);
  355. }
  356. void can_tx_0x18010019_message(void)
  357. {
  358. uint8_t data[8] = {0};
  359. data[0] = 2;
  360. data[1] = 0xFF;
  361. push_can_message_to_queue(0x18010019, 8, data);
  362. }
  363. void can_tx_0x1801001A_message(void)
  364. {
  365. volatile static int i = 0;
  366. uint8_t data[8] = {0};
  367. data[0] = 2 + i;
  368. data[1] = 0x12 >> i;
  369. i++;
  370. if (i > 7)
  371. {
  372. i = 0;
  373. }
  374. push_can_message_to_queue(0x1801001A, 8, data);
  375. }
  376. void can_tx_0x1801001B_message(void)
  377. {
  378. uint8_t data[8] = {0};
  379. data[0] = 2;
  380. data[1] = 0xFF;
  381. push_can_message_to_queue(0x1801001B, 8, data);
  382. }
  383. void can_tx_0x1801001C_message(void)
  384. {
  385. uint8_t data[8] = {0};
  386. data[0] = 2;
  387. data[4] = 0xFF;
  388. push_can_message_to_queue(0x1801001C, 8, data);
  389. }
  390. void can_tx_0x1801001D_message(void)
  391. {
  392. uint8_t data[8] = {0};
  393. data[0] = 3;
  394. data[2] = 0xFF;
  395. push_can_message_to_queue(0x1801001D, 8, data);
  396. }
  397. void can_tx_0x18010023_message(void)
  398. {
  399. uint8_t send_data[8] = {0};
  400. data_bit_move(0, 8, 'A');
  401. data_bit_move(8, 8, 'A');
  402. data_bit_move(16, 8, 'A');
  403. data_bit_move(24, 8, 'C');
  404. data_bit_move(32, 8, 'A');
  405. data_bit_move(40, 8, '-');
  406. data_bit_move(48, 8, 'A');
  407. data_bit_move(56, 8, 'B');
  408. product_array(send_data);
  409. push_can_message_to_queue(0x18010023, 8, send_data);
  410. }
  411. void can_tx_0x18011802_message(void)
  412. {
  413. static uint8_t fdf = 0;
  414. if (flag == 1)
  415. {
  416. push_can_message_to_queue(0x18011802, 8, tx_data);
  417. flag = 0;
  418. }
  419. uint8_t data[8] = {0};
  420. uint16_t addr = 0xA000;
  421. if (tx_flag == 1)
  422. {
  423. data[0] = (addr + fdf) & 0xFF;
  424. data[1] = (addr + fdf) >> 8;
  425. data[2] = 0xFF;
  426. data[3] = 8;
  427. fdf++;
  428. if (fdf == 80)
  429. {
  430. tx_flag = 0;
  431. }
  432. push_can_message_to_queue(0x18011802, 8, data);
  433. }
  434. }
  435. void can_tx_0x18011803_message(void)
  436. {
  437. uint8_t data[8] = {0};
  438. uint16_t addr = 0xB000;
  439. static uint8_t fdf = 0;
  440. static int16_t a = -263;
  441. data[0] = (addr + fdf) & 0xFF;
  442. data[1] = (addr + fdf) >> 8;
  443. data[2] = a & 0xFF;
  444. data[3] = a >> 8;
  445. fdf++;
  446. a++;
  447. if (fdf > 6)
  448. {
  449. fdf = 0;
  450. }
  451. push_can_message_to_queue(0x18011803, 8, data);
  452. }
  453. void can_tx_0x18011803_2_message(void)
  454. {
  455. uint8_t data[8] = {0};
  456. static uint8_t fdf_2 = 0;
  457. uint16_t addr_vol = 0xB110;
  458. data[0] = (addr_vol + fdf_2) & 0xFF;
  459. data[1] = (addr_vol + fdf_2) >> 8;
  460. data[2] = 1;
  461. data[3] = -2 & 0xFF;
  462. data[4] = -2 >> 8;
  463. fdf_2++;
  464. if (fdf_2 > 80)
  465. {
  466. fdf_2 = 0;
  467. }
  468. push_can_message_to_queue(0x18011803, 8, data);
  469. }
  470. void can_tx_0x18180003_message(void)
  471. {
  472. uint8_t data[8] = {0};
  473. data_bit_move(0, 16, 0);
  474. data_bit_move(16, 16, 0x2710);
  475. data_bit_move(32, 16, 0x4E20);
  476. data_bit_move(48, 16, 0x7530);
  477. product_array(data);
  478. push_can_message_to_queue(0x18190006, 8, data);
  479. }
  480. void can_tx_0x18190007_message(void)
  481. {
  482. uint8_t data[8] = {0};
  483. data_bit_move(0, 16, 0xDFFF);
  484. data_bit_move(16, 16, 0xF000);
  485. data_bit_move(32, 16, 0xE800);
  486. data_bit_move(48, 16, 0xE13D);
  487. product_array(data);
  488. push_can_message_to_queue(0x18190005, 8, data);
  489. }
  490. void can_tx_0x18190009_message(void)
  491. {
  492. uint8_t data[8] = {0};
  493. data_bit_move(0, 16, 0xDFFF);
  494. data_bit_move(16, 16, 0xF000);
  495. data_bit_move(32, 16, 0xE800);
  496. data_bit_move(48, 16, 0xE13D);
  497. product_array(data);
  498. push_can_message_to_queue(0x18190005, 0, data);
  499. }
  500. void send_message(void)
  501. {
  502. static volatile uint8_t times = 0;
  503. if (times >= 100)
  504. {
  505. times = 0;
  506. }
  507. if (times % 100 == 0)
  508. {
  509. can_tx_0x18010001_message();
  510. can_tx_0x18010002_message();
  511. can_tx_0x18010004_message();
  512. can_tx_0x18010005_message();
  513. }
  514. else if (times % 100 == 5)
  515. {
  516. can_tx_0x18010006_message();
  517. can_tx_0x18010007_message();
  518. can_tx_0x18010008_message();
  519. can_tx_0x18010009_message();
  520. can_tx_0x1801000A_message();
  521. }
  522. else if (times % 100 == 10)
  523. {
  524. can_tx_0x1801000B_message();
  525. can_tx_0x1801000C_message();
  526. can_tx_0x1801000D_message();
  527. can_tx_0x18010023_message();
  528. can_tx_0x18010010_message();
  529. }
  530. else if (times % 100 == 15)
  531. {
  532. can_tx_0x18010018_message();
  533. can_tx_0x18010019_message();
  534. can_tx_0x1801001A_message();
  535. can_tx_0x1801001B_message();
  536. can_tx_0x1801001C_message();
  537. }
  538. else if (times % 100 == 20)
  539. {
  540. can_tx_0x1801001D_message();
  541. can_tx_0x1801000F_message();
  542. can_tx_0x18030003_message();
  543. can_tx_0x18030001_message();
  544. can_tx_0x18010000_message();
  545. }
  546. else if (times % 100 == 25)
  547. {
  548. can_tx_0x18010017_message();
  549. can_tx_0x18010012_message();
  550. can_tx_0x18011802_message();
  551. can_tx_0x18011803_message();
  552. can_tx_0x18011803_2_message();
  553. can_tx_0x18180003_message();
  554. can_tx_0x18190007_message();
  555. }
  556. else
  557. {
  558. // return;
  559. }
  560. times++;
  561. }
  562. void can_network_init(void)
  563. {
  564. queue_init(&can_rx_queue);
  565. queue_init(&can_tx_queue);
  566. can_rx_back_init(can_rx_callback);
  567. can_tx_back_init(can_tx_callback);
  568. }
  569. void can_process(void)
  570. {
  571. can_rx_process();
  572. send_message();
  573. can_start_send();
  574. }