Compare commits
31 Commits
87e6936051
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 419b86999d | |||
| a5f8e8e3d7 | |||
| 119de32445 | |||
| 8f88cdccea | |||
| dfb076cda8 | |||
| 6d9ef6e4be | |||
| bcd18db494 | |||
| 04b6782ec4 | |||
| ae03c8d43e | |||
| 41b9ef74f9 | |||
| 6f592dd098 | |||
| fa0a65a94c | |||
| 0e128bcb7d | |||
| 0a52110d47 | |||
| f751833a88 | |||
| 1bd49f65fa | |||
| cb436b11a8 | |||
| 2a2b9b8817 | |||
| 0532bf48b0 | |||
| f6df6a6a18 | |||
| 1bdc06a325 | |||
| ddf105a175 | |||
| 2fd05483ee | |||
| 16c9015f43 | |||
| 7c21664fe4 | |||
| e326e40b38 | |||
| 8d028aa635 | |||
| c4700ed824 | |||
| 1ee9bc8ca4 | |||
| 1c026e8eb3 | |||
| c4f38cbcdf |
8
.gitignore
vendored
8
.gitignore
vendored
@@ -2,4 +2,10 @@
|
|||||||
Release
|
Release
|
||||||
Debug
|
Debug
|
||||||
*.componentinfo.xml
|
*.componentinfo.xml
|
||||||
avrdude.bat
|
*.elf
|
||||||
|
*.o
|
||||||
|
*.hex
|
||||||
|
*.srec
|
||||||
|
*.eeprom
|
||||||
|
*.lss
|
||||||
|
*.map
|
||||||
|
|||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2019 BlackMark
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is furnished
|
||||||
|
to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice (including the next
|
||||||
|
paragraph) shall be included in all copies or substantial portions of the
|
||||||
|
Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
||||||
|
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
|
||||||
|
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
@@ -27,17 +27,17 @@ namespace detail {
|
|||||||
|
|
||||||
template <DataBits dataBits>
|
template <DataBits dataBits>
|
||||||
struct choose_data_type {
|
struct choose_data_type {
|
||||||
using type = uint8_t;
|
using type = std::uint8_t;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct choose_data_type<DataBits::NINE> {
|
struct choose_data_type<DataBits::NINE> {
|
||||||
using type = uint16_t;
|
using type = std::uint16_t;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE,
|
template <std::uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE,
|
||||||
StopBits stopBits = StopBits::ONE>
|
StopBits stopBits = StopBits::ONE>
|
||||||
struct Config {
|
struct Config {
|
||||||
static constexpr auto BAUD_RATE = baudRate;
|
static constexpr auto BAUD_RATE = baudRate;
|
||||||
|
|||||||
323
hardware.hpp
323
hardware.hpp
@@ -2,15 +2,13 @@
|
|||||||
|
|
||||||
#include "../clock.hpp"
|
#include "../clock.hpp"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
enum class Mode {
|
enum class Mode {
|
||||||
ASYNCHRONOUS,
|
ASYNCHRONOUS,
|
||||||
ASYNCHRONOUS_2X,
|
|
||||||
SYNCHRONOUS_MASTER,
|
SYNCHRONOUS_MASTER,
|
||||||
SYNCHRONOUS_SLAVE,
|
SYNCHRONOUS_SLAVE,
|
||||||
SPI,
|
SPI,
|
||||||
@@ -23,105 +21,160 @@ enum class Driven {
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode,
|
using reg_ptr_t = volatile std::uint8_t *;
|
||||||
Driven driven>
|
|
||||||
|
template <std::uintptr_t Address>
|
||||||
|
static inline reg_ptr_t getRegPtr()
|
||||||
|
{
|
||||||
|
return reinterpret_cast<reg_ptr_t>(Address);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename data_t, std::uint8_t Size>
|
||||||
|
struct RingBuffer {
|
||||||
|
std::uint8_t head;
|
||||||
|
std::uint8_t tail;
|
||||||
|
data_t buf[Size];
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Driven driven,
|
||||||
|
Mode mode>
|
||||||
class Hardware {
|
class Hardware {
|
||||||
public:
|
public:
|
||||||
static void init() FORCE_INLINE
|
[[gnu::always_inline]] static void init()
|
||||||
{
|
{
|
||||||
constexpr auto baudVal = calcBaud();
|
constexpr auto AbsDoubleError = std::fabs(calcBaudError<true>());
|
||||||
|
constexpr auto AbsNormalError = std::fabs(calcBaudError<false>());
|
||||||
|
static_assert(AbsDoubleError <= 3.0 || AbsNormalError <= 3.0, "Baud rate error over 3%, probably unusable");
|
||||||
|
|
||||||
*Registers::BAUD_REG_H = static_cast<uint8_t>(baudVal >> 8);
|
constexpr auto UseDoubleSpeed = (AbsDoubleError < AbsNormalError);
|
||||||
*Registers::BAUD_REG_L = static_cast<uint8_t>(baudVal);
|
constexpr auto BaudVal = calcBaudVal<UseDoubleSpeed>();
|
||||||
|
|
||||||
constexpr auto dataBitsVal = calcDataBits();
|
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<std::uint8_t>(BaudVal >> 8);
|
||||||
constexpr auto parityVal = calcParity();
|
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<std::uint8_t>(BaudVal);
|
||||||
constexpr auto stopBitsVal = calcStopBits();
|
|
||||||
constexpr auto modeVal = calcMode();
|
|
||||||
constexpr auto enableRx = calcRxState<true>();
|
|
||||||
constexpr auto enableTx = calcTxState<true>();
|
|
||||||
constexpr auto interruptVal = calcInterrupt();
|
|
||||||
|
|
||||||
constexpr uint8_t controlRegB = dataBitsVal.regBVal | enableRx | enableTx | interruptVal;
|
constexpr auto DataBitsValues = calcDataBits();
|
||||||
constexpr uint8_t controlRegC = dataBitsVal.regCVal | parityVal | stopBitsVal | modeVal;
|
constexpr auto ParityVal = calcParity();
|
||||||
|
constexpr auto StopBitsVal = calcStopBits();
|
||||||
|
constexpr auto ModeVal = calcMode();
|
||||||
|
constexpr auto EnableRx = calcRxState<true>();
|
||||||
|
constexpr auto EnableTx = calcTxState<true>();
|
||||||
|
constexpr auto InterruptVal = calcInterrupt();
|
||||||
|
|
||||||
*Registers::CTRL_STAT_REG_B = controlRegB;
|
constexpr std::uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
|
||||||
*Registers::CTRL_STAT_REG_C = controlRegC;
|
constexpr std::uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal;
|
||||||
|
|
||||||
|
auto ctrlStatRegA = getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>();
|
||||||
|
|
||||||
|
if constexpr (UseDoubleSpeed)
|
||||||
|
*ctrlStatRegA = *ctrlStatRegA | (1 << CtrlFlagsA::SPEED_2X);
|
||||||
|
else
|
||||||
|
*ctrlStatRegA = *ctrlStatRegA & ~(1 << CtrlFlagsA::SPEED_2X);
|
||||||
|
|
||||||
|
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB;
|
||||||
|
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rxByteBlocking(typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static bool rxByteBlocking(typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
if (*Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||||
byte = *Registers::IO_REG;
|
byte = *getRegPtr<Registers::IO_REG_ADDR>();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static typename cfg::data_t rxByteInterrupt() FORCE_INLINE
|
[[gnu::always_inline]] static typename cfg::data_t rxByteInterrupt()
|
||||||
{
|
{
|
||||||
return *Registers::IO_REG;
|
return *getRegPtr<Registers::IO_REG_ADDR>();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool txEmpty() FORCE_INLINE
|
[[gnu::always_inline]] static bool txEmpty()
|
||||||
{
|
{
|
||||||
return *Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::DATA_REG_EMPTY);
|
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::DATA_REG_EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool txComplete() FORCE_INLINE
|
[[gnu::always_inline]] static bool txComplete()
|
||||||
{
|
{
|
||||||
return *Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clearTxComplete() FORCE_INLINE
|
[[gnu::always_inline]] static void clearTxComplete()
|
||||||
{
|
{
|
||||||
*Registers::CTRL_STAT_REG_A |= (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() |= (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByteBlocking(const typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByteBlocking(const typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
while (!txEmpty())
|
while (!txEmpty())
|
||||||
;
|
;
|
||||||
|
|
||||||
*Registers::IO_REG = byte;
|
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByteInterrupt(volatile const typename cfg::data_t &byte) FORCE_INLINE
|
[[gnu::always_inline]] static void txByteInterrupt(volatile const typename cfg::data_t &byte)
|
||||||
{
|
{
|
||||||
*Registers::IO_REG = byte;
|
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool peekBlocking() FORCE_INLINE
|
[[gnu::always_inline]] static bool peekBlocking()
|
||||||
{
|
{
|
||||||
if (*Registers::CTRL_STAT_REG_A & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void enableDataRegEmptyInt() FORCE_INLINE
|
[[gnu::always_inline]] static void enableDataRegEmptyInt()
|
||||||
{
|
{
|
||||||
*Registers::CTRL_STAT_REG_B |= (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||||
|
*ctrlStatRegB = *ctrlStatRegB | (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void disableDataRegEmptyInt() FORCE_INLINE
|
[[gnu::always_inline]] static void disableDataRegEmptyInt()
|
||||||
{
|
{
|
||||||
*Registers::CTRL_STAT_REG_B &= ~(1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||||
|
*ctrlStatRegB = *ctrlStatRegB & ~(1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct DataBitsVal {
|
struct DataBitsVal {
|
||||||
uint8_t regCVal = 0;
|
std::uint8_t regCVal = 0;
|
||||||
uint8_t regBVal = 0;
|
std::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<std::uint16_t>(round(F_CPU / (8.0 * cfg::BAUD_RATE) - 1));
|
||||||
return baudVal;
|
return BaudVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto BaudVal = static_cast<std::uint16_t>(round(F_CPU / (16.0 * cfg::BAUD_RATE) - 1));
|
||||||
|
return BaudVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::uint16_t BaudVal, bool DoubleSpeed = true>
|
||||||
|
static constexpr auto calcBaudRate()
|
||||||
|
{
|
||||||
|
if constexpr (DoubleSpeed) {
|
||||||
|
constexpr auto BaudRate = static_cast<std::uint32_t>(round(F_CPU / (8.0 * (BaudVal + 1))));
|
||||||
|
return BaudRate;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr auto BaudRate = static_cast<std::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()
|
||||||
@@ -152,7 +205,7 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcParity()
|
static constexpr auto calcParity()
|
||||||
{
|
{
|
||||||
uint8_t parityVal = 0;
|
std::uint8_t parityVal = 0;
|
||||||
|
|
||||||
if (cfg::PARITY == Parity::EVEN)
|
if (cfg::PARITY == Parity::EVEN)
|
||||||
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1);
|
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1);
|
||||||
@@ -164,7 +217,7 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcStopBits()
|
static constexpr auto calcStopBits()
|
||||||
{
|
{
|
||||||
uint8_t stopBitsVal = 0;
|
std::uint8_t stopBitsVal = 0;
|
||||||
|
|
||||||
if (cfg::STOP_BITS == StopBits::TWO)
|
if (cfg::STOP_BITS == StopBits::TWO)
|
||||||
stopBitsVal = (1 << CtrlFlagsC::STOP_BIT_SEL);
|
stopBitsVal = (1 << CtrlFlagsC::STOP_BIT_SEL);
|
||||||
@@ -176,7 +229,7 @@ class Hardware {
|
|||||||
{
|
{
|
||||||
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
||||||
|
|
||||||
uint8_t modeVal = 0;
|
std::uint8_t modeVal = 0;
|
||||||
|
|
||||||
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
||||||
modeVal = (1 << CtrlFlagsC::MODE_SEL_0);
|
modeVal = (1 << CtrlFlagsC::MODE_SEL_0);
|
||||||
@@ -185,23 +238,23 @@ class Hardware {
|
|||||||
return modeVal;
|
return modeVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool enable>
|
template <bool Enable>
|
||||||
static constexpr auto calcRxState()
|
static constexpr auto calcRxState()
|
||||||
{
|
{
|
||||||
uint8_t enableVal = 0;
|
std::uint8_t enableVal = 0;
|
||||||
|
|
||||||
if (enable)
|
if (Enable)
|
||||||
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
||||||
|
|
||||||
return enableVal;
|
return enableVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool enable>
|
template <bool Enable>
|
||||||
static constexpr auto calcTxState()
|
static constexpr auto calcTxState()
|
||||||
{
|
{
|
||||||
uint8_t enableVal = 0;
|
std::uint8_t enableVal = 0;
|
||||||
|
|
||||||
if (enable)
|
if (Enable)
|
||||||
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
||||||
|
|
||||||
return enableVal;
|
return enableVal;
|
||||||
@@ -209,24 +262,162 @@ class Hardware {
|
|||||||
|
|
||||||
static constexpr auto calcInterrupt()
|
static constexpr auto calcInterrupt()
|
||||||
{
|
{
|
||||||
uint8_t interruptVal = 0;
|
std::uint8_t interruptVal = 0;
|
||||||
|
|
||||||
if (driven == Driven::INTERRUPT)
|
if (driven == Driven::INTERRUPT)
|
||||||
interruptVal |= (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE) | (1 << CtrlFlagsB::RX_INT_ENABLE);
|
interruptVal = (1 << CtrlFlagsB::RX_INT_ENABLE);
|
||||||
|
|
||||||
return interruptVal;
|
return interruptVal;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename data_t, uint8_t Size>
|
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
|
||||||
struct RingBuffer {
|
class BlockingHardware {
|
||||||
uint8_t head;
|
public:
|
||||||
uint8_t tail;
|
using data_t = typename cfg::data_t;
|
||||||
data_t buf[Size];
|
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void init()
|
||||||
|
{
|
||||||
|
HardwareImpl::init();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void txByte(const data_t &byte)
|
||||||
|
{
|
||||||
|
HardwareImpl::txByteBlocking(byte);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool rxByte(data_t &byte)
|
||||||
|
{
|
||||||
|
return HardwareImpl::rxByteBlocking(byte);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool peek(data_t &)
|
||||||
|
{
|
||||||
|
static_assert(util::always_false_v<data_t>, "Peek with data is not supported in blocking mode");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool peek()
|
||||||
|
{
|
||||||
|
return HardwareImpl::peekBlocking();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void flushTx()
|
||||||
|
{
|
||||||
|
while (!HardwareImpl::txEmpty())
|
||||||
|
;
|
||||||
|
while (!HardwareImpl::txComplete())
|
||||||
|
;
|
||||||
|
HardwareImpl::clearTxComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using HardwareImpl = Hardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, Driven::BLOCKING, mode>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
|
||||||
|
class InterruptHardware {
|
||||||
|
public:
|
||||||
|
using data_t = typename cfg::data_t;
|
||||||
|
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void txByte(const data_t &byte)
|
||||||
|
{
|
||||||
|
std::uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
|
||||||
|
while (tmpHead == sm_txBuf.tail)
|
||||||
|
;
|
||||||
|
|
||||||
|
sm_txBuf.buf[tmpHead] = byte;
|
||||||
|
sm_txBuf.head = tmpHead;
|
||||||
|
|
||||||
|
HardwareImpl::enableDataRegEmptyInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool rxByte(data_t &byte)
|
||||||
|
{
|
||||||
|
if (sm_rxBuf.head == sm_rxBuf.tail)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
std::uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
||||||
|
byte = sm_rxBuf.buf[tmpTail];
|
||||||
|
sm_rxBuf.tail = tmpTail;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool peek(data_t &byte)
|
||||||
|
{
|
||||||
|
if (sm_rxBuf.head == sm_rxBuf.tail)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
std::uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
||||||
|
byte = sm_rxBuf.buf[tmpTail];
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static bool peek()
|
||||||
|
{
|
||||||
|
return (sm_rxBuf.head != sm_rxBuf.tail);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void flushTx()
|
||||||
|
{
|
||||||
|
while (sm_txBuf.head != sm_txBuf.tail)
|
||||||
|
;
|
||||||
|
while (!HardwareImpl::txEmpty())
|
||||||
|
;
|
||||||
|
while (!HardwareImpl::txComplete())
|
||||||
|
;
|
||||||
|
HardwareImpl::clearTxComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
|
{
|
||||||
|
const auto data = HardwareImpl::rxByteInterrupt();
|
||||||
|
|
||||||
|
const std::uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE;
|
||||||
|
|
||||||
|
if (tmpHead != sm_rxBuf.tail) {
|
||||||
|
sm_rxBuf.head = tmpHead;
|
||||||
|
sm_rxBuf.buf[tmpHead] = data;
|
||||||
|
} else {
|
||||||
|
// TODO: Handle overflow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
|
{
|
||||||
|
if (sm_txBuf.head != sm_txBuf.tail) {
|
||||||
|
const std::uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
|
||||||
|
sm_txBuf.tail = tmpTail;
|
||||||
|
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
|
||||||
|
} else
|
||||||
|
HardwareImpl::disableDataRegEmptyInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using HardwareImpl = Hardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, Driven::INTERRUPT, mode>;
|
||||||
|
|
||||||
|
static constexpr auto TX_BUFFER_SIZE = 16;
|
||||||
|
static constexpr auto RX_BUFFER_SIZE = 16;
|
||||||
|
|
||||||
|
static volatile RingBuffer<data_t, TX_BUFFER_SIZE> sm_txBuf;
|
||||||
|
static volatile RingBuffer<data_t, RX_BUFFER_SIZE> sm_rxBuf;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
|
||||||
|
volatile RingBuffer<typename InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::data_t,
|
||||||
|
InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::TX_BUFFER_SIZE>
|
||||||
|
InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::sm_txBuf = {0, 0, {0}};
|
||||||
|
|
||||||
|
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
|
||||||
|
volatile RingBuffer<typename InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::data_t,
|
||||||
|
InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::RX_BUFFER_SIZE>
|
||||||
|
InterruptHardware<Registers, CtrlFlagsA, CtrlFlagsB, CtrlFlagsC, cfg, mode>::sm_rxBuf = {0, 0, {0}};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "hardware0.hpp"
|
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
|
|
||||||
namespace uart {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__)
|
|
||||||
|
|
||||||
void (*fnRx0IntHandler)() = nullptr;
|
|
||||||
void (*fnDataReg0EmptyIntHandler)() = nullptr;
|
|
||||||
|
|
||||||
ISR(USART0_RX_vect)
|
|
||||||
{
|
|
||||||
if (fnRx0IntHandler)
|
|
||||||
fnRx0IntHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(USART0_UDRE_vect)
|
|
||||||
{
|
|
||||||
if (fnDataReg0EmptyIntHandler)
|
|
||||||
fnDataReg0EmptyIntHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error "This chip is not supported"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
} // namespace uart
|
|
||||||
214
hardware0.hpp
214
hardware0.hpp
@@ -1,30 +1,45 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
#include <avr/sfr_defs.h>
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "hardware.hpp"
|
#include "hardware.hpp"
|
||||||
|
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__)
|
#if defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega328P__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
The following works in avr-gcc 5.4.0, but is not legal C++, because ptr's are not legal constexpr's:
|
||||||
|
constexpr auto *foo = ptr;
|
||||||
|
|
||||||
|
Workaround is to store the the address of the ptr in a uintptr_t and reinterpret_cast it at call site.
|
||||||
|
The _SFR_ADDR macro in sfr_defs.h would give the address, but it does that by taking the address of the dereferenced
|
||||||
|
pointer and casts it to uint16_t, which is still not a legal constexpr.
|
||||||
|
The workaround therefore is to disable the pointer cast and dereference macro _MMIO_BYTE temporarily.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma push_macro("_MMIO_BYTE")
|
||||||
|
#undef _MMIO_BYTE
|
||||||
|
#define _MMIO_BYTE
|
||||||
|
|
||||||
struct Registers0 {
|
struct Registers0 {
|
||||||
static constexpr volatile auto *IO_REG = &UDR0;
|
static constexpr std::uintptr_t IO_REG_ADDR = UDR0;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_A = &UCSR0A;
|
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR0A;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_B = &UCSR0B;
|
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR0B;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_C = &UCSR0C;
|
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR0C;
|
||||||
static constexpr volatile auto *BAUD_REG_L = &UBRR0L;
|
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR0L;
|
||||||
static constexpr volatile auto *BAUD_REG_H = &UBRR0H;
|
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR0H;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#pragma pop_macro("_MMIO_BYTE")
|
||||||
|
|
||||||
enum class ControlFlagsA0 {
|
enum class ControlFlagsA0 {
|
||||||
MULTI_PROC_COMM_MODE = MPCM0,
|
MULTI_PROC_COMM_MODE = MPCM0,
|
||||||
SPEED_2X = U2X0,
|
SPEED_2X = U2X0,
|
||||||
@@ -64,8 +79,10 @@ constexpr int operator<<(const int &lhs, const ControlFlagsB0 &rhs) { return lhs
|
|||||||
constexpr int operator<<(const int &lhs, const ControlFlagsC0 &rhs) { return lhs << static_cast<int>(rhs); }
|
constexpr int operator<<(const int &lhs, const ControlFlagsC0 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
extern void (*fnRx0IntHandler)();
|
#if defined(__AVR_ATmega328P__)
|
||||||
extern void (*fnDataReg0EmptyIntHandler)();
|
#define USART0_RX_vect USART_RX_vect
|
||||||
|
#define USART0_UDRE_vect USART_UDRE_vect
|
||||||
|
#endif
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "This chip is not supported"
|
#error "This chip is not supported"
|
||||||
@@ -73,160 +90,63 @@ extern void (*fnDataReg0EmptyIntHandler)();
|
|||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <Mode mode = Mode::ASYNCHRONOUS, class cfg = Config<>, Driven driven = Driven::INTERRUPT>
|
template <class cfg = Config<>, Driven driven = Driven::INTERRUPT, Mode mode = Mode::ASYNCHRONOUS>
|
||||||
class Hardware0 {
|
class Hardware0 : public detail::BlockingHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||||
public:
|
detail::ControlFlagsC0, cfg, mode> {
|
||||||
using data_t = typename cfg::data_t;
|
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
|
||||||
|
|
||||||
static void init() FORCE_INLINE
|
|
||||||
{
|
|
||||||
HardwareImpl::init();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void txByte(data_t byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
HardwareImpl::txByteBlocking(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
return HardwareImpl::rxByteBlocking(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
static_cast<void>(byte);
|
|
||||||
static_assert(driven == Driven::BLOCKING, "Peek with data is not supported in blocking mode");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
|
||||||
{
|
|
||||||
return HardwareImpl::peekBlocking();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
|
||||||
{
|
|
||||||
while (!HardwareImpl::txEmpty())
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txComplete())
|
|
||||||
;
|
|
||||||
HardwareImpl::clearTxComplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
|
||||||
detail::ControlFlagsC0, cfg, mode, driven>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
template <class cfg, Mode mode>
|
||||||
class Hardware0<mode, cfg, Driven::INTERRUPT> {
|
class Hardware0<cfg, Driven::INTERRUPT, mode>
|
||||||
|
: public detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||||
|
detail::ControlFlagsC0, cfg, mode> {
|
||||||
public:
|
public:
|
||||||
using data_t = typename cfg::data_t;
|
[[gnu::always_inline]] static void init()
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
|
||||||
|
|
||||||
static void init() FORCE_INLINE
|
|
||||||
{
|
{
|
||||||
detail::fnRx0IntHandler = rxIntHandler;
|
|
||||||
detail::fnDataReg0EmptyIntHandler = dataRegEmptyIntHandler;
|
|
||||||
|
|
||||||
HardwareImpl::init();
|
HardwareImpl::init();
|
||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByte(const data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
|
|
||||||
while (tmpHead == sm_txBuf.tail)
|
|
||||||
;
|
|
||||||
|
|
||||||
sm_txBuf.buf[tmpHead] = byte;
|
|
||||||
sm_txBuf.head = tmpHead;
|
|
||||||
|
|
||||||
HardwareImpl::enableDataRegEmptyInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
|
||||||
sm_rxBuf.tail = tmpTail;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
|
||||||
{
|
|
||||||
return (sm_rxBuf.head != sm_rxBuf.tail);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
|
||||||
{
|
|
||||||
while (sm_txBuf.head != sm_txBuf.tail)
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txEmpty())
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txComplete())
|
|
||||||
;
|
|
||||||
HardwareImpl::clearTxComplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||||
detail::ControlFlagsC0, cfg, mode, Driven::INTERRUPT>;
|
detail::ControlFlagsC0, cfg, Driven::INTERRUPT, mode>;
|
||||||
|
|
||||||
static constexpr auto TX_BUFFER_SIZE = 16;
|
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0,
|
||||||
static constexpr auto RX_BUFFER_SIZE = 16;
|
detail::ControlFlagsB0, detail::ControlFlagsC0, cfg, mode>;
|
||||||
|
|
||||||
static volatile detail::RingBuffer<data_t, TX_BUFFER_SIZE> sm_txBuf;
|
// Must be friends with Uart interface to call these private handlers
|
||||||
static volatile detail::RingBuffer<data_t, RX_BUFFER_SIZE> sm_rxBuf;
|
template <class Driver>
|
||||||
|
friend class Uart;
|
||||||
|
|
||||||
static void rxIntHandler()
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
{
|
{
|
||||||
uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE;
|
InterruptHardwareImpl::rxIntHandler();
|
||||||
|
|
||||||
if (tmpHead != sm_rxBuf.tail) {
|
|
||||||
sm_rxBuf.head = tmpHead;
|
|
||||||
sm_rxBuf.buf[tmpHead] = HardwareImpl::rxByteInterrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dataRegEmptyIntHandler() FORCE_INLINE
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
{
|
{
|
||||||
if (sm_txBuf.head != sm_txBuf.tail) {
|
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||||
uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
|
|
||||||
sm_txBuf.tail = tmpTail;
|
|
||||||
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
|
|
||||||
} else
|
|
||||||
HardwareImpl::disableDataRegEmptyInt();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
|
||||||
volatile detail::RingBuffer<typename Hardware0<mode, cfg, Driven::INTERRUPT>::data_t,
|
|
||||||
Hardware0<mode, cfg, Driven::INTERRUPT>::TX_BUFFER_SIZE>
|
|
||||||
Hardware0<mode, cfg, Driven::INTERRUPT>::sm_txBuf = {0, 0, {0}};
|
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
|
||||||
volatile detail::RingBuffer<typename Hardware0<mode, cfg, Driven::INTERRUPT>::data_t,
|
|
||||||
Hardware0<mode, cfg, Driven::INTERRUPT>::RX_BUFFER_SIZE>
|
|
||||||
Hardware0<mode, cfg, Driven::INTERRUPT>::sm_rxBuf = {0, 0, {0}};
|
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// Forward declare interrupt functions to allow adding them as friends
|
||||||
|
extern "C" {
|
||||||
|
void USART0_RX_vect() __attribute__((signal));
|
||||||
|
void USART0_UDRE_vect() __attribute__((signal));
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
#define REGISTER_UART0_INT_VECTORS(uart_type) \
|
||||||
|
ISR(USART0_RX_vect) \
|
||||||
|
{ \
|
||||||
|
uart_type::rxIntHandler(); \
|
||||||
|
} \
|
||||||
|
ISR(USART0_UDRE_vect) \
|
||||||
|
{ \
|
||||||
|
uart_type::dataRegEmptyIntHandler(); \
|
||||||
|
} \
|
||||||
|
struct _##uart_type {}
|
||||||
|
// clang-format on
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "hardware1.hpp"
|
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
|
||||||
|
|
||||||
namespace uart {
|
|
||||||
namespace detail {
|
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__)
|
|
||||||
|
|
||||||
void (*fnRx1IntHandler)() = nullptr;
|
|
||||||
void (*fnDataReg1EmptyIntHandler)() = nullptr;
|
|
||||||
|
|
||||||
ISR(USART1_RX_vect)
|
|
||||||
{
|
|
||||||
if (fnRx1IntHandler)
|
|
||||||
fnRx1IntHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
ISR(USART1_UDRE_vect)
|
|
||||||
{
|
|
||||||
if (fnDataReg1EmptyIntHandler)
|
|
||||||
fnDataReg1EmptyIntHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
#error "This chip is not supported"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace detail
|
|
||||||
} // namespace uart
|
|
||||||
217
hardware1.hpp
217
hardware1.hpp
@@ -1,30 +1,45 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
#include <avr/sfr_defs.h>
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "hardware.hpp"
|
#include "hardware.hpp"
|
||||||
|
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
#if defined(__AVR_ATmega1284P__)
|
#if defined(__AVR_ATmega1284P__)
|
||||||
|
|
||||||
|
/*
|
||||||
|
The following works in avr-gcc 5.4.0, but is not legal C++, because ptr's are not legal constexpr's:
|
||||||
|
constexpr auto *foo = ptr;
|
||||||
|
|
||||||
|
Workaround is to store the the address of the ptr in a uintptr_t and reinterpret_cast it at call site.
|
||||||
|
The _SFR_ADDR macro in sfr_defs.h would give the address, but it does that by taking the address of the dereferenced
|
||||||
|
pointer and casts it to uint16_t, which is still not a legal constexpr.
|
||||||
|
The workaround therefore is to disable the pointer cast and dereference macro _MMIO_BYTE temporarily.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma push_macro("_MMIO_BYTE")
|
||||||
|
#undef _MMIO_BYTE
|
||||||
|
#define _MMIO_BYTE
|
||||||
|
|
||||||
struct Registers1 {
|
struct Registers1 {
|
||||||
static constexpr volatile auto *IO_REG = &UDR1;
|
static constexpr std::uintptr_t IO_REG_ADDR = UDR1;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_A = &UCSR1A;
|
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR1A;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_B = &UCSR1B;
|
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR1B;
|
||||||
static constexpr volatile auto *CTRL_STAT_REG_C = &UCSR1C;
|
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR1C;
|
||||||
static constexpr volatile auto *BAUD_REG_L = &UBRR1L;
|
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR1L;
|
||||||
static constexpr volatile auto *BAUD_REG_H = &UBRR1H;
|
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR1H;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#pragma pop_macro("_MMIO_BYTE")
|
||||||
|
|
||||||
enum class ControlFlagsA1 {
|
enum class ControlFlagsA1 {
|
||||||
MULTI_PROC_COMM_MODE = MPCM1,
|
MULTI_PROC_COMM_MODE = MPCM1,
|
||||||
SPEED_2X = U2X1,
|
SPEED_2X = U2X1,
|
||||||
@@ -64,175 +79,79 @@ constexpr int operator<<(const int &lhs, const ControlFlagsB1 &rhs) { return lhs
|
|||||||
constexpr int operator<<(const int &lhs, const ControlFlagsC1 &rhs) { return lhs << static_cast<int>(rhs); }
|
constexpr int operator<<(const int &lhs, const ControlFlagsC1 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
extern void (*fnRx1IntHandler)();
|
|
||||||
extern void (*fnDataReg1EmptyIntHandler)();
|
|
||||||
|
|
||||||
#define HAS_UART1
|
#define HAS_UART1
|
||||||
|
|
||||||
#else
|
|
||||||
#error "This chip is not supported"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
#ifdef HAS_UART1
|
#ifdef HAS_UART1
|
||||||
|
|
||||||
template <Mode mode = Mode::ASYNCHRONOUS, class cfg = Config<>, Driven driven = Driven::INTERRUPT>
|
template <class cfg = Config<>, Driven driven = Driven::INTERRUPT, Mode mode = Mode::ASYNCHRONOUS>
|
||||||
class Hardware1 {
|
class Hardware1 : public detail::BlockingHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||||
public:
|
detail::ControlFlagsC1, cfg, mode> {
|
||||||
using data_t = typename cfg::data_t;
|
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
|
||||||
|
|
||||||
static void init() FORCE_INLINE
|
|
||||||
{
|
|
||||||
HardwareImpl::init();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void txByte(const data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
HardwareImpl::txByteBlocking(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
return HardwareImpl::rxByteBlocking(byte);
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
static_cast<void>(byte);
|
|
||||||
static_assert(driven != Driven::BLOCKING, "Peek with data is not supported in blocking mode");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
|
||||||
{
|
|
||||||
return HardwareImpl::peekBlocking();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
|
||||||
{
|
|
||||||
while (!HardwareImpl::txEmpty())
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txComplete())
|
|
||||||
;
|
|
||||||
HardwareImpl::clearTxComplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
|
||||||
detail::ControlFlagsC1, cfg, mode, driven>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
template <class cfg, Mode mode>
|
||||||
class Hardware1<mode, cfg, Driven::INTERRUPT> {
|
class Hardware1<cfg, Driven::INTERRUPT, mode>
|
||||||
|
: public detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||||
|
detail::ControlFlagsC1, cfg, mode> {
|
||||||
public:
|
public:
|
||||||
using data_t = typename cfg::data_t;
|
[[gnu::always_inline]] static void init()
|
||||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
|
||||||
|
|
||||||
static void init() FORCE_INLINE
|
|
||||||
{
|
{
|
||||||
detail::fnRx1IntHandler = rxIntHandler;
|
|
||||||
detail::fnDataReg1EmptyIntHandler = dataRegEmptyIntHandler;
|
|
||||||
|
|
||||||
HardwareImpl::init();
|
HardwareImpl::init();
|
||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void txByte(const data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
uint8_t tmpHead = (sm_txBuf.head + 1) % TX_BUFFER_SIZE;
|
|
||||||
while (tmpHead == sm_txBuf.tail)
|
|
||||||
;
|
|
||||||
|
|
||||||
sm_txBuf.buf[tmpHead] = byte;
|
|
||||||
sm_txBuf.head = tmpHead;
|
|
||||||
|
|
||||||
HardwareImpl::enableDataRegEmptyInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool rxByte(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
|
||||||
sm_rxBuf.tail = tmpTail;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek(data_t &byte) FORCE_INLINE
|
|
||||||
{
|
|
||||||
if (sm_rxBuf.head == sm_rxBuf.tail)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
uint8_t tmpTail = (sm_rxBuf.tail + 1) % RX_BUFFER_SIZE;
|
|
||||||
byte = sm_rxBuf.buf[tmpTail];
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool peek() FORCE_INLINE
|
|
||||||
{
|
|
||||||
return (sm_rxBuf.head != sm_rxBuf.tail);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flushTx() FORCE_INLINE
|
|
||||||
{
|
|
||||||
while (sm_txBuf.head != sm_txBuf.tail)
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txEmpty())
|
|
||||||
;
|
|
||||||
while (!HardwareImpl::txComplete())
|
|
||||||
;
|
|
||||||
HardwareImpl::clearTxComplete();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||||
detail::ControlFlagsC1, cfg, mode, Driven::INTERRUPT>;
|
detail::ControlFlagsC1, cfg, Driven::INTERRUPT, mode>;
|
||||||
|
|
||||||
static constexpr auto TX_BUFFER_SIZE = 16;
|
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1,
|
||||||
static constexpr auto RX_BUFFER_SIZE = 16;
|
detail::ControlFlagsB1, detail::ControlFlagsC1, cfg, mode>;
|
||||||
|
|
||||||
static volatile detail::RingBuffer<data_t, TX_BUFFER_SIZE> sm_txBuf;
|
// Must be friends with Uart interface to call these private handlers
|
||||||
static volatile detail::RingBuffer<data_t, RX_BUFFER_SIZE> sm_rxBuf;
|
template <class Driver>
|
||||||
|
friend class Uart;
|
||||||
|
|
||||||
static void rxIntHandler()
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
{
|
{
|
||||||
uint8_t tmpHead = (sm_rxBuf.head + 1) % RX_BUFFER_SIZE;
|
InterruptHardwareImpl::rxIntHandler();
|
||||||
|
|
||||||
if (tmpHead != sm_rxBuf.tail) {
|
|
||||||
sm_rxBuf.head = tmpHead;
|
|
||||||
sm_rxBuf.buf[tmpHead] = HardwareImpl::rxByteInterrupt();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dataRegEmptyIntHandler() FORCE_INLINE
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
{
|
{
|
||||||
if (sm_txBuf.head != sm_txBuf.tail) {
|
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||||
uint8_t tmpTail = (sm_txBuf.tail + 1) % TX_BUFFER_SIZE;
|
|
||||||
sm_txBuf.tail = tmpTail;
|
|
||||||
HardwareImpl::txByteInterrupt(sm_txBuf.buf[tmpTail]);
|
|
||||||
} else
|
|
||||||
HardwareImpl::disableDataRegEmptyInt();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
|
||||||
volatile detail::RingBuffer<typename Hardware1<mode, cfg, Driven::INTERRUPT>::data_t,
|
|
||||||
Hardware1<mode, cfg, Driven::INTERRUPT>::TX_BUFFER_SIZE>
|
|
||||||
Hardware1<mode, cfg, Driven::INTERRUPT>::sm_txBuf = {0, 0, {0}};
|
|
||||||
|
|
||||||
template <Mode mode, class cfg>
|
|
||||||
volatile detail::RingBuffer<typename Hardware1<mode, cfg, Driven::INTERRUPT>::data_t,
|
|
||||||
Hardware1<mode, cfg, Driven::INTERRUPT>::RX_BUFFER_SIZE>
|
|
||||||
Hardware1<mode, cfg, Driven::INTERRUPT>::sm_rxBuf = {0, 0, {0}};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef HAS_UART1
|
||||||
|
|
||||||
|
// Forward declare interrupt functions to allow adding them as friends
|
||||||
|
extern "C" {
|
||||||
|
void USART1_RX_vect() __attribute__((signal));
|
||||||
|
void USART1_UDRE_vect() __attribute__((signal));
|
||||||
|
}
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
#define REGISTER_UART1_INT_VECTORS(uart_type) \
|
||||||
|
ISR(USART1_RX_vect) \
|
||||||
|
{ \
|
||||||
|
uart_type::rxIntHandler(); \
|
||||||
|
} \
|
||||||
|
ISR(USART1_UDRE_vect) \
|
||||||
|
{ \
|
||||||
|
uart_type::dataRegEmptyIntHandler(); \
|
||||||
|
} \
|
||||||
|
struct _##uart_type { \
|
||||||
|
}
|
||||||
|
// clang-format off
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "utils.hpp"
|
|
||||||
|
|
||||||
#include "../io/io.hpp"
|
#include "../io/io.hpp"
|
||||||
|
#include "../util/util.hpp"
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
|
|||||||
121
uart.hpp
121
uart.hpp
@@ -1,26 +1,27 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <limits>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
#include "software.hpp"
|
||||||
|
|
||||||
#include "hardware0.hpp"
|
#include "hardware0.hpp"
|
||||||
#include "hardware1.hpp"
|
#include "hardware1.hpp"
|
||||||
#include "software.hpp"
|
|
||||||
#include "utils.hpp"
|
|
||||||
|
|
||||||
#include "../flash/flash.hpp"
|
#include "../flash/flash.hpp"
|
||||||
|
#include "../util/util.hpp"
|
||||||
#define FORCE_INLINE __attribute__((always_inline))
|
|
||||||
|
|
||||||
namespace uart {
|
namespace uart {
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template <typename T, T limit, size_t Base>
|
template <typename T, T Limit, std::size_t Base>
|
||||||
static constexpr size_t cntDigits()
|
static constexpr std::size_t cntDigits()
|
||||||
{
|
{
|
||||||
T num = limit;
|
T num = Limit;
|
||||||
size_t cnt = 0;
|
std::size_t cnt = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
num /= Base;
|
num /= Base;
|
||||||
@@ -30,28 +31,16 @@ static constexpr size_t cntDigits()
|
|||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t Base>
|
template <typename T, std::size_t Base>
|
||||||
static constexpr size_t maxNumDigits()
|
static constexpr std::size_t maxNumDigits()
|
||||||
{
|
{
|
||||||
constexpr T minVal = util::NumericLimits<T>::min();
|
constexpr T MinVal = std::numeric_limits<T>::min();
|
||||||
constexpr T maxVal = util::NumericLimits<T>::max();
|
constexpr T MaxVal = std::numeric_limits<T>::max();
|
||||||
|
|
||||||
T minDigits = cntDigits<T, minVal, Base>() + ((minVal < 0) ? 1 : 0);
|
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
||||||
T maxDigits = cntDigits<T, maxVal, Base>() + ((maxVal < 0) ? 1 : 0);
|
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
||||||
|
|
||||||
return (minDigits < maxDigits) ? maxDigits : minDigits;
|
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits;
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, size_t Base>
|
|
||||||
static constexpr size_t maxPadding()
|
|
||||||
{
|
|
||||||
constexpr T minVal = util::NumericLimits<T>::min();
|
|
||||||
constexpr T maxVal = util::NumericLimits<T>::max();
|
|
||||||
|
|
||||||
T minDigits = cntDigits<T, minVal, Base>();
|
|
||||||
T maxDigits = cntDigits<T, maxVal, Base>();
|
|
||||||
|
|
||||||
return (minDigits < maxDigits) ? maxDigits : minDigits;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
@@ -121,19 +110,16 @@ class Uart {
|
|||||||
txByte(ch);
|
txByte(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
|
template <typename T, std::size_t Base = 10, std::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(std::is_integral_v<T>, "Only supported on integral types");
|
||||||
static_assert(Base >= 2, "Numbers with bases 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 bases higher than 16 are not supported");
|
static_assert(Base <= 16, "Numbers with base higher than 16 are not supported");
|
||||||
static_assert(Padding <= detail::maxPadding<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");
|
||||||
|
|
||||||
constexpr char alphaChar = (LowerCase) ? 'a' : 'A';
|
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
||||||
constexpr size_t numDigits = detail::maxNumDigits<T, Base>();
|
constexpr std::size_t NumDigits = detail::maxNumDigits<T, Base>();
|
||||||
|
|
||||||
data_t buffer[numDigits];
|
|
||||||
data_t *bufEnd = buffer + numDigits - 1;
|
|
||||||
|
|
||||||
T digits = val;
|
T digits = val;
|
||||||
|
|
||||||
@@ -142,23 +128,26 @@ class Uart {
|
|||||||
txByte('-');
|
txByte('-');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data_t buffer[NumDigits];
|
||||||
|
data_t *bufEnd = buffer + NumDigits - 1;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
data_t lastDigit = digits % Base;
|
const data_t lastDigit = digits % Base;
|
||||||
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (alphaChar + lastDigit - 10);
|
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (AlphaChar + lastDigit - 10);
|
||||||
digits /= Base;
|
digits /= Base;
|
||||||
} while (digits > 0);
|
} while (digits > 0);
|
||||||
|
|
||||||
if (Padding > 0) {
|
if (Padding > 0) {
|
||||||
size_t strLen = buffer + numDigits - (bufEnd + 1);
|
std::size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
||||||
|
|
||||||
if (Padding > strLen) {
|
if (Padding > strLen) {
|
||||||
for (size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
for (std::size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
||||||
*bufEnd-- = PadChar;
|
*bufEnd-- = PadChar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (data_t *buf = bufEnd + 1; buf < buffer + numDigits; ++buf)
|
for (data_t *buf = bufEnd + 1; buf < buffer + NumDigits; ++buf)
|
||||||
txByte(*buf);
|
txByte(*buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +190,7 @@ class Uart {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(unsigned short &val)
|
Uart &operator<<(const unsigned short &val)
|
||||||
{
|
{
|
||||||
txNumber(val);
|
txNumber(val);
|
||||||
return *this;
|
return *this;
|
||||||
@@ -225,19 +214,19 @@ class Uart {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(unsigned long &val)
|
Uart &operator<<(const unsigned long &val)
|
||||||
{
|
{
|
||||||
txNumber(val);
|
txNumber(val);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(long long &val)
|
Uart &operator<<(const long long &val)
|
||||||
{
|
{
|
||||||
txNumber(val);
|
txNumber(val);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(unsigned long long &val)
|
Uart &operator<<(const unsigned long long &val)
|
||||||
{
|
{
|
||||||
txNumber(val);
|
txNumber(val);
|
||||||
return *this;
|
return *this;
|
||||||
@@ -246,19 +235,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 implemented");
|
static_assert(util::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 implemented");
|
static_assert(util::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 implemented");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
Uart &operator<<(const bool &val)
|
Uart &operator<<(const bool &val)
|
||||||
@@ -270,7 +259,7 @@ class Uart {
|
|||||||
Uart &operator<<(const void *val)
|
Uart &operator<<(const void *val)
|
||||||
{
|
{
|
||||||
txString(F("0x"));
|
txString(F("0x"));
|
||||||
txNumber<uint16_t, 16>(reinterpret_cast<uint16_t>(val));
|
txNumber<std::uint16_t, 16, 4, '0', false>(reinterpret_cast<std::uint16_t>(val));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,19 +329,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 implemented");
|
static_assert(util::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 implemented");
|
static_assert(util::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 implemented");
|
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
@@ -366,17 +355,35 @@ class Uart {
|
|||||||
{
|
{
|
||||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend void ::USART0_RX_vect();
|
||||||
|
friend void ::USART0_UDRE_vect();
|
||||||
|
|
||||||
|
#ifdef HAS_UART1
|
||||||
|
friend void ::USART1_RX_vect();
|
||||||
|
friend void ::USART1_UDRE_vect();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void rxIntHandler()
|
||||||
|
{
|
||||||
|
Driver::rxIntHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||||
|
{
|
||||||
|
Driver::dataRegEmptyIntHandler();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename cfg = Config<>>
|
template <typename cfg = Config<>>
|
||||||
using Uart0 = Uart<Hardware0<Mode::ASYNCHRONOUS, cfg>>;
|
using Uart0 = Uart<Hardware0<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
||||||
|
|
||||||
#ifdef HAS_UART1
|
#ifdef HAS_UART1
|
||||||
template <typename cfg = Config<>>
|
template <typename cfg = Config<>>
|
||||||
using Uart1 = Uart<Hardware1<Mode::ASYNCHRONOUS, cfg>>;
|
using Uart1 = Uart<Hardware1<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|
||||||
#undef FORCE_INLINE
|
|
||||||
#undef HAS_UART1
|
#undef HAS_UART1
|
||||||
|
|||||||
143
utils.hpp
143
utils.hpp
@@ -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
|
|
||||||
Reference in New Issue
Block a user