Compare commits
9 Commits
41b9ef74f9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 419b86999d | |||
| a5f8e8e3d7 | |||
| 119de32445 | |||
| 8f88cdccea | |||
| dfb076cda8 | |||
| 6d9ef6e4be | |||
| bcd18db494 | |||
| 04b6782ec4 | |||
| ae03c8d43e |
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
@@ -27,17 +27,17 @@ namespace detail {
|
|||||||
|
|
||||||
template <DataBits dataBits>
|
template <DataBits dataBits>
|
||||||
struct choose_data_type {
|
struct choose_data_type {
|
||||||
using type = uint8_t;
|
using type = std::uint8_t;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct choose_data_type<DataBits::NINE> {
|
struct choose_data_type<DataBits::NINE> {
|
||||||
using type = uint16_t;
|
using type = std::uint16_t;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE,
|
template <std::uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE,
|
||||||
StopBits stopBits = StopBits::ONE>
|
StopBits stopBits = StopBits::ONE>
|
||||||
struct Config {
|
struct Config {
|
||||||
static constexpr auto BAUD_RATE = baudRate;
|
static constexpr auto BAUD_RATE = baudRate;
|
||||||
|
|||||||
158
hardware.hpp
158
hardware.hpp
@@ -2,17 +2,13 @@
|
|||||||
|
|
||||||
#include "../clock.hpp"
|
#include "../clock.hpp"
|
||||||
|
|
||||||
#include "../type/type.hpp"
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
enum class Mode {
|
enum class Mode {
|
||||||
ASYNCHRONOUS,
|
ASYNCHRONOUS,
|
||||||
ASYNCHRONOUS_2X,
|
|
||||||
SYNCHRONOUS_MASTER,
|
SYNCHRONOUS_MASTER,
|
||||||
SYNCHRONOUS_SLAVE,
|
SYNCHRONOUS_SLAVE,
|
||||||
SPI,
|
SPI,
|
||||||
@@ -25,18 +21,18 @@ enum class Driven {
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
using reg_ptr_t = volatile uint8_t *;
|
using reg_ptr_t = volatile std::uint8_t *;
|
||||||
|
|
||||||
template <uintptr_t Address>
|
template <std::uintptr_t Address>
|
||||||
static inline reg_ptr_t getRegPtr()
|
static inline reg_ptr_t getRegPtr()
|
||||||
{
|
{
|
||||||
return reinterpret_cast<reg_ptr_t>(Address);
|
return reinterpret_cast<reg_ptr_t>(Address);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename data_t, uint8_t Size>
|
template <typename data_t, std::uint8_t Size>
|
||||||
struct RingBuffer {
|
struct RingBuffer {
|
||||||
uint8_t head;
|
std::uint8_t head;
|
||||||
uint8_t tail;
|
std::uint8_t tail;
|
||||||
data_t buf[Size];
|
data_t buf[Size];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,12 +40,17 @@ template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename Ct
|
|||||||
Mode mode>
|
Mode mode>
|
||||||
class Hardware {
|
class Hardware {
|
||||||
public:
|
public:
|
||||||
static void init() FORCE_INLINE
|
[[gnu::always_inline]] static void init()
|
||||||
{
|
{
|
||||||
constexpr auto BaudVal = calcBaud();
|
constexpr auto AbsDoubleError = std::fabs(calcBaudError<true>());
|
||||||
|
constexpr auto AbsNormalError = std::fabs(calcBaudError<false>());
|
||||||
|
static_assert(AbsDoubleError <= 3.0 || AbsNormalError <= 3.0, "Baud rate error over 3%, probably unusable");
|
||||||
|
|
||||||
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<uint8_t>(BaudVal >> 8);
|
constexpr auto UseDoubleSpeed = (AbsDoubleError < AbsNormalError);
|
||||||
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(BaudVal);
|
constexpr auto BaudVal = calcBaudVal<UseDoubleSpeed>();
|
||||||
|
|
||||||
|
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<std::uint8_t>(BaudVal >> 8);
|
||||||
|
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<std::uint8_t>(BaudVal);
|
||||||
|
|
||||||
constexpr auto DataBitsValues = calcDataBits();
|
constexpr auto DataBitsValues = calcDataBits();
|
||||||
constexpr auto ParityVal = calcParity();
|
constexpr auto ParityVal = calcParity();
|
||||||
@@ -59,14 +60,21 @@ class Hardware {
|
|||||||
constexpr auto EnableTx = calcTxState<true>();
|
constexpr auto EnableTx = calcTxState<true>();
|
||||||
constexpr auto InterruptVal = calcInterrupt();
|
constexpr auto InterruptVal = calcInterrupt();
|
||||||
|
|
||||||
constexpr uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
|
constexpr std::uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
|
||||||
constexpr uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal;
|
constexpr std::uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal;
|
||||||
|
|
||||||
|
auto ctrlStatRegA = getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>();
|
||||||
|
|
||||||
|
if constexpr (UseDoubleSpeed)
|
||||||
|
*ctrlStatRegA = *ctrlStatRegA | (1 << CtrlFlagsA::SPEED_2X);
|
||||||
|
else
|
||||||
|
*ctrlStatRegA = *ctrlStatRegA & ~(1 << CtrlFlagsA::SPEED_2X);
|
||||||
|
|
||||||
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB;
|
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB;
|
||||||
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC;
|
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rxByteBlocking(typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static bool rxByteBlocking(typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||||
byte = *getRegPtr<Registers::IO_REG_ADDR>();
|
byte = *getRegPtr<Registers::IO_REG_ADDR>();
|
||||||
@@ -76,27 +84,27 @@ class Hardware {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static typename cfg::data_t rxByteInterrupt() FORCE_INLINE
|
[[gnu::always_inline]] static typename cfg::data_t rxByteInterrupt()
|
||||||
{
|
{
|
||||||
return *getRegPtr<Registers::IO_REG_ADDR>();
|
return *getRegPtr<Registers::IO_REG_ADDR>();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool txEmpty() FORCE_INLINE
|
[[gnu::always_inline]] static bool txEmpty()
|
||||||
{
|
{
|
||||||
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::DATA_REG_EMPTY);
|
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::DATA_REG_EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool txComplete() FORCE_INLINE
|
[[gnu::always_inline]] static bool txComplete()
|
||||||
{
|
{
|
||||||
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clearTxComplete() FORCE_INLINE
|
[[gnu::always_inline]] static void clearTxComplete()
|
||||||
{
|
{
|
||||||
*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() |= (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() |= (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByteBlocking(const typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByteBlocking(const typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
while (!txEmpty())
|
while (!txEmpty())
|
||||||
;
|
;
|
||||||
@@ -104,12 +112,12 @@ class Hardware {
|
|||||||
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByteInterrupt(volatile const typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByteInterrupt(volatile const typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peekBlocking() FORCE_INLINE
|
[[gnu::always_inline]] static bool peekBlocking()
|
||||||
{
|
{
|
||||||
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -118,29 +126,57 @@ class Hardware {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void enableDataRegEmptyInt() FORCE_INLINE
|
[[gnu::always_inline]] static void enableDataRegEmptyInt()
|
||||||
{
|
{
|
||||||
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() |= (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||||
|
*ctrlStatRegB = *ctrlStatRegB | (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void disableDataRegEmptyInt() FORCE_INLINE
|
[[gnu::always_inline]] static void disableDataRegEmptyInt()
|
||||||
{
|
{
|
||||||
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() &= ~(1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||||
|
*ctrlStatRegB = *ctrlStatRegB & ~(1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct DataBitsVal {
|
struct DataBitsVal {
|
||||||
uint8_t regCVal = 0;
|
std::uint8_t regCVal = 0;
|
||||||
uint8_t regBVal = 0;
|
std::uint8_t regBVal = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr auto calcBaud()
|
template <bool DoubleSpeed = true>
|
||||||
|
static constexpr auto calcBaudVal()
|
||||||
{
|
{
|
||||||
// The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly
|
if constexpr (DoubleSpeed) {
|
||||||
constexpr auto BaudVal = (F_CPU + 8 * cfg::BAUD_RATE) / (16 * cfg::BAUD_RATE) - 1;
|
constexpr auto BaudVal = static_cast<std::uint16_t>(round(F_CPU / (8.0 * cfg::BAUD_RATE) - 1));
|
||||||
return BaudVal;
|
return BaudVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr auto BaudVal = static_cast<std::uint16_t>(round(F_CPU / (16.0 * cfg::BAUD_RATE) - 1));
|
||||||
|
return BaudVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::uint16_t BaudVal, bool DoubleSpeed = true>
|
||||||
|
static constexpr auto calcBaudRate()
|
||||||
|
{
|
||||||
|
if constexpr (DoubleSpeed) {
|
||||||
|
constexpr auto BaudRate = static_cast<std::uint32_t>(round(F_CPU / (8.0 * (BaudVal + 1))));
|
||||||
|
return BaudRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto BaudRate = static_cast<std::uint32_t>(round(F_CPU / (16.0 * (BaudVal + 1))));
|
||||||
|
return BaudRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <bool DoubleSpeed = true>
|
||||||
|
static constexpr auto calcBaudError()
|
||||||
|
{
|
||||||
|
constexpr auto BaudVal = calcBaudVal<DoubleSpeed>();
|
||||||
|
constexpr auto ClosestBaudRate = calcBaudRate<BaudVal, DoubleSpeed>();
|
||||||
|
constexpr auto BaudError = (static_cast<double>(ClosestBaudRate) / cfg::BAUD_RATE - 1) * 100;
|
||||||
|
return BaudError;
|
||||||
|
}
|
||||||
|
|
||||||
static constexpr auto calcDataBits()
|
static constexpr auto calcDataBits()
|
||||||
{
|
{
|
||||||
DataBitsVal dataBitsVal;
|
DataBitsVal dataBitsVal;
|
||||||
@@ -169,7 +205,7 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcParity()
|
static constexpr auto calcParity()
|
||||||
{
|
{
|
||||||
uint8_t parityVal = 0;
|
std::uint8_t parityVal = 0;
|
||||||
|
|
||||||
if (cfg::PARITY == Parity::EVEN)
|
if (cfg::PARITY == Parity::EVEN)
|
||||||
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1);
|
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1);
|
||||||
@@ -181,7 +217,7 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcStopBits()
|
static constexpr auto calcStopBits()
|
||||||
{
|
{
|
||||||
uint8_t stopBitsVal = 0;
|
std::uint8_t stopBitsVal = 0;
|
||||||
|
|
||||||
if (cfg::STOP_BITS == StopBits::TWO)
|
if (cfg::STOP_BITS == StopBits::TWO)
|
||||||
stopBitsVal = (1 << CtrlFlagsC::STOP_BIT_SEL);
|
stopBitsVal = (1 << CtrlFlagsC::STOP_BIT_SEL);
|
||||||
@@ -193,7 +229,7 @@ class Hardware {
|
|||||||
{
|
{
|
||||||
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
||||||
|
|
||||||
uint8_t modeVal = 0;
|
std::uint8_t modeVal = 0;
|
||||||
|
|
||||||
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
||||||
modeVal = (1 << CtrlFlagsC::MODE_SEL_0);
|
modeVal = (1 << CtrlFlagsC::MODE_SEL_0);
|
||||||
@@ -205,7 +241,7 @@ class Hardware {
|
|||||||
template <bool Enable>
|
template <bool Enable>
|
||||||
static constexpr auto calcRxState()
|
static constexpr auto calcRxState()
|
||||||
{
|
{
|
||||||
uint8_t enableVal = 0;
|
std::uint8_t enableVal = 0;
|
||||||
|
|
||||||
if (Enable)
|
if (Enable)
|
||||||
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
||||||
@@ -216,7 +252,7 @@ class Hardware {
|
|||||||
template <bool Enable>
|
template <bool Enable>
|
||||||
static constexpr auto calcTxState()
|
static constexpr auto calcTxState()
|
||||||
{
|
{
|
||||||
uint8_t enableVal = 0;
|
std::uint8_t enableVal = 0;
|
||||||
|
|
||||||
if (Enable)
|
if (Enable)
|
||||||
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
||||||
@@ -226,7 +262,7 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcInterrupt()
|
static constexpr auto calcInterrupt()
|
||||||
{
|
{
|
||||||
uint8_t interruptVal = 0;
|
std::uint8_t interruptVal = 0;
|
||||||
|
|
||||||
if (driven == Driven::INTERRUPT)
|
if (driven == Driven::INTERRUPT)
|
||||||
interruptVal = (1 << CtrlFlagsB::RX_INT_ENABLE);
|
interruptVal = (1 << CtrlFlagsB::RX_INT_ENABLE);
|
||||||
@@ -241,33 +277,33 @@ class BlockingHardware {
|
|||||||
using data_t = typename cfg::data_t;
|
using data_t = typename cfg::data_t;
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||||
|
|
||||||
static void init() FORCE_INLINE
|
[[gnu::always_inline]] static void init()
|
||||||
{
|
{
|
||||||
HardwareImpl::init();
|
HardwareImpl::init();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByte(const data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByte(const data_t &byte)
|
||||||
{
|
{
|
||||||
HardwareImpl::txByteBlocking(byte);
|
HardwareImpl::txByteBlocking(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static bool rxByte(data_t &byte)
|
||||||
{
|
{
|
||||||
return HardwareImpl::rxByteBlocking(byte);
|
return HardwareImpl::rxByteBlocking(byte);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peek(data_t &) FORCE_INLINE
|
[[gnu::always_inline]] static bool peek(data_t &)
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<data_t>, "Peek with data is not supported in blocking mode");
|
static_assert(util::always_false_v<data_t>, "Peek with data is not supported in blocking mode");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
[[gnu::always_inline]] static bool peek()
|
||||||
{
|
{
|
||||||
return HardwareImpl::peekBlocking();
|
return HardwareImpl::peekBlocking();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
[[gnu::always_inline]] static void flushTx()
|
||||||
{
|
{
|
||||||
while (!HardwareImpl::txEmpty())
|
while (!HardwareImpl::txEmpty())
|
||||||
;
|
;
|
||||||
@@ -286,9 +322,9 @@ class InterruptHardware {
|
|||||||
using data_t = typename cfg::data_t;
|
using data_t = typename cfg::data_t;
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||||
|
|
||||||
static void txByte(const data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByte(const data_t &byte)
|
||||||
{
|
{
|
||||||
uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
|
std::uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
|
||||||
while (tmpHead == sm_txBuf.tail)
|
while (tmpHead == sm_txBuf.tail)
|
||||||
;
|
;
|
||||||
|
|
||||||
@@ -298,35 +334,35 @@ class InterruptHardware {
|
|||||||
HardwareImpl::enableDataRegEmptyInt();
|
HardwareImpl::enableDataRegEmptyInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static bool rxByte(data_t &byte)
|
||||||
{
|
{
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
if (sm_rxBuf.head == sm_rxBuf.tail)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
std::uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
byte = sm_rxBuf.buf[tmpTail];
|
||||||
sm_rxBuf.tail = tmpTail;
|
sm_rxBuf.tail = tmpTail;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peek(data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static bool peek(data_t &byte)
|
||||||
{
|
{
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
if (sm_rxBuf.head == sm_rxBuf.tail)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
std::uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
byte = sm_rxBuf.buf[tmpTail];
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
[[gnu::always_inline]] static bool peek()
|
||||||
{
|
{
|
||||||
return (sm_rxBuf.head != sm_rxBuf.tail);
|
return (sm_rxBuf.head != sm_rxBuf.tail);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
[[gnu::always_inline]] static void flushTx()
|
||||||
{
|
{
|
||||||
while (sm_txBuf.head != sm_txBuf.tail)
|
while (sm_txBuf.head != sm_txBuf.tail)
|
||||||
;
|
;
|
||||||
@@ -338,11 +374,11 @@ class InterruptHardware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void rxIntHandler() FORCE_INLINE
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
{
|
{
|
||||||
auto data = HardwareImpl::rxByteInterrupt();
|
const auto data = HardwareImpl::rxByteInterrupt();
|
||||||
|
|
||||||
uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE;
|
const std::uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE;
|
||||||
|
|
||||||
if (tmpHead != sm_rxBuf.tail) {
|
if (tmpHead != sm_rxBuf.tail) {
|
||||||
sm_rxBuf.head = tmpHead;
|
sm_rxBuf.head = tmpHead;
|
||||||
@@ -352,10 +388,10 @@ class InterruptHardware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dataRegEmptyIntHandler() FORCE_INLINE
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
{
|
{
|
||||||
if (sm_txBuf.head != sm_txBuf.tail) {
|
if (sm_txBuf.head != sm_txBuf.tail) {
|
||||||
uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
|
const std::uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
|
||||||
sm_txBuf.tail = tmpTail;
|
sm_txBuf.tail = tmpTail;
|
||||||
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
|
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
|
||||||
} else
|
} else
|
||||||
@@ -385,5 +421,3 @@ volatile RingBuffer<typename InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB
|
|||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
|
|||||||
115
hardware0.hpp
115
hardware0.hpp
@@ -1,7 +1,6 @@
|
|||||||
#ifndef UART_HARDWARE_0_HPP
|
#pragma once
|
||||||
#define UART_HARDWARE_0_HPP
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
@@ -10,8 +9,6 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "hardware.hpp"
|
#include "hardware.hpp"
|
||||||
|
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@@ -33,12 +30,12 @@ The workaround therefore is to disable the pointer cast and dereference macro _M
|
|||||||
#define _MMIO_BYTE
|
#define _MMIO_BYTE
|
||||||
|
|
||||||
struct Registers0 {
|
struct Registers0 {
|
||||||
static constexpr uintptr_t IO_REG_ADDR = UDR0;
|
static constexpr std::uintptr_t IO_REG_ADDR = UDR0;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_A_ADDR = UCSR0A;
|
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR0A;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_B_ADDR = UCSR0B;
|
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR0B;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_C_ADDR = UCSR0C;
|
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR0C;
|
||||||
static constexpr uintptr_t BAUD_REG_L_ADDR = UBRR0L;
|
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR0L;
|
||||||
static constexpr uintptr_t BAUD_REG_H_ADDR = UBRR0H;
|
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR0H;
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma pop_macro("_MMIO_BYTE")
|
#pragma pop_macro("_MMIO_BYTE")
|
||||||
@@ -82,8 +79,10 @@ constexpr int operator<<(const int &lhs, const ControlFlagsB0 &rhs) { return lhs
|
|||||||
constexpr int operator<<(const int &lhs, const ControlFlagsC0 &rhs) { return lhs << static_cast<int>(rhs); }
|
constexpr int operator<<(const int &lhs, const ControlFlagsC0 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
extern void (*fnRx0IntHandler)();
|
#if defined(__AVR_ATmega328P__)
|
||||||
extern void (*fnDataReg0EmptyIntHandler)();
|
#define USART0_RX_vect USART_RX_vect
|
||||||
|
#define USART0_UDRE_vect USART_UDRE_vect
|
||||||
|
#endif
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "This chip is not supported"
|
#error "This chip is not supported"
|
||||||
@@ -100,70 +99,54 @@ template <class cfg, Mode mode>
|
|||||||
class Hardware0<cfg, Driven::INTERRUPT, mode>
|
class Hardware0<cfg, Driven::INTERRUPT, mode>
|
||||||
: public detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
: public detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||||
detail::ControlFlagsC0, cfg, mode> {
|
detail::ControlFlagsC0, cfg, mode> {
|
||||||
using detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
public:
|
||||||
detail::ControlFlagsC0, cfg, mode>::rxIntHandler;
|
[[gnu::always_inline]] static void init()
|
||||||
|
{
|
||||||
using detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
HardwareImpl::init();
|
||||||
detail::ControlFlagsC0, cfg, mode>::dataRegEmptyIntHandler;
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||||
detail::ControlFlagsC0, cfg, Driven::INTERRUPT, mode>;
|
detail::ControlFlagsC0, cfg, Driven::INTERRUPT, mode>;
|
||||||
|
|
||||||
public:
|
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0,
|
||||||
static void init() FORCE_INLINE
|
detail::ControlFlagsB0, detail::ControlFlagsC0, cfg, mode>;
|
||||||
{
|
|
||||||
detail::fnRx0IntHandler = rxIntHandler;
|
|
||||||
detail::fnDataReg0EmptyIntHandler = dataRegEmptyIntHandler;
|
|
||||||
|
|
||||||
HardwareImpl::init();
|
// Must be friends with Uart interface to call these private handlers
|
||||||
sei();
|
template <class Driver>
|
||||||
|
friend class Uart;
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
|
{
|
||||||
|
InterruptHardwareImpl::rxIntHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
|
{
|
||||||
|
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef UART0_INT_VECTORS
|
// Forward declare interrupt functions to allow adding them as friends
|
||||||
|
extern "C" {
|
||||||
#include <avr/interrupt.h>
|
void USART0_RX_vect() __attribute__((signal));
|
||||||
|
void USART0_UDRE_vect() __attribute__((signal));
|
||||||
namespace uart {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega328P__)
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega328P__)
|
|
||||||
#define USART0_RX_vect USART_RX_vect
|
|
||||||
#define USART0_UDRE_vect USART_UDRE_vect
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void (*fnRx0IntHandler)() = nullptr;
|
|
||||||
void (*fnDataReg0EmptyIntHandler)() = nullptr;
|
|
||||||
|
|
||||||
ISR(USART0_RX_vect)
|
|
||||||
{
|
|
||||||
if (fnRx0IntHandler)
|
|
||||||
fnRx0IntHandler();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART0_UDRE_vect)
|
// clang-format off
|
||||||
{
|
#define REGISTER_UART0_INT_VECTORS(uart_type) \
|
||||||
if (fnDataReg0EmptyIntHandler)
|
ISR(USART0_RX_vect) \
|
||||||
fnDataReg0EmptyIntHandler();
|
{ \
|
||||||
}
|
uart_type::rxIntHandler(); \
|
||||||
|
} \
|
||||||
#else
|
ISR(USART0_UDRE_vect) \
|
||||||
#error "This chip is not supported"
|
{ \
|
||||||
#endif
|
uart_type::dataRegEmptyIntHandler(); \
|
||||||
|
} \
|
||||||
} // namespace detail
|
struct _##uart_type {}
|
||||||
} // namespace uart
|
// clang-format on
|
||||||
|
|
||||||
#undef UART0_INT_VECTORS
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
103
hardware1.hpp
103
hardware1.hpp
@@ -1,7 +1,6 @@
|
|||||||
#ifndef UART_HARDWARE_1_HPP
|
#pragma once
|
||||||
#define UART_HARDWARE_1_HPP
|
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
@@ -10,8 +9,6 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "hardware.hpp"
|
#include "hardware.hpp"
|
||||||
|
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@@ -33,12 +30,12 @@ The workaround therefore is to disable the pointer cast and dereference macro _M
|
|||||||
#define _MMIO_BYTE
|
#define _MMIO_BYTE
|
||||||
|
|
||||||
struct Registers1 {
|
struct Registers1 {
|
||||||
static constexpr uintptr_t IO_REG_ADDR = UDR1;
|
static constexpr std::uintptr_t IO_REG_ADDR = UDR1;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_A_ADDR = UCSR1A;
|
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR1A;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_B_ADDR = UCSR1B;
|
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR1B;
|
||||||
static constexpr uintptr_t CTRL_STAT_REG_C_ADDR = UCSR1C;
|
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR1C;
|
||||||
static constexpr uintptr_t BAUD_REG_L_ADDR = UBRR1L;
|
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR1L;
|
||||||
static constexpr uintptr_t BAUD_REG_H_ADDR = UBRR1H;
|
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR1H;
|
||||||
};
|
};
|
||||||
|
|
||||||
#pragma pop_macro("_MMIO_BYTE")
|
#pragma pop_macro("_MMIO_BYTE")
|
||||||
@@ -82,9 +79,6 @@ constexpr int operator<<(const int &lhs, const ControlFlagsB1 &rhs) { return lhs
|
|||||||
constexpr int operator<<(const int &lhs, const ControlFlagsC1 &rhs) { return lhs << static_cast<int>(rhs); }
|
constexpr int operator<<(const int &lhs, const ControlFlagsC1 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
extern void (*fnRx1IntHandler)();
|
|
||||||
extern void (*fnDataReg1EmptyIntHandler)();
|
|
||||||
|
|
||||||
#define HAS_UART1
|
#define HAS_UART1
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -102,23 +96,32 @@ template <class cfg, Mode mode>
|
|||||||
class Hardware1<cfg, Driven::INTERRUPT, mode>
|
class Hardware1<cfg, Driven::INTERRUPT, mode>
|
||||||
: public detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
: public detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||||
detail::ControlFlagsC1, cfg, mode> {
|
detail::ControlFlagsC1, cfg, mode> {
|
||||||
using detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
public:
|
||||||
detail::ControlFlagsC1, cfg, mode>::rxIntHandler;
|
[[gnu::always_inline]] static void init()
|
||||||
|
{
|
||||||
using detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
HardwareImpl::init();
|
||||||
detail::ControlFlagsC1, cfg, mode>::dataRegEmptyIntHandler;
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||||
detail::ControlFlagsC1, cfg, Driven::INTERRUPT, mode>;
|
detail::ControlFlagsC1, cfg, Driven::INTERRUPT, mode>;
|
||||||
|
|
||||||
public:
|
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1,
|
||||||
static void init() FORCE_INLINE
|
detail::ControlFlagsB1, detail::ControlFlagsC1, cfg, mode>;
|
||||||
{
|
|
||||||
detail::fnRx1IntHandler = rxIntHandler;
|
|
||||||
detail::fnDataReg1EmptyIntHandler = dataRegEmptyIntHandler;
|
|
||||||
|
|
||||||
HardwareImpl::init();
|
// Must be friends with Uart interface to call these private handlers
|
||||||
sei();
|
template <class Driver>
|
||||||
|
friend class Uart;
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
|
{
|
||||||
|
InterruptHardwareImpl::rxIntHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
|
{
|
||||||
|
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -126,41 +129,29 @@ class Hardware1<cfg, Driven::INTERRUPT, mode>
|
|||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef UART1_INT_VECTORS
|
#ifdef HAS_UART1
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
// Forward declare interrupt functions to allow adding them as friends
|
||||||
|
extern "C" {
|
||||||
namespace uart {
|
void USART1_RX_vect() __attribute__((signal));
|
||||||
namespace detail {
|
void USART1_UDRE_vect() __attribute__((signal));
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__)
|
|
||||||
|
|
||||||
void (*fnRx1IntHandler)() = nullptr;
|
|
||||||
void (*fnDataReg1EmptyIntHandler)() = nullptr;
|
|
||||||
|
|
||||||
ISR(USART1_RX_vect)
|
|
||||||
{
|
|
||||||
if (fnRx1IntHandler)
|
|
||||||
fnRx1IntHandler();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART1_UDRE_vect)
|
// clang-format off
|
||||||
{
|
#define REGISTER_UART1_INT_VECTORS(uart_type) \
|
||||||
if (fnDataReg1EmptyIntHandler)
|
ISR(USART1_RX_vect) \
|
||||||
fnDataReg1EmptyIntHandler();
|
{ \
|
||||||
}
|
uart_type::rxIntHandler(); \
|
||||||
|
} \
|
||||||
|
ISR(USART1_UDRE_vect) \
|
||||||
|
{ \
|
||||||
|
uart_type::dataRegEmptyIntHandler(); \
|
||||||
|
} \
|
||||||
|
struct _##uart_type { \
|
||||||
|
}
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
} // namespace uart
|
|
||||||
|
|
||||||
#undef UART1_INT_VECTORS
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
#include "../io/io.hpp"
|
#include "../io/io.hpp"
|
||||||
#include "../type/type.hpp"
|
#include "../util/util.hpp"
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
template <io::P rxPin, io::P txPin, class cfg = Config<>>
|
template <io::P rxPin, io::P txPin, class cfg = Config<>>
|
||||||
class Software {
|
class Software {
|
||||||
static_assert(type::always_false_v<cfg>, "Not implemented");
|
static_assert(util::always_false_v<cfg>, "Not implemented");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using data_t = typename cfg::data_t;
|
using data_t = typename cfg::data_t;
|
||||||
|
|||||||
91
uart.hpp
91
uart.hpp
@@ -1,28 +1,27 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <limits>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "software.hpp"
|
#include "software.hpp"
|
||||||
|
|
||||||
#undef UART0_INT_VECTORS
|
|
||||||
#include "hardware0.hpp"
|
#include "hardware0.hpp"
|
||||||
#undef UART1_INT_VECTORS
|
|
||||||
#include "hardware1.hpp"
|
#include "hardware1.hpp"
|
||||||
|
|
||||||
#include "../flash/flash.hpp"
|
#include "../flash/flash.hpp"
|
||||||
|
#include "../util/util.hpp"
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template <typename T, T Limit, size_t Base>
|
template <typename T, T Limit, std::size_t Base>
|
||||||
static constexpr size_t cntDigits()
|
static constexpr std::size_t cntDigits()
|
||||||
{
|
{
|
||||||
T num = Limit;
|
T num = Limit;
|
||||||
size_t cnt = 0;
|
std::size_t cnt = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
num /= Base;
|
num /= Base;
|
||||||
@@ -32,11 +31,11 @@ static constexpr size_t cntDigits()
|
|||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t Base>
|
template <typename T, std::size_t Base>
|
||||||
static constexpr size_t maxNumDigits()
|
static constexpr std::size_t maxNumDigits()
|
||||||
{
|
{
|
||||||
constexpr T MinVal = type::NumericLimits<T>::min();
|
constexpr T MinVal = std::numeric_limits<T>::min();
|
||||||
constexpr T MaxVal = type::NumericLimits<T>::max();
|
constexpr T MaxVal = std::numeric_limits<T>::max();
|
||||||
|
|
||||||
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
||||||
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
||||||
@@ -111,16 +110,16 @@ class Uart {
|
|||||||
txByte(ch);
|
txByte(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
|
template <typename T, std::size_t Base = 10, std::size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
|
||||||
static void txNumber(const T &val)
|
static void txNumber(const T &val)
|
||||||
{
|
{
|
||||||
static_assert(type::is_integral_v<T>, "Only supported on integral types");
|
static_assert(std::is_integral_v<T>, "Only supported on integral types");
|
||||||
static_assert(Base >= 2, "Numbers with base less than 2 make no sense");
|
static_assert(Base >= 2, "Numbers with base less than 2 make no sense");
|
||||||
static_assert(Base <= 16, "Numbers with base higher than 16 are not supported");
|
static_assert(Base <= 16, "Numbers with base higher than 16 are not supported");
|
||||||
static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number");
|
static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number");
|
||||||
|
|
||||||
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
||||||
constexpr size_t NumDigits = detail::maxNumDigits<T, Base>();
|
constexpr std::size_t NumDigits = detail::maxNumDigits<T, Base>();
|
||||||
|
|
||||||
T digits = val;
|
T digits = val;
|
||||||
|
|
||||||
@@ -139,10 +138,10 @@ class Uart {
|
|||||||
} while (digits > 0);
|
} while (digits > 0);
|
||||||
|
|
||||||
if (Padding > 0) {
|
if (Padding > 0) {
|
||||||
size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
std::size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
||||||
|
|
||||||
if (Padding > strLen) {
|
if (Padding > strLen) {
|
||||||
for (size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
for (std::size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
||||||
*bufEnd-- = PadChar;
|
*bufEnd-- = PadChar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -236,19 +235,19 @@ class Uart {
|
|||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator<<(float) const
|
Uart &operator<<(float) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator<<(double) const
|
Uart &operator<<(double) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator<<(long double) const
|
Uart &operator<<(long double) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(const bool &val)
|
Uart &operator<<(const bool &val)
|
||||||
@@ -260,7 +259,7 @@ class Uart {
|
|||||||
Uart &operator<<(const void *val)
|
Uart &operator<<(const void *val)
|
||||||
{
|
{
|
||||||
txString(F("0x"));
|
txString(F("0x"));
|
||||||
txNumber<uint16_t, 16, 4, '0', false>(reinterpret_cast<uint16_t>(val));
|
txNumber<std::uint16_t, 16, 4, '0', false>(reinterpret_cast<std::uint16_t>(val));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,91 +269,110 @@ class Uart {
|
|||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(char &) const
|
Uart &operator>>(char &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(unsigned char &) const
|
Uart &operator>>(unsigned char &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(short &) const
|
Uart &operator>>(short &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(unsigned short &) const
|
Uart &operator>>(unsigned short &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(int &) const
|
Uart &operator>>(int &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(unsigned int &) const
|
Uart &operator>>(unsigned int &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(long &) const
|
Uart &operator>>(long &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(unsigned long &) const
|
Uart &operator>>(unsigned long &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(long long &) const
|
Uart &operator>>(long long &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(unsigned long long &) const
|
Uart &operator>>(unsigned long long &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(float &) const
|
Uart &operator>>(float &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(double &) const
|
Uart &operator>>(double &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(long double &) const
|
Uart &operator>>(long double &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(bool &) const
|
Uart &operator>>(bool &) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
Uart &operator>>(const void *&) const
|
Uart &operator>>(const void *&) const
|
||||||
{
|
{
|
||||||
static_assert(type::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend void ::USART0_RX_vect();
|
||||||
|
friend void ::USART0_UDRE_vect();
|
||||||
|
|
||||||
|
#ifdef HAS_UART1
|
||||||
|
friend void ::USART1_RX_vect();
|
||||||
|
friend void ::USART1_UDRE_vect();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
|
{
|
||||||
|
Driver::rxIntHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
|
{
|
||||||
|
Driver::dataRegEmptyIntHandler();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -368,5 +386,4 @@ using Uart1 = Uart<Hardware1<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
|||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
#undef HAS_UART1
|
#undef HAS_UART1
|
||||||
|
|||||||
Reference in New Issue
Block a user