ARM Technical Support Knowledge Articles


Applies to: C51 C Compiler


Information in this article applies to:


I am trying to write a program that will download a new version of the program into a scratch area of flash. My program will execute several routines in RAM that are placed in a separate C file located in a known location that will write the downloaded code to the beginning of flash, and then reset to start the new image.

Is it possible to implement it for C51? How can I implement it?


Yes, it's possible to implement it using PK51. You must have the PK51 professional development kit in order to use the extended linker functionalities required by this example ( LX51 ).

This example is going to copy all functions from your separate source file to RAM and execute them.

You may request the FLASH project in the attachments section below.

The project uses the following files:

Copying from ROM and executing from RAM example for C51 Microcontrollers

The FLASH demonstrates the features of the Lx51 Linker for
In-System Flash Programming practice.

The FLASH project contains the following source files:

SROM.C        Contains the C code for copying and calling the FLASH function in RAM
              This module may be extended to the actual function required
              by your software.

SROM.H        Contains modified macros to access xdata( RAM ) and
                code ( ROM ) segments.

PFLASH.C      Should contain programming routines for the PFlash area
              in the 8051 device.
              All the functions of this module are going to be placed
              in the user memory class CODE_FLASHMEM.
             Those functions will be copied to RAM by SROM.C

Linker parameters.:

LX51 Locate - User Classes:  SROM(C:0x1000-C:0x1FFF)
                            :  CODE_FLASHMEM(C:0x2000-C:0x2FFF) [] )

        * Execute at address 0x2000 and store it anywhere in SROM

The macros below are described in detail in the Ax51 Macro Assembler and Utilities ( Segment and Class Information, Page #291 ). The following pointers were modified to allow the macros to handle the target address in XDATA and the source address in CODE:


#define SROM_MC_TRG(n) ((void xdata *)      &_##n##_T_)
#define SROM_MC_SRC(n) ((void code *)       &_##n##_S_)
#define SROM_MC_END(n) ((void code *)       &_##n##_E_)
#define SROM_MC_LEN(n) ((unsigned int)       &_##n##_L_)

The SROM.C does the following tasks:


#include <string.h>
#include <intrins.h>
#include "srom.h"              // SROM Handling definitions

extern void flash ( void );     // first function to be executed in RAM.

SROM_MC (CODE_FLASHMEM)         // define SROM program segment

void main (void)
memcpy (SROM_MC_TRG(CODE_FLASHMEM),     // copy flash function from ROM to RAM

flash();                      // calls flash function in RAM

while (1);

All the functions in this module will be copied and executed in RAM ( von- Neumann ).


#include <intrins.h>

#pragma USERCLASS(CODE = FLASHMEM)      // user class CODE_FLASHMEM

void delay ( unsigned int i )
while (i--);

void flash ( void )
unsigned int i= 0xFFFF;


// This function should NOT return since
// the caller was probably overwritten when
// the flash was reprogrammed.
// It should RESET the CPU or jump to 0x0000.

The execution and storage addresses are defined in the Options for Target - LX51 locate tab. For this example, the memory class CODE_FLASHMEM is stored at anywhere in SROM class, and is executed at address 0x2000.

LX51 Locate tab

User classes:
CODE_FLASHMEM(C:0x2000-C:0x2FFF) [ ] )

You may check the map file to confirm the linker performed the correct operation. Here's the section of the map file that shows what happened with the CODE_FLASHMEM memory class:

*** '?PR?_DELAY?PFLASH'  execution at: 002000H
001000H   00100AH   00000BH   BYTE   UNIT     SROM           ?PR?_DELAY?PFLASH
*** '?PR?FLASH?PFLASH'  execution at: 00200BH
00100BH   001013H   000009H   BYTE   UNIT     SROM           ?PR?FLASH?PFLASH
001014H   001FFFH   000FECH   ---    ---      **GAP**
*** '?PR?_DELAY?PFLASH' stored at: 001000H   execution at: 002000H
002000H   00200AH   00000BH   BYTE   UNIT     CODE_FLASHMEM  ?PR?_DELAY?PFLASH
*** '?PR?FLASH?PFLASH' stored at: 00100BH   execution at: 00200BH
00200BH   002013H   000009H   BYTE   UNIT     CODE_FLASHMEM  ?PR?FLASH?PFLASH

The RAM memory should be von-Neumann wired and the following simulation script is required in order to see the code being read, written and executed from RAM.:

map x:0x2000,x:0x2FFF read write exec vnm

See the Attached Files section below to download this example project.




Request the files attached to this knowledgebase article.

Article last edited on: 2005-09-22 12:49:57

Rate this article

Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential