Detect that device has been disconnected from PC USB Host

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

cross mob
Not applicable
Scenario: The XMC45 is connected to a PC using a USB cable. The cable is then removed. I want to detect this event, but how?. I've seen several similar questions on the forum, but no satisfactory answer.

I'm OK with a workaround solution.

I've tested with polling for the SOF interrupt, but it is generated even if Host is removed, which I find a bit odd.
0 Likes
9 Replies
User12775
Level 5
Level 5
First solution authored First like received
I hope this is what you need:

WM_DEVICECHANGE message
0 Likes
Not applicable
Thanks for the answer, but no. I'm not interested in detecting the event on the PC. I'm only interested in detecting it on the XMC45. The title of this thread was a bit ambigous - sorry for that.
0 Likes
jferreira
Employee
Employee
10 sign-ins 5 sign-ins First like received
Hi,

If you have the USB_ID connected as below, the connection/disconnection is indicated by the Interrupt Register (GINTSTS)
2723.attach

Otherwise you will need to use a software procedure which is described in the reference manual of the XMC4200_XMC4100, section 13.4.3 Device Disconnection.

Regards,
Jesus
0 Likes
Not applicable
Thanks for the answer.

I don't see how a pull up on the ID pin, as suggested in the picture, would help. The ID pin should be floating both when USB cable is inserted and when it is removed. The solution in the picture seems to be for detecting USB A-plugs (OTG), which is only useful if acting as a Host.

Section 13.4.3 of the XMC4200_XMC4100 reference manual suggests that the GINTSTS.USBRst interrupt should be generated after the removal of the cable. Unfortunately, no such interrupt is generated on the XMC45 when cable is removed.
0 Likes
User12775
Level 5
Level 5
First solution authored First like received
Have you enabled the GINTSTS.USBRst interrupt?
0 Likes
Not applicable
Yes, the GINTSTS.USBRst interrupt is enabled.
0 Likes
Not applicable
For some reason, I do get the GINTSTS.OTGInt interrupt when removing the cable from another PC Host. The interrupt won't fire if cable is removed from my main PC Host. Very odd.
0 Likes
Not applicable
Has anyone found a solution/workaround to this problem?
0 Likes
Not applicable
The problem was specific to the XMC4500 Relax Kit board. The VBUS signals from the main plug (X3) and the debug plug (X100) are connected to each other. This is shown in figure 4 in the Board Users Manual for the Relax Kit.
When the USB cable was removed from the X3 plug, VBUS would remain high as the X100 still supplied VBUS. This prevented the USB controller from detecting that the PC Host had stopped holding VBUS high and no GINTSTS.OTGInt interrupt were generated.

The solution was to remove the D2 diode connected to VBUS on the X3 plug. VBUS would then go low when connection to PC Host was lost, causing the GINTSTS.OTGInt interrupt to trigger as expected.
0 Likes