app_rs485_1.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef APP_RS485_1_H
  2. #define APP_RS485_1_H
  3. #define CONFIG_TX_BUFF_SIZE 256
  4. #define CONFIG_RX_BUFF_SIZE 256
  5. #define CONFIG_LINK_GOOD 5
  6. enum
  7. {
  8. OP_NONE,
  9. OP_BOND,
  10. OP_WRITE_SN,
  11. OP_PAIR,
  12. OP_UPDATE_PAIR,
  13. OP_READ_INFO,
  14. OP_ALARM_TIMES,
  15. OP_CELL_VOL,//0x07
  16. OP_TEMP_OTHER,//0x08
  17. OP_OPEN_FET = 0x0B,
  18. OP_CLEAR_PAIR = OP_TEMP_OTHER + 1,
  19. OP_UPDATE_SOFTWARE_REQ = 0x0E,
  20. OP_UPDATE_SOFTWARE = 0x0F,
  21. OP_MAX
  22. };
  23. typedef struct
  24. {
  25. uint8_t operate;
  26. uint8_t op_result;
  27. uint16_t resver;
  28. uint32_t param;
  29. }SUB_BMS_CMD;
  30. #define RS485_COM_TIMEOUT (150)
  31. #define RS485_COM_ERROR (3)
  32. #define RS485_COM_UPDATE_TIMEOUT (1500)
  33. /*******************************************通用帧头-start*********************************************************/
  34. #pragma pack (push,1)
  35. typedef struct
  36. {
  37. //整个包大小
  38. uint8_t size;
  39. //命令设备
  40. uint8_t type;
  41. //协议类型
  42. uint8_t protocol;
  43. //命令类型
  44. uint8_t cmd;
  45. //命令校验
  46. uint16_t checksum;
  47. //命令处理调用
  48. uint8_t dir;
  49. //命令返回状态
  50. uint8_t bStatus;
  51. //命令数据部分
  52. }COMMAND_BODY;
  53. #pragma pack(pop)
  54. //
  55. #define BMS_STA_BOND (0x01)
  56. #define BMS_STA_D_OPEN (0x02)
  57. #define BMS_STA_C_OPEN (0x04)
  58. #define BMS_STA_S_OPEN (0x08)
  59. #define BMS_STA_C_FULL (0x10)
  60. #define BMS_STA_S_BAHU (0x20)
  61. #define BMS_STA_JIAO_YAN (0x40)
  62. #define BMS_POWER_FULL (BMS_STA_D_OPEN | BMS_STA_C_OPEN)
  63. #define BMS_POWER_SMALL (BMS_STA_S_OPEN)
  64. #define BMS_POWER_ALL (BMS_POWER_FULL | BMS_POWER_SMALL)
  65. /*******************************************通用帧头-end*********************************************************/
  66. /*******************************************BMS帧头-start*********************************************************/
  67. #define SBU_BMS_ADDRESS (0x30)
  68. #define SUB_BMS_ADDRESS_0 (SBU_BMS_ADDRESS)
  69. #define SUB_BMS_ADDRESS_1 (SBU_BMS_ADDRESS + 1)
  70. #define SUB_BMS_ADDRESS_2 (SBU_BMS_ADDRESS + 2)
  71. #define BMS_DIR (0x16)
  72. #define SET_COMMAND (0x10)
  73. #define GET_COMMAND (0x20)
  74. #define ACK_COMMAND (0x30)
  75. #define ENG_COMMAND (0x40)
  76. /*******************************************BMS帧头-end*********************************************************/
  77. typedef enum {
  78. SHARK_BMS_VGS_ACC2 = 1 << 0,
  79. SHARK_BMS_VGS_FV = 1 << 1,
  80. SHARK_BMS_VGS_ALL = 0xFF
  81. } shark_bms_vgs_t;
  82. typedef enum {
  83. SHARK_BATT_POWER_FAULT,
  84. SHARK_BATT_POWER_OFF,
  85. SHARK_BATT_POWER_SMALL,
  86. SHARK_BATT_POWER_FULL,
  87. SHARK_BATT_POWER_ALL,
  88. } shark_battery_power_t;
  89. typedef enum {
  90. SHARK_BATT_EXIT_SUCCESS,
  91. SHARK_BATT_EXIT_UPGRADE,
  92. SHARK_BATT_EXIT_POWER,
  93. SHARK_BATT_EXIT_SHAKE,
  94. SHARK_BATT_EXIT_ERROR,
  95. SHARK_BATT_EXIT_SOAK,
  96. SHARK_BATT_EXIT_485,
  97. } shark_battery_exit_t;
  98. typedef enum {
  99. SHARK_BATT_MASK_NONE = 0,
  100. SHARK_BATT_MASK_BAT1 = 1 << 0,
  101. SHARK_BATT_MASK_BAT2 = 1 << 1,
  102. SHARK_BATT_MASK_BOTH = SHARK_BATT_MASK_BAT1 | SHARK_BATT_MASK_BAT2
  103. } shark_battery_mask_t;
  104. #pragma pack (push,1)
  105. typedef struct
  106. {
  107. uint8_t yjkxslc;
  108. uint8_t m_percent;
  109. uint8_t yjcdwcsj;
  110. uint8_t charge_flag;
  111. int32_t m_current;
  112. uint32_t m_total_vol;
  113. uint8_t bms_temp;
  114. uint16_t work_status;
  115. uint32_t banlance_cell;
  116. uint8_t bms_status;
  117. uint8_t operate_result;
  118. }PACKET_COMMON;
  119. #define SERIAL_NUM_SIZE 18
  120. #define SOFTWARE_SIZE 20
  121. typedef struct
  122. {
  123. uint8_t sn[SERIAL_NUM_SIZE];
  124. uint8_t soft_ver[SOFTWARE_SIZE];
  125. }BATTERY_DEV_INFO;
  126. typedef struct
  127. {
  128. uint16_t dcsmxs;
  129. uint32_t charge_times;
  130. uint32_t charge_over_vol_times;
  131. uint32_t discharge_under_vol_times;
  132. uint32_t charge_over_cur_times;
  133. uint32_t discharge_under_cur_times;
  134. uint32_t short_times;
  135. uint32_t charge_under_temp_times;
  136. uint32_t discharge_under_temp_times;
  137. uint32_t charge_over_temp_times;
  138. uint32_t discharge_over_temp_times;
  139. }BATTERY_TIMES;
  140. #define CELL_NUM_MAX (15u)
  141. typedef struct
  142. {
  143. uint8_t cv_cell_num;
  144. uint16_t cv_cell_vol[CELL_NUM_MAX];
  145. }CELL_VOL;
  146. #define THERMISTOR_NUM_MAX (4u)
  147. typedef struct
  148. {
  149. int8_t to_temp_num[THERMISTOR_NUM_MAX];
  150. uint8_t to_fet;
  151. }TEMP_OTHER;
  152. #pragma pack(pop)
  153. typedef enum {
  154. SHARK_SEND_IDLE,
  155. SHARK_SEND_PENDING,
  156. SHARK_SEND_SUCCESS,
  157. SHARK_SEND_TIMEOUT,
  158. } shark_send_state_t;
  159. typedef struct
  160. {
  161. PACKET_COMMON packet_common;
  162. BATTERY_DEV_INFO bat_dev_info;
  163. BATTERY_TIMES bat_times;
  164. CELL_VOL cell_vol;
  165. TEMP_OTHER temp_other;
  166. SUB_BMS_CMD sub_bms_cmd;
  167. u32 uart;
  168. u8 address;
  169. u8 connected;
  170. shark_bool used;
  171. shark_send_state_t send_state;
  172. shark_battery_exit_t exit_code;
  173. u8 rx_buff[CONFIG_RX_BUFF_SIZE];
  174. u8 tx_buff[CONFIG_TX_BUFF_SIZE];
  175. u16 rx_length;
  176. u16 tx_length;
  177. u16 tx_index;
  178. u16 poll_ticks;
  179. shark_bool poll_pending;
  180. shark_bool rx_pending;
  181. u8 rx_busy;
  182. u8 tx_busy;
  183. u8 define_error;
  184. u8 test_error;
  185. u8 send_times;
  186. }SUB_BMS_INFO;
  187. extern SUB_BMS_INFO sub_bms_info_1;
  188. extern SUB_BMS_INFO sub_bms_info_2;
  189. typedef struct
  190. {
  191. uint8_t set;
  192. uint16_t count;
  193. uint8_t com_err_flag;
  194. uint32_t com_err_count;
  195. }SUB_BMS_COM;
  196. #if 0
  197. _inline void Sub_BMS_1_RS485_Com_Time_Out(void)
  198. {
  199. if(sub_rs485_time_out_1.set)
  200. {
  201. if(++sub_rs485_time_out_1.count >= RS485_COM_TIMEOUT)
  202. {
  203. RS485_busy_1 = 0;
  204. sub_rs485_time_out_1.set = 0;
  205. sub_rs485_time_out_1.count = 0;
  206. if(sub_rs485_time_out_1.com_err_flag == 0)
  207. {
  208. sub_rs485_time_out_1.com_err_flag = 1;
  209. sub_rs485_time_out_1.com_err_count = 1;
  210. }
  211. else
  212. {
  213. if(++sub_rs485_time_out_1.com_err_count >= 5)
  214. {
  215. sub_rs485_time_out_1.com_err_count = 0;
  216. g_event |= SUB_BMS_1_RS485_DISC_EVENT;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. __inline void Sub_BMS_2_RS485_Com_Time_Out(void)
  223. {
  224. if(sub_rs485_time_out_2.set)
  225. {
  226. if(++sub_rs485_time_out_2.count >= RS485_COM_TIMEOUT)
  227. {
  228. RS485_busy_2 = 0;
  229. sub_rs485_time_out_2.set = 0;
  230. sub_rs485_time_out_2.count = 0;
  231. if(sub_rs485_time_out_2.com_err_flag == 0)
  232. {
  233. sub_rs485_time_out_2.com_err_flag = 1;
  234. sub_rs485_time_out_2.com_err_count = 1;
  235. }
  236. else
  237. {
  238. if(++sub_rs485_time_out_2.com_err_count >= 5)
  239. {
  240. sub_rs485_time_out_2.com_err_count = 0;
  241. g_event |= SUB_BMS_2_RS485_DISC_EVENT;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. #endif
  248. enum
  249. {
  250. CW_NONE,
  251. CW_CHONG_DIAN_ZUO,
  252. CW_CHE_SHANG_CHARGER,
  253. CW_CHE_SHANG_NO_CHARGER,
  254. CW_MAX
  255. };
  256. extern uint8_t cang_wei;
  257. typedef struct
  258. {
  259. uint32_t bms_err_timeout_cnt;
  260. uint16_t bms_err_cnt;
  261. uint16_t bms_err_num;
  262. }BMS_ERROR;
  263. #define TI_VALID_FLAG_KEY (0x0327)
  264. typedef struct
  265. {
  266. uint16_t ti_flag;
  267. uint16_t ti_set;
  268. BMS_ERROR ti_bms_1;
  269. BMS_ERROR ti_bms_2;
  270. }TEST_INFO;
  271. extern TEST_INFO test_info;
  272. int8_t Handle_RS485_1_Data(void);
  273. int8_t Handle_RS485_2_Data(void);
  274. void Sub_BMS_1_Initial(void);
  275. shark_battery_exit_t Is_Sub_BMS_1_Normal(void);
  276. shark_battery_exit_t Is_Sub_BMS_2_Normal(void);
  277. void Initial_Test_Info(void);
  278. void Save_Test_Info(void);
  279. void Ca_Chu_Test_Info(void);
  280. void Check_Enable_Test_Info(void);
  281. shark_battery_exit_t shark_battery_is_normal(SUB_BMS_INFO *info);
  282. void shark_bms_set_vgs(shark_bms_vgs_t vgs, shark_bool enable);
  283. void shark_bms_set_mos(shark_bool ss, shark_bool s11, shark_bool s21);
  284. shark_battery_mask_t shark_battery_set_power(shark_battery_power_t power1, shark_battery_power_t power2, shark_battery_mask_t mask);
  285. shark_battery_power_t shark_battery_get_power(SUB_BMS_INFO *info);
  286. shark_bool shark_battery_check_power(SUB_BMS_INFO *info, shark_battery_power_t power);
  287. shark_battery_mask_t shark_battery_detect(u8 times);
  288. shark_bool shark_battery_ping(u8 times);
  289. shark_bool shark_battery_is_normal_power_on(SUB_BMS_INFO *info);
  290. u32 shark_battery_get_voltage(SUB_BMS_INFO *info);
  291. u32 shark_battery_get_voltage_delta(void);
  292. u32 shark_battery_get_voltage_min(void);
  293. u32 shark_battery_get_voltage_max(void);
  294. void shark_battery_clear(SUB_BMS_INFO *info);
  295. shark_bool shark_battery_process(SUB_BMS_INFO *info, const u8 *buff, u8 length);
  296. shark_bool shark_battery_send_command(SUB_BMS_INFO *info);
  297. static inline void shark_bms_set_mos_close(void)
  298. {
  299. shark_bms_set_mos(shark_false, shark_false, shark_false);
  300. }
  301. static inline void shark_bms_set_mos_bat1(void)
  302. {
  303. shark_bms_set_mos(shark_false, shark_false, shark_true);
  304. }
  305. static inline void shark_bms_set_mos_bat2(void)
  306. {
  307. shark_bms_set_mos(shark_false, shark_true, shark_false);
  308. }
  309. static inline void shark_bms_set_mos_series(void)
  310. {
  311. shark_bms_set_mos(shark_true, shark_false, shark_false);
  312. }
  313. static inline void shark_bms_set_mos_parrallel(void)
  314. {
  315. shark_bms_set_mos(shark_false, shark_true, shark_true);
  316. }
  317. static inline shark_bool shark_battery_is_power_full(SUB_BMS_INFO *info)
  318. {
  319. return SHARK_BOOL((info->packet_common.bms_status & BMS_POWER_FULL) == BMS_POWER_FULL);
  320. }
  321. static inline u32 shark_battery_get_voltage1(void)
  322. {
  323. return shark_battery_get_voltage(&sub_bms_info_1);
  324. }
  325. static inline u32 shark_battery_get_voltage2(void)
  326. {
  327. return shark_battery_get_voltage(&sub_bms_info_2);
  328. }
  329. static inline shark_bool shark_battery_send_pending(SUB_BMS_INFO *info)
  330. {
  331. return info->send_state == SHARK_SEND_PENDING;
  332. }
  333. static inline shark_bool shark_battery_send_success(SUB_BMS_INFO *info)
  334. {
  335. return info->send_state == SHARK_SEND_SUCCESS;
  336. }
  337. #endif