Skip to content

Commit

Permalink
samd/mphalport: Add a timeout to mp_hal_stdout_tx_strn().
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robert-hh authored and dpgeorge committed Nov 9, 2022
1 parent 8447fef commit ca63ead
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ports/samd/mphalport.c
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions ports/samd/mphalport.h
Expand Up @@ -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);
Expand Down

0 comments on commit ca63ead

Please sign in to comment.