From 34fa81eaf3e7d020e743206047928f567e387ba6 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 26 Oct 2016 13:32:41 -0700 Subject: [PATCH] atmel-samd: Hook in DTR for more ports and make the serial TX more resiliant to long strings. --- .../boards/feather_m0_adalogger/conf_usb.h | 6 +++-- atmel-samd/boards/feather_m0_basic/conf_usb.h | 6 +++-- atmel-samd/boards/metro_m0_flash/conf_usb.h | 6 +++-- atmel-samd/mphalport.c | 22 +++++++++++++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/atmel-samd/boards/feather_m0_adalogger/conf_usb.h b/atmel-samd/boards/feather_m0_adalogger/conf_usb.h index fa832a3e27..d4367f90f8 100644 --- a/atmel-samd/boards/feather_m0_adalogger/conf_usb.h +++ b/atmel-samd/boards/feather_m0_adalogger/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/boards/feather_m0_basic/conf_usb.h b/atmel-samd/boards/feather_m0_basic/conf_usb.h index 235ce8335b..89fa1620a1 100644 --- a/atmel-samd/boards/feather_m0_basic/conf_usb.h +++ b/atmel-samd/boards/feather_m0_basic/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/boards/metro_m0_flash/conf_usb.h b/atmel-samd/boards/metro_m0_flash/conf_usb.h index 5ffa56f2a6..c54359562c 100644 --- a/atmel-samd/boards/metro_m0_flash/conf_usb.h +++ b/atmel-samd/boards/metro_m0_flash/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 c84640fd5f..2a3bfc1d68 100644 --- a/atmel-samd/mphalport.c +++ b/atmel-samd/mphalport.c @@ -9,6 +9,7 @@ #include "asf/sam0/drivers/sercom/usart/usart.h" #include "py/mphal.h" #include "py/mpstate.h" +#include "py/smallint.h" #include "mpconfigboard.h" #include "mphalport.h" @@ -226,8 +227,25 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { // 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); + uint32_t start = 0; + uint32_t start_tick = mp_hal_ticks_ms(); + uint32_t duration = 0; + if (mp_cdc_enabled) { + while (start < len && duration < 10) { + uint8_t buffer_space = udi_cdc_get_free_tx_buffer(); + uint8_t transmit = min(len - start, buffer_space); + if (transmit > 0) { + if (udi_cdc_write_buf(str + start, transmit) > 0) { + // It didn't transmit successfully so give up. + break; + } + } + start += transmit; + if (mp_msc_enabled) { + udi_msc_process_trans(); + } + duration = (mp_hal_ticks_ms() - start_tick) & MP_SMALL_INT_POSITIVE_MASK; + } } #endif }