fly_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*-------------------------------------------------------------------------------------------------
  2. ** module: samkoon communication
  3. ** com : 485\Modbus Rtu
  4. ** data : 2022-4-15
  5. ** by : xie
  6. -------------------------------------------------------------------------------------------------*/
  7. #include "fly_uart.h"
  8. INT16U misc_info_buf[BUF_LEN_128] = {0};
  9. INT16U cell_temp_buf[BUF_LEN_512] = {0};
  10. INT16U samkoon_txbuf[BUF_LEN_128] = {0};
  11. /*---------------------------------------
  12. ** func :Count_CRC
  13. ** brief:CRC cal
  14. ** para :addr: 数据指针 num: 数量
  15. ---------------------------------------*/
  16. unsigned short Count_CRC(const INT8U *addr, int num)
  17. {
  18. unsigned short crc = 0xFFFF;
  19. int i;
  20. while (num--)
  21. {
  22. crc ^= *addr++;
  23. for (i = 0; i < 8; i++)
  24. {
  25. crc = (crc & 0x0001) ? ((crc >> 1) ^ 0xA001) : (crc >> 1);
  26. }
  27. }
  28. return crc;
  29. }
  30. /*--------------------------------------------
  31. ** func :ImportantData_TxGet
  32. ** brief:copy send data;
  33. soc 1th start from 0x4001;
  34. ** para : samkoon_txbuf\misc_info_buf for use
  35. --------------------------------------------*/
  36. void ImportantData_TxGet(INT16U pos, INT16U cnt, INT8U *des_data, INT8U *src_data)
  37. {
  38. INT8U i = 0;
  39. pos = pos - 0x4001; // addr offset 4300 hex
  40. src_data = src_data + pos;
  41. des_data = des_data + 6; // cell txbuf offset:6
  42. for (i = pos; i < cnt; i++)
  43. {
  44. *des_data++ = *src_data++;
  45. }
  46. }
  47. /*---------------------------------------
  48. ** func :Cell_Info_ReadOut
  49. ** brief:copy cell data
  50. 1th start from 0x4301
  51. ** para :none
  52. ---------------------------------------*/
  53. void Cell_Info_ReadOut(INT16U *p)
  54. {
  55. INT8U i, j;
  56. for (i = 0; i < 6; i++)
  57. {
  58. for (j = 0; j < 6; j++)
  59. {
  60. *p++ = 3300;
  61. }
  62. }
  63. // for(i = 0;i < config_get_slave_num(); i++) // without offset
  64. // {
  65. // for(j = 0;j < bmu_get_cell_num(i); j++)
  66. // {
  67. // *p++ = 3300; //bmu_get_cell_vol(i,j);
  68. // }
  69. // }
  70. }
  71. /*---------------------------------------
  72. ** func :Cell_Info_TxGet
  73. ** brief: none
  74. ** para :position \ cnt
  75. ---------------------------------------*/
  76. void Cell_Info_TxGet(INT16U pos, INT16U cnt, INT8U *des_data, INT8U *src_data)
  77. {
  78. INT8U i = 0;
  79. pos = pos - 0x4300; // addr offset 4300 hex
  80. src_data = src_data + pos;
  81. des_data = des_data + 6; // cell txbuf offset:6
  82. for (i = pos; i < cnt; i++)
  83. {
  84. *des_data++ = *src_data++;
  85. }
  86. }
  87. /*---------------------------------------
  88. ** func :Temperature_ReadOut
  89. ** brief:copy temperature data,
  90. 1th start from 0x55C1
  91. offset -40
  92. ** para :none
  93. ---------------------------------------*/
  94. void Temperature_ReadOut(INT16U *p)
  95. {
  96. INT8U i, j;
  97. for (i = 0; i < 6; i++)
  98. {
  99. for (j = 0; j < 6; j++)
  100. {
  101. *p++ = 20;
  102. }
  103. }
  104. // for(i = 0;i < config_get_slave_num(); i++) // without offset //PackNum = (total + (singleNum - 1))/singleNum
  105. // {
  106. // for(j = 0;j < bmu_get_temp_num(i); j++)
  107. // {
  108. // *p++ = bmu_get_cell_temp(i,j);
  109. // }
  110. // }
  111. }
  112. /*---------------------------------------
  113. ** func :Cell_Info_TxGet
  114. ** brief: none
  115. ** para :position \ cnt
  116. ---------------------------------------*/
  117. void Temperature_TxGet(INT16U pos, INT16U cnt, INT8U *des_data, INT8U *src_data)
  118. {
  119. INT8U i = 0;
  120. pos = pos - 0x55c0; // addr offset 55c0 hex
  121. src_data = src_data + pos;
  122. des_data = des_data + 6; // tmp txbuf offset:6
  123. for (i = pos; i < cnt; i++)
  124. {
  125. *des_data++ = *src_data++;
  126. }
  127. }
  128. /*---------------------------------------
  129. ** func :Rtu_TxData_Get()
  130. ** brief: none
  131. ** para :position \ cnt
  132. ---------------------------------------*/
  133. void Rtu_TxData_Get(INT16U pos,
  134. INT16U cnt,
  135. INT16U addr_start,
  136. INT8U fotmat_offset, // fotmat_offset:3
  137. INT8U *des_data,
  138. INT8U *src_data)
  139. {
  140. INT8U i = 0;
  141. if (pos < addr_start)
  142. return;
  143. pos = pos - addr_start; // addr offset 55c0 hex
  144. src_data = src_data + pos * 2;
  145. // des_data = des_data + fotmat_offset;
  146. des_data = (INT8U *)des_data + fotmat_offset;
  147. for (i = 2 * pos; i < 2 * cnt; i++)
  148. {
  149. *des_data++ = *src_data++;
  150. }
  151. }
  152. /*---------------------------------------
  153. ** func :Crc_Write()
  154. ** brief: none
  155. ** para :
  156. ---------------------------------------*/
  157. void Crc_Write(INT8U *p, INT16U data, INT8U count)
  158. {
  159. p = p + (2 * count + 3);
  160. *p++ = (INT8U)data;
  161. *p++ = (INT8U)(data >> 8);
  162. }
  163. /*---------------------------------------
  164. ** func :Small2Big()
  165. ** brief: none
  166. ** para :
  167. ---------------------------------------*/
  168. void Small2Big(INT16U *buf, INT16U count)
  169. {
  170. INT8U i = 0;
  171. INT8U *p = (INT8U *)buf;
  172. INT8U *p_next = (INT8U *)buf + 1;
  173. char tmp;
  174. for (i = 0; i < 2 * count; i++)
  175. {
  176. tmp = *p;
  177. *p = *p_next;
  178. *p_next = tmp;
  179. p = p + 2;
  180. p_next = p_next + 2;
  181. }
  182. }
  183. /*---------------------------------------
  184. ** func :Modbus_Head_Copy
  185. ** brief: none
  186. ** para :
  187. ---------------------------------------*/
  188. void Modbus_Head_Copy(INT8U *des_data, INT8U *src_data)
  189. {
  190. INT8U i = 0;
  191. for (i = 0; i < 2; i++)
  192. {
  193. *des_data++ = *src_data++; // addr fun_code
  194. }
  195. src_data = src_data + 3;
  196. *des_data = (*src_data) * 2;
  197. // for(i = 0; i < 1; i++)
  198. // {
  199. // *des_data++ = *src_data++; //count
  200. // }
  201. }
  202. /*----------------------------------------------
  203. ** @func : samkoon_com_work
  204. ** @brief : BMS with samkoon communication
  205. ** format : xx xx xx xx xx xx xx xx
  206. addr func -reg- -count- -crc-
  207. ans: xx xx xx xx xx....xx xx xx
  208. addr func count- --data-- -crc-
  209. crc:small other:big
  210. -----------------------------------------------*/
  211. void samkoon_com_work(INT8U *rec_data, void (*tx_p)(const INT8U *buf, INT16U len))
  212. {
  213. INT16U register_addr_hmi;
  214. INT16U tmp_crc;
  215. volatile INT16U register_count;
  216. volatile INT8U illegal_flag = 0;
  217. INT8U function_code = 0;
  218. register_addr_hmi = (rec_data[REGISTER_ADDR_H] << 8) + rec_data[REGISTER_ADDR_L];
  219. register_count = (rec_data[COUNT_INDEX_H] << 8) + rec_data[COUNT_INDEX_L];
  220. function_code = rec_data[FUNCTION_CODE_INDEX];
  221. switch (function_code)
  222. {
  223. case FUNC_03:
  224. if ((register_addr_hmi >= MODBUS_ADDR_RACK_REMOTE) && (register_addr_hmi < MODBUS_ADDR_RACK_REMOTE_END))
  225. {
  226. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  227. // Important_Bin_ReadOut(misc_info_buf);
  228. Small2Big(misc_info_buf, register_count);
  229. Rtu_TxData_Get(register_addr_hmi, register_count, 0xc9, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  230. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  231. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  232. // samkoon_txbuf[register_count + 2] = tmp_crc;
  233. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  234. }
  235. else if ((register_addr_hmi >= MODBUS_ADDR_SOC) && (register_addr_hmi < MODBUS_RACK_END))
  236. {
  237. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  238. // ImportantData_Readout(misc_info_buf);
  239. Small2Big(misc_info_buf, register_count);
  240. Rtu_TxData_Get(register_addr_hmi, register_count, 0x4101, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  241. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  242. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  243. // samkoon_txbuf[register_count + 2] = tmp_crc;
  244. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  245. }
  246. else if ((register_addr_hmi >= MODBUS_ADDR_CELL) && (register_addr_hmi < MODBUS_CELL_END))
  247. {
  248. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  249. Cell_Info_ReadOut(cell_temp_buf); // read all cell data
  250. Small2Big(cell_temp_buf, register_count);
  251. Rtu_TxData_Get(register_addr_hmi, register_count, 0x4301, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
  252. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  253. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  254. // samkoon_txbuf[register_count + 2] = tmp_crc;
  255. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  256. }
  257. else if ((register_addr_hmi >= MODBUS_ADDR_TEMP) && (register_addr_hmi < MODBUS_TEMP_END))
  258. {
  259. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  260. Temperature_ReadOut(cell_temp_buf); // read all temp data
  261. Small2Big(cell_temp_buf, register_count);
  262. Rtu_TxData_Get(register_addr_hmi, register_count, 0x55c1, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
  263. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  264. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  265. // samkoon_txbuf[register_count + 2] = tmp_crc;
  266. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  267. }
  268. else if ((register_addr_hmi >= MODBUS_ADDR_READ_KA) && (register_addr_hmi < MODBUS_KA_END))
  269. {
  270. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  271. // Relay_Bin_Readout(misc_info_buf);
  272. Small2Big(misc_info_buf, register_count);
  273. Rtu_TxData_Get(register_addr_hmi, register_count, 0x1001, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  274. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  275. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  276. // samkoon_txbuf[register_count + 2] = tmp_crc;
  277. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  278. }
  279. break;
  280. /* do other work... */
  281. case FUNC_05:
  282. break;
  283. default:
  284. illegal_flag = 1;
  285. break;
  286. }
  287. }
  288. void fly_uart1_task(void)
  289. {
  290. INT8U err = 0;
  291. // INT8U test_buf[20] = {0};
  292. UartFrame_TypeDef *msg;
  293. INT16U crc_check = 0;
  294. INT16U rcv_crc = 0;
  295. txfun_p Modbus_RTU_Send = Uart1_dma_Send_Data;
  296. while (1)
  297. {
  298. OSTimeDly(200);
  299. iwdg_feed(UART1_DOG);
  300. msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
  301. if ((err == OS_ERR_NONE) && (msg->len >= 2))
  302. {
  303. crc_check = Count_CRC(msg->buf, 6);
  304. rcv_crc = ((msg->buf[7] << 8) | msg->buf[6]);
  305. if (crc_check != rcv_crc)
  306. {
  307. continue;
  308. }
  309. samkoon_com_work(&(msg->buf[0]), Modbus_RTU_Send);
  310. }
  311. }
  312. }
  313. // void bms_uart0_task(void)
  314. //{
  315. // INT8U err = 0;
  316. // INT8U test_buf[20] = {0};
  317. // UartFrame_TypeDef *msg;
  318. // while(1)
  319. // {
  320. // wtdg_feed(UART0_DOG);
  321. // msg = (UartFrame_TypeDef *)OSMboxPend(uart0_mbox, 50, &err);
  322. // if((err == OS_ERR_NONE) && (msg->len >= 2))
  323. // {
  324. // if((msg->buf[0] == 0x00) && (msg->buf[1] == 0xAA) && (msg->buf[2] == 0xBB))
  325. // {
  326. // test_buf[0] = 0x00;
  327. // test_buf[1] = 0xBB;
  328. // test_buf[2] = 0xAA;
  329. // Uart0_dma_Send_Data(test_buf, 3);
  330. // }
  331. // }
  332. // }
  333. // }