How to know if device has been connected to the USB ?

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
spiky
Posts: 18
Joined: Mon Jul 27, 2009 4:27 pm

How to know if device has been connected to the USB ?

Post by spiky » Thu Aug 13, 2009 4:23 pm

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.

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Re: How to know if device has been connected to the USB ?

Post by Grendel » Thu Aug 13, 2009 9:54 pm

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).

spiky
Posts: 18
Joined: Mon Jul 27, 2009 4:27 pm

Re: How to know if device has been connected to the USB ?

Post by spiky » Wed Aug 19, 2009 1:54 pm

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 ?

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Re: How to know if device has been connected to the USB ?

Post by Grendel » Thu Aug 20, 2009 1:39 am

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.

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Re: How to know if device has been connected to the USB ?

Post by Grendel » Mon Aug 24, 2009 8:20 am

Christian, could you flag this for inclusion w/ the next release please ?

spiky
Posts: 18
Joined: Mon Jul 27, 2009 4:27 pm

Re: How to know if device has been connected to the USB ?

Post by spiky » Wed Aug 26, 2009 4:30 pm

Thanks for the tip Grendel , but it doesnt seem to help. usbConfiguration doesnt seem to change at all.

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Re: How to know if device has been connected to the USB ?

Post by Grendel » Thu Aug 27, 2009 8:21 am

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() ;
}

spiky
Posts: 18
Joined: Mon Jul 27, 2009 4:27 pm

Re: How to know if device has been connected to the USB ?

Post by spiky » Thu Aug 27, 2009 10:01 am

thanks. i'll try it out .

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Re: How to know if device has been connected to the USB ?

Post by Grendel » Thu Aug 27, 2009 10:12 am

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.

spiky
Posts: 18
Joined: Mon Jul 27, 2009 4:27 pm

Re: How to know if device has been connected to the USB ?

Post by spiky » Fri Aug 28, 2009 6:49 pm

thanks grendel. That looks like the easiest way.

christian
Objective Development
Objective Development
Posts: 1443
Joined: Thu Nov 09, 2006 11:46 am

Re: How to know if device has been connected to the USB ?

Post by christian » Tue Sep 22, 2009 10:16 am

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.

Post Reply