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
|
|
|
|
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
|
|
|
enum class Mode {
|
|
|
|
ASYNCHRONOUS,
|
|
|
|
ASYNCHRONOUS_2X,
|
|
|
|
SYNCHRONOUS_MASTER,
|
|
|
|
SYNCHRONOUS_SLAVE,
|
|
|
|
SPI,
|
|
|
|
};
|
|
|
|
|
2019-07-28 14:09:09 +02:00
|
|
|
enum class Driven {
|
|
|
|
INTERRUPT,
|
|
|
|
BLOCKING,
|
|
|
|
};
|
|
|
|
|
2019-07-28 17:32:51 +02:00
|
|
|
namespace detail {
|
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
#if defined(__AVR_ATmega1284P__)
|
|
|
|
|
|
|
|
struct Registers0 {
|
|
|
|
static constexpr volatile auto *IO_REG = &UDR0;
|
|
|
|
static constexpr volatile auto *CTRL_STAT_REG_A = &UCSR0A;
|
|
|
|
static constexpr volatile auto *CTRL_STAT_REG_B = &UCSR0B;
|
|
|
|
static constexpr volatile auto *CTRL_STAT_REG_C = &UCSR0C;
|
|
|
|
static constexpr volatile auto *BAUD_REG_L = &UBRR0L;
|
|
|
|
static constexpr volatile auto *BAUD_REG_H = &UBRR0H;
|
2019-07-28 17:32:51 +02:00
|
|
|
};
|
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
static constexpr auto getLastRxError() {}
|
|
|
|
static constexpr void set2xSpeed() {}
|
2019-07-28 17:32:51 +02:00
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
template <uint16_t BaudVal>
|
|
|
|
static inline void setBaudRate()
|
|
|
|
{
|
|
|
|
*Registers0::BAUD_REG_H = static_cast<uint8_t>(BaudVal >> 8);
|
|
|
|
*Registers0::BAUD_REG_L = static_cast<uint8_t>(BaudVal);
|
|
|
|
}
|
2019-07-28 17:32:51 +02:00
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
template <uint8_t RegVal>
|
|
|
|
static inline void setCtrlStatRegC()
|
|
|
|
{
|
|
|
|
*Registers0::CTRL_STAT_REG_C = RegVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error "This chip is not supported"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <Mode mode = Mode::ASYNCHRONOUS, class cfg = Config<>, Driven driven = Driven::INTERRUPT>
|
|
|
|
class Hardware0 {
|
|
|
|
public:
|
|
|
|
using data_t = typename cfg::data_t;
|
|
|
|
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
detail::setBaudRate<calcBaud()>();
|
|
|
|
|
|
|
|
constexpr auto dataBitsVal = calcDataBits();
|
|
|
|
constexpr auto parityVal = calcParity();
|
|
|
|
constexpr auto stopBitsVal = calcStopBits();
|
|
|
|
constexpr auto modeVal = calcMode();
|
|
|
|
constexpr auto enableRx = calcRxState<true>();
|
|
|
|
constexpr auto enableTx = calcTxState<true>();
|
|
|
|
|
|
|
|
constexpr uint8_t controlRegB = dataBitsVal.regBVal | enableRx | enableTx;
|
|
|
|
constexpr uint8_t controlRegC = dataBitsVal.regCVal | parityVal | stopBitsVal | modeVal;
|
|
|
|
|
|
|
|
*detail::Registers0::CTRL_STAT_REG_B = controlRegB;
|
|
|
|
detail::setCtrlStatRegC<controlRegC>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void txByte(data_t byte) FORCE_INLINE
|
|
|
|
{
|
|
|
|
while (!(*detail::Registers0::CTRL_STAT_REG_A & (1 << UDRE0)))
|
|
|
|
;
|
|
|
|
|
|
|
|
*detail::Registers0::IO_REG = byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
static data_t rxByte() {}
|
|
|
|
|
|
|
|
static data_t peek() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr auto BAUD_RATE = cfg::BAUD_RATE;
|
|
|
|
static constexpr auto PARITY = cfg::PARITY;
|
|
|
|
static constexpr auto STOP_BITS = cfg::STOP_BITS;
|
2019-07-28 17:32:51 +02:00
|
|
|
|
|
|
|
static constexpr auto calcBaud()
|
|
|
|
{
|
2019-07-28 19:20:03 +02:00
|
|
|
// The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly
|
2019-07-30 20:29:38 +02:00
|
|
|
constexpr auto baudVal = (F_CPU + 8 * BAUD_RATE) / (16 * BAUD_RATE) - 1;
|
2019-07-28 19:20:03 +02:00
|
|
|
return baudVal;
|
2019-07-28 17:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DataBitsVal {
|
2019-07-30 20:29:38 +02:00
|
|
|
uint8_t regCVal = 0;
|
|
|
|
uint8_t regBVal = 0;
|
2019-07-28 17:32:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr auto calcDataBits()
|
|
|
|
{
|
|
|
|
DataBitsVal dataBitsVal;
|
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
switch (DATA_BITS) {
|
2019-07-28 17:32:51 +02:00
|
|
|
case DataBits::FIVE:
|
2019-07-30 20:29:38 +02:00
|
|
|
dataBitsVal.regCVal = 0;
|
2019-07-28 17:32:51 +02:00
|
|
|
break;
|
|
|
|
case DataBits::SIX:
|
2019-07-30 20:29:38 +02:00
|
|
|
dataBitsVal.regCVal = (1 << UCSZ00);
|
2019-07-28 17:32:51 +02:00
|
|
|
break;
|
|
|
|
case DataBits::SEVEN:
|
2019-07-30 20:29:38 +02:00
|
|
|
dataBitsVal.regCVal = (1 << UCSZ01);
|
2019-07-28 17:32:51 +02:00
|
|
|
break;
|
|
|
|
case DataBits::EIGHT:
|
2019-07-30 20:29:38 +02:00
|
|
|
dataBitsVal.regCVal = (1 << UCSZ01) | (1 << UCSZ00);
|
2019-07-28 17:32:51 +02:00
|
|
|
break;
|
|
|
|
case DataBits::NINE:
|
2019-07-30 20:29:38 +02:00
|
|
|
dataBitsVal.regCVal = (1 << UCSZ01) | (1 << UCSZ00);
|
|
|
|
dataBitsVal.regBVal = (1 << UCSZ02);
|
2019-07-28 17:32:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataBitsVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto calcParity()
|
|
|
|
{
|
|
|
|
uint8_t parityVal = 0;
|
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
if (PARITY == Parity::EVEN)
|
2019-07-28 17:32:51 +02:00
|
|
|
parityVal = (1 << UPM01);
|
2019-07-30 20:29:38 +02:00
|
|
|
else if (PARITY == Parity::ODD)
|
2019-07-28 17:32:51 +02:00
|
|
|
parityVal = (1 << UPM01) | (1 << UPM00);
|
|
|
|
|
|
|
|
return parityVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto calcStopBits()
|
|
|
|
{
|
|
|
|
uint8_t stopBitsVal = 0;
|
|
|
|
|
2019-07-30 20:29:38 +02:00
|
|
|
if (STOP_BITS == StopBits::TWO)
|
2019-07-28 17:32:51 +02:00
|
|
|
stopBitsVal = (1 << USBS0);
|
|
|
|
|
|
|
|
return stopBitsVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto calcMode()
|
|
|
|
{
|
|
|
|
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
|
|
|
|
|
|
|
uint8_t modeVal = 0;
|
|
|
|
|
|
|
|
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
|
|
|
modeVal = (1 << UMSEL00);
|
|
|
|
}
|
|
|
|
|
|
|
|
return modeVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool enable>
|
|
|
|
static constexpr auto calcRxState()
|
|
|
|
{
|
|
|
|
uint8_t enableVal = 0;
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
enableVal = (1 << RXEN0);
|
|
|
|
|
|
|
|
return enableVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool enable>
|
|
|
|
static constexpr auto calcTxState()
|
|
|
|
{
|
|
|
|
uint8_t enableVal = 0;
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
enableVal = (1 << TXEN0);
|
|
|
|
|
|
|
|
return enableVal;
|
|
|
|
}
|
2019-07-27 18:55:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace uart
|
2019-07-28 17:32:51 +02:00
|
|
|
|
|
|
|
#undef FORCE_INLINE
|