dev_can.c 16 KB

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