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.
This commit is contained in:
Damien George 2014-07-20 13:57:43 +01:00
parent 1163cb9cb5
commit 951ed9d02f
11 changed files with 47 additions and 58 deletions

View File

@ -43,7 +43,6 @@
#include "stackctrl.h" #include "stackctrl.h"
#include "gc.h" #include "gc.h"
#include "gccollect.h" #include "gccollect.h"
#include "pybstdio.h"
#include "readline.h" #include "readline.h"
#include "pyexec.h" #include "pyexec.h"
#include "i2c.h" #include "i2c.h"
@ -64,6 +63,7 @@
#include "servo.h" #include "servo.h"
#include "dac.h" #include "dac.h"
#include "pybwlan.h" #include "pybwlan.h"
#include "pybstdio.h"
void SystemClock_Config(void); void SystemClock_Config(void);
@ -311,12 +311,10 @@ soft_reset:
MP_OBJ_NEW_SMALL_INT(PYB_UART_6), MP_OBJ_NEW_SMALL_INT(PYB_UART_6),
MP_OBJ_NEW_SMALL_INT(115200), MP_OBJ_NEW_SMALL_INT(115200),
}; };
pyb_uart_global_debug = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, pyb_stdio_uart = pyb_uart_type.make_new((mp_obj_t)&pyb_uart_type, MP_ARRAY_SIZE(args), 0, args);
MP_ARRAY_SIZE(args),
0, args);
} }
#else #else
pyb_uart_global_debug = NULL; pyb_stdio_uart = NULL;
#endif #endif
// Micro Python init // Micro Python init

View File

@ -37,7 +37,6 @@
#include "gc.h" #include "gc.h"
#include "gccollect.h" #include "gccollect.h"
#include "systick.h" #include "systick.h"
#include "pybstdio.h"
#include "pyexec.h" #include "pyexec.h"
#include "led.h" #include "led.h"
#include "pin.h" #include "pin.h"
@ -57,6 +56,7 @@
#include "dac.h" #include "dac.h"
#include "lcd.h" #include "lcd.h"
#include "usb.h" #include "usb.h"
#include "pybstdio.h"
#include "ff.h" #include "ff.h"
#include "portmodules.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. /// 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) { STATIC mp_obj_t pyb_repl_uart(uint n_args, const mp_obj_t *args) {
if (n_args == 0) { if (n_args == 0) {
if (pyb_uart_global_debug == NULL) { if (pyb_stdio_uart == NULL) {
return mp_const_none; return mp_const_none;
} else { } else {
return pyb_uart_global_debug; return pyb_stdio_uart;
} }
} else { } else {
if (args[0] == mp_const_none) { 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) { } else if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
pyb_uart_global_debug = args[0]; pyb_stdio_uart = args[0];
} else { } else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "need a UART object"));
} }

View File

@ -40,6 +40,7 @@
#endif #endif
#include "uart.h" #include "uart.h"
#include "usb.h" #include "usb.h"
#include "pybstdio.h"
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
#include "formatfloat.h" #include "formatfloat.h"
@ -47,22 +48,11 @@
int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args); int pfenv_vprintf(const pfenv_t *pfenv, const char *fmt, va_list args);
void pfenv_prints(const pfenv_t *pfenv, const char *str) { STATIC void stdout_print_strn(void *dummy_env, const char *str, unsigned int len) {
pfenv->print_strn(pfenv->data, str, strlen(str)); stdout_tx_strn_cooked(str, len);
} }
STATIC void stdout_print_strn(void *data, const char *str, unsigned int len) { STATIC const pfenv_t pfenv_stdout = {0, stdout_print_strn};
// 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};
int printf(const char *fmt, ...) { int printf(const char *fmt, ...) {
va_list ap; va_list ap;

View File

@ -26,6 +26,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include "mpconfig.h" #include "mpconfig.h"
#include "misc.h" #include "misc.h"
@ -34,31 +35,41 @@
#include "obj.h" #include "obj.h"
#include "stream.h" #include "stream.h"
#include MICROPY_HAL_H #include MICROPY_HAL_H
#include "pybstdio.h"
#include "usb.h" #include "usb.h"
#include "uart.h" #include "uart.h"
#include "pybstdio.h"
// TODO make stdin, stdout and stderr writable objects so they can // 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) { void stdout_tx_str(const char *str) {
if (pyb_uart_global_debug != PYB_UART_NONE) { stdout_tx_strn(str, strlen(str));
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);
} }
void stdout_tx_strn(const char *str, uint len) { void stdout_tx_strn(const char *str, mp_uint_t len) {
if (pyb_uart_global_debug != PYB_UART_NONE) { if (pyb_stdio_uart != PYB_UART_NONE) {
uart_tx_strn(pyb_uart_global_debug, str, len); uart_tx_strn(pyb_stdio_uart, str, len);
} }
#if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD #if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
lcd_print_strn(str, len); lcd_print_strn(str, len);
#endif #endif
if (usb_vcp_is_enabled()) {
usb_vcp_send_strn(str, len); 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) { int stdin_rx_chr(void) {
@ -74,14 +85,13 @@ int stdin_rx_chr(void) {
#endif #endif
if (usb_vcp_rx_num() != 0) { if (usb_vcp_rx_num() != 0) {
return usb_vcp_rx_get(); return usb_vcp_rx_get();
} else if (pyb_uart_global_debug != PYB_UART_NONE && uart_rx_any(pyb_uart_global_debug)) { } else if (pyb_stdio_uart != PYB_UART_NONE && uart_rx_any(pyb_stdio_uart)) {
return uart_rx_char(pyb_uart_global_debug); return uart_rx_char(pyb_stdio_uart);
} }
__WFI(); __WFI();
} }
} }
/******************************************************************************/ /******************************************************************************/
// Micro Python bindings // 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) { 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; pyb_stdio_obj_t *self = self_in;
if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) { if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
stdout_tx_strn(buf, size); stdout_tx_strn_cooked(buf, size);
*errcode = 0; *errcode = 0;
return size; return size;
} else { } else {

View File

@ -24,6 +24,9 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
extern pyb_uart_obj_t *pyb_stdio_uart;
void stdout_tx_str(const char *str); 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); int stdin_rx_chr(void);

View File

@ -44,10 +44,11 @@
#include "gccollect.h" #include "gccollect.h"
#include MICROPY_HAL_H #include MICROPY_HAL_H
#include "systick.h" #include "systick.h"
#include "pybstdio.h"
#include "readline.h" #include "readline.h"
#include "pyexec.h" #include "pyexec.h"
#include "usb.h" #include "usb.h"
#include "uart.h"
#include "pybstdio.h"
#include "genhdr/py-version.h" #include "genhdr/py-version.h"
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL; pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;

View File

@ -34,9 +34,10 @@
#include "misc.h" #include "misc.h"
#include "obj.h" #include "obj.h"
#include MICROPY_HAL_H #include MICROPY_HAL_H
#include "pybstdio.h"
#include "readline.h" #include "readline.h"
#include "usb.h" #include "usb.h"
#include "uart.h"
#include "pybstdio.h"
#if 0 // print debugging info #if 0 // print debugging info
#define DEBUG_PRINT (1) #define DEBUG_PRINT (1)

View File

@ -65,8 +65,6 @@ struct _pyb_uart_obj_t {
UART_HandleTypeDef uart; UART_HandleTypeDef uart;
}; };
pyb_uart_obj_t *pyb_uart_global_debug = NULL;
// assumes Init parameters have been set up correctly // assumes Init parameters have been set up correctly
bool uart_init2(pyb_uart_obj_t *uart_obj) { bool uart_init2(pyb_uart_obj_t *uart_obj) {
USART_TypeDef *UARTx = NULL; 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); 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) { 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); HAL_UART_Transmit(&uart_obj->uart, (uint8_t*)str, len, 100000);
} }

View File

@ -43,14 +43,11 @@ typedef enum {
} pyb_uart_t; } pyb_uart_t;
typedef struct _pyb_uart_obj_t pyb_uart_obj_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; extern const mp_obj_type_t pyb_uart_type;
bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate); bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate);
bool uart_rx_any(pyb_uart_obj_t *uart_obj); bool uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(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(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); void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);

View File

@ -107,10 +107,6 @@ char usb_vcp_rx_get(void) {
return USBD_CDC_RxGet(); 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) { void usb_vcp_send_strn(const char *str, int len) {
#ifdef USE_DEVICE_MODE #ifdef USE_DEVICE_MODE
if (dev_is_enabled) { if (dev_is_enabled) {

View File

@ -48,7 +48,6 @@ bool usb_vcp_is_connected(void);
void usb_vcp_set_interrupt_char(int c); void usb_vcp_set_interrupt_char(int c);
int usb_vcp_rx_num(void); int usb_vcp_rx_num(void);
char usb_vcp_rx_get(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(const char* str, int len);
void usb_vcp_send_strn_cooked(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, ? void usb_hid_send_report(uint8_t *buf); // 4 bytes for mouse: ?, x, y, ?