@@ -102,12 +102,13 @@ static void init_hardware(void) {
102
102
#endif
103
103
}
104
104
105
- COMPILER_ALIGNED (4 ) uint8_t cdc_packet_buffer [64 ];
105
+ #define CDC_BULKOUT_SIZE CONF_USB_COMPOSITE_CDC_ACM_DATA_BULKOUT_MAXPKSZ
106
+ COMPILER_ALIGNED (4 ) uint8_t cdc_packet_buffer [CDC_BULKOUT_SIZE ];
106
107
static volatile bool pending_read ;
107
108
108
109
static int32_t start_read (void ) {
109
110
pending_read = true;
110
- int32_t result = cdcdf_acm_read (cdc_packet_buffer , 64 );
111
+ int32_t result = cdcdf_acm_read (cdc_packet_buffer , CDC_BULKOUT_SIZE );
111
112
if (result != ERR_NONE ) {
112
113
pending_read = false;
113
114
}
@@ -151,14 +152,6 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u
151
152
}
152
153
atomic_leave_critical (& flags );
153
154
154
- // Trigger a follow up read if we have space.
155
- if (usb_rx_count < USB_RX_BUF_SIZE ) {
156
- int32_t result = start_read ();
157
- if (result != ERR_NONE ) {
158
- return true;
159
- }
160
- }
161
-
162
155
/* No error. */
163
156
return false;
164
157
}
@@ -170,7 +163,9 @@ static bool write_complete(const uint8_t ep,
170
163
return false; // No errors.
171
164
}
172
165
// This is called after writes are finished.
166
+
173
167
usb_transmitting = false;
168
+
174
169
/* No error. */
175
170
return false;
176
171
}
@@ -227,25 +222,32 @@ void init_usb(void) {
227
222
}
228
223
229
224
static bool cdc_enabled (void ) {
230
- if (mp_cdc_enabled ) {
231
- return true;
232
- }
233
225
if (!cdcdf_acm_is_enabled ()) {
226
+ mp_cdc_enabled = false;
234
227
return false;
235
228
}
236
- cdcdf_acm_register_callback (CDCDF_ACM_CB_READ , (FUNC_PTR )read_complete );
237
- cdcdf_acm_register_callback (CDCDF_ACM_CB_WRITE , (FUNC_PTR )write_complete );
238
- cdcdf_acm_register_callback (CDCDF_ACM_CB_STATE_C , (FUNC_PTR )usb_device_cb_state_c );
239
- cdcdf_acm_register_callback (CDCDF_ACM_CB_LINE_CODING_C , (FUNC_PTR )usb_device_cb_line_coding_c );
240
- mp_cdc_enabled = true;
229
+ if (!mp_cdc_enabled ) {
230
+ cdcdf_acm_register_callback (CDCDF_ACM_CB_READ , (FUNC_PTR )read_complete );
231
+ cdcdf_acm_register_callback (CDCDF_ACM_CB_WRITE , (FUNC_PTR )write_complete );
232
+ cdcdf_acm_register_callback (CDCDF_ACM_CB_STATE_C , (FUNC_PTR )usb_device_cb_state_c );
233
+ cdcdf_acm_register_callback (CDCDF_ACM_CB_LINE_CODING_C , (FUNC_PTR )usb_device_cb_line_coding_c );
234
+ mp_cdc_enabled = true;
235
+ }
241
236
242
237
return true;
243
238
}
244
239
245
240
bool usb_bytes_available (void ) {
241
+ // Check if the buffer has data, but not enough
242
+ // space to hold another read.
243
+ if (usb_rx_count > USB_RX_BUF_SIZE - CDC_BULKOUT_SIZE ) {
244
+ return true;
245
+ }
246
+ // Buffer has enough room
246
247
if (cdc_enabled () && !pending_read ) {
247
248
start_read ();
248
249
}
250
+ // Buffer is empty and/or no new data is available
249
251
if (usb_rx_count == 0 ) {
250
252
return false;
251
253
}
@@ -263,16 +265,11 @@ int usb_read(void) {
263
265
data = usb_rx_buf [usb_rx_buf_head ];
264
266
usb_rx_buf_head ++ ;
265
267
usb_rx_count -- ;
266
- if (( USB_RX_BUF_SIZE ) == usb_rx_buf_head ) {
268
+ if (usb_rx_buf_head == USB_RX_BUF_SIZE ) {
267
269
usb_rx_buf_head = 0 ;
268
270
}
269
271
CRITICAL_SECTION_LEAVE ();
270
272
271
- // Trigger a new read because we just cleared some space.
272
- if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1 ) {
273
- start_read ();
274
- }
275
-
276
273
return data ;
277
274
}
278
275
@@ -319,9 +316,10 @@ bool usb_connected(void) {
319
316
320
317
// Poll for input if keyboard interrupts are enabled,
321
318
// so that we can check for the interrupt char. read_complete() does the checking.
319
+ // also make sure we have enough room in the local buffer
322
320
void usb_cdc_background () {
323
321
//
324
- if (mp_interrupt_char != -1 && cdc_enabled () && !pending_read ) {
322
+ if (mp_interrupt_char != -1 && cdc_enabled () && !pending_read && ( usb_rx_count < USB_RX_BUF_SIZE - CDC_BULKOUT_SIZE ) ) {
325
323
start_read ();
326
324
}
327
325
}
0 commit comments