Skip to content

Commit 9a892a9

Browse files
committed
ports/*: Various changes to implement multiple-output logic.
This is an attempt to implement the following logic: "mp_hal_stdout_tx_strn() should attempt to write len bytes to all of the possible destinations for that data, and return the minimum successful write length" Implementing this is complicated by these facts: - multiple outputs may be enabled/disabled at compiled time - multiple outputs may be enabled/disabled at runtime - mp_os_dupterm_tx_strn() is one such output, optionally containing multiple additional outputs - each of these outputs may or may not be able to report success - each of these outputs may or may not be able to report partial writes The result is that there is no single approach that can be followed for various ports, so each instance of mp_hal_stdout_tx_strn() has its own logic. Additionally, cases where "mp_uint_t ret = len;" with len unchanged in the function, are changed to simply return len. Note that some instances of mp_hal_stdout_tx_strn() modify len while writing, others don't. Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
1 parent 56c35c1 commit 9a892a9

File tree

17 files changed

+90
-45
lines changed

17 files changed

+90
-45
lines changed

ports/cc3200/hal/cc3200_hal.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ void mp_hal_delay_ms(mp_uint_t delay) {
142142
}
143143

144144
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
145-
mp_uint_t ret = len;
146145
if (MP_STATE_PORT(os_term_dup_obj)) {
147146
if (mp_obj_is_type(MP_STATE_PORT(os_term_dup_obj)->stream_o, &pyb_uart_type)) {
148147
uart_tx_strn(MP_STATE_PORT(os_term_dup_obj)->stream_o, str, len);
@@ -153,7 +152,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
153152
}
154153
// and also to telnet
155154
telnet_tx_strn(str, len);
156-
return ret;
155+
return len;
157156
}
158157

159158
int mp_hal_stdin_rx_chr(void) {

ports/esp32/mphalport.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,32 @@ int mp_hal_stdin_rx_chr(void) {
120120
}
121121

122122
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
123-
mp_uint_t ret = len;
124123
// Only release the GIL if many characters are being sent
124+
mp_uint_t ret = len, did_write = 0;
125125
bool release_gil = len > 20;
126126
if (release_gil) {
127127
MP_THREAD_GIL_EXIT();
128128
}
129129
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
130130
usb_serial_jtag_tx_strn(str, len);
131+
did_write = 1;
131132
#elif CONFIG_USB_OTG_SUPPORTED
132133
usb_tx_strn(str, len);
134+
did_write = 1;
133135
#endif
134136
#if MICROPY_HW_ENABLE_UART_REPL
135137
uart_stdout_tx_strn(str, len);
138+
did_write = 1;
136139
#endif
137140
if (release_gil) {
138141
MP_THREAD_GIL_ENTER();
139142
}
140-
mp_os_dupterm_tx_strn(str, ret);
141-
return ret;
143+
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
144+
if (dupterm_res >= 0) {
145+
did_write = 1;
146+
ret = MIN((mp_uint_t)dupterm_res, ret);
147+
}
148+
return did_write?ret:0;
142149
}
143150

144151
uint32_t mp_hal_ticks_ms(void) {

ports/esp8266/esp_mphal.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ void mp_hal_debug_str(const char *str) {
9595
#endif
9696

9797
mp_uint_t mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
98-
mp_uint_t ret = len;
99-
mp_os_dupterm_tx_strn(str, ret);
100-
return ret;
98+
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
99+
if (dupterm_res < 0) {
100+
// no outputs, nothing was written
101+
return 0;
102+
} else {
103+
return dupterm_res;
104+
}
101105
}
102106

103107
void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {

ports/mimxrt/mphalport.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ int mp_hal_stdin_rx_chr(void) {
113113
}
114114

115115
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
116-
mp_uint_t ret = len;
116+
mp_uint_t ret = len, did_write = 0;
117117
if (tud_cdc_connected()) {
118-
for (size_t i = 0; i < len;) {
118+
size_t i = 0;
119+
while (i < len) {
119120
uint32_t n = len - i;
120121
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
121122
n = CFG_TUD_CDC_EP_BUFSIZE;
@@ -134,11 +135,17 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
134135
tud_cdc_write_flush();
135136
i += n2;
136137
}
138+
did_write = 1;
139+
ret = MIN(i, ret);
137140
}
138141
#if MICROPY_PY_OS_DUPTERM
139-
mp_os_dupterm_tx_strn(str, ret);
142+
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
143+
if (dupterm_res >= 0) {
144+
did_write = 1;
145+
ret = MIN((mp_uint_t)dupterm_res, ret);
146+
}
140147
#endif
141-
return ret;
148+
return did_write?ret:0;
142149
}
143150

144151
uint64_t mp_hal_time_ns(void) {

ports/minimal/uart_core.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
3434
#if MICROPY_MIN_USE_STDOUT
3535
int r = write(STDOUT_FILENO, str, len);
3636
if (r >= 0) {
37-
// in case of an error in the syscall, pretend write succeeded
38-
ret = r;
37+
// in case of an error in the syscall, report no bytes written
38+
ret = 0;
3939
}
4040
#elif MICROPY_MIN_USE_STM32_MCU
4141
while (len--) {

ports/nrf/drivers/bluetooth/ble_uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ int mp_hal_stdin_rx_chr(void) {
111111
}
112112

113113
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
114-
mp_uint_t ret = len;
115114
// Not connected: drop output
116-
if (!ble_uart_enabled()) return ret;
115+
if (!ble_uart_enabled()) return 0;
117116

117+
mp_uint_t ret = len;
118118
uint8_t *buf = (uint8_t *)str;
119119
size_t send_len;
120120

ports/nrf/drivers/usb/usb_cdc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,11 @@ int mp_hal_stdin_rx_chr(void) {
232232
}
233233

234234
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
235-
mp_uint_t ret = len;
236235
for (const char *top = str + len; str < top; str++) {
237236
ringbuf_put((ringbuf_t*)&tx_ringbuf, *str);
238237
usb_cdc_loop();
239238
}
240-
return ret;
239+
return len;
241240
}
242241

243242
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {

ports/nrf/mphalport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@ int mp_hal_stdin_rx_chr(void) {
197197
}
198198

199199
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
200-
mp_uint_t ret = len;
201200
if (MP_STATE_PORT(board_stdio_uart) != NULL) {
202201
uart_tx_strn(MP_STATE_PORT(board_stdio_uart), str, len);
202+
return len;
203203
}
204-
return ret;
204+
return 0;
205205
}
206206

207207
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {

ports/powerpc/uart_lpc_serial.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,12 @@ int mp_hal_stdin_rx_chr(void) {
109109

110110

111111
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
112-
mp_uint_t ret = len;
113112
int i;
114113
for (i = 0; i < len; i++) {
115114
while (lpc_uart_tx_full()) {
116115
;
117116
}
118117
lpc_uart_reg_write(REG_RBR, str[i]);
119118
}
120-
return ret;
119+
return len;
121120
}

ports/powerpc/uart_potato.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ int mp_hal_stdin_rx_chr(void) {
119119
}
120120

121121
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
122-
mp_uint_t ret = len;
123122
int i;
124123

125124
for (i = 0; i < len; i++) {
@@ -130,5 +129,5 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
130129
}
131130
potato_uart_reg_write(POTATO_CONSOLE_TX, val);
132131
}
133-
return ret;
132+
return len;
134133
}

0 commit comments

Comments
 (0)