cancel
Showing results for 
Search instead for 
Did you mean: 

Could I import a set of data stored in a file to array while building?

Lingjun Kong
Associate III
Posted on July 30, 2017 at 12:59

I have a large data set int a[10000]. which is needed in program. But it is too large to write by hand to the .c file for me.

How could I put it into ROM? There any synthex for to help me to import it to program when compiling?

which may let the program looks like this:

const int a[10000];

#loaddata '10000*4 bytes', './a.bin', &a[0];

.

.

.

.

7 REPLIES 7
Posted on July 30, 2017 at 14:15

Hi..

Search in internet for  'Bin to C'

Posted on July 30, 2017 at 14:34

Use external programs/scripts as part of the build process to construct data tables. These could be added to makefile, or User tab in Keil.

If you can code in C and do file functions, you can automate the construction of data tables in a compilable form.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Christopher Pappas
Senior II
Posted on July 30, 2017 at 19:04

You can also use the ST-Link utility to open you data set BIN file, and then write the data to a vacant area of Flash.

Access your data set by reading from flash directly.

For instance;

int DataRead = *((int*)(the address where you stored your data set in Flash, plus the offset into the data set);

Posted on August 03, 2017 at 20:47

So, there is no nature support for importing a binary file while compiling.

Posted on August 03, 2017 at 21:00

Compilers typically don't accept binary input, most linkers will only take native format object files. Some linkers can, or you can use BIN2OBJ type tools.

Have the tool generating the data output to an object file, or compilable C source. The latter requires a high-school level understanding of data representation, and basic C coding skills.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on August 04, 2017 at 10:05

There are many ways to insert a binary into a memory space. One is to look at linker level options, sometime you can add a binary at a specific base address. Otherwise, you can create in the linker file a specific memory segment which you create as a constant pointer to refer to the start of this zone, erase, flash your user program, then without erasing, write the binary onto this particular space (erase and twice write to create the image).

Last, put your binary in Excel and create a simple string macro which convert your byte by byte binary into a C structure which you can copy paste into a C file to add to your project. 

It depends how often this binary changes.

Posted on August 04, 2017 at 08:14

Yes.

https://github.com/Jeroen6/bin2c

bin2c -m -d .\data\data.h -o .\data\data.c '.\data\a.bin'�?