2727#include "py/mpconfig.h"
2828#include "py/runtime.h"
2929#include "py/stream.h"
30+ #include "extmod/misc.h"
3031#include "src/zephyr_getchar.h"
3132// Zephyr headers
3233#include <zephyr/kernel.h>
@@ -68,13 +69,26 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
6869 if (poll_flags & MP_STREAM_POLL_WR ) {
6970 ret |= MP_STREAM_POLL_WR ;
7071 }
72+
73+ #if MICROPY_PY_OS_DUPTERM
74+ ret |= mp_os_dupterm_poll (poll_flags );
75+ #endif
76+
7177 return ret ;
7278}
7379
7480// Receive single character
7581int mp_hal_stdin_rx_chr (void ) {
7682 for (;;) {
7783 int _chr ;
84+
85+ #if MICROPY_PY_OS_DUPTERM
86+ _chr = mp_os_dupterm_rx_chr ();
87+ if (_chr >= 0 ) {
88+ return _chr ;
89+ }
90+ #endif
91+
7892 #ifdef CONFIG_CONSOLE_SUBSYS
7993 _chr = mp_console_getchar ();
8094 #else
@@ -90,9 +104,11 @@ int mp_hal_stdin_rx_chr(void) {
90104// Send string of given length
91105mp_uint_t mp_hal_stdout_tx_strn (const char * str , mp_uint_t len ) {
92106 mp_uint_t ret = len ;
107+ mp_uint_t console_len = len ;
108+ const char * console_str = str ;
93109 #ifdef CONFIG_CONSOLE_SUBSYS
94- while (len -- ) {
95- char c = * str ++ ;
110+ while (console_len -- ) {
111+ char c = * console_str ++ ;
96112 while (mp_console_putchar (c ) == -1 ) {
97113 MICROPY_EVENT_POLL_HOOK
98114 }
@@ -101,10 +117,18 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
101117 static const struct device * uart_console_dev =
102118 DEVICE_DT_GET (DT_CHOSEN (zephyr_console ));
103119
104- while (len -- ) {
105- uart_poll_out (uart_console_dev , * str ++ );
120+ while (console_len -- ) {
121+ uart_poll_out (uart_console_dev , * console_str ++ );
122+ }
123+ #endif
124+
125+ #if MICROPY_PY_OS_DUPTERM
126+ int dupterm_res = mp_os_dupterm_tx_strn (str , len );
127+ if (dupterm_res >= 0 ) {
128+ ret = MIN ((mp_uint_t )dupterm_res , ret );
106129 }
107130 #endif
131+
108132 return ret ;
109133}
110134
0 commit comments