From b90da9bd9cb90634c980ca02279e1144dd7e03bf Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 2 Aug 2019 17:13:53 +0200 Subject: [PATCH] Moved ring buffer to it's own struct --- hardware.hpp | 7 +++++++ hardware0.hpp | 29 ++++++++++++----------------- hardware1.hpp | 29 ++++++++++++----------------- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/hardware.hpp b/hardware.hpp index b8f553d..3e67d2d 100644 --- a/hardware.hpp +++ b/hardware.hpp @@ -176,6 +176,13 @@ class Hardware { } }; +template +struct RingBuffer { + uint8_t head; + uint8_t tail; + data_t buf[Size]; +}; + } // namespace detail } // namespace uart diff --git a/hardware0.hpp b/hardware0.hpp index 521aac6..931399d 100644 --- a/hardware0.hpp +++ b/hardware0.hpp @@ -129,12 +129,12 @@ class Hardware0 { 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 { 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 sm_txBuf; static void rxIntHandler() { @@ -160,10 +158,10 @@ class Hardware0 { 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 { }; template -volatile uint8_t Hardware0::sm_txHead = 0; -template -volatile uint8_t Hardware0::sm_txTail = 0; -template -volatile typename Hardware0::data_t - Hardware0::sm_txBuffer[TX_BUFFER_SIZE]; +volatile detail::RingBuffer::data_t, + Hardware0::TX_BUFFER_SIZE> + Hardware0::sm_txBuf = {0, 0, {0}}; } // namespace uart diff --git a/hardware1.hpp b/hardware1.hpp index b053fdc..7b6ce17 100644 --- a/hardware1.hpp +++ b/hardware1.hpp @@ -133,12 +133,12 @@ class Hardware1 { 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 { 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 sm_txBuf; static void rxIntHandler() { @@ -164,10 +162,10 @@ class Hardware1 { 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 { }; template -volatile uint8_t Hardware1::sm_txHead = 0; -template -volatile uint8_t Hardware1::sm_txTail = 0; -template -volatile typename Hardware1::data_t - Hardware1::sm_txBuffer[TX_BUFFER_SIZE]; +volatile detail::RingBuffer::data_t, + Hardware1::TX_BUFFER_SIZE> + Hardware1::sm_txBuf = {0, 0, {0}}; #endif