Skip to content

Commit 951ed9d

Browse files
committed
stmhal: Fix REPL printing by cooking output sent to stdout_obj.
Recent changes to builtin print meant that print was printing to the mp_sys_stdout_obj, which was sending data raw to the USB CDC device. The data should be cooked so that \n turns into \r\n.
1 parent 1163cb9 commit 951ed9d

File tree

11 files changed

+47
-58
lines changed

11 files changed

+47
-58
lines changed

stmhal/main.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "stackctrl.h"
4444
#include "gc.h"
4545
#include "gccollect.h"
46-
#include "pybstdio.h"
4746
#include "readline.h"
4847
#include "pyexec.h"
4948
#include "i2c.h"
@@ -64,6 +63,7 @@
6463
#include "servo.h"
6564
#include "dac.h"
6665
#include "pybwlan.h"
66+
#include "pybstdio.h"
6767

6868
void SystemClock_Config(void);
6969

@@ -311,12 +311,10 @@ int main(void) {
311311
MP_OBJ_NEW_SMALL_INT(PYB_UART_6),
312312
MP_OBJ_NEW_SMALL_INT(115200),
313313
};
314-
pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type,
315-
MP_ARRAY_SIZE(args),
316-
0, args);
314+
pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
317315
}
318316
#else
319-
pyb_uart_global_debug = NULL;
317+
pyb_stdio_uart = NULL;
320318
#endif
321319

322320
// Micro Python init

stmhal/modpyb.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "gc.h"
3838
#include "gccollect.h"
3939
#include "systick.h"
40-
#include "pybstdio.h"
4140
#include "pyexec.h"
4241
#include "led.h"
4342
#include "pin.h"
@@ -57,6 +56,7 @@
5756
#include "dac.h"
5857
#include "lcd.h"
5958
#include "usb.h"
59+
#include "pybstdio.h"
6060
#include "ff.h"
6161
#include "portmodules.h"
6262

@@ -307,16 +307,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_have_cdc_obj, pyb_have_cdc);
307307
/// Get or set the UART object that the REPL is repeated on.
308308
STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
309309
if (n_args == 0) {
310-
if (pyb_uart_global_debug == NULL) {
310+
if (pyb_stdio_uart == NULL) {
311311
return mp_const_none;
312312
} else {
313-
return pyb_uart_global_debug;
313+
return pyb_stdio_uart;
314314
}
315315
} else {
316316
if (args[0] == mp_const_none) {
317-
pyb_uart_global_debug = NULL;
317+
pyb_stdio_uart = NULL;
318318
} else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
319-
pyb_uart_global_debug = args[0];
319+
pyb_stdio_uart = args[0];
320320
} else {
321321
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
322322
}

stmhal/printf.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,19 @@
4040
#endif
4141
#include "uart.h"
4242
#include "usb.h"
43+
#include "pybstdio.h"
4344

4445
#if MICROPY_PY_BUILTINS_FLOAT
4546
#include "formatfloat.h"
4647
#endif
4748

4849
int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args);
4950

50-
void pfenv_prints(const pfenv_t *pfenv, const char *str) {
51-
pfenv->print_strn(pfenv->data, str, strlen(str));
51+
STATIC void stdout_print_strn(void *dummy_env, const char *str, unsigned int len) {
52+
stdout_tx_strn_cooked(str, len);
5253
}
5354

54-
STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) {
55-
// TODO this needs to be replaced with a proper stdio interface ala CPython
56-
// send stdout to UART and USB CDC VCP
57-
if (pyb_uart_global_debug != PYB_UART_NONE) {
58-
uart_tx_strn_cooked(pyb_uart_global_debug, str, len);
59-
}
60-
if (usb_vcp_is_enabled()) {
61-
usb_vcp_send_strn_cooked(str, len);
62-
}
63-
}
64-
65-
static const pfenv_t pfenv_stdout = {0, stdout_print_strn};
55+
STATIC const pfenv_t pfenv_stdout = {0, stdout_print_strn};
6656

6757
int printf(const char *fmt, ...) {
6858
va_list ap;

stmhal/pybstdio.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <stdio.h>
2828
#include <stdint.h>
29+
#include <string.h>
2930

3031
#include "mpconfig.h"
3132
#include "misc.h"
@@ -34,31 +35,41 @@
3435
#include "obj.h"
3536
#include "stream.h"
3637
#include MICROPY_HAL_H
37-
#include "pybstdio.h"
3838
#include "usb.h"
3939
#include "uart.h"
40+
#include "pybstdio.h"
4041

4142
// TODO make stdin, stdout and stderr writable objects so they can
42-
// be changed by Python code.
43+
// be changed by Python code. This requires some changes, as these
44+
// objects are in a read-only module (py/modsys.c).
45+
46+
// stdio is repeated on this UART object if it's not null
47+
pyb_uart_obj_t *pyb_stdio_uart = NULL;
4348

4449
void stdout_tx_str(const char *str) {
45-
if (pyb_uart_global_debug != PYB_UART_NONE) {
46-
uart_tx_str(pyb_uart_global_debug, str);
47-
}
48-
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
49-
lcd_print_str(str);
50-
#endif
51-
usb_vcp_send_str(str);
50+
stdout_tx_strn(str, strlen(str));
5251
}
5352

54-
void stdout_tx_strn(const char *str, uint len) {
55-
if (pyb_uart_global_debug != PYB_UART_NONE) {
56-
uart_tx_strn(pyb_uart_global_debug, str, len);
53+
void stdout_tx_strn(const char *str, mp_uint_t len) {
54+
if (pyb_stdio_uart != PYB_UART_NONE) {
55+
uart_tx_strn(pyb_stdio_uart, str, len);
5756
}
5857
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
5958
lcd_print_strn(str, len);
6059
#endif
61-
usb_vcp_send_strn(str, len);
60+
if (usb_vcp_is_enabled()) {
61+
usb_vcp_send_strn(str, len);
62+
}
63+
}
64+
65+
void stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
66+
// send stdout to UART and USB CDC VCP
67+
if (pyb_stdio_uart != PYB_UART_NONE) {
68+
uart_tx_strn_cooked(pyb_stdio_uart, str, len);
69+
}
70+
if (usb_vcp_is_enabled()) {
71+
usb_vcp_send_strn_cooked(str, len);
72+
}
6273
}
6374

6475
int stdin_rx_chr(void) {
@@ -74,14 +85,13 @@ int stdin_rx_chr(void) {
7485
#endif
7586
if (usb_vcp_rx_num() != 0) {
7687
return usb_vcp_rx_get();
77-
} else if (pyb_uart_global_debug != PYB_UART_NONE && uart_rx_any(pyb_uart_global_debug)) {
78-
return uart_rx_char(pyb_uart_global_debug);
88+
} else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) {
89+
return uart_rx_char(pyb_stdio_uart);
7990
}
8091
__WFI();
8192
}
8293
}
8394

84-
8595
/******************************************************************************/
8696
// Micro Python bindings
8797

@@ -120,7 +130,7 @@ STATIC mp_int_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *err
120130
STATIC mp_int_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
121131
pyb_stdio_obj_t *self = self_in;
122132
if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
123-
stdout_tx_strn(buf, size);
133+
stdout_tx_strn_cooked(buf, size);
124134
*errcode = 0;
125135
return size;
126136
} else {

stmhal/pybstdio.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
extern pyb_uart_obj_t *pyb_stdio_uart;
28+
2729
void stdout_tx_str(const char *str);
28-
void stdout_tx_strn(const char *str, uint len);
30+
void stdout_tx_strn(const char *str, mp_uint_t len);
31+
void stdout_tx_strn_cooked(const char *str, mp_uint_t len);
2932
int stdin_rx_chr(void);

stmhal/pyexec.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@
4444
#include "gccollect.h"
4545
#include MICROPY_HAL_H
4646
#include "systick.h"
47-
#include "pybstdio.h"
4847
#include "readline.h"
4948
#include "pyexec.h"
5049
#include "usb.h"
50+
#include "uart.h"
51+
#include "pybstdio.h"
5152
#include "genhdr/py-version.h"
5253

5354
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;

stmhal/readline.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
#include "misc.h"
3535
#include "obj.h"
3636
#include MICROPY_HAL_H
37-
#include "pybstdio.h"
3837
#include "readline.h"
3938
#include "usb.h"
39+
#include "uart.h"
40+
#include "pybstdio.h"
4041

4142
#if 0 // print debugging info
4243
#define DEBUG_PRINT (1)

stmhal/uart.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ struct _pyb_uart_obj_t {
6565
UART_HandleTypeDef uart;
6666
};
6767

68-
pyb_uart_obj_t *pyb_uart_global_debug = NULL;
69-
7068
// assumes Init parameters have been set up correctly
7169
bool uart_init2(pyb_uart_obj_t *uart_obj) {
7270
USART_TypeDef *UARTx = NULL;
@@ -219,10 +217,6 @@ void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) {
219217
HAL_UART_Transmit(&uart_obj->uart, &ch, 1, 100000);
220218
}
221219

222-
void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str) {
223-
HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, strlen(str), 100000);
224-
}
225-
226220
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
227221
HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, len, 100000);
228222
}

stmhal/uart.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ typedef enum {
4343
} pyb_uart_t;
4444

4545
typedef struct _pyb_uart_obj_t pyb_uart_obj_t;
46-
47-
extern pyb_uart_obj_t *pyb_uart_global_debug;
4846
extern const mp_obj_type_t pyb_uart_type;
4947

5048
bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate);
5149
bool uart_rx_any(pyb_uart_obj_t *uart_obj);
5250
int uart_rx_char(pyb_uart_obj_t *uart_obj);
53-
void uart_tx_str(pyb_uart_obj_t *uart_obj, const char *str);
5451
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
5552
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
5653

stmhal/usb.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,6 @@ char usb_vcp_rx_get(void) {
107107
return USBD_CDC_RxGet();
108108
}
109109

110-
void usb_vcp_send_str(const char *str) {
111-
usb_vcp_send_strn(str, strlen(str));
112-
}
113-
114110
void usb_vcp_send_strn(const char *str, int len) {
115111
#ifdef USE_DEVICE_MODE
116112
if (dev_is_enabled) {

0 commit comments

Comments
 (0)