Skip to content

Commit 5cbc9e0

Browse files
committed
stmhal: Reduce coupling between USB driver and readline.
This makes it easier to re-use readline.c and pyexec.c from stmhal in other ports.
1 parent 6f5eb84 commit 5cbc9e0

12 files changed

Lines changed: 45 additions & 48 deletions

File tree

stmhal/input.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
4141
vstr_t line;
4242
vstr_init(&line, 16);
4343
int ret = readline(&line, "");
44-
if (line.len == 0 && ret == VCP_CHAR_CTRL_D) {
44+
if (line.len == 0 && ret == CHAR_CTRL_D) {
4545
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
4646
}
4747
mp_obj_t o = mp_obj_new_str(line.buf, line.len, false);

stmhal/mphal.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "misc.h"
66
#include "qstr.h"
77
#include "obj.h"
8+
#include "usb.h"
89
#include "mphal.h"
910

1011
// this table converts from HAL_StatusTypeDef to POSIX errno
@@ -18,3 +19,7 @@ const byte mp_hal_status_to_errno_table[4] = {
1819
NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
1920
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(mp_hal_status_to_errno_table[status])));
2021
}
22+
23+
void mp_hal_set_interrupt_char(int c) {
24+
usb_vcp_set_interrupt_char(c);
25+
}

stmhal/mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
extern const byte mp_hal_status_to_errno_table[4];
1111

1212
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
13+
void mp_hal_set_interrupt_char(int c); // -1 to disable

stmhal/pyexec.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
#include "systick.h"
4747
#include "readline.h"
4848
#include "pyexec.h"
49-
#include "usb.h"
50-
#include "uart.h"
5149
#include "pybstdio.h"
5250
#include "genhdr/py-version.h"
5351

@@ -97,9 +95,9 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
9795
nlr_buf_t nlr;
9896
uint32_t start = HAL_GetTick();
9997
if (nlr_push(&nlr) == 0) {
100-
usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C); // allow ctrl-C to interrupt us
98+
mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
10199
mp_call_function_0(module_fun);
102-
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
100+
mp_hal_set_interrupt_char(-1); // disable interrupt
103101
nlr_pop();
104102
ret = 1;
105103
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
@@ -108,7 +106,7 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
108106
} else {
109107
// uncaught exception
110108
// FIXME it could be that an interrupt happens just before we disable it here
111-
usb_vcp_set_interrupt_char(VCP_CHAR_NONE); // disable interrupt
109+
mp_hal_set_interrupt_char(-1); // disable interrupt
112110
// print EOF after normal output
113111
if (exec_flags & EXEC_FLAG_PRINT_EOF) {
114112
stdout_tx_strn("\x04", 1);
@@ -125,8 +123,8 @@ STATIC int parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t input_ki
125123

126124
// display debugging info if wanted
127125
if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
128-
uint32_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
129-
printf("took %lu ms\n", ticks);
126+
mp_uint_t ticks = HAL_GetTick() - start; // TODO implement a function that does this properly
127+
printf("took " UINT_FMT " ms\n", ticks);
130128
gc_collect();
131129
// qstr info
132130
{
@@ -166,19 +164,19 @@ int pyexec_raw_repl(void) {
166164
stdout_tx_str(">");
167165
for (;;) {
168166
char c = stdin_rx_chr();
169-
if (c == VCP_CHAR_CTRL_A) {
167+
if (c == CHAR_CTRL_A) {
170168
// reset raw REPL
171169
goto raw_repl_reset;
172-
} else if (c == VCP_CHAR_CTRL_B) {
170+
} else if (c == CHAR_CTRL_B) {
173171
// change to friendly REPL
174172
stdout_tx_str("\r\n");
175173
vstr_clear(&line);
176174
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
177175
return 0;
178-
} else if (c == VCP_CHAR_CTRL_C) {
176+
} else if (c == CHAR_CTRL_C) {
179177
// clear line
180178
vstr_reset(&line);
181-
} else if (c == VCP_CHAR_CTRL_D) {
179+
} else if (c == CHAR_CTRL_D) {
182180
// input finished
183181
break;
184182
} else if (c <= 127) {
@@ -230,7 +228,7 @@ int pyexec_friendly_repl(void) {
230228
for (;;) {
231229
nlr_buf_t nlr;
232230
printf("pyexec_repl: %p\n", x);
233-
usb_vcp_set_interrupt_char(VCP_CHAR_CTRL_C);
231+
mp_hal_set_interrupt_char(CHAR_CTRL_C);
234232
if (nlr_push(&nlr) == 0) {
235233
for (;;) {
236234
}
@@ -246,21 +244,21 @@ int pyexec_friendly_repl(void) {
246244
vstr_reset(&line);
247245
int ret = readline(&line, ">>> ");
248246

249-
if (ret == VCP_CHAR_CTRL_A) {
247+
if (ret == CHAR_CTRL_A) {
250248
// change to raw REPL
251249
stdout_tx_str("\r\n");
252250
vstr_clear(&line);
253251
pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
254252
return 0;
255-
} else if (ret == VCP_CHAR_CTRL_B) {
253+
} else if (ret == CHAR_CTRL_B) {
256254
// reset friendly REPL
257255
stdout_tx_str("\r\n");
258256
goto friendly_repl_reset;
259-
} else if (ret == VCP_CHAR_CTRL_C) {
257+
} else if (ret == CHAR_CTRL_C) {
260258
// break
261259
stdout_tx_str("\r\n");
262260
continue;
263-
} else if (ret == VCP_CHAR_CTRL_D) {
261+
} else if (ret == CHAR_CTRL_D) {
264262
// exit for a soft reset
265263
stdout_tx_str("\r\n");
266264
vstr_clear(&line);
@@ -272,11 +270,11 @@ int pyexec_friendly_repl(void) {
272270
while (mp_repl_continue_with_input(vstr_str(&line))) {
273271
vstr_add_char(&line, '\n');
274272
int ret = readline(&line, "... ");
275-
if (ret == VCP_CHAR_CTRL_C) {
273+
if (ret == CHAR_CTRL_C) {
276274
// cancel everything
277275
stdout_tx_str("\r\n");
278276
goto input_restart;
279-
} else if (ret == VCP_CHAR_CTRL_D) {
277+
} else if (ret == CHAR_CTRL_D) {
280278
// stop entering compound statement
281279
break;
282280
}

stmhal/readline.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include MICROPY_HAL_H
3737
#include "readline.h"
3838
#include "usb.h"
39-
#include "uart.h"
4039
#include "pybstdio.h"
4140

4241
#if 0 // print debugging info
@@ -80,16 +79,16 @@ int readline(vstr_t *line, const char *prompt) {
8079
bool redraw_from_cursor = false;
8180
int redraw_step_forward = 0;
8281
if (escape_seq == ESEQ_NONE) {
83-
if (VCP_CHAR_CTRL_A <= c && c <= VCP_CHAR_CTRL_D && vstr_len(line) == orig_line_len) {
82+
if (CHAR_CTRL_A <= c && c <= CHAR_CTRL_D && vstr_len(line) == orig_line_len) {
8483
// control character with empty line
8584
return c;
86-
} else if (c == VCP_CHAR_CTRL_A) {
85+
} else if (c == CHAR_CTRL_A) {
8786
// CTRL-A with non-empty line is go-to-start-of-line
8887
goto home_key;
89-
} else if (c == VCP_CHAR_CTRL_C) {
88+
} else if (c == CHAR_CTRL_C) {
9089
// CTRL-C with non-empty line is cancel
9190
return c;
92-
} else if (c == VCP_CHAR_CTRL_E) {
91+
} else if (c == CHAR_CTRL_E) {
9392
// CTRL-E is go-to-end-of-line
9493
goto end_key;
9594
} else if (c == '\r') {

stmhal/readline.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#define CHAR_CTRL_A (1)
28+
#define CHAR_CTRL_B (2)
29+
#define CHAR_CTRL_C (3)
30+
#define CHAR_CTRL_D (4)
31+
#define CHAR_CTRL_E (5)
32+
2733
void readline_init0(void);
2834
int readline(vstr_t *line, const char *prompt);

stmhal/usb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ STATIC mp_obj_t mp_const_vcp_interrupt = MP_OBJ_NULL;
5454
void pyb_usb_init0(void) {
5555
// create an exception object for interrupting by VCP
5656
mp_const_vcp_interrupt = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
57-
USBD_CDC_SetInterrupt(VCP_CHAR_NONE, mp_const_vcp_interrupt);
57+
USBD_CDC_SetInterrupt(-1, mp_const_vcp_interrupt);
5858
}
5959

6060
void pyb_usb_dev_init(usb_device_mode_t mode, usb_storage_medium_t medium) {
@@ -105,7 +105,7 @@ bool usb_vcp_is_connected(void) {
105105

106106
void usb_vcp_set_interrupt_char(int c) {
107107
if (dev_is_enabled) {
108-
if (c != VCP_CHAR_NONE) {
108+
if (c != -1) {
109109
mp_obj_exception_clear_traceback(mp_const_vcp_interrupt);
110110
}
111111
USBD_CDC_SetInterrupt(c, mp_const_vcp_interrupt);

stmhal/usb.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#define VCP_CHAR_NONE (0)
28-
#define VCP_CHAR_CTRL_A (1)
29-
#define VCP_CHAR_CTRL_B (2)
30-
#define VCP_CHAR_CTRL_C (3)
31-
#define VCP_CHAR_CTRL_D (4)
32-
#define VCP_CHAR_CTRL_E (5)
33-
3427
typedef enum {
3528
USB_DEVICE_MODE_CDC_MSC,
3629
USB_DEVICE_MODE_CDC_HID,

stmhal/usbd_cdc_interface.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@
4141
#include "usbd_cdc_interface.h"
4242
#include "pendsv.h"
4343

44-
#include "mpconfig.h"
45-
#include "misc.h"
46-
#include "qstr.h"
47-
#include "obj.h"
48-
#include "usb.h"
49-
5044
// CDC control commands
5145
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
5246
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
@@ -79,7 +73,7 @@ static uint16_t UserTxBufPtrOutShadow = 0; // shadow of above
7973
static uint8_t UserTxBufPtrWaitCount = 0; // used to implement a timeout waiting for low-level USB driver
8074
static uint8_t UserTxNeedEmptyPacket = 0; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size
8175

82-
static int user_interrupt_char = VCP_CHAR_NONE;
76+
static int user_interrupt_char = -1;
8377
static void *user_interrupt_data = NULL;
8478

8579
/* USB handler declaration */
@@ -159,7 +153,7 @@ static int8_t CDC_Itf_Init(void)
159153
* may be called before this init function to set these values.
160154
* This can happen if the USB enumeration occurs after the call to
161155
* USBD_CDC_SetInterrupt.
162-
user_interrupt_char = VCP_CHAR_NONE;
156+
user_interrupt_char = -1;
163157
user_interrupt_data = NULL;
164158
*/
165159

@@ -351,7 +345,7 @@ static int8_t CDC_Itf_Receive(uint8_t* Buf, uint32_t *Len) {
351345

352346
uint32_t delta_len;
353347

354-
if (user_interrupt_char == VCP_CHAR_NONE) {
348+
if (user_interrupt_char == -1) {
355349
// no special interrupt character
356350
delta_len = *Len;
357351

teensy/teensy_hal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ uint32_t HAL_GetTick(void) {
1414
void HAL_Delay(uint32_t Delay) {
1515
delay(Delay);
1616
}
17+
18+
void mp_hal_set_interrupt_char(int c) {
19+
// The teensy 3.1 usb stack doesn't currently have the notion of generating
20+
// an exception when a certain character is received. That just means that
21+
// you can't press Control-C and get your python script to stop.
22+
}

0 commit comments

Comments
 (0)