unable to write data to program flash memory

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

unable to write data to program flash memory

1,464 Views
Gaoya
Contributor I

I am using 56F8345 and want to test how security method works. Before writing back door key  to FM configuration field, I just want to create a function to write data to flash memory.

I  wrote a function according to 56f8300 peripheral user manual . It is  able to write data to Data_flash memory since I could see value changes in debug mode. However, it seems not work for program flash memory. 

Here is the code for writing data to program flash (  to write data to data flash , only need change address and bits in register FMCR):

 

#define data_space  1
#define prog_space  0

 

#define data_flash_address    (*((volatile word *)0x00001000))     // pick up one data flash memory unit for writing test

#define prog_flash_address    (*((volatile word *)0x0000FE00))    // pick up one prog flash memory unit for writing test
 


//create a function to do write on program flash memory

void write_on_progflash( void);

void write_on_progflash( void)
{
   //first check the FMCLKD, if it's not written, no further operation
 
  
   if(FMCLKD &0x0080)
   {
       asm(nop); //FMCLKD is written   
   }
   else
   FMCLKD =0x0032;  //DIV=50 PDIV=0  then FMCLKD =20Mhz/2/51=196KHZ
  
   asm(nop);
  
  
   // select data flash
   FMCR =(FMCR & 0x0fffc)| prog_space ;
   FMCR &=0x0FFDF ;//Clear KEYACC bit to disable Security Key Writing
 
   //test LOCK bit on FMCR
   if(FMCR&0x400) 
    return ; // LOCK is set, so no way to change FMPORT
 
   asm(nop);
  
   FMPROT =0 ; //make the flash space be able to be written
  
   asm(nop);
  
   //check the status of FMUSTAT, CBEIF is set?
   while ((FMUSTAT &0x0080)==0)
   {
   asm(nop);   //CBEIF is 0, buffer is full
    
   }
 
   asm(nop); 
   asm(nop);
   asm(nop);
  
   //now flash is ready for operation
  
   //Before writing data to flash, erase first
  
   prog_flash_address =0;
  
  
   FMCMD = 0x040; //page erase command
  
  
   FMUSTAT |=0x080; //set up CBEIF
  
   //check any error happens?
   if(FMUSTAT & 0x0030)
    return;   // error happens
   
   // no error? check erase finished or not
   while((FMUSTAT & 0x0c0)!=0xc0)
   {
    asm(nop);
   }
  
   asm(nop);
   asm(nop);
   asm(nop);
    
    //ok, now able to write
    prog_flash_address =99;
    
    asm(nop);
    asm(nop);
    
    FMCMD =0x020;
    
     FMUSTAT |=0x080; //set up CBEIF
  
   //check any error happens?
   if(FMUSTAT & 0x0030)
    return;   // error happens
   
   // no error? check erase finished or not
   while((FMUSTAT & 0x0c0)!=0xc0)
   {
    asm(nop);
   }
  
   asm(nop);
   asm(nop);
   asm(nop);
    
 
}

 

Any ideas, thanks in advance.

Labels (1)
0 Kudos
Reply
2 Replies

1,091 Views
kef
Specialist I

I'm not familiar with 56F, but I guess it is the same like in other MCU's. On S08, S12, CF flash is not readable while it is being programmed. This means you can't run code from flash while you are erasing/programming flash. Part of erase/program code should be copied to RAM and executed from there. This may explain why you are able to program data flash and not able to program program flash.

0 Kudos
Reply

1,091 Views
Gaoya
Contributor I

Hi Kef,

Thank you for reply.

I also thought the program should run at RAM when program/erase need to be done at program flash memory. But so far I could not find any application notes related to running part of program in RAM and documentation of ELF linker in targeting MC56f83XX is not so clear.  Do you know  are there some example and reference? 

0 Kudos
Reply