LUFA USB CDC - Detect when host application connects

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using LUFA to create a virtual serial connection to the host computer.

Is it possible for the microcontroller to detect when a host application makes a connection?
For example, when a terminal emulation program connects to the COM port?

I'm looking for a USB bus event that signals that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use EVENT_CDC_Device_ControLineStateChanged() and check for the DCD (Data Carrier Detect) virtual line, similar to this one that uses DTR:

http://code.google.com/p/lufa-li...

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The only DCD that I can find is: CDC_CONTROL_LINE_IN_DCD which is "notification from the device to the host" where the CDC_CONTROL_LINE_OUT_DTR is "request from the host".

From this it seems that _OUT_ is from the host and _IN_ is from the device.

I'm getting this from group___group___u_s_b_class_c_d_c_common.html

LUFA Library -> Modules -> USB Class Drivers -> CDC-ACM (Virtual Serial) Class Driver -> Common Class Definitions

Last Edited: Tue. Mar 20, 2012 - 10:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Err yes, DTR rather.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, I'll give it a try.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dean,

Ok, it worked. Used CDC_CONTROL_LINE_OUT_DTR with EVENT_CDC_Device_ControLineStateChanged().

Thanks for the help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have something like this:
This was origionally from Chuck99's post: https://www.avrfreaks.net/index.p...

Then the added EVENT_CDC_Device_ControLineStateChanged() is from Deans link, But I changed it so there are callable functions from other parts in the code. Ie: I have a lot of print commands for debugging out of the CDC connection.
Now my print commands first check if there is actually a connection before trying it.
This is mentioned in the docs here: http://www.fourwalledcubicle.com...

What does not work here is that: when I open a serial connection to the device, and then disconnect again, My functions still say "connected", at least until i pull the plug (which is fine for me, for now)

[EDIT] Use Chucks (below) it works way better.

/** Event handler for the library USB Connection event. */
void EVENT_USB_Device_Connect(void)
{
	// No set connected here, becuase we only want when we are Actually connect to the device
}

/** Event handler for the library USB Disconnection event. */
void EVENT_USB_Device_Disconnect(void)
{
  setUSBnotconnected();
}

/** Event handler for the library USB Configuration Changed event. */
void EVENT_USB_Device_ConfigurationChanged(void)
{
  	bool ConfigSuccess = true;

	ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);

}

/** Event handler for the library USB Control Request reception event. */
void EVENT_USB_Device_ControlRequest(void)
{
	CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
}

void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *const 	CDCInterfaceInfo) {
	static bool PreviousDTRState = false;
	        bool        CurrentDTRState  = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);

	        /* Check if the DTR line has been asserted - if so, start the target AVR's reset pulse */
	        if (!(PreviousDTRState) && CurrentDTRState)
	        {
	        	setUSBconnected();
	        }

	        PreviousDTRState = CurrentDTRState;
}

uint8_t USBconnected(void) {return USB_connected;}
void setUSBconnected(void) {USB_connected = 1;}
void setUSBnotconnected(void) {USB_connected = 0;}
Last Edited: Thu. Nov 8, 2012 - 09:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

spirail wrote:
What does not work here is that: when I open a serial connection to the device, and then disconnect again, My functions still say "connected", at least until i pull the plug (which is fine for me, for now)

I modified the code in "Part 2" of the tutorial (Link) to turn the LED on when the host connects, and off when host disconnects.

void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)
{
        static bool PreviousDTRState = false;
        bool        CurrentDTRState  = (CDCInterfaceInfo->State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR);

        /* Check if the DTR line has been asserted */
        if (!(PreviousDTRState) && CurrentDTRState)
        {
			// PreviousDTRState == False AND CurrentDTRState == True
			// Host application has Connected to the COM port
			// Set LED On	
            LEDs_TurnOffLEDs(LEDS_LED1);			// this actually turns LED on
												    // (bug in LED driver for OLIMEX board - LUFA-120219)
        }
        else
        {
		  if (PreviousDTRState && !(CurrentDTRState) )
		  {
			 // PreviousDTRState == True AND CurrentDTRState == False
			 // Host application has Disconnected from the COM port
			 // Set LED Off	
             LEDs_TurnOnLEDs(LEDS_LED1);		// this actually turns the LED off
												// (bug in LED driver for OLIMEX board - LUFA-120219)
		  }		  	   	
		}			
        PreviousDTRState = CurrentDTRState;
}

I tested it using HyperTerminal and the LED switched on when it connected and off when disconnected.

I suggest you create a global variable HostIsConnected that you set to TRUE where I have set the LED on, and FALSE where I set the LED off.
Then your program checks the variable before sending output to the host.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Chuck99 wrote:

I modified the code in "Part 2" of the tutorial (Link) to turn the LED on when the host connects, and off when host disconnects.

Nice!. Yours works way better! Mine was quite buggy.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi! Does anybody know why two USB modems may stop working with the new LUFA library? May it be compiler problems or anything else?
The first modem is Novatel Wireless the other one is Huawei E160. Both modems fail to get enumerated.
Tried three different GCC compilers and all existing optimization methods.
They just won't enumerate and with the E160 - it says that its a wrong CDC device.

Any suggestions?

The micro is AT90USB

Update: on Huawei E160 it isn't able to configure the pipes. Wasn't yet able to see what is wrong with the other one, but must mention that both modems work fine on a windows machine.

Update: it is not able to find a compatible data exchange interface (no notification endpoint???) with USB_GetNextDescriptorComp()

Update: GetNextDescriptorComp() tried to find a control interface using the comparator function DComp_NextCDCControlInterface() which went through the descriptors' headers (11 pieces) and found three with:
Class 255 SubClass 255 Protocol 255 StrIndex 0
But the first contained the flag TotalEndPoints with the value "3" and the other two had that value set to "2"