writing to flash

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

writing to flash

Jump to solution
1,554 Views
electronicfan
Contributor II
Hello all,

i have a litle problem with my code to write in flash.

Code  for RG60:
void main(void) {  MCU_init();       erase_flash();  program_flash();
  ...
}

#pragma CODE_SEG MY_ADDRESSES
unsigned char addr[8];

#pragma CODE_SEG MY_ADDRESS_CODE
erase_flash(void)
{
   addr[0] = 0xCC;       // write in address array to start flash operation
   if (FSTAT_FACCERR==1)         // flash access error?
    {
      FSTAT = FSTAT & 0x30;      // clear FACCERR flag
    }
   FCMD = mPageErase;            // flash command -> page erase 
   FSTAT_FCBEF = 1;              // clear flash command buffer empty flag
                                 // and launch command
   _asm NOP;                     // wait 4 cycles
   _asm NOP;
   _asm NOP;
   _asm NOP;
   while(FSTAT_FCCF==0){}        // wait for flash command complete
}

program_flash(void)
{
   addr[0] = 0xCC;       // write in address array to start flash operation
   if (FSTAT_FACCERR==1)         // flash access error?
    {
      FSTAT = FSTAT & 0x30;      // clear FACCERR flag
    }
   FCMD = mByteProg;             // flash command -> byte program
   FSTAT_FCBEF = 1;              // clear flash command buffer empty flag
                                 // and launch command 
   _asm NOP;                     // wait 4 cycles
   _asm NOP;
   _asm NOP;
   _asm NOP;    
    
   while(FSTAT_FCCF==0){}        // wait for flash command complete
}

my prm-file:

NAMES END

SEGMENTS /* Here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */
    ROM                      =  READ_ONLY    0x182C TO 0xFFAF;   // flash
    Z_RAM                    =  READ_WRITE   0x0046 TO 0x00FF;
    RAM                      =  READ_WRITE   0x0100 TO 0x0845;   // ram
   
    MY_RAM                   =  READ_ONLY    0x0846 TO 0x0A46;   // flash
    MY_RAM2                  =  READ_WRITE   0x0A47 TO 0x17FF;   // flash
    ROM2                     =  READ_ONLY    0xFFC0 TO 0xFFDF;   // flash
   
END

PLACEMENT
    DEFAULT_RAM                         INTO  RAM;
    DEFAULT_ROM, ROM_VAR, STRINGS       INTO  ROM;
    _DATA_ZEROPAGE, MY_ZEROPAGE         INTO  Z_RAM;
    ...
    MY_ADDRESS_CODE                     INTO  ROM;
    ...   
    MY_RF_ADDRESSES                     INTO  MY_RAM;   // my flash
   
END
STACKSIZE 0x50  


I hope, somebody can help me and can find my mistake.
Labels (1)
Tags (1)
0 Kudos
1 Solution
370 Views
crane
Contributor III
You can't execute the code from ROM. You need to make a segment in RAM to where you copy the code. Check which addresses the routines will get i RAM, and the make asm command "asm jsr FLASH_ERASE_START;" for instance, where you have #define FLASH_ERASE_START 0x100, or whichever address you link to.
 
Håkan

View solution in original post

0 Kudos
1 Reply
371 Views
crane
Contributor III
You can't execute the code from ROM. You need to make a segment in RAM to where you copy the code. Check which addresses the routines will get i RAM, and the make asm command "asm jsr FLASH_ERASE_START;" for instance, where you have #define FLASH_ERASE_START 0x100, or whichever address you link to.
 
Håkan
0 Kudos