AppTaskUart0.c 7.4 KB

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