Skip to content

supervisor: Improve serial connection detection #3437

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
Sep 18, 2020

Conversation

jepler
Copy link

@jepler jepler commented Sep 17, 2020

These changes remove the caveat from supervisor.runtime.serial_connected (that it always returns True if USB serial console was ever opened, even if USB is later unplugged).

It appears that _tud_cdc_connected() only tracks explicit changes to the "DTR" bit, which leads to disconnects not being registered.

Instead:

  • when line state is changed explicitly, track the dtr value in _serial_connected
  • when the USB bus is suspended, set _serial_connected to False

Testing performed (using sam e54 xplained): Run a program to show the state of serial_connected on the LED:

import digitalio
import supervisor
import board

led = digitalio.DigitalInOut(board.LED)
while True:
    led.switch_to_output(not supervisor.runtime.serial_connected)

Try all the following:

  • open, close serial terminal program
    • LED status tracks whether terminal is open
  • turn on/off data lines using the switchable charge-only cable
    • LED turns off when switch is in "charger" position
    • LED turns back on when switch is in Data position and terminal is
      opened (but doesn't turn back on just because switch position is
      changed)

These changes remove the caveat from supervisor.runtime.serial_connected.

It appears that _tud_cdc_connected() only tracks explicit changes to the
"DTR" bit, which leads to disconnects not being registered.

Instead:
 * when line state is changed explicitly, track the dtr value in
   _serial_connected
 * when the USB bus is suspended, set _serial_connected to False

Testing performed (using sam e54 xplained):  Run a program to show
the state of `serial_connected` on the LED:
```
import digitalio
import supervisor
import board

led = digitalio.DigitalInOut(board.LED)
while True:
    led.switch_to_output(not supervisor.runtime.serial_connected)
```

Try all the following:
 * open, close serial terminal program
    - LED status tracks whether terminal is open
 * turn on/off data lines using the switchable charge-only cable
    - LED turns off when switch is in "charger" position
    - LED turns back on when switch is in Data position and terminal is
      opened (but doesn't turn back on just because switch position is
      changed)
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Fantastic!

@dhalbert dhalbert merged commit 749cbe1 into adafruit:main Sep 18, 2020
@jepler jepler deleted the serial-connected-fix branch November 3, 2021 21:10
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.

2 participants