Skip to content

Commit 4691ef4

Browse files
committed
USB: Added optimized Stream::readBytes for EP OUT handler
This bypass the generic (but inefficient) implementation of Stream::readBytes and uses an optimized version of readBytes than can do efficient multi-byte reads.
1 parent 3127d1d commit 4691ef4

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

cores/arduino/USB/CDC.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ int Serial_::read(void)
184184
return usb.recv(CDC_ENDPOINT_OUT);
185185
}
186186

187+
size_t Serial_::readBytes(char *buffer, size_t length)
188+
{
189+
size_t count = 0;
190+
_startMillis = millis();
191+
while (count < length)
192+
{
193+
uint32_t n = usb.recv(CDC_ENDPOINT_OUT, buffer+count, length-count);
194+
if (n == 0 && (millis() - _startMillis) >= _timeout)
195+
break;
196+
count += n;
197+
}
198+
return count;
199+
}
200+
187201
void Serial_::flush(void)
188202
{
189203
usb.flush(CDC_ENDPOINT_IN);

cores/arduino/USB/USBAPI.h

+2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class Serial_ : public Stream
127127
using Print::write; // pull in write(str) from Print
128128
operator bool();
129129

130+
size_t readBytes(char *buffer, size_t length);
131+
130132
// This method allows processing "SEND_BREAK" requests sent by
131133
// the USB host. Those requests indicate that the host wants to
132134
// send a BREAK signal and are accompanied by a single uint16_t

0 commit comments

Comments
 (0)