Skip to content

Commit

Permalink
CDC/Uart: avoid overfetch on ::read(), increase Uart RX/TX buffer siz…
Browse files Browse the repository at this point in the history
…e to 128
  • Loading branch information
GrumpyOldPizza committed Jan 29, 2017
1 parent 913a911 commit f90e5b6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 91 deletions.
100 changes: 49 additions & 51 deletions cores/stm32l4/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,38 @@ int CDC::read()

size_t CDC::read(uint8_t *buffer, size_t size)
{
unsigned int rx_read, rx_count;
size_t count;
unsigned int rx_read, rx_count;
size_t count;

count = 0;
count = 0;

while (count < size) {
while (count < size) {

rx_count = _rx_count;
rx_count = _rx_count;

if (rx_count == 0) {
break;
}
if (rx_count == 0) {
break;
}

rx_read = _rx_read;
rx_read = _rx_read;

if (rx_count > (UART_RX_BUFFER_SIZE - rx_read)) {
rx_count = (UART_RX_BUFFER_SIZE - rx_read);
}
if (rx_count > (CDC_RX_BUFFER_SIZE - rx_read)) {
rx_count = (CDC_RX_BUFFER_SIZE - rx_read);
}

memcpy(&buffer[count], &_rx_data[rx_read], rx_count);
count += rx_count;
if (rx_count > (size - count)) {
rx_count = (size - count);
}

memcpy(&buffer[count], &_rx_data[rx_read], rx_count);
count += rx_count;

_rx_read = (rx_read + rx_count) & (CDC_RX_BUFFER_SIZE -1);
_rx_read = (rx_read + rx_count) & (CDC_RX_BUFFER_SIZE -1);

armv7m_atomic_sub(&_rx_count, rx_count);
}
armv7m_atomic_sub(&_rx_count, rx_count);
}

return count;
return count;
}

void CDC::flush()
Expand Down Expand Up @@ -286,7 +290,7 @@ size_t CDC::write(const uint8_t *buffer, size_t size)
return count;
}

void CDC::onReceive(void(*callback)(int))
void CDC::onReceive(void(*callback)(void))
{
_receiveCallback = callback;
}
Expand All @@ -308,42 +312,36 @@ void CDC::EventCallback(uint32_t events)
bool empty;

if (events & USBD_CDC_EVENT_RECEIVE) {
while (_rx_count != CDC_RX_BUFFER_SIZE) {
empty = (_rx_count == 0);

count = 0;

do {
rx_size = 0;
rx_count = CDC_RX_BUFFER_SIZE - _rx_count;
empty = (_rx_count == 0);

if (rx_count == 0) {
break;
}

rx_write = _rx_write;
count = 0;

if (rx_count > (CDC_RX_BUFFER_SIZE - rx_write)) {
rx_count = (CDC_RX_BUFFER_SIZE - rx_write);
}

rx_size = stm32l4_usbd_cdc_receive(_usbd_cdc, &_rx_data[rx_write], rx_count);

_rx_write = (rx_write + rx_size) & (CDC_RX_BUFFER_SIZE -1);

armv7m_atomic_add(&_rx_count, rx_size);

count += rx_size;

} while (rx_size);

if (empty && _receiveCallback) {
(*_receiveCallback)(count);
}

if (!rx_size) {
do {
rx_size = 0;
rx_count = CDC_RX_BUFFER_SIZE - _rx_count;

if (rx_count == 0) {
break;
}

rx_write = _rx_write;

if (rx_count > (CDC_RX_BUFFER_SIZE - rx_write)) {
rx_count = (CDC_RX_BUFFER_SIZE - rx_write);
}

rx_size = stm32l4_usbd_cdc_receive(_usbd_cdc, &_rx_data[rx_write], rx_count);

_rx_write = (rx_write + rx_size) & (CDC_RX_BUFFER_SIZE -1);

armv7m_atomic_add(&_rx_count, rx_size);

count += rx_size;

} while (rx_size);

if (empty && count && _receiveCallback) {
armv7m_pendsv_enqueue((armv7m_pendsv_routine_t)_receiveCallback, NULL, 0);
}
}

Expand Down
4 changes: 2 additions & 2 deletions cores/stm32l4/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class CDC : public HardwareSerial
size_t read(uint8_t *buffer, size_t size);

// STM32L4 EXTENSTION: asynchronous receive
void onReceive(void(*callback)(int));
void onReceive(void(*callback)(void));

// STM32L4 EXTENSTION: enable/disabe blocking writes
void blockOnOverrun(bool enable);
Expand All @@ -122,7 +122,7 @@ class CDC : public HardwareSerial
volatile uint32_t _tx_count;
volatile uint32_t _tx_size;

void (*_receiveCallback)(int);
void (*_receiveCallback)(void);

static void _event_callback(void *context, uint32_t events);
void EventCallback(uint32_t events);
Expand Down
66 changes: 32 additions & 34 deletions cores/stm32l4/Uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ size_t Uart::read(uint8_t *buffer, size_t size)
rx_count = (UART_RX_BUFFER_SIZE - rx_read);
}

if (rx_count > (size - count)) {
rx_count = (size - count);
}

memcpy(&buffer[count], &_rx_data[rx_read], rx_count);
count += rx_count;
count += rx_count;

_rx_read = (rx_read + rx_count) & (UART_RX_BUFFER_SIZE -1);

Expand Down Expand Up @@ -268,7 +272,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size)
return count;
}

void Uart::onReceive(void(*callback)(int))
void Uart::onReceive(void(*callback)(void))
{
_receiveCallback = callback;
}
Expand All @@ -290,42 +294,36 @@ void Uart::EventCallback(uint32_t events)
bool empty;

if (events & UART_EVENT_RECEIVE) {
while (_rx_count != UART_RX_BUFFER_SIZE) {
empty = (_rx_count == 0);

count = 0;
empty = (_rx_count == 0);

do {
rx_size = 0;
rx_count = UART_RX_BUFFER_SIZE - _rx_count;
count = 0;

if (rx_count == 0) {
break;
}

rx_write = _rx_write;

if (rx_count > (UART_RX_BUFFER_SIZE - rx_write)) {
rx_count = (UART_RX_BUFFER_SIZE - rx_write);
}

rx_size = stm32l4_uart_receive(_uart, &_rx_data[rx_write], rx_count);

_rx_write = (rx_write + rx_size) & (UART_RX_BUFFER_SIZE -1);

armv7m_atomic_add(&_rx_count, rx_size);

count += rx_size;

} while (rx_size);

if (empty && _receiveCallback) {
(*_receiveCallback)(count);
}

if (!rx_size) {
do {
rx_size = 0;
rx_count = UART_RX_BUFFER_SIZE - _rx_count;

if (rx_count == 0) {
break;
}

rx_write = _rx_write;

if (rx_count > (UART_RX_BUFFER_SIZE - rx_write)) {
rx_count = (UART_RX_BUFFER_SIZE - rx_write);
}

rx_size = stm32l4_uart_receive(_uart, &_rx_data[rx_write], rx_count);

_rx_write = (rx_write + rx_size) & (UART_RX_BUFFER_SIZE -1);

armv7m_atomic_add(&_rx_count, rx_size);

count += rx_size;

} while (rx_size);

if (empty && count && _receiveCallback) {
armv7m_pendsv_enqueue((armv7m_pendsv_routine_t)_receiveCallback, NULL, 0);
}
}

Expand Down
8 changes: 4 additions & 4 deletions cores/stm32l4/Uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

#include "HardwareSerial.h"

#define UART_RX_BUFFER_SIZE 64
#define UART_TX_BUFFER_SIZE 64
#define UART_RX_BUFFER_SIZE 128
#define UART_TX_BUFFER_SIZE 128

class Uart : public HardwareSerial
{
Expand All @@ -55,7 +55,7 @@ class Uart : public HardwareSerial
size_t read(uint8_t *buffer, size_t size);

// STM32L4 EXTENSTION: asynchronous receive
void onReceive(void(*callback)(int));
void onReceive(void(*callback)(void));

// STM32L4 EXTENSTION: enable/disabe blocking writes
void blockOnOverrun(bool block);
Expand All @@ -77,7 +77,7 @@ class Uart : public HardwareSerial
volatile uint32_t _tx_count;
volatile uint32_t _tx_size;

void (*_receiveCallback)(int);
void (*_receiveCallback)(void);

static void _event_callback(void *context, uint32_t events);
void EventCallback(uint32_t events);
Expand Down

0 comments on commit f90e5b6

Please sign in to comment.