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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions shared-bindings/supervisor/Runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@
//|

//| serial_connected: bool
//| """Returns the USB serial communication status (read-only).
//|
//| .. note::
//|
//| SAMD: Will return ``True`` if the USB serial connection
//| has been established at any point. Will not reset if
//| USB is disconnected but power remains (e.g. battery connected)"""
//| """Returns the USB serial communication status (read-only)."""
//|

STATIC mp_obj_t supervisor_get_serial_connected(mp_obj_t self){
Expand Down
1 change: 1 addition & 0 deletions supervisor/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ char serial_read(void);
bool serial_bytes_available(void);
bool serial_connected(void);

extern volatile bool _serial_connected;
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H
4 changes: 3 additions & 1 deletion supervisor/shared/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ busio_uart_obj_t debug_uart;
byte buf_array[64];
#endif

volatile bool _serial_connected;

void serial_early_init(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
debug_uart.base.type = &busio_uart_type;
Expand All @@ -69,7 +71,7 @@ bool serial_connected(void) {
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
return true;
#else
return tud_cdc_connected();
return _serial_connected;
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions supervisor/shared/usb/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "shared-module/usb_midi/__init__.h"
#include "supervisor/background_callback.h"
#include "supervisor/port.h"
#include "supervisor/serial.h"
#include "supervisor/usb.h"
#include "lib/utils/interrupt_char.h"
#include "lib/mp-readline/readline.h"
Expand Down Expand Up @@ -115,6 +116,7 @@ void tud_umount_cb(void) {
// remote_wakeup_en : if host allows us to perform remote wakeup
// USB Specs: Within 7ms, device must draw an average current less than 2.5 mA from bus
void tud_suspend_cb(bool remote_wakeup_en) {
_serial_connected = false;
}

// Invoked when usb bus is resumed
Expand All @@ -126,6 +128,8 @@ void tud_resume_cb(void) {
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
(void) itf; // interface ID, not used

_serial_connected = dtr;

// DTR = false is counted as disconnected
if ( !dtr )
{
Expand Down