can_task.c 8.7 KB

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