diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt index 0ae4b26ac6..c15d68babe 100644 --- a/ports/zephyr/CMakeLists.txt +++ b/ports/zephyr/CMakeLists.txt @@ -66,6 +66,7 @@ set(MICROPY_SOURCE_SHARED runtime/mpirq.c runtime/pyexec.c runtime/stdout_helpers.c + runtime/sys_stdio_mphal.c timeutils/timeutils.c ) list(TRANSFORM MICROPY_SOURCE_SHARED PREPEND ${MICROPY_DIR}/shared/) diff --git a/ports/zephyr/mpconfigport.h b/ports/zephyr/mpconfigport.h index e7e67b02d6..b6f9176b6a 100644 --- a/ports/zephyr/mpconfigport.h +++ b/ports/zephyr/mpconfigport.h @@ -96,6 +96,7 @@ #define MICROPY_PY_ZEPHYR (1) #define MICROPY_PY_ZSENSOR (1) #define MICROPY_PY_SYS_MODULES (0) +#define MICROPY_PY_SYS_STDFILES (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PY_BUILTINS_COMPLEX (0) diff --git a/ports/zephyr/src/zephyr_getchar.c b/ports/zephyr/src/zephyr_getchar.c index 94e35e2e84..7660e3cc1f 100644 --- a/ports/zephyr/src/zephyr_getchar.c +++ b/ports/zephyr/src/zephyr_getchar.c @@ -49,6 +49,11 @@ static int console_irq_input_hook(uint8_t ch) { return 1; } +// Returns true if a char is available for reading. +int zephyr_getchar_check(void) { + return i_get != i_put; +} + int zephyr_getchar(void) { mp_hal_wait_sem(&uart_sem, 0); if (k_sem_take(&uart_sem, K_MSEC(0)) == 0) { diff --git a/ports/zephyr/src/zephyr_getchar.h b/ports/zephyr/src/zephyr_getchar.h index fee899e1b4..3f31c4317f 100644 --- a/ports/zephyr/src/zephyr_getchar.h +++ b/ports/zephyr/src/zephyr_getchar.h @@ -17,4 +17,5 @@ #include void zephyr_getchar_init(void); +int zephyr_getchar_check(void); int zephyr_getchar(void); diff --git a/ports/zephyr/uart_core.c b/ports/zephyr/uart_core.c index ee525c33f7..fe8a2a51db 100644 --- a/ports/zephyr/uart_core.c +++ b/ports/zephyr/uart_core.c @@ -26,6 +26,7 @@ #include #include "py/mpconfig.h" #include "py/runtime.h" +#include "py/stream.h" #include "src/zephyr_getchar.h" // Zephyr headers #include @@ -52,6 +53,24 @@ static uint8_t mp_console_txbuf[CONFIG_CONSOLE_PUTCHAR_BUFSIZE]; * Core UART functions to implement for a port */ +uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) { + uintptr_t ret = 0; + if (poll_flags & MP_STREAM_POLL_RD) { + #ifdef CONFIG_CONSOLE_SUBSYS + // It's not easy to test if tty is readable, so just unconditionally set it for now. + ret |= MP_STREAM_POLL_RD; + #else + if (zephyr_getchar_check()) { + ret |= MP_STREAM_POLL_RD; + } + #endif + } + if (poll_flags & MP_STREAM_POLL_WR) { + ret |= MP_STREAM_POLL_WR; + } + return ret; +} + // Receive single character int mp_hal_stdin_rx_chr(void) { for (;;) {