Skip to content

Commit 2487226

Browse files
committed
Read serial input as a background task so we can check for the interrupt character.
1 parent f0a12d9 commit 2487226

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ bool start_mp(safe_mode_t safe_mode) {
127127

128128
pyexec_result_t result;
129129
bool found_main = false;
130+
130131
if (safe_mode != NO_SAFE_MODE) {
131132
serial_write(MSG_SAFE_MODE_NO_MAIN);
132133
} else {

ports/atmel-samd/background.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#include "background.h"
2727

2828
// #include "common-hal/audioio/AudioOut.h"
29+
#include "usb.h"
2930
#include "usb_mass_storage.h"
3031

3132
void run_background_tasks(void) {
3233
// audioout_background();
3334
usb_msc_background();
35+
usb_cdc_background();
3436
}

ports/atmel-samd/usb.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
static uint8_t usb_rx_buf[USB_RX_BUF_SIZE];
5757

5858
// Receive buffer head
59-
static volatile uint8_t usb_rx_buf_head;
59+
static volatile uint8_t usb_rx_buf_head = 0;
6060

6161
// Receive buffer tail
62-
static volatile uint8_t usb_rx_buf_tail;
62+
static volatile uint8_t usb_rx_buf_tail = 0;
6363

6464
// Number of bytes in receive buffer
65-
volatile uint8_t usb_rx_count;
65+
volatile uint8_t usb_rx_count = 0;
6666

6767
volatile bool mp_cdc_enabled = false;
6868
volatile bool usb_transmitting = false;
@@ -133,8 +133,11 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u
133133
uint8_t c = cdc_packet_buffer[i];
134134
if (c == mp_interrupt_char) {
135135
mp_keyboard_interrupt();
136-
// Don't put the interrupt into the buffer, just continue.
137-
continue;
136+
// If interrupted, flush all the input.
137+
usb_rx_count = 0;
138+
usb_rx_buf_head = 0;
139+
usb_rx_buf_tail = 0;
140+
break;
138141
} else {
139142
// The count of characters present in receive buffer is
140143
// incremented.
@@ -144,7 +147,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u
144147
if (usb_rx_buf_tail == USB_RX_BUF_SIZE) {
145148
// Reached the end of buffer, revert back to beginning of
146149
// buffer.
147-
usb_rx_buf_tail = 0x00;
150+
usb_rx_buf_tail = 0;
148151
}
149152
}
150153
}
@@ -219,8 +222,7 @@ void init_usb(void) {
219222
mscdf_register_callback(MSCDF_CB_TEST_DISK_READY, (FUNC_PTR)usb_msc_disk_is_ready);
220223
mscdf_register_callback(MSCDF_CB_XFER_BLOCKS_DONE, (FUNC_PTR)usb_msc_xfer_done);
221224

222-
int32_t result = usbdc_start(&multi_desc);
223-
while (result != ERR_NONE) {}
225+
usbdc_start(&multi_desc);
224226
usbdc_attach();
225227
}
226228

@@ -315,3 +317,12 @@ void usb_write(const char* buffer, uint32_t len) {
315317
bool usb_connected(void) {
316318
return cdc_enabled();
317319
}
320+
321+
// Poll for input if keyboard interrupts are enabled,
322+
// so that we can check for the interrupt char. read_complete() does the checking.
323+
void usb_cdc_background() {
324+
//
325+
if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) {
326+
start_read();
327+
}
328+
}

ports/atmel-samd/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ int usb_read(void);
3737
void usb_write(const char* buffer, uint32_t len);
3838
bool usb_bytes_available(void);
3939
bool usb_connected(void);
40+
void usb_cdc_background(void);
4041

4142
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_USB_H__

0 commit comments

Comments
 (0)