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.
This commit is contained in:
Damien George 2017-10-19 14:15:32 +11:00
parent 9725a654bd
commit 0eb333e3cf
5 changed files with 15 additions and 36 deletions

View File

@ -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);
}
if (usb_vcp_is_enabled()) {
usb_vcp_send_strn_cooked(str, len);
mp_hal_stdout_tx_strn("\r\n", 2);
++str;
last = str;
} else {
++str;
}
}
if (str > last) {
mp_hal_stdout_tx_strn(last, str - last);
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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);