Keil Logo

OH51: Combining Code Banking Hex Files


Information in this article applies to:

  • C51 all Versions

QUESTION

I can create one HEX file for each code bank in my µVision project when I use the BL51, OC51, OH51 tools, or I can create one big banked hex file when I use the LX51, OHX51 tools. In both cases, the memory layout does not match my hardware. I need one HEX file to download to my Flash memory where I can define all memory addresses for the Common Area and the Code Banks. How can I do this?

ANSWER

You can achieve this using the free srec_cat.exe utility available as Windows EXE file from https://sourceforge.net/projects/srecord/files/srecord-win32. This utility is part of the SRecord project hosted on sourceforge.net. With this utility, you can load multiple HEX files, cut out specific address areas, move them to a new address and merge everything to one HEX file.

You can invoke srec_cat.exe from a Windows command prompt or Make tool:

  srec_cat.exe @MergeHex.cmd

You can also automatically invoke this tool after each project build/rebuild by specifying its invocation in the µVision dialog Options for Target - User - After Build/Rebuild. Using a command file is recommended because the number of parameters needed might be high. When using a command file for srec_cat.exe in µVision, be sure to double the '@' character, or µVision might interpret it as a key-sequence.

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 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 a 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:

BL51, OC51 and OH51 create four code bank HEX files (MyProject.H00, MyProject.H01, ..., MyProject.H03) which are all up to 64k in size as described in example 'Four 64K Code Banks' in the BL51 manual. Each one contains the common area and the corresponding code bank. I need to combine them to one HEX file with the following memory organization:

  • 000000H -> 00FFFFH - Common Area + Code Bank 0
  • 010000H -> 01FFFFH - Common Area + Code Bank 1
  • 020000H -> 02FFFFH - Common Area + Code Bank 2
  • 030000H -> 03FFFFH - Common Area + Code Bank 3

You can create the following command file (MergeHex.cmd) to merge them all to one HEX file (MyCompleteProject.HEX):

# BL51 hex files are not sorted for ascending addresses. Suppress this warning
-disable-sequence-warning
# take common area + bank 0
.\OBJ\MyProject.H00 -Intel -crop 0x000000 0x00FFFF
# take common area + bank 1 and move it up to 0x010000-0x01FFFF
.\OBJ\MyProject.H01 -Intel -crop 0x000000 0x00FFFF -offset 0x010000
# take common area + bank 2 and move it up to 0x020000-0x02FFFF
.\OBJ\MyProject.H02 -Intel -crop 0x000000 0x00FFFF -offset 0x020000
# take common area + bank 3 and move it up to 0x030000-0x03FFFF
.\OBJ\MyProject.H03 -Intel -crop 0x000000 0x00FFFF -offset 0x030000
#generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
# generate a complete Intel hex file
-o .\OBJ\MyCompleteProject.HEX -Intel

Example 2:

BL51, OC51 and OH51 create seven code bank HEX files (MyProject.H01, MyProject.H02, ..., MyProject.H07) which are all up to 64k in size as described in example '32K Common Area' in the BL51 manual. Each one contains a fixed common area from 0x0000-0x7FFF and the corresponding code bank from 0x8000-0xFFFF. I need to combine them to one Hex file with the following memory organization:

  • 000000H -> 007FFFH - Common Area
  • 008000H -> 00FFFFH - Code Bank 1
  • 010000H -> 017FFFH - Code Bank 2
  • 018000H -> 01FFFFH - Code Bank 3
  • 020000H -> 027FFFH - Code Bank 4
  • 028000H -> 02FFFFH - Code Bank 5
  • 030000H -> 037FFFH - Code Bank 6
  • 038000H -> 03FFFFH - Code Bank 7

You can create the following command file (MySrec_cat.cmd) to merge them all to one HEX file:

# BL51 hex files are not sorted for ascending addresses. Suppress this warning
-disable-sequence-warning
# take common area + bank 1
.\OBJ\MyProject.H01 -Intel -crop 0x000000 0x00FFFF
# take bank 2 and move it up to 0x010000-0x017FFF
.\OBJ\MyProject.H02 -Intel -crop 0x008000 0x00FFFF -offset 0x008000
# take bank 3 and move it up to 0x018000-0x01FFFF
.\OBJ\MyProject.H03 -Intel -crop 0x008000 0x00FFFF -offset 0x010000
# take bank 4 and move it up to 0x020000-0x027FFF
.\OBJ\MyProject.H04 -Intel -crop 0x008000 0x00FFFF -offset 0x018000
# take bank 5 and move it up to 0x028000-0x02FFFF
.\OBJ\MyProject.H05 -Intel -crop 0x008000 0x00FFFF -offset 0x020000
# take bank 6 and move it up to 0x030000-0x037FFF
.\OBJ\MyProject.H06 -Intel -crop 0x008000 0x00FFFF -offset 0x028000
# take bank 7 and move it up to 0x038000-0x03FFFF
.\OBJ\MyProject.H07 -Intel -crop 0x008000 0x00FFFF -offset 0x030000
#generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
# generate a complete Intel hex file
-o .\OBJ\MyCompleteProject.HEX -Intel

Example 3:

An application with 3 code banks 0,1,2 is set up with a common area of 32K from 0x0000-0x7FFF and 32K code banks from 0x8000-0xFFFF. It is built with LX51 and OHX51 and therefore one combined HEX file (MyProject.hex) is generated with three 64K banks, each containing the common area and one code bank. The OHX51 option MERGE32K cannot be used because code bank 0 is needed. The memory layout of this combined hex file is:

  • 000000H -> 007FFFH - Common Area
  • 008000H -> 00FFFFH - Code Bank 0
  • 010000H -> 017FFFH - Common Area
  • 018000H -> 01FFFFH - Code Bank 1
  • 020000H -> 027FFFH - Common Area
  • 028000H -> 02FFFFH - Code Bank 2

The memory areas of this combined HEX file should be rearranged to match the following memory organization:

  • 000000H -> 007FFFH - Common Area
  • 008000H -> 00FFFFH - Code Bank 0
  • 010000H -> 017FFFH - Code Bank 1
  • 018000H -> 01FFFFH - Code Bank 2

You can create the following command file (MergeHex.cmd) to cut out the memory areas from the combined HEX file, move them to a different address and then merge these areas to one HEX file (MyCompleteProject.HEX):

# take common area (0x000000-0x007FFF) + bank 0 (0x008000-0x00FFFF)
.\OBJ\MyCompleteProject.HEX -Intel -crop 0x000000 0x00FFFF
# take bank 1 (0x018000-0x01FFFF) and move it down to 0x010000-0x017FFF
.\OBJ\MyCompleteProject.HEX -Intel -crop 0x018000 0x01FFFF -offset -0x008000
# take bank 2 (0x028000-0x02FFFF) and move it down to 0x018000-0x01FFFF
.\OBJ\MyCompleteProject.HEX -Intel -crop 0x028000 0x02FFFF -offset -0x010000
#generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
# generate a complete Intel hex file
-o .\OBJ\MyCompleteProject.HEX -Intel

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.