Compare commits

...

4 Commits

4 changed files with 61 additions and 36 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,6 +11,8 @@ namespace spi {
template <class Cfg> template <class Cfg>
class Hardware { class Hardware {
public: public:
using word_t = std::uint8_t;
static void init() static void init()
{ {
if constexpr (Cfg::SIDE == Side::MASTER) { if constexpr (Cfg::SIDE == Side::MASTER) {
@@ -32,10 +36,10 @@ class Hardware {
setMaster(); setMaster();
setBitOrder(); setBitOrder();
SPCR |= (1 << SPE); SPCR = SPCR | (1 << SPE);
} }
static uint8_t transfer(uint8_t data) static word_t transfer(word_t data)
{ {
SPDR = data; SPDR = data;
while (!(SPSR & (1 << SPIF))) while (!(SPSR & (1 << SPIF)))
@@ -56,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);
} }
} }
@@ -95,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,10 +2,14 @@
#include "../clock.hpp" #include "../clock.hpp"
#include <stdint.h> #include <type_traits>
#include <utility>
#include <cstdint>
#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 {
@@ -22,15 +26,15 @@ class Software {
return (delayUs > 0 ? delayUs : 0); return (delayUs > 0 ? delayUs : 0);
} }
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
public:
static void init() static void init()
{ {
sm_sck = ((Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_1) ? false : true); sm_sck = ((Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_1) ? false : true);
@@ -45,6 +49,8 @@ class Software {
static word_t transfer(const word_t data) static word_t transfer(const word_t data)
{ {
const auto oldInterruptState = disableInterrupts();
auto dataIn = word_t{}; auto dataIn = word_t{};
util::for_constexpr( util::for_constexpr(
@@ -72,7 +78,9 @@ class Software {
_delay_us(DELAY_US); _delay_us(DELAY_US);
} }
}, },
util::make_index_sequence<Cfg::BITS>{}); std::make_index_sequence<Cfg::BITS>{});
enableInterrupts(oldInterruptState);
return dataIn; return dataIn;
} }
@@ -89,6 +97,18 @@ class Software {
static io::Pin<Cfg::SS_PIN> sm_ss; static io::Pin<Cfg::SS_PIN> sm_ss;
static constexpr auto DELAY_US = calcClockDelay(); static constexpr auto DELAY_US = calcClockDelay();
static inline std::uint8_t disableInterrupts()
{
const auto oldInterruptState = SREG;
cli();
return oldInterruptState;
}
static inline void enableInterrupts(const std::uint8_t oldInterruptState)
{
SREG = oldInterruptState;
}
}; };
} // namespace spi } // namespace spi

17
spi.hpp
View File

@@ -9,24 +9,23 @@
namespace spi { namespace spi {
template <class Driver> template <class Driver>
class Spi { struct Spi {
public: using word_t = typename Driver::word_t;
static void init()
static inline void init()
{ {
Driver::init(); Driver::init();
} }
static uint8_t transfer(uint8_t ui8Data) static inline word_t transfer(const word_t data)
{ {
return Driver::transfer(ui8Data); return Driver::transfer(data);
} }
static void select(bool bSelect) static inline void select(const bool selectState)
{ {
Driver::select(bSelect); Driver::select(selectState);
} }
private:
}; };
} // namespace spi } // namespace spi