Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 3 months ago.
how to read data from flash --kl25z
I use the library in http://mbed.org/users/Sissors/code/FreescaleIAP/ to read and write data from and to flash, my code is
int main() { int i = 0; pc.baud(19200); int address = flash_size() - SECTOR_SIZE; //Write in last sector char *data = (char*)address; char in[12] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00}; // erase_sector(address);//don't erase old data int j = 0; while(1) { if(data[j] == 0xff && data[j+1] == 0xff && data[j+2]==0xff && data[j+3]==0xff&& data[j+4]==0xff&& data[j+5]==0xff&& data[j+6]==0xff && data[j+7]==0xff&&!pc.writeable())//the end break; pc.putc(data[j]); j++; } while(true) { // if(pc.readable()) { char c = pc.getc();//char or int is all right // pc.putc(c); if(c == (0x84)) { for(int k=0; k<10; k++) { in[k] = c; if(k!=9) c = pc.getc(); } // for(int k =0; k<12; k++) // pc.putc(in[k]); program_flash(address+12*i, (char*)&in, 12); for (int j = 0+12*i; j<12+12*i; j++) pc.putc(data[j]); i+=1; } // } } }
it has some problems storing data, the red section is not the data i store, i don't know where it comes from. and " pc.putc(data[j]);" will not give the data to pc when encountering the red section.
1 Answer
10 years, 3 months ago.
It is hard to see for me what you wrote it to do exactly. It will send that data also to your pc, only if you check it is in ascii it won't make sense. Although without really checking most other data there also doesn't look like ascii to me.
Can you reproduce your problem with just a simple program which writes a standard sequence to flash and which doesn't end up correctly? Also it will refuse to program parts of the flash which aren't erased to 0xFF.
when my code is just like this:
int main() { int i = 0; pc.baud(19200); int address = flash_size() - SECTOR_SIZE; //Write in last sector char *data = (char*)address; char in[12] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00}; while(true) { char c = pc.getc();//char or int is all right if(c == (0x84)) { for(int k=0; k<10; k++) { in[k] = c; if(k!=9) c = pc.getc(); } program_flash(address+12*i, (char*)&in, 12); i+=1; } } }
it will have some oxff oxff oxff in middle of the data, the data is collected through pin PTA1. i add
int j = 0; while(1) { if(data[j] == 0xff && data[j+1] == 0xff && data[j+2]==0xff && data[j+3]==0xff&& data[j+4]==0xff&& data[j+5]==0xff&& data[j+6]==0xff && data[j+7]==0xff&&!pc.writeable())//the end break; pc.putc(data[j]); j++; }
to let me store the data. After the collection is over, i link kl25z to pc using opensda, and press reset to see the data, but it will stop printing because of the 0xff 0xff
posted by 04 Jun 2014But what if you don't use the getc() part to get data, but instead just use a predefined sequence? I wonder if the programming is the issue, or the generation of the data pattern. Also when you say data is collected through PTA1, do you mean via an extra connection to PTA1, or via the opensda port? Because PTA1 is the same as USBRX, if it is an external connection I would try another pin.
Echoing the read data to your pc might also give more information (or directly reading it back after programming). And if it also happens with just a predefined pattern I can try it on my own KL25, but if you manually use an input that is harder to reproduce.
posted by 04 Jun 2014my new code is
DigitalOut myled(LED_GREEN); Serial pc(USBTX, USBRX); Serial re(PTD3, PTD2); int main() { int i = 0; pc.baud(19200); re.baud(19200); int test = 0; int address = flash_size() - SECTOR_SIZE; //Write in last sector char *data = (char*)address; char in[12] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00}; while(test<100) { program_flash(address+12*i, (char*)&in, 12); test+=1; } }
i collect data via extra connection to pta1, i change it to ptd2, it seems no influence on the result. When store predefined data, there still exists some 0xff s in the middle of it, the pic likes this.
posted by 04 Jun 2014Your program is a bit weird on some parts (you do for example 12 * i while not ever changing i, it should only program one place, the others are most likely old data.
I tried the following code, and that returned correct values to my serial terminal:
#include "mbed.h" #include "FreescaleIAP.h" DigitalOut myled(LED_GREEN); int main() { int test = 0; int address = flash_size() - SECTOR_SIZE; //Write in last sector char *data = (char*)address; char in[12] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x00,0x00}; erase_sector(address); while(test<50) { program_flash(address+12*test, (char*)&in, 12); test+=1; } test = 0; while(test<50) { printf("%d: ", test); for (int i = 0; i<12; i++) printf("0x%02x - ", data[test * 12 + i]); printf("\r\n"); test++; } }
I careless removed i+=1 when pasting the code, but if changing 'test' to 100, it will also be wrong, we can only get 85 sequences.
posted by 05 Jun 2014That is an easy one: Working as intended :D.
A sector on the KL25Z is 1024 bytes, and you start one sector before the end. 85 * 12 = 1020 bytes. So in the 86th sequence it tries to write (and read) outside the flash memory and it will fail.
If you require more you should just move another sector back. (Don't forget to erase both sectors).
posted by 05 Jun 2014