Replaced legacy io library with new templated io library

This commit is contained in:
BlackMark 2020-02-21 16:56:30 +01:00
parent aa78ced242
commit d28cfc2929

52
spi.hpp
View File

@ -1,6 +1,6 @@
#pragma once
#include "../inout/inout.h"
#include "../io/io.hpp"
#include <avr/io.h>
@ -26,15 +26,10 @@ class Spi {
static void setCPOL(bool bCPOL);
static void setCPHA(bool bCPHA);
static constexpr InOut::Pin sm_enmSCK = InOut::Pin::P_B5;
static constexpr InOut::Pin sm_enmMISO = InOut::Pin::P_B4;
static constexpr InOut::Pin sm_enmMOSI = InOut::Pin::P_B3;
static constexpr InOut::Pin sm_enmSS = InOut::Pin::P_B2;
static InOutPin sm_cSCK;
static InOutPin sm_cMISO;
static InOutPin sm_cMOSI;
static InOutPin sm_cSS;
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::CLKDIV_128, Mode enmMode = Mode::MODE_0, bool bMaster = true,
@ -50,12 +45,6 @@ class Spi {
static void select(bool bSelect);
};
//////////////////////////////////////////////////////////////////////////
InOutPin Spi::sm_cSCK;
InOutPin Spi::sm_cMISO;
InOutPin Spi::sm_cMOSI;
InOutPin Spi::sm_cSS;
//////////////////////////////////////////////////////////////////////////
void Spi::setCPOL(bool bCPOL)
{
@ -80,23 +69,20 @@ void Spi::setCPHA(bool bCPHA)
void Spi::init(ClockDiv enmClockDiv /* = ClockDiv::CLKDIV_128 */, Mode enmMode /* = Mode::MODE_0 */,
bool bMaster /* = true */, bool bLSBFirst /* = false */, bool bMISOPullup /* = false */)
{
sm_cSCK.setPin(sm_enmSCK);
sm_cMISO.setPin(sm_enmMISO);
sm_cMOSI.setPin(sm_enmMOSI);
sm_cSS.setPin(sm_enmSS);
if (bMaster) {
sm_cSS.write(true);
sm_cSCK.setDirection(InOut::Dir::D_OUT, false);
sm_cMISO.setDirection(InOut::Dir::D_IN, bMISOPullup);
sm_cMOSI.setDirection(InOut::Dir::D_OUT, false);
sm_cSS.setDirection(InOut::Dir::D_OUT, false);
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.setDirection(InOut::Dir::D_IN, false);
sm_cMISO.setDirection(InOut::Dir::D_OUT, false);
sm_cMOSI.setDirection(InOut::Dir::D_IN, false);
sm_cSS.setDirection(InOut::Dir::D_IN, true);
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);
@ -112,10 +98,10 @@ void Spi::deinit()
{
SPCR = 0;
sm_cSCK.setDirection(InOut::Dir::D_IN, false);
sm_cMISO.setDirection(InOut::Dir::D_IN, false);
sm_cMOSI.setDirection(InOut::Dir::D_IN, false);
sm_cSS.setDirection(InOut::Dir::D_IN, false);
sm_cSCK.dir(io::Dir::IN);
sm_cMISO.dir(io::Dir::IN);
sm_cMOSI.dir(io::Dir::IN);
sm_cSS.dir(io::Dir::IN);
}
//////////////////////////////////////////////////////////////////////////