USB Power detect in SDK 16.0 as event to prevent polling

Hi Nordic

I have been using your example on USB MST to embed it into my application

Everything works ok, but in the main loop im constantly polling for USB power event(app_usbd_event_queue_process()) to detect when the USB cable is connected and where I should initialize the USB stack

Is there a way to register events from the softdevice so the usb event handler is called when an usb cable is inserted ?

I have already added following

static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{

case APP_USBD_EVT_POWER_DETECTED:
NRF_LOG_INFO("USB power detected");

if (!nrf_drv_usbd_is_enabled())
{
fatfs_uninit();
app_usbd_enable();
}
break;
case APP_USBD_EVT_POWER_REMOVED:
NRF_LOG_INFO("USB power removed");
app_usbd_stop();
m_usb_connected = false;
break;

....

...

}

}

init usb()

{

static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler };

app_usbd_power_events_enable();

sd_power_usbpwrrdy_enable(true);
sd_power_usbdetected_enable(true);
sd_power_usbremoved_enable(true);

}

main()

{

app_sched_execute();
#if DEBUG
while (NRF_LOG_PROCESS());
#endif
power_management_run();

     while (app_usbd_event_queue_process());

}

Best Regards

Tommy F Kristensen

  • Hello Tommy,

    The reason why the examples are using the app_usbd_event_queue_process() is that there are some bugs in the USB drivers, which prevents the USB event handler from working properly. I see from some internal communication that this was not fixed in the nRF5 SDK, and I wouldn't rely on that being fixed in the future (in the nRF5 SDK), since we put most of our efforts in the nRF Connect SDK at this point in time. 

    Best regards,

    Edvin

  • Hi Edvin 

    Thanks for your answer. I was trying to configure a SOC handler and then get the power events this way

    Some thin like this, and then handle the USB stack init here. Is this an reasonable way to go and what have to be enabled to use this, do you have an example, as it is a bit big task to switch SDK now

    NRF_SDH_SOC_OBSERVER(m_soc_observer, APP_SOC_OBSERVER_PRIO, soc_evt_handler, NULL);

    // SoftDevice SoC event handler.
    static void soc_evt_handler(uint32_t evt_id, void * p_context)
    {
    switch (evt_id)
    {
    case NRF_EVT_POWER_USB_POWER_READY: /**< Event indicating that a USB 3.3 V supply is ready. */

    break;
    case NRF_EVT_POWER_USB_DETECTED: /**< Event indicating that voltage supply is detected on VBUS. */

    break;

    case NRF_EVT_POWER_USB_REMOVED: /**< Event indicating that voltage supply is removed from VBUS. */

    break;

    default:
    // No implementation needed.
    break;
    }
    }

    Best Regards

    Tommy F Kristensen

  • I think the issue is that when the USB is interrupt driven, there are events that doesn't occur. If you get the NRF_EVT_POWER_USB_POWER_READY and the other events that you need using this implementation, then I guess it is fine. Do you see these events?

    I don't remember the details of what events that doesn't occur, but if you get these events, and it is all you need, then I guess it can work. But if you want to do data transfers over USB, then I don't think you can use it in interrupt mode because of the lacking events.

    Best regards,

    Edvin

Related