Skip to content

Commit 614c1fd

Browse files
committed
atmel-samd: Only output to USB after DTR and don't send anything larger than the room left in the USB TX buffer.
1 parent b7768a7 commit 614c1fd

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

atmel-samd/boards/arduino_zero/conf_usb.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ extern void mp_cdc_disable(uint8_t port);
5454
#define UDI_CDC_RX_NOTIFY(port) usb_rx_notify()
5555
void usb_rx_notify(void);
5656
#define UDI_CDC_SET_CODING_EXT(port,cfg)
57-
#define UDI_CDC_SET_DTR_EXT(port,set)
58-
#define UDI_CDC_SET_RTS_EXT(port,set)
57+
#define UDI_CDC_SET_DTR_EXT(port,set) usb_dtr_notify(port, set)
58+
void usb_dtr_notify(uint8_t port, bool set);
59+
#define UDI_CDC_SET_RTS_EXT(port,set) usb_rts_notify(port, set)
60+
void usb_rts_notify(uint8_t port, bool set);
5961

6062
/**
6163
* USB CDC low level configuration

atmel-samd/mphalport.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void mp_msc_disable()
5050

5151
bool mp_cdc_enable(uint8_t port)
5252
{
53-
mp_cdc_enabled = true;
53+
mp_cdc_enabled = false;
5454
return true;
5555
}
5656

@@ -59,6 +59,14 @@ void mp_cdc_disable(uint8_t port)
5959
mp_cdc_enabled = false;
6060
}
6161

62+
void usb_dtr_notify(uint8_t port, bool set) {
63+
mp_cdc_enabled = set;
64+
}
65+
66+
void usb_rts_notify(uint8_t port, bool set) {
67+
return;
68+
}
69+
6270
void usb_rx_notify(void)
6371
{
6472
irqflags_t flags;
@@ -177,7 +185,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
177185
#endif
178186

179187
#ifdef USB_REPL
180-
if (mp_cdc_enabled && udi_cdc_is_tx_ready()) {
188+
// Always make sure there is enough room in the usb buffer for the outgoing
189+
// string. If there isn't we risk getting caught in a loop within the usb
190+
// code as it tries to send all the characters it can't buffer.
191+
if (mp_cdc_enabled && udi_cdc_get_free_tx_buffer() >= len) {
181192
udi_cdc_write_buf(str, len);
182193
}
183194
#endif

0 commit comments

Comments
 (0)