Compare commits

...

2 Commits

Author SHA1 Message Date
8bf3d1a874 Make use of C++ standard library 2022-05-29 16:14:42 +02:00
85346f258f Fix volatile compound assignments being deprecated in C++20 2022-05-29 14:45:57 +02:00
3 changed files with 34 additions and 28 deletions

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <cstdint>
#include "../io/io.hpp" #include "../io/io.hpp"
namespace spi { namespace spi {
@@ -44,8 +46,8 @@ struct HardwareConfig {
static constexpr auto PULLUP = pullup; static constexpr auto PULLUP = pullup;
}; };
template <io::P sckPin, io::P misoPin, io::P mosiPin, io::P ssPin, uint32_t freq = 100'000, Mode mode = Mode::MODE_0, template <io::P sckPin, io::P misoPin, io::P mosiPin, io::P ssPin, std::uint32_t freq = 100'000,
BitOrder bitOrder = BitOrder::MSB_FIRST, uint8_t bits = 8, bool pullup = false> Mode mode = Mode::MODE_0, BitOrder bitOrder = BitOrder::MSB_FIRST, std::uint8_t bits = 8, bool pullup = false>
struct SoftwareConfig { struct SoftwareConfig {
static constexpr auto SCK_PIN = sckPin; static constexpr auto SCK_PIN = sckPin;
static constexpr auto MISO_PIN = misoPin; static constexpr auto MISO_PIN = misoPin;

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include <cstdint>
#include "../io/io.hpp" #include "../io/io.hpp"
namespace spi { namespace spi {
@@ -9,7 +11,7 @@ namespace spi {
template <class Cfg> template <class Cfg>
class Hardware { class Hardware {
public: public:
using word_t = uint8_t; using word_t = std::uint8_t;
static void init() static void init()
{ {
@@ -34,7 +36,7 @@ class Hardware {
setMaster(); setMaster();
setBitOrder(); setBitOrder();
SPCR |= (1 << SPE); SPCR = SPCR | (1 << SPE);
} }
static word_t transfer(word_t data) static word_t transfer(word_t data)
@@ -58,24 +60,24 @@ class Hardware {
static void setClockDiv() static void setClockDiv()
{ {
uint8_t ui8ClockDiv = static_cast<uint8_t>(Cfg::FREQ); std::uint8_t ui8ClockDiv = static_cast<std::uint8_t>(Cfg::FREQ);
if (ui8ClockDiv & 1) { if (ui8ClockDiv & 1) {
SPCR |= (1 << SPR0); SPCR = SPCR | (1 << SPR0);
} else { } else {
SPCR &= ~(1 << SPR0); SPCR = SPCR & ~(1 << SPR0);
} }
if (ui8ClockDiv & (1 << 1)) { if (ui8ClockDiv & (1 << 1)) {
SPCR |= (1 << SPR1); SPCR = SPCR | (1 << SPR1);
} else { } else {
SPCR &= ~(1 << SPR1); SPCR = SPCR & ~(1 << SPR1);
} }
if (ui8ClockDiv & (1 << 2)) { if (ui8ClockDiv & (1 << 2)) {
SPSR |= (1 << SPI2X); SPSR = SPSR | (1 << SPI2X);
} else { } else {
SPSR &= ~(1 << SPI2X); SPSR = SPSR & ~(1 << SPI2X);
} }
} }
@@ -97,35 +99,35 @@ class Hardware {
static void setMaster() static void setMaster()
{ {
if constexpr (Cfg::SIDE == Side::MASTER) { if constexpr (Cfg::SIDE == Side::MASTER) {
SPCR |= (1 << MSTR); SPCR = SPCR | (1 << MSTR);
} else { } else {
SPCR &= ~(1 << MSTR); SPCR = SPCR & ~(1 << MSTR);
} }
} }
static void setBitOrder() static void setBitOrder()
{ {
if constexpr (Cfg::BIT_ORDER == BitOrder::LSB_FIRST) { if constexpr (Cfg::BIT_ORDER == BitOrder::LSB_FIRST) {
SPCR |= (1 << DORD); SPCR = SPCR | (1 << DORD);
} else { } else {
SPCR &= ~(1 << DORD); SPCR = SPCR & ~(1 << DORD);
} }
} }
static void setCPOL(bool bCPOL) static void setCPOL(bool bCPOL)
{ {
if (bCPOL) { if (bCPOL) {
SPCR |= (1 << CPOL); SPCR = SPCR | (1 << CPOL);
} else { } else {
SPCR &= ~(1 << CPOL); SPCR = SPCR & ~(1 << CPOL);
} }
} }
static void setCPHA(bool bCPHA) static void setCPHA(bool bCPHA)
{ {
if (bCPHA) { if (bCPHA) {
SPCR |= (1 << CPHA); SPCR = SPCR | (1 << CPHA);
} else { } else {
SPCR &= ~(1 << CPHA); SPCR = SPCR & ~(1 << CPHA);
} }
} }
}; };

View File

@@ -2,12 +2,14 @@
#include "../clock.hpp" #include "../clock.hpp"
#include <stdint.h> #include <type_traits>
#include <utility>
#include <cstdint>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include "../io/io.hpp" #include "../io/io.hpp"
#include "../util/type.hpp"
#include "../util/util.hpp" #include "../util/util.hpp"
namespace spi { namespace spi {
@@ -27,10 +29,10 @@ class Software {
public: public:
static_assert(Cfg::BITS >= 1 && Cfg::BITS <= 64, "Word size must be in range [1-64]"); static_assert(Cfg::BITS >= 1 && Cfg::BITS <= 64, "Word size must be in range [1-64]");
// clang-format off // clang-format off
using word_t = util::conditional_t<Cfg::BITS <= 8, uint8_t, using word_t = std::conditional_t<Cfg::BITS <= 8, std::uint8_t,
util::conditional_t<Cfg::BITS <= 16, uint16_t, std::conditional_t<Cfg::BITS <= 16, std::uint16_t,
util::conditional_t<Cfg::BITS <= 32, uint32_t, std::conditional_t<Cfg::BITS <= 32, std::uint32_t,
uint64_t>>>; std::uint64_t>>>;
// clang-format on // clang-format on
static void init() static void init()
@@ -76,7 +78,7 @@ class Software {
_delay_us(DELAY_US); _delay_us(DELAY_US);
} }
}, },
util::make_index_sequence<Cfg::BITS>{}); std::make_index_sequence<Cfg::BITS>{});
enableInterrupts(oldInterruptState); enableInterrupts(oldInterruptState);
@@ -96,14 +98,14 @@ class Software {
static constexpr auto DELAY_US = calcClockDelay(); static constexpr auto DELAY_US = calcClockDelay();
static inline uint8_t disableInterrupts() static inline std::uint8_t disableInterrupts()
{ {
const auto oldInterruptState = SREG; const auto oldInterruptState = SREG;
cli(); cli();
return oldInterruptState; return oldInterruptState;
} }
static inline void enableInterrupts(const uint8_t oldInterruptState) static inline void enableInterrupts(const std::uint8_t oldInterruptState)
{ {
SREG = oldInterruptState; SREG = oldInterruptState;
} }