#define LED_PORTC10 (1 #define LED_PORTC11 (1 #define RAM_ADDR 0xc000000
void (*Run)(void) = (void (*)(void))RAM_ADDR;
void infoFlash(void);
int wait_flash_ready ( INT16U *address, INT16U data );
int writeFlash(INT16U *Address,INT16U Data);
int eraseSector(INT16U* SectorAddr);
int eraseChip(void);
   void PortInit(void);
void LedDisp(int LedStatus);
   //*****************************************
// FLASH WIRTING
//*****************************************
void Main(void)
{ INT32U k ;
INT16U *pdist,*psrc;
rSYSCFG=CACHECFG;
PortInit();
//infoFlash();
eraseChip();
psrc=(INT16U *)0xc300000;
for(k=0;k<0x4000> *psrc++=0x0; //clear ram
psrc=(INT16U *)0xc300000;
pdist=(INT16U *)0x0;
for(k=0;k<0x4000 k ram to> writeFlash(pdist++,*psrc++);
while(1)
{
LedDisp(0);
for(k=0;k LedDisp(2);
for(k=0;k }
}
  
//*****************************************
// init the port
//*****************************************
void PortInit(void)
{
   rPDATC = 0xffff; //All IO is high
rPCONC = 0x0f55ff54;
rPUPC = 0x3000; //PULL UP RESISTOR should be enabled to I/O
}
   //*****************************************
// light led
//*****************************************
void LedDisp(int LedStatus)
{
if((LedStatus&0x01)==0x01)
rPDATC &= (~LED_PORTC10); //LED ON
else
rPDATC |= LED_PORTC10; //LED OFF
if((LedStatus&0x02)==0x02)
rPDATC &=(~LED_PORTC11); //LED ON
else
rPDATC |=LED_PORTC11; //LED OFF
}
   //*****************************************
// show the flash soft id
//*****************************************
void infoFlash()
{
int i,j;
INT16U *pFlash;
*((volatile INT16U *)FLASH_START_ADDR)=FLASH_SID_EXIT;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_DATA_UNLOCK1;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK2)=FLASH_DATA_UNLOCK2;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_SID_QUERY;
for(i=0;i pFlash=FLASH_START_ADDR;
i=0;j=0;
i=(INT16U)*pFlash++;
j=(INT16U)*pFlash;
}
//*****************************************
// erase hold flash
//*****************************************
int eraseChip()
{
*((volatile INT16U *)FLASH_START_ADDR)=FLASH_SID_EXIT;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_DATA_UNLOCK1;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK2)=FLASH_DATA_UNLOCK2;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_ERASE;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_DATA_UNLOCK1;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK2)=FLASH_DATA_UNLOCK2;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_ERASE_CHIP;
if( wait_flash_ready((INT16U *)FLASH_START_ADDR,0xffff) )
return 1;
else return 0;
}
   //*****************************************
// write one falsh word( 16bit)
//*****************************************
int writeFlash(INT16U *Address,INT16U Data)
{ *((volatile INT16U *)FLASH_START_ADDR)=FLASH_SID_EXIT;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_DATA_UNLOCK1;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK2)=FLASH_DATA_UNLOCK2;
*((volatile INT16U *)FLASH_START_ADDR+FLASH_ADDR_UNLOCK1)=FLASH_DATA_WRITE;
*Address=Data;
if(wait_flash_ready(Address,Data))
return 1;
else return 0;
}
   //*****************************************
// wait for operation finish
//*****************************************
int wait_flash_ready ( INT16U *address, INT16U data )
{
INT32U tmp;
INT16U *p;
tmp =0xff;
p=address;
while(((*p)&0x8080)!=(data&0x8080))
{tmp--;
if (tmp==0x0)
return 1; // timeout
}
return 0;
}