Linking binary files

Have you ever had the need to add a binary file to your sketch?  You know, you are working on a project with a TFT screen, and you have a couple of nice icons you’d like to import into the sketch to then display them on the screen, or you’ve got some hardware attached for generating audio, and you’d like to embed a couple of small WAV files into your sketch.  It’s not the easiest thing to do, normally, is it?  It typically involves converting the binary file into source code, usually an array, and adding it into your sketch that way.  A bit clumsy really.

Well, with the release of the latest version of UECIDE (0.8.3b), there is a better way (compiler permitting):

You can now create a special folder inside your sketch called “objects”.  Any files placed in this folder will be gathered together at compile time, and converted (usually with objcopy) into object (.o) files.  These files are then linked with your sketch, and you get full access to the data in them.

No more messing with programs to convert the file into source code.

So how does it work?

Well, at this moment in time it’s only supported for PIC32 targets, but it will be rolled out to the other major compilers as soon as we are able, but the standard toolchain command “objcopy” has the ability to convert just about anything into an object file.  When it creates the object file it adds a couple of standard symbols into it as well, so you can access the data.  These symbols give you the start address of the data, the end address of the data, and the size of the data.  Each one is unique and is derived from the source file the object file is created from.

So let’s say you have a file “icon.565”, which is an icon file you have converted into 16-bit “565” format common to most TFT screens.  Placing that in the “objects” folder and compiling your sketch will create “icon.565.o” in the build folder, which is then linked to the sketch.  Now, unfortunately it’s not just a case of defining an “extern” to the symbols you want to use.  Instead you have to use a little tiny bit of assembly to reference the symbols.  Don’t worry, it’s pretty straight forward:

extern uint16_t icon[] asm(“_binary_objects_icon_565_start”);

You can then access the array “icon[]” and get at all your data.

The format of the symbols is: _binary_(munged filename)_start, _binary_(munged filename)_end and _binary_(munged filename)_size.  The munged filename is the path of the file you added, including the “objects” folder, with any slashes and dots replaced with underscores.  Thus, icon.565 becomes _binary_objects_icon_565_start etc.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: