Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 0eb333e

Browse files
committed
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.
1 parent 9725a65 commit 0eb333e

File tree

5 files changed

+15
-36
lines changed

5 files changed

+15
-36
lines changed

ports/stm32/mphalport.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,23 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
5858
}
5959
}
6060

61+
// Efficiently convert "\n" to "\r\n"
6162
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
62-
// send stdout to UART and USB CDC VCP
63-
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
64-
uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len);
63+
const char *last = str;
64+
while (len--) {
65+
if (*str == '\n') {
66+
if (str > last) {
67+
mp_hal_stdout_tx_strn(last, str - last);
68+
}
69+
mp_hal_stdout_tx_strn("\r\n", 2);
70+
++str;
71+
last = str;
72+
} else {
73+
++str;
74+
}
6575
}
66-
if (usb_vcp_is_enabled()) {
67-
usb_vcp_send_strn_cooked(str, len);
76+
if (str > last) {
77+
mp_hal_stdout_tx_strn(last, str - last);
6878
}
6979
}
7080

ports/stm32/uart.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -452,26 +452,11 @@ STATIC size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_
452452
return num_tx;
453453
}
454454

455-
STATIC void uart_tx_char(pyb_uart_obj_t *uart_obj, int c) {
456-
uint16_t ch = c;
457-
int errcode;
458-
uart_tx_data(uart_obj, &ch, 1, &errcode);
459-
}
460-
461455
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
462456
int errcode;
463457
uart_tx_data(uart_obj, str, len, &errcode);
464458
}
465459

466-
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len) {
467-
for (const char *top = str + len; str < top; str++) {
468-
if (*str == '\n') {
469-
uart_tx_char(uart_obj, '\r');
470-
}
471-
uart_tx_char(uart_obj, *str);
472-
}
473-
}
474-
475460
// this IRQ handler is set up to handle RXNE interrupts only
476461
void uart_irq_handler(mp_uint_t uart_id) {
477462
// get the uart object

ports/stm32/uart.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,5 @@ void uart_irq_handler(mp_uint_t uart_id);
4848
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj);
4949
int uart_rx_char(pyb_uart_obj_t *uart_obj);
5050
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
51-
void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len);
5251

5352
#endif // MICROPY_INCLUDED_STMHAL_UART_H

ports/stm32/usb.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,20 +177,6 @@ void usb_vcp_send_strn(const char *str, int len) {
177177
#endif
178178
}
179179

180-
void usb_vcp_send_strn_cooked(const char *str, int len) {
181-
#ifdef USE_DEVICE_MODE
182-
if (usb_device.enabled) {
183-
for (const char *top = str + len; str < top; str++) {
184-
if (*str == '\n') {
185-
usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)"\r\n", 2);
186-
} else {
187-
usbd_cdc_tx_always(&usb_device.usbd_cdc_itf, (const uint8_t*)str, 1);
188-
}
189-
}
190-
}
191-
#endif
192-
}
193-
194180
/******************************************************************************/
195181
// MicroPython bindings for USB
196182

ports/stm32/usb.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void pyb_usb_dev_deinit(void);
6363
bool usb_vcp_is_enabled(void);
6464
int usb_vcp_recv_byte(uint8_t *c); // if a byte is available, return 1 and put the byte in *c, else return 0
6565
void usb_vcp_send_strn(const char* str, int len);
66-
void usb_vcp_send_strn_cooked(const char *str, int len);
6766

6867
void pyb_usb_host_init(void);
6968
void pyb_usb_host_process(void);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy