Skip to content

Commit 6ab2c5e

Browse files
committed
lib/interrupt_char: Factor out typical Ctrl+C handling from esp8266 port.
Utility functions for keyboard interrupt handling, to be reused across (baremetal) ports.
1 parent 53bfcc9 commit 6ab2c5e

7 files changed

Lines changed: 74 additions & 17 deletions

File tree

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ LIB_SRC_C = $(addprefix lib/,\
128128
timeutils/timeutils.c \
129129
utils/pyexec.c \
130130
utils/pyhelp.c \
131+
utils/interrupt_char.c \
131132
fatfs/ff.c \
132133
fatfs/option/ccsbcs.c \
133134
)

esp8266/esp_mphal.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,6 @@ void mp_hal_delay_ms(uint32_t delay) {
128128
mp_hal_delay_us(delay * 1000);
129129
}
130130

131-
void mp_hal_set_interrupt_char(int c) {
132-
if (c != -1) {
133-
mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
134-
}
135-
extern int interrupt_char;
136-
interrupt_char = c;
137-
}
138-
139131
void ets_event_poll(void) {
140132
ets_loop_iter();
141133
if (MP_STATE_VM(mp_pending_exception) != NULL) {
@@ -180,7 +172,7 @@ static int call_dupterm_read(void) {
180172
mp_buffer_info_t bufinfo;
181173
mp_get_buffer_raise(MP_STATE_PORT(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ);
182174
nlr_pop();
183-
if (*(byte*)bufinfo.buf == interrupt_char) {
175+
if (*(byte*)bufinfo.buf == mp_interrupt_char) {
184176
mp_keyboard_interrupt();
185177
return -2;
186178
}

esp8266/esp_mphal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
#define _INCLUDED_MPHAL_H_
2929

3030
#include "py/ringbuf.h"
31+
#include "lib/utils/interrupt_char.h"
3132
#include "xtirq.h"
3233

3334
void mp_keyboard_interrupt(void);
34-
extern int interrupt_char;
3535

3636
struct _mp_print_t;
3737
// Structure for UART-only output via mp_printf()

esp8266/main.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
141141
}
142142
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
143143

144-
void mp_keyboard_interrupt(void) {
145-
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception);
146-
}
147-
148144
void nlr_jump_fail(void *val) {
149145
printf("NLR jump failed\n");
150146
for (;;) {

esp8266/uart.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ static void uart0_rx_intr_handler(void *para);
3939
void soft_reset(void);
4040
void mp_keyboard_interrupt(void);
4141

42-
int interrupt_char;
43-
4442
/******************************************************************************
4543
* FunctionName : uart_config
4644
* Description : Internal used function
@@ -172,7 +170,7 @@ static void uart0_rx_intr_handler(void *para) {
172170

173171
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
174172
uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
175-
if (RcvChar == interrupt_char) {
173+
if (RcvChar == mp_interrupt_char) {
176174
mp_keyboard_interrupt();
177175
} else {
178176
ringbuf_put(&input_buf, RcvChar);

lib/utils/interrupt_char.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "py/mpstate.h"
29+
30+
int mp_interrupt_char;
31+
32+
void mp_hal_set_interrupt_char(int c) {
33+
if (c != -1) {
34+
mp_obj_exception_clear_traceback(MP_STATE_PORT(mp_kbd_exception));
35+
}
36+
mp_interrupt_char = c;
37+
}
38+
39+
void mp_keyboard_interrupt(void) {
40+
MP_STATE_VM(mp_pending_exception) = MP_STATE_PORT(mp_kbd_exception);
41+
}

lib/utils/interrupt_char.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
extern int mp_interrupt_char;
28+
void mp_hal_set_interrupt_char(int c);
29+
void mp_keyboard_interrupt(void);

0 commit comments

Comments
 (0)