Skip to content

improve USB CDC disconnect/reconnect checking #3624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2020

Conversation

dhalbert
Copy link
Collaborator

Fixes #3588.

Check for DTR by querying tud_cdc_get_line_state(), and check for tud_usb_ready(). One doesn't need to save state in callbacks this way.

Tested two ways:

  • By putting the host computer to sleep, and then waking it up. When sleeping, supervisor.runtime.serial_available becomes falase. After wakeup, the serial connection is restored, and supervisor.runtime.serial_available is True. Interrupting the running program with ctrl-c works properly, and one can get to the REPL.
  • Powering the board externally (AC adapter on a Metro M4), and unplugging and replugging the USB cable.

I did try the data/charge-only cable with a switch, but it gives peculiar results. It detects as disconnected and then reconnected when switched, I think because some resistors are added to the data lines to mark the cable as charge-only.

Copy link

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you -- this looks great, much better than what I had come up with! No testing performed.

@jepler jepler merged commit bbed1b0 into adafruit:6.0.x Oct 31, 2020
@dhalbert dhalbert deleted the usb-serial-detect-2 branch October 31, 2020 16:30
return _serial_connected;
// True if DTR is asserted, and the USB connection is up.
// tud_cdc_get_line_state(): bit 0 is DTR, bit 1 is RTS
return (tud_cdc_get_line_state() & 1) && tud_ready();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I'll use this in the alarm PR. I have to change stuff there anyway.

@siddacious
Copy link

@dhalbert Apologies if I missed it, but is there a similar ticket for mass storage? It's pretty much expected that when I start using my computer in the morning, if I had a board (Feather M4 for sure, Metro M4 likely, nRF possibly as well) plugged in and running the night before it will run and have CDC working (in the same terminal window but I won't have a CIRCUITPY drive until I reset the board.

@dhalbert
Copy link
Collaborator Author

dhalbert commented Nov 6, 2020

@siddacious I don't think there is a similar ticket. You can go ahead and open one. It might depend on the host OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants