protocol.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include <stdint.h>
  3. #include "app/sox/soc.h"
  4. #include "app/sox/measure.h"
  5. #include "app/sox/measure_task.h"
  6. #include "app/sox/health.h"
  7. #include "app/sox/state.h"
  8. /* ================ 老协议 ================== */
  9. #pragma pack (push,1)
  10. typedef struct
  11. {
  12. //整个包大小,包含protocol_head_t
  13. uint8_t size;
  14. //命令设备
  15. uint8_t type;
  16. //协议类型
  17. uint8_t protocol;
  18. //命令类型
  19. uint8_t cmd;
  20. //命令校验
  21. uint16_t checksum;
  22. //命令处理调用
  23. uint8_t dir;
  24. //命令返回状态
  25. uint8_t bStatus;
  26. }protocol_old_head_t;
  27. #pragma pack(pop)
  28. /* ================ 新协议 ================== */
  29. #define MY_CAN_ADDR 0x30
  30. #pragma pack (push,1)
  31. typedef struct
  32. {
  33. uint8_t can_addr:7;
  34. uint8_t can_dir:1; //0->out, 1 ->in,如果发现收到的是0,可能是红外那边自环过来的,需要丢弃
  35. }can_head_t;
  36. #define CAN_OUT(head, addr) {(head)->can_addr = addr; (head)->can_dir = 0;}
  37. #define CAN_IN(head) ((head)->can_dir == 1)
  38. typedef struct {
  39. can_head_t head;
  40. uint8_t key;
  41. uint8_t data[0];
  42. }can_frame_t;
  43. #pragma pack(pop)
  44. #define CAN_KEY_BMS_BASE_INFO 0x00 //电压,电流,能量, SOC,max temp
  45. #pragma pack (push,1)
  46. typedef struct {
  47. uint32_t pack_voltage;
  48. int32_t pack_current;
  49. uint8_t capacity;
  50. uint32_t energy; //能量,给PS100/200/310/360计算续航里程用
  51. int8_t max_temp;//最高的那个温度
  52. uint32_t health :24;
  53. uint32_t state :8; // stat_cmd_resp_t
  54. }binfo_cmd_resp_t;
  55. #pragma pack(pop)
  56. #define CAN_KEY_GET_SN 0x05 //return string
  57. #define CAN_KEY_SET_SN 0x06 //string
  58. #define CAN_KEY_BMS_GET_CELLS 0x07
  59. #pragma pack (push,1)
  60. typedef struct {
  61. uint8_t cell_num;
  62. uint16_t voltages[CELLS_NUM];
  63. }cell_cmd_resp_t;
  64. #pragma pack(pop)
  65. /* can key define */
  66. #define CAN_KEY_BMS_SET_POWER 0x0B //开关各种mos和小电流
  67. #pragma pack (push,1)
  68. typedef struct {
  69. uint8_t discharger_fet :1;//dis/charger mos是否打开
  70. uint8_t charger_fet :1;
  71. uint8_t small_power :1;//小电是否打开
  72. uint8_t res :1;
  73. uint8_t discharger_mask :1;
  74. uint8_t charger_mask :1;
  75. uint8_t small_mask :1;
  76. }pwr_cmd_t;
  77. #pragma pack(pop)
  78. #define CAN_KEY_GET_VERSION 0x0c //return string
  79. #define CAN_KEY_BMS_TEMPS 0x11 //return int[4]
  80. #define CAN_KEY_AGING_TEMPS 0x12
  81. #define CAN_KEY_BMS_GET_STAT 0xa0 //bms_stat_t
  82. #pragma pack (push,1)
  83. typedef struct {
  84. uint8_t insert :1;//是否新插入,检测到霍尔马上置为1,等CAN_KEY_BMS_CLEAR
  85. uint8_t health :1;//电池是否有异常
  86. uint8_t discharger_fet :1;//dis/charger mos是否打开
  87. uint8_t charger_fet :1;
  88. uint8_t small_power :1;//小电是否打开
  89. uint8_t is_charging :1;//是否在充电
  90. uint8_t is_charger_in :1;
  91. uint8_t is_balancing :1; //是否在均衡
  92. }stat_cmd_resp_t;
  93. #pragma pack(pop)
  94. #define CAN_KEY_BMS_CHARG_INFO 0xa1
  95. #pragma pack (push,1)
  96. typedef struct {
  97. uint32_t charge_current;
  98. uint32_t charge_remain_time; //s
  99. }cinfo_cmd_resp_t;
  100. #pragma pack(pop)
  101. #define CAN_KEY_SET_LOGGER 0xa2 //byte 0:modle, byte1 level
  102. #define CAN_KEY_BMS_CLEAR 0xa3 //clear some flags used by PSxxx, exp: stat_cmd_resp_t.insert
  103. #define CAN_KEY_BMS_GET_HEALTH_STAT 0xa4 //return bms_health_t
  104. #define CAN_KEY_BMS_GET_TIME 0xa5 //return bms running times(second) uint32_t
  105. #define CAN_KEY_BMS_SET_WORK_MODE 0xa6 //byte0=mode, byte1=start/stop
  106. #define CAN_KEY_GET_SOC_INFO 0xa7
  107. typedef struct {
  108. uint32_t c_min;
  109. uint32_t c_max;
  110. uint32_t c_now;
  111. uint32_t c_charger;
  112. uint32_t c_discharger;
  113. uint32_t cycle; //充放电循环次数
  114. uint32_t calibrated;
  115. }soc_info_t;
  116. #define CAN_KEY_START_CALI 0xa8
  117. #pragma pack (push,1)
  118. typedef struct {
  119. uint8_t flags; // bit0:1-cs1180, 0- ml5238, bit1-2 0:stop, 1:start, 2: continue
  120. uint8_t gain;
  121. uint8_t totol_samples;
  122. int16_t current;
  123. uint16_t voltage;
  124. }cali_cmd_t;
  125. #pragma pack(pop)
  126. #define CAN_KEY_PCBA_TEST 0xa9
  127. #define CAN_KEY_RESTORE_NV 0xaa /* v1/v2升级到v3使用 */
  128. #pragma pack (push,1)
  129. typedef struct {
  130. uint8_t flags;// 1 表示已经校准, 0 未校准
  131. uint8_t capacity;
  132. uint8_t sn_len;
  133. uint8_t sn[24];
  134. }restore_nv_cmd_t;
  135. #pragma pack(pop)
  136. #define CAN_KEY_MIN_SOC 0xab /*设置最小容量,测试使用*/
  137. #define CAN_KEY_GET_EVENT 0xac /*获取事件信息*/
  138. #define CAN_KEY_POWERDOWN 0xad /* power down, magic:0xFF005AA5*/
  139. #define CAN_KEY_IAP_ENTER 0xF0
  140. #define CAN_KEY_IAP_BEGIN 0xF1
  141. #define CAN_KEY_IAP_CHECK 0xF2
  142. #define CAN_KEY_IAP_WRITE 0xF4
  143. #define CAN_KEY_IAP_BOOT 0xF5
  144. #define CAN_KEY_IAP_STAT 0xF6
  145. #define CAN_EEY_IAP_READ_STRING 0xF8
  146. #define CAN_KEY_REBOOT 0xFC
  147. #define CAN_KET_ERASE_NV 0xFE
  148. void protocol_send_bms_info(uint8_t dest, uint8_t key, uint8_t *data, int len);
  149. void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size);
  150. void protocol_send_ack(uint8_t dest, uint8_t key, int result);