8 years, 1 month ago.

Not able to read/write from flash - FRDM k64f

I am trying to read/write a couple of characters to flash on a FRDM k64f. This is the code I'm using:

read_write_flash

#include "mbed.h"
#include "FreescaleIAP.h"

int(main)
{
    int fmSnapShotIsSaved = flash_size() - SECTOR_SIZE;
    int fmSnapShotIsInMemory = flash_size() - SECTOR_SIZE + 1;
    int fmSnapShotLocation = flash_size() - SECTOR_SIZE + 2;
    erase_sector(fmSnapShotIsSaved);
    char x[] = {'x'};
    char y[] = {'y'};
    program_flash( fmSnapShotIsSaved, x, 1);
    program_flash( fmSnapShotIsInMemory, y, 1);
    char* ssis = (char*)fmSnapShotIsSaved;
    char* ssim = (char*)fmSnapShotIsInMemory;
    user.printf("Flash Size  : %d\n", flash_size());
    user.printf("Sector Size : %d\n", SECTOR_SIZE);
    user.printf("     char x : %c\n", ssis[0]);
    user.printf("     char y : %c\n", ssim[0]);
    user.printf("     dec  x : %d\n", ssis[0]);
    user.printf("     dec  y : %d\n", ssim[0]);
}

and this is the result I'm getting:

Flash Size  : 1048576
Sector Size : 4096
     char x : 
     char y : 
     dec  x : 255
     dec  y : 255

I admit I'm a bit lost. Seems straight forward but I don't think I'm even writing so I can't be sure I'm reading at all either.

Thanks

Question relating to:

IAP code for Freescale platforms

1 Answer

8 years, 1 month ago.

In FreescaleIAP.cpp you can enable debug messages. While sometimes a bit cryptic (since the errors from the flash peripheral are cryptic often), it does help.

The first write gives an error when it verifies if the sector is erased. Reason is that verifying if sector is erased goes per 8 byte, dividing by 8 (actually by 4, small bug which has been smashed now) is done by shifting 3 bits, so if you had a single byte the result was 0, which the flash peripheral did not like and resulted in an error. Technically you are not supposed to write a single byte, the library accepts it, but it still writes 8 bytes, the last 7 are however just semi-random. If you update your FreescaleIAP lib it will now accept single byte writes (but still write those 7 random bits afterwards, but your first one is written correctly).

Second one is simply allignment error: For the K64F you need to align per 8-byte, you are not allowed to write at 0xF0001 for example, 0xF0000 and 0xF0008 are fine. So either combine the two writes in a single one: char x[] = {'x','y'}; (Fun facts, I just checked this, and it worked fine, despite that I was still writing a single byte. Because it just takes the next values in its RAM to fill the other 7-bytes), or change the +1 in your second ones address to +16. (Yes, that is more than 8-byte aligned, since apparantly the check erased function only accepts 16-byte aligned values, blame Freescale/NXP, I probably should make the library return an alignment error on this, but thats for later).

Accepted Answer

Erik, I've learned a lot reading your posts! Not enough, but I'm a slow learner. Thanks for the quick response.

posted by Rick McNeely 16 Mar 2016