2626
2727#include <stdio.h>
2828#include <stdint.h>
29+ #include <string.h>
2930
3031#include "mpconfig.h"
3132#include "misc.h"
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
4449void 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
6475int 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
120130STATIC 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 {
0 commit comments