From 0ab372585f57c80202a432bd757d52088de80850 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 20 May 2016 22:20:37 +0300 Subject: extmod/moduos_dupterm: Dumpterm subsystem is responsible for closing stream. Make dupterm subsystem close a term stream object when EOF or error occurs. There's no other party than dupterm itself in a better position to do this, and this is required to properly reclaim stream resources, especially if multiple dupterm sessions may be established (e.g. as networking connections). --- esp8266/esp_mphal.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'esp8266/esp_mphal.c') diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index 25f1a9322f..ec5da7d3d0 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -171,16 +171,13 @@ static int call_dupterm_read(void) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); if (bufinfo.len == 0) { - MP_STATE_PORT(term_obj) = NULL; - mp_printf(&mp_plat_print, "dupterm: EOF received, deactivating\n"); + mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); return -1; } nlr_pop(); return *(byte*)bufinfo.buf; } else { - MP_STATE_PORT(term_obj) = NULL; - mp_printf(&mp_plat_print, "dupterm: Exception in read() method, deactivating: "); - mp_obj_print_exception(&mp_plat_print, nlr.ret_val); + mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val); } return -1; -- cgit v1.2.3 From 116eeee6dbdffc52a34c3e18e001597e2bfb7bcd Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 24 May 2016 01:30:28 +0300 Subject: esp8266/esp_mphal: Fix NLR buffer leak in call_dupterm_read(). --- esp8266/esp_mphal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'esp8266/esp_mphal.c') diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index ec5da7d3d0..eabf8ca208 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -166,15 +166,16 @@ static int call_dupterm_read(void) { read_m[2] = MP_OBJ_NEW_SMALL_INT(1); mp_obj_t res = mp_call_method_n_kw(1, 0, read_m); if (res == mp_const_none) { + nlr_pop(); return -2; } mp_buffer_info_t bufinfo; mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); + nlr_pop(); if (bufinfo.len == 0) { mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); return -1; } - nlr_pop(); return *(byte*)bufinfo.buf; } else { mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val); -- cgit v1.2.3 From 4681b86850076c013be2477d3850fd536361b62e Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 24 May 2016 01:37:56 +0300 Subject: esp8266/esp_mphal: Handle Ctrl+C from dupterm (e.g. WebREPL). --- esp8266/esp_mphal.c | 4 ++++ esp8266/esp_mphal.h | 3 +++ 2 files changed, 7 insertions(+) (limited to 'esp8266/esp_mphal.c') diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index eabf8ca208..04154a3781 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -176,6 +176,10 @@ static int call_dupterm_read(void) { mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); return -1; } + if (*(byte*)bufinfo.buf == interrupt_char) { + mp_keyboard_interrupt(); + return -2; + } return *(byte*)bufinfo.buf; } else { mp_uos_deactivate("dupterm: Exception in read() method, deactivating: ", nlr.ret_val); diff --git a/esp8266/esp_mphal.h b/esp8266/esp_mphal.h index 13b1c8fdf0..377bbeb6a4 100644 --- a/esp8266/esp_mphal.h +++ b/esp8266/esp_mphal.h @@ -29,6 +29,9 @@ #include "py/ringbuf.h" +void mp_keyboard_interrupt(void); +extern int interrupt_char; + struct _mp_print_t; // Structure for UART-only output via mp_printf() extern const struct _mp_print_t mp_debug_print; -- cgit v1.2.3 From 2b05b60bbc7e2ddcfc206eb817707713e1a90cc6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 24 May 2016 15:04:59 +0300 Subject: esp8266/esp_mphal: mp_uos_dupterm_deactivate() may raise exception. So, keep call to it protected via NLR still. --- esp8266/esp_mphal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'esp8266/esp_mphal.c') diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index 04154a3781..45f7f5c0fd 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -171,11 +171,12 @@ static int call_dupterm_read(void) { } mp_buffer_info_t bufinfo; mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ); - nlr_pop(); if (bufinfo.len == 0) { mp_uos_deactivate("dupterm: EOF received, deactivating\n", MP_OBJ_NULL); + nlr_pop(); return -1; } + nlr_pop(); if (*(byte*)bufinfo.buf == interrupt_char) { mp_keyboard_interrupt(); return -2; -- cgit v1.2.3 From 4b37e775eaedaf7706597090d82d26a618cfa027 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 26 May 2016 17:06:40 +0100 Subject: extmod/machine_i2c: Redo mp_hal_pin macros to use open_drain and od_low. mp_hal_pin_config_od is renamed mp_hal_pin_open_drain, and mp_hal_pin_low is mp_hal_pin_od_low. --- esp8266/esp_mphal.c | 2 +- esp8266/esp_mphal.h | 4 ++-- extmod/machine_i2c.c | 8 ++++---- stmhal/mphalport.h | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'esp8266/esp_mphal.c') diff --git a/esp8266/esp_mphal.c b/esp8266/esp_mphal.c index 45f7f5c0fd..9f090a0973 100644 --- a/esp8266/esp_mphal.c +++ b/esp8266/esp_mphal.c @@ -216,7 +216,7 @@ void mp_hal_signal_dupterm_input(void) { system_os_post(DUPTERM_TASK_ID, 0, 0); } -void mp_hal_pin_config_od(mp_hal_pin_obj_t pin_id) { +void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin_id) { const pyb_pin_obj_t *pin = &pyb_pin_obj[pin_id]; if (pin->phys_port == 16) { diff --git a/esp8266/esp_mphal.h b/esp8266/esp_mphal.h index 0de494300a..866a5a94bd 100644 --- a/esp8266/esp_mphal.h +++ b/esp8266/esp_mphal.h @@ -70,8 +70,8 @@ void ets_event_poll(void); #define mp_hal_get_pin_obj(o) mp_obj_get_pin(o) void mp_hal_pin_input(mp_hal_pin_obj_t pin); void mp_hal_pin_output(mp_hal_pin_obj_t pin); -void mp_hal_pin_config_od(mp_hal_pin_obj_t pin); -#define mp_hal_pin_low(p) do { \ +void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin); +#define mp_hal_pin_od_low(p) do { \ if ((p) == 16) { WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1) | 1); } \ else { gpio_output_set(0, 1 << (p), 1 << (p), 0); } \ } while (0) diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c index e3bdb36925..ceddf0730e 100644 --- a/extmod/machine_i2c.c +++ b/extmod/machine_i2c.c @@ -48,7 +48,7 @@ STATIC void mp_hal_i2c_delay(machine_i2c_obj_t *self) { } STATIC void mp_hal_i2c_scl_low(machine_i2c_obj_t *self) { - mp_hal_pin_low(self->scl); + mp_hal_pin_od_low(self->scl); } STATIC void mp_hal_i2c_scl_release(machine_i2c_obj_t *self) { @@ -56,7 +56,7 @@ STATIC void mp_hal_i2c_scl_release(machine_i2c_obj_t *self) { } STATIC void mp_hal_i2c_sda_low(machine_i2c_obj_t *self) { - mp_hal_pin_low(self->sda); + mp_hal_pin_od_low(self->sda); } STATIC void mp_hal_i2c_sda_release(machine_i2c_obj_t *self) { @@ -91,8 +91,8 @@ STATIC void mp_hal_i2c_init(machine_i2c_obj_t *self, uint32_t freq) { if (self->us_delay == 0) { self->us_delay = 1; } - mp_hal_pin_config_od(self->scl); - mp_hal_pin_config_od(self->sda); + mp_hal_pin_open_drain(self->scl); + mp_hal_pin_open_drain(self->sda); mp_hal_i2c_stop(self); } diff --git a/stmhal/mphalport.h b/stmhal/mphalport.h index 7b29f9c9c0..b155d9616e 100644 --- a/stmhal/mphalport.h +++ b/stmhal/mphalport.h @@ -45,7 +45,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable #include "stmhal/pin.h" #define mp_hal_pin_obj_t pin_obj_t* #define mp_hal_get_pin_obj(o) (pin_obj_t*)pin_find(o) -#define mp_hal_pin_config_od(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0) -#define mp_hal_pin_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask) +#define mp_hal_pin_open_drain(p) mp_hal_gpio_config((p)->gpio, (p)->pin, 5, 0, 0) +#define mp_hal_pin_od_low(p) GPIO_clear_pin((p)->gpio, (p)->pin_mask) #define mp_hal_pin_od_high(p) GPIO_set_pin((p)->gpio, (p)->pin_mask) #define mp_hal_pin_read(p) GPIO_read_pin((p)->gpio, (p)->pin) -- cgit v1.2.3