diff --git a/hardware.hpp b/hardware.hpp index 3e67d2d..369744e 100644 --- a/hardware.hpp +++ b/hardware.hpp @@ -45,6 +45,11 @@ class Hardware { *Registers::CTRL_STAT_REG_C = controlRegC; } + static typename cfg::data_t rxByteInterrupt() FORCE_INLINE + { + return *Registers::IO_REG; + } + static void txByteBlocking(const typename cfg::data_t &byte) FORCE_INLINE { while (!(*Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::DATA_REG_EMPTY))) diff --git a/hardware1.hpp b/hardware1.hpp index 7b6ce17..2f94416 100644 --- a/hardware1.hpp +++ b/hardware1.hpp @@ -107,7 +107,10 @@ class Hardware1 { HardwareImpl::txByteBlocking(byte); } - static data_t rxByte() FORCE_INLINE {} + static bool rxByte(data_t &byte) FORCE_INLINE + { + return false; + } static data_t peek() FORCE_INLINE {} @@ -143,7 +146,18 @@ class Hardware1 { HardwareImpl::enableDataRegEmptyInt(); } - static data_t rxByte() FORCE_INLINE {} + static bool rxByte(data_t &byte) FORCE_INLINE + { + if (sm_rxBuf.head == sm_rxBuf.tail) + return false; + + uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE; + + byte = sm_rxBuf.buf[tmpTail]; + sm_rxBuf.tail = tmpTail; + + return true; + } static data_t peek() FORCE_INLINE {} @@ -152,12 +166,19 @@ class Hardware1 { detail::ControlFlagsC1, cfg, mode, Driven::INTERRUPT>; static constexpr auto TX_BUFFER_SIZE = 16; + static constexpr auto RX_BUFFER_SIZE = 16; static volatile detail::RingBuffer sm_txBuf; + static volatile detail::RingBuffer sm_rxBuf; static void rxIntHandler() { - // TODO + uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE; + + if (tmpHead != sm_rxBuf.tail) { + sm_rxBuf.head = tmpHead; + sm_rxBuf.buf[tmpHead] = HardwareImpl::rxByteInterrupt(); + } } static void dataRegEmptyIntHandler() FORCE_INLINE @@ -181,6 +202,11 @@ volatile detail::RingBuffer::da Hardware1::TX_BUFFER_SIZE> Hardware1::sm_txBuf = {0, 0, {0}}; +template +volatile detail::RingBuffer::data_t, + Hardware1::RX_BUFFER_SIZE> + Hardware1::sm_rxBuf = {0, 0, {0}}; + #endif } // namespace uart diff --git a/uart.hpp b/uart.hpp index 2cb45c2..b64bab0 100644 --- a/uart.hpp +++ b/uart.hpp @@ -40,9 +40,9 @@ class Uart { Driver::txByte(byte); } - static typename Driver::data_t rxByte() + static bool rxByte(typename Driver::data_t &byte) { - return Driver::rxByte(); + return Driver::rxByte(byte); } static typename Driver::data_t peek()