This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Wait on NRF_EVT_FLASH_OPERATION_SUCCESS to continue

We have some legacy code ported from other platfrom to nRF51822. The old platform supports blocking flash write, so there will be some readback code after flash write function.

After ported to nRF51822, I am wondering whether there is way that the code can wait or sleep after issue flash write request, and continue the code after received NRF_EVT_FLASH_OPERATION_SUCCESS event.

void sys_evt_dispatch(uint32_t sys_evt)
{
    if (sys_evt == NRF_EVT_FLASH_OPERATION_SUCCESS){
    	// how to trigger/wake up some_way_to_wait()
    }else{
    	LOG_DEBUG(" sys_evt_dispatch code 0x%x", sys_evt);
    }
}

func()
{
    flash_write(...);        
    some_way_to_wait();        
    read_flash_back_do_something1();

    flash_write(...);        
    some_way_to_wait();        
    read_flash_back_do_something2();
}

Of course I know I could put read_flash_back_do_something() in a callback function but that needs some significant change and we prefer not to do it if we can find a solution for this post.

  • I was able to work out a solution myself.

    The trick is to use two different mechanism for system event and BLE event.

    1. Use application scheduler to poll system event sd_ble_evt_get(), so func() will be called by application scheduler in main thread, so sd_app_evt_wait() will put the main thread on wait.

    2. Use SWI2_IRQHandler to poll BLE event check the example Source/sd_common/softdevice_handler.c sys_evt_dispatch is called by SWI2_IRQHandler, will change the flag, wake up main thread

      static volatile uint8_t flashBusy = 0;

      void setFlashIdle(void) { flashBusy = 0; }

      void sys_evt_dispatch(uint32_t sys_evt) { if (sys_evt == NRF_EVT_FLASH_OPERATION_SUCCESS){ setFlashIdle(); }else{ LOG_DEBUG(" sys_evt_dispatch code 0x%x", sys_evt); } }

      func() { flash_write(...);

       flashBusy = 1;
       while (flashBusy)
       {
       	(void) sd_app_evt_wait();
       }
       read_flash_back_do_something1();
      

      }

  • Thank you for answering your own question! And sorry that we couldn't be of more assistance. I will close this thread now.

Related