Intel HEX Format
If you have dabbled in embedded systems or microcontroller programming, you might have heard of the "Intel HEX format". Don't let the name intimidate you! In simple terms, it's a way of representing binary data using easy-to-read text. Let's break it down so it's crystal clear.
What is the Intel HEX format?
Think of the Intel HEX format as a translator between human-readable text and machine-friendly binary data. It was created by Intel, but now it's widely used by various devices and tools.
Intel HEX consists of ASCII text lines that are separated by a line feed, carriage returns, or both. Hexadecimal letters are used in each text line to represent multiple binary numbers. Depending on their placements on the line, and other factors, the binary integers may represent data, memory addresses, or other values.
File Records
A record is a line of text that consists of six fields. Each field contains a character and multiple digits in the following order, from left to right:
Field | Description |
---|---|
Field 1. | Start Code - one character This character is an ASCII colon ( : ). Each line starts with : , which represents the new line. |
Field 2. | Byte Count - two hexadecimal digits These two digits indicate the data field's number of bytes (HEX digit pairs). The maximum byte count is 255 ( 0xFF ).For example, 16 (0x10) and 32 (0x20) byte counts are commonly used. |
Field 3. | Address - four hexadecimal digits These four digits represent the 16-bit beginning memory address offset of the data. The physical address of the data is computed by adding this offset to a previously established base address. This provides memory addressing beyond the 64KB limit of 16-bit addresses. The base address, which defaults to zero, can be changed by various types of records. Base addresses and address offsets are always expressed as big-endian values. |
Field 4. | Record Type - two hexadecimal digits, 00 to 05 These two digits define the meaning of the data field. For SQTP, only three types are used; 00, 01, and 04. 00 - Normal Data 01 - End of File 04 - Extended Linear Address |
Field 5. | Data - a sequence of n bytes of data Data is represented by 2n hexadecimal digits. Some records omit this field (n equals zero). The meaning and interpretation of the data types depend on the application. |
Field 6. | Checksum - two hexadecimal digits These two digits represent a computed value that is used to verify the record has no errors by ensuring the summation of each of the bytes in the line adds up to zero. See the "Calculating the Checksum" section. |
Calculating the Checksum
The final two characters, as shown in the format table above, stand for a checksum of the data in the line. The checksum's two-digit hexadecimal value allows it to represent any number between 0 and 255, inclusive.
The checksum is calculated by summing the value of the data on the line, excluding the leading colon and checksum byte itself, and taking its two's complement. For example, consider the line:
: 03 0030 00 02337A 1E
Breaking this line into its components:
- Start Code:
:
- Record Length:
03
(3 bytes of data) - Address:
0030
(the 3 bytes will be stored at0030
,0031
, and0032
) - Record Type:
00
(normal data) - Data:
02
,33
,7A
- Checksum:
1E
Taking all the data bytes above, we have to calculate the checksum based on the following values:
03 + 00 + 30 + 00 + 02 + 33 + 7A = E2
The two's complement of E2 is 1E which is, as you can see, the checksum value. The two's complement of a number is the value that must be added to the number to reach the value 256 (decimal). That is to say, E2 + 1E = 100.
You may also calculate the two's complement by subtracting the value from 100h. In other words, 100h - E2h = 1Eh, which is the checksum.
If the value in question is greater than FFh, simply take the part which is less than 100h.
For example, if you want thw two's complement of the value 494h, simply drop the leading "4" which leaves you with 94h. The two's complement of 94h is 6Ch.
Conclusion
The Intel HEX format might sound complex, but it's just a handy way to store and share data in a format that's easy for humans and computers to understand. By knowing the structure and interpreting the data correctly, you'll be well-prepared for working with embedded systems and firmware development. Happy coding!