Keil Logo

GENERAL: Generating Hex Files with Even Number of Bytes


Information in this article applies to:

  • Keil MDK
  • C166
  • C251
  • C51

QUESTION

Is there any way to prevent the number of bytes for a data record from being an odd number in the generated Hex file? Our Flash memory is configured to use word mode which prevents us from writing one byte to it.

ANSWER

Yes, there is a project called SRecord hosted on sourceforge.net which contains utilities to manipulate Intel HEX files in many ways. The free of charge Windows version 1.64 or later 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 with Intel HEX files which were created by Keil OH51, OHX51, OH251, OH166 Object-Hex converter or the fromelf tool (part of Arm Compiler Toolchains). It has an option to fill up bytes with odd addresses. This tool always stores Intel HEX records in ascending address order.

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. µVision key sequences like '#H' can be used to specify the input hex file name. When using a command file for srec_cat.exe, 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, 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 byte (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. Instead of an explicit StartAddress and EndAddress, you can also use expressions which define the address range of a file. In this case, the option '-within HexFile.hex -Intel' is useful to limit the fill-range to the actual HEX file size. '-fill' can also be combined with the option '-range-padding 2' to only fill up 16-bit words.
  • -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 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:

  • Only even addresses,
  • An even number of bytes in each HEX record,
  • All gaps having odd addresses filled with 0xFF(to represent erased Flash).

You can use the following command file:

# BL51 hex files are not sorted for ascending addresses. Suppress this warning
-disable-sequence-warning
# load input HEX file
HexFile.hex -Intel
# fill all incomplete 16-bit words with 0xFF. The range is limited to the application
-fill 0xFF -within HexFile.hex -Intel -range-padding 2
# generate hex records with 16 byte data length (default 32 byte)
-Output_Block_Size=16
#generate 16-bit address records. Do no use for address ranges > 64K
-address-length=2
# generate a Intel hex file
-o HexFilePadded.hex -Intel

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.