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

Problem when switching role from peripheral to central on S120

Hi,

I am now on implementing a dynamic change between the 2 possible roles central and peripheral. I try to do this like described here: nordicsemi.com/question/35367... But when trying to switch from peripheral to central, I always get an error on the SOFTDEVICE_HANDLER_INIT() function. My ble_stack_init() function looks like this:

static uint32_t ble_stack_init(uint8_t role)
{
uint32_t err_code;
	ble_uuid128_t base_uuid = DEVICE_PERIPHERAL_BASE_UUID;

	// Initialize the SoftDevice handler module.
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));

	if(role == BLE_GAP_ROLE_PERIPH) ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
	else if (role == BLE_GAP_ROLE_CENTRAL) ble_enable_params.gatts_enable_params.service_changed = false;
#ifdef S120  
  ble_enable_params.gap_enable_params.role              = role;
#endif    
	err_code = sd_ble_enable(&ble_enable_params);
    APP_ERROR_CHECK(err_code);
	
	if(role == BLE_GAP_ROLE_PERIPH)
	{
		err_code = sd_ble_uuid_vs_add(&base_uuid, &m_base_uuid_type);
		APP_ERROR_CHECK(err_code);
	}
	
	err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
	APP_ERROR_CHECK(err_code);
	
	if(role == BLE_GAP_ROLE_PERIPH)
	{
		// Register with the SoftDevice handler module for BLE events.
		err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
		APP_ERROR_CHECK(err_code);
	}
	return err_code;
}

Before I call this ble_stack_init() function, I disable the softdevice with:

err_code = sd_softdevice_disable();

SOFTDEVICE_HANDLER_INIT() returns error code 4097. For me this does not look like a valid error code. The documentation for that function says, that SOFTDEVICE_HANDLER_INIT should be called only once. So, how to achive the switch between the roles? Is there a working example with the S120 where the role is switched somewhere in the application?

Regards, BTprogrammer

  • Ok. I tried your code. Seems to be working fine here. I tested with S120 v2.1.0 and SDK 10.0.0. Are these the versions you are working with as well? I think you should use softdevice_handler_sd_disable() instead of sd_softdevice_disable() directly, but I don't think that is the cause of your issue.

  • Hello, thanks for testing my code. I am working with the SDK 8.1.0 and the integrated sofdevice version. Do I have to switch the SDK version!? I think this would be a bigger job for me to do because it is an existing project.

  • I tried your code with SDK 8.1.0, with compatible S120 SoftDevice. Seems to be working fine as well. Could you upload a project which reproduces the issue? Have you changed any of the libraries? I just tried this:

    err_code = ble_stack_init(BLE_GAP_ROLE_PERIPH);
    APP_ERROR_CHECK(err_code);
    
    err_code = sd_softdevice_disable();
    APP_ERROR_CHECK(err_code);
    
    err_code = ble_stack_init(BLE_GAP_ROLE_CENTRAL);
    APP_ERROR_CHECK(err_code);
    
  • Ok, I did a quick test with the more simple ble_app_uart_c example:

    ble_stack_init(BLE_GAP_ROLE_PERIPH); sd_softdevice_disable(); ble_stack_init(BLE_GAP_ROLE_CENTRAL); This worked. So there must be a problem with the rest of my application code. I am using SPI, TWI and UART in my final application. Do I have to take care with one of these drivers when disabling the ble stack / switching the role?

  • I don't think so, but I haven't done much testing with switching roles with the S120.

Related