Hi I am trying to write to flash on a LPC2138. I am using Keils sample code with just the sector number and the ram address changed. Cannot get anything to happen other than I must reconnect to device. Compiling in thumb. Freq=60MHz Here is the code Any help appliciated
typedef void (*IAP)(unsigned int [],unsigned int []); IAP iap_bypointer; void iap_byfunction (unsigned *cmd,unsigned *rslt,unsigned entry);
unsigned int command[5]; unsigned int result[5]; char Ram_Arry[512] = "Hello World"; char *Ram_Pointer;
void flash_write(void) { unsigned char index;
iap_bypointer = (IAP) 0x7FFFFFF1; //set IAP entry address in function pointer
Ram_Pointer = &Ram_Arry;
for (index = 0; index<0x0B; index++) //Copy data to be written to flash into the RAM { *Ram_Pointer = Ram_Arry[index]; Ram_Pointer++; } Ram_Pointer = &Ram_Arry;
command[0] = 0x36; //command code for "Read part ID" iap_bypointer(command,result);
command[0] = 50; //Prepare sector ten for a write operayion command[1] = 10; command[2] = 10; iap_bypointer(command,result);
command[0] = 52; //erase sector ten command[1] = 10; command[2] = 10; command[3] = 60000; //Cclk == 12Mhz Pll is disabled in startup code iap_bypointer(command,result);
command[0] = 51; //write 512 bytes from address Pointer command[1] = 0x00018000; //to 0x0000A000 in flash memory; command[2] = Ram_Pointer; command[3] = 512; command[4] = 60000; // see erase comment above
while(1) { ; }
}
You must turn off interrupts while programming flash sectors, or move interrupt vector table and all ISR into RAM. Your application must not try to access any part of the flash.
Interrupts are turned off (in Main before call to flash_write()). There are no Flash accesses. Can see the string in Ram and although I leave for 30secs, nothing gets written into flash. No other code is called, just this function from main.
Have changed Ram_pointer to: Ram_Pointer = 0x40001000;
and adjusted the write command to: command[0] = 51; command[1] = 0x00018000; command[2] = 0x40001000; command[3] = 512; command[4] = 60000;
Still nothing. Have planned to wait until status reg shows done, however, the forever loop should mean it finishes? Can you tell me why command is "51" rather than "5110" as per manual?
Maybe someone could answer this question (maybe this is the problem)
In the Keil sample code, sector 5 is prepared for write and then when the write command is invoked, the address used is:
command[1] = 0x0000A000;
However, the LPC2138 manual gives sector 5 address as:
0X0000 5000 - 0X0000 5FFF
So 0x0000A000 is outside this range.
where do Keil get 0x0000A000 from and if this is correct, what address should I use for sector 10?
I don't have the manual for the LPC2148, but the Keil addresses (0xa000 for sector 5) seems to match the manual contents for LPC2119, 2129, 2194, 2294).
Check the user manual for your specific processor. There is one chapter about IAP somewhere at the end. It should contain a table with all sector numbers, and their addresses.
Got it working, it seems that I had a problem with my JTAG. (address is fine). Note also that a copy and paste error at the end where I deleted the write function call when posting and removing comments. Many thanks for your help