mimxrt/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.
This commit is contained in:
robert-hh 2022-10-16 12:28:05 +02:00 committed by Damien George
parent ca63ead2d8
commit 5d473d3093
2 changed files with 10 additions and 2 deletions

View File

@ -116,9 +116,15 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (n > CFG_TUD_CDC_EP_BUFSIZE) { if (n > CFG_TUD_CDC_EP_BUFSIZE) {
n = CFG_TUD_CDC_EP_BUFSIZE; n = CFG_TUD_CDC_EP_BUFSIZE;
} }
while (n > tud_cdc_write_available()) { uint64_t timeout = ticks_us64() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT * 1000);
__WFE(); // Wait with a max of USC_CDC_TIMEOUT ms
while (n > tud_cdc_write_available() && ticks_us64() < timeout) {
MICROPY_EVENT_POLL_HOOK
} }
if (ticks_us64() >= timeout) {
break;
}
uint32_t n2 = tud_cdc_write(str + i, n); uint32_t n2 = tud_cdc_write(str + i, n);
tud_cdc_write_flush(); tud_cdc_write_flush();
i += n2; i += n2;

View File

@ -35,6 +35,8 @@
#define MICROPY_HAL_VERSION "2.8.0" #define MICROPY_HAL_VERSION "2.8.0"
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
#define MP_HAL_PIN_FMT "%q" #define MP_HAL_PIN_FMT "%q"
extern ringbuf_t stdin_ringbuf; extern ringbuf_t stdin_ringbuf;