Compare commits

..

3 Commits

4 changed files with 75 additions and 175 deletions

View File

@@ -2,9 +2,10 @@
#include "../clock.hpp" #include "../clock.hpp"
#include <stdint.h> #include "../type/type.hpp"
#include "utils.hpp" #include <math.h>
#include <stdint.h>
#define FORCE_INLINE __attribute__((always_inline)) #define FORCE_INLINE __attribute__((always_inline))
@@ -12,7 +13,6 @@ namespace uart {
enum class Mode { enum class Mode {
ASYNCHRONOUS, ASYNCHRONOUS,
ASYNCHRONOUS_2X,
SYNCHRONOUS_MASTER, SYNCHRONOUS_MASTER,
SYNCHRONOUS_SLAVE, SYNCHRONOUS_SLAVE,
SPI, SPI,
@@ -46,7 +46,12 @@ class Hardware {
public: public:
static void init() FORCE_INLINE static void init() FORCE_INLINE
{ {
constexpr auto BaudVal = calcBaud(); constexpr auto AbsDoubleError = fabs(calcBaudError<true>());
constexpr auto AbsNormalError = fabs(calcBaudError<false>());
static_assert(AbsDoubleError <= 3.0 || AbsNormalError <= 3.0, "Baud rate error over 3%, probably unusable");
constexpr auto UseDoubleSpeed = (AbsDoubleError < AbsNormalError);
constexpr auto BaudVal = calcBaudVal<UseDoubleSpeed>();
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<uint8_t>(BaudVal >> 8); *getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<uint8_t>(BaudVal >> 8);
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(BaudVal); *getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(BaudVal);
@@ -58,10 +63,12 @@ class Hardware {
constexpr auto EnableRx = calcRxState<true>(); constexpr auto EnableRx = calcRxState<true>();
constexpr auto EnableTx = calcTxState<true>(); constexpr auto EnableTx = calcTxState<true>();
constexpr auto InterruptVal = calcInterrupt(); constexpr auto InterruptVal = calcInterrupt();
constexpr auto DoubleSpeedVal = calcDoubleSpeedVal<UseDoubleSpeed>();
constexpr uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal; constexpr uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
constexpr uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal; constexpr uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal;
*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() |= DoubleSpeedVal;
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB; *getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB;
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC; *getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC;
} }
@@ -134,13 +141,39 @@ class Hardware {
uint8_t regBVal = 0; uint8_t regBVal = 0;
}; };
static constexpr auto calcBaud() template <bool DoubleSpeed = true>
static constexpr auto calcBaudVal()
{ {
// The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly if constexpr (DoubleSpeed) {
constexpr auto BaudVal = (F_CPU + 8 * cfg::BAUD_RATE) / (16 * cfg::BAUD_RATE) - 1; constexpr auto BaudVal = static_cast<uint16_t>(round(F_CPU / (8.0 * cfg::BAUD_RATE) - 1));
return BaudVal;
}
constexpr auto BaudVal = static_cast<uint16_t>(round(F_CPU / (16.0 * cfg::BAUD_RATE) - 1));
return BaudVal; return BaudVal;
} }
template <uint16_t BaudVal, bool DoubleSpeed = true>
static constexpr auto calcBaudRate()
{
if constexpr (DoubleSpeed) {
constexpr auto BaudRate = static_cast<uint32_t>(round(F_CPU / (8.0 * (BaudVal + 1))));
return BaudRate;
}
constexpr auto BaudRate = static_cast<uint32_t>(round(F_CPU / (16.0 * (BaudVal + 1))));
return BaudRate;
}
template <bool DoubleSpeed = true>
static constexpr auto calcBaudError()
{
constexpr auto BaudVal = calcBaudVal<DoubleSpeed>();
constexpr auto ClosestBaudRate = calcBaudRate<BaudVal, DoubleSpeed>();
constexpr auto BaudError = (static_cast<double>(ClosestBaudRate) / cfg::BAUD_RATE - 1) * 100;
return BaudError;
}
static constexpr auto calcDataBits() static constexpr auto calcDataBits()
{ {
DataBitsVal dataBitsVal; DataBitsVal dataBitsVal;
@@ -233,6 +266,17 @@ class Hardware {
return interruptVal; return interruptVal;
} }
template <bool DoubleSpeed>
static constexpr auto calcDoubleSpeedVal()
{
uint8_t doubleSpeedVal = 0;
if constexpr (DoubleSpeed)
doubleSpeedVal = (1 << CtrlFlagsA::SPEED_2X);
return doubleSpeedVal;
}
}; };
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode> template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
@@ -258,7 +302,7 @@ class BlockingHardware {
static bool peek(data_t &) FORCE_INLINE static bool peek(data_t &) FORCE_INLINE
{ {
static_assert(util::always_false_v<data_t>, "Peek with data is not supported in blocking mode"); static_assert(type::always_false_v<data_t>, "Peek with data is not supported in blocking mode");
return false; return false;
} }

View File

@@ -1,15 +1,15 @@
#pragma once #pragma once
#include "config.hpp" #include "config.hpp"
#include "utils.hpp"
#include "../io/io.hpp" #include "../io/io.hpp"
#include "../type/type.hpp"
namespace uart { namespace uart {
template <io::P rxPin, io::P txPin, class cfg = Config<>> template <io::P rxPin, io::P txPin, class cfg = Config<>>
class Software { class Software {
static_assert(util::always_false_v<cfg>, "Not implemented"); static_assert(type::always_false_v<cfg>, "Not implemented");
public: public:
using data_t = typename cfg::data_t; using data_t = typename cfg::data_t;

View File

@@ -4,7 +4,6 @@
#include "config.hpp" #include "config.hpp"
#include "software.hpp" #include "software.hpp"
#include "utils.hpp"
#undef UART0_INT_VECTORS #undef UART0_INT_VECTORS
#include "hardware0.hpp" #include "hardware0.hpp"
@@ -36,8 +35,8 @@ static constexpr size_t cntDigits()
template <typename T, size_t Base> template <typename T, size_t Base>
static constexpr size_t maxNumDigits() static constexpr size_t maxNumDigits()
{ {
constexpr T MinVal = util::NumericLimits<T>::min(); constexpr T MinVal = type::numeric_limits<T>::min();
constexpr T MaxVal = util::NumericLimits<T>::max(); constexpr T MaxVal = type::numeric_limits<T>::max();
constexpr T MinDigits = cntDigits<T, MinVal, Base>(); constexpr T MinDigits = cntDigits<T, MinVal, Base>();
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>(); constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
@@ -115,7 +114,7 @@ class Uart {
template <typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true> template <typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
static void txNumber(const T &val) static void txNumber(const T &val)
{ {
static_assert(util::is_integral_v<T>, "Only supported on integral types"); static_assert(type::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 >= 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(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"); static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number");
@@ -237,19 +236,19 @@ class Uart {
template <typename... Ts> template <typename... Ts>
Uart &operator<<(float) const Uart &operator<<(float) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator<<(double) const Uart &operator<<(double) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator<<(long double) const Uart &operator<<(long double) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
Uart &operator<<(const bool &val) Uart &operator<<(const bool &val)
@@ -271,91 +270,91 @@ class Uart {
template <typename... Ts> template <typename... Ts>
Uart &operator>>(char &) const Uart &operator>>(char &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(unsigned char &) const Uart &operator>>(unsigned char &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(short &) const Uart &operator>>(short &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(unsigned short &) const Uart &operator>>(unsigned short &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(int &) const Uart &operator>>(int &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(unsigned int &) const Uart &operator>>(unsigned int &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(long &) const Uart &operator>>(long &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(unsigned long &) const Uart &operator>>(unsigned long &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(long long &) const Uart &operator>>(long long &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(unsigned long long &) const Uart &operator>>(unsigned long long &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(float &) const Uart &operator>>(float &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(double &) const Uart &operator>>(double &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(long double &) const Uart &operator>>(long double &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not supported by hardware"); static_assert(type::always_false_v<Ts...>, "Not supported by hardware");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(bool &) const Uart &operator>>(bool &) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
template <typename... Ts> template <typename... Ts>
Uart &operator>>(const void *&) const Uart &operator>>(const void *&) const
{ {
static_assert(util::always_false_v<Ts...>, "Not implemented"); static_assert(type::always_false_v<Ts...>, "Not implemented");
} }
}; };

143
utils.hpp
View File

@@ -1,143 +0,0 @@
#pragma once
// Fix for limits.h not exposing LLONG_MIN, LLONG_MIN, and ULLONG_MAX to C++ context
#ifdef __cplusplus
#define __STDC_VERSION__ 201112L
#endif
#include <float.h>
#include <limits.h>
namespace uart {
namespace util {
// clang-format off
template <bool Val> struct set_bool { static constexpr auto value = Val; };
struct true_type : set_bool<true> {};
struct false_type : set_bool<false> {};
template <typename...> struct always_false : false_type {};
template <typename... Ts> static constexpr auto always_false_v = always_false<Ts...>::value;
template <typename T> struct is_integral : false_type {};
template <> struct is_integral<bool> : true_type {};
template <> struct is_integral<char> : true_type {};
template <> struct is_integral<signed char> : true_type {};
template <> struct is_integral<unsigned char> : true_type {};
template <> struct is_integral<short> : true_type {};
template <> struct is_integral<int> : true_type {};
template <> struct is_integral<long int> : true_type {};
template <> struct is_integral<long long int> : true_type {};
template <> struct is_integral<unsigned short> : true_type {};
template <> struct is_integral<unsigned int> : true_type {};
template <> struct is_integral<unsigned long int> : true_type {};
template <> struct is_integral<unsigned long long int> : true_type {};
template <typename T> static constexpr auto is_integral_v = is_integral<T>::value;
template <typename T, typename U> struct is_same : false_type {};
template <typename T> struct is_same<T, T> : true_type {};
template <typename T, typename U> static constexpr auto is_same_v = is_same<T, U>::value;
template <typename T>
struct NumericLimits {
static constexpr T min() { return T(); }
static constexpr T max() { return T(); }
};
template <>
struct NumericLimits<bool> {
static constexpr bool min() { return false; }
static constexpr bool max() { return true; }
};
template <>
struct NumericLimits<char> {
static constexpr char min() { return CHAR_MIN; }
static constexpr char max() { return CHAR_MAX; }
};
template <>
struct NumericLimits<signed char> {
static constexpr signed char min() { return SCHAR_MIN; }
static constexpr signed char max() { return SCHAR_MAX; }
};
template <>
struct NumericLimits<unsigned char> {
static constexpr unsigned char min() { return 0; }
static constexpr unsigned char max() { return UCHAR_MAX; }
};
template <>
struct NumericLimits<short> {
static constexpr short min() { return SHRT_MIN; }
static constexpr short max() { return SHRT_MAX; }
};
template <>
struct NumericLimits<int> {
static constexpr int min() { return INT_MIN; }
static constexpr int max() { return INT_MAX; }
};
template <>
struct NumericLimits<long> {
static constexpr long int min() { return LONG_MIN; }
static constexpr long int max() { return LONG_MAX; }
};
template <>
struct NumericLimits<long long int> {
static constexpr long long int min() { return LLONG_MIN; }
static constexpr long long int max() { return LLONG_MAX; }
};
template <>
struct NumericLimits<unsigned short> {
static constexpr unsigned short min() { return 0; }
static constexpr unsigned short max() { return USHRT_MAX; }
};
template <>
struct NumericLimits<unsigned int> {
static constexpr unsigned int min() { return 0; }
static constexpr unsigned int max() { return UINT_MAX; }
};
template <>
struct NumericLimits<unsigned long int> {
static constexpr unsigned long int min() { return 0; }
static constexpr unsigned long int max() { return ULONG_MAX; }
};
template <>
struct NumericLimits<unsigned long long int> {
static constexpr unsigned long long int min() { return 0; }
static constexpr unsigned long long int max() { return ULLONG_MAX; }
};
template <>
struct NumericLimits<float> {
template <typename... Ts> static constexpr float min() { return FLT_MIN; }
template <typename... Ts> static constexpr float max() { return FLT_MAX; }
};
template <>
struct NumericLimits<double> {
template <typename... Ts> static constexpr double min() { return DBL_MIN; }
template <typename... Ts> static constexpr double max() { return DBL_MAX; }
};
template <>
struct NumericLimits<long double> {
template <typename... Ts> static constexpr long double min() { return LDBL_MIN; }
template <typename... Ts> static constexpr long double max() { return LDBL_MAX; }
};
// clang-format on
} // namespace util
} // namespace uart