Compare commits

..

No commits in common. "fa0a65a94c84ad3e69fa205a921bf3f57e7eb4f4" and "cb436b11a8428c2c40a11b51fedb4d2c7d38ece7" have entirely different histories.

2 changed files with 36 additions and 36 deletions

View File

@ -46,24 +46,24 @@ class Hardware {
public: public:
static void init() FORCE_INLINE 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_H_ADDR>() = static_cast<uint8_t>(baudVal >> 8);
*getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(BaudVal); *getRegPtr<Registers::BAUD_REG_L_ADDR>() = static_cast<uint8_t>(baudVal);
constexpr auto DataBitsValues = calcDataBits(); constexpr auto dataBitsVal = calcDataBits();
constexpr auto ParityVal = calcParity(); constexpr auto parityVal = calcParity();
constexpr auto StopBitsVal = calcStopBits(); constexpr auto stopBitsVal = calcStopBits();
constexpr auto ModeVal = calcMode(); constexpr auto modeVal = calcMode();
constexpr auto EnableRx = calcRxState<true>(); constexpr auto enableRx = calcRxState<true>();
constexpr auto EnableTx = calcTxState<true>(); constexpr auto enableTx = calcTxState<true>();
constexpr auto InterruptVal = calcInterrupt(); constexpr auto interruptVal = calcInterrupt();
constexpr uint8_t ControlRegB = DataBitsValues.regBVal | EnableRx | EnableTx | InterruptVal; constexpr uint8_t controlRegB = dataBitsVal.regBVal | enableRx | enableTx | interruptVal;
constexpr uint8_t ControlRegC = DataBitsValues.regCVal | ParityVal | StopBitsVal | ModeVal; constexpr uint8_t controlRegC = dataBitsVal.regCVal | parityVal | stopBitsVal | modeVal;
*getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = ControlRegB; *getRegPtr<Registers::CTRL_STAT_REG_B_ADDR>() = controlRegB;
*getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = ControlRegC; *getRegPtr<Registers::CTRL_STAT_REG_C_ADDR>() = controlRegC;
} }
static bool rxByteBlocking(typename cfg::data_t &byte) FORCE_INLINE static bool rxByteBlocking(typename cfg::data_t &byte) FORCE_INLINE
@ -137,8 +137,8 @@ class Hardware {
static constexpr auto calcBaud() static constexpr auto calcBaud()
{ {
// The actual formula is (F_CPU / (16 * baudRate)) - 1, but this one has the advantage of rounding correctly // 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; constexpr auto baudVal = (F_CPU + 8 * cfg::BAUD_RATE) / (16 * cfg::BAUD_RATE) - 1;
return BaudVal; return baudVal;
} }
static constexpr auto calcDataBits() static constexpr auto calcDataBits()
@ -202,23 +202,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; 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; uint8_t enableVal = 0;
if (Enable) if (enable)
enableVal = (1 << CtrlFlagsB::TX_ENABLE); enableVal = (1 << CtrlFlagsB::TX_ENABLE);
return enableVal; return enableVal;

View File

@ -19,10 +19,10 @@ namespace uart {
namespace detail { namespace detail {
template <typename T, T Limit, size_t Base> template <typename T, T limit, size_t Base>
static constexpr size_t cntDigits() static constexpr size_t cntDigits()
{ {
T num = Limit; T num = limit;
size_t cnt = 0; size_t cnt = 0;
do { do {
@ -36,13 +36,13 @@ static constexpr size_t cntDigits()
template <typename T, size_t Base> template <typename T, size_t Base>
static constexpr size_t maxNumDigits() static constexpr size_t maxNumDigits()
{ {
constexpr T MinVal = util::NumericLimits<T>::min(); constexpr T minVal = util::NumericLimits<T>::min();
constexpr T MaxVal = util::NumericLimits<T>::max(); constexpr T maxVal = util::NumericLimits<T>::max();
constexpr T MinDigits = cntDigits<T, MinVal, Base>(); T minDigits = cntDigits<T, minVal, Base>();
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>(); T maxDigits = cntDigits<T, maxVal, Base>();
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits; return (minDigits < maxDigits) ? maxDigits : minDigits;
} }
} // namespace detail } // namespace detail
@ -120,8 +120,11 @@ class Uart {
static_assert(Base <= 16, "Numbers with base higher than 16 are not supported"); static_assert(Base <= 16, "Numbers with base higher than 16 are not supported");
static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number"); static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number");
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A'; constexpr char alphaChar = (LowerCase) ? 'a' : 'A';
constexpr size_t NumDigits = detail::maxNumDigits<T, Base>(); constexpr size_t numDigits = detail::maxNumDigits<T, Base>();
data_t buffer[numDigits];
data_t *bufEnd = buffer + numDigits - 1;
T digits = val; T digits = val;
@ -130,17 +133,14 @@ class Uart {
txByte('-'); txByte('-');
} }
data_t buffer[NumDigits];
data_t *bufEnd = buffer + NumDigits - 1;
do { do {
const data_t lastDigit = digits % Base; 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); size_t strLen = buffer + numDigits - (bufEnd + 1);
if (Padding > strLen) { if (Padding > strLen) {
for (size_t i = Padding; i > strLen && bufEnd >= buffer; --i) { 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); txByte(*buf);
} }