8 years, 11 months ago.

Not able to read data from Flash

Hi!!

I've encountered a problem in reading data from a flash sector where i wrote previously. The first program i've loaded on my K22F is the library example:

#include "mbed.h"
#include "FreescaleIAP.h"
 
 
int main() {
    int address = flash_size() - SECTOR_SIZE;           //Write in last sector
    
    int *data = (int*)address;
    printf("Starting\r\n"); 
    erase_sector(address);
    int numbers[10] = {0, 1, 10, 100, 1000, 10000, 1000000, 10000000, 100000000, 1000000000};
    program_flash(address, (char*)&numbers, 40);        //10 integers of 4 bytes each: 40 bytes length
    printf("Resulting flash: \r\n");
    for (int i = 0; i<10; i++)
        printf("%d\r\n", data[i]);
    
    printf("Done\r\n\n");
        
 
    while (true) {
    }
}

Then i will write to an other sector and want to read from the previous sector:

#include "mbed.h"
#include "FreescaleIAP.h"
 
 
     
int main() {
    int sizeflash = flash_size();          
    printf(" Flash Size: %d\r\n",sizeflash);
    
    int sizesector = SECTOR_SIZE;  
    printf("Sector Size: %d\r\n",sizesector);
    
    int address01 = flash_size() - (SECTOR_SIZE*2); 
    int *data01 = (int*)address01;
    int address = flash_size()- SECTOR_SIZE;
    int *data = (int*)address;
    
    printf("Address01: %d\r\n",address01);
    printf(" \r\n");
    
    printf("Address: %d\r\n",address);
    printf(" \r\n");
  
    printf("Starting\r\n"); 
    erase_sector(address01);
   
   int numbers[10] = {5,6,7,8,9,15,777,91,2,88};
   program_flash(address01, (char*)&numbers, 40);  
   
   printf("Resulting flash for address01\r\n");
     for(int i=0;i<10;i++)
        printf("%d\r\n", data01[i]);
        printf("\r\n");
        
         printf("Resulting flash for address\r\n");
         for(int i=0;i<10;i++)
        printf("%d\r\n", data[i]);
  
        
        
        printf("Done\r\n\n");
        
         while (true) {
    }
}

I load these programs in sequency.

Where is the error?

Thank you!!

Question relating to:

IAP code for Freescale platforms

The first printf works properly and returns "5,6,7,8,9,15,777,91,2,88". The second one returns me ten times "-1".

posted by giacomo amici 25 May 2015

1 Answer

8 years, 11 months ago.

Just checked it with just running the first program for a second time, but with the erase and program part commented out. That gave the same result. I am pretty sure this doesn't happen on the KL25 for example, not 100% sure though.

So it looks like the drag and drop loader performs a complete chip erase when programming.

Accepted Answer

So with the K22F is not possibile to read data written previously, with an other program?

posted by giacomo amici 25 May 2015

In theory it is possible, but if when programming the new program all data is erased, then it is not possible to retrieve it anymore. If you use offline tools you can also use the CMSIS-DAP debugger to program, and that might only erase the necesary parts.

posted by Erik - 25 May 2015