Keil Logo

GENERAL: Sorting Intel HEX Files


Information in this article applies to:

  • Keil MDK All Versions
  • C166 All Versions
  • C251 All Versions
  • C51 All Versions

QUESTION

Is there a utility to sort Intel HEX files so that the records are written in ascending address order?

ANSWER

Yes, there is a project called SRecord hosted on sourceforge.net which contains utilities to convert Intel HEX files in many ways. The free of charge Windows version can be downloaded here: https://sourceforge.net/projects/srecord/files/srecord-win32

The command line utility srec_cat.exe is part of this package. It can be used to read an Intel HEX file which was created by Keil OH51, OHX51, OH251, OH166 or AXF. srec_cat.exe always stores Intel HEX records in ascending order. So you just need to load an Intel HEX file into srec_cat.exe and store it to a new Intel HEX file.

You can execute this tool after each project build/rebuild by specifying its invocation in the µVision dialog Options for Target - User - After Build/Rebuild. The key sequence '#H' can be used for the input hex file name.

Example:

  srec_cat.exe UnsortedHexFile.hex -Intel -o SortedHexFile.hex -Intel

Useful options for srec_cat:

  • -Disable_Sequence_Warnings
    This option suppresses a warning if records of the input Intel HEX file are not sorted in ascending address order. HEX file generated by OH51, OHX51, OH251 or OH166 are not sorted in ascending address order.
  • -address-length= 2 or 3 or 4
    Specifies the number of address bytes in the Intel HEX output file. By default, srec_cat generates extended address records (type 04) for an address range of up to 4GB. If you want to avoid the generation of extended address records, specify -address-length=2 before specifying the output file. This limits the max. address space to 64K and can be used for most 8051 applications.
  • -Output_Block_Size= ByteCount
    Specifies the length of each HEX record. By default, srec_cat generates lines containing up to 32 bytes of data. If you want to limit the max. line length to 16 bytes (compatible to OH51, OHX51, OH251 or OH166), use -Output_Block_Size=16.
  • -fill FillValue StartAddress EndAddress
    Fills unused areas with the specified constant value. If you want to use this option, you might want to use a fill value of 0xFF which corresponds to a erased Flash.
  • -Intel
    Can be used after an input or output filename to specify that an input file should be interpreted as an Intel HEX file or an output file should be generated as an Intel HEX file.
  • -Binary
    Can be used after an input or output filename to specify that an input file should be interpreted as a binary file or an output file should be generated as binary file.
  • @CommandFile
    A command file can contain some of all invocation parameters of srec_cat. You can even use comments starting with '#' extending to the end of the line.

Example: If you need an Intel HEX file with address records sorted in ascending order where all gaps are filled with value 0xFF and with an exact size of 32K, you can use the following command file:

-Disable_Sequence_Warnings    #disable warning about hex records not in ascending order
MyInputFile.hex               #input file name
-Intel                        #read input file in intel hex format
-fill 0xFF 0x0000 0x8000      #fill gaps from 0x0000-0x7FFF (32K) with value 0xFF
-Output_Block_Size=16         #generate hex records with 16 byte data length (default 32 byte)
-address-length=2             #generate 16-bit address records (no extended adress records type 04)
-o MyOutputFile.hex           #output file name
-Intel                        #generate intel hex output file

You can then invoke srec_cat.exe with this command file:

srec_cat.exe @CommandFile

MORE INFORMATION

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.