can_debug.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "can_debug.h"
  2. #include "usart_data_handle.h"
  3. #include "string.h"
  4. rkfifo_t candebug_rkfifo;
  5. Debug_buf debug_can;
  6. void check_can_dev_connect(void)
  7. {
  8. if(debug_can.Total_Dev_num != 0)
  9. {
  10. for(uint8_t i=0;i<debug_can.Total_Dev_num;i++)
  11. {
  12. if(HAL_GetTick() - debug_can.ID_buf[i].ID_time > 5000)
  13. {
  14. debug_can.ID_buf[i].connect_status = 2;
  15. }
  16. else
  17. {
  18. debug_can.ID_buf[i].connect_status = 1;
  19. }
  20. }
  21. }
  22. }
  23. int get_data_total_len(uint8_t i)
  24. {
  25. uint8_t len = 0;
  26. len += 4; //ID
  27. len++; //len
  28. len++; //status
  29. len++; //send_fre
  30. len += i;
  31. return len;
  32. }
  33. void register_can_dev_func(uint8_t *buf)
  34. {
  35. #pragma pack(1)
  36. struct can_dev_temp
  37. {
  38. uint32_t ID;
  39. uint8_t len;
  40. uint8_t data[8];
  41. uint32_t time;
  42. };
  43. #pragma pack(0)
  44. struct can_dev_temp temp_a;
  45. uint8_t i = 0;
  46. memcpy(&temp_a.ID,buf,sizeof(struct can_dev_temp));
  47. if(debug_can.Total_Dev_num != 0)
  48. {
  49. for(i=0;i<debug_can.Total_Dev_num;i++)
  50. {
  51. if(debug_can.ID_buf[i].ID == temp_a.ID)
  52. {
  53. break;
  54. }
  55. if((i + 1 == debug_can.Total_Dev_num) && (debug_can.Total_Dev_num < MAX_ID_NUM - 1)) //防止超buf
  56. {
  57. debug_can.Total_Dev_num++;
  58. i++;
  59. break;
  60. }
  61. }
  62. }
  63. else
  64. {
  65. debug_can.Total_Dev_num++;
  66. }
  67. debug_can.ID_buf[i].ID = temp_a.ID;
  68. debug_can.ID_buf[i].len = temp_a.len;
  69. debug_can.ID_buf[i].send_fre = 1000 / (HAL_GetTick() - debug_can.ID_buf[i].ID_time);
  70. debug_can.ID_buf[i].ID_time = HAL_GetTick();
  71. memcpy(&debug_can.ID_buf[i].data[0],&temp_a.data[0],temp_a.len);
  72. }
  73. void seek_can_debug_buf_adr(void)
  74. {
  75. uint8_t temp_buf[128] = {0}, c = 0, seek_i = 0;
  76. while (rkfifo_out(&candebug_rkfifo, &c, 1) != 0)
  77. {
  78. temp_buf[seek_i] = c;
  79. if(temp_buf[seek_i] == 'K' && temp_buf[seek_i - 1] == 'V')
  80. {
  81. temp_buf[seek_i] = c;
  82. register_can_dev_func(temp_buf);
  83. seek_i = 0;
  84. break;
  85. }
  86. else
  87. {
  88. seek_i++;
  89. }
  90. }
  91. }
  92. void put_candata_to_rkfifo_rx(CAN_RxHeaderTypeDef rxhead,uint8_t *data)
  93. {
  94. uint32_t cur_time = HAL_GetTick();
  95. uint8_t tem_buf[30] = {0},i = 0;
  96. if(rxhead.IDE == CAN_ID_STD)
  97. memcpy(&tem_buf[i],&rxhead.StdId,4);
  98. else
  99. memcpy(&tem_buf[i],&rxhead.ExtId,4);
  100. i += 4;
  101. tem_buf[i++] = rxhead.DLC;
  102. memcpy(&tem_buf[i],&data[0],8);
  103. i += 8;
  104. memcpy(&tem_buf[i],&cur_time,4);
  105. i += 4;
  106. tem_buf[i++] = 'V';
  107. tem_buf[i++] = 'K';
  108. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  109. }
  110. void put_candata_to_rkfifo_tx(CAN_TxHeaderTypeDef txhead,uint8_t *data)
  111. {
  112. uint32_t cur_time = HAL_GetTick();
  113. uint8_t tem_buf[30] = {0},i = 0;
  114. if(txhead.IDE == CAN_ID_STD)
  115. memcpy(&tem_buf[i],&txhead.StdId,4);
  116. else
  117. memcpy(&tem_buf[i],&txhead.ExtId,4);
  118. i += 4;
  119. tem_buf[i++] = txhead.DLC;
  120. memcpy(&tem_buf[i],&data[0],8);
  121. i += 8;
  122. memcpy(&tem_buf[i],&cur_time,4);
  123. i += 4;
  124. tem_buf[i++] = 'V';
  125. tem_buf[i++] = 'K';
  126. rkfifo_in(&candebug_rkfifo,tem_buf,i);
  127. }