Skip to content

Commit ea39f43

Browse files
authored
Merge pull request adafruit#698 from sommersoft/repl_fix
Fix 128 Character Max Paste Into REPL
2 parents 8b6aeb9 + 9bd55cf commit ea39f43

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

ports/atmel-samd/usb.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ static void init_hardware(void) {
102102
#endif
103103
}
104104

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];
106107
static volatile bool pending_read;
107108

108109
static int32_t start_read(void) {
109110
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);
111112
if (result != ERR_NONE) {
112113
pending_read = false;
113114
}
@@ -151,14 +152,6 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u
151152
}
152153
atomic_leave_critical(&flags);
153154

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-
162155
/* No error. */
163156
return false;
164157
}
@@ -170,7 +163,9 @@ static bool write_complete(const uint8_t ep,
170163
return false; // No errors.
171164
}
172165
// This is called after writes are finished.
166+
173167
usb_transmitting = false;
168+
174169
/* No error. */
175170
return false;
176171
}
@@ -227,25 +222,32 @@ void init_usb(void) {
227222
}
228223

229224
static bool cdc_enabled(void) {
230-
if (mp_cdc_enabled) {
231-
return true;
232-
}
233225
if (!cdcdf_acm_is_enabled()) {
226+
mp_cdc_enabled = false;
234227
return false;
235228
}
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+
}
241236

242237
return true;
243238
}
244239

245240
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
246247
if (cdc_enabled() && !pending_read) {
247248
start_read();
248249
}
250+
// Buffer is empty and/or no new data is available
249251
if (usb_rx_count == 0) {
250252
return false;
251253
}
@@ -263,16 +265,11 @@ int usb_read(void) {
263265
data = usb_rx_buf[usb_rx_buf_head];
264266
usb_rx_buf_head++;
265267
usb_rx_count--;
266-
if ((USB_RX_BUF_SIZE) == usb_rx_buf_head) {
268+
if (usb_rx_buf_head == USB_RX_BUF_SIZE) {
267269
usb_rx_buf_head = 0;
268270
}
269271
CRITICAL_SECTION_LEAVE();
270272

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-
276273
return data;
277274
}
278275

@@ -319,9 +316,10 @@ bool usb_connected(void) {
319316

320317
// Poll for input if keyboard interrupts are enabled,
321318
// 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
322320
void usb_cdc_background() {
323321
//
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)) {
325323
start_read();
326324
}
327325
}

0 commit comments

Comments
 (0)