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 detail
} // namespace uart } // namespace uart

View File

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

View File

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