Skip to content

Commit bbed1b0

Browse files
authored
Merge pull request #3624 from dhalbert/usb-serial-detect-2
improve USB CDC disconnect/reconnect checking
2 parents aba4447 + 345d84f commit bbed1b0

File tree

3 files changed

+3
-7
lines changed

3 files changed

+3
-7
lines changed

supervisor/serial.h

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

50-
extern volatile bool _serial_connected;
5150
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H

supervisor/shared/serial.c

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

50-
volatile bool _serial_connected;
51-
5250
void serial_early_init(void) {
5351
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
5452
debug_uart.base.type = &busio_uart_type;
@@ -71,7 +69,9 @@ bool serial_connected(void) {
7169
#if defined(DEBUG_UART_TX) && defined(DEBUG_UART_RX)
7270
return true;
7371
#else
74-
return _serial_connected;
72+
// True if DTR is asserted, and the USB connection is up.
73+
// tud_cdc_get_line_state(): bit 0 is DTR, bit 1 is RTS
74+
return (tud_cdc_get_line_state() & 1) && tud_ready();
7575
#endif
7676
}
7777

supervisor/shared/usb/usb.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ void tud_umount_cb(void) {
116116
// remote_wakeup_en : if host allows us to perform remote wakeup
117117
// USB Specs: Within 7ms, device must draw an average current less than 2.5 mA from bus
118118
void tud_suspend_cb(bool remote_wakeup_en) {
119-
_serial_connected = false;
120119
}
121120

122121
// Invoked when usb bus is resumed
@@ -128,8 +127,6 @@ void tud_resume_cb(void) {
128127
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
129128
(void) itf; // interface ID, not used
130129

131-
_serial_connected = dtr;
132-
133130
// DTR = false is counted as disconnected
134131
if ( !dtr )
135132
{

0 commit comments

Comments
 (0)