5656static 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
6767volatile bool mp_cdc_enabled = false;
6868volatile 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) {
315317bool 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+ }
0 commit comments