Skip to content

Commit

Permalink
USBSerial: fix sending more than CDC_MAX_PACKET_SIZE in a single shot
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Dec 3, 2020
1 parent e2b0634 commit 6c36b42
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 7 additions & 1 deletion cores/arduino/USB/PluggableUSBSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ class USBSerial: public USBCDC, public ::mbed::Stream, public HardwareSerial {
if (!connected()) {
return 0;
}
return send((uint8_t*)buf, size);
size_t sent = 0;
while (sent < size) {
size_t to_send = (size - sent) > CDC_MAX_PACKET_SIZE ? CDC_MAX_PACKET_SIZE : (size - sent);
send((uint8_t*)&buf[sent], to_send);
sent += to_send;
}
return sent;
}
using Print::write; // pull in write(str) and write(buf, size) from Print

Expand Down
6 changes: 0 additions & 6 deletions cores/arduino/USB/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ static const uint8_t cdc_line_coding_default[7] = {0x80, 0x25, 0x00, 0x00, 0x00,
#define CLS_DTR (1 << 0)
#define CLS_RTS (1 << 1)

#if defined(MBED_CONF_TARGET_USB_SPEED) && (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
#define CDC_MAX_PACKET_SIZE 512
#else
#define CDC_MAX_PACKET_SIZE 64
#endif

class USBCDC::AsyncWrite: public AsyncOp {
public:
AsyncWrite(USBCDC *serial, uint8_t *buf, uint32_t size):
Expand Down
10 changes: 8 additions & 2 deletions cores/arduino/USB/USBCDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include "OperationList.h"
#include "PluggableUSBDevice.h"

#if defined(MBED_CONF_TARGET_USB_SPEED) && (MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS)
#define CDC_MAX_PACKET_SIZE 512
#else
#define CDC_MAX_PACKET_SIZE 64
#endif

class AsyncOp;

namespace arduino {
Expand Down Expand Up @@ -209,13 +215,13 @@ class USBCDC: public internal::PluggableUSBModule{

OperationList<AsyncWrite> _tx_list;
bool _tx_in_progress;
uint8_t _tx_buffer[512];
uint8_t _tx_buffer[CDC_MAX_PACKET_SIZE];
uint8_t *_tx_buf;
uint32_t _tx_size;

OperationList<AsyncRead> _rx_list;
bool _rx_in_progress;
uint8_t _rx_buffer[512];
uint8_t _rx_buffer[CDC_MAX_PACKET_SIZE];
uint8_t *_rx_buf;
uint32_t _rx_size;

Expand Down

0 comments on commit 6c36b42

Please sign in to comment.