#include "sys_data.h" #include "led.h" #include "usart.h" USART_MSG_t usartMsg; uint8_t result ; // 校验码 unsigned char CheckXor(const uint8_t *strData,int len) { char checksum = 0; for (int i = 0;i < len;i++) { checksum = checksum ^ strData[i]; } return (unsigned char)checksum; } void call_back() { // 检测发送完成标志位 if (RESET != USART_GetFlagStatus(USART_232,USART_FLAG_RXNE)) { if (usartMsg.rxIndex < _USART3_RXSIZE - 1){ usartMsg.rxBuf[usartMsg.rxIndex] = USART_ReceiveData(USART_232); usartMsg.rxIndex++; } else USART_ReceiveData(USART_232); } // 检测空闲标志位 if ((usartMsg.rxIndex > 0) && RESET != USART_GetFlagStatus(USART_232, USART_FLAG_IDLE)) { uint8_t i; usartMsg.done = 1; // 清除空闲中断 i = USART_232->SR; // 先读取SR寄存器 i = USART_232->DR; // 再读取DR寄存器 } else { uint8_t i; // 清除空闲中断 i = USART_232->SR; // 先读取SR寄存器 i = USART_232->DR; // 再读取DR寄存器 } } // 发送回馈信息 int sendCmd(uint8_t *data, uint16_t size) { while(usartMsg.txBusy == 1) { delay_ms(10); } usartMsg.txBusy = 1; memset(usartMsg.rxBuf, 0, _USART3_RXSIZE); usartMsg.rxIndex = 0; delay_ms(1); for (uint16_t i = 0; i < size; i++) { USART_SendData(USART_232,data[i]); while (USART_GetFlagStatus(USART_232, USART_FLAG_TXE) == RESET); } while (RESET == USART_GetFlagStatus(USART_232, USART_FLAG_TC)); usartMsg.done=0; usartMsg.txBusy = 0; return 1; } // 文件更新升级 uint8_t updata() { return 1; } void broad_to_host() { int i, j; result = 2;// 初始化为 2 表示操作失败(硬件故障或其他原因) // 反馈信息 uint8_t txData[8]; // 校验位验证信息 uint8_t crc[1 + (4 * usartMsg.rxBuf[4])]; // 判断帧头帧尾 if(usartMsg.rxBuf[0] != 0xAA || usartMsg.rxBuf[1] != 0xFF || usartMsg.rxBuf[2] != 0x55 || usartMsg.rxBuf[3] != 0xCC|| usartMsg.rxBuf[10 + (4 * (usartMsg.rxBuf[4] - 1))] != 0xDD || usartMsg.rxBuf[11 + (4 * (usartMsg.rxBuf[4] - 1))] != 0xFF) { result = 0x04;//帧头/帧尾异常 goto __feedback; } // 判断指令数 if(usartMsg.rxBuf[4] < 1 || usartMsg.rxBuf[4] > 255) { result = 5;//帧内容异常(指令值超出范围等) goto __feedback; } // 判断校验位 // 提取指令数 crc[0] = usartMsg.rxBuf[4]; // 提取指令码、channel、平台号、指令值 for(j = 0; j < usartMsg.rxBuf[4] * 4;j += 4) { crc[1 + j] = usartMsg.rxBuf[5+ j];//指令码 crc[2 + j] = usartMsg.rxBuf[6+ j];//channel crc[3 + j] = usartMsg.rxBuf[7+ j];//平台号 crc[4 + j] = usartMsg.rxBuf[8+ j];//指令值 } if((CheckXor(crc,1 + (4 * usartMsg.rxBuf[4])) != usartMsg.rxBuf[9 + (4 * (usartMsg.rxBuf[4] - 1))])) { result = 0x03;//校验位异常 goto __feedback; } // 执行指令 for(i = 0; i < usartMsg.rxBuf[4] * 4;i += 4) { // 判断指令码 switch (usartMsg.rxBuf[5 + i]) { case 0x01: // LED指令 result = channel_LED(usartMsg.rxBuf[6 + i], i); if(result != 1) goto __feedback; break; case 0x02: // 12V BAT指令 result = channel_12VBAT(usartMsg.rxBuf[6 + i], i); if(result != 1) goto __feedback; break; case 0x03: // 5V BAT指令 channel和平台号固定为0x00 result = channel_5VBAT(usartMsg.rxBuf[8 + i]); if(result != 1)goto __feedback; break; case 0x04: // updata指令 if(usartMsg.rxBuf[8] == 1) { result = updata(); } break; default: result = 0x05;//帧内容异常(指令值超出范围等) goto __feedback; break; } } __feedback: txData[0] = 0xBB; // 帧头 txData[1] = 0xFF; // 帧头 txData[2] = 0x55; // 帧头 txData[3] = 0xCC; // 帧头 txData[4] = result; // 反馈值 txData[5] = CheckXor(&txData[4], 1); // 校验位 txData[6] = 0xEE; // 帧尾 txData[7] = 0xFF; // 帧尾 sendCmd(txData,8); }