Keil Logo

__ram: RAM Function

The CARM Compiler supports RAM functions which are copied to RAM for execution. The __ram function attribute defines RAM functions.

<[>type<]> funcname (<[>args<]>) __ram


type is the type of the value returned from the function. If no type is specified, int is assumed.
funcname is the name of the function.
args is the argument list for the function.
__ram indicates that the function should be copied to RAM before execution.

For example:

#define M16(adr) (*((volatile unsigned short *) (adr)))

void my_prog_func (unsigned short val, unsigned short *adr) __ram {
  M16(base_adr + 0xAAA) = 0xAA;    // enter programming mode
  M16(base_adr + 0x554) = 0x55;
  M16(base_adr + 0xAAA) = 0xA0;
  M16(adr) = val;                  // program value
  while (M16(adr) != val);         // wait until programmed

The function my_prog_func may be called in the same way as any other function. The function code in RAM is located with the ERAM memory class. You may need to specify an address range for RAM with the LA Linker/Locater CLASSES directive. For example, ERAM (0x40000000-0x40003FFF). In µVision, this is specified in the dialog Project — Options — LA Locate — User Classes.

There are several reasons to execute functions from RAM.

  • Functions in RAM may execute faster than functions in ROM because of the access speed of the memory.
  • If your application reprograms the Flash memory of your device, you may copy the Flash reprogramming function to RAM for execution. Once the function is finished, the Flash is reprogrammed and the function may reset or restart the microcontroller.

The __ram attribute affects the generated code of the function as follows:

  • The function is copied to RAM on program startup. The program code may be located using the ERAM memory class.
  • The CARM Compiler generates function calls to the RAM address of the function.


  • Functions copied to and executed from RAM may call other functions that are located in ROM. If the RAM function does not disable or erase the (Flash) ROM, that does not cause a problem. However, if the RAM function clears ROM it cannot call functions stored in ROM.
  • The CARM Compiler does not check to determine if a RAM function calls other routines that are stored in ROM space. This includes library routine for some division and floating-point operations. Therefore, you must check the compiled program code for calls to ROM routines (if ROM space is not available as described above).
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.