Skip to content

Commit d181360

Browse files
committed
Fixed incorrect CDC-USB buffer handling
1 parent 052af21 commit d181360

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

cores/arduino/USB/CDC.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#ifdef CDC_ENABLED
3636

37-
#define CDC_SERIAL_BUFFER_SIZE 64
37+
#define CDC_SERIAL_BUFFER_SIZE 512
3838

3939
/* For information purpose only since RTS is not always handled by the terminal application */
4040
#define CDC_LINESTATE_DTR 0x01 // Data Terminal Ready
@@ -160,26 +160,39 @@ void Serial_::end(void)
160160

161161
void Serial_::accept(void)
162162
{
163+
volatile uint32_t len,k=0, size=0;
163164
uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
164-
uint32_t len = USBD_Recv(CDC_ENDPOINT_OUT, (void*)&buffer, CDC_SERIAL_BUFFER_SIZE);
165165

166-
noInterrupts();
167166
ring_buffer *ringBuffer = &cdc_rx_buffer;
168-
uint32_t i = ringBuffer->head;
167+
uint32_t i = (uint32_t)(ringBuffer->head+1) % CDC_SERIAL_BUFFER_SIZE;
168+
169+
size = 0;
170+
do
171+
{
172+
len = USBD_Recv(CDC_ENDPOINT_OUT, (void*)&buffer+size, CDC_SERIAL_BUFFER_SIZE);
173+
size += len;
174+
if( size >= 512) break;
175+
} while(len != 0 );
176+
177+
udd_clear_OUT_transf_cplt(CDC_ENDPOINT_OUT);
169178

170179
// if we should be storing the received character into the location
171180
// just before the tail (meaning that the head would advance to the
172181
// current location of the tail), we're about to overflow the buffer
173182
// and so we don't write the character or advance the head.
174-
uint32_t k = 0;
175-
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
176-
while (i != ringBuffer->tail && len>0) {
177-
len--;
178-
ringBuffer->buffer[ringBuffer->head] = buffer[k++];
183+
while (i != ringBuffer->tail) {
184+
uint32_t c;
185+
if (size == 0) { // if (!USBD_Available(CDC_RX)) { udd_ack_fifocon(CDC_RX);
186+
break;
187+
}
188+
size--;
189+
c = buffer[k++];
190+
// c = UDD_Recv8(CDC_RX & 0xF);
191+
ringBuffer->buffer[ringBuffer->head] = c;
179192
ringBuffer->head = i;
193+
180194
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
181195
}
182-
interrupts();
183196
}
184197

185198
int Serial_::available(void)

cores/arduino/USB/USBCore.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,27 @@ uint32_t USBD_Available(uint32_t ep)
105105
// Return number of bytes read
106106
uint32_t USBD_Recv(uint32_t ep, void* d, uint32_t len)
107107
{
108-
if (!_usbConfiguration)
109-
return -1;
110-
111-
uint8_t *buffer;
108+
uint8_t *tmpbuffer;
112109
uint8_t *data = (uint8_t *)d;
113110

114-
len = min(UDD_FifoByteCount(ep), len);
111+
if (!_usbConfiguration || len < 0)
112+
return -1;
115113

116-
UDD_Recv_data(ep, len);
117-
UDD_Recv(ep, &buffer);
118-
for (uint32_t i=0; i<len; i++) {
119-
data[i] = buffer[i];
114+
uint32_t n = UDD_FifoByteCount(ep);
115+
116+
// len = min(n,len);
117+
// n = len;
118+
119+
UDD_Recv_data(ep, n);
120+
UDD_Recv(ep, &tmpbuffer);
121+
for (int i=0; i<n; i++) {
122+
data[i] = tmpbuffer[i];
120123
}
121124

122-
if (len && !UDD_FifoByteCount(ep)) // release empty buffer
123-
UDD_ReleaseRX(ep);
125+
// if (n && !UDD_FifoByteCount(ep)) // release empty buffer
126+
// UDD_ReleaseRX(ep);
124127

125-
return len;
128+
return n;
126129
}
127130

128131
// Recv 1 byte if ready
@@ -206,7 +209,7 @@ uint32_t USBD_RecvControl(void* d, uint32_t len)
206209
read = len;
207210
UDD_Recv(EP0, &buffer);
208211
while (!udd_is_OUT_transf_cplt(EP0));
209-
for (uint32_t i=0; i<read; i++) {
212+
for (int i=0; i<read; i++) {
210213
data[i] = buffer[i];
211214
}
212215
udd_OUT_transfer_allowed(EP0);

cores/arduino/USB/samd21_device.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@
2525
#include "USB/samd21_device.h"
2626
#include "sam.h"
2727

28-
#ifdef __cplusplus
29-
extern "C"{
30-
#endif // __cplusplus
31-
32-
#ifdef SAMD_SERIES
33-
3428
//#define TRACE_DEVICE(x) x
3529
#define TRACE_DEVICE(x)
3630

37-
__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_out_cache_buffer[4][64];
38-
__attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/ uint8_t udd_ep_in_cache_buffer[4][128];
31+
__attribute__((__aligned__(4))) __attribute__((__section__(".bss_hram0"))) uint8_t udd_ep_out_cache_buffer[4][64];
32+
__attribute__((__aligned__(4))) __attribute__((__section__(".bss_hram0"))) uint8_t udd_ep_in_cache_buffer[4][128];
3933

4034
/**
4135
* USB SRAM data containing pipe descriptor table
@@ -270,7 +264,6 @@ uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
270264
{
271265
TRACE_DEVICE(printf("=> UDD_Recvdata : ep=%d\r\n", (char)ep);)
272266

273-
if (len>64) len=64;
274267
usb_endpoint_table[ep].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[ep];
275268
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = len;
276269
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
@@ -280,7 +273,7 @@ uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
280273
/* Wait for transfer to complete */
281274
while (!udd_is_OUT_transf_cplt(ep));
282275
/* Clear Transfer complete 0 flag */
283-
udd_clear_OUT_transf_cplt(ep);
276+
// udd_clear_OUT_transf_cplt(ep);
284277

285278
return udd_ep_out_cache_buffer[ep][0];
286279
}
@@ -309,7 +302,7 @@ void UDD_ReleaseRX(uint32_t ep)
309302
// The RAM Buffer is empty: we can receive data
310303
udd_OUT_transfer_allowed(ep);
311304
/* Clear Transfer complete 0 flag */
312-
udd_clear_OUT_transf_cplt(ep);
305+
// udd_clear_OUT_transf_cplt(ep);
313306
}
314307

315308
void UDD_ReleaseTX(uint32_t ep)
@@ -318,7 +311,7 @@ void UDD_ReleaseTX(uint32_t ep)
318311
// The RAM Buffer is full: we can send data
319312
udd_IN_transfer_allowed(ep);
320313
/* Clear the transfer complete flag */
321-
udd_clear_IN_transf_cplt(ep);
314+
// udd_clear_IN_transf_cplt(ep);
322315
}
323316

324317
void UDD_SetAddress(uint32_t addr)
@@ -342,10 +335,3 @@ uint32_t UDD_GetFrameNumber(void)
342335
{
343336
return udd_frame_number();
344337
}
345-
346-
#ifdef __cplusplus
347-
}
348-
#endif
349-
350-
#endif /* SAMD_SERIES */
351-

0 commit comments

Comments
 (0)