summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-02-12 13:32:27 +1100
committerDamien George <damien@micropython.org>2021-02-12 13:48:51 +1100
commitf31c6b484060eb3554aa13bb758cc9a5974cafbb (patch)
tree04509c6051b6e836f1d4795938ee111937c62b41
parentc9260dda23bfdaea042d3bb94e1a830af0dbfa18 (diff)
downloadmicropython-f31c6b484060eb3554aa13bb758cc9a5974cafbb.tar.gz
micropython-f31c6b484060eb3554aa13bb758cc9a5974cafbb.zip
mimxrt: Fix USB CDC handling so it works reliably.
On i.MX the SysTick IRQ cannot wake the CPU from a WFI so the CPU was blocked on WFI waiting for USB data in mp_hal_stdin_rx_chr() even though it had already arrived (because it may arrive just after calling the check tud_cdc_available()). This commit fixes this problem by using SEV/WFE to indicate that there has been a USB event. The mp_hal_stdout_tx_strn() function is also fixed so that it doesn't overflow the USB buffers. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/mimxrt/board_init.c2
-rw-r--r--ports/mimxrt/mpconfigport.h2
-rw-r--r--ports/mimxrt/mphalport.c17
3 files changed, 11 insertions, 10 deletions
diff --git a/ports/mimxrt/board_init.c b/ports/mimxrt/board_init.c
index 102764cac1..cd7dc9a7dd 100644
--- a/ports/mimxrt/board_init.c
+++ b/ports/mimxrt/board_init.c
@@ -92,9 +92,11 @@ void SysTick_Handler(void) {
void USB_OTG1_IRQHandler(void) {
tud_int_handler(0);
tud_task();
+ __SEV();
}
void USB_OTG2_IRQHandler(void) {
tud_int_handler(1);
tud_task();
+ __SEV();
}
diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h
index 15a292486f..68c7a89422 100644
--- a/ports/mimxrt/mpconfigport.h
+++ b/ports/mimxrt/mpconfigport.h
@@ -94,7 +94,7 @@ extern const struct _mp_obj_module_t mp_module_utime;
do { \
extern void mp_handle_pending(bool); \
mp_handle_pending(true); \
- __WFI(); \
+ __WFE(); \
} while (0);
#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p) | 1))
diff --git a/ports/mimxrt/mphalport.c b/ports/mimxrt/mphalport.c
index d7642d6b6d..3032464d39 100644
--- a/ports/mimxrt/mphalport.c
+++ b/ports/mimxrt/mphalport.c
@@ -75,7 +75,7 @@ int mp_hal_stdin_rx_chr(void) {
return buf[0];
}
}
- __WFI();
+ MICROPY_EVENT_POLL_HOOK
}
}
@@ -83,17 +83,16 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (tud_cdc_connected()) {
for (size_t i = 0; i < len;) {
uint32_t n = len - i;
- uint32_t n2 = tud_cdc_write(str + i, n);
- if (n2 < n) {
- while (!tud_cdc_write_flush()) {
- __WFI();
- }
+ if (n > CFG_TUD_CDC_EP_BUFSIZE) {
+ n = CFG_TUD_CDC_EP_BUFSIZE;
}
+ while (n > tud_cdc_write_available()) {
+ __WFE();
+ }
+ uint32_t n2 = tud_cdc_write(str + i, n);
+ tud_cdc_write_flush();
i += n2;
}
- while (!tud_cdc_write_flush()) {
- __WFI();
- }
}
// TODO
// while (len--) {