390 lines
7.9 KiB
C++
390 lines
7.9 KiB
C++
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "config.hpp"
|
|
#include "software.hpp"
|
|
|
|
#include "hardware0.hpp"
|
|
#include "hardware1.hpp"
|
|
|
|
#include "../flash/flash.hpp"
|
|
|
|
#define FORCE_INLINE __attribute__((always_inline))
|
|
|
|
namespace uart {
|
|
|
|
namespace detail {
|
|
|
|
template <typename T, T Limit, size_t Base>
|
|
static constexpr size_t cntDigits()
|
|
{
|
|
T num = Limit;
|
|
size_t cnt = 0;
|
|
|
|
do {
|
|
num /= Base;
|
|
++cnt;
|
|
} while (num > 0);
|
|
|
|
return cnt;
|
|
}
|
|
|
|
template <typename T, size_t Base>
|
|
static constexpr size_t maxNumDigits()
|
|
{
|
|
constexpr T MinVal = util::numeric_limits<T>::min();
|
|
constexpr T MaxVal = util::numeric_limits<T>::max();
|
|
|
|
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
|
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
|
|
|
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits;
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
template <class Driver>
|
|
class Uart {
|
|
public:
|
|
using data_t = typename Driver::data_t;
|
|
|
|
// Constructing a uart object does not initialize the driver to allow different specializations with the same
|
|
// back-end to exists at the same time
|
|
// Note that init must be called every time when switching specializations with the same back-end
|
|
Uart() = default;
|
|
|
|
// Moving and copying uart objects is not supported
|
|
Uart(const Uart &) = delete;
|
|
Uart(Uart &&) = delete;
|
|
Uart &operator=(const Uart &) = delete;
|
|
Uart &operator=(Uart &&) = delete;
|
|
|
|
// Before using the uart init must be called
|
|
static void init()
|
|
{
|
|
Driver::init();
|
|
}
|
|
|
|
static void txByte(const data_t &byte)
|
|
{
|
|
Driver::txByte(byte);
|
|
}
|
|
|
|
static bool rxByte(data_t &byte)
|
|
{
|
|
return Driver::rxByte(byte);
|
|
}
|
|
|
|
static bool peek(data_t &byte)
|
|
{
|
|
return Driver::peek(byte);
|
|
}
|
|
|
|
static bool peek()
|
|
{
|
|
return Driver::peek();
|
|
}
|
|
|
|
static void flushTx()
|
|
{
|
|
Driver::flushTx();
|
|
}
|
|
|
|
static void txString(const char *str)
|
|
{
|
|
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
|
|
|
while (char ch = *str++)
|
|
txByte(ch);
|
|
}
|
|
|
|
static void txString(const ::detail::FlashString *str)
|
|
{
|
|
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
|
|
|
const char *strIt = reinterpret_cast<const char *>(str);
|
|
|
|
while (char ch = pgm_read_byte(strIt++))
|
|
txByte(ch);
|
|
}
|
|
|
|
template <typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
|
|
static void txNumber(const T &val)
|
|
{
|
|
static_assert(util::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 <= 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");
|
|
|
|
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
|
constexpr size_t NumDigits = detail::maxNumDigits<T, Base>();
|
|
|
|
T digits = val;
|
|
|
|
if (digits < 0) {
|
|
digits = -digits;
|
|
txByte('-');
|
|
}
|
|
|
|
data_t buffer[NumDigits];
|
|
data_t *bufEnd = buffer + NumDigits - 1;
|
|
|
|
do {
|
|
const data_t lastDigit = digits % Base;
|
|
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (AlphaChar + lastDigit - 10);
|
|
digits /= Base;
|
|
} while (digits > 0);
|
|
|
|
if (Padding > 0) {
|
|
size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
|
|
|
if (Padding > strLen) {
|
|
for (size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
|
*bufEnd-- = PadChar;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (data_t *buf = bufEnd + 1; buf < buffer + NumDigits; ++buf)
|
|
txByte(*buf);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Output stream overloads
|
|
|
|
Uart &operator<<(const char *str)
|
|
{
|
|
txString(str);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const ::detail::FlashString *str)
|
|
{
|
|
txString(str);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const char &val)
|
|
{
|
|
txByte(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const signed char &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const unsigned char &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const short &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const unsigned short &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const int &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const unsigned int &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const long &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const unsigned long &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const long long &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const unsigned long long &val)
|
|
{
|
|
txNumber(val);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(float) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(double) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(long double) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
Uart &operator<<(const bool &val)
|
|
{
|
|
txString(val ? F("true") : F("false"));
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const void *val)
|
|
{
|
|
txString(F("0x"));
|
|
txNumber<uint16_t, 16, 4, '0', false>(reinterpret_cast<uint16_t>(val));
|
|
return *this;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Input stream overloads
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(char &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned char &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(short &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned short &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(int &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned int &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned long &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long long &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned long long &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(float &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(double &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long double &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(bool &) const
|
|
{
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(const void *&) const
|
|
{
|
|
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
|
|
|
|
static void rxIntHandler() FORCE_INLINE
|
|
{
|
|
Driver::rxIntHandler();
|
|
}
|
|
|
|
static void dataRegEmptyIntHandler() FORCE_INLINE
|
|
{
|
|
Driver::dataRegEmptyIntHandler();
|
|
}
|
|
};
|
|
|
|
template <typename cfg = Config<>>
|
|
using Uart0 = Uart<Hardware0<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
|
|
|
#ifdef HAS_UART1
|
|
template <typename cfg = Config<>>
|
|
using Uart1 = Uart<Hardware1<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
|
#endif
|
|
|
|
} // namespace uart
|
|
|
|
#undef FORCE_INLINE
|
|
#undef HAS_UART1
|