Hi Xiangjun,
I have some error in the code in the email before.
Here again my code, please advise.
//program 1 kbyte 256 longword
static uint8_t gCopy1K[1024]; //1K of Flash sector
#define FlashStoreStart (0x00FC00u)
volatile uint32_t *const FLASH_Addr = (volatile uint32_t *)&(FTFA->FCCOB3);
#define Program4Byte 0x06
#define Erase1Sector 0x09
#define Read1sSection 0x01
static uint16_t gFlashOffset;
status_t flash_write(void)//const void *src, size_t nbytes)
{
// In order to write to flash: system must be in Run mode (not Vlpr) and
// interrupts must be disabled.
uint8_t TestByte;
uint16_t Offset=0;
uint8_t status;
__disable_irq();
for(Offset=0; Offset<0x400; Offset+=4) //1024/4 times
{
TestByte =FTFA->FSTAT;
while((FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK)==0){}; //CCIF =1?
if((TestByte & 0x30)>0)FTFA->FSTAT |=0x30; //write '1' to clear
FTFA->FCCOB0 =Program4Byte;
*FLASH_Addr = FlashStoreStart+Offset;
FTFA->FPROT0=0x80; //highest section 2K byte
FTFA->FPROT1=0x00;
FTFA->FPROT2=0x00;
FTFA->FPROT3=0x00;
(FLASH_Addr) =(gCopy1K+Offset); //copy 4 bytes
(FLASH_Addr+1) =(gCopy1KOffset1);
(FLASH_Addr+2) =(gCopy1KOffset2);
(FLASH_Addr+3) =(gCopy1KOffset3);
FTFA->FSTAT |=FTFA_FSTAT_CCIF_MASK; //Start execution
while((FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK) ==0){};
}
FTFA->FPROT0=0x00; // turn off the highest section 2K byte
status=(FTFA->FSTAT & (FTFA_FSTAT_RDCOLERR_MASK | FTFA_FSTAT_ACCERR_MASK | FTFA_FSTAT_FPVIOL_MASK | FTFA_FSTAT_MGSTAT0_MASK));
__enable_irq();
return status;
}
// and the Erase 1 sector:
status_t Flash_Erase(void) //const void *sec)
{
uint8_t TestByte;
TestByte =FTFA->FSTAT;
while((FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK) ==0){}; //CCIF =1?
if((TestByte & 0x30)>0)FTFA->FSTAT |=0x30; //write '1' to clear
FTFA->FCCOB0 =Erase1Sector;
*FLASH_Addr =FlashStoreStart;
//FTFA->FCCB1 =0x00;
//FTFA->FCCB2 =0xFC;
//FTFA->FCCB3 =0x00;
FTFA->FPROT0=0x80; //highest section 2K byte
FTFA->FPROT1=0x00;
FTFA->FPROT2=0x00;
FTFA->FPROT3=0x00;
FTFA->FSTAT |=FTFA_FSTAT_CCIF_MASK; //Start Erase
while((FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK) ==0){};
FTFA->FCCOB0 =Read1sSection;
*FLASH_Addr =FlashStoreStart;
//FTFA->FCCB1 =0x00;
//FTFA->FCCB2 =0xFC;
//FTFA->FCCB3 =0x00;
FTFA->FCCOB4 =0x00;
FTFA->FCCOB5 =0x01; // 256 longword
FTFA->FCCOB6 =0x00; //Norm margin level
FTFA->FSTAT |=FTFA_FSTAT_CCIF_MASK; //Start
while((FTFA->FSTAT & FTFA_FSTAT_CCIF_MASK) ==0);
if((FTFA->FSTAT & 0x01)!=0)return 1;
else return 0;
}
Than you in advance.
Albert Zhou
Electronics Engineer
Sensorex Corp.