Skip to content

Commit 7f9d1d6

Browse files
committed
py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
1 parent 56beb01 commit 7f9d1d6

91 files changed

Lines changed: 671 additions & 716 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bare-arm/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "py/compile.h"
77
#include "py/runtime.h"
88
#include "py/repl.h"
9-
#include "py/pfenv.h"
109

1110
void do_str(const char *src) {
1211
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
@@ -23,7 +22,7 @@ void do_str(const char *src) {
2322
nlr_pop();
2423
} else {
2524
// uncaught exception
26-
mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val);
25+
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
2726
}
2827
}
2928

cc3200/misc/pin_named_pins.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
#include "pybpin.h"
3838
#include MICROPY_HAL_H
3939

40-
STATIC void pin_named_pins_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
40+
STATIC void pin_named_pins_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
4141
pin_named_pins_obj_t *self = self_in;
42-
print(env, "<Pin.%s>", qstr_str(self->name));
42+
mp_printf(print, "<Pin.%s>", qstr_str(self->name));
4343
}
4444

4545
const mp_obj_type_t pin_cpu_pins_obj_type = {

cc3200/mods/modwlan.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -691,19 +691,19 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
691691
return &wlan_obj;
692692
}
693693

694-
STATIC void wlan_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
694+
STATIC void wlan_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
695695
wlan_obj_t *self = self_in;
696-
print(env, "<WLAN, mode=%u", self->mode);
696+
mp_printf(print, "<WLAN, mode=%u", self->mode);
697697

698698
// only print the bssid if in station mode
699699
if (self->mode != ROLE_AP && GET_STATUS_BIT(self->status, STATUS_BIT_CONNECTION)) {
700-
print(env, ", connected to: ssid=%s, bssid=%02x:%02x:%02x:%02x:%02x:%02x", self->ssid,
700+
mp_printf(print, ", connected to: ssid=%s, bssid=%02x:%02x:%02x:%02x:%02x:%02x", self->ssid,
701701
self->bssid[0], self->bssid[1], self->bssid[2], self->bssid[3], self->bssid[4], self->bssid[5]);
702702
}
703703
else {
704-
print(env, ", ssid=%s", self->ssid);
704+
mp_printf(print, ", ssid=%s", self->ssid);
705705
}
706-
print(env, ", security=%u>", self->security);
706+
mp_printf(print, ", security=%u>", self->security);
707707
}
708708

709709
/// \method connect(ssid, security=OPEN, key=None, bssid=None)

cc3200/mods/pybadc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
100100
/******************************************************************************/
101101
/* Micro Python bindings : adc object */
102102

103-
STATIC void adc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
103+
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
104104
pyb_adc_obj_t *self = self_in;
105-
print(env, "<ADC, channel=%u>", self->num);
105+
mp_printf(print, "<ADC, channel=%u>", self->num);
106106
}
107107

108108
/// \classmethod \constructor(channel)

cc3200/mods/pybi2c.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
294294
return (mp_obj_t)self;
295295
}
296296

297-
STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
297+
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
298298
pyb_i2c_obj_t *self = self_in;
299299
if (self->baudrate > 0) {
300-
print(env, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
300+
mp_printf(print, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
301301
}
302302
else {
303-
print(env, "<I2C0>");
303+
mp_print_str(print, "<I2C0>");
304304
}
305305
}
306306

cc3200/mods/pybpin.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,14 +429,14 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
429429

430430
/// \method print()
431431
/// Return a string describing the pin object.
432-
STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
432+
STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
433433
pin_obj_t *self = self_in;
434434
uint32_t af = MAP_PinModeGet(self->pin_num);
435435
uint32_t type = pin_get_type(self);
436436
uint32_t strength = pin_get_strenght(self);
437437

438438
// pin name
439-
print(env, "<Pin.cpu.%s, af=%u", qstr_str(self->name), af);
439+
mp_printf(print, "<Pin.cpu.%s, af=%u", qstr_str(self->name), af);
440440

441441
if (af == PIN_MODE_0) {
442442
// IO mode
@@ -447,7 +447,7 @@ STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env,
447447
} else {
448448
mode_qst = MP_QSTR_OUT;
449449
}
450-
print(env, ", mode=Pin.%s", qstr_str(mode_qst)); // safe because mode_qst has no formatting chars
450+
mp_printf(print, ", mode=Pin.%s", qstr_str(mode_qst));
451451
}
452452

453453
// pin type
@@ -465,7 +465,7 @@ STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env,
465465
} else {
466466
type_qst = MP_QSTR_OD_PD;
467467
}
468-
print(env, ", pull=Pin.%s", qstr_str(type_qst));
468+
mp_printf(print, ", pull=Pin.%s", qstr_str(type_qst));
469469

470470
// Strength
471471
qstr str_qst;
@@ -476,7 +476,7 @@ STATIC void pin_print(void (*print)(void *env, const char *fmt, ...), void *env,
476476
} else {
477477
str_qst = MP_QSTR_S6MA;
478478
}
479-
print(env, ", strength=Pin.%s>", qstr_str(str_qst));
479+
mp_printf(print, ", strength=Pin.%s>", qstr_str(str_qst));
480480
}
481481

482482
/// \classmethod \constructor(id, ...)

cc3200/mods/pybspi.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ STATIC void pybspi_transfer (pyb_spi_obj_t *self, const char *txdata, char *rxda
163163
/******************************************************************************/
164164
/* Micro Python bindings */
165165
/******************************************************************************/
166-
STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
166+
STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
167167
pyb_spi_obj_t *self = self_in;
168168

169169
if (self->baudrate > 0) {
170-
print(env, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
170+
mp_printf(print, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
171171
self->baudrate, self->config, self->submode, (self->wlen * 8));
172172
}
173173
else {
174-
print(env, "<SPI0>");
174+
mp_print_str(print, "<SPI0>");
175175
}
176176
}
177177

cc3200/mods/pybuart.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,37 +312,37 @@ STATIC void uart_callback_disable (mp_obj_t self_in) {
312312
/******************************************************************************/
313313
/* Micro Python bindings */
314314

315-
STATIC void pyb_uart_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
315+
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
316316
pyb_uart_obj_t *self = self_in;
317317
if (self->baudrate > 0) {
318-
print(env, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate);
318+
mp_printf(print, "<UART%u, baudrate=%u, bits=", self->uart_id, self->baudrate);
319319
switch (self->config & UART_CONFIG_WLEN_MASK) {
320320
case UART_CONFIG_WLEN_5:
321-
print(env, "5");
321+
mp_print_str(print, "5");
322322
break;
323323
case UART_CONFIG_WLEN_6:
324-
print(env, "6");
324+
mp_print_str(print, "6");
325325
break;
326326
case UART_CONFIG_WLEN_7:
327-
print(env, "7");
327+
mp_print_str(print, "7");
328328
break;
329329
case UART_CONFIG_WLEN_8:
330-
print(env, "8");
330+
mp_print_str(print, "8");
331331
break;
332332
default:
333333
break;
334334
}
335335
if ((self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_NONE) {
336-
print(env, ", parity=None");
336+
mp_print_str(print, ", parity=None");
337337
} else {
338-
print(env, ", parity=%u", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? 0 : 1);
338+
mp_printf(print, ", parity=%u", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? 0 : 1);
339339
}
340-
print(env, ", stop=%u, timeout=%u, timeout_char=%u, read_buf_len=%u>",
340+
mp_printf(print, ", stop=%u, timeout=%u, timeout_char=%u, read_buf_len=%u>",
341341
(self->config & UART_CONFIG_STOP_MASK) == UART_CONFIG_STOP_ONE ? 1 : 2,
342342
self->timeout, self->timeout_char, self->read_buf_len);
343343
}
344344
else {
345-
print(env, "<UART%u>", self->uart_id);
345+
mp_printf(print, "<UART%u>", self->uart_id);
346346
}
347347
}
348348

cc3200/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ typedef void *machine_ptr_t; // must be of pointer size
146146
typedef const void *machine_const_ptr_t; // must be of pointer size
147147
typedef long mp_off_t;
148148

149+
void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len);
150+
#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
151+
149152
#define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
150153
#define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state)
151154

esp8266/modesp.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "py/obj.h"
3333
#include "py/gc.h"
3434
#include "py/runtime.h"
35-
#include "py/pfenv.h"
3635
#include MICROPY_HAL_H
3736
#include "queue.h"
3837
#include "user_interface.h"
@@ -52,7 +51,7 @@ mp_obj_t call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
5251
if (nlr_push(&nlr) == 0) {
5352
return mp_call_function_1(fun, arg);
5453
} else {
55-
mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val);
54+
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
5655
return (mp_obj_t)nlr.ret_val;
5756
}
5857
}

0 commit comments

Comments
 (0)