Want to convert a binary data to ARM object file?You can do it easy.

binary convert

Don’t you want sometimes to have object files such as images to display and an audio sound data to play, do you? you need to convert it from binary to ARM object file. All the time, it didn’t work for me at all  and I was scratching my head!

There is not so many articles of how to do it out in the internet and most of them didn’t help for me. I think there should be a more effective and smart methods than mine, but in this article I can show you my way of how to convert a binary data into an object file for ARM cortex-M, and take it in your project.

What I want to do

I want to have a binary data like an audio sound data in my project, and finally I want to output the sound on I2S interface as PCM data to external devices.

The sound should be a sine wave since it is easy to see if it is surely output on I2S interface.

This time, I will first show you how to make an sine wave and will convert it into ARM object file to be linked in your project.

Also, I will show you how to access it externally and take it in my project.

Workflow

  1. To generate a sine wave as demo (using Audacity of free tool)
  2. To convert the sine wave binary data into ARM object file to be linked and built in my project
  3. To have it in a project.

Summary of Important things

  • You can use Free software Audacity to generate a sine wave, and it is so easy to use.
  • Audio file format is required as PCM.
  • The generated sound binary data needs to be converted into ARM object file.
  • To convert it, you can use objcopy utility tool available as GNU.
  • You need to change the section of the converted object file.
  • In C source code, you can declare it as an array and access to the first address.

Well, let’s get into it.

Generate sound audio data

We will make first a sound audio data, but if you have it already, you can skip this section.

You can use Audacity which is a free software and I am using it always.

Audio sound can be recorded by just clicking a button, but this times shows a way of making a sine wave for my purpose that I want to output on I2S.

Download Audacity

You can download Audacity here. ↓

Official Audacity download

Start up Audacity

I am sorry about the Japanese Menu as my PC is Japanese environment…

First view after starting Audacity

Right after starting up the Audacity at first time, there is no track, so you need to make a track and generate audio sound.

Add a track

トラック作成

You may go to Tool bar menu, Track-Add-Stereo track. Sorry, but this article was originally written in Japanese. So, the application I use is also Japanese version. You can find where it is to supposed to click.

Separate stereo track

Before generating sine wave, it is better to separate the stereo track. This way allows you to add different frequencies on each channel, left and right ch.

Info

It is so handy with different frequency on each channel that you can easily check the right and left channel.

 

Click a button▼of “Audio Track”, and then click the button of ” Split Stereo track ”

トラックの分離

Now you have the split track as below ↓

Generate Tone (Sine wave)

Now you are generating a sine wave from here. Go tool bar menu Generator, click “Generator” to select “Tone…”, then.

The frequency can be set any frequency you like, but this time it is now set to 440Hz. The rest should be default for now.

トーンの生成トーンの設定

This way gives you two identical tone on both of the tracks. Now you need to click anywhere on the right channel and select the channel, once again you need to click Generator and tone to generate different frequency sine wave, e,g, 900Hz.

サイン波設定後

Configuration of sampling frequency

You set the sampling frequency to 44.1kHz, by clicking left-lower corner sampling box.

サンプリング周波数設定

Merge the stereo track

You may want to merge the tracks making stereo track.

Click a button▼of “Audio Track”, and click create stereo track.

トラックを統合

Audio format

Click a button▼of “Audio Track”, and click “Format” and click “24bit PCM format” is selected.

オーディオフォーマットの指定

Stereo track

Export audio

I always select a raw data type in the header format option, since the header info is added to the file. The file name and file extension can be left as is.

The file extension will be stored and changed to “.raw”.

オーディオ書き出し設定

And, click OK and finish.

Convert it to ARM binary format

ARM binary format is 32bit little-endian. You need to convert the audio sound image to the ARM format as well.

In order to do that, you can use an utility tool available from open source software, which is objcopy. If you already installed MCUXpresso IDE, it comes with objcopy.  You can find it in the folder of MCUXpresso as below picture.

objectcopyの場所

Objcopy can convert a binary in to a variety of formats.

For example:

WAV file  ⇄ bin

ELF32  ⇄ bin

etc.

Command

objectcopy -I binary -B arm -O elf32-littlearm foo.bin foo.o

-I binary: –input-target You need to specify what type of format (bdfname) as input file. This time it is set to binary.
-B arm: Binary architecture is specified. This time it is set to arm
-O elf32-littlearm : You need to specify what type of format (bdfname) as output file.  For ARM Cortex-M, elf32-littlearm can be set.

 

情報

Change section

When you add the binary in your project, it will be placed in .data section. In the case, you need to change the section to “.rodata”.

Options

–rename-section .data=.rodata,alloc,load,readonly,data,contents foo.bin foo.o

Any file name is ok for foo.bin and foo.o, unless the extension is the same.

This time, I changed it to like this…

foo.bin –> sine-wave.aiff

foo.o –> sine-wave.o

 

Execute objcopy

objcopy実行

Check the content of the generated objet file

Let’s check and see the contents of the file. ..

You can do readelf to show the header and the section info.

readelf command

readelf foo.o -h -S

To show the info of header and section.

readelf実行

To show symbol table with objdumpobjdump実行

You can find that  .rodata start address is _binary_sine_wave_raw_start[] and you can access the first address of the array.

Now you can write in C source like this to access to the object file.

extern char _binary_sine_wave_raw_start[];

extern char _binary_sine_wave_raw_size[];

const char *sine-wave = _binary_sine_wave_raw_start;

size_t sine-wave_size = _binary_sine_wave_raw_size;

It is important that you can access the object file by pointing to the first address of the array.

Summary

In order to generate a sine wave, Audacity is handy application. I very often use this application when I create a sound source data for evaluation.

In the case of converting the binary image into linkable object file, GNU free software of objcopy can be used and it is bundled with NXP MCUXpresso IDE.

After conversion, you can access it by means of pointer access to the first address of the array.