Compare commits
94 Commits
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 | |||
87e6936051 | |||
1d633c538e | |||
231fc0de48 | |||
6438aa81c1 | |||
8153696309 | |||
9f9f7a8de5 | |||
b6c1c3b51b | |||
8d07e2d4db | |||
fe9e67036e | |||
9f7dc0da55 | |||
2a07744575 | |||
ecdefe40e7 | |||
53b791cb05 | |||
50e01c480d | |||
7d4eddbd8b | |||
778f5f9754 | |||
1ca8ea2061 | |||
80633998c7 | |||
2cd4069654 | |||
33c3cedb1e | |||
925bb56f98 | |||
a3d76a138d | |||
95963295e2 | |||
0bfd303a26 | |||
16f4be8c6c | |||
efe1446907 | |||
b90da9bd9c | |||
51a9d30c0a | |||
496bb3d4d1 | |||
5d6bc4761c | |||
3aeb43ee1e | |||
a13a1ca9ab | |||
bdf4bd169e | |||
6e45340993 | |||
f0a3cd6da8 | |||
6861b8f5d1 | |||
aac73447b3 | |||
fbd087808a | |||
65bbf5e96a | |||
c03196493a | |||
470eb06345 | |||
099be106a7 | |||
6ded0e1c8d | |||
00cb9ad13c | |||
4a9cee922a | |||
8e0ba5a463 | |||
a71035d4b7 | |||
fd7e8e7238 | |||
d9a05d0273 | |||
2bbba0fcbd | |||
29b4d85ce3 | |||
ae90fdae3f | |||
2364bff11f | |||
f08f607265 | |||
f9c34b09ba | |||
e07ba7ecd8 | |||
81e6a9fdc0 | |||
b2a3b03867 | |||
88eb11cc02 | |||
7fd6d8ddec | |||
c261d7d309 | |||
e008cc8da1 | |||
99d09b0f0f |
13
.clang-format
Normal file
13
.clang-format
Normal file
@ -0,0 +1,13 @@
|
||||
---
|
||||
BasedOnStyle: LLVM
|
||||
ColumnLimit: 120
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: ForIndentation
|
||||
AlignEscapedNewlines: DontAlign
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
AlwaysBreakTemplateDeclarations: true
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterFunction: true
|
||||
...
|
8
.gitignore
vendored
8
.gitignore
vendored
@ -2,4 +2,10 @@
|
||||
Release
|
||||
Debug
|
||||
*.componentinfo.xml
|
||||
avrdude.bat
|
||||
*.elf
|
||||
*.o
|
||||
*.hex
|
||||
*.srec
|
||||
*.eeprom
|
||||
*.lss
|
||||
*.map
|
||||
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "usart/array"]
|
||||
path = usart/array
|
||||
url = git@blackmark.me:array.git
|
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.
|
50
config.hpp
Normal file
50
config.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace uart {
|
||||
|
||||
enum class DataBits {
|
||||
FIVE,
|
||||
SIX,
|
||||
SEVEN,
|
||||
EIGHT,
|
||||
NINE,
|
||||
};
|
||||
|
||||
enum class StopBits {
|
||||
ONE,
|
||||
TWO,
|
||||
};
|
||||
|
||||
enum class Parity {
|
||||
NONE,
|
||||
ODD,
|
||||
EVEN,
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <DataBits dataBits>
|
||||
struct choose_data_type {
|
||||
using type = std::uint8_t;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct choose_data_type<DataBits::NINE> {
|
||||
using type = std::uint16_t;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <std::uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE,
|
||||
StopBits stopBits = StopBits::ONE>
|
||||
struct Config {
|
||||
static constexpr auto BAUD_RATE = baudRate;
|
||||
static constexpr auto DATA_BITS = dataBits;
|
||||
static constexpr auto PARITY = parity;
|
||||
static constexpr auto STOP_BITS = stopBits;
|
||||
using data_t = typename detail::choose_data_type<DATA_BITS>::type;
|
||||
};
|
||||
|
||||
} // namespace uart
|
423
hardware.hpp
Normal file
423
hardware.hpp
Normal file
@ -0,0 +1,423 @@
|
||||
#pragma once
|
||||
|
||||
#include "../clock.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace uart {
|
||||
|
||||
enum class Mode {
|
||||
ASYNCHRONOUS,
|
||||
SYNCHRONOUS_MASTER,
|
||||
SYNCHRONOUS_SLAVE,
|
||||
SPI,
|
||||
};
|
||||
|
||||
enum class Driven {
|
||||
INTERRUPT,
|
||||
BLOCKING,
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
using reg_ptr_t = volatile std::uint8_t *;
|
||||
|
||||
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 {
|
||||
public:
|
||||
[[gnu::always_inline]] static void init()
|
||||
{
|
||||
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");
|
||||
|
||||
constexpr auto UseDoubleSpeed = (AbsDoubleError < AbsNormalError);
|
||||
constexpr auto BaudVal = calcBaudVal<UseDoubleSpeed>();
|
||||
|
||||
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<std::uint8_t>(BaudVal >> 8);
|
||||
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<std::uint8_t>(BaudVal);
|
||||
|
||||
constexpr auto DataBitsValues = calcDataBits();
|
||||
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();
|
||||
|
||||
constexpr std::uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
|
||||
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;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool rxByteBlocking(typename cfg::data_t &byte)
|
||||
{
|
||||
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||
byte = *getRegPtr<Registers::IO_REG_ADDR>();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static typename cfg::data_t rxByteInterrupt()
|
||||
{
|
||||
return *getRegPtr<Registers::IO_REG_ADDR>();
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool txEmpty()
|
||||
{
|
||||
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::DATA_REG_EMPTY);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool txComplete()
|
||||
{
|
||||
return *getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void clearTxComplete()
|
||||
{
|
||||
*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() |= (1 << CtrlFlagsA::TRANSMIT_COMPLETE);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void txByteBlocking(const typename cfg::data_t &byte)
|
||||
{
|
||||
while (!txEmpty())
|
||||
;
|
||||
|
||||
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void txByteInterrupt(volatile const typename cfg::data_t &byte)
|
||||
{
|
||||
*getRegPtr<Registers::IO_REG_ADDR>() = byte;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool peekBlocking()
|
||||
{
|
||||
if (*getRegPtr<Registers::CTRL_STAT_REG_A_ADDR>() & (1 << CtrlFlagsA::RECEIVE_COMPLETE)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void enableDataRegEmptyInt()
|
||||
{
|
||||
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||
*ctrlStatRegB = *ctrlStatRegB | (1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void disableDataRegEmptyInt()
|
||||
{
|
||||
auto ctrlStatRegB = getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>();
|
||||
*ctrlStatRegB = *ctrlStatRegB & ~(1 << CtrlFlagsB::DATA_REG_EMPTY_INT_ENABLE);
|
||||
}
|
||||
|
||||
private:
|
||||
struct DataBitsVal {
|
||||
std::uint8_t regCVal = 0;
|
||||
std::uint8_t regBVal = 0;
|
||||
};
|
||||
|
||||
template <bool DoubleSpeed = true>
|
||||
static constexpr auto calcBaudVal()
|
||||
{
|
||||
if constexpr (DoubleSpeed) {
|
||||
constexpr auto BaudVal = static_cast<std::uint16_t>(round(F_CPU / (8.0 * cfg::BAUD_RATE) - 1));
|
||||
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()
|
||||
{
|
||||
DataBitsVal dataBitsVal;
|
||||
|
||||
switch (cfg::DATA_BITS) {
|
||||
case DataBits::FIVE:
|
||||
dataBitsVal.regCVal = 0;
|
||||
break;
|
||||
case DataBits::SIX:
|
||||
dataBitsVal.regCVal = (1 << CtrlFlagsC::CHAR_SIZE_0);
|
||||
break;
|
||||
case DataBits::SEVEN:
|
||||
dataBitsVal.regCVal = (1 << CtrlFlagsC::CHAR_SIZE_1);
|
||||
break;
|
||||
case DataBits::EIGHT:
|
||||
dataBitsVal.regCVal = (1 << CtrlFlagsC::CHAR_SIZE_1) | (1 << CtrlFlagsC::CHAR_SIZE_0);
|
||||
break;
|
||||
case DataBits::NINE:
|
||||
dataBitsVal.regCVal = (1 << CtrlFlagsC::CHAR_SIZE_1) | (1 << CtrlFlagsC::CHAR_SIZE_0);
|
||||
dataBitsVal.regBVal = (1 << CtrlFlagsB::CHAR_SIZE_2);
|
||||
break;
|
||||
}
|
||||
|
||||
return dataBitsVal;
|
||||
}
|
||||
|
||||
static constexpr auto calcParity()
|
||||
{
|
||||
std::uint8_t parityVal = 0;
|
||||
|
||||
if (cfg::PARITY == Parity::EVEN)
|
||||
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1);
|
||||
else if (cfg::PARITY == Parity::ODD)
|
||||
parityVal = (1 << CtrlFlagsC::PARITY_MODE_1) | (1 << CtrlFlagsC::PARITY_MODE_0);
|
||||
|
||||
return parityVal;
|
||||
}
|
||||
|
||||
static constexpr auto calcStopBits()
|
||||
{
|
||||
std::uint8_t stopBitsVal = 0;
|
||||
|
||||
if (cfg::STOP_BITS == StopBits::TWO)
|
||||
stopBitsVal = (1 << CtrlFlagsC::STOP_BIT_SEL);
|
||||
|
||||
return stopBitsVal;
|
||||
}
|
||||
|
||||
static constexpr auto calcMode()
|
||||
{
|
||||
static_assert(mode != Mode::SPI, "SPI mode can not be used with uart");
|
||||
|
||||
std::uint8_t modeVal = 0;
|
||||
|
||||
if (mode == Mode::SYNCHRONOUS_MASTER || mode == Mode::SYNCHRONOUS_SLAVE) {
|
||||
modeVal = (1 << CtrlFlagsC::MODE_SEL_0);
|
||||
}
|
||||
|
||||
return modeVal;
|
||||
}
|
||||
|
||||
template <bool Enable>
|
||||
static constexpr auto calcRxState()
|
||||
{
|
||||
std::uint8_t enableVal = 0;
|
||||
|
||||
if (Enable)
|
||||
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
||||
|
||||
return enableVal;
|
||||
}
|
||||
|
||||
template <bool Enable>
|
||||
static constexpr auto calcTxState()
|
||||
{
|
||||
std::uint8_t enableVal = 0;
|
||||
|
||||
if (Enable)
|
||||
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
||||
|
||||
return enableVal;
|
||||
}
|
||||
|
||||
static constexpr auto calcInterrupt()
|
||||
{
|
||||
std::uint8_t interruptVal = 0;
|
||||
|
||||
if (driven == Driven::INTERRUPT)
|
||||
interruptVal = (1 << CtrlFlagsB::RX_INT_ENABLE);
|
||||
|
||||
return interruptVal;
|
||||
}
|
||||
};
|
||||
|
||||
template <class Registers, typename CtrlFlagsA, typename CtrlFlagsB, typename CtrlFlagsC, class cfg, Mode mode>
|
||||
class BlockingHardware {
|
||||
public:
|
||||
using data_t = typename cfg::data_t;
|
||||
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 uart
|
152
hardware0.hpp
Normal file
152
hardware0.hpp
Normal file
@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/sfr_defs.h>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "hardware.hpp"
|
||||
|
||||
namespace uart {
|
||||
|
||||
namespace detail {
|
||||
|
||||
#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 {
|
||||
static constexpr std::uintptr_t IO_REG_ADDR = UDR0;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR0A;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR0B;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR0C;
|
||||
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR0L;
|
||||
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR0H;
|
||||
};
|
||||
|
||||
#pragma pop_macro("_MMIO_BYTE")
|
||||
|
||||
enum class ControlFlagsA0 {
|
||||
MULTI_PROC_COMM_MODE = MPCM0,
|
||||
SPEED_2X = U2X0,
|
||||
PARITY_ERROR = UPE0,
|
||||
DATA_OVER_RUN = DOR0,
|
||||
FRAME_ERROR = FE0,
|
||||
DATA_REG_EMPTY = UDRE0,
|
||||
TRANSMIT_COMPLETE = TXC0,
|
||||
RECEIVE_COMPLETE = RXC0,
|
||||
};
|
||||
|
||||
enum class ControlFlagsB0 {
|
||||
TX_DATA_BIT_8 = TXB80,
|
||||
RX_DATA_BIT_8 = RXB80,
|
||||
CHAR_SIZE_2 = UCSZ02,
|
||||
TX_ENABLE = TXEN0,
|
||||
RX_ENABLE = RXEN0,
|
||||
DATA_REG_EMPTY_INT_ENABLE = UDRIE0,
|
||||
TX_INT_ENABLE = TXCIE0,
|
||||
RX_INT_ENABLE = RXCIE0,
|
||||
};
|
||||
|
||||
enum class ControlFlagsC0 {
|
||||
CLK_POLARITY = UCPOL0,
|
||||
CHAR_SIZE_0 = UCSZ00,
|
||||
CHAR_SIZE_1 = UCSZ01,
|
||||
STOP_BIT_SEL = USBS0,
|
||||
PARITY_MODE_0 = UPM00,
|
||||
PARITY_MODE_1 = UPM01,
|
||||
MODE_SEL_0 = UMSEL00,
|
||||
MODE_SEL_1 = UMSEL01,
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
constexpr int operator<<(const int &lhs, const ControlFlagsA0 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||
constexpr int operator<<(const int &lhs, const ControlFlagsB0 &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
|
||||
|
||||
#if defined(__AVR_ATmega328P__)
|
||||
#define USART0_RX_vect USART_RX_vect
|
||||
#define USART0_UDRE_vect USART_UDRE_vect
|
||||
#endif
|
||||
|
||||
#else
|
||||
#error "This chip is not supported"
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <class cfg = Config<>, Driven driven = Driven::INTERRUPT, Mode mode = Mode::ASYNCHRONOUS>
|
||||
class Hardware0 : public detail::BlockingHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||
detail::ControlFlagsC0, cfg, mode> {
|
||||
};
|
||||
|
||||
template <class cfg, Mode mode>
|
||||
class Hardware0<cfg, Driven::INTERRUPT, mode>
|
||||
: public detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||
detail::ControlFlagsC0, cfg, mode> {
|
||||
public:
|
||||
[[gnu::always_inline]] static void init()
|
||||
{
|
||||
HardwareImpl::init();
|
||||
sei();
|
||||
}
|
||||
|
||||
private:
|
||||
using HardwareImpl = detail::Hardware<detail::Registers0, detail::ControlFlagsA0, detail::ControlFlagsB0,
|
||||
detail::ControlFlagsC0, cfg, Driven::INTERRUPT, mode>;
|
||||
|
||||
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers0, detail::ControlFlagsA0,
|
||||
detail::ControlFlagsB0, detail::ControlFlagsC0, cfg, mode>;
|
||||
|
||||
// Must be friends with Uart interface to call these private handlers
|
||||
template <class Driver>
|
||||
friend class Uart;
|
||||
|
||||
[[gnu::always_inline]] static void rxIntHandler()
|
||||
{
|
||||
InterruptHardwareImpl::rxIntHandler();
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||
{
|
||||
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace uart
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// 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
|
157
hardware1.hpp
Normal file
157
hardware1.hpp
Normal file
@ -0,0 +1,157 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#include <avr/sfr_defs.h>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "hardware.hpp"
|
||||
|
||||
namespace uart {
|
||||
|
||||
namespace detail {
|
||||
|
||||
#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 {
|
||||
static constexpr std::uintptr_t IO_REG_ADDR = UDR1;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_A_ADDR = UCSR1A;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_B_ADDR = UCSR1B;
|
||||
static constexpr std::uintptr_t CTRL_STAT_REG_C_ADDR = UCSR1C;
|
||||
static constexpr std::uintptr_t BAUD_REG_L_ADDR = UBRR1L;
|
||||
static constexpr std::uintptr_t BAUD_REG_H_ADDR = UBRR1H;
|
||||
};
|
||||
|
||||
#pragma pop_macro("_MMIO_BYTE")
|
||||
|
||||
enum class ControlFlagsA1 {
|
||||
MULTI_PROC_COMM_MODE = MPCM1,
|
||||
SPEED_2X = U2X1,
|
||||
PARITY_ERROR = UPE1,
|
||||
DATA_OVER_RUN = DOR1,
|
||||
FRAME_ERROR = FE1,
|
||||
DATA_REG_EMPTY = UDRE1,
|
||||
TRANSMIT_COMPLETE = TXC1,
|
||||
RECEIVE_COMPLETE = RXC1,
|
||||
};
|
||||
|
||||
enum class ControlFlagsB1 {
|
||||
TX_DATA_BIT_8 = TXB81,
|
||||
RX_DATA_BIT_8 = RXB81,
|
||||
CHAR_SIZE_2 = UCSZ12,
|
||||
TX_ENABLE = TXEN1,
|
||||
RX_ENABLE = RXEN1,
|
||||
DATA_REG_EMPTY_INT_ENABLE = UDRIE1,
|
||||
TX_INT_ENABLE = TXCIE1,
|
||||
RX_INT_ENABLE = RXCIE1,
|
||||
};
|
||||
|
||||
enum class ControlFlagsC1 {
|
||||
CLK_POLARITY = UCPOL1,
|
||||
CHAR_SIZE_0 = UCSZ10,
|
||||
CHAR_SIZE_1 = UCSZ11,
|
||||
STOP_BIT_SEL = USBS1,
|
||||
PARITY_MODE_0 = UPM10,
|
||||
PARITY_MODE_1 = UPM11,
|
||||
MODE_SEL_0 = UMSEL10,
|
||||
MODE_SEL_1 = UMSEL11,
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
constexpr int operator<<(const int &lhs, const ControlFlagsA1 &rhs) { return lhs << static_cast<int>(rhs); }
|
||||
constexpr int operator<<(const int &lhs, const ControlFlagsB1 &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
|
||||
|
||||
#define HAS_UART1
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#ifdef HAS_UART1
|
||||
|
||||
template <class cfg = Config<>, Driven driven = Driven::INTERRUPT, Mode mode = Mode::ASYNCHRONOUS>
|
||||
class Hardware1 : public detail::BlockingHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||
detail::ControlFlagsC1, cfg, mode> {
|
||||
};
|
||||
|
||||
template <class cfg, Mode mode>
|
||||
class Hardware1<cfg, Driven::INTERRUPT, mode>
|
||||
: public detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||
detail::ControlFlagsC1, cfg, mode> {
|
||||
public:
|
||||
[[gnu::always_inline]] static void init()
|
||||
{
|
||||
HardwareImpl::init();
|
||||
sei();
|
||||
}
|
||||
|
||||
private:
|
||||
using HardwareImpl = detail::Hardware<detail::Registers1, detail::ControlFlagsA1, detail::ControlFlagsB1,
|
||||
detail::ControlFlagsC1, cfg, Driven::INTERRUPT, mode>;
|
||||
|
||||
using InterruptHardwareImpl = detail::InterruptHardware<detail::Registers1, detail::ControlFlagsA1,
|
||||
detail::ControlFlagsB1, detail::ControlFlagsC1, cfg, mode>;
|
||||
|
||||
// Must be friends with Uart interface to call these private handlers
|
||||
template <class Driver>
|
||||
friend class Uart;
|
||||
|
||||
[[gnu::always_inline]] static void rxIntHandler()
|
||||
{
|
||||
InterruptHardwareImpl::rxIntHandler();
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void dataRegEmptyIntHandler()
|
||||
{
|
||||
InterruptHardwareImpl::dataRegEmptyIntHandler();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace uart
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#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
|
||||
|
21
software.hpp
Normal file
21
software.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
#include "../io/io.hpp"
|
||||
#include "../util/util.hpp"
|
||||
|
||||
namespace uart {
|
||||
|
||||
template <io::P rxPin, io::P txPin, class cfg = Config<>>
|
||||
class Software {
|
||||
static_assert(util::always_false_v<cfg>, "Not implemented");
|
||||
|
||||
public:
|
||||
using data_t = typename cfg::data_t;
|
||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||
|
||||
static void init() {}
|
||||
};
|
||||
|
||||
} // namespace uart
|
389
uart.hpp
Normal file
389
uart.hpp
Normal file
@ -0,0 +1,389 @@
|
||||
#pragma once
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "config.hpp"
|
||||
#include "software.hpp"
|
||||
|
||||
#include "hardware0.hpp"
|
||||
#include "hardware1.hpp"
|
||||
|
||||
#include "../flash/flash.hpp"
|
||||
#include "../util/util.hpp"
|
||||
|
||||
namespace uart {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, T Limit, std::size_t Base>
|
||||
static constexpr std::size_t cntDigits()
|
||||
{
|
||||
T num = Limit;
|
||||
std::size_t cnt = 0;
|
||||
|
||||
do {
|
||||
num /= Base;
|
||||
++cnt;
|
||||
} while (num > 0);
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t Base>
|
||||
static constexpr std::size_t maxNumDigits()
|
||||
{
|
||||
constexpr T MinVal = std::numeric_limits<T>::min();
|
||||
constexpr T MaxVal = std::numeric_limits<T>::max();
|
||||
|
||||
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
||||
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
||||
|
||||
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <class Driver>
|
||||
class Uart {
|
||||
public:
|
||||
using data_t = typename Driver::data_t;
|
||||
|
||||
// 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;
|
||||
|
||||
// Moving and copying uart objects is not supported
|
||||
Uart(const Uart &) = delete;
|
||||
Uart(Uart &&) = delete;
|
||||
Uart &operator=(const Uart &) = delete;
|
||||
Uart &operator=(Uart &&) = delete;
|
||||
|
||||
// Before using the uart init must be called
|
||||
static void init()
|
||||
{
|
||||
Driver::init();
|
||||
}
|
||||
|
||||
static void txByte(const data_t &byte)
|
||||
{
|
||||
Driver::txByte(byte);
|
||||
}
|
||||
|
||||
static bool rxByte(data_t &byte)
|
||||
{
|
||||
return Driver::rxByte(byte);
|
||||
}
|
||||
|
||||
static bool peek(data_t &byte)
|
||||
{
|
||||
return Driver::peek(byte);
|
||||
}
|
||||
|
||||
static bool peek()
|
||||
{
|
||||
return Driver::peek();
|
||||
}
|
||||
|
||||
static void flushTx()
|
||||
{
|
||||
Driver::flushTx();
|
||||
}
|
||||
|
||||
static void txString(const char *str)
|
||||
{
|
||||
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
||||
|
||||
while (char ch = *str++)
|
||||
txByte(ch);
|
||||
}
|
||||
|
||||
static void txString(const ::detail::FlashString *str)
|
||||
{
|
||||
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
||||
|
||||
const char *strIt = reinterpret_cast<const char *>(str);
|
||||
|
||||
while (char ch = pgm_read_byte(strIt++))
|
||||
txByte(ch);
|
||||
}
|
||||
|
||||
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_assert(std::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 <= 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");
|
||||
|
||||
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
||||
constexpr std::size_t NumDigits = detail::maxNumDigits<T, Base>();
|
||||
|
||||
T digits = val;
|
||||
|
||||
if (digits < 0) {
|
||||
digits = -digits;
|
||||
txByte('-');
|
||||
}
|
||||
|
||||
data_t buffer[NumDigits];
|
||||
data_t *bufEnd = buffer + NumDigits - 1;
|
||||
|
||||
do {
|
||||
const data_t lastDigit = digits % Base;
|
||||
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (AlphaChar + lastDigit - 10);
|
||||
digits /= Base;
|
||||
} while (digits > 0);
|
||||
|
||||
if (Padding > 0) {
|
||||
std::size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
||||
|
||||
if (Padding > strLen) {
|
||||
for (std::size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
||||
*bufEnd-- = PadChar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (data_t *buf = bufEnd + 1; buf < buffer + NumDigits; ++buf)
|
||||
txByte(*buf);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Output stream overloads
|
||||
|
||||
Uart &operator<<(const char *str)
|
||||
{
|
||||
txString(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const ::detail::FlashString *str)
|
||||
{
|
||||
txString(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const char &val)
|
||||
{
|
||||
txByte(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const signed char &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const unsigned char &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const short &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const unsigned short &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const int &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const unsigned int &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const long &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const unsigned long &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const long long &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const unsigned long long &val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator<<(float) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator<<(double) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator<<(long double) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
Uart &operator<<(const bool &val)
|
||||
{
|
||||
txString(val ? F("true") : F("false"));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart &operator<<(const void *val)
|
||||
{
|
||||
txString(F("0x"));
|
||||
txNumber<std::uint16_t, 16, 4, '0', false>(reinterpret_cast<std::uint16_t>(val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Input stream overloads
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(char &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(unsigned char &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(short &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(unsigned short &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(int &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(unsigned int &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(long &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(unsigned long &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(long long &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(unsigned long long &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(float &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(double &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(long double &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(bool &) const
|
||||
{
|
||||
static_assert(util::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template <typename... Ts>
|
||||
Uart &operator>>(const void *&) const
|
||||
{
|
||||
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<>>
|
||||
using Uart0 = Uart<Hardware0<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
||||
|
||||
#ifdef HAS_UART1
|
||||
template <typename cfg = Config<>>
|
||||
using Uart1 = Uart<Hardware1<cfg, Driven::INTERRUPT, Mode::ASYNCHRONOUS>>;
|
||||
#endif
|
||||
|
||||
} // namespace uart
|
||||
|
||||
#undef HAS_UART1
|
22
usart.atsln
22
usart.atsln
@ -1,22 +0,0 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Atmel Studio Solution File, Format Version 11.00
|
||||
VisualStudioVersion = 14.0.23107.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "usart", "usart\usart.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|AVR = Debug|AVR
|
||||
Release|AVR = Release|AVR
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
|
||||
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
|
||||
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
|
||||
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
@ -1 +0,0 @@
|
||||
Subproject commit 634564a7bb6f34305dbc6f68993a34a4419899a6
|
@ -1,14 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) by BlackMark 2015
|
||||
* Date 24/11/2015
|
||||
* Version 1.1
|
||||
*/
|
||||
|
||||
#ifndef CLOCK_H
|
||||
#define CLOCK_H
|
||||
|
||||
#define F_CPU 8000000
|
||||
|
||||
#include <util/delay.h>
|
||||
|
||||
#endif
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) by BlackMark 2015-2016
|
||||
* Date 21/05/2016
|
||||
* Version 1.5
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "clock.h"
|
||||
#include "usart.h"
|
||||
#include "array/array.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
sei();
|
||||
USART0 &cUSART = USART0::inst();
|
||||
cUSART.init();
|
||||
|
||||
uint32_t ui32Counter = 0;
|
||||
|
||||
array<char, 64> arrBuffer;
|
||||
|
||||
cUSART << "\r\nSizes: \r\n";
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( char ) );
|
||||
cUSART << "sizeof( char ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned char ) );
|
||||
cUSART << "sizeof( unsigned char ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( short int ) );
|
||||
cUSART << "sizeof( short int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned short int ) );
|
||||
cUSART << "sizeof( unsigned short int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( int ) );
|
||||
cUSART << "sizeof( int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned int ) );
|
||||
cUSART << "sizeof( unsigned int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long int ) );
|
||||
cUSART << "sizeof( long int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long int ) );
|
||||
cUSART << "sizeof( unsigned long int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long long int ) );
|
||||
cUSART << "sizeof( long long int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long long int ) );
|
||||
cUSART << "sizeof( unsigned long long int ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( float ) );
|
||||
cUSART << "sizeof( float ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( double ) );
|
||||
cUSART << "sizeof( double ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long double ) );
|
||||
cUSART << "sizeof( long double ) = " << arrBuffer.data();
|
||||
|
||||
sprintf( arrBuffer.data(), "%d\r\n\r\n", sizeof( void* ) );
|
||||
cUSART << "sizeof( void* ) = " << arrBuffer.data();
|
||||
|
||||
while( true )
|
||||
{
|
||||
sprintf( arrBuffer.data(), "%lu", ui32Counter++ );
|
||||
cUSART << "This has been running for \"" << arrBuffer.data() << "\" seconds!\r\n";
|
||||
|
||||
if( !cUSART.receiveLine( arrBuffer.data(), arrBuffer.size(), "\r" ) )
|
||||
{
|
||||
cUSART << "Receive error: ";
|
||||
}
|
||||
else
|
||||
{
|
||||
cUSART << "Echo: ";
|
||||
}
|
||||
|
||||
cUSART << arrBuffer.data() << "\r\n";
|
||||
|
||||
uint8_t ui8Byte;
|
||||
|
||||
if( cUSART.receiveByte( ui8Byte, 1000 ) )
|
||||
{
|
||||
cUSART.transmitByte( ui8Byte );
|
||||
cUSART << "\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
474
usart/usart.cpp
474
usart/usart.cpp
@ -1,474 +0,0 @@
|
||||
#include "usart.h"
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART0 USART0::sm_cInstance;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART0::USART0()
|
||||
{
|
||||
#ifdef USART_SHAREDIO
|
||||
m_vui8pUCSRA = &UCSRA;
|
||||
m_vui8pUCSRB = &UCSRB;
|
||||
m_vui8pUCSRC = &UCSRC;
|
||||
m_vui8pUBRRH = &UBRRH;
|
||||
m_vui8pUBRRL = &UBRRL;
|
||||
m_vui8pUDR = &UDR;
|
||||
#endif
|
||||
|
||||
#ifndef USART_SHAREDIO
|
||||
m_vui8pUCSRA = &UCSR0A;
|
||||
m_vui8pUCSRB = &UCSR0B;
|
||||
m_vui8pUCSRC = &UCSR0C;
|
||||
m_vui8pUBRRH = &UBRR0H;
|
||||
m_vui8pUBRRL = &UBRR0L;
|
||||
m_vui8pUDR = &UDR0;
|
||||
#endif
|
||||
|
||||
m_vsizeRXBufferHead = 0;
|
||||
m_vsizeRXBufferTail = 0;
|
||||
m_vsizeTXBufferHead = 0;
|
||||
m_vsizeTXBufferTail = 0;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
uint8_t USART0::readUCSRC()
|
||||
{
|
||||
uint8_t ui8UCSRC;
|
||||
|
||||
#ifdef USART_SHAREDIO
|
||||
ui8UCSRC = UBRRH;
|
||||
ui8UCSRC = UCSRC;
|
||||
#else
|
||||
ui8UCSRC = *m_vui8pUCSRC;
|
||||
#endif
|
||||
|
||||
return ui8UCSRC;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setUCSRC( uint8_t ui8UCSRC )
|
||||
{
|
||||
#ifdef USART_SHAREDIO
|
||||
*m_vui8pUCSRC = ( 1 << URSEL ) | ui8UCSRC;
|
||||
#else
|
||||
*m_vui8pUCSRC = ui8UCSRC;
|
||||
#endif
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setRXState( bool bEnable )
|
||||
{
|
||||
if( bEnable )
|
||||
{
|
||||
*m_vui8pUCSRB |= ( 1 << RXEN_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
*m_vui8pUCSRB &= ~( 1 << RXEN_D );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setTXState( bool bEnable )
|
||||
{
|
||||
if( bEnable )
|
||||
{
|
||||
*m_vui8pUCSRB |= ( 1 << TXEN_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
*m_vui8pUCSRB &= ~( 1 << TXEN_D );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setRXInterrupt( bool bEnable )
|
||||
{
|
||||
if( bEnable )
|
||||
{
|
||||
*m_vui8pUCSRB |= ( 1 << RXCIE_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
*m_vui8pUCSRB &= ~( 1 << RXCIE_D );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setUDREInterrupt( bool bEnable )
|
||||
{
|
||||
if( bEnable )
|
||||
{
|
||||
*m_vui8pUCSRB |= ( 1 << UDRIE_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
*m_vui8pUCSRB &= ~( 1 << UDRIE_D );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setBaudRate( uint32_t ui32BaudRate )
|
||||
{
|
||||
uint16_t ui16UBRR = ( ( F_CPU / ( 16 * ui32BaudRate ) ) - 1 );
|
||||
*m_vui8pUBRRH = static_cast<uint8_t>( ui16UBRR >> 8 );
|
||||
*m_vui8pUBRRL = static_cast<uint8_t>( ui16UBRR );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setDataBits( uint8_t ui8DataBits )
|
||||
{
|
||||
uint8_t ui8UCSRC = readUCSRC();
|
||||
|
||||
if( ui8DataBits < 5 )
|
||||
{
|
||||
ui8DataBits = 5;
|
||||
}
|
||||
else if( ui8DataBits > 9 )
|
||||
{
|
||||
ui8DataBits = 9;
|
||||
}
|
||||
|
||||
if( ui8DataBits <= 8 )
|
||||
{
|
||||
bool bZeroBit = ( ui8DataBits - 5 ) & 1;
|
||||
bool bOneBit = ( ( ui8DataBits - 5 ) >> 1 ) & 1;
|
||||
|
||||
if( bZeroBit )
|
||||
{
|
||||
ui8UCSRC |= ( 1 << UCSZ0_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
ui8UCSRC &= ~( 1 << UCSZ0_D );
|
||||
}
|
||||
|
||||
if( bOneBit )
|
||||
{
|
||||
ui8UCSRC |= ( 1 << UCSZ1_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
ui8UCSRC &= ~( 1 << UCSZ1_D );
|
||||
}
|
||||
|
||||
*m_vui8pUCSRB &= ~( 1 << UCSZ2_D );
|
||||
}
|
||||
else
|
||||
{
|
||||
ui8UCSRC |= ( 1 << UCSZ1_D ) | ( 1 << UCSZ0_D );
|
||||
*m_vui8pUCSRB |= ( 1 << UCSZ2_D );
|
||||
}
|
||||
|
||||
setUCSRC( ui8UCSRC );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setParity( Parity enmParity )
|
||||
{
|
||||
uint8_t ui8UCSRC = readUCSRC();
|
||||
|
||||
if( enmParity == Parity::DISABLED )
|
||||
{
|
||||
ui8UCSRC &= ~( ( 1 << UPM1_D ) | ( 1 << UPM0_D ) );
|
||||
}
|
||||
else if( enmParity == Parity::ODD )
|
||||
{
|
||||
ui8UCSRC |= ( ( 1 << UPM1_D ) | ( 1 << UPM0_D ) );
|
||||
}
|
||||
else if( enmParity == Parity::EVEN )
|
||||
{
|
||||
ui8UCSRC &= ~( ( 1 << UPM0_D ) );
|
||||
ui8UCSRC |= ( ( 1 << UPM1_D ) );
|
||||
}
|
||||
|
||||
setUCSRC( ui8UCSRC );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setStopBits( StopBit enmStopBits )
|
||||
{
|
||||
uint8_t ui8UCSRC = readUCSRC();
|
||||
|
||||
if( enmStopBits == StopBit::ONE )
|
||||
{
|
||||
ui8UCSRC &= ~( 1 << USBS_D );
|
||||
}
|
||||
else if( enmStopBits == StopBit::TWO )
|
||||
{
|
||||
ui8UCSRC |= ( 1 << USBS_D );
|
||||
}
|
||||
|
||||
setUCSRC( ui8UCSRC );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::setMode( Mode enmMode )
|
||||
{
|
||||
uint8_t ui8UCSRC = readUCSRC();
|
||||
|
||||
#ifdef USART_SPI
|
||||
if( enmMode == Mode::ASYNCHRONOUS )
|
||||
{
|
||||
ui8UCSRC &= ~( ( 1 << UMSEL1_D ) | ( 1 << UMSEL0_D ) );
|
||||
}
|
||||
else if( enmMode == Mode::SYNCHRONOUS )
|
||||
{
|
||||
ui8UCSRC &= ~( 1 << UMSEL1_D );
|
||||
ui8UCSRC |= ( 1 << UMSEL0_D );
|
||||
}
|
||||
else if( enmMode == Mode::MASTERSPI )
|
||||
{
|
||||
ui8UCSRC |= ( ( 1 << UMSEL1_D ) | ( 1 << UMSEL0_D ) );
|
||||
}
|
||||
#else
|
||||
if( enmMode == Mode::ASYNCHRONOUS )
|
||||
{
|
||||
ui8UCSRC &= ~( 1 << UMSEL_D );
|
||||
}
|
||||
else if( enmMode == Mode::SYNCHRONOUS )
|
||||
{
|
||||
ui8UCSRC |= ( 1 << UMSEL_D );
|
||||
}
|
||||
#endif
|
||||
|
||||
setUCSRC( ui8UCSRC );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART0::~USART0()
|
||||
{
|
||||
flushTransmit();
|
||||
|
||||
setBaudRate( 0 );
|
||||
|
||||
setRXState( false );
|
||||
setTXState( false );
|
||||
|
||||
setRXInterrupt( false );
|
||||
setUDREInterrupt( false );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART0& USART0::inst()
|
||||
{
|
||||
return sm_cInstance;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::init( uint32_t ui32BaudRate /* = 9600 */, uint8_t ui8DataBits /* = 8 */, Parity enmParity /* = Parity::DISABLED */, StopBit enmStopBits /* = StopBit::ONE */, Mode enmMode /* = Mode::ASYNCHRONOUS */ )
|
||||
{
|
||||
setBaudRate( ui32BaudRate );
|
||||
setDataBits( ui8DataBits );
|
||||
setParity( enmParity );
|
||||
setStopBits( enmStopBits );
|
||||
setMode( enmMode );
|
||||
|
||||
setRXState( true );
|
||||
setTXState( true );
|
||||
|
||||
setRXInterrupt( true );
|
||||
setUDREInterrupt( false );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool USART0::receiveByte( uint8_t &ui8Data )
|
||||
{
|
||||
if( m_vsizeRXBufferHead == m_vsizeRXBufferTail && !( SREG & ( 1 << SREG_I ) ) )
|
||||
{
|
||||
while( !( *m_vui8pUCSRA & ( 1 << RXC_D ) ) );
|
||||
ui8Data = *m_vui8pUDR;
|
||||
return true;
|
||||
}
|
||||
else if( m_vsizeRXBufferHead == m_vsizeRXBufferTail )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ui8Data = m_vui8aRXBuffer[m_vsizeRXBufferTail];
|
||||
m_vsizeRXBufferTail = ( m_vsizeRXBufferTail + 1 ) % sm_sizeRXBUFFER_SIZE;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS )
|
||||
{
|
||||
uint16_t ui16DelayCounter = 0;
|
||||
|
||||
while( !receiveByte( ui8Data ) )
|
||||
{
|
||||
_delay_ms( 1 );
|
||||
|
||||
if( ui16DelayCounter++ >= ui16TimeoutMS )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator /* = "\r\n" */ )
|
||||
{
|
||||
size_t sizeReceived = 0;
|
||||
|
||||
while( sizeReceived < sizeBufferLength - 1 )
|
||||
{
|
||||
uint8_t ui8ReceiveByte;
|
||||
|
||||
while( !receiveByte( ui8ReceiveByte ) );
|
||||
szBuffer[sizeReceived++] = ui8ReceiveByte;
|
||||
szBuffer[sizeReceived] = '\0';
|
||||
|
||||
if( strstr( szBuffer, szLineTerminator ) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::flushReceive()
|
||||
{
|
||||
uint8_t ui8Received;
|
||||
|
||||
while( m_vsizeRXBufferHead != m_vsizeRXBufferTail )
|
||||
{
|
||||
receiveByte( ui8Received );
|
||||
}
|
||||
|
||||
if( !( SREG & ( 1 << SREG_I ) ) && ( *m_vui8pUCSRA & ( 1 << RXC_D ) ) )
|
||||
{
|
||||
ui8Received = *m_vui8pUDR;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::transmitByte( uint8_t ui8Data )
|
||||
{
|
||||
if( m_vsizeTXBufferHead == m_vsizeTXBufferTail && *m_vui8pUCSRA & ( 1 << UDRE_D ) )
|
||||
{
|
||||
*m_vui8pUDR = ui8Data;
|
||||
return;
|
||||
}
|
||||
|
||||
size_t sizeIndex = ( m_vsizeTXBufferHead + 1 ) % sm_sizeTXBUFFER_SIZE;
|
||||
|
||||
while( sizeIndex == m_vsizeTXBufferTail )
|
||||
{
|
||||
if( !( SREG & ( 1 << SREG_I ) ) && *m_vui8pUCSRA & ( 1 << UDRE_D ) )
|
||||
{
|
||||
transmitInterruptHandler();
|
||||
}
|
||||
}
|
||||
|
||||
m_vui8aTXBuffer[m_vsizeTXBufferHead] = ui8Data;
|
||||
m_vsizeTXBufferHead = sizeIndex;
|
||||
|
||||
if( !( SREG & ( 1 << SREG_I ) ) )
|
||||
{
|
||||
while( !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) );
|
||||
transmitInterruptHandler();
|
||||
}
|
||||
else
|
||||
{
|
||||
setUDREInterrupt( true );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::transmitString( const char *szString )
|
||||
{
|
||||
while( *szString )
|
||||
{
|
||||
transmitByte( *szString++ );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::flushTransmit()
|
||||
{
|
||||
while( m_vsizeTXBufferHead != m_vsizeTXBufferTail && !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) );
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::receiveInterruptHandler()
|
||||
{
|
||||
uint8_t ui8ReceivedByte = *m_vui8pUDR;
|
||||
size_t sizeIndex = ( m_vsizeRXBufferHead + 1 ) % sm_sizeRXBUFFER_SIZE;
|
||||
|
||||
if( sizeIndex != m_vsizeRXBufferTail )
|
||||
{
|
||||
m_vui8aRXBuffer[m_vsizeRXBufferHead] = ui8ReceivedByte;
|
||||
m_vsizeRXBufferHead = sizeIndex;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void USART0::transmitInterruptHandler()
|
||||
{
|
||||
uint8_t ui8TransmitByte = m_vui8aTXBuffer[m_vsizeTXBufferTail];
|
||||
m_vsizeTXBufferTail = ( m_vsizeTXBufferTail + 1 ) % sm_sizeTXBUFFER_SIZE;
|
||||
|
||||
*m_vui8pUDR = ui8TransmitByte;
|
||||
|
||||
if( m_vsizeTXBufferHead == m_vsizeTXBufferTail )
|
||||
{
|
||||
setUDREInterrupt( false );
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
ISR( USART0_RX_vect_D )
|
||||
{
|
||||
USART0::inst().receiveInterruptHandler();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
ISR( USART0_UDRE_vect_D )
|
||||
{
|
||||
USART0::inst().transmitInterruptHandler();
|
||||
}
|
||||
|
||||
/************************************************************************/
|
||||
/************************************************************************/
|
||||
|
||||
#ifdef SECOND_USART
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART1 USART1::sm_cInstance;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART1& USART1::inst()
|
||||
{
|
||||
return sm_cInstance;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
USART1::USART1()
|
||||
{
|
||||
m_vui8pUCSRA = &UCSR1A;
|
||||
m_vui8pUCSRB = &UCSR1B;
|
||||
m_vui8pUCSRC = &UCSR1C;
|
||||
m_vui8pUBRRH = &UBRR1H;
|
||||
m_vui8pUBRRL = &UBRR1L;
|
||||
m_vui8pUDR = &UDR1;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
ISR( USART1_RX_vect_D )
|
||||
{
|
||||
USART1::inst().receiveInterruptHandler();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
ISR( USART1_UDRE_vect_D )
|
||||
{
|
||||
USART1::inst().transmitInterruptHandler();
|
||||
}
|
||||
|
||||
#endif
|
@ -1,215 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectVersion>7.0</ProjectVersion>
|
||||
<ToolchainName>com.Atmel.AVRGCC8.CPP</ToolchainName>
|
||||
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
|
||||
<avrdevice>ATmega328P</avrdevice>
|
||||
<avrdeviceseries>none</avrdeviceseries>
|
||||
<OutputType>Executable</OutputType>
|
||||
<Language>CPP</Language>
|
||||
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
|
||||
<OutputFileExtension>.elf</OutputFileExtension>
|
||||
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
|
||||
<AssemblyName>usart</AssemblyName>
|
||||
<Name>usart</Name>
|
||||
<RootNamespace>usart</RootNamespace>
|
||||
<ToolchainFlavour>Native</ToolchainFlavour>
|
||||
<KeepTimersRunning>true</KeepTimersRunning>
|
||||
<OverrideVtor>false</OverrideVtor>
|
||||
<CacheFlash>true</CacheFlash>
|
||||
<ProgFlashFromRam>true</ProgFlashFromRam>
|
||||
<RamSnippetAddress>0x20000000</RamSnippetAddress>
|
||||
<UncachedRange />
|
||||
<preserveEEPROM>true</preserveEEPROM>
|
||||
<OverrideVtorValue>exception_table</OverrideVtorValue>
|
||||
<BootSegment>2</BootSegment>
|
||||
<eraseonlaunchrule>0</eraseonlaunchrule>
|
||||
<AsfFrameworkConfig>
|
||||
<framework-data xmlns="">
|
||||
<options />
|
||||
<configurations />
|
||||
<files />
|
||||
<documentation help="" />
|
||||
<offline-documentation help="" />
|
||||
<dependencies>
|
||||
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.31.0" />
|
||||
</dependencies>
|
||||
</framework-data>
|
||||
</AsfFrameworkConfig>
|
||||
<avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
|
||||
<avrtoolserialnumber />
|
||||
<avrdeviceexpectedsignature>0x1E950F</avrdeviceexpectedsignature>
|
||||
<com_atmel_avrdbg_tool_stk500>
|
||||
<ToolOptions>
|
||||
<InterfaceProperties>
|
||||
<IspClock>1843200</IspClock>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>ISP</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
|
||||
<ToolNumber>
|
||||
</ToolNumber>
|
||||
<ToolName>STK500</ToolName>
|
||||
</com_atmel_avrdbg_tool_stk500>
|
||||
<avrtoolinterface>ISP</avrtoolinterface>
|
||||
<avrtoolinterfaceclock>1843200</avrtoolinterfaceclock>
|
||||
<com_atmel_avrdbg_tool_simulator>
|
||||
<ToolOptions xmlns="">
|
||||
<InterfaceProperties>
|
||||
</InterfaceProperties>
|
||||
<InterfaceName>
|
||||
</InterfaceName>
|
||||
</ToolOptions>
|
||||
<ToolType xmlns="">com.atmel.avrdbg.tool.simulator</ToolType>
|
||||
<ToolNumber xmlns="">
|
||||
</ToolNumber>
|
||||
<ToolName xmlns="">Simulator</ToolName>
|
||||
</com_atmel_avrdbg_tool_simulator>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.optimization.RelaxBranches>True</avrgcc.common.optimization.RelaxBranches>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
<Value>libprintf_flt</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>NDEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
|
||||
</AvrGccCpp>
|
||||
</ToolchainSettings>
|
||||
<PreBuildEvent>echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<ToolchainSettings>
|
||||
<AvrGccCpp>
|
||||
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\gcc\dev\atmega328p"</avrgcc.common.Device>
|
||||
<avrgcc.common.optimization.RelaxBranches>True</avrgcc.common.optimization.RelaxBranches>
|
||||
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
|
||||
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
|
||||
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
|
||||
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
|
||||
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
|
||||
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcc.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.directories.IncludePaths>
|
||||
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
|
||||
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
|
||||
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
|
||||
<avrgcccpp.compiler.directories.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.directories.IncludePaths>
|
||||
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
|
||||
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
|
||||
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
|
||||
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
|
||||
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
|
||||
<avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
|
||||
<avrgcccpp.linker.libraries.Libraries>
|
||||
<ListValues>
|
||||
<Value>libm</Value>
|
||||
<Value>libprintf_flt</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.linker.libraries.Libraries>
|
||||
<avrgcccpp.assembler.general.IncludePaths>
|
||||
<ListValues>
|
||||
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.assembler.general.IncludePaths>
|
||||
<avrgcc.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcc.compiler.symbols.DefSymbols>
|
||||
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
|
||||
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
|
||||
<avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<ListValues>
|
||||
<Value>DEBUG</Value>
|
||||
</ListValues>
|
||||
</avrgcccpp.compiler.symbols.DefSymbols>
|
||||
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
|
||||
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
|
||||
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
|
||||
</AvrGccCpp>
|
||||
</ToolchainSettings>
|
||||
<PreBuildEvent>echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="array\array.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="clock.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="main.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="usart.cpp">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
<Compile Include="usart.h">
|
||||
<SubType>compile</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="array" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
|
||||
</Project>
|
176
usart/usart.h
176
usart/usart.h
@ -1,176 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) by BlackMark 2015-2016
|
||||
* Date 24/05/2016
|
||||
* Version 2.9
|
||||
*/
|
||||
|
||||
#ifndef USART_H
|
||||
#define USART_H
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "clock.h"
|
||||
|
||||
#if defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
|
||||
#define USART_SPI
|
||||
#define USART0_RX_vect_D USART_RX_vect
|
||||
#define USART0_UDRE_vect_D USART_UDRE_vect
|
||||
#endif
|
||||
#if defined (__AVR_ATmega32A__) || (__AVR_ATmega8__)
|
||||
#define USART_SHAREDIO
|
||||
#define USART0_RX_vect_D USART_RXC_vect
|
||||
#define USART0_UDRE_vect_D USART_UDRE_vect
|
||||
#endif
|
||||
#if defined (__AVR_ATmega1284P__)
|
||||
#define SECOND_USART
|
||||
#define USART0_RX_vect_D USART0_RX_vect
|
||||
#define USART1_RX_vect_D USART1_RX_vect
|
||||
#define USART0_UDRE_vect_D USART0_UDRE_vect
|
||||
#define USART1_UDRE_vect_D USART1_UDRE_vect
|
||||
#endif
|
||||
|
||||
#ifdef USART_SHAREDIO
|
||||
#define RXEN_D RXEN
|
||||
#define TXEN_D TXEN
|
||||
#define RXCIE_D RXCIE
|
||||
#define UDRIE_D UDRIE
|
||||
#define UCSZ0_D UCSZ0
|
||||
#define UCSZ1_D UCSZ1
|
||||
#define UCSZ2_D UCSZ2
|
||||
#define UPM0_D UPM0
|
||||
#define UPM1_D UPM1
|
||||
#define USBS_D USBS
|
||||
#define RXC_D RXC
|
||||
#define UDRE_D UDRE
|
||||
#else
|
||||
#define RXEN_D RXEN0
|
||||
#define TXEN_D TXEN0
|
||||
#define RXCIE_D RXCIE0
|
||||
#define UDRIE_D UDRIE0
|
||||
#define UCSZ0_D UCSZ00
|
||||
#define UCSZ1_D UCSZ01
|
||||
#define UCSZ2_D UCSZ02
|
||||
#define UPM0_D UPM00
|
||||
#define UPM1_D UPM01
|
||||
#define USBS_D USBS0
|
||||
#define RXC_D RXC0
|
||||
#define UDRE_D UDRE0
|
||||
#endif
|
||||
|
||||
#ifdef USART_SPI
|
||||
#define UMSEL0_D UMSEL00
|
||||
#define UMSEL1_D UMSEL01
|
||||
#else
|
||||
#define UMSEL_D UMSEL
|
||||
#endif
|
||||
|
||||
class USART0
|
||||
{
|
||||
public:
|
||||
enum class Mode
|
||||
{
|
||||
ASYNCHRONOUS = 0,
|
||||
SYNCHRONOUS = 1,
|
||||
#ifdef USART_SPI
|
||||
MASTERSPI = 2
|
||||
#endif
|
||||
};
|
||||
|
||||
enum class Parity
|
||||
{
|
||||
DISABLED = 0,
|
||||
ODD = 1,
|
||||
EVEN = 2
|
||||
};
|
||||
|
||||
enum class StopBit
|
||||
{
|
||||
ONE = 1,
|
||||
TWO = 2
|
||||
};
|
||||
|
||||
static constexpr size_t sm_sizeRXBUFFER_SIZE = 16;
|
||||
static constexpr size_t sm_sizeTXBUFFER_SIZE = 16;
|
||||
|
||||
protected:
|
||||
volatile uint8_t *m_vui8pUCSRA;
|
||||
volatile uint8_t *m_vui8pUCSRB;
|
||||
volatile uint8_t *m_vui8pUCSRC;
|
||||
volatile uint8_t *m_vui8pUBRRH;
|
||||
volatile uint8_t *m_vui8pUBRRL;
|
||||
volatile uint8_t *m_vui8pUDR;
|
||||
|
||||
volatile size_t m_vsizeRXBufferHead;
|
||||
volatile size_t m_vsizeRXBufferTail;
|
||||
volatile size_t m_vsizeTXBufferHead;
|
||||
volatile size_t m_vsizeTXBufferTail;
|
||||
|
||||
volatile uint8_t m_vui8aRXBuffer[sm_sizeRXBUFFER_SIZE];
|
||||
volatile uint8_t m_vui8aTXBuffer[sm_sizeTXBUFFER_SIZE];
|
||||
|
||||
USART0();
|
||||
|
||||
private:
|
||||
static USART0 sm_cInstance;
|
||||
|
||||
uint8_t readUCSRC();
|
||||
void setUCSRC( uint8_t ui8UCSRC );
|
||||
|
||||
void setRXState( bool bEnable );
|
||||
void setTXState( bool bEnable );
|
||||
void setRXInterrupt( bool bEnable );
|
||||
void setUDREInterrupt( bool bEnable );
|
||||
void setBaudRate( uint32_t ui32BaudRate );
|
||||
void setDataBits( uint8_t ui8DataBits );
|
||||
void setParity( Parity enmParity );
|
||||
void setStopBits( StopBit enmStopBits );
|
||||
void setMode( Mode enmMode );
|
||||
|
||||
~USART0();
|
||||
|
||||
public:
|
||||
static USART0& inst();
|
||||
USART0( const USART0& ) = delete;
|
||||
void operator=( const USART0& ) = delete;
|
||||
|
||||
void init( uint32_t ui32BaudRate = 9600, uint8_t ui8DataBits = 8, Parity enmParity = Parity::DISABLED, StopBit enmStopBits = StopBit::ONE, Mode enmMode = Mode::ASYNCHRONOUS );
|
||||
|
||||
bool receiveByte( uint8_t &ui8Data );
|
||||
bool receiveByte( uint8_t &ui8Data, uint16_t ui16TimeoutMS );
|
||||
bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n" );
|
||||
void flushReceive();
|
||||
|
||||
void transmitByte( uint8_t ui8Data );
|
||||
void transmitString( const char *szString );
|
||||
void flushTransmit();
|
||||
|
||||
void receiveInterruptHandler();
|
||||
void transmitInterruptHandler();
|
||||
|
||||
inline USART0& operator<<( const char *szString )
|
||||
{
|
||||
transmitString( szString );
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef SECOND_USART
|
||||
|
||||
class USART1 : public USART0
|
||||
{
|
||||
public:
|
||||
static USART1& inst();
|
||||
USART1( const USART1& ) = delete;
|
||||
void operator=( const USART1& ) = delete;
|
||||
|
||||
private:
|
||||
static USART1 sm_cInstance;
|
||||
|
||||
USART1();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user