How to detect if USB connected?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to detect if USB connected?

2,573 Views
ashesman
Contributor III

Hi,

How can I detect if a USB cable is plugged in?  I have a FAT file system and a USB MSC both sharing the same SD card.  I want to only enable the MSC (and disable FAT) at startup if a USB cable is connected.  USB 5V is not connected directly to the K24F I am using.

How can I do this?

Thanks in advance...

Ashley

0 Kudos
Reply
6 Replies

2,303 Views
jorge_a_vazquez
NXP Employee
NXP Employee

Hi Ashley

We recommend to use a GPIO pin as VBUS detection, which also could wake up the chip from all low power mode (includes LLS mode or VLLSx mode (using LLWU module with external pin)).

 

The VBUS connects to VERGIN and a GPIO pin doesn't cause conflict. I think customer can fly a wise to connect VBUS with related GPIO pin.


Hope this information helps you
Have a great day,
Jorge Alcala

0 Kudos
Reply

2,303 Views
yinni
Contributor III

Hi Jorge,

I did exactly what you suggested for my MK50 system. But my problem is my GPIO that connects to VBUS and also VREGIN always reads HIGH because VREGIN measured about 3.3V without USB connection while measured 5V with USB connection. I have also tried to disable USB voltage regulator during initialization, as a result I got 2.9V at VREGIN which still a logic HIGH.

How can I solve this problem?

Many thanks in advance!

0 Kudos
Reply

2,303 Views
mjbcswitzerland
Specialist V

Hi

Why not just have a short delay to see whether USB enumerates or not? If it enumerates within this time it is connected and if not it is not connected.
If not connected, things can then be activated that are dependent on this.

This is the way that the uTasker USB-MSD loader works, which automatically operates with either a memory stick (in host mode) or USB-MSD (device mode). It starts the USD as device and if it enumerates withing about 500ms it stays in that mode. If it doesn't, it switches to host mode to try to detect a memory stick instead (and applies host power in the process).

Regards

Mark


Complete Kinetis solutions for faster/more efficient professional needs, training and support: http://www.utasker.com/kinetis.html
i.MX RT project compatibility: http://www.utasker.com/iMX.html
Including FreeRTOS integration for all Kinetis parts


uTasker: supporting >1'000 registered Kinetis users get products faster and cheaper to market

Request Free emergency remote desk-top consulting at http://www.utasker.com/services.html

Open Source version at https://github.com/uTasker/uTasker-Kinetis

https://community.nxp.com/thread/512558
https://community.nxp.com/thread/352862
https://community.nxp.com/thread/498809

0 Kudos
Reply

2,303 Views
yinni
Contributor III

Hi Mark,

Thanks a lot for the suggestion. But I am a newbie and wondering if you could give me a hint on how to check USB enumeration. 

Isn't a USB enumeration done by the USB host?

I searched usb enumeration and found the following in usb_device.h but don't know how to use it

* @brief Device callback function typedef.
*
* This callback function is used to notify the upper layer that the device status has changed.
* This callback pointer is passed by calling API #USB_DeviceInit.
*
* @param handle The device handle. It equals the value returned from #USB_DeviceInit.
* @param callbackEvent The callback event type. See enumeration #usb_device_event_t.
* @param eventParam The event parameter for this callback. The parameter type is determined by the callback event.
*
* @return A USB error code or kStatus_USB_Success.
*/
typedef usb_status_t (*usb_device_callback_t)(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);

Many thanks!

0 Kudos
Reply

2,303 Views
mjbcswitzerland
Specialist V

Hi

In the uTasker project the user's task receives an event E_USB_ACTIVATE_CONFIGURATION when enumeration has completed and an active configuration is to be activated. This is used as confirmation of successful enumeration, and so that a USB Host is present and has completed this communication initialisation.

Your framework routine looks to give you a call back which you enter which will be executed when the USB state changes and so is probably suitable. The documentation probably describes the callback event numbers used, but you may need to be careful that it may be a callback form an interrupt routine and thus will need to be handled quickly to potentially avoid blocking the system.

You can also try it out and see which events are received and when - again be carefully because if you use a debugger to break in the routine it will tend to cause USB enumeration failure since this has quite strict timing requirements; it is better to "record" the events received and look at them later.

Regards

Mark

0 Kudos
Reply

2,303 Views
ashesman
Contributor III

Hi, thanks for your help.  I have already put in a request to put VBUs onto a GPIO in next hardware revision.  I was just hoping there was a way that the voltage regulator or USB peripheral could be used to detect a connection through software.

0 Kudos
Reply