From a3d76a138ddb9e5c0e7607aae38248ef0b33959b Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 2 Aug 2019 18:20:06 +0200 Subject: [PATCH] Implemented peeking with and without data for interrupt and blocking mode --- hardware.hpp | 9 +++++++++ hardware0.hpp | 29 ++++++++++++++++++++++++++--- hardware1.hpp | 29 ++++++++++++++++++++++++++--- uart.hpp | 7 ++++++- 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/hardware.hpp b/hardware.hpp index 96f3c2e..1aec066 100644 --- a/hardware.hpp +++ b/hardware.hpp @@ -73,6 +73,15 @@ class Hardware { *Registers::IO_REG = byte; } + static bool peekBlocking() FORCE_INLINE + { + if (*Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) { + return true; + } + + return false; + } + static void enableDataRegEmptyInt() FORCE_INLINE { *Registers::CTRL_STAT_REG_B |= (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE); diff --git a/hardware0.hpp b/hardware0.hpp index 62ab024..0dc82e4 100644 --- a/hardware0.hpp +++ b/hardware0.hpp @@ -101,7 +101,17 @@ class Hardware0 { return HardwareImpl::rxByteBlocking(byte); } - static data_t peek() FORCE_INLINE {} + static bool peek(data_t &byte) FORCE_INLINE + { + static_cast(byte); + static_assert(driven == Driven::BLOCKING, "Peek with data is not supported in blocking mode"); + return false; + } + + static bool peek() FORCE_INLINE + { + return HardwareImpl::peekBlocking(); + } private: using HardwareImpl = detail::Hardware { 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 {} + static bool peek(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]; + + return true; + } + + static bool peek() FORCE_INLINE + { + return (sm_rxBuf.head != sm_rxBuf.tail); + } private: using HardwareImpl = detail::Hardware(byte); + static_assert(driven != Driven::BLOCKING, "Peek with data is not supported in blocking mode"); + return false; + } + + static bool peek() FORCE_INLINE + { + return HardwareImpl::peekBlocking(); + } private: using HardwareImpl = detail::Hardware { 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 {} + static bool peek(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]; + + return true; + } + + static bool peek() FORCE_INLINE + { + return (sm_rxBuf.head != sm_rxBuf.tail); + } private: using HardwareImpl = detail::Hardware