protocol.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include <stdint.h>
  3. /* ================ 老协议 ================== */
  4. #pragma pack (push,1)
  5. typedef struct
  6. {
  7. //整个包大小,包含protocol_head_t
  8. uint8_t size;
  9. //命令设备
  10. uint8_t type;
  11. //协议类型
  12. uint8_t protocol;
  13. //命令类型
  14. uint8_t cmd;
  15. //命令校验
  16. uint16_t checksum;
  17. //命令处理调用
  18. uint8_t dir;
  19. //命令返回状态
  20. uint8_t bStatus;
  21. }protocol_old_head_t;
  22. #pragma pack(pop)
  23. /* ================ 新协议 ================== */
  24. #define MY_CAN_ADDR 0x30
  25. #pragma pack (push,1)
  26. typedef struct
  27. {
  28. uint8_t can_addr:7;
  29. uint8_t can_dir:1; //0->out, 1 ->in,如果发现收到的是0,可能是红外那边自环过来的,需要丢弃
  30. }can_head_t;
  31. #define CAN_OUT(head, addr) {(head)->can_addr = addr; (head)->can_dir = 0;}
  32. #define CAN_IN(head) ((head)->can_dir == 1)
  33. typedef struct {
  34. can_head_t head;
  35. uint8_t key;
  36. uint8_t data[0];
  37. }can_frame_t;
  38. #pragma pack(pop)
  39. /* can key define */
  40. #define CAN_KEY_BMS_SET_POWER 0x0B //开关各种mos和小电流
  41. #pragma pack (push,1)
  42. typedef struct {
  43. uint8_t discharger_fet :1;//dis/charger mos是否打开
  44. uint8_t charger_fet :1;
  45. uint8_t small_power :1;//小电是否打开
  46. uint8_t res :5;
  47. }pwr_cmd_t;
  48. #pragma pack(pop)
  49. #define CAN_KEY_BMS_GET_STAT 0xa0 //bms_stat_t
  50. #pragma pack (push,1)
  51. typedef struct {
  52. uint8_t insert :1;//是否新插入,检测到霍尔马上置为1,等CAN_KEY_BMS_CLEAR
  53. uint8_t health :1;//电池是否有异常
  54. uint8_t discharger_fet :1;//dis/charger mos是否打开
  55. uint8_t charger_fet :1;
  56. uint8_t small_power :1;//小电是否打开
  57. uint8_t is_charging :1;//是否在充电
  58. uint8_t is_charger_in :1;
  59. uint8_t is_balancing :1; //是否在均衡
  60. }stat_cmd_resp_t;
  61. #pragma pack(pop)
  62. #define CAN_KEY_BMS_BASE_INFO 0x00 //电压,电流,能量, SOC,max temp
  63. #pragma pack (push,1)
  64. typedef struct {
  65. uint32_t pack_voltage;
  66. int32_t pack_current;
  67. uint8_t capacity;
  68. uint32_t energy; //能量,给PS100/200/310/360计算续航里程用
  69. int8_t max_temp;//最高的那个温度
  70. uint32_t cycle; //充放电循环次数
  71. }binfo_cmd_resp_t;
  72. #pragma pack(pop)
  73. #define CAN_KEY_BMS_CHARG_INFO 0xa1
  74. typedef struct {
  75. uint32_t charge_current;
  76. uint32_t charge_remain_time; //s
  77. }cinfo_cmd_resp_t;
  78. #define CAN_KEY_BMS_TEMPS 0x11 //return int[4]
  79. #define CAN_KEY_BMS_GET_CELLS 0x07 //return uint16_t[15]
  80. #define CAN_KEY_SET_LOGGER 0xa2 //byte 0:modle, byte1 level
  81. #define CAN_KEY_BMS_CLEAR 0xa3 //clear some flags used by PSxxx, exp: stat_cmd_resp_t.insert
  82. #define CAN_KEY_BMS_GET_HEALTH_STAT 0xa4 //return bms_health_t
  83. #define CAN_KEY_BMS_GET_TIME 0xa5 //return bms running times(second) uint32_t
  84. #define CAN_KEY_BMS_SET_WORK_MODE 0xa6 //byte0=mode, byte1=start/stop
  85. #define CAN_KEY_GET_VERSION 0x0c //return string
  86. #define CAN_KEY_SET_SN 0x06 //string
  87. #define CAN_KEY_GET_SN 0x05 //return string
  88. void protocol_send_bms_info(uint8_t dest, uint8_t key, uint8_t *data, int len);
  89. void protocol_send_debug_info(uint8_t dest, uint8_t *data, int size);
  90. void protocol_send_ack(uint8_t dest, uint8_t key, int result);