| | |
| | | UsartReceiveWaitIndex, |
| | | UsartReceiveWaitDataLen, |
| | | UsartReceiveWaitData, |
| | | UsartReceiveWaitChecksum |
| | | UsartReceiveWaitChecksum1, |
| | | UsartReceiveWaitChecksum2 |
| | | }UsartRecvPackState; |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | |
| | |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | } |
| | | |
| | | uint16_t checksum2 = 0; |
| | | u8 usart_checksum[2]; |
| | | void UsartParseDataHandler(uint8_t data) |
| | | { |
| | | static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0; |
| | | uint16_t checksum = 0; |
| | | |
| | | static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ; |
| | | static uint8_t index = 0; |
| | | |
| | | |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) { //若收到校验和包 |
| | | checksum = 0; |
| | | if(usart_receive_state == UsartReceiveWaitChecksum2) { //若收到校验和包 |
| | | checksum2 = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | checksum2 += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if(((data + checksum)&0xff) == 0xFF) //校验通过 |
| | | checksum2 += pack_cmd; |
| | | checksum2 += pack_length; |
| | | checksum2 += pack_index; |
| | | checksum2 += pack_datalen; |
| | | checksum2 += pack_msgtype; |
| | | checksum2=~checksum2; |
| | | usart_checksum[1] = data; |
| | | if(checksum2==(usart_checksum[0]|usart_checksum[1]<<8)) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | |
| | | delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(g_com_map[CNT_RESTART]==1) |
| | | { |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | usart_receive_state = UsartReceiveWaitChecksum1; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | }else if(usart_receive_state == UsartReceiveWaitChecksum1) { |
| | | usart_checksum[0] = data; |
| | | usart_receive_state = UsartReceiveWaitChecksum2; |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |