5

When working with embedded systems, it is sometimes necessary for a processor to be loaded with several independently compiled and linked binaries; in my present project, a processor is supposed to have both its own code image and a code image which it is supposed to transmit to remote devices with which it communicates. If I was programming the chip with a hex or binary file, I could use a batch file to simply concatenate the contents of separately built and linked files (perhaps using a "FIND /V" to remove things like Intel end-of-file records). When using the Keil debugger to program a device, however, the chip isn't loaded from such a file but instead from a .AXF file.

If the code image which should be sent to remote devices will change many times while I'm testing code for the main device (to which the debugger is attached), what would likely be the most useful way to set up the build process? My inclination would be to write a utility to convert a binary file of the remote processor's code to into either a C file containing const unsigned char REMOTE_CPU_DATA[] = {...}'; and configure the linker to locate that file's const section at the appropriate address, or else an ASM file containing absolute data directives, and then have that utility run as part of the build process for the main code, but converting binary data into text format for inclusion within a project seems icky. Also, would it be better to find a stock utility to do such conversion, or write a special-purpose one in something like C# or VB.NET (I could use either language, but primarily use the latter for PC development)? I would expect that ASM output would be Keil-specific, while C output would be development-system agnostic, but using C rather than ASM would require adding a line to the linker spec to set the absolute address of the remote CPU data [the address of the remote CPU data is fixed to allow it to be loaded under program control, but the hardware channel through which it will be loaded does not exist yet].

supercat
  • 77,689
  • 9
  • 166
  • 211
  • Possible duplicate: [Which program creates a C array...](http://stackoverflow.com/questions/1155578/which-program-creates-a-c-array-given-any-file) – artless noise Jun 05 '13 at 16:26
  • @artlessnoise: I know it's possible to create a C array, but I don't know whether that's the best approach. If any of the Keil tools can cause the binary file to be merged in directly, that would be better. Third-party (e.g. gnu) files which can produce appropriate object files wouldn't be quite as good, but may still be better than converting binary files to C format. – supercat Jun 05 '13 at 18:14
  • Hmm, well, I am not really sure what your question is asking. See this [armcc `section`](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0474c/BABDJCAA.html) documentation. From what I read, *Keil* is the old `armcc`? Ref: [Keil wiki](http://en.wikipedia.org/wiki/Keil_%28company%29). You can annotate the 'C' array to put it in whatever section you like; whether assembler, compiler, or linker these solutions all seem similar. – artless noise Jun 05 '13 at 20:10
  • @artlessnoise: What I would like to do is have the debugger end up loading the remote-code binary along with the executable image for the loaded project, *without* having to convert it to a bunch of numbers in a C source file. I wish C compilers would include a directive to initialize a C array with data in a particular binary file; such things are needed frequently, especially when programming embedded systems. – supercat Jun 05 '13 at 20:16
  • You mean a hypothetical `char my_data[] = { #include "file.bin" };`? This is basically the same things as all the suggestions? I see how this is a little nicer, but I don't see how it buys us much. It isn't really more maintainable than `objcopy`, `make` driven 'C' *or* assembler file creation, or direct inclusion by the linker. The binary ends up being a binary. This is a beauty of *nix scripting; you can quickly make a tool/script to do this, like the `printf` and `hexdump` combo and you can use `make` rules to re-generate the file sensibly. Of course other OSs have these tools too! – artless noise Jun 05 '13 at 20:45
  • @artlessnoise: I prefer builds which only require the installation of a single tool set. I'm using Windows 8 and the Arm/Keil tool set, which do not include the utilities common in *nix. I've used a fair number of IDE's where build processes that could be handled within the IDE were noticeably faster and more convenient than those which required running external utilities, even though the difference isn't great in the Keil system. – supercat Jun 05 '13 at 20:45

1 Answers1

2

You may create a ELF file from a binary with objcopy like so,

objcopy -I binary -B arm -O elf32-littlearm --rename-section \
     .data=.remote.data remote.bin remote.elf

Then you can update your linker script like this,

.remote { 
     REMOTE_CPU_DATA = . ;
     *(.remote.data); 
}

Placing it where ever you like. The data is accessed from 'C' with extern char REMOTE_CPU_DATA[]; or however you wish to type the data. You can massage the endian of the binary with dd, etc.

Another way is to use the Gnu Assembler's .incbin directive as per Linux's firmware Makefile.

I don't know if the Keil tools can handle the ELF formats output by either objdump or gas. I think similar ideas may apply with the Keil tools if they have similar semantics.

Edit: This can also be done from a shell script with some *nix tools,

printf 'char REMOTE_CPU_DATA[] = {' && \
 hexdump -v -e '16/1 "0x%x," "\n"' remote.bin && \
 printf '};' > remote.c
artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Is `objcopy` one of the bundled tools with the Keil system? How do I find information about it? The Keil help system seems a bit goofy? – supercat Jun 05 '13 at 15:33
  • 1
    A GNU tool, separate from Keil, part of the binutils package for whichever distribution you are using. http://www.gnu.org/software/binutils/ – JustinC Jun 05 '13 at 15:42
  • See also: [SO bit2C question](http://stackoverflow.com/questions/2185693/changing-bin-files-data-in-c). There are lots of programs to convert a binary to a *'C'* array. There is also [`hexdump`](http://linux.about.com/library/cmd/blcmdl1_hexdump.htm) on *nix, which can produce a file like you want. Also: [SO:Which program to c array?](http://stackoverflow.com/questions/1155578/which-program-creates-a-c-array-given-any-file) – artless noise Jun 05 '13 at 16:21