Switched CDC to using line status to determine endpoint presence. #378

Merged
merged 2 commits into from Aug 9, 2013

Conversation

Projects
None yet
2 participants
Member

majenkotech commented Aug 8, 2013

The current CDC driver uses a bit of a fudge to determine if the host has anything connected to it's end of the pipe. I.e., if the transmit buffers fill up and aren't emptied enough within 1 second it is assumed that the remote end isn't connected and the CDC is set to discard any future outgoing communications. Once something is received on the CDC connection that discard flag is then cleared and sending is allowed again. This has a number of drawbacks:

  1. Once you start sending data with nothing connected to the remote end and the buffers fill up there is a 1 second delay in your sketch while it times out.
  2. All subsequent communication is disabled until a character is sent by the remote end - not always possible or desirable depending on what is at the remote end doing the communicating.

The CDC protocol has a "line state" facility which informs the device whether or not something is connected at the hosts's end (setup packet of SET_CONTROL_LINE_STATE). Bit 0 of the value field transmitted with that packet indicates the presence of a host side communication program; 0 is not present, 1 is present.

This patch replaces the original method of detecting and timing out the remote connection (boolean gDiscard) and replaces it with a system which monitors this host present flag. If the host is indicated as present then outgoing communication is allowed. If it is not indicated as present then communication is disallowed at the earliest possible opportunity. This has the effect of:

  1. Buffers do not fill up with untransmittable data.
  2. There is no delay of 1 second while the transmitting routine waits for buffers to clear that will never clear.
  3. There is no requirement for the host to send a character to the device to restart communications after a timeout.
@@ -245,7 +245,6 @@ void cdcacm_print(const byte *buffer, int length)
delay(1);
if (m++ > 1000)
{
- gDiscard = true;
@majenkotech

majenkotech Aug 8, 2013

Member

This could feasibly be replaced with gConnected = false as a "belt and braces" fallback timeout, but I don't think it's needed.

Member

majenkotech commented Aug 8, 2013

Just committed an enhancement. There is now an int() operator to the USBSerial class which returns 1 if the USB is connected, and 0 if it's not (a simple "return gConnected ? 1 : 0"). This now operates in the same way as Arduino USB serial on boards like the Leonardo, where it is common to use

while (!Serial);

to wait for the USB to be opened by a host-side process.

Member

majenkotech commented Aug 8, 2013

Test sketch:

/*  Small sketch to test the USB serial opening routines
 *  Written to run on the Fubarino SD - tweak LED for others
 */

void setup() {
    Serial.begin(9600);

    // Start off with the LED turned off
    pinMode(21, OUTPUT);
    digitalWrite(21, LOW);  

    // Wait for someone to open the USB serial device
    while (!Serial);

    // Small delay to allow the remote end to be ready
    delay(100);

    // Say hello, and turn on the LED.
    Serial.println("Welcome");
    digitalWrite(21, HIGH);
}

void loop() {
}

ricklon added a commit that referenced this pull request Aug 9, 2013

Merge pull request #378 from majenkotech/master
Switched CDC to using line status to determine endpoint presence.

@ricklon ricklon merged commit 31e43c9 into chipKIT32:master Aug 9, 2013

ricklon added a commit that referenced this pull request Jan 5, 2014

Merge pull request #378 from majenkotech/master
Switched CDC to using line status to determine endpoint presence.

Former-commit-id: 31e43c9

gmrozek pushed a commit to gmrozek/chipKIT32-MAX that referenced this pull request Jan 5, 2014

Merge pull request #378 from majenkotech/master
Switched CDC to using line status to determine endpoint presence.

Former-commit-id: 31e43c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment