Skip to content

Commit 749cbe1

Browse files
authored
Merge pull request #3437 from jepler/serial-connected-fix
supervisor: Improve serial connection detection
2 parents 7f60ebd + 28043c9 commit 749cbe1

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

shared-bindings/supervisor/Runtime.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@
4646
//|
4747

4848
//| serial_connected: bool
49-
//| """Returns the USB serial communication status (read-only).
50-
//|
51-
//| .. note::
52-
//|
53-
//| SAMD: Will return ``True`` if the USB serial connection
54-
//| has been established at any point. Will not reset if
55-
//| USB is disconnected but power remains (e.g. battery connected)"""
49+
//| """Returns the USB serial communication status (read-only)."""
5650
//|
5751

5852
STATIC mp_obj_t supervisor_get_serial_connected(mp_obj_t self){

supervisor/serial.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ char serial_read(void);
4747
bool serial_bytes_available(void);
4848
bool serial_connected(void);
4949

50+
extern volatile bool _serial_connected;
5051
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H

supervisor/shared/serial.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ busio_uart_obj_t debug_uart;
4747
byte buf_array[64];
4848
#endif
4949

50+
volatile bool _serial_connected;
51+
5052
void serial_early_init(void) {
5153
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
5254
debug_uart.base.type = &busio_uart_type;
@@ -69,7 +71,7 @@ bool serial_connected(void) {
6971
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
7072
return true;
7173
#else
72-
return tud_cdc_connected();
74+
return _serial_connected;
7375
#endif
7476
}
7577

supervisor/shared/usb/usb.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "shared-module/usb_midi/__init__.h"
3030
#include "supervisor/background_callback.h"
3131
#include "supervisor/port.h"
32+
#include "supervisor/serial.h"
3233
#include "supervisor/usb.h"
3334
#include "lib/utils/interrupt_char.h"
3435
#include "lib/mp-readline/readline.h"
@@ -115,6 +116,7 @@ void tud_umount_cb(void) {
115116
// remote_wakeup_en : if host allows us to perform remote wakeup
116117
// USB Specs: Within 7ms, device must draw an average current less than 2.5 mA from bus
117118
void tud_suspend_cb(bool remote_wakeup_en) {
119+
_serial_connected = false;
118120
}
119121

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

131+
_serial_connected = dtr;
132+
129133
// DTR = false is counted as disconnected
130134
if ( !dtr )
131135
{

0 commit comments

Comments
 (0)