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

Disabling/Enabling softdevice with a button

I have a nRF51DK. And I would like to be able, to enable/disable the softdevice with 2 buttons.

Button 1 on nRF51DK --> Enable Softdevice

Button 2 on nRF51DK --> Disable Softdevice

Why Disable? Its not enough, if the Board is just invisible. Because I want to use faster timers (5-10us) and maybe Timer0.

There is a multiprotocol example in the sdk -->ble_app_gzll

My enabling and disabling of the softdevice is based on this example.

What is working? When I press on Button 1 the softdevice will be initialized and the board will be visible on the MCP. If I then press Button 2, the board isnt visible anymore and I can use the timer with 10us interval.

The problem is, when try to connect it will cause an error:

NRF_ERROR_INVALID_STATE               (NRF_ERROR_BASE_NUM + 8)  

Invalid state, operation disallowed in this state.

static void on_connect(ble_evt_t * p_ble_evt)
{
    m_conn_handle         = p_ble_evt->evt.gap_evt.conn_handle;
    m_current_conn_params = p_ble_evt->evt.gap_evt.params.connected.conn_params;
    m_update_count        = 0;  

    if (m_conn_params_config.start_on_notify_cccd_handle == BLE_GATT_HANDLE_INVALID)
    {
        conn_params_negotiation();
    }
}

I will get into the conn_params_negotiation(); function, but I dont understand why.

When I hit the break point on the if query:

start_on_notify_cccd_handle = 0x0000

BLE_GATT_HANDLE_INVALID = 0x00000000

These values seems not be right for me. But they are the same on working program...

Here my initializaton of the softdevice after Button1 click:

void ble_stack_start(void)
{
bool erase_bonds;
uint32_t err_code;

SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, NULL);

// Enable BLE stack 
ble_enable_params_t ble_enable_params;
memset(&ble_enable_params, 0, sizeof(ble_enable_params));
#ifdef S130
ble_enable_params.gatts_enable_params.attr_tab_size   = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
#endif
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
err_code = sd_ble_enable(&ble_enable_params);
APP_ERROR_CHECK(err_code);

err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
APP_ERROR_CHECK(err_code);

   // Register with the SoftDevice handler module for BLE events.
   err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
   APP_ERROR_CHECK(err_code);

  //BLE_STACK_INIT
  device_manager_init(erase_bonds);
  gap_params_init();
  advertising_init();
  services_init();
  conn_params_init();
  application_timers_start();
	
  err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
  APP_ERROR_CHECK(err_code);
}

void ble_stack_stop(void)
{
    uint32_t err_code;

    err_code = softdevice_handler_sd_disable();
    APP_ERROR_CHECK(err_code);
}

Maybe the initialization is not the problem. Because its the same as it is in the templates. Maybe I have to do more stuff in the ble_stack_stop function?

I hope you can help me

Here the error:

int main(void)
{
     previous_mode = running_mode;

     timers_init();
     in_output_config();
     gpiote_init();

    // Enter main loop.
    for (;;)
    {
        power_manage();
    if (running_mode != previous_mode)
        {
            previous_mode = running_mode;
				
            if (running_mode == 0)
            {   
            //nrf_drv_gpiote_uninit();
            // Disable the S110 stack.
            ble_stack_stop();
            timers_init();
	start_timer();
            }
            else if (running_mode == 1)
            {
	    stop_timer();
                ble_stack_start();
               timers_init();
            }
        }
    }
}

Error reason:

In the main loop, timers_init will be called twice.

Parents
  • When you disable the stack, you also clear all memory regions used by the stack and the protocol extension (in this case BLE). In other words, the entire GATT database has been removed.

    When you enable the stack, you need to both re-enable the stack extension (sd_ble_enable) and initialize the GATT database with the required services and characteristics.

    To stop the stack you only need to call "sd_softdevice_disable".

Reply
  • When you disable the stack, you also clear all memory regions used by the stack and the protocol extension (in this case BLE). In other words, the entire GATT database has been removed.

    When you enable the stack, you need to both re-enable the stack extension (sd_ble_enable) and initialize the GATT database with the required services and characteristics.

    To stop the stack you only need to call "sd_softdevice_disable".

Children
No Data
Related