Program Space Visibility - Reading Flash Memory On a 16-bit PIC® MCU

Program Space Visibility (PSV) maps select blocks of flash program memory into the upper 32 KB of data memory space to provide quick access to literal constants and Flash-based data tables.

PSV access is read-only. PSV does not provide the ability to write to flash memory. See the Reading from and Writing to Flash Memory page for details on how to write to Flash memory. The Extended Data Memory page shows how to map extended RAM to the upper 32 kBytes of data space.

Two registers are used to enable PSV:

  • CORCON - the Core Control register
  • PSVPAG - the PSV Page register

When the PSV enables bit in CORCON, CORCON<2> is set, the 32k Byte block of program memory specified by the value of PSVPAG will appear at data memory addresses 0x8000-0xFFFF. Only the lower 16-bit of each address word will appear in the PSV space.

Programming Considerations

MPLAB® XC16 C compiler considers the PSVPAG register a compiler managed resource. While it is possible for the application to manipulate the PSV registers, relying on the compiler to manage PSV access is the recommended approach.

MPLAB XC16's default memory model, mconst-in-code, places all constants in program memory. When const is used to create a constant, the compiler will place the data in program memory and ensure it is formatted so it can be read from PSV. Constants can also be created and placed into the PSV window with the (space(psv)) attribute (see the following example).

When constants are placed into PSV, the compiler ensures that any application program accessing the constant will use the re-mapped PSV address.

Example

The following are examples of creating constants with const and (space(psv)) attributes.

code.png

The preceding code will cause the constants example1, example2, and example3 to be placed into program memory. In this application, the linker placed the constants at addresses 0x029E, 0x02A2, and 0x02A4. The 32-bit long constant example1 was placed in the lower 16-bits of adjacent program memory words so that it can be correctly read through PSV.

pm.png

The following disassembly code demonstrates how the data elements located in program memory locations 0x029E, 0x02A2, and 0x02A4 (example1, example2, and example3) are accessed as if they were in data memory addresses 0x829E, 0x82A2, and 0x82A4.

diss.png

Interrupt Service Routine Considerations

PSV data accesses, especially those using pointers, can take several instructions to complete. If an interrupt were to occur during a PSV access, the value of PSVPAG could be changed by the Interrupt Service Routine (ISR). This could result in the main program accessing the wrong constant value. To prevent such an occurrence, the compiler automatically inserts code to save and restore PSVPAG into each ISR.

While it is prudent to preserve the contents of PSVPAG, in some cases, saving PSV is NOT needed. For applications with only one managed PSV page or for ISRs which do not access PSVPAG, the saving and restoring of PSVPAG offers no benefits. Applications needing to minimize interrupt latency may wish to eliminate the preservation of PSVPAG.

MPLAB XC16 can be told to omit the PSV saving code with the interrupt attribute of no_auto_psv. ISRs defined with the no_auto_psv attribute will not have the code which saves and restores PSVPAG. If an ISR is defined with the attribute auto_psv or defined without an attribute, the PSVPAG preservation code will be included with the ISR.

isrcode.png
isrmem.png

 Learn More

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.