This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Flash write issue!

hello,

 I am using SDK 16.0 and I use flash storage to store some value which is critical for our application, when I use fstorage_write as below inside main(),

int main()

{

uint32_t mode=0x22;
uint32_t address_reg=0x54000;

 fstorage_init();                    // Initializing the flash_storage


 fstorage_write(address_reg,&mode);
}

and view memory location using segger I am getting 0x22 int address ox54000, which is correct

but when I put fstorage_write inside another function, and observe the memory location it showing completely unrelated values.

void write_test(uint32_t mode)
{


uint32_t address_reg=0x54000;

fstorage_write(address_reg,&mode);

}

int main()

{

uint32_t mode=0x22;

 fstorage_init();                    // Initializing the flash_storage

write_test( mode)

}

I am really confused and what may be the reason, when I change this function to inline function it seems to work fine, 

Parents
  • Hi,

    The variable that you pass to the function (uint32_t mode) will go out of scope when the function call returns. fstorage write and erase operations happen asynchronously and may not complete before the function returns. Since you pass a reference to the mode variable to fstorage_write, the content of the address may have changed before fstorage will write the data to flash.

    You need to make sure that the variable that is referenced is in scope until the write is complete. This can be done either by using a global variable for passing data to the function or by using a pointer in the function arguments and pass a pointer to a variable that is in scope after the function call returns.

    Best regards,
    Jørgen

Reply
  • Hi,

    The variable that you pass to the function (uint32_t mode) will go out of scope when the function call returns. fstorage write and erase operations happen asynchronously and may not complete before the function returns. Since you pass a reference to the mode variable to fstorage_write, the content of the address may have changed before fstorage will write the data to flash.

    You need to make sure that the variable that is referenced is in scope until the write is complete. This can be done either by using a global variable for passing data to the function or by using a pointer in the function arguments and pass a pointer to a variable that is in scope after the function call returns.

    Best regards,
    Jørgen

Children
No Data
Related