nrf_sdh_enable_request() Error:4097 when switch from USBD or Gazell to BLE.

Good day, I am working on function to switch between USBD HID, Gazell and BLE HID with nrf52840 and SDK 17.1.0.

From the very beginning of program, USBD and BLE services are all initiated without any problem, and also no problem when switching between USBD HID and BLE HID.

as per below part coding, switch from USBD to Gazell, at the same time, ble services and ble stack stopped smoothly. but switch back to ble, RTT output unknow error, error no.4097 from nrf_sdh_enable_request(), pls see below part codes.

void set_output(uint8_t output) {
    static uint8_t last_output = OUTPUT_NONE;
    switch(output) {
        case OUTPUT_USB:
            switch (last_output) {
                case OUTPUT_BLE_HID:
                    if (nrf_sdh_is_enabled()) ble_disconnect();
                    break;
                case OUTPUT_GZLL:
                    nrf_drv_clock_lfclk_release();
                    gzll_kbd_device_stop();
                    break;
            }
            if (nrf_drv_usbd_is_enabled()) app_usbd_start();
            break;
        case OUTPUT_BLE_HID:
            switch (last_output) {
                case OUTPUT_GZLL:
                    nrf_drv_clock_lfclk_release();
                    gzll_kbd_device_stop();
                    break;
            }
            if (!nrf_sdh_is_enabled()) {
                ble_stack_init();
                ble_hid_service_start();
            }
            advertising_restart(false);
            break;
        case OUTPUT_GZLL:
            switch (last_output) {
                case OUTPUT_BLE_HID:
                    if (nrf_sdh_is_enabled()) {
                        ble_hid_service_stop();
                        ble_stack_stop();
                    }
            }
            nrf_drv_clock_lfclk_request(NULL);
            gzll_kbd_device_start();
            break;
    }
    last_output = output;
}

void ble_stack_stop(void)
{
    uint32_t err_code;

    err_code = nrf_sdh_disable_request();
    APP_ERROR_CHECK(err_code);

    ASSERT(!nrf_sdh_is_enabled());
}

void ble_stack_init(void) {
    ret_code_t err_code;

    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    ASSERT(nrf_sdh_is_enabled());

    // Configure the BLE stack using the default settings.
    // Fetch the start address of the application RAM.
    uint32_t ram_start = 0;
    err_code           = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
    APP_ERROR_CHECK(err_code);

    // Enable BLE stack.
    err_code = nrf_sdh_ble_enable(&ram_start);
    APP_ERROR_CHECK(err_code);

    // Register a handler for BLE events.
    NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
}

void ble_hid_service_stop(void) {
    ret_code_t err_code;

    err_code = ble_conn_params_stop();
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_stop(m_battery_timer_id);
    APP_ERROR_CHECK(err_code);
}

void ble_hid_service_start(void) {
    // Initialize.
    timers_create();
    // ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();
    // Start execution.
    NRF_LOG_INFO("HID Keyboard example started.");
    timers_start();
    // advertising_restart(false);
}

Related