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

Flash storage and BLE Event handlers are not synchronizing.

Hi Team,

We are using the NRF52840 Device with S140 Soft device for our BLE applications. As part of this we have enabled the flash storage as well.

We are unable to write to flash when ble is enabled. How to design the things which will work together. Is there any approach or any sample code which will handle the both with out effecting each other.

When we trying to write the flash Advertising is not happening. Here with sharing my current code how we are doing.

           int main(void)
          {
              //Initializations 

          gap_params_init();
          gatt_init();
          services_init();
         advertising_init();
         conn_params_init();
         peer_manager_init();

        fs_Init();

      

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

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

                     if(true == m_fs_Data_write)
                       {
                            m_fs_Data_Update = false;
                           m_fs_Data_write = false;
                        }
   }

              app_sched_execute();
              idle_state_handle();
}

//BLE Idle flag we are updating at ble idle event. Based on this event we are writing the flash data. But we are not entering into BLE_ADV_EVT_IDLE at all.

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:
            err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
             APP_ERROR_CHECK(err_code);
            break;

          case BLE_ADV_EVT_IDLE:
             m_ble_idle_flage = true;

         //sleep_mode_enter();
        break;

      default:
        break;
      }
}

Can any one suggest me the design approach or sample code which handles flash and BLE synchronously without impacting each other.I tried couple of ways but succeed on this one. we are not calling any blocking calls in flash operations.See below code for flash write.

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);

   //wait_for_flash_ready(&fstorage);

}

Regards,

Srinivas.V

Parents
  • Hello,

    What's the reasoning for running "Update_flash_Data()" in a critical section? Have you tested without? The wait_for_flash_ready() loop will never return if application interrupts are blocked.

    Regards,

    Vidar 

  • Hi Vidar,

    Thanks for your reply.

    We are running the Update_flash_Data() function in critical section because while writing no other functionalities should not do preemption.without section it is  not updating the data.

    We are not using the "wait_for_flash_ready()"  function in write operation. 

    Is there any other design approach for this flash and BLE synchronization, it would helpful to us.

    Regards,

    Srinivas.V

  • Hi Srinivas,

    Did you make sure to select the Softdevice flash backend for fstorage when you initialized it? The Softdevice "Flash memory API" already handles synchronization with BLE events.

    Initializing fstorage with Softdevice flash backend:

    nrf_fstorage_api_t * p_fs_api;
    p_fs_api = &nrf_fstorage_sd;
    uint32_t rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);

    Regards,

    Vidar

  • Hi Vidar,

    Thanks for your valuable reply.

    We also doing the same initialization what you have suggested. If BLE Synchronizing the events we are not able to write the data when BLE is enabled, that is the reason we are writing when BLE is in idle state.See below event handler.

    1. But code is not coming to Idle state at all.Is there any reason why we are not hitting the BLE idle state.

    2. Is this correct way to write flash data irrespective or BLE state like write the data in while loop, when ever data is changed or we need to sync with BLE state.

    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:
                err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
                 APP_ERROR_CHECK(err_code);
                break;

              case BLE_ADV_EVT_IDLE:
                 m_ble_idle_flage = true;

             //sleep_mode_enter();
            break;

          default:
            break;
          }
    }

    3. A flash write must be made in chunks smaller or equal to the flash page size, Our total flash data will be 142 bytes only, is that ok or we need to split into smaller sizes.

    4. We are performing the flash operation in main for loop based on BLE event flag(m_ble_idle_flage = true;)which i mentioned in ble event handler.Is that below code is ok or we need to do any modifications.Is there any other best design approach for flash along with BLE.

            int main(void)

              {
                          gap_params_init();
                           gatt_init();
             
                          advertising_init();
                         conn_params_init();
                         fs_Init();

          

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

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

                         if(true == m_fs_Data_write)
                           {
                                m_fs_Data_Update = false;
                               m_fs_Data_write = false;
                            }
               }

     }

    We will appreciate, if you could able to give better design for above scenario.

    Regards,

    Srinivas.V

  • Hi Srinivas,

    You will get a Softdevice assertion if you try using Fstorage with the nrf_fstorage_nvmc backend instead of the nf_fstorage_sd backend made for the Softdevice, so please make sure you have selected the backend as shown in my previous response.

    1. The program will end up in the app error fault handler if you access NVMC registers directly instead of using the Softdevice Flash APIs. That could be an explanation for this behavior.

    2. You can usually perform flash writes while advertising. Page erases, on the other hand, can be a bit more difficult as they take a considerable amount of time to complete. Please refer to the  "Flash memory API" chapter for limitations. 

    3. 142 bytes is not a problem. I would maybe consider smaller chunks if the data starts to exceed 1000 bytes.

    4. Bluetooth events are processed in the background by the Softdevice. If the goal is to do flash operations while there is no BLE activity, you will need to terminate any existing connection and stop advertising, then complete the flash operations before you start BLE advertising again. 

    Regards,

    Vidar

  • Hi Vidar,

    Thanks for your valuable and quick reply.

    1.We are sure using the nf_fstorage_sd backend mode for the Softdevice,not using the nrf_fstorage_nvmc.

    2. If the Soft device will take care while performing the both(ble, flash) activities synchronously , there is no issue with us.

    3. In which scenario the ble handler will go to idle state, i am asking with my curiosity.

    We will use the same code for our flash write purpose.

    Regards,

    Srinivas. V 

  • Hi Srinivas,

    I'm not sure what causes the problem as you had already selected the correct backend. Any chance you can provide a stripped-down version of your project so I can try debugging it here?

    Regards,

    Vidar

Reply Children
No Data
Related