Skip to content

Commit 05c0e04

Browse files
committed
non blocking rx
1 parent b9af7f9 commit 05c0e04

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/USBHostSerial.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ std::size_t USBHostSerial::available() {
112112
uint8_t USBHostSerial::read() {
113113
std::size_t pxItemSize = 0;
114114
uint8_t retVal = 0;
115-
void* ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), 1);
115+
void* ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(10), 1);
116116
if (pxItemSize > 0) {
117117
retVal = *reinterpret_cast<uint8_t*>(ret);
118118
vRingbufferReturnItem(_rx_buf_handle, ret);
@@ -124,7 +124,7 @@ std::size_t USBHostSerial::read(uint8_t *dest, std::size_t size) {
124124
std::size_t retVal = 0;
125125
std::size_t pxItemSize = 0;
126126
while (size > pxItemSize) {
127-
void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - pxItemSize);
127+
void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(10), size - pxItemSize);
128128
if (ret) {
129129
std::memcpy(dest, ret, pxItemSize);
130130
retVal += pxItemSize;
@@ -143,7 +143,7 @@ void USBHostSerial::flush(bool txOnly) {
143143
taskYIELD();
144144
} while (numItemsWaiting > 0);
145145

146-
// if not only tx has to be flushed, read rx buffer untill empty
146+
// if not only tx has to be flushed, read rx buffer until empty
147147
if (!txOnly) {
148148
do {
149149
vRingbufferGetInfo(_rx_buf_handle, nullptr, nullptr, nullptr, nullptr, &numItemsWaiting);
@@ -185,15 +185,17 @@ void USBHostSerial::_setup() {
185185
}
186186

187187
bool USBHostSerial::_handle_rx(const uint8_t *data, size_t data_len, void *arg) {
188-
std::size_t lenReceived = 0;
189-
while (lenReceived < data_len && xRingbufferSend(static_cast<USBHostSerial*>(arg)->_rx_buf_handle, &data[lenReceived], 1, pdMS_TO_TICKS(10)) == pdTRUE) {
190-
++lenReceived;
188+
USBHostSerial* instance = static_cast<USBHostSerial*>(arg);
189+
if (xRingbufferSend(instance->_rx_buf_handle, data, data_len, 0) == pdTRUE) {
190+
return true;
191191
}
192-
if (lenReceived < data_len) {
193-
// log overflow warning
194-
static_cast<USBHostSerial*>(arg)->_log("USB rx buf overflow");
192+
// log overflow warning if free buffer space is the issue
193+
if (xRingbufferGetCurFreeSize(instance->_rx_buf_handle) < data_len) {
194+
instance->_log("USB rx buf overflow");
195+
return true;
195196
}
196-
return true;
197+
// no spurious logging here, might be an expected timeout
198+
return false;
197199
}
198200

199201
void USBHostSerial::_handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ctx) {

0 commit comments

Comments
 (0)