can_debug.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "can_debug.h"
  2. #include "stm32f1xx_hal.h"
  3. #include "string.h"
  4. #include "stdbool.h"
  5. #include "soft_p_2_c.h"
  6. #include "crc.h"
  7. #include "soft_uart.h"
  8. rkfifo_t candebug_rkfifo;
  9. Debug_buf debug_can;
  10. void check_can_dev_connect(void)
  11. {
  12. if(debug_can.Total_Dev_num != 0)
  13. {
  14. for(uint8_t i=0;i<debug_can.Total_Dev_num;i++)
  15. {
  16. if(HAL_GetTick() - debug_can.ID_buf[i].ID_time > 5000)
  17. {
  18. debug_can.ID_buf[i].connect_status = 2;
  19. }
  20. else
  21. {
  22. debug_can.ID_buf[i].connect_status = 1;
  23. }
  24. }
  25. }
  26. }
  27. int get_data_total_len(uint8_t i)
  28. {
  29. uint8_t len = 0;
  30. len += 4; //ID
  31. len++; //len
  32. len++; //status
  33. len++; //send_fre
  34. len += i;
  35. return len;
  36. }
  37. void Can_send_debug_to_app(void)
  38. {
  39. int index = 0;
  40. uint16_t crc = 0;
  41. uint8_t send_time = debug_can.Total_Dev_num / 16 + 1;
  42. uint8_t element_num = 0,total_pack_num = 0;
  43. check_can_dev_connect();
  44. for(uint8_t i = 0;i < send_time; i++)
  45. {
  46. index = 0;
  47. crc = 0;
  48. msg_buf[index++] = 0xFE;
  49. msg_buf[index++] = 0;
  50. msg_buf[index++] = 0; //组件计数
  51. msg_buf[index++] = 0x00;
  52. msg_buf[index++] = 0x00;
  53. msg_buf[index++] = _MSGID_CANDEBUG;
  54. debug_can.Len = get_data_total_len;
  55. if(send_time > 1)
  56. {
  57. element_num = i * 15;
  58. total_pack_num = (send_time - 1 - i) == 0? debug_can.Total_Dev_num - (i *15) : 15;
  59. }
  60. else
  61. {
  62. element_num = 0;
  63. total_pack_num = debug_can.Total_Dev_num;
  64. }
  65. msg_buf[index++] = total_pack_num;
  66. for(uint8_t i=0;i<total_pack_num;i++)
  67. {
  68. memcpy(&msg_buf[index], &debug_can.ID_buf[i + element_num].ID, debug_can.Len(debug_can.ID_buf[i + element_num].len));
  69. index += debug_can.Len(debug_can.ID_buf[i + element_num].len);
  70. }
  71. msg_buf[1] = index - 6;
  72. crc = Get_Crc16(msg_buf, index);
  73. msg_buf[index++] = crc;
  74. msg_buf[index++] = (crc >> 8) & 0xff;
  75. uart2_send_msg(msg_buf, index);
  76. //uart3_send_msg(msg_buf, index);
  77. if(send_time > 1)
  78. {
  79. HAL_Delay(50);
  80. }
  81. }
  82. }
  83. void register_can_dev_func(uint8_t *buf)
  84. {
  85. #pragma pack(1)
  86. struct can_dev_temp
  87. {
  88. uint32_t ID;
  89. uint8_t len;
  90. uint8_t data[8];
  91. uint32_t time;
  92. };
  93. #pragma pack(0)
  94. struct can_dev_temp temp_a;
  95. uint8_t i = 0;
  96. memcpy(&temp_a.ID,buf,sizeof(struct can_dev_temp));
  97. if(debug_can.Total_Dev_num != 0)
  98. {
  99. for(i=0;i<debug_can.Total_Dev_num;i++)
  100. {
  101. if(debug_can.ID_buf[i].ID == temp_a.ID)
  102. {
  103. break;
  104. }
  105. if((i + 1 == debug_can.Total_Dev_num) && (debug_can.Total_Dev_num < MAX_ID_NUM - 1)) //防止超buf
  106. {
  107. debug_can.Total_Dev_num++;
  108. i++;
  109. break;
  110. }
  111. }
  112. }
  113. else
  114. {
  115. debug_can.Total_Dev_num++;
  116. }
  117. debug_can.ID_buf[i].ID = temp_a.ID;
  118. debug_can.ID_buf[i].len = temp_a.len;
  119. debug_can.ID_buf[i].send_fre = 1000 / (HAL_GetTick() - debug_can.ID_buf[i].ID_time);
  120. debug_can.ID_buf[i].ID_time = HAL_GetTick();
  121. memcpy(&debug_can.ID_buf[i].data[0],&temp_a.data[0],temp_a.len);
  122. }
  123. void seek_can_debug_buf_adr(void)
  124. {
  125. uint8_t temp_buf[128] = {0}, c = 0, seek_i = 0;
  126. while (rkfifo_out(&candebug_rkfifo, &c, 1) != 0)
  127. {
  128. temp_buf[seek_i] = c;
  129. if(temp_buf[seek_i] == 'K' && temp_buf[seek_i - 1] == 'V')
  130. {
  131. temp_buf[seek_i] = c;
  132. register_can_dev_func(temp_buf);
  133. seek_i = 0;
  134. break;
  135. }
  136. else
  137. {
  138. seek_i++;
  139. }
  140. }
  141. }
  142. void put_candata_to_rkfifo_rx(CAN_RxHeaderTypeDef rxhead,uint8_t *data)
  143. {
  144. uint32_t cur_time = HAL_GetTick();
  145. uint8_t tem_buf[30] = {0},i = 0;
  146. if(rxhead.IDE == CAN_ID_STD)
  147. memcpy(&tem_buf[i],&rxhead.StdId,4);
  148. else
  149. memcpy(&tem_buf[i],&rxhead.ExtId,4);
  150. i += 4;
  151. tem_buf[i++] = rxhead.DLC;
  152. memcpy(&tem_buf[i],&data[0],8);
  153. i += 8;
  154. memcpy(&tem_buf[i],&cur_time,4);
  155. i += 4;
  156. tem_buf[i++] = 'V';
  157. tem_buf[i++] = 'K';
  158. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  159. }
  160. void put_candata_to_rkfifo_tx(CAN_TxHeaderTypeDef txhead,uint8_t *data)
  161. {
  162. uint32_t cur_time = HAL_GetTick();
  163. uint8_t tem_buf[30] = {0},i = 0;
  164. if(txhead.IDE == CAN_ID_STD)
  165. memcpy(&tem_buf[i],&txhead.StdId,4);
  166. else
  167. memcpy(&tem_buf[i],&txhead.ExtId,4);
  168. i += 4;
  169. tem_buf[i++] = txhead.DLC;
  170. memcpy(&tem_buf[i],&data[0],8);
  171. i += 8;
  172. memcpy(&tem_buf[i],&cur_time,4);
  173. i += 4;
  174. tem_buf[i++] = 'V';
  175. tem_buf[i++] = 'K';
  176. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  177. }