1 /* name : bin2intelhex.c
2 * from : Jean Marc Lacroix <jeanmarc.lacroix@free.fr>
4 * abstract : Y have rewrite this program from ????? with some modifications
6 * - the Intel specification.
7 * - correct a bug because my prom programmer don't understand the
8 * initial format. Y suspect a bug in the calcul of the lrc
9 * in the original program.
10 * - correct the format of printf . In the original program, it was
11 * %x, and it is in fact %X, because in the Intel Format, all the
12 * char are in upper case.
13 * - correct the lrc calculation.
16 * this program read the standard input and put to the standard output
17 * the result of the conversion.
19 * cat my_bin | bin2intelhex > my_bin.hex or.....
20 * bin2intelhex < my_bin > my_bin.hex
27 * Revision 1.1 2005/05/17 16:45:06 mcb30
30 * Revision 1.9 1997/12/14 05:14:54 install
31 * - some documentation....
38 /* Intel Hex format specifications
40 The 8-bit Intel Hex File Format is a printable ASCII format consisting of one
41 or more data records followed by an end of file record. Each
42 record consists of one line of information. Data records may appear in any
43 order. Address and data values are represented as 2 or 4 hexadecimal
47 :LLAAAARRDDDD......DDDDCC
55 Length field. Number of data bytes.
56 Address field. Address of first byte.
57 Record type field. 00 for data and 01 for end of record.
59 Checksum field. One's complement of length, address, record type and data
61 CC = LL + AAAA + RR + all DD = 0
64 :06010000010203040506E4
67 The first line in the above example Intel Hex file is a data record addressed
68 at location 100H with data values 1 to 6. The second line is the end
69 of file record, so that the LL field is 0
74 typedef unsigned char t_u8
;
75 typedef unsigned short t_u16
;
77 * the choice for the total length (16) of a line, but the specification
78 * can support an another value
80 #define LL_MAX_LINE 16
86 t_u8 intel_data
[LL_MAX_LINE
];
89 #define INTEL_DATA_TYPE 0
91 int main (const int argc
, const char ** const argv
)
95 * init for the adress, please note that it is assume that the program begin at 0
98 line
.intel_type
= INTEL_DATA_TYPE
;
100 * read the data on the standard input
102 while ((line
.intel_lg_data
= read (0, &line
.intel_data
[0] ,LL_MAX_LINE
)) > 0)
106 * and now for this line, calculate the lrc.
108 line
.intel_lrc
= line
.intel_lg_data
;
109 line
.intel_lrc
+= ((line
.intel_adr
>> 8) & 0xff);
110 line
.intel_lrc
+= (line
.intel_adr
&0xff);
111 line
.intel_lrc
+= line
.intel_type
;
113 * the structure is ready, print it to stdout in the
116 (void) printf (":%02X%04X%02X",
121 * edit all the data read
123 for (i
=0; i
<line
.intel_lg_data
; i
++)
125 (void) printf ("%02X",
126 (line
.intel_data
[i
] & 0xff));
128 * add to the lrc the data print
130 line
.intel_lrc
+=line
.intel_data
[i
];
133 * edit the value of the lrc and new line for the next
135 (void) printf ("%02X\n",
136 (0x100 - line
.intel_lrc
) & 0xff);
138 * prepare the new adress for the next line
140 line
.intel_adr
+=line
.intel_lg_data
;
143 * print the last line with a length of 0 data, so that the lrc is easy to
144 * calculate (ff+01 =0)
146 printf (":00000001FF\n");