How to know if device has been connected to the USB ?
How to know if device has been connected to the USB ?
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.
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 ?
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).
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 ?
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 ?
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 ?
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
Code: Select all
usbConfiguration = 0 ;
in usbdrv.c:usbPoll() after the line
Code: Select all
usbDeviceAddr = 0;
Now, usbConfiguration != 0 should signal that the device is connected & enumerated.
Re: How to know if device has been connected to the USB ?
Christian, could you flag this for inclusion w/ the next release please ?
Re: How to know if device has been connected to the USB ?
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 ?
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 ?
thanks. i'll try it out .
Re: How to know if device has been connected to the USB ?
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 ?
thanks grendel. That looks like the easiest way.
Re: How to know if device has been connected to the USB ?
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.