protocol.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. /* can key define */
  45. #define CAN_KEY_BMS_SET_POWER 0x0B //开关各种mos和小电流
  46. #pragma pack (push,1)
  47. typedef struct {
  48. uint8_t discharger_fet :1;//dis/charger mos是否打开
  49. uint8_t charger_fet :1;
  50. uint8_t small_power :1;//小电是否打开
  51. uint8_t res :1;
  52. uint8_t discharger_mask :1;
  53. uint8_t charger_mask :1;
  54. uint8_t small_mask :1;
  55. }pwr_cmd_t;
  56. #pragma pack(pop)
  57. #define CAN_KEY_BMS_GET_STAT 0xa0 //bms_stat_t
  58. #pragma pack (push,1)
  59. typedef struct {
  60. uint8_t insert :1;//是否新插入,检测到霍尔马上置为1,等CAN_KEY_BMS_CLEAR
  61. uint8_t health :1;//电池是否有异常
  62. uint8_t discharger_fet :1;//dis/charger mos是否打开
  63. uint8_t charger_fet :1;
  64. uint8_t small_power :1;//小电是否打开
  65. uint8_t is_charging :1;//是否在充电
  66. uint8_t is_charger_in :1;
  67. uint8_t is_balancing :1; //是否在均衡
  68. }stat_cmd_resp_t;
  69. #pragma pack(pop)
  70. #define CAN_KEY_BMS_BASE_INFO 0x00 //电压,电流,能量, SOC,max temp
  71. #pragma pack (push,1)
  72. typedef struct {
  73. uint32_t pack_voltage;
  74. int32_t pack_current;
  75. uint8_t capacity;
  76. uint32_t energy; //能量,给PS100/200/310/360计算续航里程用
  77. int8_t max_temp;//最高的那个温度
  78. uint32_t health :24;
  79. uint32_t state :8; // stat_cmd_resp_t
  80. }binfo_cmd_resp_t;
  81. #pragma pack(pop)
  82. #define CAN_KEY_BMS_CHARG_INFO 0xa1
  83. #pragma pack (push,1)
  84. typedef struct {
  85. uint32_t charge_current;
  86. uint32_t charge_remain_time; //s
  87. }cinfo_cmd_resp_t;
  88. #pragma pack(pop)
  89. #define CAN_KEY_BMS_GET_CELLS 0x07
  90. #pragma pack (push,1)
  91. typedef struct {
  92. uint8_t cell_num;
  93. uint16_t voltages[CELLS_NUM];
  94. }cell_cmd_resp_t;
  95. #pragma pack(pop)
  96. #define CAN_KEY_GET_SOC_INFO 0xa7
  97. typedef struct {
  98. int c_min;
  99. int c_max;
  100. int c_now;
  101. int c_charger;
  102. int c_discharger;
  103. uint32_t cycle; //充放电循环次数
  104. uint32_t calibrated;
  105. }soc_info_t;
  106. #define CAN_KEY_BMS_TEMPS 0x11 //return int[4]
  107. #define CAN_KEY_SET_LOGGER 0xa2 //byte 0:modle, byte1 level
  108. #define CAN_KEY_BMS_CLEAR 0xa3 //clear some flags used by PSxxx, exp: stat_cmd_resp_t.insert
  109. #define CAN_KEY_BMS_GET_HEALTH_STAT 0xa4 //return bms_health_t
  110. #define CAN_KEY_BMS_GET_TIME 0xa5 //return bms running times(second) uint32_t
  111. #define CAN_KEY_BMS_SET_WORK_MODE 0xa6 //byte0=mode, byte1=start/stop
  112. #define CAN_KEY_GET_VERSION 0x0c //return string
  113. #define CAN_KEY_SET_SN 0x06 //string
  114. #define CAN_KEY_GET_SN 0x05 //return string
  115. #define CAN_KEY_START_CALI 0xa8
  116. #define CAN_KEY_PCBA_TEST 0xa9
  117. #define CAN_KEY_IAP_ENTER 0xF0
  118. #define CAN_KEY_IAP_BEGIN 0xF1
  119. #define CAN_KEY_IAP_CHECK 0xF2
  120. #define CAN_KEY_IAP_WRITE 0xF4
  121. #define CAN_KEY_IAP_BOOT 0xF5
  122. #define CAN_EEY_IAP_READ_STRING 0xF8
  123. #define CAN_KEY_REBOOT 0xFC
  124. #define CAN_KET_ERASE_NV 0xFE
  125. void protocol_send_bms_info(uint8_t dest, uint8_t key, uint8_t *data, int len);
  126. void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size);
  127. void protocol_send_ack(uint8_t dest, uint8_t key, int result);