12.3 Variables in Program Space

The 16-bit core families of processors contain hardware support for accessing data from within program Flash using a hardware feature that is commonly called Program Space Visibility (PSV). More detail about how PSV works can be found in device data sheets or Family Reference Manuals (see sections Allocation and Access of Program Memory Objects and PSV Usage with Interrupt Service Routines).

The architecture allows the mapping of one 32K page of Flash into the upper 32K of the data address space via the Special Function Register (SFR) PSVPAG or DSRPAG. By default, the compiler only supports direct access to one single PSV page, referred to as the auto_psv space. In this model, 16-bit data pointers can be used. However, this can make it difficult to manage large amounts of constant data stored in Flash on larger devices.

When the option -mconst-in-code is enabled, const-qualified variables that are not auto are placed in program memory. Any auto variables qualified const are placed on the stack along with other auto variables.

Any const-qualified (auto or non-auto) variable will always be read-only and any attempt to write to these in your source code will result in an error being issued by the compiler.

A const object is usually defined with initial values, as the program cannot write to these objects at runtime. However this is not a requirement. An uninitialized const object is allocated space along with other uninitialized RAM variables, but is still read-only. Here are examples of const object definitions.

const char IOtype = ’A’; // initialized const object

const char buffer[10]; // I reserve memory in RAM

See the Mixing C and Assembly Code section for the equivalent assembly symbols that are used to represent const-qualified variables in program memory.