include binary code

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

How can I include a binary file (raw bytewise data) as a big array into an AVR32 project so I can then acces the data from c code? I can't find any example or hint, although it does not seem a unique problem.

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

The usual way is to convert the binary data to a source code representation - eg an initialized array - that can be compiled with the rest of the source code.

IIRC a utility to do this conversion has been named here, but I can't locate it. I think Cliff (AVRfreak clawson) has mentioned it before, so maybe wait for him to come around...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:

I think Cliff (AVRfreak clawson) has mentioned it before, so maybe wait for him to come around...

Ta da!

The utility I use is xxd which is a component that comes with vim. So if you download the Win32 vim install package you can simply take xxd.exe from it.

C:\VMLAB\AVR_demo>\Windows\xxd -?
Usage:
       xxd [options] [infile [outfile]]
    or
       xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
    -a          toggle autoskip: A single '*' replaces nul-lines. Default off.
    -b          binary digit dump (incompatible with -p,-i,-r). Default hex.
    -c cols     format  octets per line. Default 16 (-i: 12, -ps: 30).
    -E          show characters in EBCDIC. Default ASCII.
    -g          number of octets per group in normal output. Default 2.
    -h          print this summary.
    -i          output in C include file style.
    -l len      stop after  octets.
    -ps         output in postscript plain hexdump style.
    -r          reverse operation: convert (or patch) hexdump into binary.
    -r -s off   revert with  added to file positions found in hexdump.
    -s [+][-]seek  start at  bytes abs. (or +: rel.) infile offset.
    -u          use upper case hex letters.
    -v          show version: "xxd V1.10 27oct98 by Juergen Weigert (Win32)".

I use

xxd -i source.bin source.c

and it will create something like:

unsigned char source_bin[] = {
  0x40, 0x40, 0x45, 0x64, 0x69, 0x74, 0x6f, 0x72, 0x20, 0x74, 0x77, 0x69,
  0x2e, 0x70, 0x72, 0x6a, 0x20, 0x37, 0x20, 0x32, 0x36, 0x20, 0x37, 0x20,
  0x31, 0x34, 0x35, 0x36, 0x34, 0x30, 0x36, 0x35, 0x32, 0x38, 0x20, 0x34,
  0x34, 0x20, 0x31, 0x20, 0x31, 0x20, 0x38, 0x30, 0x30, 0x20, 0x36, 0x34,
  0x38, 0x20, 0x2d, 0x31, 0x20, 0x2d, 0x31, 0x20, 0x33, 0x20, 0x34, 0x20,
  0x33, 0x38, 0x34, 0x20, 0x33, 0x39, 0x39, 0x20, 0x40, 0x40, 0x4d, 0x65,
  0x73, 0x73, 0x61, 0x67, 0x65, 0x73, 0x20, 0x31, 0x34, 0x35, 0x36, 0x34,
  0x30, 0x36, 0x35, 0x32, 0x38, 0x20, 0x34, 0x34, 0x20, 0x31, 0x20, 0x31,
  0x20, 0x30, 0x20, 0x36, 0x32, 0x30, 0x20, 0x2d, 0x34, 0x20, 0x2d, 0x32,
  0x33, 0x20, 0x33, 0x39, 0x32, 0x20, 0x34, 0x30, 0x37, 0x20, 0x38, 0x33,
  0x35, 0x20, 0x35, 0x34, 0x30, 0x20, 0x40, 0x40, 0x43, 0x6f, 0x64, 0x65,
  0x42, 0x6f, 0x6f, 0x6b, 0x20, 0x31, 0x34, 0x35, 0x36, 0x34, 0x30, 0x36,
  0x35, 0x32, 0x38, 0x20, 0x34, 0x34, 0x20, 0x31, 0x20, 0x31, 0x20, 0x31,
  0x36, 0x30, 0x20, 0x36, 0x34, 0x38, 0x20, 0x2d, 0x31, 0x20, 0x2d, 0x31,
  0x20, 0x33, 0x38, 0x37, 0x20, 0x36, 0x20, 0x38, 0x33, 0x38, 0x20, 0x34,
  0x30, 0x34, 0x20, 0x40, 0x40, 0x50, 0x61, 0x6e, 0x65, 0x6c, 0x20, 0x31,
  0x34, 0x35, 0x36, 0x30, 0x37, 0x38, 0x38, 0x34, 0x38, 0x20, 0x34, 0x34,
  0x20, 0x31, 0x20, 0x31, 0x20, 0x33, 0x32, 0x30, 0x20, 0x36, 0x32, 0x30,
  0x20, 0x2d, 0x31, 0x20, 0x2d, 0x31, 0x20, 0x31, 0x36, 0x20, 0x32, 0x30,
  0x35, 0x20, 0x33, 0x39, 0x37, 0x20, 0x35, 0x37, 0x34, 0x20, 0x0d, 0x0a
};
unsigned int source_bin_len = 240;

Having said all that you CAN use avr-objcopy to convert a source.bin into a linkable source.o too then you just add the .o file name to the list of files to be linked - the data has an auto-generated name you can link against (though I can't remember the specific details of how it does this - there have been threads here about it in the past and if you search "xxd" you'll also hit the threads where this was discussed too)

EDIT: vim download is here:

http://www.vim.org/download.php#pc

Under "Win32 console executable" get vim73w32.zip - 1MB .zip file it contains the 49K xxd.exe

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

You can achieve what you want, avoiding the conversion to "source" and subsequent compilation, using purely binutils tools you already have. Assuming the bin data is to go to FLASH (i.e. .progmem):

avr-objcopy f.bin f.o -I binary -O elf32-avr --rename-section .data=.progmem

then add f.o into the list of linked objects. objcopy creates a couple of symbols through which you can access the binary, the most interesting is its beginning which is _binary_f_bin_start (the "f" comes from the file name), so you can declare it for example as:

extern unsigned char _binary_f_bin_start[];

and use accordingly.

albertvv wrote:
into an AVR32 project
Oh, but then you are in wrong forum.

JW

PS. IIRC, srecord.sf.net can perform several kinds of "conversion" to source, too.

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

There's a few things for me to try out. Thanks very much guys! Why is it the wrong forum?

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

This forum is for the 8 bit AVR, but you asked about AVR32 which is the 32 bit AVRs (which have their own set of fora).

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:

Why is it the wrong forum?

Because you mentioned the AVR32. This is for GCC on the AVR8. The AVR32 has it's own set of five (is it?) separate fora. But for something like this the answer's the same for 8 or 32 bit.

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

I have a online converter, that I use for storing webfiles in flash...

http://stokerlog.dk/hexConverter...

It does not store the length, instead it uses ./.END to mark the end of a file.
Besides simple hex converting, it offers gzip compression(for sending static webfiles), and javascript optimize by google API.

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

Stokerbot: yes, works fine too (and good to refresh my limited danish).
Mine seems to be the only not-shown location: New Zealand, off the map I suppose.