2019-07-27 18:55:17 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-07-28 12:15:19 +02:00
|
|
|
#include "config.hpp"
|
2019-07-27 18:55:17 +02:00
|
|
|
#include "hardware0.hpp"
|
|
|
|
#include "hardware1.hpp"
|
|
|
|
#include "software.hpp"
|
2019-08-03 10:58:32 +02:00
|
|
|
#include "utils.hpp"
|
2019-07-27 18:55:17 +02:00
|
|
|
|
|
|
|
#include "../flash/flash.hpp"
|
|
|
|
|
2019-07-28 17:32:51 +02:00
|
|
|
#define FORCE_INLINE __attribute__((always_inline))
|
|
|
|
|
2019-07-27 18:55:17 +02:00
|
|
|
namespace uart {
|
|
|
|
|
2019-07-28 12:15:19 +02:00
|
|
|
template <class Driver>
|
2019-07-28 17:57:49 +02:00
|
|
|
class Uart {
|
2019-07-27 18:55:17 +02:00
|
|
|
public:
|
2019-08-03 11:14:01 +02:00
|
|
|
// 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;
|
2019-07-27 18:55:17 +02:00
|
|
|
|
|
|
|
// Moving and copying uart objects is not supported
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart(const Uart &) = delete;
|
|
|
|
Uart(Uart &&) = delete;
|
|
|
|
Uart &operator=(const Uart &) = delete;
|
|
|
|
Uart &operator=(Uart &&) = delete;
|
2019-07-27 18:55:17 +02:00
|
|
|
|
2019-08-03 11:14:01 +02:00
|
|
|
// Before using the uart init must be called
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
Driver::init();
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:41:34 +02:00
|
|
|
static void txByte(const typename Driver::data_t &byte)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-07-28 12:15:19 +02:00
|
|
|
Driver::txByte(byte);
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:36:07 +02:00
|
|
|
static bool rxByte(typename Driver::data_t &byte)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-02 17:36:07 +02:00
|
|
|
return Driver::rxByte(byte);
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:20:06 +02:00
|
|
|
static bool peek(typename Driver::data_t &byte)
|
|
|
|
{
|
|
|
|
return Driver::peek(byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool peek()
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-07-28 12:15:19 +02:00
|
|
|
return Driver::peek();
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:41:34 +02:00
|
|
|
static void txString(const char *str)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-07-28 12:15:19 +02:00
|
|
|
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
2019-07-27 18:55:17 +02:00
|
|
|
|
|
|
|
while (char ch = *str++)
|
|
|
|
txByte(ch);
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:41:34 +02:00
|
|
|
static void txString(const ::detail::FlashString *str)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-07-28 12:15:19 +02:00
|
|
|
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
2019-07-27 18:55:17 +02:00
|
|
|
|
|
|
|
const char *strIt = reinterpret_cast<const char *>(str);
|
|
|
|
|
|
|
|
while (char ch = pgm_read_byte(strIt++))
|
|
|
|
txByte(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Output stream overloads
|
|
|
|
|
2019-08-02 16:41:34 +02:00
|
|
|
Uart &operator<<(const char *str)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
|
|
|
txString(str);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:41:34 +02:00
|
|
|
Uart &operator<<(const ::detail::FlashString *str)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
|
|
|
txString(str);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(char)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(unsigned char)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(short)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(unsigned short)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(int)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(unsigned int)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(long)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(unsigned long)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(long long)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(unsigned long long)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(float)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(double)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(long double)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(bool)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator<<(const void *)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Input stream overloads
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(char &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(unsigned char &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(short &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(unsigned short &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(int &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(unsigned int &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(long &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(unsigned long &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(long long &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(unsigned long long &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(float &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(double &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(long double &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(bool &)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-28 10:35:11 +02:00
|
|
|
template <typename... Ts>
|
2019-07-28 17:57:49 +02:00
|
|
|
Uart &operator>>(const void *&)
|
2019-07-27 18:55:17 +02:00
|
|
|
{
|
2019-08-03 10:58:32 +02:00
|
|
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
2019-07-27 18:55:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-28 17:58:23 +02:00
|
|
|
template <typename cfg = Config<>>
|
|
|
|
using Uart0 = Uart<Hardware0<Mode::ASYNCHRONOUS, cfg>>;
|
|
|
|
|
2019-07-30 21:43:52 +02:00
|
|
|
#ifdef HAS_UART1
|
|
|
|
template <typename cfg = Config<>>
|
|
|
|
using Uart1 = Uart<Hardware1<Mode::ASYNCHRONOUS, cfg>>;
|
|
|
|
#endif
|
|
|
|
|
2019-07-27 18:55:17 +02:00
|
|
|
} // namespace uart
|
2019-07-28 17:32:51 +02:00
|
|
|
|
|
|
|
#undef FORCE_INLINE
|
2019-07-30 21:43:52 +02:00
|
|
|
#undef HAS_UART1
|