void UartObj::Decode(void) { unsigned char ch; unsigned int *Ptr; while (ReceiveWritePtr != ReceiveReadPtr) { ch = ReceiveBuffer[ReceiveReadPtr++]; ReceiveReadPtr &= 0xff; switch (State) { case 0://'$'搜索 if (ch == '$') { State = 1; ReceiveCount = 0; } ReceivePosition = ReceiveReadPtr; break; case 1://命令 if (ch == ':') { if (ReceiveCount == 5 && ReceiveBuffer[ReceivePosition] == 'W' && ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' && ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'i' && ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 't' && ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e') { State = 2;//Write命令地址 } else if (ReceiveCount == 4 && ReceiveBuffer[ReceivePosition] == 'R' && ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' && ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' && ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'd') { State = 3;//Read命令地址 } else if (ReceiveCount == 5 && ReceiveBuffer[ReceivePosition] == 'E' && ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' && ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' && ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 's' && ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e') { State = 4;//Erase命令地址 } else { State = 0; } ReceiveCount = 0; ReceivePosition = ReceiveReadPtr; Address = 0; Data = 0; } else if (ch == ';') { if (ReceiveCount == 5 && ReceiveBuffer[ReceivePosition] == 'R' && ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' && ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 's' && ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'e' && ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 't') { State = 0; for(;;); } } else//命令字符个数 { ReceiveCount ++; } break; case 2://Write命令地址 case 3://Read命令地址 case 4://Erase命令地址 if ((State == 2) && (ch == ',')) { State = 5;//Write命令数据 } else if ((State == 3) && (ch == ';')) { Ptr = (unsigned int*)Address; Data = *Ptr; if ((Address >= 0x80000) && (Address <= 0x8ffff)) { putstr("FLASH数据读出:["); } else { putstr("RAM数据读出:]["); } puthex(Address); putstr("]="); puthex(Data); puts(""); State = 0; } else if ((State == 4) && (ch == ';')) { if ((Address >= 0x80000) && (Address <= 0x8ffff)) { Flash.SectorErase((Address & 0xffff) >> 11); putstr("FLASE扇区擦除:["); puthex(Address & 0xfffff800); putstr("-"); puthex(Address | 0x7ff); putstr("]"); puts(""); } State = 0; } else if ((ch >= '0') && (ch <= '9')) { Address <<= 4; Address |= ch - '0'; } else if ((ch >= 'A') && (ch <= 'F')) { Address <<= 4; Address |= ch - 'A' + 10; } else if ((ch >= 'a') && (ch <= 'f')) { Address <<= 4; Address |= ch - 'a' + 10; } break; case 5://Write命令数据 if (ch == ';')//命令结束字符 { if ((Address >= 0x80000) && (Address <= 0x8ffff)) { Flash.Write(FLASH[Address & 0xffff], Data); putstr("FLASH数据写入:["); } else { Ptr = (unsigned int*)Address; *Ptr = Data; putstr("RAM数据写入:]["); } puthex(Address); putstr("]="); puthex(Data); puts(""); State = 0; } else if ((ch >= '0') && (ch <= '9')) { Data <<= 4; Data |= ch - '0'; } else if ((ch >= 'A') && (ch <= 'F')) { Data <<= 4; Data |= ch - 'A' + 10; } else if ((ch >= 'a') && (ch <= 'f')) { Data <<= 4; Data |= ch - 'a' + 10; } break; default: State = 0; break; } } }
|