How to check USB connection presence?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
Dear XMC community.
I use Relax Lite Kit, DAVE 3.1.10 and USBD_VCOM app. One function of my device is to show time on LCD screen, it runs on batteries but sometimes USB cable might be connected to set time or just to power the device.
When the only power supply are batteries I want the device to go to deep sleep mode (with stopping USB clock if possible). When there is a USB power supply I want it to work in normal mode.
Can I detect USB presence using software? Maybe software can detect USB VBUS presence - couldn't understand it from reference manual.
Thank You!
0 Likes
5 Replies
Not applicable
I don't believe that there is no easy way to check USB connection presence.
0 Likes
AngelB
Employee
Employee
Hi Dronchic,

It depends on the hardware set up.

- If the ID line is connected you can have easily the disconnect Interrupt when the ID line gets disconnected.

- If the ID line is not connected you will not have the disconnect interrupt and the process is a bit more complex.
In this case the device disconnect detection flow is as follows:
1. When the USB cable is unplugged or when the VBUS is switched off by the Host, the
device core triggers the RESET(GINTSTS.USBRst [bit 12]) interrupt bit.
2. When the device application detects GINTSTS.USBRst, the application sets a timeout
check for set address control transfer from host.
3. If application does not receive set address control transfer from host before the timeout
period, it is treated as a device disconnection.


The Relax Lite Kit has not connected the ID line so, in your case, the process to follow would fit to the second point described.


Best regards,
Angel
0 Likes
Not applicable
Hi AngelB,
Thanks for your answer, I'll try to implement your method.
I've found USB_DeviceGetState() function. It returns USB Device status flags such as "powered" and "connected".
These flags are set correctly when I plug USB cable in, but they don't reset when I unplug USB cable.
Best regards,
Andrey
0 Likes
Not applicable
Dear Andrey

I have the same situation as you for my Relax kit and Dave3. The USB_DeviceGetState() bits are set correctly such as "active", "powered" and "connected" when I plug USB cable
in, but they don't reset when I unplug USB cable

Did you find any solution that you can share to detect disconnection when USB cable is unplugged.

thanks,
Rehan
0 Likes
Not applicable
Hello, maybe with new version your problem is solved. Could you try to update your dave3 to dave4.1.4. Did you used dave app or xmc lib? In both cases you should update to latest version too.
0 Likes