The nrf52840 usbd examples of peripheral in SDK 13.0.0 worked fine. I want usb work with BLE softdevice and there are lots of conflicts between nrf drv and softdevice. Is there any example of usbd working together with ble softdevice?
The nrf52840 usbd examples of peripheral in SDK 13.0.0 worked fine. I want usb work with BLE softdevice and there are lots of conflicts between nrf drv and softdevice. Is there any example of usbd working together with ble softdevice?
Hi,
I just tested this without any problems. Only thing I had to do was to comment out the buttons_leds_init()
function used in the ble_app_uart code, so it didn’t overwrite the button functionality used for the USBD part of the code.
My main code looks like this:
int main(void)
{
uint32_t err_code = 0;
bool erase_bonds;
uint32_t ret = 0;
// Initialize.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
uart_init();
err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("\r\nUART Start!\r\n");
ret = nrf_drv_power_init(NULL);
APP_ERROR_CHECK(ret);
ret = nrf_drv_clock_init();
APP_ERROR_CHECK(ret);
NRF_LOG_INFO("Hello USB!\r\n");
/* Configure LEDs */
bsp_board_leds_init();
bsp_board_buttons_init();
serial_number_string_create();
ret = app_usbd_init();
APP_ERROR_CHECK(ret);
app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
ret = app_usbd_class_append(class_cdc_acm);
APP_ERROR_CHECK(ret);
bool last_usb_conn_status = false;
usb_start();
NRF_LOG_INFO("Enabling BLE part!\r\n");
//buttons_leds_init(&erase_bonds);
ble_stack_init();
gap_params_init();
services_init();
advertising_init();
conn_params_init();
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
// Enter main loop.
for (;;)
{
last_usb_conn_status = usb_connection_handle(last_usb_conn_status);
if (bsp_board_button_state_get(BTN_CDC_DATA_SEND))
{
static int frame_counter;
sprintf(m_tx_buffer, "Hello USB CDC FA demo: %u\r\n", frame_counter);
ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, sizeof(m_tx_buffer));
if (ret == NRF_SUCCESS)
{
++frame_counter;
}
}
if (bsp_board_button_state_get(BTN_CDC_NOTIFY_SEND))
{
ret = app_usbd_cdc_acm_serial_state_notify(&m_app_cdc_acm,
APP_USBD_CDC_ACM_SERIAL_STATE_BREAK,
false);
UNUSED_VARIABLE(ret);
}
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
power_manage();
}
}
Sigurd, thanks very much for the quick reply. Don't we also need an updated version of the SDK too? Otherwise the app still can't register a USB power event handler. nrf_drv_power_usbevt_init() doesn't allow it.
From nrf_drv_power.c:
ret_code_t nrf_drv_power_usbevt_init(nrf_drv_power_usbevt_config_t const * p_config)
{
nrf_drv_power_usbevt_uninit();
if (p_config->handler != NULL)
{
m_usbevt_handler = p_config->handler;
#ifdef SOFTDEVICE_PRESENT
if (nrf_sdh_is_enabled())
{
/** @todo Implement USB power events when SD support it */
return NRF_ERROR_INVALID_STATE;
}
else
#endif
{
nrf_power_int_enable(
NRF_POWER_INT_USBDETECTED_MASK |
NRF_POWER_INT_USBREMOVED_MASK |
NRF_POWER_INT_USBPWRRDY_MASK);
}
}
return NRF_SUCCESS;
}
Example application's power event handler and registration of the hander:
static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
{
switch(event)
{
case NRF_DRV_POWER_USB_EVT_DETECTED:
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case NRF_DRV_POWER_USB_EVT_REMOVED:
m_usb_connected = false;
break;
case NRF_DRV_POWER_USB_EVT_READY:
m_usb_connected = true;
break;
default:
ASSERT(false);
}
}
static const nrf_drv_power_usbevt_config_t config =
{
.handler = power_usb_event_handler
};
ret_code_t ret;
ret = nrf_drv_power_usbevt_init(&config);
APP_ERROR_CHECK(ret);
Thanks! Don
Sigurd, thanks very much for the quick reply. Don't we also need an updated version of the SDK too? Otherwise the app still can't register a USB power event handler. nrf_drv_power_usbevt_init() doesn't allow it.
From nrf_drv_power.c:
ret_code_t nrf_drv_power_usbevt_init(nrf_drv_power_usbevt_config_t const * p_config)
{
nrf_drv_power_usbevt_uninit();
if (p_config->handler != NULL)
{
m_usbevt_handler = p_config->handler;
#ifdef SOFTDEVICE_PRESENT
if (nrf_sdh_is_enabled())
{
/** @todo Implement USB power events when SD support it */
return NRF_ERROR_INVALID_STATE;
}
else
#endif
{
nrf_power_int_enable(
NRF_POWER_INT_USBDETECTED_MASK |
NRF_POWER_INT_USBREMOVED_MASK |
NRF_POWER_INT_USBPWRRDY_MASK);
}
}
return NRF_SUCCESS;
}
Example application's power event handler and registration of the hander:
static void power_usb_event_handler(nrf_drv_power_usb_evt_t event)
{
switch(event)
{
case NRF_DRV_POWER_USB_EVT_DETECTED:
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case NRF_DRV_POWER_USB_EVT_REMOVED:
m_usb_connected = false;
break;
case NRF_DRV_POWER_USB_EVT_READY:
m_usb_connected = true;
break;
default:
ASSERT(false);
}
}
static const nrf_drv_power_usbevt_config_t config =
{
.handler = power_usb_event_handler
};
ret_code_t ret;
ret = nrf_drv_power_usbevt_init(&config);
APP_ERROR_CHECK(ret);
Thanks! Don