Keil Logo

C166: Writing Relocatable C Functions for Copying To RAM


Information in this article applies to:

  • C166

QUESTION

I have read application note 138, which describes how to copy assembler routines from Flash memory to RAM for execution. I want to copy C routines. Is this possible?

ANSWER

Yes. However, you must locate the C code in FAR memory (MEDIUM, LARGE, or HLARGE memory model) and you must use the RENAMECLASS directive to specify the correct name for the code class.

Write your C functions in a separate source file as functions. For example:

#pragma LARGE     // only required when you are using the SMALL memory model!
#pragma RENAMECLASS(FCODE=FLASH_CODE)

int PFlash_Erase (void huge *sector_adr)
{
sector_adr = sector_adr;   /* Avoid compiler warning */

return (0);
}

int PFlash_Write (void huge *target_adr, void huge *buffer)
{
target_adr = target_adr;   /* Avoid compiler warning */
buffer = buffer;           /* Avoid compiler warning */

return (0);
}

void PFlash_Reset (void)
{
}

Your C routines are then ready to use!

You will probably receive a linker warning similar to the following:

*** WARNING L14: INCOMPATIBLE MEMORY MODEL
    MODULE:  pflash.obj (PFLASH)
    MODEL:   LARGE

The warning is caused (in this instance) because the memory model of the program is SMALL while the memory model of the PFLASH module must be LARGE. It is safe to ignore this warning.

MORE INFORMATION

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  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.