Compare commits
6 Commits
70aabc07f6
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8bf3d1a874 | |||
| 85346f258f | |||
| 852ad5a318 | |||
| f359f46ffc | |||
| 999cd0e0c9 | |||
| bb78d2291d |
23
config.hpp
23
config.hpp
@@ -1,5 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "../io/io.hpp"
|
||||||
|
|
||||||
namespace spi {
|
namespace spi {
|
||||||
|
|
||||||
enum class ClockDiv {
|
enum class ClockDiv {
|
||||||
@@ -32,12 +36,27 @@ enum class BitOrder {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <ClockDiv freq = ClockDiv::DIV_128, Mode mode = Mode::MODE_0, Side side = Side::MASTER,
|
template <ClockDiv freq = ClockDiv::DIV_128, Mode mode = Mode::MODE_0, Side side = Side::MASTER,
|
||||||
BitOrder bitOrder = BitOrder::MSB_FIRST, bool pullup = false>
|
BitOrder bitOrder = BitOrder::MSB_FIRST, io::P ssPin = io::P::B2, bool pullup = false>
|
||||||
struct Config {
|
struct HardwareConfig {
|
||||||
static constexpr auto FREQ = freq;
|
static constexpr auto FREQ = freq;
|
||||||
static constexpr auto MODE = mode;
|
static constexpr auto MODE = mode;
|
||||||
static constexpr auto SIDE = side;
|
static constexpr auto SIDE = side;
|
||||||
static constexpr auto BIT_ORDER = bitOrder;
|
static constexpr auto BIT_ORDER = bitOrder;
|
||||||
|
static constexpr auto SS_PIN = ssPin;
|
||||||
|
static constexpr auto PULLUP = pullup;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <io::P sckPin, io::P misoPin, io::P mosiPin, io::P ssPin, std::uint32_t freq = 100'000,
|
||||||
|
Mode mode = Mode::MODE_0, BitOrder bitOrder = BitOrder::MSB_FIRST, std::uint8_t bits = 8, bool pullup = false>
|
||||||
|
struct SoftwareConfig {
|
||||||
|
static constexpr auto SCK_PIN = sckPin;
|
||||||
|
static constexpr auto MISO_PIN = misoPin;
|
||||||
|
static constexpr auto MOSI_PIN = mosiPin;
|
||||||
|
static constexpr auto SS_PIN = ssPin;
|
||||||
|
static constexpr auto FREQ = freq;
|
||||||
|
static constexpr auto MODE = mode;
|
||||||
|
static constexpr auto BIT_ORDER = bitOrder;
|
||||||
|
static constexpr auto BITS = bits;
|
||||||
static constexpr auto PULLUP = pullup;
|
static constexpr auto PULLUP = pullup;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
40
hardware.hpp
40
hardware.hpp
@@ -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)))
|
||||||
@@ -52,28 +56,28 @@ class Hardware {
|
|||||||
static io::Pin<io::P::B5> sm_sck;
|
static io::Pin<io::P::B5> sm_sck;
|
||||||
static io::Pin<io::P::B4> sm_miso;
|
static io::Pin<io::P::B4> sm_miso;
|
||||||
static io::Pin<io::P::B3> sm_mosi;
|
static io::Pin<io::P::B3> sm_mosi;
|
||||||
static io::Pin<io::P::B2> sm_ss;
|
static io::Pin<Cfg::SS_PIN> sm_ss;
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
114
software.hpp
Normal file
114
software.hpp
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../clock.hpp"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
|
#include "../io/io.hpp"
|
||||||
|
#include "../util/util.hpp"
|
||||||
|
|
||||||
|
namespace spi {
|
||||||
|
|
||||||
|
template <typename Cfg>
|
||||||
|
class Software {
|
||||||
|
static constexpr double calcClockDelay()
|
||||||
|
{
|
||||||
|
constexpr auto staticClockCycles = 10;
|
||||||
|
constexpr auto maxFrequency = F_CPU / staticClockCycles;
|
||||||
|
static_assert(Cfg::FREQ <= maxFrequency, "SPI frequency not achievable using selected clock speed");
|
||||||
|
constexpr auto staticDelay = (1.0 * 1000 * 1000 / maxFrequency);
|
||||||
|
const auto delayUs = ((1.0 * 1000 * 1000 / Cfg::FREQ) - staticDelay) / 2;
|
||||||
|
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 = std::conditional_t<Cfg::BITS <= 8, std::uint8_t,
|
||||||
|
std::conditional_t<Cfg::BITS <= 16, std::uint16_t,
|
||||||
|
std::conditional_t<Cfg::BITS <= 32, std::uint32_t,
|
||||||
|
std::uint64_t>>>;
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
static void init()
|
||||||
|
{
|
||||||
|
sm_sck = ((Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_1) ? false : true);
|
||||||
|
sm_ss = true;
|
||||||
|
|
||||||
|
sm_sck.dir(io::Dir::OUT);
|
||||||
|
sm_miso.dir(io::Dir::IN);
|
||||||
|
sm_miso.pullup(Cfg::PULLUP);
|
||||||
|
sm_mosi.dir(io::Dir::OUT);
|
||||||
|
sm_ss.dir(io::Dir::OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static word_t transfer(const word_t data)
|
||||||
|
{
|
||||||
|
const auto oldInterruptState = disableInterrupts();
|
||||||
|
|
||||||
|
auto dataIn = word_t{};
|
||||||
|
|
||||||
|
util::for_constexpr(
|
||||||
|
[&](const auto idx) {
|
||||||
|
constexpr auto i = idx.value;
|
||||||
|
constexpr auto bitPos = (Cfg::BIT_ORDER == BitOrder::MSB_FIRST ? Cfg::BITS - i - 1 : i);
|
||||||
|
|
||||||
|
if constexpr (Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_2) {
|
||||||
|
sm_mosi = data >> bitPos & 1;
|
||||||
|
_delay_us(DELAY_US);
|
||||||
|
sm_sck.toggle();
|
||||||
|
|
||||||
|
const auto receivedBit = sm_miso.read();
|
||||||
|
dataIn |= word_t{receivedBit} << bitPos;
|
||||||
|
_delay_us(DELAY_US);
|
||||||
|
sm_sck.toggle();
|
||||||
|
} else {
|
||||||
|
sm_sck.toggle();
|
||||||
|
sm_mosi = data >> bitPos & 1;
|
||||||
|
_delay_us(DELAY_US);
|
||||||
|
|
||||||
|
sm_sck.toggle();
|
||||||
|
const auto receivedBit = sm_miso.read();
|
||||||
|
dataIn |= word_t{receivedBit} << bitPos;
|
||||||
|
_delay_us(DELAY_US);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
std::make_index_sequence<Cfg::BITS>{});
|
||||||
|
|
||||||
|
enableInterrupts(oldInterruptState);
|
||||||
|
|
||||||
|
return dataIn;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void select(const bool selectState)
|
||||||
|
{
|
||||||
|
sm_ss = !selectState;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static io::Pin<Cfg::SCK_PIN> sm_sck;
|
||||||
|
static io::Pin<Cfg::MISO_PIN> sm_miso;
|
||||||
|
static io::Pin<Cfg::MOSI_PIN> sm_mosi;
|
||||||
|
static io::Pin<Cfg::SS_PIN> sm_ss;
|
||||||
|
|
||||||
|
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
|
||||||
18
spi.hpp
18
spi.hpp
@@ -2,30 +2,30 @@
|
|||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "hardware.hpp"
|
#include "hardware.hpp"
|
||||||
|
#include "software.hpp"
|
||||||
|
|
||||||
#include "../io/io.hpp"
|
#include "../io/io.hpp"
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user