uart/uart.hpp

273 lines
5.8 KiB
C++
Raw Normal View History

#pragma once
2019-07-28 12:15:19 +02:00
#include "config.hpp"
#include "hardware0.hpp"
#include "hardware1.hpp"
#include "software.hpp"
#include "../flash/flash.hpp"
#define FORCE_INLINE __attribute__((always_inline))
namespace uart {
2019-07-28 10:35:11 +02:00
namespace detail {
template <typename...>
struct always_false {
static constexpr auto value = false;
};
} // namespace detail
2019-07-28 12:15:19 +02:00
template <class Driver>
class uart {
public:
// Initialization is done upon construction
uart()
{
2019-07-28 12:15:19 +02:00
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(typename Driver::data_t byte) FORCE_INLINE
{
2019-07-28 12:15:19 +02:00
Driver::txByte(byte);
}
2019-07-28 12:15:19 +02:00
static typename Driver::data_t rxByte()
{
2019-07-28 12:15:19 +02:00
return Driver::rxByte();
}
2019-07-28 12:15:19 +02:00
static typename Driver::data_t peek()
{
2019-07-28 12:15:19 +02:00
return Driver::peek();
}
static void txString(const char *str) FORCE_INLINE
{
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) FORCE_INLINE
{
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);
}
//////////////////////////////////////////////////////////////////////////
// Output stream overloads
uart &operator<<(const char *str) FORCE_INLINE
{
txString(str);
return *this;
}
uart &operator<<(const ::detail::FlashString *str) FORCE_INLINE
{
txString(str);
return *this;
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(char)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(unsigned char)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(short)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(unsigned short)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(int)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(unsigned int)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(long)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(unsigned long)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(long long)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(unsigned long long)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(float)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(double)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(long double)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(bool)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator<<(const void *)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
//////////////////////////////////////////////////////////////////////////
// Input stream overloads
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(char &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(unsigned char &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(short &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(unsigned short &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(int &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(unsigned int &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(long &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(unsigned long &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(long long &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(unsigned long long &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(float &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(double &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(long double &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(bool &)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
2019-07-28 10:35:11 +02:00
template <typename... Ts>
uart &operator>>(const void *&)
{
2019-07-28 10:35:11 +02:00
static_assert(detail::always_false<Ts...>::value, "Not implemented");
}
};
} // namespace uart
#undef FORCE_INLINE