Page 1 of 1
How to know if device has been connected to the USB ?
Posted: Thu Aug 13, 2009 4:23 pm
by spiky
Hello,
When a certain V-USB device is running , how can the device know if it has been plugged into the USB ? Like how an ipod says "Do not Disconnect" once plugged to the USB. The simplest way maybe to send a control message to the device informing it. But that requires the vendor application to be running.Is it possible to do it without having to resort to that method ?
I looked at the comments in usbdrv.h and usbconfig.h but could not figure out how it can be done. Could you guys please help me ?
Thanks.
Re: How to know if device has been connected to the USB ?
Posted: Thu Aug 13, 2009 9:54 pm
by Grendel
Usually the last thing the host does during enumeration is setting the configuration. You can check if the variable usbConfiguration is not zero for an indication if the device is plugged in and enumerated.
Christian, usbConfiguration should be cleared during a reset condition (usbPoll(), where usbDevice Addr is cleared).
Re: How to know if device has been connected to the USB ?
Posted: Wed Aug 19, 2009 1:54 pm
by spiky
My apologies for the late reply.
Thanks for the suggestion grendel. But it doesnt work for me. I tested the variable usbConfiguration in the main loop. It doesnt change at all. If i disconnect or re-connect the device , it remains the same. Is there something i did wrong ?
Re: How to know if device has been connected to the USB ?
Posted: Thu Aug 20, 2009 1:39 am
by Grendel
spiky wrote:I tested the variable usbConfiguration in the main loop. It doesnt change at all. If i disconnect or re-connect the device , it remains the same. Is there something i did wrong ?
No, the driver has a bug (oversight actually), usbConfiguration doesn't get cleared if a reset happens on the bus:
Grendel wrote:Christian, usbConfiguration should be cleared during a reset condition (usbPoll(), where usbDevice Addr is cleared).
Try adding the line
in usbdrv.c:usbPoll() after the line
Now, usbConfiguration != 0 should signal that the device is connected & enumerated.
Re: How to know if device has been connected to the USB ?
Posted: Mon Aug 24, 2009 8:20 am
by Grendel
Christian, could you flag this for inclusion w/ the next release please ?
Re: How to know if device has been connected to the USB ?
Posted: Wed Aug 26, 2009 4:30 pm
by spiky
Thanks for the tip Grendel , but it doesnt seem to help. usbConfiguration doesnt seem to change at all.
Re: How to know if device has been connected to the USB ?
Posted: Thu Aug 27, 2009 8:21 am
by Grendel
Hm, that's probably because V-USB doesn't detect the disconnect. Forgot about that
In order for this to work you could enable the SOF counter (see USB_COUT_SOF in usbconfig-prototype.h) and monitor the SOFs. If they stop comming you are disconnected and could manually reset the driver with this function:
Code: Select all
void usbUnplugged ( void )
{
usbNewDeviceAddr = 0 ;
usbDeviceAddr = 0 ;
usbConfiguration = 0 ;
usbResetStall() ;
}
Re: How to know if device has been connected to the USB ?
Posted: Thu Aug 27, 2009 10:01 am
by spiky
thanks. i'll try it out .
Re: How to know if device has been connected to the USB ?
Posted: Thu Aug 27, 2009 10:12 am
by Grendel
On a 2nd thought -- the SOFs also go away if the device is suspended, that could be a problem. Am afraid that the only really safe way to detect a disconnect is monitoring the VBus signal.
Re: How to know if device has been connected to the USB ?
Posted: Fri Aug 28, 2009 6:49 pm
by spiky
thanks grendel. That looks like the easiest way.
Re: How to know if device has been connected to the USB ?
Posted: Tue Sep 22, 2009 10:16 am
by christian
Grendel wrote:Christian, usbConfiguration should be cleared during a reset condition (usbPoll(), where usbDevice Addr is cleared).
I considered this, but it costs two bytes of code space. Some boot loaders fit extremely tight and the two bytes may be a problem there. On the other hand, usbConfiguration is rarely used.
If you need to have it reset on USB Reset, add a USB_RESET_HOOK to your usbconfig.h and clear it there.
Regarding the main topic:
The safest way to find out whether USB is connected is the +5V supply from USB. You would normally decouple VBus from the stand-alone supply via a diode and connect the 1k5 pull-up to VBus, not the stand-alone supply. This way you can monitor D- from the main loop. If it is high level, you are connected to a bus. If it is low level for several samples in a row, you are disconnected.
You probably won't ever sample a low level data bit because the interrupt routine is running while data is transferred.