protocol.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 :5;
  52. }pwr_cmd_t;
  53. #pragma pack(pop)
  54. #define CAN_KEY_BMS_GET_STAT 0xa0 //bms_stat_t
  55. #pragma pack (push,1)
  56. typedef struct {
  57. uint8_t insert :1;//是否新插入,检测到霍尔马上置为1,等CAN_KEY_BMS_CLEAR
  58. uint8_t health :1;//电池是否有异常
  59. uint8_t discharger_fet :1;//dis/charger mos是否打开
  60. uint8_t charger_fet :1;
  61. uint8_t small_power :1;//小电是否打开
  62. uint8_t is_charging :1;//是否在充电
  63. uint8_t is_charger_in :1;
  64. uint8_t is_balancing :1; //是否在均衡
  65. }stat_cmd_resp_t;
  66. #pragma pack(pop)
  67. #define CAN_KEY_BMS_BASE_INFO 0x00 //电压,电流,能量, SOC,max temp
  68. #pragma pack (push,1)
  69. typedef struct {
  70. uint32_t pack_voltage;
  71. int32_t pack_current;
  72. uint8_t capacity;
  73. uint32_t energy; //能量,给PS100/200/310/360计算续航里程用
  74. int8_t max_temp;//最高的那个温度
  75. uint32_t cycle; //充放电循环次数
  76. uint8_t calibrated;
  77. }binfo_cmd_resp_t;
  78. #pragma pack(pop)
  79. #define CAN_KEY_BMS_CHARG_INFO 0xa1
  80. #pragma pack (push,1)
  81. typedef struct {
  82. uint32_t charge_current;
  83. uint32_t charge_remain_time; //s
  84. }cinfo_cmd_resp_t;
  85. #pragma pack(pop)
  86. #define CAN_KEY_BMS_GET_CELLS 0x07
  87. #pragma pack (push,1)
  88. typedef struct {
  89. uint8_t cell_num;
  90. uint16_t voltages[CELLS_NUM];
  91. }cell_cmd_resp_t;
  92. #pragma pack(pop)
  93. #define CAN_KEY_BMS_TEMPS 0x11 //return int[4]
  94. #define CAN_KEY_SET_LOGGER 0xa2 //byte 0:modle, byte1 level
  95. #define CAN_KEY_BMS_CLEAR 0xa3 //clear some flags used by PSxxx, exp: stat_cmd_resp_t.insert
  96. #define CAN_KEY_BMS_GET_HEALTH_STAT 0xa4 //return bms_health_t
  97. #define CAN_KEY_BMS_GET_TIME 0xa5 //return bms running times(second) uint32_t
  98. #define CAN_KEY_BMS_SET_WORK_MODE 0xa6 //byte0=mode, byte1=start/stop
  99. #define CAN_KEY_GET_VERSION 0x0c //return string
  100. #define CAN_KEY_SET_SN 0x06 //string
  101. #define CAN_KEY_GET_SN 0x05 //return string
  102. void protocol_send_bms_info(uint8_t dest, uint8_t key, uint8_t *data, int len);
  103. void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size);
  104. void protocol_send_ack(uint8_t dest, uint8_t key, int result);