@@ -39,7 +39,10 @@ static void _copy_out_of_fifo(void) {
39
39
req_len = USB_SERIAL_JTAG_BUF_SIZE ;
40
40
}
41
41
uint8_t rx_buf [USB_SERIAL_JTAG_BUF_SIZE ];
42
+
43
+ // Read up to req_len bytes. Does not block.
42
44
size_t len = usb_serial_jtag_ll_read_rxfifo (rx_buf , req_len );
45
+
43
46
for (size_t i = 0 ; i < len ; ++ i ) {
44
47
if (rx_buf [i ] == mp_interrupt_char ) {
45
48
mp_sched_keyboard_interrupt ();
@@ -62,7 +65,9 @@ static void usb_serial_jtag_isr_handler(void *arg) {
62
65
}
63
66
64
67
if (flags & USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT ) {
68
+ // New bytes are in the FIFO. Read them and check for keyboard interrupt.
65
69
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
70
+ // This is executed at interrupt level, so we don't explicitly need to make it atomic.
66
71
_copy_out_of_fifo ();
67
72
port_wake_main_task_from_isr ();
68
73
}
@@ -81,28 +86,41 @@ bool usb_serial_jtag_connected(void) {
81
86
}
82
87
83
88
char usb_serial_jtag_read_char (void ) {
84
- if (ringbuf_num_filled (& ringbuf ) == 0 && !usb_serial_jtag_ll_rxfifo_data_available ()) {
89
+ uint32_t num_filled = ringbuf_num_filled (& ringbuf );
90
+
91
+ if (num_filled == 0 && !usb_serial_jtag_ll_rxfifo_data_available ()) {
85
92
return -1 ;
86
93
}
87
94
char c = -1 ;
88
- if (ringbuf_num_filled (& ringbuf ) > 0 ) {
95
+
96
+ if (num_filled > 0 ) {
97
+ common_hal_mcu_disable_interrupts ();
89
98
c = ringbuf_get (& ringbuf );
99
+ common_hal_mcu_enable_interrupts ();
100
+
101
+ num_filled -- ;
90
102
}
103
+
91
104
// Maybe re-enable the recv interrupt if we've emptied the ringbuf.
92
- if (ringbuf_num_filled ( & ringbuf ) == 0 ) {
105
+ if (num_filled == 0 ) {
93
106
usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
94
107
_copy_out_of_fifo ();
95
- usb_serial_jtag_ll_ena_intr_mask ( USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
108
+
96
109
// May have only been ctrl-c.
97
110
if (c == -1 && ringbuf_num_filled (& ringbuf ) > 0 ) {
98
111
c = ringbuf_get (& ringbuf );
99
112
}
113
+ usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
100
114
}
101
115
return c ;
102
116
}
103
117
104
118
uint32_t usb_serial_jtag_bytes_available (void ) {
105
- return ringbuf_num_filled (& ringbuf ) + usb_serial_jtag_ll_rxfifo_data_available ();
119
+ // Atomically get the number of bytes in the ringbuf plus what is not yet in the ringbuf.
120
+ common_hal_mcu_disable_interrupts ();
121
+ const uint32_t count = ringbuf_num_filled (& ringbuf ) + usb_serial_jtag_ll_rxfifo_data_available ();
122
+ common_hal_mcu_enable_interrupts ();
123
+ return count ;
106
124
}
107
125
108
126
void usb_serial_jtag_write (const char * text , uint32_t length ) {
0 commit comments