Keil Logo

C51: In-System Flash Programming (Part 2)


Information in this article applies to:

  • C51 Version 7.00 or later
  • PK51 Professional Development's Kit

QUESTION

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?

ANSWER

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:

SROM.H

#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:

  • Defines the memory class that will be stored in SROM user's class.
  • Uses macros from SROM.H do determine length, source and target address (ROM/RAM).
  • Copies the memory class with your flash routines RAM.
  • Calls one of the functions in RAM.
SROM.C

#include
#include
#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
          SROM_MC_SRC(CODE_FLASHMEM),
          SROM_MC_LEN(CODE_FLASHMEM));

flash();                      // calls flash function in RAM

while (1);
}

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

PFLASH.C

#include

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

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


void flash ( void )
{
unsigned int i= 0xFFFF;
   delay(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.

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:
SROM(C:0x1000-C:0x1FFF),
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.

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.