Compare commits

...

2 Commits

3 changed files with 30 additions and 11 deletions

View File

@@ -9,6 +9,8 @@ namespace spi {
template <class Cfg> template <class Cfg>
class Hardware { class Hardware {
public: public:
using word_t = uint8_t;
static void init() static void init()
{ {
if constexpr (Cfg::SIDE == Side::MASTER) { if constexpr (Cfg::SIDE == Side::MASTER) {
@@ -35,7 +37,7 @@ class Hardware {
SPCR |= (1 << SPE); 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)))

View File

@@ -4,6 +4,8 @@
#include <stdint.h> #include <stdint.h>
#include <avr/interrupt.h>
#include "../io/io.hpp" #include "../io/io.hpp"
#include "../util/type.hpp" #include "../util/type.hpp"
#include "../util/util.hpp" #include "../util/util.hpp"
@@ -22,6 +24,7 @@ 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 = util::conditional_t<Cfg::BITS <= 8, uint8_t,
@@ -30,7 +33,6 @@ class Software {
uint64_t>>>; 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 +47,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(
@@ -74,6 +78,8 @@ class Software {
}, },
util::make_index_sequence<Cfg::BITS>{}); util::make_index_sequence<Cfg::BITS>{});
enableInterrupts(oldInterruptState);
return dataIn; return dataIn;
} }
@@ -89,6 +95,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 uint8_t disableInterrupts()
{
const auto oldInterruptState = SREG;
cli();
return oldInterruptState;
}
static inline void enableInterrupts(const 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