This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Program Memory Constants

Note: This was originally posted on 22nd July 2013 at http://forums.arm.com

I am using a Cortex-M0 with Keil uVision4.  How are constants located in program memory and how can these program memory constants be changed by code?
  • Note: This was originally posted on 24th July 2013 at http://forums.arm.com

    What is it that you're trying to accomplish?

    If they change then they are not really constants; if the code is running and you change the constants (say, from a debugger) then there's no guarantee that the code will still work.  For example two functions might share part of a constant literal.  It might be better to store the values in variables.

    The compiler has various ways encoding constants.  A constant might be part of an instruction, a piece of data in the same section as code or as a separate (read only) data section.

    Changing a constant at runtime (once you've located it) might be simple if it's in RAM or tricky if it's in FLASH.
  • Note: This was originally posted on 24th July 2013 at http://forums.arm.com

    I want to store calibration constants for an external sensor in program flash since there is no onboard EEPROM. I have code that performs the calibration and determines the constants. Most MCU's that I have worked with have assembler commands that write directly to program flash.
  • Note: This was originally posted on 25th July 2013 at http://forums.arm.com

    There are two parts to the problem:

    1. getting the compiler to store the constants in just one place in FLASH

    2. updating that place.

    The surest way of accomplishing #1 would be to define the the constants in a separate asm file.  But, using asm is probably a bit overkill; if you define them in a separate C file (and don't let the compiler optimize across source files) you'll probably be fine:

    calibration.h

    struct CalibrationData {
      ...
    };
    extern const struct CalibrationData mycal;


    constants.c:

    #include "calibration.h"


    extern const struct CalibrationData mycal __attribute__((section("cal_data"))) = {
      ...
    };


    Now you can specify where in FLASH to place the "cal_data" section in the scatter file/linker script.  You're code can find the values with mycal.foo, &mycal and sizeof(mycal).

    I can't really help with part #2; it is specific to the microcontroller and FLASH parts that you're using.  Find out from the manufacturer(s) if they have examples/libraries to do In-Application FLASH Programming (IAP).