Skip to content

Commit 2e7cd9f

Browse files
committed
added micropython.kbd_intr_enable(bool) to allow enable/disable keyboard interrupt on ESP32*
This is useful for ESP32* as it allows STDIO ports to be used as UART ports for interacting with extern devices. mp_hal_set_interrupt_char(int) is supposed to work but does not work because every time when parse_compile_execute() is called, the intr_char is reset depending on exec_flags.
1 parent 83877c0 commit 2e7cd9f

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

ports/esp32/uart.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ int uart_stdout_tx_strn(const char *str, size_t len) {
100100
}
101101

102102
// all code executed in ISR must be in IRAM, and any const data must be in DRAM
103+
extern bool mp_kbd_intr_enable;
103104
static void IRAM_ATTR uart_irq_handler(void *arg) {
104105
uint8_t rbuf[SOC_UART_FIFO_LEN];
105106
int len;
@@ -112,7 +113,7 @@ static void IRAM_ATTR uart_irq_handler(void *arg) {
112113
uart_hal_read_rxfifo(&repl_hal, rbuf, &len);
113114

114115
for (int i = 0; i < len; i++) {
115-
if (rbuf[i] == mp_interrupt_char) {
116+
if (mp_kbd_intr_enable && rbuf[i] == mp_interrupt_char) {
116117
mp_sched_keyboard_interrupt();
117118
} else {
118119
// this is an inline function so will be in IRAM

ports/esp32/usb_serial_jtag.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
static DRAM_ATTR portMUX_TYPE rx_mux = portMUX_INITIALIZER_UNLOCKED;
4141
static uint8_t rx_buf[USB_SERIAL_JTAG_BUF_SIZE];
4242
static volatile bool terminal_connected = false;
43+
extern bool mp_kbd_intr_enable;
4344

4445
static void usb_serial_jtag_handle_rx(void) {
4546
if (xPortInIsrContext()) {
@@ -53,7 +54,7 @@ static void usb_serial_jtag_handle_rx(void) {
5354
}
5455
size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
5556
for (size_t i = 0; i < len; ++i) {
56-
if (rx_buf[i] == mp_interrupt_char) {
57+
if (mp_kbd_intr_enable && rx_buf[i] == mp_interrupt_char) {
5758
mp_sched_keyboard_interrupt();
5859
} else {
5960
ringbuf_put(&stdin_ringbuf, rx_buf[i]);

py/modmicropython.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ static MP_DEFINE_CONST_FUN_OBJ_1(mp_alloc_emergency_exception_buf_obj, mp_alloc_
149149
#endif
150150

151151
#if MICROPY_KBD_EXCEPTION
152+
extern int mp_interrupt_char, mp_kbd_intr_enable;
152153
static mp_obj_t mp_micropython_kbd_intr(mp_obj_t int_chr_in) {
154+
int old_intr_char = mp_interrupt_char;
153155
mp_hal_set_interrupt_char(mp_obj_get_int(int_chr_in));
154-
return mp_const_none;
156+
return mp_obj_new_int(old_intr_char);
155157
}
156158
static MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_obj, mp_micropython_kbd_intr);
159+
160+
static mp_obj_t mp_micropython_kbd_intr_enable(mp_obj_t state) {
161+
mp_kbd_intr_enable = mp_obj_get_int(state);
162+
return mp_const_none;
163+
}
164+
static MP_DEFINE_CONST_FUN_OBJ_1(mp_micropython_kbd_intr_enable_obj, mp_micropython_kbd_intr_enable);
157165
#endif
158166

159167
#if MICROPY_ENABLE_SCHEDULER
@@ -199,6 +207,7 @@ static const mp_rom_map_elem_t mp_module_micropython_globals_table[] = {
199207
#endif
200208
#if MICROPY_KBD_EXCEPTION
201209
{ MP_ROM_QSTR(MP_QSTR_kbd_intr), MP_ROM_PTR(&mp_micropython_kbd_intr_obj) },
210+
{ MP_ROM_QSTR(MP_QSTR_kbd_intr_enable), MP_ROM_PTR(&mp_micropython_kbd_intr_enable_obj) },
202211
#endif
203212
#if MICROPY_PY_MICROPYTHON_RINGIO
204213
{ MP_ROM_QSTR(MP_QSTR_RingIO), MP_ROM_PTR(&mp_type_ringio) },

shared/runtime/interrupt_char.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#if MICROPY_KBD_EXCEPTION
3131

3232
int mp_interrupt_char = -1;
33-
33+
int mp_kbd_intr_enable = 1;
3434
void mp_hal_set_interrupt_char(int c) {
3535
mp_interrupt_char = c;
3636
}

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