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

How to get the voltage level of USB D+ and D-

Hi, I want to distinguish whether the USB is connected to a PC or a power adapter.

For my application, FATFS still needs to work when connected to a power adapter, so it can not be distinguished via the USB event, as described in https://devzone.nordicsemi.com/f/nordic-q-a/36414/usb.

My idea is that distinguish by the voltage level of D+ and D- when USB is connected. I found a USB register DPDMVALUE  relate to the D+ and D-, but I always get a zero value whether connecting to a PC or a power adapter.

My question is how to get the value of DPDMVALUE ? Does it need any other setting? Thanks!

Parents
  • Hi Jason,

    maybe I didn't understand your case right, but I think you can still use USB SOF event - for wall adapter, there will be only APP_USBD_EVT_POWER_DETECTED.

    DPDMVALUE can only force D+ and D- to some state, it cannot be used to detect their states.

  • Hi Dmitry.

    Thank you for your reply.

    For the wall adapter, APP_USBD_EVT_POWER_DETECTED is not the only event. 

    void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
    	if(event)
    	{
    		NRF_LOG_INFO("USB event:%d", event);
    	}
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                //bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                //bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                //bsp_board_led_on(LED_USB_START);
                break;
            case APP_USBD_EVT_STOPPED:	  
    		    NRF_LOG_INFO("USB stopped");
                UNUSED_RETURN_VALUE(fatfs_init());
                app_usbd_disable();
                //bsp_board_leds_off();
                break;
            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;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                m_usb_connected = true;
                break;
            default:
                break;
        }
    }

    the log as below when wall adapter is connected:

    00> <info> app: USB event:8
    00>
    00> <info> app: USB power detected
    00>
    00> <info> app:

    00>
    00> <info> app: Un-initializing disk 0 (QSPI)...
    00>
    00> <info> app: USB event:10
    00>
    00> <info> app: USB ready
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:13
    00>
    00> <info> app: USB event:2

    the log as below when PC is connected:

    00> <info> app: Un-initializing disk 0 (QSPI)...
    00>
    00> <info> app: USB event:10
    00>
    00> <info> app: USB ready
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:13
    00>
    00> <info> app: USB event:2
    00>
    00> <info> app: USB event:3
    00>
    00> <info> app: USB event:1
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:1
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:15

    I tried to move the fatfs_uninit function to the case of APP_USBD_EVT_DRV_RESUME event, but the MSC did not work .

     Actually, My target is that do not un-initialized the disk if connect to a wall adapter.

Reply
  • Hi Dmitry.

    Thank you for your reply.

    For the wall adapter, APP_USBD_EVT_POWER_DETECTED is not the only event. 

    void usbd_user_ev_handler(app_usbd_event_type_t event)
    {
    	if(event)
    	{
    		NRF_LOG_INFO("USB event:%d", event);
    	}
        switch (event)
        {
            case APP_USBD_EVT_DRV_SUSPEND:
                //bsp_board_led_off(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_DRV_RESUME:
                //bsp_board_led_on(LED_USB_RESUME);
                break;
            case APP_USBD_EVT_STARTED:
                //bsp_board_led_on(LED_USB_START);
                break;
            case APP_USBD_EVT_STOPPED:	  
    		    NRF_LOG_INFO("USB stopped");
                UNUSED_RETURN_VALUE(fatfs_init());
                app_usbd_disable();
                //bsp_board_leds_off();
                break;
            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;
            case APP_USBD_EVT_POWER_READY:
                NRF_LOG_INFO("USB ready");
                app_usbd_start();
                m_usb_connected = true;
                break;
            default:
                break;
        }
    }

    the log as below when wall adapter is connected:

    00> <info> app: USB event:8
    00>
    00> <info> app: USB power detected
    00>
    00> <info> app:

    00>
    00> <info> app: Un-initializing disk 0 (QSPI)...
    00>
    00> <info> app: USB event:10
    00>
    00> <info> app: USB ready
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:13
    00>
    00> <info> app: USB event:2

    the log as below when PC is connected:

    00> <info> app: Un-initializing disk 0 (QSPI)...
    00>
    00> <info> app: USB event:10
    00>
    00> <info> app: USB ready
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:13
    00>
    00> <info> app: USB event:2
    00>
    00> <info> app: USB event:3
    00>
    00> <info> app: USB event:1
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:1
    00>
    00> <info> app: USB event:15
    00>
    00> <info> app: USB event:15

    I tried to move the fatfs_uninit function to the case of APP_USBD_EVT_DRV_RESUME event, but the MSC did not work .

     Actually, My target is that do not un-initialized the disk if connect to a wall adapter.

Children
No Data
Related