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

NRF52832 disable and enable SoftDevice

I work with NRF52832. Implemented an application based on the "ble_app_uart" example. Everything is working. I decided to add the saving of configurable parameters to the internal Flash memory. I write to Flash using the NVMC module, the use of which is possible only
with the SoftDevice stack disabled, otherwise Flash will crash. This is how I initially initialize the stack when power is applied to the device:

timers_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
advertising_start();

Then, in the body of the program, I disable the stack:

advertising_stop();
if (nrf_sdh_is_enabled())
{
   err_code = nrf_sdh_disable_request();
   APP_ERROR_CHECK(err_code);
   while (nrf_sdh_is_enabled()) {}
   disabled_softdevice = true;
}

Then I successfully work with Flash memory (erase, write):

Flash_exe();

But I don't understand how to re-run the stack and advertising. Can someone tell me?

  • I write to Flash using the NVMC module,

    Not recommended.

    Just use FDS, which supports writing to flash while SD is running. Note that peer manager also uses FDS in order to store pairing/bonding information. Many BLE examples use peer manager.

  • still I'm interested in how to restart SoftDevice.

  • I agree with here. You can use FDS (or fstorage for that matter). You don't need to disable the softdevice. fstorage and FDS has two backends. Either NVMC or the softdevice. From the application point of view, they do the exact same thing. The difference is that you need to use the softdevice backend when the softdevice is enabled.

    I strongly recommend you to look into that.

    However, you can use nrf_sdh_disable_request(); to request to disable the softdevice. Note the documentation in the declaration of this function:

    /**@brief   Function for requesting to disable the SoftDevice.
     *
     * This function issues a @ref NRF_SDH_EVT_DISABLE_REQUEST request to all observers that
     * were registered using the @ref NRF_SDH_REQUEST_OBSERVER macro. The observers may or
     * may not acknowledge the request. If all observers acknowledge the request, the
     * SoftDevice will be disabled. Otherwise, the process will be stopped and the observers
     * that did not acknowledge have the responsibility to restart it by calling
     * @ref nrf_sdh_request_continue when they are ready for the SoftDevice to change state.
     *
     * @retval  NRF_SUCCESS                 The process is started.
     * @retval  NRF_ERROR_INVALID_STATE     The SoftDevice is already disabled.
     */
    ret_code_t nrf_sdh_disable_request(void);

    First when all modules that are observing the SDH events have acknowledged the request, the softdevice will be disabled. You can check the status of the softdevice handler by using 

    /**@brief   Function for retrieving the SoftDevice state.
     *
     * @retval  true    If the SoftDevice is enabled.
     * @retval  false   If the SoftDevice is disabled.
     */
    bool nrf_sdh_is_enabled(void);

    After that you need to enable the stack again. Just use the same function that you used initially, ble_stack_init(), I guess.

    Please look at the examples flash_fstorage or flash_fds examples in SDK\examples\peripheral. Note that they both have projects for "blank" and "s132", which refers to without and with the softdevice, respectively. Look into this.

    If you decide to disable the softdevice, it means you can't use the flash without disconnecting, and seise all softdevice activities.

    BR,

    Edvin

Related