4 Jul 2012

Hex file format and how to read it?



Using Vi editor you could open this .hex file in previous section i showed you how to create a hex file and now we are going to learn how to read a hex file.
What all the things it consists you will learn.

$ vi file.hex

Now you could see the first line in the picture that is represented in the form

:10 0000  00 8F EF 87 BB 18 BA 60 E0 81 E0 90 E0 E7 E0 F0 E0 B6

RED : Indicates the number of bytes is being written

BLUE : Indicates the starting address of the data being written

yellow: indicates the type of record . These were of 5 types:
             1. 00  -->  data record
             2. 01  -->  end of the record
             3. 02  -->  extended segment address record
             4. 04  -->  extended linear address record
             5. 05  -->  start linear address record

Blue : indicates the data

Black: indicates the checksum .

Now lets discuss about the checksum how it is calculated.
suppose you have the hex line give below:
:02 0000 02 1400  E8
Its nothing but 2's complement of whole except the checksum because we are calculating it right.


    now 01h+(~(02h + 00h + 00h + 02h + 14h +00h))
                   0000 0010 = 02h
                   0000 0000 = 00h
                   0000 0000 = 00h
                   0000 0010 = 02h
                   0001 0100 = 14h
                   0000 0000 = 00h  (+)
                  -----------------------------
                ~(0001 1000 )  =  1110 0111
                                              0000 0001 (+)
                  -------------------------------------------------------
                                               1110 1000 = E8 checksum
                  -------------------------------------------------------
          For all the lines it is in the same way take the whole line add up all the bytes until checksum and do 1s compliment for the result and then add the 01h the complement of result so that you will get the checksum .
        
I hope everyone got how the checksum is calculated right .
--------------------------------------------------------------------------------------------------------------------
Now lets discuss in detail about the type of records in detail ok. Lets go ........

Type 00 Record
          
              As i told you this will indicate the record is of data type so the type tells that it is the data record .
               This thing no need to tell in detail . This basic information is enough to understand ok.

Type 01 Record
           
               This record tells that it is the end of the record so this will be the last line in all hex file what ever it matters until you do something wrong .
                A perfectly executed hex file will have perfect format what i am describing here.
                And here checksum is calculated in the form:
                                     :00000001 FF
        This is calculated like this 01h+(~(00+00+00+01) )= 0xFF
The address in the end of record is meaning less so we can ignore it.

Type 02 Record   (Extended segment address record)


         1. Extended segment address record is also called HEX-86 record.
           2. Always have two data bytes and appear as follows

                        :02 0000 02 1400   E8
           3. Address field is always 0000h
         
Now the real part comes when an extended segment address record is read, the extended segment address  stored in the data field is saved and is applied to subsequent records read from the Intel HEX file. The segment address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted-address data from the extended segment address record. The following example illustrates this process.

Address from data record's address field  =           2340
Extended segment address rec data field   =        1400
                                                                        -------------------
Absolute memory address                                00016340
                                                                        -------------------


Type 04 Record: (Extended linear Address Record)
         
         1. it is called 32-bit address records and Hex386 records.
         2. The upper 16bits (16-31) contains address of data .
    The extended linear address record  always have two data bytes and appears as follows:
                         :02 0000 04 FFFF FC
          FFFF   --> upper 16 bits of address
When an extended linear address record is read, the extended linear address stored in the data field is saved and is applied to subsequent records read from the Intel Hex file. The linear address remains effective until changed by another extended address record.

The absolute-memory address of a data record is obtained by adding the address field in the record to the shifted address data from the extended linear address record. The following example illustrates this process.

Address from the data record's address field =           2360
Extended linear address record data field      =  FFFF
                                                                            --------------------
Absolute-memory address                               =  FFFF2360   (32 bit)
                                                                            --------------------


Type 05 Record : Start linear address record .
       
          1. Only for MDK-ARM
          2. Specify the start address of the application
          3. contains 32 bit address (always 4 data bytes ) appear as
                   :04 0000 05 000000CD 2A

         000000CD --> starting address of the application.

Start linear address specifies the address of the __main(pre-main) function but not the address of the startup code which usually calls __main after calling Systeminit().
                    An odd linear start address specifies that  __main is compiled for the Thumb instruction set.

                    The start linear address Record can appear anywhere in hex file. In most cases this record can be ignored because it does not contain information which is needed to program flash memory .





No comments:

Post a Comment