Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99251
  • 博文数量: 20
  • 博客积分: 496
  • 博客等级: 二等列兵
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-07 10:26
文章分类

全部博文(20)

文章存档

2012年(9)

2011年(11)

分类:

2012-09-21 10:35:49

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

  1.   6 typedef struct{
  2.   7 unsigned char size; /* Hex data size */
  3.   8 unsigned char type; /* Hex data type */
  4.   9 unsigned short addr; /* Downloader address */
  5.  10 unsigned char data[256]; /* Hex data */
  6.  11 }HEX_INFO, *P_HEX_INFO;



  7.  15 /* Conversion ascii code to hex data */
  8.  16 static unsigned char ascii_to_hex(const unsigned char n)
  9.  17 {
  10.  18 if (>= '0' && n <= '9')
  11.  19 return n - '0';
  12.  20 else if (>= 'A' && n <= 'F')
  13.  21 return n - ('A' - 10);
  14.  22 else if (>= 'a' && n <= 'f')
  15.  23 return n - ('a' - 10);
  16.  24
  17.  25 return 0;
  18.  26 }


  19.  28 /*******************************************************
  20.  29 Desc : Parse a hex recode specified by recode
  21.  30 cache the result to hex struct
  22.  31 recode : which hex recode need parse
  23.  32 hex : after parse hex recode
  24.  33 result : 0 success recode in hex
  25.  34 1 it was not a hex recode
  26.  35 2 recode lengrh have error
  27.  36 3 recode check sum have error
  28.  37 *******************************************************/
  29.  38 static unsigned int parse_hex(const char *recode, P_HEX_INFO hex)
  30.  39 {
  31.  40
  32.  41 const char *src = recode;
  33.  42 unsigned char buf[256];
  34.  43 unsigned int chksum, recode_chksum;
  35.  44 unsigned int idx = 0, cnt, total;
  36.  45
  37.  46 /* It was not a hex recode */
  38.  47 if (*src != ':'){
  39.  48 return 1;
  40.  49 }
  41.  50
  42.  51 /* Move to read data */
  43.  52 src++;
  44.  53 bzero(buf, sizeof(buf));
  45.  54
  46.  55 /* Conversion ascii data to hex */
  47.  56 for (idx = 0; *src; src++, idx++){
  48.  57
  49.  58 buf[idx] = ascii_to_hex(*src);
  50.  59 }
  51.  60
  52.  61 /* Hex data total */
  53.  62 total = idx;
  54.  63
  55.  64 /* Get data size length and check */
  56.  65 hex->size = (buf[0] << 4) | buf[1];
  57.  66
  58.  67 if (!(hex->size >= 0 && hex->size < 256))return 2;
  59.  68
  60.  69 /* Get download addr */
  61.  70 hex->addr = (buf[2] << 12) | (buf[3] << 8) | (buf[4] << 4) | buf[5];
  62.  71
  63.  72 /* Get this line data type */
  64.  73 hex->type = (buf[6] << 4) | buf[7];
  65.  74
  66.  75 /* Get data and calc each of the chksum */
  67.  76 chksum = hex->size + hex->addr + (hex->addr >> 8) + hex->type;

  68.  77
  69.  78 for (cnt = 0, idx = 8; (cnt < hex->size) && (idx < total);){
  70.  79
  71.  80 hex->data[cnt] = (buf[idx] << 4) | buf[idx + 1];
  72.  81
  73.  82 chksum += hex->data[cnt];
  74.  83 cnt ++;
  75.  84 idx += 2;
  76.  85 }
  77.  86
  78.  87 /* Get file chksum */
  79.  88 recode_chksum = (buf[idx] << 4) | buf[idx + 1];
  80.  89
  81.  90 /* Check total check sum */
  82.  91 if ((recode_chksum + chksum) & 0xff)return 3;
  83.  92
  84.  93 return 0;
  85.  94 }
阅读(1368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~