Skip to content

Commit 6c36b42

Browse files
committed
USBSerial: fix sending more than CDC_MAX_PACKET_SIZE in a single shot
Fixes #85
1 parent e2b0634 commit 6c36b42

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

Diff for: cores/arduino/USB/PluggableUSBSerial.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,13 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
248248
if (!connected()) {
249249
return 0;
250250
}
251-
return send((uint8_t*)buf, size);
251+
size_t sent = 0;
252+
while (sent < size) {
253+
size_t to_send = (size - sent) > CDC_MAX_PACKET_SIZE ? CDC_MAX_PACKET_SIZE : (size - sent);
254+
send((uint8_t*)&buf[sent], to_send);
255+
sent += to_send;
256+
}
257+
return sent;
252258
}
253259
using Print::write; // pull in write(str) and write(buf, size) from Print
254260

Diff for: cores/arduino/USB/USBCDC.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ static const uint8_t cdc_line_coding_default[7] = {0x80, 0x25, 0x00, 0x00, 0x00,
4141
#define CLS_DTR (1 << 0)
4242
#define CLS_RTS (1 << 1)
4343

44-
#if defined(MBED_CONF_TARGET_USB_SPEED) && (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
45-
#define CDC_MAX_PACKET_SIZE 512
46-
#else
47-
#define CDC_MAX_PACKET_SIZE 64
48-
#endif
49-
5044
class USBCDC::AsyncWrite: public AsyncOp {
5145
public:
5246
AsyncWrite(USBCDC *serial, uint8_t *buf, uint32_t size):

Diff for: cores/arduino/USB/USBCDC.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include "OperationList.h"
2727
#include "PluggableUSBDevice.h"
2828

29+
#if defined(MBED_CONF_TARGET_USB_SPEED) && (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
30+
#define CDC_MAX_PACKET_SIZE 512
31+
#else
32+
#define CDC_MAX_PACKET_SIZE 64
33+
#endif
34+
2935
class AsyncOp;
3036

3137
namespace arduino {
@@ -209,13 +215,13 @@ class USBCDC: public internal::PluggableUSBModule{
209215

210216
OperationList<AsyncWrite> _tx_list;
211217
bool _tx_in_progress;
212-
uint8_t _tx_buffer[512];
218+
uint8_t _tx_buffer[CDC_MAX_PACKET_SIZE];
213219
uint8_t *_tx_buf;
214220
uint32_t _tx_size;
215221

216222
OperationList<AsyncRead> _rx_list;
217223
bool _rx_in_progress;
218-
uint8_t _rx_buffer[512];
224+
uint8_t _rx_buffer[CDC_MAX_PACKET_SIZE];
219225
uint8_t *_rx_buf;
220226
uint32_t _rx_size;
221227

0 commit comments

Comments
 (0)