MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
16-Bit C <strong>Compiler</strong> User’s Guide<br />
For example, to place a variable in the auto_psv space, which will cause storage to<br />
be allocated in Flash in a convenient way to be access by a single PSVPAG setting,<br />
specify:<br />
unsigned int FLASH_variable __attribute__((space(auto_psv)));<br />
Other user spaces that relate to Flash are available:<br />
• space(psv) - a PSV space that the compiler does not access automatically<br />
• space(prog) - any location in Flash that the compiler does not access automatically<br />
Note that both the psv <strong>and</strong> auto_psv spaces are appropriately blocked or aligned so<br />
that a single PSVPAG setting is suitable <strong>for</strong> accessing the entire variable.<br />
6.2.2 Managed PSV Access<br />
Just placing something into Flash using the space attribute does not mean the compiler<br />
will be able to manage the access. The compiler requires that you identify variables in<br />
a special way. This is done because the managed PSV can be less efficient than managing<br />
the PSVPAG by h<strong>and</strong> (though far less complicated).<br />
The compiler introduces several new qualifiers (CV-qualifiers <strong>for</strong> the language lawyers<br />
in the audience). Like const-volatile qualifier, the new qualifiers can be applied to<br />
pointers or objects. These are:<br />
• __psv__ <strong>for</strong> accessing objects that do not cross a PSV boundary, such as those<br />
allocated in space(auto_psv) or space(psv)<br />
• __prog__ <strong>for</strong> accessing objects that may cross a PSV boundary, specifically<br />
those allocated in space(prog), but it may be applied to any object in Flash<br />
Typically there is no need to specify __psv__ or __prog__ <strong>for</strong> an object placed in<br />
space(auto_psv), though there is no reason why it could be not done.<br />
Moving the FLASH_variable, from the previous section, into an normal Flash space<br />
<strong>and</strong> requesting that the compiler manage the space is easy:<br />
__psv__ unsigned int FLASH_variable __attribute__((space(psv)));<br />
Reading from the variable now will cause the compiler to generate code that adjusts<br />
the PSVPAG SFR as necessary to access the variable correctly. These qualifiers can<br />
equally decorate pointers:<br />
__psv__ unsigned int *pFLASH;<br />
produces a pointer to something in PSV, which can be assigned to a managed PSV<br />
object in the normal way. For example:<br />
pFLASH = &FLASH_variable;<br />
6.2.3 ISR Considerations<br />
A data access using managed PSV pointers is definitely not atomic, meaning it can<br />
take several instructions to complete the access. Care should be taken if an access<br />
should not be interrupted.<br />
Furthermore an interrupt service routine (ISR) never really knows what the current<br />
state of the PSVPAG register will be. Un<strong>for</strong>tunately the compiler is not really in any<br />
position to determine whether or not this is important in all cases.<br />
The compiler will make the simplifying assumption that the writer of the interrupt service<br />
routine will know whether or not the automatic, compiler managed PSVPAG is required<br />
by the ISR. This is required to access any constant data in the auto_psv space or any<br />
string literals or constants when the default -mconst-in-code option is selected.<br />
When defining an interrupt service routine, it is best to specify whether or not it is necessary<br />
to assert the default setting of the PSVPAG SFR.<br />
This is achieved by adding a further attribute to the interrupt function definition:<br />
DS51284H-page 82 © 2008 <strong>Microchip</strong> Technology Inc.