Keil Logo

C51: In-System Flash Programming (Part 1)


Information in this article applies to:

  • C51 Version 7.00 and later

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 is possible to implement this 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
    Contains macros that return the location and size of SROM segments. This file is located in the \KEIL\C51\INC folder.
  • SROM.C
    Contains the C code for copying the FLASH function from ROM to RAM and calling it. This module may be extended to the actual function required by your software.
  • PFLASH.C
    Contains the actual function that includes the programming routines for the PFlash area in the 8051 device. The function in this module is stored in the segment name ?PR?FLASH?PFLASH with the class name SROM.
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...

  • Defines what function is going to be stored in the SROM user's class.
  • Uses macros from SROM.H to determine length, source, and target address of the SROM class.
  • Copies the function to be executed from ROM to RAM.
  • Calls the function in RAM.
#include
#include

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

// 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 µVision 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

  • Refer to SEGMENTS in the LX51 User's Guide.

SEE ALSO


ATTACHED FILES

Request the files attached to this knowledgebase article.

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.