UartTask.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /****************************************************************************
  2. *
  3. * Copy right: Qx.
  4. * File name: UartTask.c
  5. * Description: 串口任务
  6. * History: 2021-03-05
  7. *
  8. ****************************************************************************/
  9. #include "bsp.h"
  10. #include "bsp_custom.h"
  11. #include "osasys.h"
  12. #include "ostask.h"
  13. #include "queue.h"
  14. #include "ps_event_callback.h"
  15. #include "cmisim.h"
  16. #include "cmimm.h"
  17. #include "cmips.h"
  18. #include "sockets.h"
  19. #include "psifevent.h"
  20. #include "ps_lib_api.h"
  21. #include "lwip/netdb.h"
  22. //#include <cis_def.h>
  23. #include "debug_log.h"
  24. #include "slpman_ec616.h"
  25. #include "plat_config.h"
  26. #include "ec_tcpip_api.h"
  27. #include "hal_module_adapter.h"
  28. #include "UartTask.h"
  29. #include "MainTask.h"
  30. #include "app.h"
  31. #include "numeric.h"
  32. //全局变量输入区
  33. extern UINT32 Timer_count;
  34. extern volatile bool Sleep_flag;
  35. extern AppNVMDataType AppNVMData;
  36. //全局变量输出区
  37. UartReadMsgType UartReadMsg;
  38. osMutexId_t UartMutex = NULL;//Uart数据锁
  39. QueueHandle_t UartWriteCmdHandle = NULL;
  40. UINT8 BattChrgEndFlag;
  41. //
  42. extern ARM_DRIVER_USART Driver_USART1;
  43. static ARM_DRIVER_USART *USARTdrv = &Driver_USART1;
  44. volatile bool isRecvTimeout = false;
  45. volatile bool isRecvComplete = false;
  46. //线程声明区
  47. static StaticTask_t gProcess_Uart_Task_t;
  48. static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
  49. static osThreadId_t UartTaskId = NULL;
  50. static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
  51. #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
  52. //函数声明区
  53. void USART_callback(uint32_t event);
  54. UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer);
  55. UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData);
  56. UINT16 crc_chk(UINT8* data, UINT8 length);
  57. BOOL BattHeaterSwitch(UINT8* heaterSwitch);
  58. //Uart线程任务区
  59. static void UartTask(void* arg)
  60. {
  61. USARTdrv->Initialize(USART_callback);
  62. USARTdrv->PowerControl(ARM_POWER_FULL);
  63. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  64. ARM_USART_DATA_BITS_8 |
  65. ARM_USART_PARITY_NONE |
  66. ARM_USART_STOP_BITS_1 |
  67. ARM_USART_FLOW_CONTROL_NONE, 9600);
  68. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  69. UINT16 Reg_Num = 0;
  70. UINT16 Uart_Uds_LEN;
  71. UINT16 Uart_Recv_LEN;
  72. Uart_Read_Msg_Type Uart_Read_Msg;
  73. Uart_Write_Data_Type UartWriteData; //Uart控制命令
  74. if(UartWriteCmdHandle == NULL)//Uart控制命令传输指针
  75. {
  76. UartWriteCmdHandle = osMessageQueueNew(3,sizeof(Uart_Write_Data_Type), NULL);
  77. }
  78. if(UartMutex == NULL)
  79. {
  80. UartMutex = osMutexNew(NULL);
  81. }
  82. //上电起始控制区域
  83. UartWriteData.WriteCmd = 0x01;
  84. if(AppNVMData.isBattLocked)
  85. {
  86. UartWriteData.Data[0] = 0x00;
  87. UartWriteData.Data[1] = 0x00;
  88. }
  89. else
  90. {
  91. UartWriteData.Data[0] = 0x00;
  92. UartWriteData.Data[1] = 0x03;
  93. }
  94. osMessageQueuePut(UartWriteCmdHandle,&UartWriteData,0,1000);
  95. while (1)
  96. {
  97. switch (gProcess_Uart_Task)
  98. {
  99. case PROCESS_UART_STATE_IDLE:
  100. {
  101. osDelay(100);
  102. if(Sleep_flag)
  103. {
  104. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
  105. }
  106. else if(Timer_count%10==0)
  107. {
  108. #ifdef USING_PRINTF
  109. printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);
  110. #endif
  111. if(osMessageQueueGet(UartWriteCmdHandle,&UartWriteData,0,0)==osOK)
  112. {
  113. #ifdef USING_PRINTF
  114. printf("[%d]UartWriteCmdHandle :%x\n",__LINE__,UartWriteData.WriteCmd);
  115. #endif
  116. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
  117. }
  118. else
  119. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
  120. }
  121. break;
  122. }
  123. case PROCESS_UART_STATE_READ:
  124. {
  125. osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
  126. Reg_Num = 0x21+BATT_CELL_VOL_NUM+BATT_TEMP_NUM + 2;//按照协议里面的0x21+X+N的结束地址
  127. Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
  128. Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
  129. Uart_Read_Msg.Reg_Begin_H = 0x00;
  130. Uart_Read_Msg.Reg_Begin_L= 0x00;
  131. Uart_Read_Msg.Reg_Num_H = Reg_Num>>8;
  132. Uart_Read_Msg.Reg_Num_L = Reg_Num;
  133. Uart_Uds_LEN = Reg_Num*2;
  134. memset(UartReadMsg.Header,0x00,Uart_Uds_LEN);
  135. Uart_Recv_LEN = Uart_Transmit_func((UINT8 *)&Uart_Read_Msg,UartReadMsg.Header);
  136. UartReadMsg.len = Uart_Recv_LEN;
  137. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  138. if((UartReadMsg.data[(0x03+BATT_CELL_VOL_NUM)*2+1]&0x03)==0x02)
  139. {
  140. BattChrgEndFlag=TRUE;
  141. }
  142. else
  143. {
  144. BattChrgEndFlag=FALSE;
  145. }
  146. osMutexRelease(UartMutex);
  147. break;
  148. }
  149. case PROCESS_UART_STATE_WRITE:
  150. {
  151. Uart_WriteCmd_func(UartWriteData);
  152. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  153. break;
  154. }
  155. case PROCESS_UART_STATE_SLEEP:
  156. {
  157. USARTdrv->PowerControl(ARM_POWER_LOW);
  158. while(TRUE)
  159. {
  160. osDelay(60000/portTICK_PERIOD_MS);
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. //Uart线程初始化
  168. void UartTaskInit(void *arg)
  169. {
  170. osThreadAttr_t task_attr;
  171. memset(&task_attr,0,sizeof(task_attr));
  172. memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
  173. task_attr.name = "Uart_Task";
  174. task_attr.stack_mem = gProcess_Uart_TaskStack;
  175. task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
  176. task_attr.priority = osPriorityBelowNormal7;
  177. task_attr.cb_mem = &gProcess_Uart_Task_t;
  178. task_attr.cb_size = sizeof(StaticTask_t);
  179. UartTaskId = osThreadNew(UartTask, NULL, &task_attr);
  180. }
  181. void UartTaskDeInit(void *arg)
  182. {
  183. osThreadTerminate(UartTaskId);
  184. UartTaskId = NULL;
  185. }
  186. //函数区
  187. //Uart回调程序
  188. void USART_callback(uint32_t event)
  189. {
  190. if(event & ARM_USART_EVENT_RX_TIMEOUT)
  191. {
  192. isRecvTimeout = true;
  193. }
  194. if(event & ARM_USART_EVENT_RECEIVE_COMPLETE)
  195. {
  196. isRecvComplete = true;
  197. }
  198. }
  199. //Uart校验程序
  200. UINT16 crc_chk(UINT8* data, UINT8 length)
  201. {
  202. UINT8 j;
  203. UINT16 reg_crc=0xFFFF;
  204. while(length--)
  205. {
  206. reg_crc ^= *data++;
  207. for(j=0;j<8;j++)
  208. {
  209. if(reg_crc & 0x01)
  210. {
  211. reg_crc=(reg_crc>>1) ^ 0xA001;
  212. }
  213. else
  214. {
  215. reg_crc=reg_crc >>1;
  216. }
  217. }
  218. }
  219. return reg_crc;
  220. }
  221. //Uart写命令函数
  222. UINT8 Uart_WriteCmd_func(Uart_Write_Data_Type UartWriteData)
  223. {
  224. Uart_Write_Msg_Type Uart_Write_Msg;
  225. UINT16 RegAddress = 0x0000;
  226. UINT16 CRC_chk_buffer;
  227. UINT8 timeout = 0x00;
  228. UINT8 Uart_Recv_Buffer[8];
  229. switch (UartWriteData.WriteCmd)
  230. {
  231. case 0x01:
  232. {
  233. RegAddress = 0x1B + BATT_CELL_VOL_NUM+BATT_TEMP_NUM+BATT_OTHER_TEMP_NUM;
  234. Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
  235. Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
  236. Uart_Write_Msg.Reg_Begin_H = RegAddress>>8;
  237. Uart_Write_Msg.Reg_Begin_L = RegAddress;
  238. Uart_Write_Msg.Reg_Num_H = 0x00;
  239. Uart_Write_Msg.Reg_Num_L = 0x01;
  240. Uart_Write_Msg.Data_Count = 0x02;//要写入的字节数
  241. memcpy(Uart_Write_Msg.Data,UartWriteData.Data,2);
  242. CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg)-2);
  243. Uart_Write_Msg.CRC_L = CRC_chk_buffer;
  244. Uart_Write_Msg.CRC_H = CRC_chk_buffer>>8;
  245. break;
  246. }
  247. default:
  248. {
  249. UartWriteData.WriteCmd = 0x00;
  250. return 0;
  251. break;
  252. }
  253. }
  254. USARTdrv->Send((UINT8 *)&Uart_Write_Msg,sizeof(Uart_Write_Msg));
  255. #ifdef USING_PRINTF
  256. printf("Uart_Send_buffer: ");
  257. for(int i=0;i<sizeof(Uart_Write_Msg);i++)
  258. {
  259. printf("%x ",*((UINT8 *)&Uart_Write_Msg+i));
  260. }
  261. printf("\n");
  262. #endif
  263. USARTdrv->Receive(Uart_Recv_Buffer,8);
  264. while((isRecvTimeout == false) && (isRecvComplete == false))
  265. {
  266. timeout++;
  267. osDelay(100);
  268. if (timeout>=10)
  269. {
  270. timeout =0;
  271. isRecvTimeout = true;
  272. break;
  273. }
  274. }
  275. if (isRecvComplete == true)
  276. {
  277. #ifdef USING_PRINTF
  278. printf("Uart_Rece_buffer: ");
  279. for(int i=0;i<8;i++)
  280. {
  281. printf("%x ",Uart_Recv_Buffer[i]);
  282. }
  283. printf("n");
  284. #endif
  285. isRecvComplete = false;
  286. if(Uart_Recv_Buffer[1]==0x10)
  287. {
  288. return UartWriteData.WriteCmd;
  289. }
  290. else
  291. {
  292. return 0x00;
  293. }
  294. }
  295. else
  296. {
  297. isRecvTimeout = false;
  298. return 0x00;
  299. }
  300. }
  301. //Uart发送接收函数
  302. UINT8 Uart_Transmit_func(UINT8* Uart_Read_Msg,UINT8* Uart_Recv_Buffer)
  303. {
  304. UINT16 CRC_Rece_buffer;
  305. UINT16 CRC_chk_buffer;
  306. UINT16 Data_Len ;
  307. UINT8 timeout = 0x00;
  308. Data_Len = (*(Uart_Read_Msg+4)|*(Uart_Read_Msg+5))*2+5;
  309. CRC_chk_buffer = crc_chk(Uart_Read_Msg,6);
  310. *(Uart_Read_Msg+6) = CRC_chk_buffer;
  311. *(Uart_Read_Msg+7) = CRC_chk_buffer>>8;
  312. USARTdrv->Send(Uart_Read_Msg,8);
  313. // #ifdef USING_PRINTF
  314. // printf("Uart_Send_buffer: ");
  315. // for(int i=0;i<8;i++)
  316. // {
  317. // printf("%x ",*(Uart_Read_Msg+i));
  318. // }
  319. // printf("\n");
  320. // #endif
  321. USARTdrv->Receive(Uart_Recv_Buffer,Data_Len);
  322. while((isRecvTimeout == false) && (isRecvComplete == false))
  323. {
  324. timeout++;
  325. osDelay(100);
  326. if (timeout>=50)
  327. {
  328. timeout =0;
  329. isRecvTimeout = true;
  330. break;
  331. }
  332. }
  333. // #ifdef USING_PRINTF
  334. // printf("Uart_Rece_buffer1: ");
  335. // for(int i=0;i<Data_Len;i++)
  336. // {
  337. // printf("%x ",*(Uart_Recv_Buffer+i));
  338. // }
  339. // #endif
  340. if (isRecvComplete == true)
  341. {
  342. isRecvComplete = false;
  343. CRC_Rece_buffer =*(Uart_Recv_Buffer+Data_Len-1)<<8|*(Uart_Recv_Buffer+Data_Len-2);
  344. CRC_chk_buffer = crc_chk(Uart_Recv_Buffer,Data_Len-2);
  345. // #ifdef USING_PRINTF
  346. // printf("Uart_Rece_buffer after Crc: ");
  347. // for(int i=0;i<Data_Len;i++)
  348. // {
  349. // printf("%x ",*(Uart_Recv_Buffer+i));
  350. // }
  351. // printf("\tcrcchk:%x,%x\n ",CRC_chk_buffer,CRC_Rece_buffer);
  352. // #endif
  353. if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
  354. {
  355. return Data_Len;//此处指针移位出现重启问题
  356. }
  357. else //接收数据的校验不过
  358. {
  359. USARTdrv->Uninitialize();
  360. osDelay(1000);
  361. USARTdrv->Initialize(USART_callback);
  362. USARTdrv->PowerControl(ARM_POWER_FULL);
  363. USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
  364. ARM_USART_DATA_BITS_8 |
  365. ARM_USART_PARITY_NONE |
  366. ARM_USART_STOP_BITS_1 |
  367. ARM_USART_FLOW_CONTROL_NONE, 9600);
  368. memset(Uart_Recv_Buffer,0xff,Data_Len);
  369. return 0;
  370. }
  371. }
  372. else
  373. {
  374. memset(Uart_Recv_Buffer,0x00,Data_Len);
  375. isRecvTimeout = false;
  376. return 0;
  377. }
  378. }
  379. //
  380. /**
  381. \fn BOOL BattHeaterSwitch(UINT8* heaterSwitch)
  382. \param[in] (UINT8*) heaterSwitch: the heater switch state
  383. \brief according to the current switch state and all the cell temp, it will turn on/off the switch
  384. \return (BOOL) isNeedtoSwitch: true: need to send cmd to turn on/off the switch
  385. false: do not need to do anything
  386. */
  387. BOOL BattHeaterSwitch(UINT8* heaterSwitch)
  388. {
  389. BOOL isNeedtoSwitch = FALSE;
  390. UINT8 battCellTemp[BATT_TEMP_NUM];
  391. UINT8 maxCellTemp,minCellTemp;
  392. UINT8 i =0;
  393. UINT8 currentSwitchState = 0;
  394. //get the current switch state and the cell temp
  395. currentSwitchState = UartReadMsg.data[(0x1C+BATT_CELL_VOL_NUM+(BATT_TEMP_NUM+2))*2+1]&0x01;
  396. for(int i=0; i<BATT_TEMP_NUM; i++)
  397. {
  398. battCellTemp[i] = UartReadMsg.data[(0x06+BATT_CELL_VOL_NUM+i)*2+1];
  399. }
  400. //cal the maxtemp and mintemp
  401. maxCellTemp = battCellTemp[0];
  402. minCellTemp = battCellTemp[0];
  403. for(i=1;i<BATT_TEMP_NUM;i++)
  404. {
  405. maxCellTemp = max(maxCellTemp,battCellTemp[i]);
  406. minCellTemp = min(minCellTemp,battCellTemp[i]);
  407. }
  408. if(currentSwitchState==0) //当前状态为关闭,判断是否应该开启
  409. {
  410. if(minCellTemp<=5+40 && maxCellTemp<25+40)//温度偏移为40
  411. {
  412. *heaterSwitch = 1;
  413. isNeedtoSwitch = true;
  414. }
  415. }
  416. else //当前状态为开启,判断是否应该关闭
  417. {
  418. if(minCellTemp>10+40||maxCellTemp>30+40)
  419. {
  420. *heaterSwitch = 0;
  421. isNeedtoSwitch= true;
  422. }
  423. }
  424. return isNeedtoSwitch;
  425. }
  426. UINT8 decryptionAlgorithm (UINT16 cipherText)
  427. {
  428. UINT16 plainText = 1;
  429. UINT16 publicKeyD = 43;
  430. UINT16 publicKeyN = 10961;
  431. cipherText = cipherText % publicKeyN;
  432. while(publicKeyD >0)
  433. {
  434. if(publicKeyD % 2 ==1)
  435. {
  436. plainText = plainText * cipherText % publicKeyN;
  437. }
  438. publicKeyD = publicKeyD/2;
  439. cipherText = (cipherText * cipherText) % publicKeyN;
  440. }
  441. return (UINT8)plainText;
  442. }
  443. UINT16 encryptionAlgorithm (UINT8 plainText)
  444. {
  445. UINT16 cipherText = 1;
  446. UINT16 privateKeyE = 37507;
  447. UINT16 privateKeyN = 10961;
  448. plainText = plainText % privateKeyN;
  449. while(privateKeyE >0)
  450. {
  451. if(privateKeyE % 2 ==1)
  452. {
  453. cipherText = cipherText * plainText % privateKeyN;
  454. }
  455. privateKeyE = privateKeyE/2;
  456. cipherText = (plainText * plainText) % privateKeyN;
  457. }
  458. return cipherText;
  459. }