8000 sys.stdio: add support for ioctl and make sys.stdin.buffer.read non-blocking by dpgeorge · Pull Request #4859 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

sys.stdio: add support for ioctl and make sys.stdin.buffer.read non-blocking #4859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extmod/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj);

#if MICROPY_PY_OS_DUPTERM
bool mp_uos_dupterm_is_builtin_stream(mp_const_obj_t stream);
uintptr_t mp_uos_dupterm_poll(uintptr_t poll_flags);
int mp_uos_dupterm_rx_chr(void);
void mp_uos_dupterm_tx_strn(const char *str, size_t len);
void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc);
Expand Down
39 changes: 39 additions & 0 deletions extmod/uos_dupterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
}
}

uintptr_t mp_uos_dupterm_poll(uintptr_t poll_flags) {
uintptr_t poll_flags_out = 0;

for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
mp_obj_t s = MP_STATE_VM(dupterm_objs[idx]);
if (s == MP_OBJ_NULL) {
continue;
}

int errcode = 0;
mp_uint_t ret = 0;
const mp_stream_p_t *stream_p = mp_get_stream(s);
#if MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM
if (mp_uos_dupterm_is_builtin_stream(s)) {
ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode);
} else
#endif
{
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
ret = stream_p->ioctl(s, MP_STREAM_POLL, poll_flags, &errcode);
nlr_pop();
} else {
// Ignore error with ioctl
}
}

if (ret != MP_STREAM_ERROR) {
poll_flags_out |= ret;
if (poll_flags_out == poll_flags) {
// Finish early if all requested flags are set
break;
}
}
}

return poll_flags_out;
}

int mp_uos_dupterm_rx_chr(void) {
for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/sys_stdio_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size,
}
}

STATIC mp_uint_t stdio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
(void)self_in;
if (request == MP_STREAM_POLL) {
return mp_hal_stdio_poll(arg);
} else {
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
}
}

STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
return mp_const_none;
}
Expand Down Expand Up @@ -112,6 +122,7 @@ STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
STATIC const mp_stream_p_t stdio_obj_stream_p = {
.read = stdio_read,
.write = stdio_write,
.ioctl = stdio_ioctl,
.is_text = true,
};

Expand Down Expand Up @@ -146,6 +157,7 @@ STATIC mp_uint_t stdio_buffer_write(mp_obj_t self_in, const void *buf, mp_uint_t
STATIC const mp_stream_p_t stdio_buffer_obj_stream_p = {
.read = stdio_buffer_read,
.write = stdio_buffer_write,
.ioctl = stdio_ioctl,
.is_text = false,
};

Expand Down
1 change: 1 addition & 0 deletions ports/cc3200/hal/cc3200_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ extern void HAL_SystemDeInit (void);
extern void HAL_IncrementTick(void);
extern void mp_hal_set_interrupt_char (int c);

#define mp_hal_stdio_poll(poll_flags) (0) // not implemented
#define mp_hal_delay_us(usec) UtilsDelay(UTILS_DELAY_US_TO_COUNT(usec))
#define mp_hal_ticks_cpu() (SysTickPeriodGet() - SysTickValueGet())
9 changes: 9 additions & 0 deletions ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "rom/uart.h"

#include "py/obj.h"
#include "py/stream.h"
#include "py/mpstate.h"
#include "py/mphal.h"
#include "extmod/misc.h"
Expand All @@ -45,6 +46,14 @@ TaskHandle_t mp_main_task_handle;
STATIC uint8_t stdin_ringbuf_array[256];
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array)};

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
}

int mp_hal_stdin_rx_chr(void) {
for (;;) {
int c = ringbuf_get(&stdin_ringbuf);
Expand Down
9 changes: 9 additions & 0 deletions ports/esp8266/esp_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "user_interface.h"
#include "ets_alt_task.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "extmod/misc.h"
#include "lib/utils/pyexec.h"

Expand All @@ -56,6 +57,14 @@ void mp_hal_delay_us(uint32_t us) {
}
}

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
}

int mp_hal_stdin_rx_chr(void) {
for (;;) {
int c = ringbuf_get(&stdin_ringbuf);
Expand Down
9 changes: 9 additions & 0 deletions ports/pic16bit/pic16bit_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include <string.h>
#include "py/stream.h"
#include "py/mphal.h"
#include "board.h"

Expand All @@ -51,6 +52,14 @@ void mp_hal_set_interrupt_char(int c) {
interrupt_char = c;
}

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && uart_rx_any()) {
ret |= MP_STREAM_POLL_RD;
}
return ret;
}

int mp_hal_stdin_rx_chr(void) {
for (;;) {
if (uart_rx_any()) {
Expand Down
11 changes: 11 additions & 0 deletions ports/stm32/mphalport.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string.h>

#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "extmod/misc.h"
Expand All @@ -19,6 +20,16 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
mp_raise_OSError(mp_hal_status_to_errno_table[status]);
}

MP_WEAK uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
int errcode;
const mp_stream_p_t *stream_p = mp_get_stream(MP_STATE_PORT(pyb_stdio_uart));
ret = stream_p->ioctl(MP_STATE_PORT(pyb_stdio_uart), MP_STREAM_POLL, poll_flags, &errcode);
}
return ret | mp_uos_dupterm_poll(poll_flags);
}

MP_WEAK int mp_hal_stdin_rx_chr(void) {
for (;;) {
#if 0
Expand Down
14 changes: 14 additions & 0 deletions ports/teensy/teensy_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>

#include "py/runtime.h"
#include "py/stream.h"
#include "py/mphal.h"
#include "usb.h"
#include "uart.h"
Expand All @@ -21,6 +22,19 @@ void mp_hal_set_interrupt_char(int c) {
// you can't press Control-C and get your python script to stop.
}

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if (poll_flags & MP_STREAM_POLL_RD) {
if (usb_vcp_rx_num()) {
ret |= MP_STREAM_POLL_RD;
}
if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
ret |= MP_STREAM_POLL_RD;
}
}
return ret;
}

int mp_hal_stdin_rx_chr(void) {
for (;;) {
byte c;
Expand Down
4 changes: 4 additions & 0 deletions py/mphal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <mphalport.h>
#endif

#ifndef mp_hal_stdio_poll
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags);
#endif

#ifndef mp_hal_stdin_rx_chr
int mp_hal_stdin_rx_chr(void);
#endif
Expand Down
0