AppTaskUart0.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-10-27
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\AppTaskUart0.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppTaskUart0.h"
  11. #ifdef APP_UART0_ENABLE
  12. static process_Uart0 gProcess_Uart0_Task = PROCESS_UART_STATE_IDLE;
  13. #define PROC_UART0_STATE_SWITCH(a) (gProcess_Uart0_Task = a)
  14. bool bmsCellInfoDecode(uint8 *dataPtr);
  15. bool bmsTempInfoDecode(uint8 *dataPtr);
  16. bool bmsOtherInfoDecode(uint8 *dataPtr);
  17. void UartBusReadDataFunc(uint16 RegAddrBegin, uint16 Reg_Num, bool (*decodeFunc)(uint8 *dataPtr));
  18. // void battDataCalFunc(void);
  19. static uint16 UartNoDataReadCounter = 0;
  20. void Uart0Task(void *pvParameters)
  21. {
  22. (void)pvParameters;
  23. UartAnsType UartAnsData;
  24. uint16 pReadLen = 0;
  25. static uint8 writecounter = 0;
  26. while (1)
  27. {
  28. switch (gProcess_Uart0_Task)
  29. {
  30. case PROCESS_UART_STATE_IDLE:
  31. {
  32. vTaskDelay(pdMS_TO_TICKS(10));
  33. if ((TimerCounter % 1000) == 0 && 1)
  34. {
  35. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_READ);
  36. }
  37. else if (TimerCounter % 100 == 0)
  38. {
  39. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
  40. }
  41. break;
  42. }
  43. case PROCESS_UART_STATE_READ:
  44. {
  45. /*01 04 00 01 00 38 A0 18
  46. 01 04 00 64 00 78 B1 F7
  47. 01 04 01 90 00 28 F1 C5 */
  48. Dio_FlipChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2);
  49. //其他参数读取
  50. UartBusReadDataFunc(0x01, 0x38, bmsOtherInfoDecode);
  51. //电压读取
  52. UartBusReadDataFunc(0x64, 120, bmsCellInfoDecode);
  53. //温度读取
  54. UartBusReadDataFunc(0x190, 40, bmsTempInfoDecode);
  55. //没有读到数据时的默认值
  56. if (UartNoDataReadCounter > 10)
  57. {
  58. memset((uint8 *)&UartAnsData, 0x00, sizeof(UartAnsData));
  59. bmsCellInfoDecode(UartAnsData.Data);
  60. bmsTempInfoDecode(UartAnsData.Data);
  61. bmsOtherInfoDecode(UartAnsData.Data);
  62. PutErrorNum(ErrorArray, sizeof(ErrorArray) / 2, 1);
  63. vTaskDelay(pdMS_TO_TICKS(2000));
  64. }
  65. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  66. break;
  67. }
  68. case PROCESS_UART_STATE_WRITE:
  69. {
  70. if (battSeparateEnable) //最多重新写5次
  71. {
  72. uint16 RegAddress = 0;
  73. uint16 CRC_chk_buffer = 0;
  74. uint8 WriteData[2] = {0x00, 0x00};
  75. uint8 UartRecvBuffer[10];
  76. if (!battSeparateCtlState)
  77. {
  78. WriteData[1] = 0x55;
  79. }
  80. else
  81. {
  82. WriteData[1] = 0xAA;
  83. }
  84. RegAddress = 100;
  85. UartWriteMsgType Uart_Write_Msg;
  86. Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
  87. Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
  88. Uart_Write_Msg.Reg_Begin_H = RegAddress >> 8;
  89. Uart_Write_Msg.Reg_Begin_L = RegAddress;
  90. Uart_Write_Msg.Reg_Num_H = 0x00;
  91. Uart_Write_Msg.Reg_Num_L = 0x01;
  92. Uart_Write_Msg.Data_Count = 0x02; //要写入的字节数
  93. memcpy(Uart_Write_Msg.Data, WriteData, 2);
  94. CRC_chk_buffer = crc_chk((uint8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg) - 2);
  95. Uart_Write_Msg.CRC_L = CRC_chk_buffer;
  96. Uart_Write_Msg.CRC_H = CRC_chk_buffer >> 8;
  97. UART_Query_Data(UART_LPUART0, UART_LPUART0, (uint8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg), UartRecvBuffer, &pReadLen, 500);
  98. if (pReadLen > 3 && *(UartRecvBuffer + 1) == 0x10)
  99. {
  100. writecounter = 0;
  101. battSeparateEnable = 0;
  102. }
  103. else
  104. {
  105. writecounter++;
  106. }
  107. }
  108. if (writecounter >= 5)
  109. {
  110. battSeparateEnable = 0;
  111. writecounter = 0;
  112. }
  113. PROC_UART0_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  114. break;
  115. }
  116. case PROCESS_UART_STATE_SLEEP:
  117. {
  118. break;
  119. }
  120. default:
  121. break;
  122. }
  123. }
  124. }
  125. bool bmsCellInfoDecode(uint8 *dataPtr) //电压数据解码
  126. {
  127. for (uint8 i = 0; i < BMS_CELLNUM; i++)
  128. {
  129. battCellU[i] = ((dataPtr[(0x00 + i) * 2] << 8) | dataPtr[(0x00 + i) * 2 + 1]);
  130. }
  131. return true;
  132. }
  133. bool bmsTempInfoDecode(uint8 *dataPtr) //温度数据解码
  134. {
  135. for (uint8 i = 0; i < BMS_TEMPNUM; i++)
  136. {
  137. battCellTemp[i] = dataPtr[(0x00 + i) * 2 + 1];
  138. }
  139. return true;
  140. }
  141. bool bmsOtherInfoDecode(uint8 *dataPtr) //其他数据解析
  142. {
  143. uint16 Batt_current;
  144. battPackVol = (dataPtr[(0x00) * 2]) << 8 | (dataPtr[(0x00) * 2 + 1]);
  145. Batt_current = (dataPtr[(0x01) * 2] << 8) | (dataPtr[(0x01) * 2 + 1]);
  146. if(battPackVol==0)
  147. {
  148. Batt_current=16000;
  149. }
  150. battI = Batt_current - 6000; //电流偏移量不同
  151. battSOC = dataPtr[(0x02) * 2 + 1];
  152. battSOH = dataPtr[(0x03) * 2 + 1];
  153. battWorkState = (dataPtr[(0x06) * 2 + 1]) & 0x03; //电池状态(原始数据),3表示开路,2表示放电,1表示充电
  154. switch (battWorkState) //电池状态(上传数据),0表示开路,1表示放电,2表示充电
  155. {
  156. case 0:
  157. battWorkState = 0;
  158. break;
  159. case 1:
  160. battWorkState = 2;
  161. break;
  162. case 2:
  163. battWorkState = 1;
  164. break;
  165. case 3:
  166. battWorkState = 0;
  167. break;
  168. default:
  169. break;
  170. }
  171. maxCellVol = (dataPtr[(0x13) * 2] << 8) | dataPtr[(0x13) * 2 + 1];
  172. minCellVol = (dataPtr[(0x16) * 2] << 8) | dataPtr[(0x16) * 2 + 1];
  173. AppConfigInfo.AppDataInfo.battDischrgAccEnrg = (dataPtr[(0x34) * 2] << 8) | dataPtr[(0x34) * 2 + 1];
  174. AppConfigInfo.AppDataInfo.battCycleTimes = AppConfigInfo.AppDataInfo.battDischrgAccEnrg / 49000;
  175. return true;
  176. }
  177. void UartBusReadDataFunc(uint16 RegAddrBegin, uint16 Reg_Num, bool (*decodeFunc)(uint8 *dataPtr))
  178. {
  179. // argv In
  180. UartQueryType Uart0AskMsg;
  181. uint16 ReadLen1 = 0;
  182. uint16 ReadLenTar = 0;
  183. uint8 ReadDelayCounter = 0;
  184. uint16 CRC_chk_buffer = 0;
  185. uint16 pReadLen;
  186. UartAnsType UartAnsData;
  187. uint8 UartDataRecv[256];
  188. pReadLen = 0;
  189. Uart0AskMsg.Bms_Address = BMS_ADDRESS_CODE;
  190. Uart0AskMsg.Bms_Funcode = UART_READ_CODE;
  191. Uart0AskMsg.Reg_Begin_H = RegAddrBegin >> 8;
  192. Uart0AskMsg.Reg_Begin_L = RegAddrBegin;
  193. Uart0AskMsg.Reg_Num_H = Reg_Num >> 8;
  194. Uart0AskMsg.Reg_Num_L = Reg_Num;
  195. CRC_chk_buffer = crc_chk((uint8 *)&Uart0AskMsg, 6);
  196. Uart0AskMsg.CRC_L = CRC_chk_buffer;
  197. Uart0AskMsg.CRC_H = CRC_chk_buffer >> 8;
  198. UART_Send_Data(UART_LPUART0, (uint8 *)&Uart0AskMsg, sizeof(Uart0AskMsg), 1000);
  199. ReadLenTar = (Reg_Num)*2 + 5;
  200. memset((uint8 *)&UartAnsData, 0x00, sizeof(UartAnsData));
  201. while (1)
  202. {
  203. UART_Receive_Data(UART_LPUART0, UartDataRecv, &pReadLen, 100);
  204. if (pReadLen > 2)
  205. {
  206. memcpy((uint8 *)&UartAnsData + ReadLen1, UartDataRecv, pReadLen);
  207. ReadLen1 = ReadLen1 + pReadLen;
  208. }
  209. else
  210. {
  211. ReadDelayCounter++;
  212. }
  213. if (ReadLen1 >= ReadLenTar)
  214. {
  215. ReadDelayCounter = 0;
  216. pReadLen = ReadLen1;
  217. ReadLen1 = 0;
  218. break;
  219. }
  220. if (ReadDelayCounter >= 20)
  221. {
  222. ReadDelayCounter = 0;
  223. pReadLen = 0;
  224. ReadLen1 = 0;
  225. break;
  226. }
  227. }
  228. if (pReadLen > 0)
  229. {
  230. uint16 CrcChkGet = 0xffff;
  231. uint16 CrcChkCal = 0x0000;
  232. CrcChkCal = crc_chk((uint8 *)&UartAnsData, pReadLen - 2);
  233. CrcChkGet = ((uint16)(UartAnsData.Data[pReadLen - 1 - 3]) << 8) | ((uint16)(UartAnsData.Data[pReadLen - 2 - 3]));
  234. if (CrcChkCal == CrcChkGet)
  235. {
  236. decodeFunc(UartAnsData.Data);
  237. UartNoDataReadCounter = 0;
  238. }
  239. else
  240. {
  241. UartNoDataReadCounter++;
  242. }
  243. }
  244. else
  245. {
  246. UartNoDataReadCounter++;
  247. }
  248. }
  249. #endif