Bridge Ji

unable to write data to program flash memory

Discussion created by Bridge Ji on Aug 8, 2011
Latest reply on Aug 11, 2011 by Bridge Ji

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.

Outcomes