8000 stm32: Make uos.dupterm() conform to specs by using extmod version. · micropython/micropython-esp32@d6bf365 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit d6bf365

Browse files
committed
stm32: Make uos.dupterm() conform to specs by using extmod version.
The legacy function pyb.repl_uart() is still provided and retains its original behaviour (it only accepts a UART object). uos.dupterm() will now accept any object with write/readinto methods. At the moment there is just 1 dupterm slot.
1 parent 0eb333e commit d6bf365

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

ports/stm32/modpyb.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <stdint.h>
2828
#include <stdio.h>
2929

30-
#include "py/obj.h"
30+
#include "py/runtime.h"
3131
#include "py/gc.h"
3232
#include "py/builtin.h"
3333
#include "py/mphal.h"
@@ -104,6 +104,28 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);
104104

105105
MP_DECLARE_CONST_FUN_OBJ_KW(pyb_main_obj); // defined in main.c
106106

107+
// Get or set the UART object that the REPL is repeated on.
108+
// This is a legacy function, use of uos.dupterm is preferred.
109+
STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) {
110+
if (n_args == 0) {
111+
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
112+
return mp_const_none;
113+
} else {
114+
return MP_STATE_PORT(pyb_stdio_uart);
115+
}
116+
} else {
117+
if (args[0] == mp_const_none) {
118+
MP_STATE_PORT(pyb_stdio_uart) = NULL;
119+
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
120+
MP_STATE_PORT(pyb_stdio_uart) = args[0];
121+
} else {
122+
mp_raise_ValueError("need a UART object");
123+
}
124+
return mp_const_none;
125+
}
126+
}
127+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
128+
107129
STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
108130
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
109131

@@ -126,7 +148,7 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
126148
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_sleep_obj) },
127149
{ MP_ROM_QSTR(MP_QSTR_standby), MP_ROM_PTR(&machine_deepsleep_obj) },
128150
{ MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) },
129-
{ MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&mod_os_dupterm_obj) },
151+
{ MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) },
130152

131153
{ MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },
132154
{ MP_ROM_QSTR(MP_QSTR_hid_mouse), MP_ROM_PTR(&pyb_usb_hid_mouse_obj) },

ports/stm32/moduos.c

Lines changed: 2 additions & 23 deletions
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "lib/timeutils/timeutils.h"
3434
#include "lib/oofatfs/ff.h"
3535
#include "lib/oofatfs/diskio.h"
36+
#include "extmod/misc.h"
3637
#include "extmod/vfs.h"
3738
#include "extmod/vfs_fat.h"
3839
#include "genhdr/mpversion.h"
@@ -105,28 +106,6 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
105106
106107
#endif
107108

108-
// Get or set the UART object that the REPL is repeated on.
109-
// TODO should accept any object with read/write methods.
110-
STATIC mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args) {
111-
if (n_args == 0) {
112-
if (MP_STATE_PORT(pyb_stdio_uart) == NULL) {
113-
return mp_const_none;
114-
} else {
115-
return MP_STATE_PORT(pyb_stdio_uart);
116-
}
117-
} else {
118-
if (args[0] == mp_const_none) {
119-
MP_STATE_PORT(pyb_stdio_uart) = NULL;
120-
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
121-
MP_STATE_PORT(pyb_stdio_uart) = args[0];
122-
} else {
123-
mp_raise_ValueError("need a UART object");
124-
}
125-
return mp_const_none;
126-
}
127-
}
128-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_os_dupterm_obj, 0, 1, os_dupterm);
129-
130109
STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
131110
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uos) },
132111

@@ -154,7 +133,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
154133
#endif
155134

156135
// these are MicroPython extensions
157-
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mod_os_dupterm_obj) },
136+
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) },
158137
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&mp_vfs_mount_obj) },
159138
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&mp_vfs_umount_obj) },
160139
{ MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) },

ports/stm32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
#define MICROPY_PY_USELECT (1)
127127
#define MICROPY_PY_UTIMEQ (1)
128128
#define MICROPY_PY_UTIME_MP_HAL (1)
129+
#define MICROPY_PY_OS_DUPTERM (1)
129130
#define MICROPY_PY_MACHINE (1)
130131
#define MICROPY_PY_MACHINE_PULSE (1)
131132
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new

ports/stm32/mphalport.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "py/runtime.h"
44
#include "py/mperrno.h"
55
#include "py/mphal.h"
6+
#include "extmod/misc.h"
67
#include "usb.h"
78
#include "uart.h"
89

@@ -38,6 +39,10 @@ int mp_hal_stdin_rx_chr(void) {
3839
} else if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
3940
return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart));
4041
}
42+
int dupterm_c = mp_uos_dupterm_rx_chr();
43+
if (dupterm_c >= 0) {
44+
return dupterm_c;
45+
}
4146
MICROPY_EVENT_POLL_HOOK
4247
}
4348
}
@@ -56,6 +61,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
5661
if (usb_vcp_is_enabled()) {
5762
usb_vcp_send_strn(str, len);
5863
}
64+
mp_uos_dupterm_tx_strn(str, len);
5965
}
6066

6167
// Efficiently convert "\n" to "\r\n"

0 commit comments

Comments
 (0)
0