ARM Technical Support Knowledge Articles

IN-SYSTEM FLASH PROGRAMMING (PART 1)

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I am writing a program that downloads a new program version into the Flash memory of my target system. I have a function that handles downloading and writing to the Flash and then restarts my target. I want to copy this function from Flash to RAM and execute it there.

Is it possible to do this with the C51 tools?

ANSWER

Yes. It's possible to implement this capability using the PK51 Professional Developer's Kit. The linker features required are implemented only in the LX51 Extended Linker.

The following example copies one C program function from ROM to RAM and executes it. A complete example is available in the attachment section of the article.

SROM Memory Class

The SROM class is a special code class created by the linker for segments that are stored in one location and executed from another. You must define a memory space for the SROM class and you must specify which segments (functions) are a part of this class.

Before you invoke any SROM code segments, you must first copy them from ROM to RAM. The SROM.H header file contains macros you may use to obtain the storage address, execution address, and segment length for the SROM class. Use the memcpy function to copy the SROM class to RAM.

Project Files

SROM.H

This header file defines a number of macros you can use to access information about the SROM class.

#define SROM_PS_TRG(n) ((void *)       &_PR_##n##_T_)
#define SROM_PS_SRC(n) ((void *)       &_PR_##n##_S_)
#define SROM_PS_END(n) ((void *)       &_PR_##n##_E_)
#define SROM_PS_LEN(n) ((unsigned int) &_PR_##n##_L_)
#define SROM_PS_BNK(n) ((unsigned int) &_PR_##n##_B_)
SROM.C

This source file...

#include <string.h>
#include <srom.h>

extern void flash (void);     // Function to copy into RAM

SROM_PS (FLASH_PFLASH)        // Define SROM variables

void main (void)
{
// Copy flash function to RAM
memcpy (SROM_PS_TRG(FLASH_PFLASH),
        SROM_PS_SRC(FLASH_PFLASH),
        SROM_PS_LEN(FLASH_PFLASH));

// Call flash function in RAM
flash();

while (1);
}
PFLASH.C

The following function is stored in ROM (in the SROM class) and executes from RAM.

#include <intrins.h>

// This function is stored in ROM
// and executes from RAM.

void flash (void)
{
volatile unsigned char i;

for (i = 0; i < 100; i++)
  {
  _nop_();
  }


// 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.
}

Linker Directives

The execution and storage addresses for the SROM class are defined in uVision in the Options for Target - LX51 locate tab. The flash function in PFLASH.C is stored in SROM but is linked for execution at address 0x2000. Several linker parameters are required to do this:

LX51 Locate - User Classes:  SROM(C:0x1000-C:0x1FFF)

This tells the linker to locate the SROM class in CODE memory in the 1000h-1FFFh address range.

LX51 Locate - User Segments: ?PR?FLASH?PFLASH (C:0x2000) []

This tells the linker to link the ?PR?FLASH?PFLASH segment for execution at address 0x2000 but stored anywhere in the SROM space. Space is reserved at 0x2000 for length of the SROM class. After linking, you may check the map file to make sure the linker produced the expected results. Be sure to look in the ACTIVE MEMORY CLASSES OF MODULE section for the address range of SROM:

BASE        START       END         USED      MEMORY CLASS
==========================================================
X:000000H   X:000000H   X:00FFFFH             HDATA
C:000000H   C:000000H   C:007FFFH   000129H   CODE
C:000000H   C:000000H   C:007FFFH             ECODE
B00:0000H   C:000000H   C:007FFFH             HCONST
B00:0000H   C:001000H   C:001FFFH   00000CH   SROM
I:000000H   I:000000H   I:00007FH   000009H   DATA
I:000000H   I:000000H   I:0000FFH   000001H   IDATA

Check the MEMORY MAP OF MODULE section for the flash function:

START     STOP      LENGTH    ALIGN  RELOC    MEMORY CLASS   SEGMENT NAME
=========================================================================
.
.
.
* * * * * * * * * * *   C O D E   M E M O R Y   * * * * * * * * * * * * *
.
.
.
*** '?PR?FLASH?PFLASH'  execution at: 002000H
001000H   00100BH   00000CH   BYTE   UNIT     SROM           ?PR?FLASH?PFLASH
00100CH   001FFFH   000FF4H   ---    ---      **GAP**
*** '?PR?FLASH?PFLASH' stored at: 001000H   execution at: 002000H
002000H   00200BH   00000CH   BYTE   UNIT     CODE           ?PR?FLASH?PFLASH

Check the PUBLIC SYMBOLS OF MODULE section for the flash function SROM symbols:

      VALUE       CLASS    TYPE      PUBLIC SYMBOL NAME
      =================================================
.
.
.
      0000000CH   NUMBER   ---       _PR_FLASH_PFLASH_L_
      01001000H   NUMBER   ---       _PR_FLASH_PFLASH_S_
      01002000H   NUMBER   ---       _PR_FLASH_PFLASH_T_
.
.
.

Executing from RAM

The RAM memory you copy the flash function to must be von-Neumann wired.

Simulating the Project

The following debugger script is required to map the von Neumann memory for read, write, and execution:

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

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

MORE INFORMATION

SEE ALSO

ATTACHED FILES

Request the files attached to this knowledgebase article.

Article last edited on: 2005-09-22 12:50:05

Rate this article

[Bad]
|
|
[Good]
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