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.
4.8 SOFTWARE STACK<br />
Run Time Environment<br />
The section attribute specifies that the function should be placed in a section<br />
named.myDataSection, rather than the default .data section. It does not specify<br />
where the user-defined section is to be located. Again, that must be done in a custom<br />
linker script, as follows. Using the device-specific linker script as a base, add the following<br />
section definition:<br />
.myDataSection 0x1000 :<br />
{<br />
*(.myDataSection);<br />
} >data<br />
This specifies that the output file should contain a section named.myDataSection<br />
starting at location 0x1000 <strong>and</strong> containing all input sections named.myDataSection.<br />
Since, in this example, there is a single variable Mabonga in that section, then the<br />
variable will be located at address 0x1000 in data memory.<br />
The <strong>dsPIC</strong> DSC device dedicates register W15 <strong>for</strong> use as a software Stack Pointer. All<br />
processor stack operations, including function calls, interrupts <strong>and</strong> exceptions, use the<br />
software stack. The stack grows upward, towards higher memory addresses.<br />
The <strong>dsPIC</strong> DSC device also supports stack overflow detection. If the Stack Pointer<br />
Limit register, SPLIM, is initialized, the device will test <strong>for</strong> overflow on all stack operations.<br />
If an overflow should occur, the processor will initiate a stack error exception. By<br />
default, this will result in a processor reset. Applications may also install a stack error<br />
exception h<strong>and</strong>ler by defining an interrupt function named _StackError. See Chapter<br />
8. “Interrupts” <strong>for</strong> details.<br />
The C run-time startup module initializes the Stack Pointer (W15) <strong>and</strong> the Stack Pointer<br />
Limit register during the startup <strong>and</strong> initialization sequence. The initial values are<br />
normally provided by the linker, which allocates the largest stack possible from unused<br />
data memory. The location of the stack is reported in the link map output file.<br />
Applications can ensure that at least a minimum-sized stack is available with the<br />
--stack linker comm<strong>and</strong>-line option. See the “<strong>MPLAB</strong> ® Assembler, Linker <strong>and</strong><br />
Utilities <strong>for</strong> <strong>PIC24</strong> <strong>MCUs</strong> <strong>and</strong> <strong>dsPIC</strong> ® <strong>DSCs</strong> User’s Guide” (DS51317) <strong>for</strong> details.<br />
Alternatively, the stack of specific size may be allocated with a user-defined section in<br />
a custom linker script. In the following example, 0x100 bytes of data memory are<br />
reserved <strong>for</strong> the stack. Two symbols are declared, __SP_init <strong>and</strong> __SPLIM_init,<br />
<strong>for</strong> use by the C run-time startup module:<br />
.stack :<br />
{<br />
__SP_init = .;<br />
. += 0x100;<br />
__SPLIM_init = .;<br />
. += 8;<br />
} >data<br />
__SP_init defines the initial value <strong>for</strong> the Stack Pointer (W15) <strong>and</strong> __SPLIM_init<br />
defines the initial value <strong>for</strong> the Stack Pointer Limit register (SPLIM). The value of<br />
__SPLIM_init should be at least 8 bytes less than the physical stack limit, to allow<br />
<strong>for</strong> stack error exception processing. This value should be decreased further to account<br />
<strong>for</strong> stack usage by the interrupt h<strong>and</strong>ler itself, if a stack error interrupt h<strong>and</strong>ler is<br />
installed. The default interrupt h<strong>and</strong>ler does not require additional stack usage.<br />
© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 69