📄 hex.c
字号:
#include <ctype.h>
#include <stdio.h>
#include "config.h"
#include "isp.h"
#include "lcd.h"
#include "hex.h"
unsigned char mBuffer[256];
unsigned int offset;
unsigned char Msg;
unsigned char m_BankSector;
unsigned char type, data_value, sum, sum_1, sum_2;
unsigned int mlength;
unsigned int mbyte;
void hex_decoder(unsigned char mRxData)
{
static state = REC_MARK;
switch(state)
{
case REC_MARK:
if (mRxData == ':')
{
state = REC_LEN_1;
mbyte=0;
}
else
state = REC_MARK;
break;
case REC_LEN_1:
mlength = toint(mRxData) * 16;
state = REC_LEN_2;
break;
case REC_LEN_2:
mlength = mlength + toint(mRxData);
state = OFFSET_1;
break;
case OFFSET_1:
offset = toint(mRxData) * 4096;
state = OFFSET_2;
break;
case OFFSET_2:
offset = offset + toint(mRxData) * 256;
sum_1= offset / 256;
state = OFFSET_3;
break;
case OFFSET_3:
offset = offset + toint(mRxData) * 16;
state = OFFSET_4;
break;
case OFFSET_4:
offset = offset + toint(mRxData);
sum_2 = offset - sum_1 * 256;
state = REC_TYP_1;
break;
case REC_TYP_1:
type = toint(mRxData) * 16;
state =REC_TYP_2;
break;
case REC_TYP_2:
type = type + toint(mRxData);
sum = (unsigned char)mlength + sum_1 + sum_2 + type;
if(mlength==0x00)state=CHEKSUM_1;
else state = DATA_1;
break;
case DATA_1:
data_value = toint(mRxData) * 16 ;
state = DATA_2;
break;
case DATA_2:
data_value = data_value + toint(mRxData);
sum = sum + data_value;
mBuffer[mbyte] = data_value;
mbyte++;
if(mlength<0xFF)
{
if(mbyte == mlength)state = CHEKSUM_1;
else state = DATA_1;
}
else
{
if(mbyte == (mlength+1))state = CHEKSUM_1;
else state = DATA_1;
}
break;
case CHEKSUM_1:
sum_1 = toint(mRxData) * 16;
sum = (~sum) + 1;
state = CHEKSUM_2;
break;
case CHEKSUM_2:
sum_1 = sum_1 + toint(mRxData);
state = REC_MARK;
if(sum_1 != sum)
{
if(type==0x00)Msg = HEX_DEC_FRAME_ERR; //帧错误
else if(type==0x02)Msg = HEX_DEC_SEG_ERR; //段错误
else if(type==0x12)Msg = HEX_DEC_SEG_VERIFY_ERR; //段地址错误
}
else
{
if(type==0x00)Msg = HEX_DEC_FRAME_DATA; //帧数据
else if(type==0x01)Msg = HEX_DEC_END; //结束
else if(type==0x02) //段地址
{
Msg = HEX_DEC_SEG_DATA; //段数据地址
m_BankSector = mBuffer[1];
}
else if(type==0x03)Msg = HEX_DEC_START; //开始下载
else if(type==0x0A)Msg = HEX_DEC_SEG_IRQ; //段请求
else if(type==0x0D)Msg = HEX_DEC_FRAME_IRQ; //帧请求
else if(type==0x10)Msg = HEX_DEC_VERIFY_START; //帧比较开始
else if(type==0x12)
{
Msg = HEX_DEC_SEG_VERIFY_DATA; //段比较数据
m_BankSector = mBuffer[1];
}
else if(type==0x15) //帧比较地址
{
Msg = HEX_DEC_FRAME_VERIFY_DATA; //比较帧地址
offset = mBuffer[0]*256+mBuffer[1];
}
else if(type==0x16)Msg = HEX_DEC_VERIFY_FRAME_IRQ; //比较帧请求
else if(type==0x19)Msg = HEX_DEC_VERIFY_FRAME_END; //比较结束
else if(type==0x1B)Msg = HEX_DEC_PROGRAM_ERR; //错误结束
else if(type==0x1C)Msg = HEX_DEC_SECTOR_IRQ; //扇区设置命令
else if(type==0x1D)
{
Msg = HEX_DEC_SECTOR_BANK; //扇区设置命令
offset = mBuffer[0]*256+mBuffer[1];
}
}
break;
default :
state = REC_MARK;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -