Skip to content

Commit d3a4d39

Browse files
committed
esp8266: Support raising KeyboardInterrupt on Ctrl+C.
1 parent 0774483 commit d3a4d39

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

esp8266/esp_mphal.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ void mp_hal_delay_ms(uint32_t delay) {
9898
}
9999

100100
void mp_hal_set_interrupt_char(int c) {
101-
// TODO
101+
if (c != -1) {
102+
mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
103+
}
104+
extern int interrupt_char;
105+
interrupt_char = c;
102106
}
103107

104108
void __assert_func(const char *file, int line, const char *func, const char *expr) {

esp8266/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ STATIC void mp_reset(void) {
4747
mp_init();
4848
mp_obj_list_init(mp_sys_path, 0);
4949
mp_obj_list_init(mp_sys_argv, 0);
50+
MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
5051
#if MICROPY_MODULE_FROZEN
5152
pyexec_frozen_module("main");
5253
#endif
@@ -83,6 +84,10 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
8384
}
8485
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
8586

87+
void mp_keyboard_interrupt(void) {
88+
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception);
89+
}
90+
8691
void nlr_jump_fail(void *val) {
8792
printf("NLR jump failed\n");
8893
for (;;) {

esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ extern const struct _mp_obj_module_t mp_module_machine;
9999

100100
#define MICROPY_PORT_ROOT_POINTERS \
101101
const char *readline_hist[8]; \
102+
mp_obj_t mp_kbd_exception; \
102103
\
103104
/* Singleton instance of scan callback, meaning that there can
104105
be only one concurrent AP scan. */ \

esp8266/uart.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,15 @@ void ICACHE_FLASH_ATTR uart_reattach() {
210210
#include "lib/utils/pyexec.h"
211211

212212
void soft_reset(void);
213+
void mp_keyboard_interrupt(void);
213214

215+
int interrupt_char;
214216
void uart_task_handler(os_event_t *evt) {
215217
int c, ret = 0;
216218
while ((c = uart_rx_one_char(UART_REPL)) >= 0) {
219+
if (c == interrupt_char) {
220+
mp_keyboard_interrupt();
221+
}
217222
ret = pyexec_event_repl_process_char(c);
218223
if (ret & PYEXEC_FORCED_EXIT) {
219224
break;

0 commit comments

Comments
 (0)