| | |
| | | #define CMD_WRITE 2 |
| | | #define CMD_REPLY 3 |
| | | #define EUART_RX_BUF_SIZE 100 |
| | | typedef enum{ UsartReceiveWaitHead0, |
| | | UsartReceiveWaitHead1, |
| | | UsartReceiveWaitMsgType, |
| | | UsartReceiveWaitLength, |
| | | UsartReceiveWaitCMD, |
| | | UsartReceiveWaitIndex, |
| | | UsartReceiveWaitDataLen, |
| | | UsartReceiveWaitData, |
| | | UsartReceiveWaitChecksum |
| | | }UsartRecvPackState; |
| | | typedef enum { UsartReceiveWaitHead0, |
| | | UsartReceiveWaitHead1, |
| | | UsartReceiveWaitMsgType, |
| | | UsartReceiveWaitLength, |
| | | UsartReceiveWaitCMD, |
| | | UsartReceiveWaitIndex, |
| | | UsartReceiveWaitDataLen, |
| | | UsartReceiveWaitData, |
| | | UsartReceiveWaitChecksum |
| | | } UsartRecvPackState; |
| | | uint8_t mUsartReceivePack[100] = {0}; |
| | | |
| | | void SendComMap(uint8_t data_length, uint8_t index) |
| | | { |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | static uint8_t send_frame[EUART_RX_BUF_SIZE]; |
| | | uint16_t checksum = 0; |
| | | send_frame[0] = 0x55; |
| | | send_frame[1] = 0xAA; |
| | | send_frame[2] = 0x03; |
| | | send_frame[3] = data_length+5; |
| | | send_frame[4] = CMD_REPLY; |
| | | send_frame[5] = index; |
| | | send_frame[6] = data_length; |
| | | memcpy(&send_frame[7], &g_com_map[index], data_length); |
| | | for(int i = 0; i<(data_length+5); i++) |
| | | { |
| | | checksum += send_frame[2+i]; |
| | | } |
| | | checksum = Checksum_u16(&send_frame[2],5+data_length); |
| | | memcpy(&send_frame[7+data_length],&checksum,2); |
| | | |
| | | UART_PushFrame(send_frame, data_length+9); |
| | | } |
| | | void UpdateProcess(uint8_t index) |
| | | { |
| | |
| | | __disable_irq(); |
| | | result = FLASH_Prepare(0x8004A38, 128); |
| | | if(result) |
| | | result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2); |
| | | result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2); |
| | | __enable_irq(); |
| | | printf("进入升级模式\r\n"); |
| | | g_com_map[CNT_UPDATE]=0; |
| | |
| | | delay_ms(100); |
| | | // STMFLASH_Write_NoCheck(0x8004A38,0xAAAA); |
| | | // Delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(index == 2*CNT_REBOOT) |
| | | { |
| | |
| | | g_com_map[MAP_SIGN_INDEX]=0; |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | if(index == 2*CNT_RESTART) |
| | | { |
| | | g_com_map[CNT_RESTART]=0; |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | |
| | | } |
| | | 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; |
| | | 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; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if((data + checksum)&0xff == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中 |
| | | if(mUsartReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | }else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | }else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | |
| | | if(usart_receive_state == UsartReceiveWaitChecksum) { //若收到校验和包 |
| | | checksum = 0; |
| | | for(int i = 0; i<pack_length-5; i++) { |
| | | checksum += mUsartReceivePack[i]; |
| | | } |
| | | checksum += pack_cmd; |
| | | checksum += pack_length; |
| | | checksum += pack_index; |
| | | checksum += pack_datalen; |
| | | checksum += pack_msgtype; |
| | | if((data + checksum)&0xff == 0xFF) //校验通过 |
| | | { |
| | | switch(pack_cmd) |
| | | { |
| | | case CMD_WRITE: |
| | | //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中 |
| | | if(mUsartReceivePack[0]==1) |
| | | UpdateProcess(pack_index); |
| | | memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen); |
| | | //返回一个error状态 |
| | | //SendComMap(pack_datalen,pack_index); |
| | | save_com_map_to_flash(); |
| | | delay_ms(100); |
| | | NVIC_SystemReset(); |
| | | break; |
| | | case CMD_READ: |
| | | //read包中data字节,即mUsartReceivePack[0]表示数据长度; |
| | | //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来 |
| | | SendComMap(pack_datalen,pack_index>>1); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | index=0; |
| | | } else if((usart_receive_state == UsartReceiveWaitData) ) { //若果收到的是正常通讯包 |
| | | mUsartReceivePack[index] = data; |
| | | index++; |
| | | if(index == pack_length-5) { //如果收到的index与长度相等 |
| | | usart_receive_state = UsartReceiveWaitChecksum; |
| | | } |
| | | } else if(usart_receive_state == UsartReceiveWaitDataLen) { //收到指令类型字节 |
| | | pack_datalen = data; |
| | | usart_receive_state = UsartReceiveWaitData; |
| | | } else if(usart_receive_state == UsartReceiveWaitIndex) { //收到指令类型字节 |
| | | pack_index = data; |
| | | usart_receive_state = UsartReceiveWaitDataLen; |
| | | } else if(usart_receive_state == UsartReceiveWaitCMD) { //收到指令类型字节 |
| | | pack_cmd = data; |
| | | usart_receive_state = UsartReceiveWaitIndex; |
| | | } else if(usart_receive_state == UsartReceiveWaitLength) { //收到长度字节 |
| | | |
| | | pack_length = data; |
| | | pack_index = 0; |
| | | usart_receive_state = UsartReceiveWaitCMD; |
| | | |
| | | } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //收到第一个包头 |
| | | usart_receive_state = UsartReceiveWaitHead1; |
| | | } else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //收到第二个包头 |
| | | usart_receive_state = UsartReceiveWaitMsgType; |
| | | } else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) { |
| | | usart_receive_state = UsartReceiveWaitLength; |
| | | pack_msgtype = data; |
| | | } |
| | | else { |
| | | usart_receive_state = UsartReceiveWaitHead0; |
| | | pack_index = 0; |
| | | pack_length = 0; |
| | | } |
| | | |
| | | } |