diff --git a/stmhal/main.c b/stmhal/main.c index ae4ff5141c9a90b4e15ecf899d7cccc5668d5177..9e1798b3b9844ee7fe864b9d50bc426434c5b01a 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -43,7 +43,6 @@ #include "stackctrl.h" #include "gc.h" #include "gccollect.h" -#include "pybstdio.h" #include "readline.h" #include "pyexec.h" #include "i2c.h" @@ -64,6 +63,7 @@ #include "servo.h" #include "dac.h" #include "pybwlan.h" +#include "pybstdio.h" void SystemClock_Config(void); @@ -311,12 +311,10 @@ soft_reset: MP_OBJ_NEW_SMALL_INT(PYB_UART_6), MP_OBJ_NEW_SMALL_INT(115200), }; - pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, - MP_ARRAY_SIZE(args), - 0, args); + pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args); } #else - pyb_uart_global_debug = NULL; + pyb_stdio_uart = NULL; #endif // Micro Python init diff --git a/stmhal/modpyb.c b/stmhal/modpyb.c index 8b594332b725fb06fb9f397a2e4138eb3ae48e56..38a680da24ae416675afdc7197af95dabbe51f61 100644 --- a/stmhal/modpyb.c +++ b/stmhal/modpyb.c @@ -37,7 +37,6 @@ #include "gc.h" #include "gccollect.h" #include "systick.h" -#include "pybstdio.h" #include "pyexec.h" #include "led.h" #include "pin.h" @@ -57,6 +56,7 @@ #include "dac.h" #include "lcd.h" #include "usb.h" +#include "pybstdio.h" #include "ff.h" #include "portmodules.h" @@ -307,16 +307,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc); /// Get or set the UART object that the REPL is repeated on. STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) { if (n_args == 0) { - if (pyb_uart_global_debug == NULL) { + if (pyb_stdio_uart == NULL) { return mp_const_none; } else { - return pyb_uart_global_debug; + return pyb_stdio_uart; } } else { if (args[0] == mp_const_none) { - pyb_uart_global_debug = NULL; + pyb_stdio_uart = NULL; } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) { - pyb_uart_global_debug = args[0]; + pyb_stdio_uart = args[0]; } else { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object")); } diff --git a/stmhal/printf.c b/stmhal/printf.c index 53c47c63386fc0e610bc03c02faf8e2d80ae10bb..db611f3d9708103cde8ef9043057e18dd33260e8 100644 --- a/stmhal/printf.c +++ b/stmhal/printf.c @@ -40,6 +40,7 @@ #endif #include "uart.h" #include "usb.h" +#include "pybstdio.h" #if MICROPY_PY_BUILTINS_FLOAT #include "formatfloat.h" @@ -47,22 +48,11 @@ int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args); -void pfenv_prints(const pfenv_t *pfenv, const char *str) { - pfenv->print_strn(pfenv->data, str, strlen(str)); +STATIC void stdout_print_strn(void *dummy_env, const char *str, unsigned int len) { + stdout_tx_strn_cooked(str, len); } -STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) { - // TODO this needs to be replaced with a proper stdio interface ala CPython - // send stdout to UART and USB CDC VCP - if (pyb_uart_global_debug != PYB_UART_NONE) { - uart_tx_strn_cooked(pyb_uart_global_debug, str, len); - } - if (usb_vcp_is_enabled()) { - usb_vcp_send_strn_cooked(str, len); - } -} - -static const pfenv_t pfenv_stdout = {0, stdout_print_strn}; +STATIC const pfenv_t pfenv_stdout = {0, stdout_print_strn}; int printf(const char *fmt, ...) { va_list ap; diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c index 2a4386e097ec2b75faec0a4533bdf77a94ebb9dc..9846ce045316ae58a2ada7d94d4e8a5dc48df652 100644 --- a/stmhal/pybstdio.c +++ b/stmhal/pybstdio.c @@ -26,6 +26,7 @@ #include <stdio.h> #include <stdint.h> +#include <string.h> #include "mpconfig.h" #include "misc.h" @@ -34,31 +35,41 @@ #include "obj.h" #include "stream.h" #include MICROPY_HAL_H -#include "pybstdio.h" #include "usb.h" #include "uart.h" +#include "pybstdio.h" // TODO make stdin, stdout and stderr writable objects so they can -// be changed by Python code. +// be changed by Python code. This requires some changes, as these +// objects are in a read-only module (py/modsys.c). + +// stdio is repeated on this UART object if it's not null +pyb_uart_obj_t *pyb_stdio_uart = NULL; void stdout_tx_str(const char *str) { - if (pyb_uart_global_debug != PYB_UART_NONE) { - uart_tx_str(pyb_uart_global_debug, str); - } -#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD - lcd_print_str(str); -#endif - usb_vcp_send_str(str); + stdout_tx_strn(str, strlen(str)); } -void stdout_tx_strn(const char *str, uint len) { - if (pyb_uart_global_debug != PYB_UART_NONE) { - uart_tx_strn(pyb_uart_global_debug, str, len); +void stdout_tx_strn(const char *str, mp_uint_t len) { + if (pyb_stdio_uart != PYB_UART_NONE) { + uart_tx_strn(pyb_stdio_uart, str, len); } #if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD lcd_print_strn(str, len); #endif - usb_vcp_send_strn(str, len); + if (usb_vcp_is_enabled()) { + usb_vcp_send_strn(str, len); + } +} + +void stdout_tx_strn_cooked(const char *str, mp_uint_t len) { + // send stdout to UART and USB CDC VCP + if (pyb_stdio_uart != PYB_UART_NONE) { + uart_tx_strn_cooked(pyb_stdio_uart, str, len); + } + if (usb_vcp_is_enabled()) { + usb_vcp_send_strn_cooked(str, len); + } } int stdin_rx_chr(void) { @@ -74,14 +85,13 @@ int stdin_rx_chr(void) { #endif if (usb_vcp_rx_num() != 0) { return usb_vcp_rx_get(); - } else if (pyb_uart_global_debug != PYB_UART_NONE && uart_rx_any(pyb_uart_global_debug)) { - return uart_rx_char(pyb_uart_global_debug); + } else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) { + return uart_rx_char(pyb_stdio_uart); } __WFI(); } } - /******************************************************************************/ // Micro Python bindings @@ -120,7 +130,7 @@ STATIC mp_int_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *err STATIC mp_int_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) { pyb_stdio_obj_t *self = self_in; if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) { - stdout_tx_strn(buf, size); + stdout_tx_strn_cooked(buf, size); *errcode = 0; return size; } else { diff --git a/stmhal/pybstdio.h b/stmhal/pybstdio.h index 5beab893da85a40d45f5ae3159e044fc06e83015..cdb7acfe59387126074dc032f3364d93857757e7 100644 --- a/stmhal/pybstdio.h +++ b/stmhal/pybstdio.h @@ -24,6 +24,9 @@ * THE SOFTWARE. */ +extern pyb_uart_obj_t *pyb_stdio_uart; + void stdout_tx_str(const char *str); -void stdout_tx_strn(const char *str, uint len); +void stdout_tx_strn(const char *str, mp_uint_t len); +void stdout_tx_strn_cooked(const char *str, mp_uint_t len); int stdin_rx_chr(void); diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index 93f4d62a96d14977cad8749f58f1153810b72f82..cac6f0cf027ff7161f6ae561429b1f34c437becb 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -44,10 +44,11 @@ #include "gccollect.h" #include MICROPY_HAL_H #include "systick.h" -#include "pybstdio.h" #include "readline.h" #include "pyexec.h" #include "usb.h" +#include "uart.h" +#include "pybstdio.h" #include "genhdr/py-version.h" pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; diff --git a/stmhal/readline.c b/stmhal/readline.c index 0703dcf4eaa1c2d3af4065713c147a18894b1336..14c9b9fb96159b07ead80bd9982283add05e73e9 100644 --- a/stmhal/readline.c +++ b/stmhal/readline.c @@ -34,9 +34,10 @@ #include "misc.h" #include "obj.h" #include MICROPY_HAL_H -#include "pybstdio.h" #include "readline.h" #include "usb.h" +#include "uart.h" +#include "pybstdio.h" #if 0 // print debugging info #define DEBUG_PRINT (1) diff --git a/stmhal/uart.c b/stmhal/uart.c index 9436c2938b04d088ac10b01315f9d0dc928aa615..0418e8f3d013dc4b30f55b5f7f4558e1b924fd0c 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -65,8 +65,6 @@ struct _pyb_uart_obj_t { UART_HandleTypeDef uart; }; -pyb_uart_obj_t *pyb_uart_global_debug = NULL; - // assumes Init parameters have been set up correctly bool uart_init2(pyb_uart_obj_t *uart_obj) { USART_TypeDef *UARTx = NULL; @@ -219,10 +217,6 @@ void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) { HAL_UART_Transmit(&uart_obj->uart, &ch, 1, 100000); } -void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str) { - HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, strlen(str), 100000); -} - void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) { HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, len, 100000); } diff --git a/stmhal/uart.h b/stmhal/uart.h index 13ac92cbfa065b631daefc4f0059c45bd24be70a..7499473c16f0507d2f0014c3c7b6bc0658b49a4d 100644 --- a/stmhal/uart.h +++ b/stmhal/uart.h @@ -43,14 +43,11 @@ typedef enum { } pyb_uart_t; typedef struct _pyb_uart_obj_t pyb_uart_obj_t; - -extern pyb_uart_obj_t *pyb_uart_global_debug; extern const mp_obj_type_t pyb_uart_type; bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate); bool uart_rx_any(pyb_uart_obj_t *uart_obj); int uart_rx_char(pyb_uart_obj_t *uart_obj); -void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str); void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len); diff --git a/stmhal/usb.c b/stmhal/usb.c index 50848f45551ab24403cc41be4569f26e7ae345d9..85dd2b4eafc47d3c3b656b42b4471e96a7834ba6 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -107,10 +107,6 @@ char usb_vcp_rx_get(void) { return USBD_CDC_RxGet(); } -void usb_vcp_send_str(const char *str) { - usb_vcp_send_strn(str, strlen(str)); -} - void usb_vcp_send_strn(const char *str, int len) { #ifdef USE_DEVICE_MODE if (dev_is_enabled) { diff --git a/stmhal/usb.h b/stmhal/usb.h index 3bd30ba92c020bf9f76b792217bdd232e24e4653..4eb29c9deef69ce7f2317e9a5dfdea4d923f360b 100644 --- a/stmhal/usb.h +++ b/stmhal/usb.h @@ -48,7 +48,6 @@ bool usb_vcp_is_connected(void); void usb_vcp_set_interrupt_char(int c); int usb_vcp_rx_num(void); char usb_vcp_rx_get(void); -void usb_vcp_send_str(const char* str); void usb_vcp_send_strn(const char* str, int len); void usb_vcp_send_strn_cooked(const char *str, int len); void usb_hid_send_report(uint8_t *buf); // 4 bytes for mouse: ?, x, y, ?