Compare commits
5 Commits
cb436b11a8
...
fa0a65a94c
Author | SHA1 | Date | |
---|---|---|---|
fa0a65a94c | |||
0e128bcb7d | |||
0a52110d47 | |||
f751833a88 | |||
1bd49f65fa |
40
hardware.hpp
40
hardware.hpp
@ -46,24 +46,24 @@ class Hardware {
|
||||
public:
|
||||
static void init() FORCE_INLINE
|
||||
{
|
||||
constexpr auto baudVal = calcBaud();
|
||||
constexpr auto BaudVal = calcBaud();
|
||||
|
||||
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<uint8_t>(baudVal >> 8);
|
||||
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(baudVal);
|
||||
*getRegPtr<Registers::BAUD_REG_H_ADDR>() = static_cast<uint8_t>(BaudVal >> 8);
|
||||
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(BaudVal);
|
||||
|
||||
constexpr auto dataBitsVal = calcDataBits();
|
||||
constexpr auto parityVal = calcParity();
|
||||
constexpr auto stopBitsVal = calcStopBits();
|
||||
constexpr auto modeVal = calcMode();
|
||||
constexpr auto enableRx = calcRxState<true>();
|
||||
constexpr auto enableTx = calcTxState<true>();
|
||||
constexpr auto interruptVal = calcInterrupt();
|
||||
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 uint8_t controlRegB = dataBitsVal.regBVal | enableRx | enableTx | interruptVal;
|
||||
constexpr uint8_t controlRegC = dataBitsVal.regCVal | parityVal | stopBitsVal | modeVal;
|
||||
constexpr uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal;
|
||||
constexpr uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal;
|
||||
|
||||
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = controlRegB;
|
||||
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = controlRegC;
|
||||
*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
|
||||
@ -137,8 +137,8 @@ class Hardware {
|
||||
static constexpr auto calcBaud()
|
||||
{
|
||||
// The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly
|
||||
constexpr auto baudVal = (F_CPU + 8 * cfg::BAUD_RATE) / (16 * cfg::BAUD_RATE) - 1;
|
||||
return baudVal;
|
||||
constexpr auto BaudVal = (F_CPU + 8 * cfg::BAUD_RATE) / (16 * cfg::BAUD_RATE) - 1;
|
||||
return BaudVal;
|
||||
}
|
||||
|
||||
static constexpr auto calcDataBits()
|
||||
@ -202,23 +202,23 @@ class Hardware {
|
||||
return modeVal;
|
||||
}
|
||||
|
||||
template <bool enable>
|
||||
template <bool Enable>
|
||||
static constexpr auto calcRxState()
|
||||
{
|
||||
uint8_t enableVal = 0;
|
||||
|
||||
if (enable)
|
||||
if (Enable)
|
||||
enableVal = (1 << CtrlFlagsB::RX_ENABLE);
|
||||
|
||||
return enableVal;
|
||||
}
|
||||
|
||||
template <bool enable>
|
||||
template <bool Enable>
|
||||
static constexpr auto calcTxState()
|
||||
{
|
||||
uint8_t enableVal = 0;
|
||||
|
||||
if (enable)
|
||||
if (Enable)
|
||||
enableVal = (1 << CtrlFlagsB::TX_ENABLE);
|
||||
|
||||
return enableVal;
|
||||
|
32
uart.hpp
32
uart.hpp
@ -19,10 +19,10 @@ namespace uart {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename T, T limit, size_t Base>
|
||||
template <typename T, T Limit, size_t Base>
|
||||
static constexpr size_t cntDigits()
|
||||
{
|
||||
T num = limit;
|
||||
T num = Limit;
|
||||
size_t cnt = 0;
|
||||
|
||||
do {
|
||||
@ -36,13 +36,13 @@ static constexpr size_t cntDigits()
|
||||
template <typename T, size_t Base>
|
||||
static constexpr size_t maxNumDigits()
|
||||
{
|
||||
constexpr T minVal = util::NumericLimits<T>::min();
|
||||
constexpr T maxVal = util::NumericLimits<T>::max();
|
||||
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>();
|
||||
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
||||
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
||||
|
||||
return (minDigits < maxDigits) ? maxDigits : minDigits;
|
||||
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
@ -120,11 +120,8 @@ class Uart {
|
||||
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 size_t numDigits = detail::maxNumDigits<T, Base>();
|
||||
|
||||
data_t buffer[numDigits];
|
||||
data_t *bufEnd = buffer + numDigits - 1;
|
||||
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
||||
constexpr size_t NumDigits = detail::maxNumDigits<T, Base>();
|
||||
|
||||
T digits = val;
|
||||
|
||||
@ -133,14 +130,17 @@ class Uart {
|
||||
txByte('-');
|
||||
}
|
||||
|
||||
data_t buffer[NumDigits];
|
||||
data_t *bufEnd = buffer + NumDigits - 1;
|
||||
|
||||
do {
|
||||
data_t lastDigit = digits % Base;
|
||||
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (alphaChar + lastDigit - 10);
|
||||
const data_t lastDigit = digits % Base;
|
||||
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (AlphaChar + lastDigit - 10);
|
||||
digits /= Base;
|
||||
} while (digits > 0);
|
||||
|
||||
if (Padding > 0) {
|
||||
size_t strLen = buffer + numDigits - (bufEnd + 1);
|
||||
size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
||||
|
||||
if (Padding > strLen) {
|
||||
for (size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
||||
@ -149,7 +149,7 @@ class Uart {
|
||||
}
|
||||
}
|
||||
|
||||
for (data_t *buf = bufEnd + 1; buf < buffer + numDigits; ++buf)
|
||||
for (data_t *buf = bufEnd + 1; buf < buffer + NumDigits; ++buf)
|
||||
txByte(*buf);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user