Remove useless receiving flag and fix overflow protection
This commit is contained in:
parent
065cf1f0c3
commit
015ab15a02
@ -42,7 +42,7 @@ class VirtualComPort {
|
||||
|
||||
[[gnu::always_inline]] static bool rxByte(uint8_t& byte)
|
||||
{
|
||||
if(m_receiving || m_rxBuffer.head == m_rxBuffer.tail)
|
||||
if(m_rxBuffer.head == m_rxBuffer.tail)
|
||||
return false;
|
||||
|
||||
m_reading = true;
|
||||
@ -63,7 +63,7 @@ class VirtualComPort {
|
||||
|
||||
[[gnu::always_inline]] static bool peek(uint8_t& byte)
|
||||
{
|
||||
if(m_receiving || m_rxBuffer.head == m_rxBuffer.tail)
|
||||
if(m_rxBuffer.head == m_rxBuffer.tail)
|
||||
return false;
|
||||
|
||||
m_reading = true;
|
||||
@ -112,7 +112,6 @@ class VirtualComPort {
|
||||
|
||||
static RingBuffer<RX_BUFFER_SIZE> m_rxBuffer;
|
||||
static Buffer<RX_BUFFER_SIZE> m_usbAsyncRxBuffer;
|
||||
static volatile bool m_receiving;
|
||||
static volatile bool m_reading;
|
||||
|
||||
static int8_t CdcInit()
|
||||
@ -141,7 +140,6 @@ class VirtualComPort {
|
||||
m_rxBuffer.head = 0;
|
||||
m_rxBuffer.tail = 0;
|
||||
m_usbAsyncRxBuffer.size = 0;
|
||||
m_receiving = false;
|
||||
m_reading = false;
|
||||
return USBD_OK;
|
||||
}
|
||||
@ -206,14 +204,9 @@ class VirtualComPort {
|
||||
if(USBD_CDC_ReceivePacket(&hUsbDeviceFS) != USBD_OK)
|
||||
return USBD_FAIL;
|
||||
|
||||
if(m_reading)
|
||||
return USBD_FAIL;
|
||||
|
||||
m_receiving = true;
|
||||
for(uint32_t i = 0; i < *length; ++i) {
|
||||
rxHandler(m_usbAsyncRxBuffer.data[i]);
|
||||
}
|
||||
m_receiving = false;
|
||||
|
||||
#ifndef NDEBUG
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
||||
@ -225,8 +218,13 @@ class VirtualComPort {
|
||||
{
|
||||
const size_t newHead = (m_rxBuffer.head + 1) % RX_BUFFER_SIZE;
|
||||
|
||||
// Overflow, overwrites last element
|
||||
if(newHead == m_rxBuffer.tail) {
|
||||
// Overflow, but tail is being read
|
||||
if(newHead == m_rxBuffer.tail && m_reading) {
|
||||
// Throw away the data, because it cannot be received safely
|
||||
return;
|
||||
}
|
||||
// Overflow, overwrite oldest data
|
||||
else if(newHead == m_rxBuffer.tail) {
|
||||
const size_t newTail = (m_rxBuffer.tail + 1) % RX_BUFFER_SIZE;
|
||||
m_rxBuffer.tail = newTail;
|
||||
}
|
||||
@ -248,9 +246,6 @@ RingBuffer<VirtualComPort<cfg>::RX_BUFFER_SIZE> VirtualComPort<cfg>::m_rxBuffer
|
||||
template<class cfg>
|
||||
Buffer<VirtualComPort<cfg>::RX_BUFFER_SIZE> VirtualComPort<cfg>::m_usbAsyncRxBuffer = {0, {0}};
|
||||
|
||||
template<class cfg>
|
||||
volatile bool VirtualComPort<cfg>::m_receiving = false;
|
||||
|
||||
template<class cfg>
|
||||
volatile bool VirtualComPort<cfg>::m_reading = false;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user