6
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Thank you very much for your reply, I found this will solve my problem but I have a question even when my PC is in sleep mode then USB is powering through battery. so at that time it is failing to off the switch. \$\endgroup\$ – verendra Aug 15 '13 at 7:43
  • \$\begingroup\$ How are you sending commands? Have you wrote a application on your computer that communicates with the microcontroller or is it with HyperTerminal or something similar? \$\endgroup\$ – Wallace Aug 15 '13 at 7:54
  • \$\begingroup\$ I have designed user interface in C#, I am sending the commands from the interface and i am receiving the response from controller. \$\endgroup\$ – verendra Aug 15 '13 at 8:11
  • \$\begingroup\$ I would go to programmers.stackexchange and ask if there was a way, in C#, to determine if the computer is in sleep mode. If so, then I would have the interface you designed to automatically send the off command when "sleep_mode == true". \$\endgroup\$ – Wallace Aug 15 '13 at 19:26
  • \$\begingroup\$ Another problem I can see you running into is if you close the C# app without turning off the switch. You might have to make sure that the app sends "off" command "onExit". \$\endgroup\$ – Wallace Aug 15 '13 at 19:39
2
\$\begingroup\$

It is likely that the solution which would best match your requirement would be to look for when the 1 ms USB frame events cease to happen. There should be related logic in your USB code for handling these events, which you could have reset a loss of frame timer. If that timer manages to expire, you know the PC is no longer actively managing the USB, even if VBUS is still present.

\$\endgroup\$
  • \$\begingroup\$ @Chirs Stratton Thanks for your reply but i am new to controller programming, i could not get the concept clearly, if i am right, i need a timer to check USB frame event lost or not. Isn't it? if not can you give some info about it? \$\endgroup\$ – verendra Aug 15 '13 at 12:14
1
\$\begingroup\$

how to know usb connection is lost or not?

Look in the manual of your microcontroller, there should be info on how to catch the USB Suspend event. This event occurs when the host ceases to communicate with the device altogether for more than 3 ms - not sending even frame events. The USB devices are supposed to lower their USB current in this state.

Note that this will not be helpful in case the driver or control program simply gets killed, e.g. by task manager. The USB frame event will still be sent every millisecond.

\$\endgroup\$
  • \$\begingroup\$ I have one more doubt If i lost USB connection then microcontroller will stop working or keep it works until power goes down without dependent of USB connection. \$\endgroup\$ – verendra Aug 16 '13 at 19:41
  • \$\begingroup\$ This is the only correct answer to OP's inquiry. \$\endgroup\$ – Ale..chenski Sep 3 '16 at 17:36
  • \$\begingroup\$ To make sure that the host is really gone, the device can try to RESUME the link. If no response, then the host is really dead. \$\endgroup\$ – Ale..chenski Sep 3 '16 at 17:49
1
\$\begingroup\$

If you're using the LUFA framework for managing the USB communication, then you can use the EVENT_USB_Device_Disconnect() event to detect the physical disconnect of the cable (the same as the VBUS detection Wallace pointed out).

Chris Stratton's answer is also supported in the LUFA framework, via the EVENT_USB_Device_StartOfFrame() which is received every 1mS, and can be used to detect a valid protocol-level connection. Simply reset a timer every time the event fires, and check if the timer ever overflows - if so, then your device is physically connected, but not communicating.

You can find out more about these events in LUFA via the documentation here: LUFA USB Events

If you're rolling your own implementation, there is a lot to be learned from the LUFA source code and examples for AVR.

\$\endgroup\$
0
\$\begingroup\$

A non USB-specific way would be for the controller to occasionally query the PC and expect an acknowledgement. After N queries without an acknowledgement, the controller can assume that communication has been lost.

\$\endgroup\$
  • \$\begingroup\$ can you give me some more clear idea how to do that, non USB specific way means i think without USB connection check. How to do this can you give some example. \$\endgroup\$ – verendra Aug 15 '13 at 7:46
  • \$\begingroup\$ @verendra, this is the solution I'd use - some PCs leave power applied when shutdown and this also checks your software is working. He just means apart the commands you already have add a new one like you send OK? to the PC and your app responds YES. If you don't get a response for a while you know something is wrong (or the PC is shutdown) and you can turn off the output after a while. \$\endgroup\$ – PeterJ Aug 15 '13 at 8:21
  • \$\begingroup\$ @PeterJ Here i am sending commands from PC (c# interface) to controller and controller is responding to those commands. From your point of view i need to send command from Controller? not from PC? isn't it? \$\endgroup\$ – verendra Aug 15 '13 at 9:34
  • \$\begingroup\$ @verendra, yes that's right. The answer Chris Stratton just posted is another good one also if you don't need to check your software is running OK as well. \$\endgroup\$ – PeterJ Aug 15 '13 at 11:59
  • \$\begingroup\$ @verendra: By "non USB-specific", I meant the technique doesn't require any special features of USB protocol or USB hardware. It could work with any kind of communication channel - RS-232, Ethernet, WiFi; you wouldn't need to know or care. In fact, isn't that just like what we do verbally when we have a crap cell-phone connection? And yes, in this case, the controller would be asking and the PC would be replying. \$\endgroup\$ – JRobert Aug 15 '13 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.