Compare commits
4 Commits
999cd0e0c9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 8bf3d1a874 | |||
| 85346f258f | |||
| 852ad5a318 | |||
| f359f46ffc |
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "../io/io.hpp"
|
||||
|
||||
namespace spi {
|
||||
@@ -44,8 +46,8 @@ struct HardwareConfig {
|
||||
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,
|
||||
BitOrder bitOrder = BitOrder::MSB_FIRST, uint8_t bits = 8, bool pullup = false>
|
||||
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;
|
||||
|
||||
38
hardware.hpp
38
hardware.hpp
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "../io/io.hpp"
|
||||
|
||||
namespace spi {
|
||||
@@ -9,6 +11,8 @@ namespace spi {
|
||||
template <class Cfg>
|
||||
class Hardware {
|
||||
public:
|
||||
using word_t = std::uint8_t;
|
||||
|
||||
static void init()
|
||||
{
|
||||
if constexpr (Cfg::SIDE == Side::MASTER) {
|
||||
@@ -32,10 +36,10 @@ class Hardware {
|
||||
setMaster();
|
||||
setBitOrder();
|
||||
|
||||
SPCR |= (1 << SPE);
|
||||
SPCR = SPCR | (1 << SPE);
|
||||
}
|
||||
|
||||
static uint8_t transfer(uint8_t data)
|
||||
static word_t transfer(word_t data)
|
||||
{
|
||||
SPDR = data;
|
||||
while (!(SPSR & (1 << SPIF)))
|
||||
@@ -56,24 +60,24 @@ class Hardware {
|
||||
|
||||
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) {
|
||||
SPCR |= (1 << SPR0);
|
||||
SPCR = SPCR | (1 << SPR0);
|
||||
} else {
|
||||
SPCR &= ~(1 << SPR0);
|
||||
SPCR = SPCR & ~(1 << SPR0);
|
||||
}
|
||||
|
||||
if (ui8ClockDiv & (1 << 1)) {
|
||||
SPCR |= (1 << SPR1);
|
||||
SPCR = SPCR | (1 << SPR1);
|
||||
} else {
|
||||
SPCR &= ~(1 << SPR1);
|
||||
SPCR = SPCR & ~(1 << SPR1);
|
||||
}
|
||||
|
||||
if (ui8ClockDiv & (1 << 2)) {
|
||||
SPSR |= (1 << SPI2X);
|
||||
SPSR = SPSR | (1 << SPI2X);
|
||||
} else {
|
||||
SPSR &= ~(1 << SPI2X);
|
||||
SPSR = SPSR & ~(1 << SPI2X);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,35 +99,35 @@ class Hardware {
|
||||
static void setMaster()
|
||||
{
|
||||
if constexpr (Cfg::SIDE == Side::MASTER) {
|
||||
SPCR |= (1 << MSTR);
|
||||
SPCR = SPCR | (1 << MSTR);
|
||||
} else {
|
||||
SPCR &= ~(1 << MSTR);
|
||||
SPCR = SPCR & ~(1 << MSTR);
|
||||
}
|
||||
}
|
||||
|
||||
static void setBitOrder()
|
||||
{
|
||||
if constexpr (Cfg::BIT_ORDER == BitOrder::LSB_FIRST) {
|
||||
SPCR |= (1 << DORD);
|
||||
SPCR = SPCR | (1 << DORD);
|
||||
} else {
|
||||
SPCR &= ~(1 << DORD);
|
||||
SPCR = SPCR & ~(1 << DORD);
|
||||
}
|
||||
}
|
||||
|
||||
static void setCPOL(bool bCPOL)
|
||||
{
|
||||
if (bCPOL) {
|
||||
SPCR |= (1 << CPOL);
|
||||
SPCR = SPCR | (1 << CPOL);
|
||||
} else {
|
||||
SPCR &= ~(1 << CPOL);
|
||||
SPCR = SPCR & ~(1 << CPOL);
|
||||
}
|
||||
}
|
||||
static void setCPHA(bool bCPHA)
|
||||
{
|
||||
if (bCPHA) {
|
||||
SPCR |= (1 << CPHA);
|
||||
SPCR = SPCR | (1 << CPHA);
|
||||
} else {
|
||||
SPCR &= ~(1 << CPHA);
|
||||
SPCR = SPCR & ~(1 << CPHA);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
36
software.hpp
36
software.hpp
@@ -2,10 +2,14 @@
|
||||
|
||||
#include "../clock.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#include "../io/io.hpp"
|
||||
#include "../util/type.hpp"
|
||||
#include "../util/util.hpp"
|
||||
|
||||
namespace spi {
|
||||
@@ -22,15 +26,15 @@ 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,
|
||||
util::conditional_t<Cfg::BITS <= 16, uint16_t,
|
||||
util::conditional_t<Cfg::BITS <= 32, uint32_t,
|
||||
uint64_t>>>;
|
||||
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
|
||||
|
||||
public:
|
||||
static void init()
|
||||
{
|
||||
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)
|
||||
{
|
||||
const auto oldInterruptState = disableInterrupts();
|
||||
|
||||
auto dataIn = word_t{};
|
||||
|
||||
util::for_constexpr(
|
||||
@@ -72,7 +78,9 @@ class Software {
|
||||
_delay_us(DELAY_US);
|
||||
}
|
||||
},
|
||||
util::make_index_sequence<Cfg::BITS>{});
|
||||
std::make_index_sequence<Cfg::BITS>{});
|
||||
|
||||
enableInterrupts(oldInterruptState);
|
||||
|
||||
return dataIn;
|
||||
}
|
||||
@@ -89,6 +97,18 @@ class Software {
|
||||
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
|
||||
|
||||
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