summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal
diff options
context:
space:
mode:
Diffstat (limited to 'stmhal')
-rw-r--r--stmhal/main.c8
-rw-r--r--stmhal/modpyb.c10
-rw-r--r--stmhal/printf.c18
-rw-r--r--stmhal/pybstdio.c44
-rw-r--r--stmhal/pybstdio.h5
-rw-r--r--stmhal/pyexec.c3
-rw-r--r--stmhal/readline.c3
-rw-r--r--stmhal/uart.c6
-rw-r--r--stmhal/uart.h3
-rw-r--r--stmhal/usb.c4
-rw-r--r--stmhal/usb.h1
11 files changed, 47 insertions, 58 deletions
diff --git a/stmhal/main.c b/stmhal/main.c
index ae4ff5141c..9e1798b3b9 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 8b594332b7..38a680da24 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 53c47c6338..db611f3d97 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 2a4386e097..9846ce0453 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 5beab893da..cdb7acfe59 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 93f4d62a96..cac6f0cf02 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 0703dcf4ea..14c9b9fb96 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 9436c2938b..0418e8f3d0 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 13ac92cbfa..7499473c16 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 50848f4555..85dd2b4eaf 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 3bd30ba92c..4eb29c9dee 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, ?