can_task.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "can_task.h"
  2. #include "can_interface.h"
  3. #include "hal_math.h"
  4. #include "iap.h"
  5. #include "queue.h"
  6. #include "utils.h"
  7. #include <stdint.h>
  8. #include <string.h>
  9. queue_entry(pdu_tag, 40) can_rx_queue;
  10. queue_entry(pdu_tag, 40) can_tx_queue;
  11. uint8_t recv_can_id;
  12. static uint64_t base_data;
  13. void data_bit_move(uint8_t start_bit, uint8_t bit_len, uint64_t data)
  14. {
  15. uint64_t mask = 0;
  16. uint64_t source_data = (uint64_t)data;
  17. mask = hal_pow(2, bit_len) - 1;
  18. base_data |= (mask & source_data) << start_bit;
  19. }
  20. void product_array(uint8_t send_array[8])
  21. {
  22. send_array[0] = (uint8_t)base_data;
  23. send_array[1] = (uint8_t)(base_data >> 8);
  24. send_array[2] = (uint8_t)(base_data >> 16);
  25. send_array[3] = (uint8_t)(base_data >> 24);
  26. send_array[4] = (uint8_t)(base_data >> 32);
  27. send_array[5] = (uint8_t)(base_data >> 40);
  28. send_array[6] = (uint8_t)(base_data >> 48);
  29. send_array[7] = (uint8_t)(base_data >> 56);
  30. base_data = 0;
  31. }
  32. uint8_t push_can_message_to_queue(uint32_t id, uint8_t len, uint8_t *p_data)
  33. {
  34. pdu_tag response_msg;
  35. QUEUE_STATUS result_status;
  36. response_msg.id.r = id;
  37. response_msg.reg.dlc = len;
  38. memcpy(&response_msg.data.u8_buf[0], p_data, len);
  39. if (id > 0x7FF)
  40. {
  41. response_msg.reg.ide = CAN_ID_EXT;
  42. }
  43. else
  44. {
  45. response_msg.reg.ide = CAN_ID_STD;
  46. }
  47. __disable_irq();
  48. en_queue(&can_tx_queue, response_msg, result_status);
  49. __enable_irq();
  50. if (result_status != Q_OK)
  51. {
  52. return 0;
  53. }
  54. return 1;
  55. }
  56. static uint8_t can_tx_frame(pdu_tag can_message)
  57. {
  58. can_write(can_message.id.r, can_message.reg.ide, can_message.data.u8_buf, can_message.reg.dlc);
  59. return 1;
  60. }
  61. void can_tx_callback(void)
  62. {
  63. pdu_tag tx_data;
  64. QUEUE_STATUS result_status;
  65. de_queue(&can_tx_queue, tx_data, result_status);
  66. // 返回值为1代表读取成功
  67. if (result_status == Q_OK)
  68. {
  69. can_tx_frame(tx_data);
  70. CAN_IT_TME_ENABLE();
  71. }
  72. else
  73. {
  74. CAN_IT_TME_DISABLE();
  75. }
  76. }
  77. void can_rx_callback(CAN_RxHeaderTypeDef rx_header, uint8_t *buff)
  78. {
  79. pdu_tag data;
  80. uint8_t ps;
  81. uint8_t pf;
  82. QUEUE_STATUS result;
  83. switch (rx_header.ExtId)
  84. {
  85. case 0x18011801:
  86. data.id.r = 0x18011801;
  87. data.reg.dlc = rx_header.DLC;
  88. memcpy(&data.data.u8_buf[0], buff, rx_header.DLC);
  89. en_queue(&can_rx_queue, data, result);
  90. break;
  91. default:
  92. pf = (rx_header.ExtId & CAN_PGN_PF) >> 16;
  93. if (pf >= 0xF0)
  94. {
  95. data.id.r = rx_header.ExtId;
  96. recv_can_id = data.id.b.sa;
  97. memcpy(&data.data.u8_buf[0], buff, rx_header.DLC);
  98. en_queue(&can_rx_queue, data, result);
  99. }
  100. else
  101. {
  102. ps = (rx_header.ExtId & CAN_PGN_PS) >> 8;
  103. if (ps == 0xF4)
  104. {
  105. data.id.r = rx_header.ExtId;
  106. recv_can_id = data.id.b.sa;
  107. data.reg.dlc = rx_header.DLC;
  108. memcpy(&data.data.u8_buf[0], buff, rx_header.DLC);
  109. if (data.id.b.pf == 0xDF)
  110. {
  111. en_queue(&can_rx_queue, data, result);
  112. }
  113. else
  114. {
  115. en_queue(&can_rx_queue, data, result);
  116. }
  117. }
  118. else
  119. {
  120. memcpy(&data.data.u8_buf[0], buff, rx_header.DLC);
  121. en_queue(&can_rx_queue, data, result);
  122. if (result == 1)
  123. {
  124. return;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. uint8_t tx_flag = 0;
  131. void can_rx_update(pdu_tag rec_msg)
  132. {
  133. if ((rec_msg.data.u8_buf[0] == 'A') && (rec_msg.data.u8_buf[1] == 'P') &&
  134. (rec_msg.data.u8_buf[2] == 'P') && (rec_msg.data.u8_buf[3] == 'L') &&
  135. (rec_msg.data.u8_buf[4] == 'O') && (rec_msg.data.u8_buf[5] == 'A') &&
  136. (rec_msg.data.u8_buf[6] == 'D') && (rec_msg.data.u8_buf[7] == 0x01))
  137. {
  138. __set_FAULTMASK(1); // 关闭所有中断
  139. NVIC_SystemReset(); // 复位
  140. }
  141. }
  142. can_rx_tab can_tab[] = {{0x18DFF4E1, iap_rec_handler}};
  143. void can_start_send(void)
  144. {
  145. pdu_tag tx_msg;
  146. QUEUE_STATUS result_status;
  147. if (0 == ((CAN_IER) & 0x1))
  148. {
  149. de_queue(&can_tx_queue, tx_msg, result_status);
  150. if (result_status == Q_OK) // 返回值为1代表读取成功
  151. {
  152. can_tx_frame(tx_msg);
  153. CAN_IT_TME_ENABLE();
  154. }
  155. }
  156. }
  157. static void can_rx_process(void)
  158. {
  159. uint8_t i;
  160. uint8_t flg = 0;
  161. pdu_tag rec_msg;
  162. QUEUE_STATUS result;
  163. for (uint8_t j = 0; j < 15; j++)
  164. {
  165. __disable_irq();
  166. de_queue(&can_rx_queue, rec_msg, result);
  167. __enable_irq();
  168. if (Q_OK == result)
  169. {
  170. for (i = 0; i < ARR_SIZE(can_tab); i++)
  171. {
  172. if (can_tab[i].id == rec_msg.id.r)
  173. {
  174. can_tab[i].p_func(rec_msg);
  175. flg = 1;
  176. break;
  177. }
  178. else if (can_tab[i].id == (uint32_t)(rec_msg.id.b.pf << 8 | rec_msg.id.b.ps))
  179. {
  180. can_tab[i].p_func(rec_msg);
  181. flg = 1;
  182. break;
  183. }
  184. else if (can_tab[i].id == rec_msg.id.b.pf)
  185. {
  186. can_tab[i].p_func(rec_msg);
  187. flg = 1;
  188. break;
  189. }
  190. }
  191. if (flg == 0)
  192. {
  193. // j1939_msg_push_queue(&rec_msg);
  194. }
  195. flg = 0;
  196. }
  197. }
  198. }
  199. void can_tx_0x18010023_message(void)
  200. {
  201. uint8_t send_data[8] = {0};
  202. data_bit_move(0, 8, 'A');
  203. data_bit_move(8, 8, 'A');
  204. data_bit_move(16, 8, 'A');
  205. data_bit_move(24, 8, 'C');
  206. data_bit_move(32, 8, 'A');
  207. data_bit_move(40, 8, '-');
  208. data_bit_move(48, 8, 'A');
  209. data_bit_move(56, 8, 'B');
  210. product_array(send_data);
  211. push_can_message_to_queue(0x18010023, 8, send_data);
  212. }
  213. void can_tx_0x18180003_message(void)
  214. {
  215. uint8_t data[8] = {0};
  216. data_bit_move(0, 16, 0);
  217. data_bit_move(16, 16, 0x2710);
  218. data_bit_move(32, 16, 0x4E20);
  219. data_bit_move(48, 16, 0x7530);
  220. product_array(data);
  221. push_can_message_to_queue(0x18190006, 8, data);
  222. }
  223. void can_tx_0x18190007_message(void)
  224. {
  225. uint8_t data[8] = {0};
  226. data_bit_move(0, 16, 0xDFFF);
  227. data_bit_move(16, 16, 0xF000);
  228. data_bit_move(32, 16, 0xE800);
  229. data_bit_move(48, 16, 0xE13D);
  230. product_array(data);
  231. push_can_message_to_queue(0x18190005, 8, data);
  232. }
  233. void can_tx_0x18190009_message(void)
  234. {
  235. uint8_t data[8] = {0};
  236. data_bit_move(0, 16, 0xDFFF);
  237. data_bit_move(16, 16, 0xF000);
  238. data_bit_move(32, 16, 0xE800);
  239. data_bit_move(48, 16, 0xE13D);
  240. product_array(data);
  241. push_can_message_to_queue(0x18190005, 0, data);
  242. }
  243. void send_message(void)
  244. {
  245. static volatile uint8_t times = 0;
  246. if (times >= 100)
  247. {
  248. times = 0;
  249. }
  250. if (times % 100 == 0)
  251. {
  252. // can_tx_0x18010001_message();
  253. // can_tx_0x18010002_message();
  254. // can_tx_0x18010004_message();
  255. // can_tx_0x18010005_message();
  256. }
  257. else if (times % 100 == 5)
  258. {
  259. // can_tx_0x18010006_message();
  260. // can_tx_0x18010007_message();
  261. // can_tx_0x18010008_message();
  262. // can_tx_0x18010009_message();
  263. // can_tx_0x1801000A_message();
  264. }
  265. else if (times % 100 == 10)
  266. {
  267. // can_tx_0x1801000B_message();
  268. // can_tx_0x1801000C_message();
  269. // can_tx_0x1801000D_message();
  270. // can_tx_0x18010023_message();
  271. // can_tx_0x18010010_message();
  272. }
  273. else if (times % 100 == 15)
  274. {
  275. // can_tx_0x18010018_message();
  276. // can_tx_0x18010019_message();
  277. // can_tx_0x1801001A_message();
  278. // can_tx_0x1801001B_message();
  279. // can_tx_0x1801001C_message();
  280. }
  281. else if (times % 100 == 20)
  282. {
  283. // can_tx_0x1801001D_message();
  284. // can_tx_0x1801000F_message();
  285. // can_tx_0x18030003_message();
  286. // can_tx_0x18030001_message();
  287. // can_tx_0x18010000_message();
  288. }
  289. else if (times % 100 == 25)
  290. {
  291. // can_tx_0x18010017_message();
  292. // can_tx_0x18010012_message();
  293. // can_tx_0x18011802_message();
  294. // can_tx_0x18011803_message();
  295. // can_tx_0x18011803_2_message();
  296. can_tx_0x18180003_message();
  297. can_tx_0x18190007_message();
  298. }
  299. else
  300. {
  301. // return;
  302. }
  303. times++;
  304. }
  305. void can_network_init(void)
  306. {
  307. queue_init(&can_rx_queue);
  308. queue_init(&can_tx_queue);
  309. can_rx_back_init(can_rx_callback);
  310. can_tx_back_init(can_tx_callback);
  311. }
  312. void can_process(void)
  313. {
  314. can_rx_process();
  315. send_message();
  316. can_start_send();
  317. }