dev_can.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #include "dev_can.h"
  2. #include "dev_iap.h"
  3. #include "hal_can.h"
  4. #include "hal_conf.h"
  5. #include "queue.h"
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. double pow_branch(double x, long long N)
  10. {
  11. double ans = 1.0;
  12. // 贡献的初始值为 x
  13. double x_contribute = x;
  14. // 在对 N 进行二进制拆分的同时计算答案
  15. while (N > 0)
  16. {
  17. if (N % 2 == 1)
  18. {
  19. // 如果 N 二进制表示的最低位为 1,那么需要计入贡献
  20. ans *= x_contribute;
  21. }
  22. // 将贡献不断地平方
  23. x_contribute *= x_contribute;
  24. // 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
  25. N = N >> 1;
  26. }
  27. return ans;
  28. }
  29. double pow(double x, double N)
  30. {
  31. return N >= 0 ? pow_branch(x, N) : 1.0 / pow_branch(x, -N);
  32. }
  33. uint8_t recv_can_id = 0;
  34. static uint64_t base_data;
  35. void data_bit_move(uint8_t start_bit, uint8_t bit_len, uint64_t data)
  36. {
  37. uint64_t mask = 0;
  38. uint64_t source_data = (uint64_t)data;
  39. mask = pow(2, bit_len) - 1;
  40. base_data |= (mask & source_data) << start_bit;
  41. }
  42. void product_array(uint8_t send_array[8])
  43. {
  44. send_array[0] = (uint8_t)base_data;
  45. send_array[1] = (uint8_t)(base_data >> 8);
  46. send_array[2] = (uint8_t)(base_data >> 16);
  47. send_array[3] = (uint8_t)(base_data >> 24);
  48. send_array[4] = (uint8_t)(base_data >> 32);
  49. send_array[5] = (uint8_t)(base_data >> 40);
  50. send_array[6] = (uint8_t)(base_data >> 48);
  51. send_array[7] = (uint8_t)(base_data >> 56);
  52. base_data = 0;
  53. }
  54. uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
  55. {
  56. pdu_tag response_msg;
  57. response_msg.id.r = id;
  58. response_msg.reg.dlc = len;
  59. memcpy(&response_msg.data.u8_buf[0], p_data, len);
  60. if (id > 0x7FF)
  61. {
  62. response_msg.reg.ide = CAN_Id_Extended;
  63. }
  64. else
  65. {
  66. response_msg.reg.ide = CAN_Id_Standard;
  67. }
  68. #if 1
  69. if (en_queue(&can_tx_queue, response_msg) != Q_OK)
  70. {
  71. return 0;
  72. }
  73. #else
  74. en_queue(can_tx, &response_msg, 1);
  75. #endif
  76. return 1;
  77. }
  78. static uint8_t can_tx_frame(pdu_tag can_message)
  79. {
  80. uint8_t result = CAN_TxStatus_NoMailBox;
  81. result = hal_can_msg_tx(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
  82. return result;
  83. }
  84. // void can_tx_stop(CAN_TypeDef *can_perpiph, u8 mailbox_number)
  85. // {
  86. // can_tx_mail_box_stop(can_perpiph, mailbox_number);
  87. // }
  88. // QUEUE_STATUS can_tx_process(p_can_queue_tag p_queue)
  89. // {
  90. // u8 mailbox_number = 0;
  91. // u8 query_result = CAN_TxStatus_NoMailBox;
  92. // if (queue_empty(p_queue))
  93. // return Q_OK;
  94. // mailbox_number = can_tx_frame(p_queue->can_message[p_queue->tail]);
  95. // if (mailbox_number == CAN_TxStatus_NoMailBox)
  96. // return Q_OK;
  97. // query_result = can_tx_mail_box_state(CAN1, mailbox_number);
  98. // if (query_result == CAN_TxStatus_Ok || query_result == CAN_TxStatus_Pending)
  99. // {
  100. // p_queue->tail = (p_queue->tail + 1) % MAX_QSIZE;
  101. // p_queue->count--;
  102. // return Q_OK;
  103. // }
  104. // return Q_ERR;
  105. // }
  106. void can_tx_callback(void)
  107. {
  108. pdu_tag tx_data;
  109. // de_queue(can_tx, &tx_data);
  110. if (de_queue(&can_tx_queue, &tx_data) == Q_OK) // 返回值为1代表读取成功
  111. {
  112. can_tx_frame(tx_data);
  113. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  114. }
  115. else
  116. {
  117. CAN_ITConfig(CAN1, CAN_IT_TME, DISABLE);
  118. }
  119. }
  120. void can_rx_callback(CanRxMsg rx_message)
  121. {
  122. pdu_tag data;
  123. uint8_t ps;
  124. uint8_t pf;
  125. switch (rx_message.ExtId)
  126. {
  127. case 0x18011801:
  128. data.id.r = 0x18011801;
  129. data.reg.dlc = rx_message.DLC;
  130. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  131. en_queue(&can_rx_queue, data);
  132. case 0x18DFF4E1:
  133. data.id.r = 0x18DFF4E1;
  134. data.reg.dlc = rx_message.DLC;
  135. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  136. en_queue(&can_rx_queue, data);
  137. case 0x18011802:
  138. data.id.r = 0x18011802;
  139. data.reg.dlc = rx_message.DLC;
  140. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  141. #if 1
  142. en_queue(&can_rx_queue, data);
  143. #else
  144. en_queue(can_rx, &data, 1);
  145. #endif
  146. break;
  147. default:
  148. pf = (rx_message.ExtId & CAN_PGN_PF) >> 16;
  149. if (pf >= 0xF0)
  150. {
  151. data.id.r = rx_message.ExtId;
  152. recv_can_id = data.id.b.sa;
  153. memcpy(&data.data.u8_buf[0], rx_message.Data, rx_message.DLC);
  154. #if 1
  155. en_queue(&can_rx_queue, data);
  156. #else
  157. en_queue(can_rx, &data, 1);
  158. #endif
  159. }
  160. else
  161. {
  162. // ps = (rx_message.ExtId & CAN_PGN_PS) >> 8;
  163. // if (ps == 0xF4)
  164. // {
  165. // }
  166. #if 1
  167. en_queue(&can_rx_queue, data);
  168. #else
  169. en_queue(can_rx, &data, 1);
  170. #endif
  171. }
  172. }
  173. }
  174. uint8_t tx_flag = 0;
  175. void can_rx_ctl(pdu_tag rec_msg)
  176. {
  177. // rec_msg.data.u8_buf[0];
  178. // LED2 = !LED2;
  179. tx_flag = 1;
  180. }
  181. uint8_t tx_data[8] = {0};
  182. uint8_t flag = 0;
  183. void can_rx_param(pdu_tag rec_msg)
  184. {
  185. // LED2 = !LED2;
  186. tx_data[0] = rec_msg.data.u8_buf[0];
  187. tx_data[1] = rec_msg.data.u8_buf[1];
  188. tx_data[2] = rec_msg.data.u8_buf[2];
  189. tx_data[3] = rec_msg.data.u8_buf[3];
  190. if (tx_data[1] = 0xFF)
  191. {
  192. tx_flag = 1;
  193. }
  194. else
  195. {
  196. flag = 1;
  197. }
  198. // push_can_message_to_queue(rec_msg.id.r, rec_msg.reg.dlc, rec_msg.data.u8_buf);
  199. }
  200. void can_rx_update(pdu_tag rec_msg)
  201. {
  202. 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))
  203. {
  204. __set_FAULTMASK(1); // 关闭所有中断
  205. NVIC_SystemReset(); // 复位
  206. }
  207. }
  208. can_rx_tab can_tab[] = {
  209. 0x18011802,
  210. can_rx_param,
  211. 0x18011801,
  212. can_rx_ctl,
  213. 0x18DFF4E1,
  214. iap_rec_handler,
  215. };
  216. void can_start_send(void)
  217. {
  218. pdu_tag tx_msg;
  219. if (RESET == CAN_GetITStatus(CAN1, CAN_IT_TME))
  220. {
  221. #if 0
  222. de_queue(can_tx, &tx_msg);
  223. if (can_tx.ret == 1) // 返回值为1代表读取成功
  224. {
  225. can_tx_frame(tx_msg);
  226. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  227. }
  228. #else
  229. if (de_queue(&can_tx_queue, &tx_msg) == Q_OK) // 返回值为1代表读取成功
  230. {
  231. can_tx_frame(tx_msg);
  232. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  233. }
  234. #endif
  235. }
  236. }
  237. static uint8_t can_rx_process(void)
  238. {
  239. uint8_t i;
  240. uint8_t flg = 0;
  241. pdu_tag rec_msg;
  242. for (uint8_t j = 0; j < 15; j++)
  243. {
  244. #if 1
  245. if (Q_OK == de_queue(&can_rx_queue, &rec_msg))
  246. {
  247. for (i = 0; i < ARR_SIZE(can_tab); i++)
  248. {
  249. if (can_tab[i].id == rec_msg.id.r)
  250. {
  251. can_tab[i].p_func(rec_msg);
  252. break;
  253. }
  254. }
  255. }
  256. #else
  257. de_queue(can_rx, &rec_msg);
  258. if (can_rx.ret == 1) // 返回值为1代表读取成功
  259. {
  260. for (i = 0; i < ARR_SIZE(can_tab); i++)
  261. {
  262. if (can_tab[i].id == rec_msg.id.r)
  263. {
  264. can_tab[i].p_func(rec_msg);
  265. break;
  266. }
  267. }
  268. }
  269. #endif
  270. }
  271. }
  272. void can_tx_0x18010000_message(void)
  273. {
  274. uint8_t data[8] = {0};
  275. data[2] = 0xFF;
  276. push_can_message_to_queue(0x18010000, 8, data);
  277. }
  278. void can_tx_0x18010001_message(void)
  279. {
  280. uint8_t data[8] = {0};
  281. push_can_message_to_queue(0x18010001, 8, data);
  282. }
  283. void can_tx_0x18030001_message(void)
  284. {
  285. uint8_t data[8] = {0};
  286. data[1] = 8;
  287. push_can_message_to_queue(0x18030001, 8, data);
  288. }
  289. void can_tx_0x18010002_message(void)
  290. {
  291. uint8_t data[8] = {0};
  292. data[0] = 0x02;
  293. push_can_message_to_queue(0x18010002, 8, data);
  294. }
  295. void can_tx_0x18030003_message(void)
  296. {
  297. uint8_t data[8] = {0};
  298. data[0] = 0x02;
  299. push_can_message_to_queue(0x18030003, 8, data);
  300. }
  301. void can_tx_0x18010004_message(void)
  302. {
  303. uint8_t data[8] = {0};
  304. data[4] = 33;
  305. data[5] = 22;
  306. push_can_message_to_queue(0x18010004, 8, data);
  307. }
  308. void can_tx_0x18010005_message(void)
  309. {
  310. uint8_t data[8] = {0};
  311. data[4] = 33;
  312. data[5] = 22;
  313. push_can_message_to_queue(0x18010005, 8, data);
  314. }
  315. void can_tx_0x18010006_message(void)
  316. {
  317. uint8_t data[8] = {0};
  318. data[4] = 33;
  319. data[5] = 22;
  320. push_can_message_to_queue(0x18010006, 8, data);
  321. }
  322. void can_tx_0x18010007_message(void)
  323. {
  324. uint8_t data[8] = {0};
  325. data[4] = 33;
  326. data[5] = 22;
  327. push_can_message_to_queue(0x18010007, 8, data);
  328. }
  329. void can_tx_0x18010008_message(void)
  330. {
  331. uint8_t data[8] = {0};
  332. data[4] = 33;
  333. data[5] = 22;
  334. push_can_message_to_queue(0x18010008, 8, data);
  335. }
  336. void can_tx_0x18010009_message(void)
  337. {
  338. uint8_t data[8] = {0};
  339. data[4] = 33;
  340. data[5] = 22;
  341. push_can_message_to_queue(0x18010009, 8, data);
  342. }
  343. void can_tx_0x1801000F_message(void)
  344. {
  345. uint8_t data[8] = {0};
  346. data[0] = 1;
  347. data[1] = 5;
  348. data[2] = 3000 & 0xFF;
  349. data[3] = 3000 >> 8;
  350. data[4] = 3000 & 0xFF;
  351. data[5] = 3000 >> 8;
  352. push_can_message_to_queue(0x1801000F, 8, data);
  353. }
  354. void can_tx_0x18010010_message(void)
  355. {
  356. uint8_t data[8] = {0};
  357. // data_bit_move(0, 8, 1);
  358. // data_bit_move(8, 8, 1);
  359. // data_bit_move(32, 8, 0);
  360. // data_bit_move(40, 8, 0);
  361. // product_array(data);
  362. data[0] = 1;
  363. data[1] = 1;
  364. data[4] = 0;
  365. data[5] = 0;
  366. push_can_message_to_queue(0x18010010, 8, data);
  367. data[1] = 5;
  368. push_can_message_to_queue(0x18010010, 8, data);
  369. data[1] = 10;
  370. push_can_message_to_queue(0x18010010, 8, data);
  371. }
  372. void can_tx_0x1801000A_message(void)
  373. {
  374. uint8_t data[8] = {0};
  375. data[4] = 33;
  376. data[5] = 22;
  377. push_can_message_to_queue(0x1801000A, 8, data);
  378. }
  379. void can_tx_0x1801000B_message(void)
  380. {
  381. uint8_t data[8] = {0};
  382. data[4] = 33;
  383. data[5] = 22;
  384. push_can_message_to_queue(0x1801000B, 8, data);
  385. }
  386. void can_tx_0x1801000C_message(void)
  387. {
  388. uint8_t data[8] = {0};
  389. data[4] = 33;
  390. data[5] = 22;
  391. push_can_message_to_queue(0x1801000C, 8, data);
  392. }
  393. void can_tx_0x1801000D_message(void)
  394. {
  395. uint8_t data[8] = {0};
  396. data[4] = 33;
  397. data[5] = 22;
  398. push_can_message_to_queue(0x1801000D, 8, data);
  399. }
  400. void can_tx_0x18010012_message(void)
  401. {
  402. uint8_t data[8] = {0};
  403. data[0] = 24;
  404. data[1] = 1;
  405. data[2] = 3;
  406. data[3] = 11;
  407. data[4] = 12;
  408. data[5] = 13;
  409. data[6] = 12;
  410. data[7] = 13;
  411. push_can_message_to_queue(0x18010012, 8, data);
  412. }
  413. void can_tx_0x18010017_message(void)
  414. {
  415. uint8_t data[8] = {0};
  416. data[0] = 24;
  417. data[1] = 1;
  418. data[0] = 3;
  419. data[1] = 11;
  420. data[0] = 12;
  421. data[1] = 13;
  422. push_can_message_to_queue(0x18010017, 8, data);
  423. }
  424. void can_tx_0x18010018_message(void)
  425. {
  426. uint8_t data[8] = {0};
  427. data[0] = 2;
  428. data[1] = 0xFF;
  429. push_can_message_to_queue(0x18010018, 8, data);
  430. }
  431. void can_tx_0x18010019_message(void)
  432. {
  433. uint8_t data[8] = {0};
  434. data[0] = 2;
  435. data[1] = 0xFF;
  436. push_can_message_to_queue(0x18010019, 8, data);
  437. }
  438. void can_tx_0x1801001A_message(void)
  439. {
  440. volatile static int i = 0;
  441. uint8_t data[8] = {0};
  442. data[0] = 2 + i;
  443. data[1] = 0x12 >> i;
  444. i++;
  445. if (i > 7)
  446. {
  447. i = 0;
  448. }
  449. push_can_message_to_queue(0x1801001A, 8, data);
  450. }
  451. void can_tx_0x1801001B_message(void)
  452. {
  453. uint8_t data[8] = {0};
  454. data[0] = 2;
  455. data[1] = 0xFF;
  456. push_can_message_to_queue(0x1801001B, 8, data);
  457. }
  458. void can_tx_0x1801001C_message(void)
  459. {
  460. uint8_t data[8] = {0};
  461. data[0] = 2;
  462. data[4] = 0xFF;
  463. push_can_message_to_queue(0x1801001C, 8, data);
  464. }
  465. void can_tx_0x1801001D_message(void)
  466. {
  467. uint8_t data[8] = {0};
  468. data[0] = 3;
  469. data[2] = 0xFF;
  470. push_can_message_to_queue(0x1801001D, 8, data);
  471. }
  472. void can_tx_0x18010023_message(void)
  473. {
  474. uint8_t send_data[8] = {0};
  475. // data[0] = 'A';
  476. // data[1] = 'A';
  477. // data[2] = 'A';
  478. // data[3] = 'A';
  479. // data[4] = 'A';
  480. // data[5] = 'A';
  481. // data[6] = 'A';
  482. // data[7] = 'A';
  483. data_bit_move(0, 8, 'A');
  484. data_bit_move(8, 8, 'A');
  485. data_bit_move(16, 8, 'A');
  486. data_bit_move(24, 8, 'C');
  487. data_bit_move(32, 8, 'A');
  488. data_bit_move(40, 8, '-');
  489. data_bit_move(48, 8, 'A');
  490. data_bit_move(56, 8, 'B');
  491. product_array(send_data);
  492. push_can_message_to_queue(0x18010023, 8, send_data);
  493. }
  494. void can_tx_0x18011802_message(void)
  495. {
  496. static uint8_t fdf = 0;
  497. if (flag == 1)
  498. {
  499. push_can_message_to_queue(0x18011802, 8, tx_data);
  500. flag = 0;
  501. }
  502. uint8_t data[8] = {0};
  503. uint16_t addr = 0xA000;
  504. if (tx_flag == 1)
  505. {
  506. data[0] = (addr + fdf) & 0xFF;
  507. data[1] = (addr + fdf) >> 8;
  508. data[2] = rand() & 0xFF;
  509. data[3] = rand() >> 8;
  510. fdf++;
  511. if (fdf == 80)
  512. {
  513. tx_flag = 0;
  514. }
  515. push_can_message_to_queue(0x18011802, 8, data);
  516. }
  517. }
  518. void can_tx_0x18011803_message(void)
  519. {
  520. uint8_t data[8] = {0};
  521. uint16_t addr = 0xB000;
  522. static uint8_t fdf = 0;
  523. static int16_t a = -263;
  524. data[0] = (addr + fdf) & 0xFF;
  525. data[1] = (addr + fdf) >> 8;
  526. data[2] = a & 0xFF;
  527. data[3] = a >> 8;
  528. fdf++;
  529. a++;
  530. if (fdf > 6)
  531. {
  532. fdf = 0;
  533. }
  534. push_can_message_to_queue(0x18011803, 8, data);
  535. }
  536. void can_tx_0x18011803_2_message(void)
  537. {
  538. uint8_t data[8] = {0};
  539. static uint8_t fdf_2 = 0;
  540. uint16_t addr_vol = 0xB110;
  541. data[0] = (addr_vol + fdf_2) & 0xFF;
  542. data[1] = (addr_vol + fdf_2) >> 8;
  543. data[2] = 1;
  544. data[3] = -2 & 0xFF;
  545. data[4] = -2 >> 8;
  546. fdf_2++;
  547. if (fdf_2 > 80)
  548. {
  549. fdf_2 = 0;
  550. }
  551. push_can_message_to_queue(0x18011803, 8, data);
  552. }
  553. void send_message(void)
  554. {
  555. static volatile uint8_t times = 0;
  556. times++;
  557. if (times % 100 == 1)
  558. {
  559. can_tx_0x18010001_message();
  560. can_tx_0x18010002_message();
  561. can_tx_0x18010004_message();
  562. can_tx_0x18010005_message();
  563. }
  564. else if (times % 100 == 5)
  565. {
  566. can_tx_0x18010006_message();
  567. can_tx_0x18010007_message();
  568. can_tx_0x18010008_message();
  569. can_tx_0x18010009_message();
  570. can_tx_0x1801000A_message();
  571. }
  572. else if (times % 100 == 10)
  573. {
  574. can_tx_0x1801000B_message();
  575. can_tx_0x1801000C_message();
  576. can_tx_0x1801000D_message();
  577. can_tx_0x18010023_message();
  578. can_tx_0x18010010_message();
  579. }
  580. else if (times % 100 == 15)
  581. {
  582. can_tx_0x18010018_message();
  583. can_tx_0x18010019_message();
  584. can_tx_0x1801001A_message();
  585. can_tx_0x1801001B_message();
  586. can_tx_0x1801001C_message();
  587. }
  588. else if (times % 100 == 20)
  589. {
  590. can_tx_0x1801001D_message();
  591. can_tx_0x1801000F_message();
  592. can_tx_0x18030003_message();
  593. can_tx_0x18030001_message();
  594. can_tx_0x18010000_message();
  595. }
  596. else if (times % 100 == 25)
  597. {
  598. can_tx_0x18010017_message();
  599. can_tx_0x18010012_message();
  600. can_tx_0x18011802_message();
  601. can_tx_0x18011803_message();
  602. can_tx_0x18011803_2_message();
  603. }
  604. else
  605. {
  606. return;
  607. }
  608. }
  609. void dev_can_network_init(void)
  610. {
  611. hal_can_init();
  612. hal_can_rx_back_init(can_rx_callback);
  613. hal_can_tx_back_init(can_tx_callback);
  614. }
  615. void can_process(void)
  616. {
  617. can_rx_process();
  618. send_message();
  619. can_start_send();
  620. }