Moved ring buffer to it's own struct

This commit is contained in:
BlackMark 2019-08-02 17:13:53 +02:00
parent 51a9d30c0a
commit b90da9bd9c
3 changed files with 31 additions and 34 deletions

View File

@ -176,6 +176,13 @@ class Hardware {
}
};
template <typename data_t, uint8_t Size>
struct RingBuffer {
uint8_t head;
uint8_t tail;
data_t buf[Size];
};
} // namespace detail
} // namespace uart

View File

@ -129,12 +129,12 @@ class Hardware0<mode, cfg, Driven::INTERRUPT> {
static void txByte(const data_t &byte) FORCE_INLINE
{
uint8_t tmpHead = (sm_txHead + 1) % TX_BUFFER_SIZE;
while (tmpHead == sm_txTail)
uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
while (tmpHead == sm_txBuf.tail)
;
sm_txBuffer[tmpHead] = byte;
sm_txHead = tmpHead;
sm_txBuf.buf[tmpHead] = byte;
sm_txBuf.head = tmpHead;
HardwareImpl::enableDataRegEmptyInt();
}
@ -149,9 +149,7 @@ class Hardware0<mode, cfg, Driven::INTERRUPT> {
static constexpr auto TX_BUFFER_SIZE = 16;
static volatile uint8_t sm_txHead;
static volatile uint8_t sm_txTail;
static volatile data_t sm_txBuffer[TX_BUFFER_SIZE];
static volatile detail::RingBuffer<data_t, TX_BUFFER_SIZE> sm_txBuf;
static void rxIntHandler()
{
@ -160,10 +158,10 @@ class Hardware0<mode, cfg, Driven::INTERRUPT> {
static void dataRegEmptyIntHandler() FORCE_INLINE
{
if (sm_txHead != sm_txTail) {
uint8_t tmpTail = (sm_txTail + 1) % TX_BUFFER_SIZE;
sm_txTail = tmpTail;
HardwareImpl::txByteInterrupt(sm_txBuffer[tmpTail]);
if (sm_txBuf.head != sm_txBuf.tail) {
uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
sm_txBuf.tail = tmpTail;
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
} else
HardwareImpl::disableDataRegEmptyInt();
}
@ -175,12 +173,9 @@ class Hardware0<mode, cfg, Driven::INTERRUPT> {
};
template <Mode mode, class cfg>
volatile uint8_t Hardware0<mode, cfg, Driven::INTERRUPT>::sm_txHead = 0;
template <Mode mode, class cfg>
volatile uint8_t Hardware0<mode, cfg, Driven::INTERRUPT>::sm_txTail = 0;
template <Mode mode, class cfg>
volatile typename Hardware0<mode, cfg, Driven::INTERRUPT>::data_t
Hardware0<mode, cfg, Driven::INTERRUPT>::sm_txBuffer[TX_BUFFER_SIZE];
volatile detail::RingBuffer<typename Hardware0<mode, cfg, Driven::INTERRUPT>::data_t,
Hardware0<mode, cfg, Driven::INTERRUPT>::TX_BUFFER_SIZE>
Hardware0<mode, cfg, Driven::INTERRUPT>::sm_txBuf = {0, 0, {0}};
} // namespace uart

View File

@ -133,12 +133,12 @@ class Hardware1<mode, cfg, Driven::INTERRUPT> {
static void txByte(const data_t &byte) FORCE_INLINE
{
uint8_t tmpHead = (sm_txHead + 1) % TX_BUFFER_SIZE;
while (tmpHead == sm_txTail)
uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
while (tmpHead == sm_txBuf.tail)
;
sm_txBuffer[tmpHead] = byte;
sm_txHead = tmpHead;
sm_txBuf.buf[tmpHead] = byte;
sm_txBuf.head = tmpHead;
HardwareImpl::enableDataRegEmptyInt();
}
@ -153,9 +153,7 @@ class Hardware1<mode, cfg, Driven::INTERRUPT> {
static constexpr auto TX_BUFFER_SIZE = 16;
static volatile uint8_t sm_txHead;
static volatile uint8_t sm_txTail;
static volatile data_t sm_txBuffer[TX_BUFFER_SIZE];
static volatile detail::RingBuffer<data_t, TX_BUFFER_SIZE> sm_txBuf;
static void rxIntHandler()
{
@ -164,10 +162,10 @@ class Hardware1<mode, cfg, Driven::INTERRUPT> {
static void dataRegEmptyIntHandler() FORCE_INLINE
{
if (sm_txHead != sm_txTail) {
uint8_t tmpTail = (sm_txTail + 1) % TX_BUFFER_SIZE;
sm_txTail = tmpTail;
HardwareImpl::txByteInterrupt(sm_txBuffer[tmpTail]);
if (sm_txBuf.head != sm_txBuf.tail) {
uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
sm_txBuf.tail = tmpTail;
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
} else
HardwareImpl::disableDataRegEmptyInt();
}
@ -179,12 +177,9 @@ class Hardware1<mode, cfg, Driven::INTERRUPT> {
};
template <Mode mode, class cfg>
volatile uint8_t Hardware1<mode, cfg, Driven::INTERRUPT>::sm_txHead = 0;
template <Mode mode, class cfg>
volatile uint8_t Hardware1<mode, cfg, Driven::INTERRUPT>::sm_txTail = 0;
template <Mode mode, class cfg>
volatile typename Hardware1<mode, cfg, Driven::INTERRUPT>::data_t
Hardware1<mode, cfg, Driven::INTERRUPT>::sm_txBuffer[TX_BUFFER_SIZE];
volatile detail::RingBuffer<typename Hardware1<mode, cfg, Driven::INTERRUPT>::data_t,
Hardware1<mode, cfg, Driven::INTERRUPT>::TX_BUFFER_SIZE>
Hardware1<mode, cfg, Driven::INTERRUPT>::sm_txBuf = {0, 0, {0}};
#endif