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>
This commit is contained in:
Damien George
2025-06-23 12:56:04 +10:00
parent 6b82eb75be
commit 4951a06bbb
5 changed files with 27 additions and 0 deletions

View File

@@ -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/)

View File

@@ -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)

View File

@@ -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) {

View File

@@ -17,4 +17,5 @@
#include <stdint.h>
void zephyr_getchar_init(void);
int zephyr_getchar_check(void);
int zephyr_getchar(void);

View File

@@ -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 (;;) {