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

fstorage problem

Hi

I tried to test the read and write in flash but without success. I added the following code in service_init()

uint8_t               initial_data[8];

err_code = nrf_fstorage_read(&fstorage, FLASH_START_ADD, initial_data, sizeof(initial_data));
APP_ERROR_CHECK(err_code);

for(uint8_t i = 0; i < 8; i++)
{
NRF_LOG_INFO("vrednost: %d", initial_data[i]);
}


if(initial_data[0] > 7)
{
initial_data[0] = 0;
}
else
{
initial_data[0] = initial_data[0] + 1;
}

for(uint8_t i = 1; i < initial_data[0] + 1; i++)
{
initial_data[i] = 5;
}


err_code = nrf_fstorage_write(&fstorage, FLASH_START_ADD, initial_data, sizeof(initial_data), NULL);
wait_for_flash_ready(&fstorage);

APP_ERROR_CHECK(err_code);

Of course I added nrf_fstorage_init() and NRF_FSTORAGE_DEF as well.  When I restart the chip the flash does not change in location FLASH_START_ADD

#define FLASH_START_ADD 0x2A800
#define FLASH_END_ADD 0x2AC00

Here is the output from J-Link. The values are the same all the time regardless the chip is restarted.

<info> app: --> Event received: wrote 8 bytes at address 0x2A800.
00>
00> <info> app: Template example started.
00>
00> <info> app: Fast advertising.
00>
00> <info> app_timer: RTC: initialized.
00>
00> <info> app: vrednost: 0
00>
00> <info> app: vrednost: 5
00>
00> <info> app: vrednost: 255
00>
00> <info> app: vrednost: 255
00>
00> <info> app: vrednost: 255
00>
00> <info> app: vrednost: 255
00>
00> <info> app: vrednost: 255
00>
00> <info> app: vrednost: 255

I use sdk16.0 and softdevice 112.

Parents
  • Hi,

    I think you have a logic error in your for-loop. In the if-else setting, you set the first byte to 0, and then you run the for-loop until i reaches (initial_data[0] + 1), which equals 1, meaning that the for loop will exit after one loop. The other data in the loop is not modified and the data you read is correct (given that initial flash-pages are erased the first time you start the application).

    Best regards,
    Jørgen

  • Did you write the data to the same address, without erasing the flash page first? Writing to flash can only change bits from 1 to 0. To change a bit from 0 to 1, you need to erase the full page. If you do not want to erase the full page, you need to write the new data to a separate, erased, location in flash. If you will be updating data in flash frequently, I would recommend you to look into the FDS library instead of fstorage. FDS provides a minimal file system and simplifies interaction with flash.

Reply Children
Related