Electronic – How to know usb connection is lost

avrembeddedmicrocontrollerprogrammingusb

I am working with AT90USB1287 controller with USB communication to PC. I am sending some commands from PC to controller using USB communication to activate some controlled switch. I am able to change the states of switch by sending commands(ON and OFF, voltage controlled switch).

My problem is when switch is ON and unfortunately user forgot to send OFF command from PC and he shutdown the PC but still controller is running or suppose if i lost PC connection via usb, At that times it is causing problems.

I want to check the in ATUSB1287 controller code itself if usb connection loosed then swicth t command should be zero.

Can any one help me how to know usb connection is lost or not? this is my approach like is it possible to check in the main loop that usb connection is lost? if lost then switch off.

Any other way to switch off? after losing connection to PC or PC shutdown.

I am new to this type of thing, if anything wrong in this question please excuse me.

Best Answer

On page 258 of the datasheet:

• 0 – VBUS: VBus flag
The value read from this bit indicates the state of the VBUS pin. This bit can be used in device mode to monitor the USB bus connection state of the application. See Section 22.10, page 255 for more details.

On page 255:

22.10 Plug-in detection
The USB connection is detected by the VBUS pad, thanks to the following architecture:
enter image description here
The control logic of the VBUS pad outputs a signal regarding the VBUS voltage level:
• The “Session_valid” signal is active high when the voltage on the VBUS pad is higher or equal to 1.4V. If lower than 1.4V, the signal is not active
•The “Vbus_valid” signal is active high when the voltage on the VBUS pad is higher or equal to 4.4V. If lower than 4.4V, the signal is not active
• The VBUS status bit is set when VBUS is greater than “Vbus_ valid”. The VBUS status bit is cleared when VBUS falls below “Session_valid” (hysteresis behavior)
• The VBUSTI flag is set each time the VBUS bit state changes

Not sure if this Is what your looking for but figured I would post it anyway.