4 years, 12 months ago.

falshIAP simple example?

Hi everyone.

I'm trying to find way how to store few bytes at NUCLEO boards after reset.

I found that Mbed 5 has FlashIAP class, but there is no simple example of using.

try to make shor code to store iteration value after reset;

MALFUNCTED

#define BLINKING_RATE_MS            100
#define ADDRESS     0x0807000          //one address at the end of NUCLEO-F401RE flash  also tried with sram
FlashIAP  flash;

int main()
{
    DigitalOut led(LED1);
    uint32_t addr = ADDRESS;
    flash.init();
    const uint32_t page_size = flash.get_page_size();
    char *page_buffer = new char[page_size];

    while (true) {
        led = !led;
       flash.read(page_buffer, addr, page_size);    //read data from flash
        printf("storaged data is : %d\r\n", page_buffer);
       page_buffer++;        
       flash.program(page_buffer, addr, page_size);   //store data back, to avoid loose after reset
        thread_sleep_for(BLINKING_RATE_MS);
    }
}

And program will start, but it crash on "flash.read()" LINE15

Also if FlashIAP.program() is "The sectors must have been erased prior to being programmed". How to do it to not erase data stored before reset?

Can someone show short code to store one byte to internal flash or sram on STM32F... ?

thank you

2 Answers

4 years, 12 months ago.

Hi,

You read a page of bytes (256 or 512 bytes) to page buffer. Ok. Then you try to print it. Not ok or you print the pointer page_buffer desimal value - not the content of the readed flash page. If yo want to see the content you must print is byte by byte. Then you add 1 to the pointer. It is now pointing to page_puffer[1] instead of [0]. When you try to write it back to flash you try to write page_buffer[1] to addr[0] instead of addr[1]. This is ok if you erase the page first and you must remember that you have to erase whole flash sector - it is not possible to erase only one page. You don't have to write the data back to the flash unless you have change it and in that case you must erase the flash before writing.

-> Start fixing the code from the printf printf("First by of the page is : %x\r\n", page_buffer[0]);

Regards, Pekka

Accepted Answer
4 years, 12 months ago.

Hi Michal,

maybe already answered question from a past, will help you. Mentioned API NVStore sounds good.

BR, Jan

Hi Jan,

I tried this:

#include "mbed.h"
 
FlashIAP flash;
 
int main()
{
    flash.init();
 
    const uint32_t  flash_start = flash.get_flash_start();
    const uint32_t  flash_size = flash.get_flash_size();
    const uint32_t  flash_end = flash_start + flash_size - 1;
    const uint32_t  page_size = flash.get_page_size();
    uint32_t        sector_size = flash.get_sector_size(flash_end);
    uint8_t*        page_buffer = new uint8_t[page_size];
    uint32_t        addr = flash_end - sector_size + 1;
    uint8_t         data_write = 55;
    uint8_t         data_read = 0;
    uint8_t         value;
     
    while (true)
    {   
        flash.read(page_buffer, addr, page_size);  //read stored value from flash
        value = page_buffer[0];  
        value++;
        page_buffer[0] = value;
        printf("Stored value %d\r\n", value);
        flash.erase(addr, sector_size);
        flash.program(page_buffer, addr, page_size);  //store changed value in flash
        delete[] page_buffer;
        
    }        
 }

and after reset is value starts from zero. What should be done to make it works?

thank you

Michal

posted by Michal Kravcik 23 Nov 2019

Hi Michal,

descripted issue came because you added the while loop.

This works well after reset or power down.

#include "mbed.h"
 
FlashIAP flash;
 
int main()
{
    printf("Flash Example\r\n");
    flash.init();
    const uint32_t  flash_start = flash.get_flash_start();
    const uint32_t  flash_size = flash.get_flash_size();
    const uint32_t  flash_end = flash_start + flash_size - 1;
    const uint32_t  page_size = flash.get_page_size();
    uint32_t        sector_size = flash.get_sector_size(flash_end);
    uint32_t        addr = flash_end - sector_size + 1;
    uint8_t         page_buffer[page_size];
    uint8_t         data_read = 0;
    
    flash.read(&page_buffer, addr, page_size);
    data_read = page_buffer[0];
    printf("data readed from flash = %d\r\n", data_read);
    page_buffer[0] = ++data_read;
    printf("data before writing to flash = %d\r\n", data_read);
    flash.erase(addr, sector_size);
    flash.program(&page_buffer, addr, page_size);
    page_buffer[0] = 0;
    flash.read(&page_buffer, addr, page_size);
    data_read = page_buffer[0];
    flash.deinit();
    printf("data writed to flash = %d\r\n", data_read);
}

For the loop is probably need to be add flash.init() and flash.deinit() inside the loop. Without that data will be hold only with a very short reset but not after power lost.

BR, Jan

posted by Jan Kamidra 24 Nov 2019