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

NRF flash storage event handler issue with Bluetooth advertising.

Hi Team,

We are facing issue with Flash storage Handlers with ble handlers. When we add the fstorage to my application ble adversing is stopped suddenly.

We are having separate Event handlers for both. We have some concerns related to fstorage,

1. How to set the start and end addresses of an fstorage instance at runtime, at the end of current application usage.

fstorage example code we have found we below code snippet , but to use this macro and nowhere it is called in application                                            

                                    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
                                     {
                                       /* Set a handler for fstorage events. */
.                                           evt_handler = fstorage_evt_handler,

                                         /* These below are the boundaries of the flash space assigned to this instance of fstorage.
                                           * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
                                                 * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
                                                     * last page of flash available to write data. */
                                                  .start_addr = 0x4e000,
                                                 .end_addr = 0x4f000,
                                      };

2. Providing the fs Read/write functions code here for cross verification,

Fstorage Handler:

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
if (p_evt->result != NRF_SUCCESS)
{
return;
}

switch (p_evt->id)
{
case NRF_FSTORAGE_EVT_WRITE_RESULT:
{
m_fs_Data_write = 1;
}
break;

case NRF_FSTORAGE_EVT_ERASE_RESULT:
{
m_fs_Data_erase = 1;
}
break;

case NRF_FSTORAGE_EVT_READ_RESULT:
{
m_fs_Data_read = 1;
}
break;

default:
break;
}
}

Write Function:

void Update_flash_Data(void)
{
ret_code_t rc;

rc = nrf_fstorage_erase(&fstorage, (uint32_t)(KEY_BASE_ADDR), 1, NULL);
APP_ERROR_CHECK(rc);

if(m_fs_Data_erase == 1)
{
m_fs_Data_erase = 0;
rc = nrf_fstorage_write(&fstorage, (uint32_t)KEY_BASE_ADDR, &m_sffl_data, sizeof(m_sffl_data), NULL);
APP_ERROR_CHECK(rc);
}

//wait_for_flash_ready(&fstorage);

}

Read Handler:

void Read_Flash_Data(void)
{
ret_code_t rc;

rc = nrf_fstorage_read(&fstorage, (uint32_t)KEY_BASE_ADDR, &m_sffl_data, sizeof(m_sffl_data));
APP_ERROR_CHECK(rc);

//wait_for_flash_ready(&fstorage);

}

Is there any modifications required in Event handler and Write/Read handlers.

Regards,

Srinivas.V

Parents Reply
  • Hi Haakonsh,

    Thanks for your reply.I have seen that exmaple, in that based on bsp button press there are deciding the running mode, but at a time only one module is running.

    My requirement is different ,we need the both the modules but only thing once in a while we update the flash data in that time it should not preempted by others.

    1. How to enalbe the SD scheduler which manages radio timeslots and priorities and sets up timers to grant time slots to peripherals.

    2. How to assign priorities to Flash API's.

    3. How to make atomic nature flash api's when they are running, means nobody will preempt at that time(10ms),once it is done we will come out from this atomic operation and execute as normal.

    Regards,

    Srinivas.V

Children
    1. See sd_radio_requestsd_radio_session_open, and sd_radio_session_close.

    2. The SoftDevice's flash operation is run at IRQ priority 5, they are scheduled, but they can get interrupted. See Flash memory API for details.

    3. Use Timeslot API, disable interrupts, execute atomic operation, re-enable interrupts. 
  • Hi Haakonsh,

    Thanks for your reply.We have some more questions on FLASH API timing and other things.

    1. How to enable flash timing-event scheduling in any available time left between other timing-events.

    2. How to configure the code to run the Flash API in a low priority,in which file we need to do this activity.

    3. How to configure and use the Time slot Flash API's apart from Normal Flash API ,like "sd_flash_write" and "sd_flash_erase".

    4. Is there any way to know the Blue tooth is in  ideal state, in that slot we can execute this Flash API's.

    Flash is running at Fourth priority and we can't change this , my intention is how to implement or use  the above mentioned points in our Application,  is there any specific example for Flash timing api's and other things.I haven't found any specific api's for flash apart from i mentioned above.

    Fourth priority
    • Flash access
    • Radio Timeslot with normal priority

    Regards,

    Srinivas.V

    1. The scheduling of flash access is handled by the SoftDevice.

    2. It has a fixed low priority.

    3. See sd_radio_requestsd_radio_session_open, and sd_radio_session_close. You can use the storage API when in a Timeslot. You have to specify the length of the timeslot in µs, and since the flash operations are deterministic you can divide your flash operations into parts that fit into a given Timeslot. 
    4. Yes, see (3). 
  • Hi Haakonsh,

    Thanks for your reply.

    3. what was the API or Macro to configure the storage API time slot length in µs and flash data size is less than 256 bytes only.

    Here with i am sharing my flash design in my application,

    /Flash Data is updated ,writing to Flash sector based on runtime flag
    if((true == m_fs_Data_Update) && (true == m_ble_idle_flage))
    {
            m_fs_Data_Update = false;
             m_ble_idle_flage = false;

    CRITICAL_REGION_ENTER();
                 Update_flash_Data();
    CRITICAL_REGION_EXIT();
    }

    static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
    {
    ret_code_t err_code;

    switch (ble_adv_evt)
    {
    case BLE_ADV_EVT_FAST:
                 NRF_LOG_INFO("Fast advertising.");
                  err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
                 APP_ERROR_CHECK(err_code);
                 break;

    case BLE_ADV_EVT_IDLE:
        m_ble_idle_flage = true;    //when ever ble is idle we are writing the data to flash.

               sleep_mode_enter();
              break;

            default:
                  break;
       }
    }

    void Update_flash_Data(void)
    {
            ret_code_t rc;
             uint32_t len = round_up_u32(sizeof(m_sffl_data));

               rc = nrf_fstorage_erase(&fstorage, fstorage.start_addr, 1, NULL);
                 APP_ERROR_CHECK(rc);

    //wait_for_flash_ready(&fstorage);

             rc = nrf_fstorage_write(&fstorage,fstorage.start_addr, &m_sffl_data, len, NULL);
              APP_ERROR_CHECK(rc);
    }

    void fs_Init(void)
    {
                ret_code_t rc;
                nrf_fstorage_api_t * p_fs_api;

           #ifdef SOFTDEVICE_PRESENT
          p_fs_api = &nrf_fstorage_sd;
           #else
                   p_fs_api = &nrf_fstorage_nvmc;
            #endif
           

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

    /* 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();

                 wait_for_flash_ready(&fstorage);

    }

    Is there any other design technique to implement the Flash operations without disturbing the BLE functionalities.

    Regards.

    Srinivas.V

  • Srinivas V said:
    Is there any other design technique to implement the Flash operations without disturbing the BLE functionalities.

     Yes, use the SoftDevice Flash API in a Timeslot as explained previously.

Related