@@ -46,6 +46,29 @@ STATIC volatile bool connected;
46
46
#error "CONFIG_ESP_PHY_ENABLE_USB must be enabled in sdkconfig"
47
47
#endif
48
48
49
+ // Make sure the recv interrupt is disabled during this. Otherwise, it could reorder data if it
50
+ // interrupts itself.
51
+ static void _copy_out_of_fifo (void ) {
52
+ size_t req_len = ringbuf_num_empty (& ringbuf );
53
+ if (req_len == 0 ) {
54
+ // Disable the interrupt so that CircuitPython can run and process the ringbuf. It will
55
+ // re-enable the interrupt once the ringbuf is empty.
56
+ usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
57
+ }
58
+ if (req_len > USB_SERIAL_JTAG_BUF_SIZE ) {
59
+ req_len = USB_SERIAL_JTAG_BUF_SIZE ;
60
+ }
61
+ uint8_t rx_buf [USB_SERIAL_JTAG_BUF_SIZE ];
62
+ size_t len = usb_serial_jtag_ll_read_rxfifo (rx_buf , req_len );
63
+ for (size_t i = 0 ; i < len ; ++ i ) {
64
+ if (rx_buf [i ] == mp_interrupt_char ) {
65
+ mp_sched_keyboard_interrupt ();
66
+ } else {
67
+ ringbuf_put (& ringbuf , rx_buf [i ]);
68
+ }
69
+ }
70
+ }
71
+
49
72
static void usb_serial_jtag_isr_handler (void * arg ) {
50
73
uint32_t flags = usb_serial_jtag_ll_get_intsts_mask ();
51
74
@@ -60,25 +83,13 @@ static void usb_serial_jtag_isr_handler(void *arg) {
60
83
61
84
if (flags & USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT ) {
62
85
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
63
- size_t req_len = ringbuf_num_empty (& ringbuf );
64
- if (req_len > USB_SERIAL_JTAG_BUF_SIZE ) {
65
- req_len = USB_SERIAL_JTAG_BUF_SIZE ;
66
- }
67
- uint8_t rx_buf [USB_SERIAL_JTAG_BUF_SIZE ];
68
- size_t len = usb_serial_jtag_ll_read_rxfifo (rx_buf , req_len );
69
- for (size_t i = 0 ; i < len ; ++ i ) {
70
- if (rx_buf [i ] == mp_interrupt_char ) {
71
- mp_sched_keyboard_interrupt ();
72
- } else {
73
- ringbuf_put (& ringbuf , rx_buf [i ]);
74
- }
75
- }
86
+ _copy_out_of_fifo ();
76
87
port_wake_main_task_from_isr ();
77
88
}
78
89
}
79
90
80
91
void usb_serial_jtag_init (void ) {
81
- ringbuf_init (& ringbuf , buf , sizeof (buf ));
92
+ ringbuf_init (& ringbuf , buf , sizeof (buf ) - 1 );
82
93
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1 );
83
94
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1 );
84
95
ESP_ERROR_CHECK (esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE , ESP_INTR_FLAG_LEVEL1 ,
@@ -93,11 +104,18 @@ char usb_serial_jtag_read_char(void) {
93
104
if (ringbuf_num_filled (& ringbuf ) == 0 ) {
94
105
return -1 ;
95
106
}
96
- return ringbuf_get (& ringbuf );
107
+ char c = ringbuf_get (& ringbuf );
108
+ // Maybe re-enable the recv interrupt if we've emptied the ringbuf.
109
+ if (ringbuf_num_filled (& ringbuf ) == 0 ) {
110
+ usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
111
+ _copy_out_of_fifo ();
112
+ usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
113
+ }
114
+ return c ;
97
115
}
98
116
99
117
bool usb_serial_jtag_bytes_available (void ) {
100
- return ringbuf_num_filled (& ringbuf );
118
+ return ringbuf_num_filled (& ringbuf ) > 0 ;
101
119
}
102
120
103
121
void usb_serial_jtag_write (const char * text , uint32_t length ) {
0 commit comments