Microchip Technology SW006022-2N Data Sheet

Page of 338
Memory Allocation and Access
 2012 Microchip Technology Inc.
DS52071B-page 131
7.5.2
Declare a New Memory Space
The compiler toolsuite requires information about each additional memory being 
attached via the PMP. In order for the 16-bit device linker to be able to properly assign 
memory, information about the size of memory available and the number of 
chip-selects needs to be provided.
In Chapter 5. “Differences Between MPLAB XC16 and ANSI C” the new pmp mem-
ory space was introduced. This attribute serves two purposes: declaring extended 
memory spaces and assigning C variable declarations to external memory (this will be 
covered in the next subsection).
Declaring an extended memory requires providing the size of the memory. You may 
optionally assign the memory to a particular chip-select pin; if none is assigned it will 
be assumed that chip-selects are not being used. These memory declarations look like 
normal external C declarations:
  extern int external_PMP_memory 
_ _
attribute
_ _
((space(pmp(size(1024),cs(0)))));
Above we defined an external memory of size 1024 bytes and there are no 
chip-selects. The compiler only supports one PMP memory unless chip-selects are 
being used:
  extern int PMP_bank1 
_ _
attribute
_ _
((space(pmp(size(1024),cs(1)))));
  extern int PMP_bank2 
_ _
attribute
_ _
((space(pmp(size(2048),cs(2)))));
Above PMP_bank1 will be activated using chip-select pin 1 (address pin 14 will be 
asserted when accessing variables in this bank). PMP_bank2 will be activated using 
chip-select pin 2 (address pin 15 will be asserted).
Note that when using chip-selects, the largest amount of memory is 16 Kbytes per 
bank. It is recommended that the declaration appear in a common header file so that 
the declaration is available to all translation units.
7.5.3
Define Variables within PMP Space
The pmp space attribute is also used to assign individual variables to the space. This 
requires that the memory space declaration to be present. Given the declarations in the 
previous subsection, the following variable declarations can be made:
  
_ _
pmp
_ _
 int external_array[256] 
          
_ _
attribute
__
((space(pmp(external_PMP_memory))));
external_array
 will be allocated in the previously declared memory  
external_PMP_memory
.  If there is only one PMP memory, and chip-selects are not 
being used, it is possible to leave out the explicit reference to the memory. It is good 
practice, however, to always make the memory explicit which would lead to code that 
is more easily maintained.
Note that, like managed PSV pointers, we have qualified the variable with a new type 
qualifier __pmp__. When attached to a variable or pointer it instructs the compiler to 
generate the correct sequence for access via the PMP peripheral.
Now that a variable has been declared it may be accessed using normal C syntax. The 
compiler will generate code to correctly communicate with the PMP peripheral.