summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2022-10-16 12:08:52 +0200
committerDamien George <damien@micropython.org>2022-11-09 11:15:24 +1100
commitca63ead2d8b242296d1fc2f6965e25dfdddeec15 (patch)
treeaaba488b92ff9cc36ded243d59288536be99750b
parent8447fef9f9d0db8104d0e600b79870c4aafa9725 (diff)
samd/mphalport: Add a timeout to mp_hal_stdout_tx_strn().
If USB CDC is connected and the board sends data, but the host does not receive the data, the device locks up. This is fixed in this commit by having a timeout of 500ms, after which time the transmission is skipped.
-rw-r--r--ports/samd/mphalport.c7
-rw-r--r--ports/samd/mphalport.h2
2 files changed, 8 insertions, 1 deletions
diff --git a/ports/samd/mphalport.c b/ports/samd/mphalport.c
index b60bada48..44d1470fc 100644
--- a/ports/samd/mphalport.c
+++ b/ports/samd/mphalport.c
@@ -194,9 +194,14 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE;
}
- while (n > tud_cdc_write_available()) {
+ int timeout = 0;
+ // Wait with a max of USC_CDC_TIMEOUT ms
+ while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
MICROPY_EVENT_POLL_HOOK
}
+ if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
+ break;
+ }
uint32_t n2 = tud_cdc_write(str + i, n);
tud_cdc_write_flush();
i += n2;
diff --git a/ports/samd/mphalport.h b/ports/samd/mphalport.h
index 2cbb3333f..4799c575c 100644
--- a/ports/samd/mphalport.h
+++ b/ports/samd/mphalport.h
@@ -35,6 +35,8 @@
#include "hpl_time_measure.h"
#include "sam.h"
+#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
+
extern int mp_interrupt_char;
extern volatile uint32_t systick_ms;
uint64_t mp_hal_ticks_us_64(void);