Function/ Variable at an Absolute Location: PSoC® 3, PSoC 4, and PSoC 5LP - KBA84773

Version: *C

 

Question:

How do you place a function or a variable at an absolute memory section (in SRAM or flash) in PSoC 3, PSoC 4, and PSoC 5LP, with the Keil, GNU Compiler Collection (GCC), and Microcontroller Development Kit (MDK) compilers?

 

Answer:

To place a variable in an absolute location of SRAM in PSoC 3 (Keil compiler), follow these steps:

 

Automatic (local) and global variables can be located at absolute memory locations in your C code by using the _at_ keyword. The usage for this feature is:

 

<memory_type> type variable_name _at_ constant;

 

where:

 

memory_type      is the memory type, e.g., idata or xdata. If excluded, the default memory type is used.
type                        is the variable type, e.g., uint8.
variable_name     is the variable name.
constant                is the address where the variable is to be located.

 

The following example places an integer variable in xdata (the default) at location 0x1000.

 

int myVariable _at_ 0x1000;

 

To place a constant in flash at any address in PSoC 3 (Keil Compiler), please follow these steps:

 

Local or global constant variables may be located at flash memory locations in your C code using the CYCODE or const keyword. The usage for this feature is:

 

CYCODE = <type> <variable_name> = constant_value;
or
const <type> <variable_name> = constant_value;
where
<type>                         is the variable type
<variable_name>       is the variable name
<constant_value>      is the constant value to be stored in the variable <variable_name>.

 

The following example places the integer variable “myVariable” with a constant value of 1000 in flash.

 

CYCODE int myVariable = 1000;

 

Follow the steps mentioned below to place a constant at an absolute flash location:

 

  1. Initialize a constant as shown below:
      const uint8 code MyFlashData=12;
  2. Place the Linker command under Project > Build Settings > DP8051 Keil 9.51 > Linker > Command Line (SE(?CO?MAIN(C:0X5A5A)) ;(See Figure 1) Here, MAIN indicates the name of the file in which the constant is used (main.c) and 0x5A5A is the relative flash location (absolute flash location with respect to the memory map would be 0x01005A5A).
     
      Figure 1 Linker Command for placing constant at absolute flash location
      Linker_Command_for_placing_constant_at_absolute_flash_location.png

 

PSoC Creator also provides a convenient macro CY_NOINIT, which places a variable in the .noinit section. This section is used for variables that are not initialized.

 

For example, the following code places the variable foo in the .noinit section:

 

uint8 foo CY_NOINIT;

 

The following features are not possible with the PSoC 3 Keil compiler:

 

  • Initializing the variable placed in an absolute location of SRAM or flash
  • Function in an absolute location of flash
  • Function in SRAM (any address)

 

See AN89610 - PSoC® 4 and PSoC 5LP ARM Cortex Code Optimization to understand how this can be done for PSoC 4 or PSoC 5LP with the GCC or MDK compiler.