Skip to content

Commit 889332c

Browse files
committed
Revert " Fixed incorrect CDC-USB buffer handling"
This reverts commit bc7115b104428f9bea4fdfa1b6af245085f6f542.
1 parent cd00d5f commit 889332c

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

cores/arduino/USB/CDC.cpp

Lines changed: 10 additions & 23 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 512
37+
#define CDC_SERIAL_BUFFER_SIZE 64
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,39 +160,26 @@ void Serial_::end(void)
160160

161161
void Serial_::accept(void)
162162
{
163-
volatile uint32_t len,k=0, size=0;
164163
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();
166167
ring_buffer *ringBuffer = &cdc_rx_buffer;
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);
168+
uint32_t i = ringBuffer->head;
178169

179170
// if we should be storing the received character into the location
180171
// just before the tail (meaning that the head would advance to the
181172
// current location of the tail), we're about to overflow the buffer
182173
// and so we don't write the character or advance the head.
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;
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++];
192179
ringBuffer->head = i;
193-
194180
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
195181
}
182+
interrupts();
196183
}
197184

198185
int Serial_::available(void)

cores/arduino/USB/USBCore.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,27 +105,24 @@ 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-
uint8_t *tmpbuffer;
109-
uint8_t *data = (uint8_t *)d;
110-
111-
if (!_usbConfiguration || len < 0)
108+
if (!_usbConfiguration)
112109
return -1;
113110

114-
uint32_t n = UDD_FifoByteCount(ep);
115-
116-
// len = min(n,len);
117-
// n = len;
111+
uint8_t *buffer;
112+
uint8_t *data = (uint8_t *)d;
113+
114+
len = min(UDD_FifoByteCount(ep), len);
118115

119-
UDD_Recv_data(ep, n);
120-
UDD_Recv(ep, &tmpbuffer);
121-
for (int i=0; i<n; i++) {
122-
data[i] = tmpbuffer[i];
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];
123120
}
124121

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

128-
return n;
125+
return len;
129126
}
130127

131128
// Recv 1 byte if ready
@@ -209,7 +206,7 @@ uint32_t USBD_RecvControl(void* d, uint32_t len)
209206
read = len;
210207
UDD_Recv(EP0, &buffer);
211208
while (!udd_is_OUT_transf_cplt(EP0));
212-
for (int i=0; i<read; i++) {
209+
for (uint32_t i=0; i<read; i++) {
213210
data[i] = buffer[i];
214211
}
215212
udd_OUT_transfer_allowed(EP0);

cores/arduino/USB/samd21_device.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
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+
2834
//#define TRACE_DEVICE(x) x
2935
#define TRACE_DEVICE(x)
3036

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];
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];
3339

3440
/**
3541
* USB SRAM data containing pipe descriptor table
@@ -264,6 +270,7 @@ uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
264270
{
265271
TRACE_DEVICE(printf("=> UDD_Recvdata : ep=%d\r\n", (char)ep);)
266272

273+
if (len>64) len=64;
267274
usb_endpoint_table[ep].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[ep];
268275
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = len;
269276
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
@@ -273,7 +280,7 @@ uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
273280
/* Wait for transfer to complete */
274281
while (!udd_is_OUT_transf_cplt(ep));
275282
/* Clear Transfer complete 0 flag */
276-
// udd_clear_OUT_transf_cplt(ep);
283+
udd_clear_OUT_transf_cplt(ep);
277284

278285
return udd_ep_out_cache_buffer[ep][0];
279286
}
@@ -302,7 +309,7 @@ void UDD_ReleaseRX(uint32_t ep)
302309
// The RAM Buffer is empty: we can receive data
303310
udd_OUT_transfer_allowed(ep);
304311
/* Clear Transfer complete 0 flag */
305-
// udd_clear_OUT_transf_cplt(ep);
312+
udd_clear_OUT_transf_cplt(ep);
306313
}
307314

308315
void UDD_ReleaseTX(uint32_t ep)
@@ -311,7 +318,7 @@ void UDD_ReleaseTX(uint32_t ep)
311318
// The RAM Buffer is full: we can send data
312319
udd_IN_transfer_allowed(ep);
313320
/* Clear the transfer complete flag */
314-
// udd_clear_IN_transf_cplt(ep);
321+
udd_clear_IN_transf_cplt(ep);
315322
}
316323

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

0 commit comments

Comments
 (0)