GENERAL: Splitting Hex Files
Information in this article applies to:
- C51 any Version
- C251 any Version
- C166 any Version
- MDK-ARM any Version
QUESTION
Is there a utility or an easy way to split one Intel HEX file into
two or more HEX files. This might be necessary if different Flash
banks need to be programmed independently or when a bootloader should
be separated from the application.
ANSWER
You may want to consider using the free
srec_cat.exe utility which is available as Windows
EXE file from https://sourceforge.net/projects/srecord/files/srecord-win32.
It is part of the SRecord project hosted on
sourceforge.net. With this utility, you can load one or multiple HEX
files, cut out specific address areas, move them to a new address and
store them to one HEX file.
You can invoke srec_cat.exe from a Windows
command prompt or Make tool:
srec_cat.exe HexFile.hex -Intel -crop 0x0000 0x7FFF -o PartHexFile.hex -Intel
You can also invoke this tool after each project build/rebuild by
specifying its invocation in the µVision dialog Options for
Target - User - After Build/Rebuild. If many parameters are
needed, it is easier to use a command file:
For a full description of all srec_cat.exe parameters, refer to
the SRecord Reference Manual. Here are some
options which are useful with Keil tools:
-
-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 or OHX51 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. Since a code banked application
is bigger than 64K, -address-length=2 must not be
used before specifying the output file. This would limit the max.
address space to 64K.
-
-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. A fill value
of 0xFF is often used with this option because it corresponds to
erased Flash.
-
-crop StartAddress EndAddress
Only loads the specified address area from the previous input file.
This option can be combined with -offset.
-
-offset Offset
Adds an address offset to the previous input file. Positive or
negative values are allowed. This option can be combined with
-crop.
-
-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 a binary file.
-
@CommandFile
A command file can contain some or all invocation parameters for
srec_cat.exe. You can even use comments -- starting with '#'
extending to the end of the line.
Example 1:
Split one HEX file (HexFile.hex) to two HEX files
(HexFilePart1.hex and HexFilePart2.hex) where HexFilePart1.hex should
contain the original address range from 0x000000-0x007FFF and
HexFilePart2.hex should contain the original address range from
0x008000-0x00FFFF moved down to address 0x000000. Two invocations of
srec_cat.exe are necessary to do this:
srec_cat.exe HexFile.hex -Intel -crop 0x000000 0x007FFF -o HexFilePart1.hex -Intel
srec_cat.exe HexFile.hex -Intel -crop 0x008000 0x00FFFF -offset -0x008000 -o HexFilePart2.hex -Intel
Example 2:
This example is identical with example 1 but because some
additional srec_cat.exe options are specified, a command file is used
to invoke srec_cat.exe two times:
Content of SplitHexPart1.cmd command file:
# BL51 hex files are not sorted for ascending addresses. Suppress this warning
-disable-sequence-warning
# take HexFile.hex address area 0x000000-0x007FFF
.\OBJ\HexFile.hex -Intel -crop 0x000000 0x007FFF
#generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
# generate a Intel hex file
-o .\OBJ\HexFilePart1.hex -Intel
Content of SplitHexPart2.cmd command file:
# BL51 hex files are not sorted for ascending addresses. Suppress this warning
-disable-sequence-warning
# take HexFile.hex address area 0x008000-0x00FFFF and shift it down to 0x000000
.\OBJ\HexFile.hex -Intel -crop 0x008000 0x00FFFF -offset 0x008000
#generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
# generate a Intel hex file
-o .\OBJ\HexFilePart2.hex -Intel
Invocation of srec_cat.exe with two command files:
srec_cat.exe @SplitHexPart1.cmd
srec_cat.exe @SplitHexPart2.cmd
SEE ALSO
Last Reviewed: Thursday, February 25, 2021