uart/uart.hpp

352 lines
6.7 KiB
C++
Raw Normal View History

#pragma once
2019-08-03 15:00:41 +02:00
#include <stdint.h>
2019-07-28 12:15:19 +02:00
#include "config.hpp"
#include "hardware0.hpp"
#include "hardware1.hpp"
#include "software.hpp"
2019-08-03 10:58:32 +02:00
#include "utils.hpp"
#include "../flash/flash.hpp"
#define FORCE_INLINE __attribute__((always_inline))
namespace uart {
2019-08-03 15:00:41 +02:00
namespace detail {
template <typename T, T limit, size_t Base>
static constexpr size_t cntDigits()
{
T num = limit;
size_t cnt = 0;
if (num < 0) {
num = -num;
++cnt;
}
do {
num /= 10;
++cnt;
} while (num > 0);
return cnt;
}
template <typename T, size_t Base>
static constexpr size_t maxNumDigits()
{
T minDigits = cntDigits<T, util::NumericLimits<T>::min(), Base>();
T maxDigits = cntDigits<T, util::NumericLimits<T>::max(), Base>();
return (minDigits < maxDigits) ? maxDigits : minDigits;
}
} // namespace detail
2019-07-28 12:15:19 +02:00
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)
{
2019-07-28 12:15:19 +02:00
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()
{
2019-07-28 12:15:19 +02:00
return Driver::peek();
}
static void txString(const char *str)
{
2019-07-28 12:15:19 +02:00
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)
{
2019-07-28 12:15:19 +02:00
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);
}
2019-08-03 15:00:41 +02:00
template <typename T, size_t Base = 10>
static inline void txNumber(const T &val)
{
static_assert(util::is_integral_v<T>, "Only supported on integral types");
2019-08-03 15:00:41 +02:00
constexpr size_t numDigits = detail::maxNumDigits<T, Base>();
data_t buffer[numDigits];
data_t *bufEnd = buffer + numDigits - 1;
2019-08-03 15:00:41 +02:00
T digits = val;
if (digits < 0) {
digits = -digits;
txByte('-');
}
do {
data_t lastDigit = digits % Base;
2019-08-03 15:00:41 +02:00
*bufEnd-- = '0' + lastDigit;
digits /= Base;
} while (digits > 0);
for (data_t *buf = bufEnd + 1; buf < buffer + numDigits; ++buf)
2019-08-03 15:00:41 +02:00
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<<(unsigned short &val)
{
txNumber(val);
return *this;
}
2019-08-03 15:00:41 +02:00
Uart &operator<<(const int &val)
{
2019-08-03 15:00:41 +02:00
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<<(unsigned long &val)
{
txNumber(val);
return *this;
}
Uart &operator<<(long long &val)
{
txNumber(val);
return *this;
}
Uart &operator<<(unsigned long long &val)
{
txNumber(val);
return *this;
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator<<(float) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator<<(double) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator<<(long double) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
Uart &operator<<(const bool &val)
{
txString(val ? F("true") : F("false"));
return *this;
}
Uart &operator<<(const void *val)
{
txNumber(reinterpret_cast<uint16_t>(val));
return *this;
}
//////////////////////////////////////////////////////////////////////////
// Input stream overloads
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(char &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(unsigned char &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(short &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(unsigned short &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(int &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(unsigned int &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(long &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(unsigned long &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(long long &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(unsigned long long &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(float &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(double &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(long double &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(bool &) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
2019-08-03 15:00:41 +02:00
Uart &operator>>(const void *&) const
{
2019-08-03 10:58:32 +02:00
static_assert(util::always_false_v<Ts...>, "Not implemented");
}
};
2019-07-28 17:58:23 +02:00
template <typename cfg = Config<>>
using Uart0 = Uart<Hardware0<Mode::ASYNCHRONOUS, cfg>>;
#ifdef HAS_UART1
template <typename cfg = Config<>>
using Uart1 = Uart<Hardware1<Mode::ASYNCHRONOUS, cfg>>;
#endif
} // namespace uart
#undef FORCE_INLINE
#undef HAS_UART1