Jeremy
Jeremy

Reputation: 85

Embedding constant at specific memory location

I'm writing a bootloader for an embedded application on the NXP S12ZVL32. I need the application to be aware of the version (major, minor, build) of the bootloader. Is it possible to declare constants at a specific location in the bootloader source so the application can find them?

I tried declaring constants at the start of a #pragma section so I would know where it started.

#pragma CODE_SEG SHADOW_ROM
const UINT8 VER_MAJOR = 0xFE;
const UINT8 VER_MINOR = 0xED;
const UINT16 VER_BUILD = 0xCEFA;

S12 is little endian, so I expected to be able to search the generated .sx file for FEEDFACE, but could not find even FEED or FACE. I have optimization turned off, so the compiler shouldn't be removing unused constants.

Upvotes: 1

Views: 844

Answers (3)

Jeremy
Jeremy

Reputation: 5311

Assuming the bootloader and main app are both under your control, you could have the bootloader jump to the application entry point with a pointer to the version information (and any other information you wish) in a register, for the app to make use of as it sees fit. This eliminates any reliance on fixed memory locations.

Upvotes: 1

Luis Colorado
Luis Colorado

Reputation: 12668

First of all, defining a global const value allows the compiler to use the value only (as an rvalue, as you normally don't reference the address of MATH_PI) and to use the value at each place it could be needed. If you want a constant to be placed in a fixed site, the best way to specify is in assembler.

When I was young, I used to use the whatis unix command to see the #(@) prefixed string in which was normally stored the version info in SCCS revision system. When I had to switch to cvs, I used the idea to define constants in each source file, and search (this can easily be made with grep(1) or some filter program) to scan the binary (yes, the binary code of the program) for lines that started in $Id: so I embedded lines like:

static const char CVS_id[] = "\n$Id$\n";

in source files, and when grep(1)ping the binary, I got the whole revision for each module linked in the executable.

Git doesn't put by itself a version string, but the best (because it doesn't need to put things in absolute addresses) is to put the version info in a string with some specific pattern (git allows to put it for yourself by means of a git hook) not to be confused with other thing by the scanning code. This way you can get the version info of each source file included, with just scanning the binaries.

All of this doesn't require you to fix things to fixed places, so from my point of view allows more flexibility to your memory map and gives you a more portable way of doing things.

Upvotes: 0

kkrambo
kkrambo

Reputation: 7057

Yes, it's possible to locate a constant at a specific memory address. There are different ways to accomplish this and the details of how to do this vary from one tool-chain to another. One way is to use a compiler-specific #pragma or _attribute_ statement in the source code definition to specify the memory address where the constant should be located. Another way is to customize the linker directive (aka linker script or scatter load) file by creating a special memory section at the desired memory address. Then use a #pragma in the code or another linker instruction to place the constant into the custom memory section. See your tool-chain documentation for details.

Upvotes: 1

Related Questions