app_rs485_2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "common.h"
  2. #include "drv_usart_2.h"
  3. #include "app_rs485_2.h"
  4. #include "app.h"
  5. #include "hardware_test.h"
  6. #include "app_end_ctr.h"
  7. //uart2
  8. static uint8_t app_rs485_buf[TX_2_BUFFER_SIZE];
  9. static uint8_t bms_addr = SUB_BMS_ADDRESS_2;
  10. SUB_BMS_COM sub_rs485_time_out_2;
  11. SUB_BMS_INFO sub_bms_info_2;
  12. uint8_t RS485_busy_2 = 0;
  13. uint8_t sub_bms_2_connect = 0;
  14. static int8_t Get_Check_Sum_2(uint16_t*value,uint8_t*data,uint16_t size)
  15. {
  16. uint32_t checksum;
  17. if((NULL==value)||(NULL==data)||(0==size))
  18. {
  19. return 0;
  20. }
  21. //
  22. checksum = 0;
  23. while(size>1)
  24. {
  25. checksum += *(uint16_t*)data;
  26. data += 2;
  27. size -= 2;
  28. }
  29. //
  30. if(size>0)
  31. {
  32. checksum += *data;
  33. }
  34. //
  35. while(checksum>>16)
  36. {
  37. checksum = (checksum&0xFFFF)+(checksum>>16);
  38. }
  39. //
  40. *value = (uint16_t)~checksum;
  41. return 1;
  42. }
  43. uint8_t SUB_BMS_2_DEC(void)
  44. {
  45. return sub_bms_2_connect;
  46. }
  47. void RS485_Communication_Time_Out_2(void)
  48. {
  49. if(sub_bms_info_2.rs485_connect)
  50. {
  51. sub_bms_info_2.conn_state = SUB_BMS_CONT_NO485;
  52. }
  53. else
  54. {
  55. sub_bms_info_2.conn_state = SUB_BMS_DISC_NO485;
  56. }
  57. if(sub_bms_info_2.rs485_time_out)
  58. return;
  59. memset(&sub_bms_info_2.packet_common,0x00,sizeof(sub_bms_info_2.packet_common));
  60. memset(&sub_bms_info_2.bat_dev_info,0x00,sizeof(sub_bms_info_2.bat_dev_info));
  61. memset(&sub_bms_info_2.bat_times,0x00,sizeof(sub_bms_info_2.bat_times));
  62. memset(&sub_bms_info_2.cell_vol,0x00,sizeof(sub_bms_info_2.cell_vol));
  63. memset(&sub_bms_info_2.temp_other,0x00,sizeof(sub_bms_info_2.temp_other));
  64. memset(&sub_bms_info_2.sub_bms_cmd,0x00,sizeof(sub_bms_info_2.sub_bms_cmd));
  65. sub_bms_info_2.rs485_time_out = 1;
  66. sub_bms_info_2.sub_bms_cmd.operate = OP_READ_INFO;
  67. //
  68. sub_bms_2_connect = 0;
  69. }
  70. static int8_t Handle_Sub_BMS_CMD_2(uint8_t *data)
  71. {
  72. static uint8_t send_times = 0;
  73. COMMAND_BODY *bms = (COMMAND_BODY *)data;
  74. int8_t bStatus;
  75. uint16_t count;
  76. SUB_BMS_INFO *temp_bms;
  77. do
  78. {
  79. if(bms->type != bms_addr || bms->dir != BMS_DIR || bms->bStatus == 0)
  80. break;
  81. memset(&sub_rs485_time_out_2,0x00,sizeof(sub_rs485_time_out_2));
  82. g_event &= ~SUB_BMS_2_RS485_DISC_EVENT;
  83. temp_bms = &sub_bms_info_2;
  84. temp_bms->rs485_time_out = 0;
  85. //
  86. sub_bms_2_connect = 1;
  87. RS485_busy_2 = 0;
  88. if(sub_bms_info_2.rs485_connect)
  89. {
  90. sub_bms_info_2.conn_state = SUB_BMS_CONT_HV485;
  91. }
  92. else
  93. {
  94. sub_bms_info_2.conn_state = SUB_BMS_DISC_HV485;
  95. }
  96. //CRC
  97. count = bms->checksum;
  98. bms->checksum = 0;
  99. bStatus = Get_Check_Sum_2((void*)&bms->checksum,data,bms->size);
  100. if(1!=bStatus || count != bms->checksum)
  101. {
  102. break;
  103. }
  104. //handle frame
  105. count = sizeof(COMMAND_BODY);
  106. memcpy(&temp_bms->packet_common,&data[count],sizeof(temp_bms->packet_common));
  107. count += sizeof(temp_bms->packet_common);
  108. //bang zi ce shi start
  109. //temp_bms ->packet_common.m_percent /= 2;
  110. //bang zi ce shi end
  111. if((temp_bms->packet_common.operate_result&0x01) != 0x01)
  112. break;
  113. switch((temp_bms->packet_common.operate_result&0xF0)>>4)
  114. {
  115. case OP_NONE:
  116. break;
  117. case OP_BOND:
  118. break;
  119. case OP_WRITE_SN:
  120. break;
  121. case OP_PAIR:
  122. break;
  123. case OP_UPDATE_PAIR:
  124. break;
  125. case OP_READ_INFO:
  126. memcpy(&temp_bms->bat_dev_info,&data[count],sizeof(temp_bms->bat_dev_info));
  127. count += sizeof(temp_bms->bat_dev_info);
  128. break;
  129. case OP_ALARM_TIMES:
  130. memcpy(&temp_bms->bat_times,&data[count],sizeof(temp_bms->bat_times));
  131. count += sizeof(temp_bms->bat_times);
  132. break;
  133. case OP_CELL_VOL:
  134. memcpy(&temp_bms->cell_vol,&data[count],sizeof(temp_bms->cell_vol));
  135. count += sizeof(temp_bms->cell_vol);
  136. break;
  137. case OP_TEMP_OTHER:
  138. memcpy(&temp_bms->temp_other,&data[count],sizeof(temp_bms->temp_other));
  139. count += sizeof(temp_bms->temp_other);
  140. break;
  141. case OP_OPEN_FET:
  142. break;
  143. case OP_CLEAR_PAIR:
  144. break;
  145. case OP_UPDATE_SOFTWARE_REQ:
  146. break;
  147. case OP_UPDATE_SOFTWARE:
  148. break;
  149. default:break;
  150. }
  151. if(((temp_bms->packet_common.operate_result&0xF0)>>4) == temp_bms->sub_bms_cmd.operate)
  152. {
  153. memset(&temp_bms->sub_bms_cmd,0x00,sizeof(temp_bms->sub_bms_cmd));
  154. ++send_times;
  155. if(send_times > 90)
  156. {
  157. temp_bms->sub_bms_cmd.operate = OP_ALARM_TIMES;
  158. send_times = 0;
  159. }
  160. else if(send_times > 60)
  161. {
  162. temp_bms->sub_bms_cmd.operate = OP_CELL_VOL;
  163. }
  164. else if(send_times > 30)
  165. {
  166. temp_bms->sub_bms_cmd.operate = OP_TEMP_OTHER;
  167. }
  168. else
  169. temp_bms->sub_bms_cmd.operate = OP_NONE;
  170. }
  171. return 1;
  172. }while(0);
  173. return 0;
  174. }
  175. int8_t Send_Sub_BMS_CMD_2(void)
  176. {
  177. int8_t bStatus;// 默认需要回复
  178. COMMAND_BODY*body = (COMMAND_BODY*)app_rs485_buf;
  179. uint8_t *buf = app_rs485_buf;
  180. SUB_BMS_INFO *temp_bms;
  181. temp_bms = &sub_bms_info_2;
  182. // 填帧头
  183. body->size = sizeof(COMMAND_BODY);
  184. body->type = bms_addr;
  185. body->protocol = 'B';
  186. body->cmd = SET_COMMAND;
  187. body->dir = BMS_DIR;
  188. body->checksum = 0;
  189. body->bStatus = 0;
  190. // 填帧数据
  191. //实时转速
  192. buf[body->size++] = 0;
  193. //本次电门打开已经行驶的里程
  194. buf[body->size++] = 0;
  195. buf[body->size++] = 0;
  196. buf[body->size++] = 0;
  197. buf[body->size++] = 0;
  198. //电门开关信号
  199. buf[body->size++] = 0;
  200. //操作
  201. buf[body->size++] = temp_bms->sub_bms_cmd.operate;
  202. //对码
  203. buf[body->size++] = cang_wei;
  204. buf[body->size++] = 0;
  205. buf[body->size++] = 0;
  206. buf[body->size++] = 0;
  207. //更换对码,新对码
  208. buf[body->size++] = 0;
  209. buf[body->size++] = 0;
  210. buf[body->size++] = 0;
  211. buf[body->size++] = 0;
  212. switch(temp_bms->sub_bms_cmd.operate)
  213. {
  214. case OP_NONE:
  215. break;
  216. case OP_BOND:
  217. break;
  218. case OP_WRITE_SN:
  219. switch(temp_bms->sub_bms_cmd.param)
  220. {
  221. case 3:
  222. buf[body->size++] = (uint8_t)(temp_bms->sub_bms_cmd.param);
  223. break;
  224. case 4:
  225. buf[body->size++] = (uint8_t)(temp_bms->sub_bms_cmd.param);
  226. break;
  227. }
  228. break;
  229. case OP_PAIR:
  230. break;
  231. case OP_UPDATE_PAIR:
  232. break;
  233. case OP_READ_INFO:
  234. break;
  235. case OP_ALARM_TIMES:
  236. break;
  237. case OP_CELL_VOL:
  238. break;
  239. case OP_OPEN_FET:
  240. buf[body->size++] = (uint8_t)(temp_bms->sub_bms_cmd.param);
  241. break;
  242. case OP_UPDATE_SOFTWARE_REQ:
  243. break;
  244. case OP_UPDATE_SOFTWARE:
  245. break;
  246. case OP_CLEAR_PAIR:
  247. break;
  248. default:break;
  249. }
  250. bStatus = Get_Check_Sum_2((void*)&body->checksum,(void*)body,body->size);
  251. if(1!=bStatus)
  252. {
  253. return 0;
  254. }
  255. Send_Data_2_RS485(buf,body->size);
  256. sub_rs485_time_out_2.set = 1;
  257. sub_rs485_time_out_2.count = 0;
  258. RS485_busy_2 = 1;
  259. return 1;
  260. }
  261. void Send_Sub_BMS_2_CMD(void)
  262. {
  263. if(RS485_busy_2)
  264. return;
  265. Send_Sub_BMS_CMD_2();
  266. }
  267. void Check_Sub_BMS_2(void)
  268. {
  269. if(SUB_BMS_2_DEC())
  270. {
  271. if(sub_bms_info_2.rs485_connect == 0)
  272. {
  273. sub_bms_info_2.rs485_connect = 1;
  274. sub_bms_info_2.sub_bms_cmd.operate = OP_READ_INFO;
  275. }
  276. }
  277. else
  278. {
  279. if(sub_bms_info_2.rs485_connect)
  280. {
  281. // memset(&sub_bms_info_2,0x00,sizeof(sub_bms_info_2));
  282. memset(&sub_rs485_time_out_2,0x00,sizeof(sub_rs485_time_out_2));
  283. RS485_busy_2 = 0;
  284. //sub_bms_2_lt_state = SUB_BMS_DISC_NO485;
  285. sub_bms_info_2.sub_bms_cmd.operate = OP_READ_INFO;
  286. if(cb_operate_state == CB_BAT2)
  287. {
  288. //Power_On_Normal(0);
  289. }
  290. }
  291. }
  292. }
  293. int8_t Handle_RS485_2_Data(void)
  294. {
  295. uint16_t len = Get_RS485_2_Data(app_rs485_buf,sizeof(app_rs485_buf));
  296. if(len != app_rs485_buf[0])
  297. {
  298. if(memcmp(app_rs485_buf,"zhengjiceshi",12) == 0 && ht_mode == 0)
  299. {
  300. Writer_HT_Flash(1);
  301. __set_FAULTMASK(1);
  302. NVIC_SystemReset();
  303. }
  304. //idra-test-start
  305. RS485_Print(0x23,app_rs485_buf,len);
  306. //idra-test-end
  307. return 0;
  308. }
  309. if((app_rs485_buf[0] > sizeof(COMMAND_BODY)))
  310. {
  311. if(!Handle_Sub_BMS_CMD_2(app_rs485_buf))
  312. {
  313. //idra-test-start
  314. RS485_Print(0x23,app_rs485_buf,len);
  315. //idra-test-end
  316. return 0;
  317. }
  318. }
  319. return 1;
  320. }
  321. void Sub_BMS_2_Initial(void)
  322. {
  323. Usart2_Initial();
  324. memset(&sub_rs485_time_out_2,0x00,sizeof(sub_rs485_time_out_2));
  325. memset(&sub_bms_info_2,0x00,sizeof(sub_bms_info_2));
  326. sub_bms_info_2.sub_bms_cmd.operate = OP_READ_INFO;
  327. }
  328. void Sub_BMS_2_lt_State(void)
  329. {
  330. switch(sub_bms_info_2.conn_state)
  331. {
  332. case SUB_BMS_INVALID_STATUS:
  333. break;
  334. case SUB_BMS_DISC_NO485:
  335. break;
  336. case SUB_BMS_DISC_HV485:
  337. break;
  338. case SUB_BMS_CONT_HV485:
  339. break;
  340. case SUB_BMS_CONT_NO485:
  341. break;
  342. }
  343. }
  344. uint8_t Is_Sub_BMS_2_Normal(void)
  345. {
  346. return shark_battery_is_normal(&sub_bms_info_2);
  347. }
  348. int8_t Operate_Sub_BMS_2_CD(uint8_t on)
  349. {
  350. uint8_t cd_fet,rtn;
  351. uint16_t i,timeout = RS485_COM_TIMEOUT;
  352. sub_bms_info_2.sub_bms_cmd.operate = OP_OPEN_FET;
  353. switch(on)
  354. {
  355. case 1:
  356. cd_fet = 0x03;
  357. break;
  358. case 0:
  359. cd_fet = 0x00;
  360. break;
  361. case 2:
  362. cd_fet = 0x04;
  363. timeout = 2000;
  364. break;
  365. default:
  366. cd_fet = 0x00;
  367. break;
  368. }
  369. sub_bms_info_2.sub_bms_cmd.param = cd_fet;
  370. g_event &= ~RS485_2_RECEIVE_END_EVENT;
  371. Send_Sub_BMS_CMD_2();
  372. cd_fet <<= 1;
  373. rtn = 0;
  374. for(i = 0; i < timeout/10;i++)
  375. {
  376. delay_1ms(10);
  377. if(g_event & RS485_2_RECEIVE_END_EVENT)
  378. {
  379. g_event &= ~RS485_2_RECEIVE_END_EVENT;
  380. Handle_RS485_2_Data();
  381. if(on != 2)
  382. {
  383. if((sub_bms_info_2.packet_common.bms_status & cd_fet) == cd_fet)
  384. {
  385. rtn = 1;
  386. }
  387. }
  388. else
  389. {
  390. rtn = 1;
  391. }
  392. break;
  393. }
  394. }
  395. return rtn;
  396. }
  397. uint8_t Sub_BMS_2_COM_Finish(void)
  398. {
  399. return sub_bms_info_1.conn_state;
  400. }