summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-06-23 12:56:04 +1000
committerDamien George <damien@micropython.org>2025-07-08 10:10:16 +1000
commit4951a06bbb5b3987a8ac922c06b8764c083d168c (patch)
treeefbc2a27cc67cab83770236bc8cb29fee5e2299d
parent6b82eb75bef70d44ef583385301b7c483ac9ae94 (diff)
zephyr: Enable sys.stdin/out/err.
This change enables `sys.stdin`, `sys.stdout` and `sys.stderr` objects. They are useful for general IO, and also help with testing zephyr boards. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/zephyr/CMakeLists.txt1
-rw-r--r--ports/zephyr/mpconfigport.h1
-rw-r--r--ports/zephyr/src/zephyr_getchar.c5
-rw-r--r--ports/zephyr/src/zephyr_getchar.h1
-rw-r--r--ports/zephyr/uart_core.c19
5 files changed, 27 insertions, 0 deletions
diff --git a/ports/zephyr/CMakeLists.txt b/ports/zephyr/CMakeLists.txt
index 0ae4b26ac..c15d68bab 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 e7e67b02d..b6f9176b6 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 94e35e2e8..7660e3cc1 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 fee899e1b..3f31c4317 100644
--- a/ports/zephyr/src/zephyr_getchar.h
+++ b/ports/zephyr/src/zephyr_getchar.h
@@ -17,4 +17,5 @@
#include <stdint.h>
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 ee525c33f..fe8a2a51d 100644
--- a/ports/zephyr/uart_core.c
+++ b/ports/zephyr/uart_core.c
@@ -26,6 +26,7 @@
#include <unistd.h>
#include "py/mpconfig.h"
#include "py/runtime.h"
+#include "py/stream.h"
#include "src/zephyr_getchar.h"
// Zephyr headers
#include <zephyr/kernel.h>
@@ -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 (;;) {