Skip to content

Commit 2547928

Browse files
committed
stmhal: Add Python function to set UART for REPL.
This adds a hook to get/set pyb_uart_global_debug from Python, using pyb.repl_uart(). You can set it to an arbitrary UART object, and then the REPL (in and out) is repeated on this UART object (as well as on USB CDC). Ultimately, this will be replaced with a proper Pythonic interface to set sys.stdin and sys.stdout.
1 parent c0711cb commit 2547928

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

stmhal/modpyb.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929

3030
#include "stm32f4xx_hal.h"
3131

32-
#include "misc.h"
3332
#include "mpconfig.h"
33+
#include "misc.h"
34+
#include "nlr.h"
3435
#include "qstr.h"
3536
#include "obj.h"
3637
#include "gc.h"
@@ -302,6 +303,28 @@ STATIC mp_obj_t pyb_have_cdc(void ) {
302303
}
303304
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
304305

306+
/// \function repl_uart(uart)
307+
/// Get or set the UART object that the REPL is repeated on.
308+
STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
309+
if (n_args == 0) {
310+
if (pyb_uart_global_debug == NULL) {
311+
return mp_const_none;
312+
} else {
313+
return pyb_uart_global_debug;
314+
}
315+
} else {
316+
if (args[0] == mp_const_none) {
317+
pyb_uart_global_debug = NULL;
318+
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
319+
pyb_uart_global_debug = args[0];
320+
} else {
321+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
322+
}
323+
return mp_const_none;
324+
}
325+
}
326+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart);
327+
305328
/// \function hid((buttons, x, y, z))
306329
/// Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
307330
/// signal a HID mouse-motion event.
@@ -342,6 +365,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
342365
{ MP_OBJ_NEW_QSTR(MP_QSTR_usb_mode), (mp_obj_t)&pyb_usb_mode_obj },
343366

344367
{ MP_OBJ_NEW_QSTR(MP_QSTR_have_cdc), (mp_obj_t)&pyb_have_cdc_obj },
368+
{ MP_OBJ_NEW_QSTR(MP_QSTR_repl_uart), (mp_obj_t)&pyb_repl_uart_obj },
345369
{ MP_OBJ_NEW_QSTR(MP_QSTR_hid), (mp_obj_t)&pyb_hid_send_report_obj },
346370

347371
{ MP_OBJ_NEW_QSTR(MP_QSTR_millis), (mp_obj_t)&pyb_millis_obj },

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Q(readall)
5454
Q(readline)
5555
Q(write)
5656
Q(have_cdc)
57+
Q(repl_uart)
5758
Q(hid)
5859
Q(time)
5960
Q(rng)

0 commit comments

Comments
 (0)