I have the following code for my LPC2138 that writes to RAM, writes to ROM through IAP call, and then checks to see if it wrote to RAM and ROM correctly:
void testMem(){ unsigned char index; unsigned command[5]; unsigned result[5]; int i; char Ram_Arry[] = "Hello World "; char* Ram_Pointer; u08 text[11]; u08 text2[20] = "WHAT THE ABCDEFG"; IAP iap_bypointer; iap_bypointer = (IAP) 0x7FFFFFF1; //set IAP entry address in function pointer Ram_Pointer = (char*) 0x40004000; //Set pointer to RAM for (index = 0; index<0x0C; index++){ //Copy data to be written to flash into the RAM *Ram_Pointer = Ram_Arry[index]; Ram_Pointer++; } command[0] = 50; //Prepare sector five for a write operayion command[1] = 15; command[2] = 15; iap_bypointer(command,result); clearScreen(); switch(result[0]) { case 0: writeText("success "); break; default: writeText("something went wrong "); break; } command[0] = 51; //write 256 bytes from address 0x40004000 command[1] = 0x00040000; //to 0x00005000 in flash memory; command[2] = 0x40004000; command[3] = 256; command[4] = 14000; // see erase comment above iap_bypointer(command,result); switch(result[0]) { case 0: writeText("success "); break; default: writeText("something went wrong "); break; } Ram_Pointer = (char*)0x40004000; for(i=0; i<12; i++) { text[i] = *Ram_Pointer; Ram_Pointer++; } delay(DELAY_WAIT); Ram_Pointer = (char*)0x00040000; memcpy(&text2, &(*Ram_Pointer), 11); /* for(i=0; i<11; i++) { text2[i] = *Ram_Pointer; Ram_Pointer++; } */ i2cMasterSendNI(0x50, 0xC, text); i2cMasterSendNI(0x50, 0x14, (u08*)text2); }
This output's to my LCD as:
success success Hello World @(@@' ABCDEFG"
I thought you just read from Flash by simply giving the address location of the data you wish to copy? I'm sure I'm missing something small, but if you can't see anything wrong with my code, can you outline the process for reading from flash into RAM?
You have a text that is 12 characters + terminating zero.
You have a for loop that reads 12 characters but excludes the terminating zero.
You have a memcpy that copies 11 characters.
I fixed those errors but I'm still getting the same output. Do I have to alter some sort of configuration setting that allows me to read from Flash directly? I was reading something about moving interrupts from Flash to RAM, but in the documentation it says that IAP calls cannot be interrupted?
Why is there no IAP read from Flash function call?
There is no need for any function call to read from flash - you can always read from flash using a normal pointer.
You have to move the interrupt table and the interrupt service routines from flash to RAM if you want to have the interrupts enabled when doing IAP-programming, because you may not have an interrupt that accesses the flash while the IAP call is busy.
If you don't move the interrupt table, the ISR and any functions called from your ISR to RAM, then the other alternative is that you must (!) turn off interrupts before doing IAP.
You haven't really described what problem you have. The posted code only have printouts for the "success " messages, but not printout that shows your hello world message. Your sample printout did show garbage but that was expected since you did not take care of your end-of-string byte at the end of the text. Note that you had more than one for loop only processing 12 charactesr. The first loop for some reason specified in hexadecimal (0x0C) but that is still one step too little - there was 12 printable characters but you need the thirteens position too.
You have to learn to stop playing with numeric constants all over your program. Use #define or enum or (if C++) const to give your constants names. And don't calculate a lot of constants. The required minimum size for your text string is strlen(str)+1. When you count manually, you are likely to count wrong! In some situations, sizeof(msg) is also ok to use - when the array is declared with empty [], the compiler will automagically create an array of optimum size for the characters and the termination.
Thanks for responding and helping me solve my issue. I have taken your advice and cleaned up my code. Here is the revised copy of it:
void testMem(){ unsigned char index; unsigned command[5]; unsigned result[5]; int i; int len; u08 hexlen; char Ram_Arry[] = "Hello World"; char* Ram_Pointer; char* Rom_Pointer; u08 text[20]; u08 text2[20]; IAP iap_bypointer; iap_bypointer = (IAP) 0x7FFFFFF1; //set IAP entry address in function pointer Ram_Pointer = (char*) 0x40004000; //Set pointer to RAM len = strlen(Ram_Arry) + 1; for (index = 0; index<len; index++){ //Copy data to be written to flash into the RAM *Ram_Pointer = Ram_Arry[index]; Ram_Pointer++; } command[0] = 50; //Prepare sector five for a write operayion command[1] = 15; command[2] = 15; iap_bypointer(command,result); clearScreen(); switch(result[0]) { case 0: writeText("success "); break; default: writeText("something went wrong "); break; } command[0] = 51; //write 256 bytes from address 0x40004000 command[1] = 0x00040000; //to 0x00005000 in flash memory; command[2] = 0x40004000; command[3] = 256; command[4] = 14000; // see erase comment above iap_bypointer(command,result); switch(result[0]) { case 0: writeText("success "); break; default: writeText("something went wrong "); break; } Ram_Pointer = (char*)0x40004000; for(i=0; i<len; i++) { text[i] = *Ram_Pointer; Ram_Pointer++; } delay(DELAY_WAIT); Rom_Pointer = (char*)0x00040000; for(i=0; i<len; i++) { text2[i] = *Rom_Pointer; Rom_Pointer++; } if(strlen((const char*)text) == 11) writeText("equal "); if(strlen((const char*)text2) == 0) writeText("zero "); else writeText("not zero "); hexlen = intToHex(len); text[len-1] = '\0'; text2[len-1] = '\0'; i2cMasterSendNI(0x50, hexlen, text); i2cMasterSendNI(0x50, hexlen, text2); }
Output:
success success equal zero Hello World@(@@h
This is what this code is suppose to do: - Write "Hello World" to specified RAM location 0x40004000 - Call IAP function "Write from RAM to Flash" from RAM 0x40004000 to Flash 0x00040000 (sector 15) - Check to make sure "Hello World" was written to RAM by printing to my LCD - Check to make sure "Hello World" was written to Flash by printing to my LCD -Inserted a couple debug statements to try and figure out my problem.
Unfortunately, the last statement fails, and I can't figure out what is causing this problem. I realize now that you reference data from Flash by simply using pointers. The only other problem would be interrupts or syntax error? This issue is crucial to the functionality of our device, and any further questions I will respond with as much detail as possible.
Also, I don't/can't use the debugger b/c I do not own the full version of Keil Tools. That is why I am just writing to my LCD screen to check for a specific condition or return value.