Skip to content

Commit 52237c8

Browse files
committed
CDC-ACM bugfix
1 parent d6857ed commit 52237c8

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

cores/arduino/USB/CDC.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,26 @@ void Serial_::end(void)
160160

161161
void Serial_::accept(void)
162162
{
163-
ring_buffer *buffer = &cdc_rx_buffer;
164-
uint32_t i = (uint32_t)(buffer->head+1) % CDC_SERIAL_BUFFER_SIZE;
163+
uint8_t buffer[CDC_SERIAL_BUFFER_SIZE];
164+
uint32_t len = USBD_Recv(CDC_ENDPOINT_OUT, (void*)&buffer, CDC_SERIAL_BUFFER_SIZE);
165+
166+
noInterrupts();
167+
ring_buffer *ringBuffer = &cdc_rx_buffer;
168+
uint32_t i = ringBuffer->head;
165169

166170
// if we should be storing the received character into the location
167171
// just before the tail (meaning that the head would advance to the
168172
// current location of the tail), we're about to overflow the buffer
169173
// and so we don't write the character or advance the head.
170-
while (i != buffer->tail) {
171-
uint32_t c;
172-
if (!USBD_Available(CDC_ENDPOINT_OUT)) {
173-
UDD_ReleaseRX(CDC_ENDPOINT_OUT);
174-
break;
175-
}
176-
c = USBD_Recv(CDC_ENDPOINT_OUT);
177-
buffer->buffer[buffer->head] = c;
178-
buffer->head = i;
179-
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++];
179+
ringBuffer->head = i;
180180
i = (i + 1) % CDC_SERIAL_BUFFER_SIZE;
181181
}
182+
interrupts();
182183
}
183184

184185
int Serial_::available(void)

cores/arduino/USB/USBCore.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,17 @@ uint32_t USBD_Recv(uint32_t ep, void* d, uint32_t len)
108108
if (!_usbConfiguration)
109109
return -1;
110110

111-
uint32_t n = UDD_FifoByteCount(ep);
112-
len = min(n,len);
113-
n = len;
114-
uint8_t* dst = (uint8_t*)d;
115-
while (n--)
116-
*dst++ = UDD_Recv_data(ep, 8);
111+
uint8_t *buffer;
112+
uint8_t *data = (uint8_t *)d;
113+
114+
len = min(UDD_FifoByteCount(ep), len);
115+
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];
120+
}
121+
117122
if (len && !UDD_FifoByteCount(ep)) // release empty buffer
118123
UDD_ReleaseRX(ep);
119124

cores/arduino/USB/samd21_device.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,23 @@ uint32_t UDD_Send(uint32_t ep, const void* data, uint32_t len)
266266
return len;
267267
}
268268

269-
uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
270-
{
271-
TRACE_DEVICE(printf("=> UDD_Recvdata : ep=%d\r\n", (char)ep);)
272-
273-
usb_endpoint_table[ep].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[ep];
274-
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = len;
275-
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
269+
uint8_t UDD_Recv_data(uint32_t ep, uint32_t len)
270+
{
271+
TRACE_DEVICE(printf("=> UDD_Recvdata : ep=%d\r\n", (char)ep);)
272+
273+
if (len>64) len=64;
274+
usb_endpoint_table[ep].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[ep];
275+
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = len;
276+
usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
276277
udd_OUT_transfer_allowed(ep);
277-
TRACE_DEVICE(printf("=> UDD_Recv_data : data=%lu\r\n", (unsigned long)data);)
278-
279-
/* Wait for transfer to complete */
280-
while (!udd_is_OUT_transf_cplt(ep));
281-
/* Clear Transfer complete 0 flag */
282-
udd_clear_OUT_transf_cplt(ep);
283-
284-
return udd_ep_out_cache_buffer[ep][0];
278+
TRACE_DEVICE(printf("=> UDD_Recv_data : data=%lu\r\n", (unsigned long)data);)
279+
280+
/* Wait for transfer to complete */
281+
while (!udd_is_OUT_transf_cplt(ep));
282+
/* Clear Transfer complete 0 flag */
283+
udd_clear_OUT_transf_cplt(ep);
284+
285+
return udd_ep_out_cache_buffer[ep][0];
285286
}
286287

287288
void UDD_Recv(uint32_t ep, uint8_t** ppData)

0 commit comments

Comments
 (0)