287 lines
6.0 KiB
C++
287 lines
6.0 KiB
C++
#pragma once
|
|
|
|
#include "config.hpp"
|
|
#include "hardware0.hpp"
|
|
#include "hardware1.hpp"
|
|
#include "software.hpp"
|
|
|
|
#include "../flash/flash.hpp"
|
|
|
|
#define FORCE_INLINE __attribute__((always_inline))
|
|
|
|
namespace uart {
|
|
|
|
namespace detail {
|
|
|
|
template <typename...>
|
|
struct always_false {
|
|
static constexpr auto value = false;
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
template <class Driver>
|
|
class Uart {
|
|
public:
|
|
// Initialization is done upon construction
|
|
Uart()
|
|
{
|
|
Driver::init();
|
|
}
|
|
|
|
// Moving and copying uart objects is not supported
|
|
Uart(const Uart &) = delete;
|
|
Uart(Uart &&) = delete;
|
|
Uart &operator=(const Uart &) = delete;
|
|
Uart &operator=(Uart &&) = delete;
|
|
|
|
static void txByte(const typename Driver::data_t &byte)
|
|
{
|
|
Driver::txByte(byte);
|
|
}
|
|
|
|
static bool rxByte(typename Driver::data_t &byte)
|
|
{
|
|
return Driver::rxByte(byte);
|
|
}
|
|
|
|
static bool peek(typename Driver::data_t &byte)
|
|
{
|
|
return Driver::peek(byte);
|
|
}
|
|
|
|
static bool peek()
|
|
{
|
|
return Driver::peek();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Output stream overloads
|
|
|
|
Uart &operator<<(const char *str)
|
|
{
|
|
txString(str);
|
|
return *this;
|
|
}
|
|
|
|
Uart &operator<<(const ::detail::FlashString *str)
|
|
{
|
|
txString(str);
|
|
return *this;
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(char)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(unsigned char)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(short)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(unsigned short)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(int)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(unsigned int)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(long)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(unsigned long)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(long long)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(unsigned long long)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(float)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(double)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(long double)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(bool)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator<<(const void *)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
// Input stream overloads
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(char &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned char &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(short &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned short &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(int &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned int &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned long &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long long &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(unsigned long long &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(float &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(double &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(long double &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(bool &)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
|
|
template <typename... Ts>
|
|
Uart &operator>>(const void *&)
|
|
{
|
|
static_assert(detail::always_false<Ts...>::value, "Not implemented");
|
|
}
|
|
};
|
|
|
|
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
|