can_debug.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  78. }
  79. void register_can_dev_func(uint8_t *buf)
  80. {
  81. #pragma pack(1)
  82. struct can_dev_temp
  83. {
  84. uint32_t ID;
  85. uint8_t len;
  86. uint8_t data[8];
  87. uint32_t time;
  88. };
  89. #pragma pack(0)
  90. struct can_dev_temp temp_a;
  91. uint8_t i = 0;
  92. memcpy(&temp_a.ID,buf,sizeof(struct can_dev_temp));
  93. if(debug_can.Total_Dev_num != 0)
  94. {
  95. for(i=0;i<debug_can.Total_Dev_num;i++)
  96. {
  97. if(debug_can.ID_buf[i].ID == temp_a.ID)
  98. {
  99. break;
  100. }
  101. if((i + 1 == debug_can.Total_Dev_num) && (debug_can.Total_Dev_num < MAX_ID_NUM - 1)) //防止超buf
  102. {
  103. debug_can.Total_Dev_num++;
  104. i++;
  105. break;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. debug_can.Total_Dev_num++;
  112. }
  113. debug_can.ID_buf[i].ID = temp_a.ID;
  114. debug_can.ID_buf[i].len = temp_a.len;
  115. debug_can.ID_buf[i].send_fre = 1000 / (HAL_GetTick() - debug_can.ID_buf[i].ID_time);
  116. debug_can.ID_buf[i].ID_time = HAL_GetTick();
  117. memcpy(&debug_can.ID_buf[i].data[0],&temp_a.data[0],temp_a.len);
  118. }
  119. void seek_can_debug_buf_adr(void)
  120. {
  121. uint8_t temp_buf[128] = {0}, c = 0, seek_i = 0;
  122. while (rkfifo_out(&candebug_rkfifo, &c, 1) != 0)
  123. {
  124. temp_buf[seek_i] = c;
  125. if(temp_buf[seek_i] == 'K' && temp_buf[seek_i - 1] == 'V')
  126. {
  127. temp_buf[seek_i] = c;
  128. register_can_dev_func(temp_buf);
  129. seek_i = 0;
  130. break;
  131. }
  132. else
  133. {
  134. seek_i++;
  135. }
  136. }
  137. }
  138. void put_candata_to_rkfifo_rx(CAN_RxHeaderTypeDef rxhead,uint8_t *data)
  139. {
  140. uint32_t cur_time = HAL_GetTick();
  141. uint8_t tem_buf[30] = {0},i = 0;
  142. if(rxhead.IDE == CAN_ID_STD)
  143. memcpy(&tem_buf[i],&rxhead.StdId,4);
  144. else
  145. memcpy(&tem_buf[i],&rxhead.ExtId,4);
  146. i += 4;
  147. tem_buf[i++] = rxhead.DLC;
  148. memcpy(&tem_buf[i],&data[0],8);
  149. i += 8;
  150. memcpy(&tem_buf[i],&cur_time,4);
  151. i += 4;
  152. tem_buf[i++] = 'V';
  153. tem_buf[i++] = 'K';
  154. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  155. }
  156. void put_candata_to_rkfifo_tx(CAN_TxHeaderTypeDef txhead,uint8_t *data)
  157. {
  158. uint32_t cur_time = HAL_GetTick();
  159. uint8_t tem_buf[30] = {0},i = 0;
  160. if(txhead.IDE == CAN_ID_STD)
  161. memcpy(&tem_buf[i],&txhead.StdId,4);
  162. else
  163. memcpy(&tem_buf[i],&txhead.ExtId,4);
  164. i += 4;
  165. tem_buf[i++] = txhead.DLC;
  166. memcpy(&tem_buf[i],&data[0],8);
  167. i += 8;
  168. memcpy(&tem_buf[i],&cur_time,4);
  169. i += 4;
  170. tem_buf[i++] = 'V';
  171. tem_buf[i++] = 'K';
  172. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  173. }