aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-10-19 14:15:32 +1100
committerDamien George <damien.p.george@gmail.com>2017-10-19 14:15:32 +1100
commit0eb333e3cf2319cfb1bf558b8f1c6e3e513fb9d8 (patch)
tree7bb2507e474a17f7149bc79b8626fab3657e827a
parent9725a654bdfc4ec0b7d6bc6aa3f4365f0825c5f3 (diff)
stm32/mphalport: Improve efficiency of mp_hal_stdout_tx_strn_cooked.
Also simplifies the code by removing the specialised (and inefficient) cooked functions from UART and USB_VCP.
-rw-r--r--ports/stm32/mphalport.c20
-rw-r--r--ports/stm32/uart.c15
-rw-r--r--ports/stm32/uart.h1
-rw-r--r--ports/stm32/usb.c14
-rw-r--r--ports/stm32/usb.h1
5 files changed, 15 insertions, 36 deletions
diff --git a/ports/stm32/mphalport.c b/ports/stm32/mphalport.c
index ab3dc227a..3bea6e2d9 100644
--- a/ports/stm32/mphalport.c
+++ b/ports/stm32/mphalport.c
@@ -58,13 +58,23 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
}
}
+// Efficiently convert "\n" to "\r\n"
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
- // send stdout to UART and USB CDC VCP
- if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
- uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len);
+ const char *last = str;
+ while (len--) {
+ if (*str == '\n') {
+ if (str > last) {
+ mp_hal_stdout_tx_strn(last, str - last);
+ }
+ mp_hal_stdout_tx_strn("\r\n", 2);
+ ++str;
+ last = str;
+ } else {
+ ++str;
+ }
}
- if (usb_vcp_is_enabled()) {
- usb_vcp_send_strn_cooked(str, len);
+ if (str > last) {
+ mp_hal_stdout_tx_strn(last, str - last);
}
}
diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c
index 2b2f782f9..0b46d4f04 100644
--- a/ports/stm32/uart.c
+++ b/ports/stm32/uart.c
@@ -452,26 +452,11 @@ STATIC size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_
return num_tx;
}
-STATIC void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) {
- uint16_t ch = c;
- int errcode;
- uart_tx_data(uart_obj, &ch, 1, &errcode);
-}
-
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
int errcode;
uart_tx_data(uart_obj, str, len, &errcode);
}
-void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
- for (const char *top = str + len; str < top; str++) {
- if (*str == '\n') {
- uart_tx_char(uart_obj, '\r');
- }
- uart_tx_char(uart_obj, *str);
- }
-}
-
// this IRQ handler is set up to handle RXNE interrupts only
void uart_irq_handler(mp_uint_t uart_id) {
// get the uart object
diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h
index e96b25b5f..d176520a1 100644
--- a/ports/stm32/uart.h
+++ b/ports/stm32/uart.h
@@ -48,6 +48,5 @@ void uart_irq_handler(mp_uint_t uart_id);
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj);
int uart_rx_char(pyb_uart_obj_t *uart_obj);
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);
#endif // MICROPY_INCLUDED_STMHAL_UART_H
diff --git a/ports/stm32/usb.c b/ports/stm32/usb.c
index 8f89107ba..69f381d9b 100644
--- a/ports/stm32/usb.c
+++ b/ports/stm32/usb.c
@@ -177,20 +177,6 @@ void usb_vcp_send_strn(const char *str, int len) {
#endif
}
-void usb_vcp_send_strn_cooked(const char *str, int len) {
-#ifdef USE_DEVICE_MODE
- if (usb_device.enabled) {
- for (const char *top = str + len; str < top; str++) {
- if (*str == '\n') {
- usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)"\r\n", 2);
- } else {
- usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)str, 1);
- }
- }
- }
-#endif
-}
-
/******************************************************************************/
// MicroPython bindings for USB
diff --git a/ports/stm32/usb.h b/ports/stm32/usb.h
index f60ea8033..41c461fb2 100644
--- a/ports/stm32/usb.h
+++ b/ports/stm32/usb.h
@@ -63,7 +63,6 @@ void pyb_usb_dev_deinit(void);
bool usb_vcp_is_enabled(void);
int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0
void usb_vcp_send_strn(const char* str, int len);
-void usb_vcp_send_strn_cooked(const char *str, int len);
void pyb_usb_host_init(void);
void pyb_usb_host_process(void);