Moved init to hardware driver class

This commit is contained in:
BlackMark 2020-02-21 17:26:14 +01:00
parent e0fd37dbb0
commit 3fae89285f
3 changed files with 130 additions and 105 deletions

View File

@ -21,10 +21,24 @@ enum class Mode {
MODE_3 = 3,
};
template <ClockDiv freq, Mode mode>
enum class Side {
MASTER,
SLAVE,
};
enum class BitOrder {
LSB_FIRST,
MSB_FIRST,
};
template <ClockDiv freq = ClockDiv::DIV_128, Mode mode = Mode::MODE_0, Side side = Side::MASTER,
BitOrder bitOrder = BitOrder::MSB_FIRST, bool pullup = false>
struct Config {
static constexpr auto FREQ = freq;
static constexpr auto MODE = mode;
static constexpr auto SIDE = side;
static constexpr auto BIT_ORDER = bitOrder;
static constexpr auto PULLUP = pullup;
};
} // namespace spi

View File

@ -1,9 +1,116 @@
#pragma once
#include "../io/io.hpp"
namespace spi {
template <class Cfg>
class Hardware {
public:
static void init()
{
if constexpr (Cfg::SIDE == Side::MASTER) {
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);
} else {
sm_sck.dir(io::Dir::IN);
sm_miso.dir(io::Dir::OUT);
sm_mosi.dir(io::Dir::IN);
sm_ss.dir(io::Dir::IN);
sm_ss.pullup(true);
}
setClockDiv();
setMode();
setMaster();
setBitOrder();
SPCR |= (1 << SPE);
}
private:
static io::Pin<io::P::B5> sm_sck;
static io::Pin<io::P::B4> sm_miso;
static io::Pin<io::P::B3> sm_mosi;
static io::Pin<io::P::B2> sm_ss;
static void setClockDiv()
{
uint8_t ui8ClockDiv = static_cast<uint8_t>(Cfg::FREQ);
if (ui8ClockDiv & 1) {
SPCR |= (1 << SPR0);
} else {
SPCR &= ~(1 << SPR0);
}
if (ui8ClockDiv & (1 << 1)) {
SPCR |= (1 << SPR1);
} else {
SPCR &= ~(1 << SPR1);
}
if (ui8ClockDiv & (1 << 2)) {
SPSR |= (1 << SPI2X);
} else {
SPSR &= ~(1 << SPI2X);
}
}
static void setMode()
{
if (Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_1) {
setCPOL(false);
} else {
setCPOL(true);
}
if (Cfg::MODE == Mode::MODE_0 || Cfg::MODE == Mode::MODE_2) {
setCPHA(false);
} else {
setCPHA(true);
}
}
static void setMaster()
{
if constexpr (Cfg::SIDE == Side::MASTER) {
SPCR |= (1 << MSTR);
} else {
SPCR &= ~(1 << MSTR);
}
}
static void setBitOrder()
{
if (Cfg::BIT_ORDER == BitOrder::LSB_FIRST) {
SPCR |= (1 << DORD);
} else {
SPCR &= ~(1 << DORD);
}
}
static void setCPOL(bool bCPOL)
{
if (bCPOL) {
SPCR |= (1 << CPOL);
} else {
SPCR &= ~(1 << CPOL);
}
}
static void setCPHA(bool bCPHA)
{
if (bCPHA) {
SPCR |= (1 << CPHA);
} else {
SPCR &= ~(1 << CPHA);
}
}
};
} // namespace spi

112
spi.hpp
View File

@ -10,55 +10,9 @@ namespace spi {
template <class Driver>
class Spi {
public:
private:
static void setCPOL(bool bCPOL)
static void init()
{
if (bCPOL) {
SPCR |= (1 << CPOL);
} else {
SPCR &= ~(1 << CPOL);
}
}
static void setCPHA(bool bCPHA)
{
if (bCPHA) {
SPCR |= (1 << CPHA);
} else {
SPCR &= ~(1 << CPHA);
}
}
static io::Pin<io::P::B5> sm_cSCK;
static io::Pin<io::P::B4> sm_cMISO;
static io::Pin<io::P::B3> sm_cMOSI;
static io::Pin<io::P::B2> sm_cSS;
public:
static void init(ClockDiv enmClockDiv = ClockDiv::DIV_128, Mode enmMode = Mode::MODE_0, bool bMaster = true,
bool bLSBFirst = false, bool bMISOPullup = false)
{
if (bMaster) {
sm_cSS.write(true);
sm_cSCK.dir(io::Dir::OUT);
sm_cMISO.dir(io::Dir::IN);
sm_cMISO.pullup(bMISOPullup);
sm_cMOSI.dir(io::Dir::OUT);
sm_cSS.dir(io::Dir::OUT);
} else {
sm_cSCK.dir(io::Dir::IN);
sm_cMISO.dir(io::Dir::OUT);
sm_cMOSI.dir(io::Dir::IN);
sm_cSS.dir(io::Dir::IN);
sm_cSS.pullup(true);
}
setClockDiv(enmClockDiv);
setMode(enmMode);
setMaster(bMaster);
setBitOrder(bLSBFirst);
SPCR |= (1 << SPE);
Driver::init();
}
static void deinit()
@ -71,62 +25,6 @@ class Spi {
sm_cSS.dir(io::Dir::IN);
}
static void setClockDiv(ClockDiv enmClockDiv)
{
uint8_t ui8ClockDiv = static_cast<uint8_t>(enmClockDiv);
if (ui8ClockDiv & 1) {
SPCR |= (1 << SPR0);
} else {
SPCR &= ~(1 << SPR0);
}
if (ui8ClockDiv & (1 << 1)) {
SPCR |= (1 << SPR1);
} else {
SPCR &= ~(1 << SPR1);
}
if (ui8ClockDiv & (1 << 2)) {
SPSR |= (1 << SPI2X);
} else {
SPSR &= ~(1 << SPI2X);
}
}
static void setMode(Mode enmMode)
{
if (enmMode == Mode::MODE_0 || enmMode == Mode::MODE_1) {
setCPOL(false);
} else {
setCPOL(true);
}
if (enmMode == Mode::MODE_0 || enmMode == Mode::MODE_2) {
setCPHA(false);
} else {
setCPHA(true);
}
}
static void setMaster(bool bMaster)
{
if (bMaster) {
SPCR |= (1 << MSTR);
} else {
SPCR &= ~(1 << MSTR);
}
}
static void setBitOrder(bool bLSBFirst)
{
if (bLSBFirst) {
SPCR |= (1 << DORD);
} else {
SPCR &= ~(1 << DORD);
}
}
static uint8_t transfer(uint8_t ui8Data)
{
SPDR = ui8Data;
@ -139,6 +37,12 @@ class Spi {
{
sm_cSS.write(!bSelect);
}
private:
static io::Pin<io::P::B5> sm_cSCK;
static io::Pin<io::P::B4> sm_cMISO;
static io::Pin<io::P::B3> sm_cMOSI;
static io::Pin<io::P::B2> sm_cSS;
};
} // namespace spi