From 614c1fdba2aca393e6769221cf8d8b55ee3464fe Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 25 Oct 2016 18:23:04 -0700 Subject: [PATCH] atmel-samd: Only output to USB after DTR and don't send anything larger than the room left in the USB TX buffer. --- atmel-samd/boards/arduino_zero/conf_usb.h | 6 ++++-- atmel-samd/mphalport.c | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/atmel-samd/boards/arduino_zero/conf_usb.h b/atmel-samd/boards/arduino_zero/conf_usb.h index d1ee1e8493..4702fe93b1 100644 --- a/atmel-samd/boards/arduino_zero/conf_usb.h +++ b/atmel-samd/boards/arduino_zero/conf_usb.h @@ -54,8 +54,10 @@ extern void mp_cdc_disable(uint8_t port); #define UDI_CDC_RX_NOTIFY(port) usb_rx_notify() void usb_rx_notify(void); #define UDI_CDC_SET_CODING_EXT(port,cfg) -#define UDI_CDC_SET_DTR_EXT(port,set) -#define UDI_CDC_SET_RTS_EXT(port,set) +#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set) +void usb_dtr_notify(uint8_t port, bool set); +#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set) +void usb_rts_notify(uint8_t port, bool set); /** * USB CDC low level configuration diff --git a/atmel-samd/mphalport.c b/atmel-samd/mphalport.c index 3b70b3d052..7102bf220f 100644 --- a/atmel-samd/mphalport.c +++ b/atmel-samd/mphalport.c @@ -50,7 +50,7 @@ void mp_msc_disable() bool mp_cdc_enable(uint8_t port) { - mp_cdc_enabled = true; + mp_cdc_enabled = false; return true; } @@ -59,6 +59,14 @@ void mp_cdc_disable(uint8_t port) mp_cdc_enabled = false; } +void usb_dtr_notify(uint8_t port, bool set) { + mp_cdc_enabled = set; +} + +void usb_rts_notify(uint8_t port, bool set) { + return; +} + void usb_rx_notify(void) { irqflags_t flags; @@ -177,7 +185,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { #endif #ifdef USB_REPL - if (mp_cdc_enabled && udi_cdc_is_tx_ready()) { + // Always make sure there is enough room in the usb buffer for the outgoing + // string. If there isn't we risk getting caught in a loop within the usb + // code as it tries to send all the characters it can't buffer. + if (mp_cdc_enabled && udi_cdc_get_free_tx_buffer() >= len) { udi_cdc_write_buf(str, len); } #endif