can_cali.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "can_cali.h"
  2. #include "can_id_deal.h"
  3. #include "utils.h"
  4. #include <stdint.h>
  5. enum
  6. {
  7. cal_ram = 0,
  8. cal_eeprom,
  9. cal_io,
  10. };
  11. enum
  12. {
  13. read_cal = 0,
  14. write_cal,
  15. };
  16. enum
  17. {
  18. cali_data_len_32bit = 0,
  19. cali_data_len_16bit,
  20. cali_data_len_8bit,
  21. cali_data_len_1bit,
  22. cali_data_len_32bit_float,
  23. };
  24. typedef union cali_msg
  25. {
  26. uint8_t u8_data[8];
  27. uint64_t u64_data;
  28. struct
  29. {
  30. uint64_t cmd_type : 4;
  31. uint64_t data_len : 3;
  32. uint64_t rw : 1;
  33. uint64_t en : 1;
  34. uint64_t rsed : 3;
  35. uint64_t data_id : 12;
  36. uint64_t data : 32;
  37. uint64_t check_sum : 8;
  38. } bit;
  39. } cali_msg_type;
  40. typedef union
  41. {
  42. float f_data;
  43. uint8_t u8_data[4];
  44. uint32_t u32_data;
  45. } float_convert;
  46. typedef struct
  47. {
  48. uint8_t en_cal; // 自动退出标定;
  49. uint8_t *val_cal; // 标定使能位
  50. uint32_t count;
  51. uint32_t over_time;
  52. } cali_connect_type;
  53. static cali_connect_type can_cal_mgt[100];
  54. // 标定自动断开设置
  55. static uint8_t can_cali_conneet_set(uint8_t index, uint32_t over_time, uint8_t *p)
  56. {
  57. if (index < ARR_SIZE(can_cal_mgt))
  58. {
  59. can_cal_mgt[index].en_cal = 1;
  60. can_cal_mgt[index].val_cal = p;
  61. can_cal_mgt[index].count = 0;
  62. can_cal_mgt[index].over_time = over_time;
  63. }
  64. return 0;
  65. }
  66. // 标定自动断开管理
  67. void can_cal_connect_mgt(void)
  68. {
  69. uint8_t i;
  70. for (i = 0; i < ARR_SIZE(can_cal_mgt); i++)
  71. {
  72. if (can_cal_mgt[i].en_cal)
  73. {
  74. if (++can_cal_mgt[i].count >= can_cal_mgt[i].over_time)
  75. {
  76. *can_cal_mgt[i].val_cal = 0; // 超时清除
  77. can_cal_mgt[i].en_cal = 0;
  78. }
  79. }
  80. else
  81. {
  82. *can_cal_mgt[i].val_cal = 0; // 命令清除
  83. }
  84. }
  85. }
  86. uint8_t led_en = 0;
  87. uint8_t led_en_value = 0;
  88. void cal_write(pdu_tag rec_msg)
  89. {
  90. pdu_tag tx_msg;
  91. cali_msg_type rx_data;
  92. float_convert data;
  93. rx_data.u64_data = rec_msg.data.u64_buf;
  94. switch (rx_data.bit.data_id)
  95. {
  96. case 1:
  97. if ((rx_data.bit.cmd_type == cal_ram) && (rx_data.bit.data_len == cali_data_len_1bit))
  98. {
  99. if (rx_data.bit.en == 1)
  100. {
  101. led_en = 1;
  102. }
  103. else
  104. {
  105. led_en = 0;
  106. }
  107. led_en_value = (uint8_t)rx_data.bit.data;
  108. can_cali_conneet_set(rx_data.bit.data_id, 2000, &led_en);
  109. }
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. void cal_read(pdu_tag rec_msg)
  116. {
  117. pdu_tag tx_msg;
  118. cali_msg_type rx_data, tx_data;
  119. float_convert data;
  120. rx_data.u64_data = rec_msg.data.u64_buf;
  121. tx_data.u64_data = rec_msg.data.u64_buf;
  122. switch (rx_data.bit.data_id)
  123. {
  124. case 1:
  125. if ((rx_data.bit.cmd_type == cal_ram) && (rx_data.bit.data_len == cali_data_len_1bit))
  126. {
  127. rx_data.bit.en == led_en;
  128. tx_data.bit.data = led_en_value;
  129. }
  130. break;
  131. default:
  132. break;
  133. }
  134. tx_msg.data.u64_buf = tx_data.u64_data;
  135. push_can_message_to_queue(get_new_can_id(CAN_CALI), 8, tx_msg.data.u8_buf);
  136. }
  137. void can_cali_msg_analysis(pdu_tag rec_msg)
  138. {
  139. cali_msg_type cal_data;
  140. cal_data.u64_data = rec_msg.data.u64_buf;
  141. if (cal_data.bit.rw == read_cal)
  142. {
  143. cal_read(rec_msg);
  144. }
  145. else if (cal_data.bit.rw == write_cal)
  146. {
  147. cal_write(rec_msg);
  148. }
  149. }