• AVR Freaks

Hex file format question

Author
leadinglights
Super Member
  • Total Posts : 396
  • Reward points : 0
  • Joined: 2003/11/07 12:41:48
  • Status: offline
2004/04/09 02:04:57 (permalink)
5 (1)

Hex file format question

What is the format of the .hex files used with IDE?

I need to be able to program a whole bunch of PCBs with slightly different data. The code is to include such things as DES authentication codes, the coefficients for an on board barometer, a unique serial number, and perhaps some contitional code depending on the exact use.

To do this I need to write a script to modify the .hex file for each PCB - and I will need to know the format of the .hex files, including checksum methods etc..

This is an old problem that I have never satisfactorily resolved. I am still modifying and re-compiling for every board.

Thanks for any help you can give,

Mike.
#1

7 Replies Related Threads

    Guest
    Super Member
    • Total Posts : 80503
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Hex file format question 2004/04/09 03:53:10 (permalink)
    0
    I recently wrote a HEX file reader/writer based on information in the MPASM & MPLINK User's Guide here.
    Between this and some sample HEX files to test, I had a working reader written in no time. It's really not that bad :)

    According to the guide, there are several HEX file formats. But I've only seen the 32-bit format (Appendix A.5 in the User's Guide) coming out of the IDE in my experience. I only use the PIC18-series, though, so I could be overgeneralizing.
    #2
    Guest
    Super Member
    • Total Posts : 80503
    • Reward points : 0
    • Joined: 2003/01/01 00:00:00
    • Location: 0
    • Status: online
    RE: Hex file format question 2004/04/09 05:19:05 (permalink)
    +2 (2)
    HEX files are plain text. Every line begins with a 9-character header and ends with a 2-character hexadecimal checksum, with the actual data (in hexadecimal) in the middle.

    :NNAAAATT......CC    (format)
    :1001F000......3E (example)


    - NN represents the number of bytes in the record (not including the header & checksum)
    - AAAA is the lower 16 bits of the data address
    - TT is the Record Type, which I'll get to in a second.
    - CC is the least-significant byte of the checksum
    - ....... is NN bytes of data

    The total number of characters on each line is (1 + 2*4 + 2*NN + 2) = (11 + 2*NN)

    TT, the Record Type, is 00 for a general record.
    TT = 01 for End-Of-File. The line with this record type is always :00000001FF and is always the last line in the file.
    TT = 04 for Extended Address. This line sets the upper 16 bits of the data address for all following records. As such, the first line in the file is usually :020000040000FA to zero the high word of the address.

    The checksum is the two's complement, of the lowest byte, of the sum of all preceding bytes in the line (including the 4 header bytes). In other words, taking an 8-bit sum of every byte on the line (including the checksum) should always give you 0.

    I think that's everything... Let me know if this helps.
    #3
    Ken_Pergola
    Super Member
    • Total Posts : 2252
    • Reward points : 0
    • Joined: 2003/11/07 12:48:48
    • Status: offline
    RE: Hex file format question 2004/04/09 08:09:24 (permalink)
    0
    ORIGINAL: leadinglights
    What is the format of the .hex files used with IDE?


    Hi Mike,

    Attached you will find the "bible" on HEX files from the source themselves -- Intel.

    In order for me to upload the Adobe PDF file, I had to append a .TXT extension to it, thus, when you download this file on your PC, just simply remove the .TXT extension and you are ready to view the PDF.

    Best regards,

    Ken Pergola
    #4
    leadinglights
    Super Member
    • Total Posts : 396
    • Reward points : 0
    • Joined: 2003/11/07 12:41:48
    • Status: offline
    RE: Hex file format question 2004/04/09 10:06:23 (permalink)
    0
    Thankyou both for the help. I am sure that is all I need to get it working - and save myself endless re-compiles.

    Mike.
    #5
    Ian.M
    Super Member
    • Total Posts : 13273
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: RE: Hex file format question 2011/02/12 03:12:51 (permalink)
    +2 (2)
    This is PROBABLY the same version of the above IntelHex format PDF, anyway its the only one a google search on 'Intel Corporation intelhex filetype:pdf' turned up that was from Intel, not a 3rd party and its size is 28.533K

    Link: http://microsym.com/editor/assets/intelhex.pdf
    + attachment (in the hope that one or the other will survive future forum changes - zipped PDF as this forum STILL cant take PDF files sad)
    Intel Corpera.on intelhex

    For future searches:
    Title Page
    Hexadecimal Object File Format
    Specification
    Revision A
    January 6, 1988

    TOC
    Table of Contents
    INTRODUCTION............................................................................................................................................................. 4
    GENERAL RECORD FORMAT..................................................................................................................................... 5
    EXTENDED LINEAR ADDRESS RECORD (32-BIT FORMAT ONLY)..................................................................... 6
    EXTENDED SEGMENT ADDRESS RECORD (16- OR 32-BIT FORMATS).............................................................. 7
    DATA RECORD (8-, 16-, OR 32-BIT FORMATS).......................................................................................................... 7
    START LINEAR ADDRESS RECORD (32-BIT FORMAT ONLY) ............................................................................. 9
    START SEGMENT ADDRESS RECORD (16- OR 32-BIT FORMATS) .................................................................... 10
    END OF FILE RECORD (8-, 16-, OR 32-BIT FORMATS) ......................................................................................... 11

    © 1998 Intel Corporation


    post edited by Ian.M - 2011/02/12 03:25:57
    #6
    jholler
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2012/09/18 08:08:51
    • Location: 0
    • Status: offline
    Re: RE: Hex file format question 2012/09/18 15:39:21 (permalink)
    0
    Ian and others:
    I picked up HEXMATE somewhere.  It generates the 2 records that i need perfectly.  All I have to do is edit them into the right spot in the hex file.  I almost got it to automatically insert the records, but it re-arranges all of the records.  Beyond Compare detected this re-arrangement.  This re-arrangement is not desirable even though it may be the same code.  Nevertheless, I have a batch command that generates the records after editing the batch command with the right CRC (not checksum).
     
    Thanks.
    #7
    Ian.M
    Super Member
    • Total Posts : 13273
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: RE: Hex file format question 2012/09/18 15:54:01 (permalink)
    +1 (1)
    IF you make HEXMATE a post-build step in your build process, you could make sure that your preferred diff utility is presented with an original file that has already been re-arranged by HEXMATE.  As it encodes *EXACTLY* the same data at exactly the same addresses (assuming you don't use any of the options to modify the file), this will not compromise the function of your original HEX file.  After this you should simply be able to use HEXMATE's data insertion capabilities directly.  
     
    Alternatively you may wish to use -FORMAT to adjust record length. If you need to spit records at arbitrary addresses, look at the HEXMATE -BREAK option.   You should be able to replicate the file layout your compiler generates exactly, but upgrading your compiler may well break your hand tuned tweaks, so it is better to post process with HEXMATE every time for consistancy.
    #8
    Jump to:
    © 2021 APG vNext Commercial Version 4.5