CY68013使用的固件是Intel的HEX格式的文件,文件的格式如下:
:[LL][ADDR][TYPE][DATA][CHKSUM]
: --- 1字节 标示一个有效的HEX记录的开始
LL --- 2字节 记录中有效数据(DATA)的字节数
ADDR --- 4字节 数据(data)需要下载到内存中的地址
TYPE --- 2字节 标示数据是否结束,00正常数据,01结束
DATA --- LL字节 数据,大小有LL指定
CHKSUM --- 2字节 整个HEX记录的校验和
校验和的计算公式如下,HEX是ASCII文本,需要将以上的数据转换为二进制格式再处理:
CHKSUM = ~(LL + ADDR高8位 + ADDR低8位 + TYPE + DATA) + 1
- 6 typedef struct{
- 7 unsigned char size; /* Hex data size */
- 8 unsigned char type; /* Hex data type */
- 9 unsigned short addr; /* Downloader address */
- 10 unsigned char data[256]; /* Hex data */
- 11 }HEX_INFO, *P_HEX_INFO;
- 15 /* Conversion ascii code to hex data */
- 16 static unsigned char ascii_to_hex(const unsigned char n)
- 17 {
- 18 if (n >= '0' && n <= '9')
- 19 return n - '0';
- 20 else if (n >= 'A' && n <= 'F')
- 21 return n - ('A' - 10);
- 22 else if (n >= 'a' && n <= 'f')
- 23 return n - ('a' - 10);
- 24
- 25 return 0;
- 26 }
- 28 /*******************************************************
- 29 Desc : Parse a hex recode specified by recode
- 30 cache the result to hex struct
- 31 recode : which hex recode need parse
- 32 hex : after parse hex recode
- 33 result : 0 success recode in hex
- 34 1 it was not a hex recode
- 35 2 recode lengrh have error
- 36 3 recode check sum have error
- 37 *******************************************************/
- 38 static unsigned int parse_hex(const char *recode, P_HEX_INFO hex)
- 39 {
- 40
- 41 const char *src = recode;
- 42 unsigned char buf[256];
- 43 unsigned int chksum, recode_chksum;
- 44 unsigned int idx = 0, cnt, total;
- 45
- 46 /* It was not a hex recode */
- 47 if (*src != ':'){
- 48 return 1;
- 49 }
- 50
- 51 /* Move to read data */
- 52 src++;
- 53 bzero(buf, sizeof(buf));
- 54
- 55 /* Conversion ascii data to hex */
- 56 for (idx = 0; *src; src++, idx++){
- 57
- 58 buf[idx] = ascii_to_hex(*src);
- 59 }
- 60
- 61 /* Hex data total */
- 62 total = idx;
- 63
- 64 /* Get data size length and check */
- 65 hex->size = (buf[0] << 4) | buf[1];
- 66
- 67 if (!(hex->size >= 0 && hex->size < 256))return 2;
- 68
- 69 /* Get download addr */
- 70 hex->addr = (buf[2] << 12) | (buf[3] << 8) | (buf[4] << 4) | buf[5];
- 71
- 72 /* Get this line data type */
- 73 hex->type = (buf[6] << 4) | buf[7];
- 74
- 75 /* Get data and calc each of the chksum */
- 76 chksum = hex->size + hex->addr + (hex->addr >> 8) + hex->type;
- 77
- 78 for (cnt = 0, idx = 8; (cnt < hex->size) && (idx < total);){
- 79
- 80 hex->data[cnt] = (buf[idx] << 4) | buf[idx + 1];
- 81
- 82 chksum += hex->data[cnt];
- 83 cnt ++;
- 84 idx += 2;
- 85 }
- 86
- 87 /* Get file chksum */
- 88 recode_chksum = (buf[idx] << 4) | buf[idx + 1];
- 89
- 90 /* Check total check sum */
- 91 if ((recode_chksum + chksum) & 0xff)return 3;
- 92
- 93 return 0;
- 94 }
阅读(1414) | 评论(0) | 转发(0) |