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

Including Flash storage library into another ble example

Hi,

I am trying to incorporate flash storage library to ble_app_uart_c example. I have added all necessary functions from ...\examples\peripheral\flash_fstorage.

When I am reading from flash it is not getting copied completely to the buffer which is passed. It is not even executing many of the lines in between.

<info> app: fstorage example started.
<info> app: SoftDevice is present.
<info> app: Initializing nrf_fstorage_sd implementation...
<info> app: ========| flash info |========
<info> app: erase unit: 	4096 bytes
<info> app: program unit: 	4 bytes
<info> app: ==============================
<info> app: got flash end addresses: -1, 4096, 128.
<info> app: Writing "BADC0FFE" to flash.
<info> app: --> Event received: wrote 4 bytes at address 0x3E000.
<info> app: Done.
.
<info> app: Done.
<info> app: Writing "hello world" to flash.
<info> app: --> Event received: wrote 12 bytes at address 0x3F000.
<info> app: Done.
<info> app: Writing "abcdefg" to flash.
<info> app: --> Event received: wrote 8 bytes at address 0x3E120.
<info> app: Done.
<info> app: initially data[i]: 0
<info> app: initially data[i]: 0
<info> app: initially data[i]: 0

As you can see in the above image (watch) it is not getting copied.

When I am putting break points it is getting copied.  

hitting first break point:

After reading the data:

After running completely:

Why it is not executing completely? I am including my main too.

uint32_t data[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static uint32_t m_data          = 0xBADC0FFE;
static char     m_hello_world[] = "hello world";
static char     m_hello[] = "abcdefg";
int main(void)
{
    ret_code_t rc;
    
    // Initialize.
    log_init();
    timer_init();
    uart_init();
    buttons_leds_init();
    db_discovery_init();
    power_management_init();

    NRF_LOG_INFO("fstorage example started.");
    

    nrf_fstorage_api_t * p_fs_api;
 #ifdef SOFTDEVICE_PRESENT
    NRF_LOG_INFO("SoftDevice is present.");
    NRF_LOG_INFO("Initializing nrf_fstorage_sd implementation...");
    /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
     * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
     * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
    p_fs_api = &nrf_fstorage_sd;
#else
    NRF_LOG_INFO("SoftDevice not present.");
    NRF_LOG_INFO("Initializing nrf_fstorage_nvmc implementation...");
    /* Initialize an fstorage instance using the nrf_fstorage_nvmc backend.
     * nrf_fstorage_nvmc uses the NVMC peripheral. This implementation can be used when the
     * SoftDevice is disabled or not present.
     *
     * Using this implementation when the SoftDevice is enabled results in a hardfault. */
    p_fs_api = &nrf_fstorage_nvmc;
#endif

    rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(rc);

    print_flash_info(&fstorage);

    /* It is possible to set the start and end addresses of an fstorage instance at runtime.
     * They can be set multiple times, should it be needed. The helper function below can
     * be used to determine the last address on the last page of flash memory available to
     * store data. */
    (void) nrf5_flash_end_addr_get();

    /* Let's write to flash. */
    NRF_LOG_INFO("Writing \"%x\" to flash.", m_data);
    rc = nrf_fstorage_write(&fstorage, 0x3e000, &m_data, sizeof(m_data), NULL);
    APP_ERROR_CHECK(rc);

    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Done.");

#ifdef SOFTDEVICE_PRESENT
    /* Enable the SoftDevice and the BLE stack. */
    NRF_LOG_INFO("Enabling the SoftDevice.");
    ble_stack_init();

    m_data = 0xDEADBEEF;

    NRF_LOG_INFO("Writing \"%x\" to flash.", m_data);
    rc = nrf_fstorage_write(&fstorage, 0x3e100, &m_data, sizeof(m_data), NULL);
    APP_ERROR_CHECK(rc);

    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Done.");
#endif

    NRF_LOG_INFO("Writing \"%s\" to flash.", m_hello_world);
    rc = nrf_fstorage_write(&fstorage, 0x3f000, m_hello_world, sizeof(m_hello_world), NULL);
    APP_ERROR_CHECK(rc);

    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Done.");

   
    NRF_LOG_INFO("Writing \"%s\" to flash.", m_hello);
    rc = nrf_fstorage_write(&fstorage, 0x3E120, m_hello, sizeof(m_hello), NULL);
    APP_ERROR_CHECK(rc);

    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Done.");

    for(int i = 0;i < 5; i++)
    {
	NRF_LOG_INFO("initially data[i]: %d", data[i]);
    }

    NRF_LOG_INFO("Reading from flash");
    rc = nrf_fstorage_read(&fstorage, 0x3e100, data, 10);
    APP_ERROR_CHECK(rc);
    
    
    wait_for_flash_ready(&fstorage);
    NRF_LOG_INFO("Done.");

    NRF_LOG_INFO("error code: %d", rc);
    NRF_LOG_INFO("Error");

    for(int i = 0; i < 5; i++)
    {
	    NRF_LOG_INFO("Reading  from flash");
        NRF_LOG_INFO("Reading  from flash data[%d]: %0x", i, data[i]); 
    }

    NRF_LOG_INFO("Reading  from flash data: %0x", data[0]); 


  
    gatt_init();
    nus_c_init();
    
    scan_init();

    // Start execution.
    NRF_LOG_INFO("BLE UART central example started.");
    scan_start();

    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

Is it something related to the delay to read from the flash? What is the difference between FDS and fstorage libray?

SOC NRF52832, soft device s132 version 15.0.2

Windows 10, segger embedded studio

Parents
  • What is the difference between FDS and fstorage libray?

    fstorage just gives basic low-level access to for reading, writing, and erasing data in flash memory

    https://www.nordicsemi.com/DocLib/Content/SDK_Doc/nRF5_SDK/v15-2-0/lib_fstorage

    FDS builds on fstorage to give, "a minimalistic file system for the flash storage on the chip that minimizes the risk of data corruption and simplifies your interaction with the persistent storage"

    https://www.nordicsemi.com/DocLib/Content/SDK_Doc/nRF5_SDK/v15-2-0/lib_fds

    Among other things, It allows you to update records, and manages wear-levelling ...

  • @awneil, Thank you.

    Hi, Somebody please help me with the rest of the problem too .

    • Is it the problem with lack of delay?
    • Should I add some delay in between? In that case which API should I use for add for adding delay?
    • I could not find any API to read and write a particular length of data to flash in FDS. So how can I use it for that purpose?
  • Hi,

    Arya said:

    • Is it the problem with lack of delay?
    • Should I add some delay in between? In that case which API should I use for add for adding delay?

    The last part of your log is missing, so it is not possible to see from where what is happening. It is not entirely clear to me, but it seems like that is the only problem, since reads seems working when you check with a debugger? In that case it might be that you log too fast compared to the processing of the logs, and/or use a too small log buffer. I suggest you either stop using deferred logging or increase the log buffer so that you make sure you actually get everything printed. I do not see any need for a delay.

    Arya said:
    I could not find any API to read and write a particular length of data to flash in FDS. So how can I use it for that purpose?

     FDS is like a file system, and it operates on records, which is a distinct piece of information, similar to a file in most file systems (and has a length.) Please refer to the FDS documentation.

  • Hi,

    This is my final LOG with debugging.

    This is my final LOG with out debugging.

  • Hi,

    I am having a bit of problem getting an overview of this. To get better understanding:

    • Can you upload the log as test rather than screenshots?
    • Can you upload the exact code you used to produce the log? (it is difficult to know if it matches when there are several days between when the log and code was posted).
    • Can you elaborate what the problems are now?

    The typical reasons for failing to read back the correct data from flash are:

    • Flash page not erased. This example does not erase the flash page before it is written to. Can you make sure to do a full chip erase before running this code? If not, you may have "leftovers" 0's from earlier flash content, as writing can only flip 1's to 0's.
    • Not waiting for the flash write operation to complete. This does not seem like the problem here, though.
Reply
  • Hi,

    I am having a bit of problem getting an overview of this. To get better understanding:

    • Can you upload the log as test rather than screenshots?
    • Can you upload the exact code you used to produce the log? (it is difficult to know if it matches when there are several days between when the log and code was posted).
    • Can you elaborate what the problems are now?

    The typical reasons for failing to read back the correct data from flash are:

    • Flash page not erased. This example does not erase the flash page before it is written to. Can you make sure to do a full chip erase before running this code? If not, you may have "leftovers" 0's from earlier flash content, as writing can only flip 1's to 0's.
    • Not waiting for the flash write operation to complete. This does not seem like the problem here, though.
Children
No Data
Related