Home > Mobile >  header file to binary file or intel HEX file
header file to binary file or intel HEX file

Time:01-05

I have a header file. It contains the value of various constants(as pre-processor macro definitions---hash defines) that I use in my embedded systems project(IDE is Keil MicroVision, Microcontroller is STM32 and programming language is C). I want to convert that header file to a hex file, so that I can separately write it to flash memory of the microcontroller. Hence, for a single general software code, I wish to to change the constants and run it for specific (around 10-15 different types) hardware.

My approach: I am trying to find a way to convert the header file to a binary file first, and then use some utility(srecorder from sourceforge) to convert the .bin to .hex

Is this approach right?

Or can anyone please suggest an alternate and effective way to achieve my goal? Thanks

CodePudding user response:

Preprocessor defines aren't stored in data flash but in program flash (.text), together with the code using them, integrated in the machine code. They aren't stored at any specific or fixed address. So your request doesn't make any sense - you simply cannot program them to flash separately.

In order to achieve that you need const uint32_t or similar variables stored at fixed memory locations. These need to be declared in a .c file, though it is sometimes acceptable to expose read-only variables "globally" with extern references from a corresponding .h file.

To allocate a variable at a specific address you typically need to change the linker script and use non-standard compiler extensions.

CodePudding user response:

I see two ways of going about this. I'll assume your constants are macros, but, it should not be different if they are enums or something else.

First option is to use the preprocessor to select your platform. Something like:

#if defined(PLATFORM_A)
#define CONSTANT_X 111
#define CONSTANT_Y 222
#elif defined(PLATFORM_B)
#define CONSTANT_X 112
#define CONSTANT_Y 221
#else
#error Need to define a platform
#endif

Of course, if you have a lot of constants, maybe keeping each platform in its own header would be better (easier to read).

#if defined(PLATFORM_A)
#include "platform_a_constants.h"
#elif defined(PLATFORM_B)
#include "platform_b_constants.h"
#else
#error Need to define a platform
#endif

This option is the only way if you use these constants to, say, dimension an array.

I don't have experience with Keil IDE's, so, I'm not sure how to configure it to build different binaries with different macro definitions, but, it's a common C thing, there should be way to do it.

Another option would be to make these globals. In that case, you would have a single header like:

extern int const constant_x;
extern int const constant_y;

And then a separate source file for each platform:

// platform_a.c
int const constant_x = 111;
int const constant_y = 222;

// platform_b.c
int const constant_x = 112;
int const constant_y = 221;

Again, how to link in only the source file that you need for a particular target depends on your build system and I'm not familiar with Keil IDE, but it should not be hard to do.

In general, this option is a little easier to maintain, as it's just setting up the linker, you don't mess up with source code in any way (and defining a preprocessor constant is messing up with the source code in potentially ugly ways).

Also, with this option it's simple to turn a constant into a runtime parameter, which can be changed during the execution of the program, if need be - just drop the const from the declaration and definitions.

  •  Tags:  
  • Related