Compare commits
2 Commits
999cd0e0c9
...
852ad5a318
| Author | SHA1 | Date | |
|---|---|---|---|
| 852ad5a318 | |||
| f359f46ffc |
@@ -9,6 +9,8 @@ namespace spi {
|
||||
template <class Cfg>
|
||||
class Hardware {
|
||||
public:
|
||||
using word_t = uint8_t;
|
||||
|
||||
static void init()
|
||||
{
|
||||
if constexpr (Cfg::SIDE == Side::MASTER) {
|
||||
@@ -35,7 +37,7 @@ class Hardware {
|
||||
SPCR |= (1 << SPE);
|
||||
}
|
||||
|
||||
static uint8_t transfer(uint8_t data)
|
||||
static word_t transfer(word_t data)
|
||||
{
|
||||
SPDR = data;
|
||||
while (!(SPSR & (1 << SPIF)))
|
||||
|
||||
20
software.hpp
20
software.hpp
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "../io/io.hpp"
|
||||
#include "../util/type.hpp"
|
||||
#include "../util/util.hpp"
|
||||
@@ -22,6 +24,7 @@ class Software {
|
||||
return (delayUs > 0 ? delayUs : 0);
|
||||
}
|
||||
|
||||
public:
|
||||
static_assert(Cfg::BITS >= 1 && Cfg::BITS <= 64, "Word size must be in range [1-64]");
|
||||
// clang-format off
|
||||
using word_t = util::conditional_t<Cfg::BITS <= 8, uint8_t,
|
||||
@@ -30,7 +33,6 @@ class Software {
|
||||
uint64_t>>>;
|
||||
// clang-format on
|
||||
|
||||
public:
|
||||
static void init()
|
||||
{
|
||||
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)
|
||||
{
|
||||
const auto oldInterruptState = disableInterrupts();
|
||||
|
||||
auto dataIn = word_t{};
|
||||
|
||||
util::for_constexpr(
|
||||
@@ -74,6 +78,8 @@ class Software {
|
||||
},
|
||||
util::make_index_sequence<Cfg::BITS>{});
|
||||
|
||||
enableInterrupts(oldInterruptState);
|
||||
|
||||
return dataIn;
|
||||
}
|
||||
|
||||
@@ -89,6 +95,18 @@ class Software {
|
||||
static io::Pin<Cfg::SS_PIN> sm_ss;
|
||||
|
||||
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
|
||||
|
||||
17
spi.hpp
17
spi.hpp
@@ -9,24 +9,23 @@
|
||||
namespace spi {
|
||||
|
||||
template <class Driver>
|
||||
class Spi {
|
||||
public:
|
||||
static void init()
|
||||
struct Spi {
|
||||
using word_t = typename Driver::word_t;
|
||||
|
||||
static inline void 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
|
||||
|
||||
Reference in New Issue
Block a user