fly_uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. }
  198. /*----------------------------------------------
  199. ** @func : samkoon_com_work
  200. ** @brief : BMS with samkoon communication
  201. ** format : xx xx xx xx xx xx xx xx
  202. addr func -reg- -count- -crc-
  203. ans: xx xx xx xx xx....xx xx xx
  204. addr func count- --data-- -crc-
  205. crc:small other:big
  206. -----------------------------------------------*/
  207. void samkoon_com_work(INT8U *rec_data, void (*tx_p)(const INT8U *buf, INT16U len))
  208. {
  209. INT16U register_addr_hmi;
  210. INT16U tmp_crc;
  211. volatile INT16U register_count;
  212. volatile INT8U illegal_flag = 0;
  213. INT8U function_code = 0;
  214. register_addr_hmi = (rec_data[REGISTER_ADDR_H] << 8) + rec_data[REGISTER_ADDR_L];
  215. register_count = (rec_data[COUNT_INDEX_H] << 8) + rec_data[COUNT_INDEX_L];
  216. function_code = rec_data[FUNCTION_CODE_INDEX];
  217. switch (function_code)
  218. {
  219. case FUNC_03:
  220. if ((register_addr_hmi >= MODBUS_ADDR_RACK_REMOTE) && (register_addr_hmi < MODBUS_ADDR_RACK_REMOTE_END))
  221. {
  222. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  223. // Important_Bin_ReadOut(misc_info_buf);
  224. Small2Big(misc_info_buf, register_count);
  225. Rtu_TxData_Get(register_addr_hmi, register_count, 0xc9, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  226. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  227. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  228. // samkoon_txbuf[register_count + 2] = tmp_crc;
  229. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  230. }
  231. else if ((register_addr_hmi >= MODBUS_ADDR_SOC) && (register_addr_hmi < MODBUS_RACK_END))
  232. {
  233. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  234. // ImportantData_Readout(misc_info_buf);
  235. Small2Big(misc_info_buf, register_count);
  236. Rtu_TxData_Get(register_addr_hmi, register_count, 0x4101, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  237. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  238. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  239. // samkoon_txbuf[register_count + 2] = tmp_crc;
  240. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  241. }
  242. else if ((register_addr_hmi >= MODBUS_ADDR_CELL) && (register_addr_hmi < MODBUS_CELL_END))
  243. {
  244. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  245. Cell_Info_ReadOut(cell_temp_buf); // read all cell data
  246. Small2Big(cell_temp_buf, register_count);
  247. Rtu_TxData_Get(register_addr_hmi, register_count, 0x4301, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
  248. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  249. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  250. // samkoon_txbuf[register_count + 2] = tmp_crc;
  251. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  252. }
  253. else if ((register_addr_hmi >= MODBUS_ADDR_TEMP) && (register_addr_hmi < MODBUS_TEMP_END))
  254. {
  255. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  256. Temperature_ReadOut(cell_temp_buf); // read all temp data
  257. Small2Big(cell_temp_buf, register_count);
  258. Rtu_TxData_Get(register_addr_hmi, register_count, 0x55c1, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
  259. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  260. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  261. // samkoon_txbuf[register_count + 2] = tmp_crc;
  262. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  263. }
  264. else if ((register_addr_hmi >= MODBUS_ADDR_READ_KA) && (register_addr_hmi < MODBUS_KA_END))
  265. {
  266. Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
  267. // Relay_Bin_Readout(misc_info_buf);
  268. Small2Big(misc_info_buf, register_count);
  269. Rtu_TxData_Get(register_addr_hmi, register_count, 0x1001, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
  270. tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
  271. Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
  272. // samkoon_txbuf[register_count + 2] = tmp_crc;
  273. tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
  274. }
  275. break;
  276. /* do other work... */
  277. case FUNC_05:
  278. break;
  279. default:
  280. illegal_flag = 1;
  281. break;
  282. }
  283. }
  284. // void fly_uart1_task(void)
  285. // {
  286. // INT8U err = 0;
  287. // // INT8U test_buf[20] = {0};
  288. // UartFrame_TypeDef *msg;
  289. // INT16U crc_check = 0;
  290. // INT16U rcv_crc = 0;
  291. // txfun_p Modbus_RTU_Send = Uart1_dma_Send_Data;
  292. // while (1)
  293. // {
  294. // OSTimeDly(200);
  295. // iwdg_feed(UART1_DOG);
  296. // msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
  297. // if ((err == OS_ERR_NONE) && (msg->len >= 2))
  298. // {
  299. // crc_check = Count_CRC(msg->buf, 6);
  300. // rcv_crc = ((msg->buf[7] << 8) | msg->buf[6]);
  301. // if (crc_check != rcv_crc)
  302. // {
  303. // continue;
  304. // }
  305. // samkoon_com_work(&(msg->buf[0]), Modbus_RTU_Send);
  306. // }
  307. // }
  308. // }
  309. void fly_uart1_task(void)
  310. {
  311. INT8U err = 0;
  312. INT8U test_buf[20] = {0};
  313. UartFrame_TypeDef *msg;
  314. while (1)
  315. {
  316. OSTimeDly(200);
  317. iwdg_feed(UART1_DOG);
  318. msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
  319. test_buf[0] = 0x01;
  320. test_buf[1] = 0xBB;
  321. test_buf[2] = 0xAA;
  322. Uart1_dma_Send_Data(test_buf, 3);
  323. if ((err == OS_ERR_NONE) && (msg->len >= 2))
  324. {
  325. if ((msg->buf[0] == 0x00) && (msg->buf[1] == 0xAA) && (msg->buf[2] == 0xBB))
  326. {
  327. test_buf[0] = 0x00;
  328. test_buf[1] = 0xBB;
  329. test_buf[2] = 0xAA;
  330. Uart1_dma_Send_Data(test_buf, 3);
  331. }
  332. }
  333. }
  334. }
  335. void fly_uart3_task(void)
  336. {
  337. INT8U err = 0;
  338. INT8U test_buf[20] = {0};
  339. UartFrame_TypeDef *msg;
  340. while (1)
  341. {
  342. OSTimeDly(200);
  343. iwdg_feed(UART3_DOG);
  344. msg = (UartFrame_TypeDef *)OSMboxPend(uart3_mbox, 50, &err);
  345. // test_buf[0] = 0x01;
  346. // test_buf[1] = 0xBB;
  347. // test_buf[2] = 0xAA;
  348. // Uart3_dma_Send_Data(test_buf, 3);
  349. if ((err == OS_ERR_NONE) && (msg->len >= 2))
  350. {
  351. if ((msg->buf[0] == 0x00) && (msg->buf[1] == 0xAA) && (msg->buf[2] == 0xBB))
  352. {
  353. test_buf[0] = 0x00;
  354. test_buf[1] = 0xBB;
  355. test_buf[2] = 0xAA;
  356. // Uart3_Send_Data(test_buf, 3);
  357. Uart3_dma_Send_Data(test_buf, 3);
  358. }
  359. }
  360. }
  361. }