how to convert C++ header file with hex array variable to binary data file

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would like to program AT45DB321D DataFlash SPI flash chips directly from my PC.  I have a FT232H USB to SPI adapter and a PC program that should program the flash chip with a binary file.  I need to make the binary file from a C++ header file with a declared hex data array, for example the file "Option_1.h":

static const unsigned char soundData[] = {
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 
    0x80, 0x80, 0x80, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 
    0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x7f, 0x80, 
    0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x7f, 0x80, 0x7f, 
    0x80, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x80, 0x80, 
    0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 
    0x7f, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f,
    
    ...
    
};

 

I have previously programmed the flash chips by including the header file in a "data loader" arduino sketch that programmed the flash chip from an AVR.

 

I tried srec to convert the file and this is the result:

C:\Users\Perigalacticon\Documents\Applications\srec>srec_cat Option_1.h -o Option1.bin -binary
srec_cat: Option_1.h: 1: warning: ignoring garbage lines
srec_cat: Option_1.h: 2705: file contains no data

Can anyone provide a method to convert hex array header files to a binary file? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just compile it:

C:\SysGCC\avr\bin>type data.c
const unsigned char soundData[] = {
    0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
    0x80, 0x80, 0x80, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f,
    0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x7f, 0x80,
    0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x7f, 0x80, 0x7f,
    0x80, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x80, 0x80, 0x80,
    0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80,
    0x7f, 0x80, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f
};

C:\SysGCC\avr\bin>avr-gcc -c -mmcu=atmega16 -Os data.c -o data.o

C:\SysGCC\avr\bin>avr-objcopy -O binary -j .rodata data.o data.bin

C:\SysGCC\avr\bin>avr-objdump -b binary -s data.bin

data.bin:     file format binary

Contents of section .data:
 0000 80808080 80808080 80808080 80808080  ................
 0010 807f807f 807f807f 807f807f 807f807f  ................
 0020 80807f80 7f807f80 7f807f80 807f807f  ................
 0030 8080807f 807f807f 80808080 80807f80  ................
 0040 7f807f80 7f807f80 7f80807f 807f807f  ................
 0050 807f807f                             ....

The only "clever bits" here are (a) removing the "static" so the compiler doesn't just ditch the data as "not accessed", (b) knowing to use "-j .rodata" in the objcopy because const data, by default is built into that section.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks.  Actually I just realized the original headerless WAV file is already in a binary format I think I should be able to program the chip with it directly right?