Hi,
I am implementing a functionality of IAP in LPC2214, for start up I have written a simple code in which I am able to ease Sector 0 & other Sectors as Well, but when I try to write any data on sector 0 then it is not written it shows all 0xFF 0xFF ............. But in all the sectors data has been written succesfully only if I comment the sector 0 ERASE & WRITE function...
I have selected the ARM Mode in keil.
Can any one let me know what is the reason why I am not able to write on sector 0 ? what are the parameters that has to be taken care while writing on sector 0 ?
#include "FlashOS.H" #define M8(adr) (*((volatile unsigned char *) (adr))) void IAP_download(void); volatile int ret; unsigned char TempBuf[1024] = " CMS CMS CMS "; static unsigned char TempBuf2[1024]; unsigned int Cnt; void IAP_download (void ) { unsigned long n; unsigned short Cnt; ret = Init(0, 48000000, 1); // Erase Sector 0 ret |= EraseSector(0x00000000); for (Cnt = 0; Cnt < 1024; Cnt++) { TempBuf2[Cnt] = '*'; } ret |=ProgramPage(0x00000000,1024,(unsigned char*) TempBuf2 );
/* main Function */ int main(void) { IODIR0 = 0xFFFFFFFF; IODIR1 = 0xFFFFFFFF; // #pragma ARM VICIntEnClr = 0xFFFFFFFF; IAP_download( ); // #pragma thumb while (1); }
/* FalshPrg.c*/ #define STACK_SIZE 64 // Stack Size #define SET_VALID_CODE 1 // Set Valid User Code Signature unsigned long CCLK; // CCLK in kHz struct sIAP { // IAP Structure unsigned long cmd; // Command unsigned long par[4]; // Parameters unsigned long stat; // Status } IAP; void IAP_Execute (struct sIAP *pIAP); unsigned long GetSecNum (unsigned long adr) { unsigned long n; n = (adr >> 13) & 0x1F; if (n >= 24) { n -= 14; } else if (n >= 8) { n = 8 + (n >> 4); } return (n); } int Init (unsigned long adr, unsigned long clk, unsigned long fnc) { unsigned int Cntr; CCLK = (1049*(clk >> 10)) >>10; MEMMAP = 0x01; for(Cntr=0; Cntr<50000; Cntr++); for(Cntr=0; Cntr<50000; Cntr++); return (0); } int UnInit (unsigned long fnc) { return (0); } int EraseChip (void) { IAP.cmd = 50; IAP.par[0] = 0; IAP.par[1] = 16; IAP_Execute (&IAP); if (IAP.stat) return (1); IAP.cmd = 52; IAP.par[0] = 0; IAP.par[1] = 16; IAP.par[2] = CCLK; IAP_Execute (&IAP); if (IAP.stat) return (1); return (0); } int EraseSector (unsigned long adr) { unsigned long n; unsigned int Cntr; n = GetSecNum(adr); IAP.cmd = 50; IAP.par[0] = n; IAP.par[1] = n; IAP_Execute (&IAP); if (IAP.stat) return (1); for(Cntr=0; Cntr<50000; Cntr++); for(Cntr=0; Cntr<50000; Cntr++); IAP.cmd = 52; IAP.par[0] = n; IAP.par[1] = n; IAP.par[2] = CCLK; IAP_Execute (&IAP); if (IAP.stat) return (1); for(Cntr=0; Cntr<50000; Cntr++); for(Cntr=0; Cntr<50000; Cntr++); return (0); } int ProgramPage (unsigned long adr, unsigned long sz, unsigned char *buf) { unsigned long n; unsigned int Cntr; #if SET_VALID_CODE != 0 if (adr == 0) { n = *((unsigned long *)(buf + 0x00)) + *((unsigned long *)(buf + 0x04)) + *((unsigned long *)(buf + 0x08)) + *((unsigned long *)(buf + 0x0C)) + *((unsigned long *)(buf + 0x10)) + *((unsigned long *)(buf + 0x18)) + *((unsigned long *)(buf + 0x1C)); *((unsigned long *)(buf + 0x14)) = 0 - n; } #endif n = GetSecNum(adr); IAP.cmd = 50; IAP.par[0] = n; IAP.par[1] = n; IAP_Execute (&IAP); if (IAP.stat) return (1); for(Cntr=0; Cntr<50000; Cntr++); for(Cntr=0; Cntr<50000; Cntr++); IAP.cmd = 51; IAP.par[0] = adr; IAP.par[1] = (unsigned long)buf; IAP.par[2] = 1024; IAP.par[3] = CCLK; IAP_Execute (&IAP); if (IAP.stat) return (1); for(Cntr=0; Cntr<50000; Cntr++); for(Cntr=0; Cntr<50000; Cntr++); return (0); }