Running code in RAM using PSoC5 and the GCC compiler

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Firmware engineers are always looking for ways to write more efficient code.  Often this means executing functions as fast as possible.  One way to improve the execution time of your code when developing for PSoC5 is to place your code into RAM.

   

Placing code in RAM using GCC

   

Gcc supports the use of the __attribute__ keyword which allows you to apply special attributes to your code.  There are many attributes you can apply; the one we are interested in is section .

   

The section attribute places code in a specific memory section as defined in the cm3gcc.ld file.  RAM is defined as the .data section.  So, for example, if you wanted to place a function in RAM the code for the function prototype would look like this:

   

void foo (void) __attribute__ ((section(".data")));

   

This method can be used for variables as well.

   

Interrupt Example

   

A great use of this feature is to place interrupt handlers in RAM for faster execution time.  If you define your own ISRs you can do this by placing __attribute__ ((section .data ))) after the ISR prototype like a normal function declaration.

   

If you are using the Cypress generated ISR code you can add a declaration statement to the section of code at the top of the .c file which is provided for the user to include modules and declare variables.

   

Here is an example:

   

// place interrupt in SRAM to improve speed

   

externCY_ISR_PROTO(isr_1_Interrupt) __attribute__ ((section(".data")));

   

for an isr named isr_1.

   

Linker Warning

   

When you place code into the .data section you will get the following warning:

   

Warning: ignoring changed section attributes for .data

   

This is because the .data section does not, by default, expect to have code attributes associated with it.  In this case you can ignore the warning, because you intend to add attributes to the .data section.  Even though the warning indicates that the linker is ignoring your attribute, it will still place your code in RAM.  You can verify this in the map file by checking which section your code has been placed in.

   

To clear this warning you would need to modify the cm3gcc.ld file.  The best approach would be to add a custom section located in RAM for you code.  However, since this is Creator generated source code, changes you make to this file will be overwritten when generating the project APIs.

   

RAM vs Flash execution

   

The following table provides some sample data taken to show the code execution speed from flash vs RAM.  There is about a 30 % improvement in execution time when executing out of RAM vs flash.  This data was taken by toggling a pin in an ISR with a varying amount of code, measured in bytes.  The Cortex M3 was running at 24 MHz. There was no difference in the time it took to get into the interrupt.

                                                                                                                                                                                                                         

Code Size (bytes)

Flash Execution Time

RAM Execution Time

% Difference

400

9.8 usec

7 usec

- 29 %

800

19.4 usec

13.4 usec

- 31 %

1600

38.4 usec

26.8 usec

- 30 %

3200

76.8 usec

53.4 usec

- 30 %

0 Likes
0 Replies