65 lines
1.4 KiB
C++
65 lines
1.4 KiB
C++
#include "softspim.h"
|
|
#include "writepin.h"
|
|
#include <util/delay.h>
|
|
|
|
SpiMaster::SpiMaster()
|
|
{
|
|
|
|
}
|
|
|
|
uint8_t SpiMaster::readWrite(uint8_t in)
|
|
{
|
|
_delay_us(DELAY_TIME_US);
|
|
uint8_t recByte = 0;
|
|
for(uint8_t i = 0; i < 8; ++i)
|
|
{
|
|
if constexpr (BIT_ORDER == 0)
|
|
writePin(_port, _pinOut, in & (1 << i));
|
|
else
|
|
writePin(_port, _pinOut, in & (1 << (7-i)));
|
|
if constexpr (CLOCK_PHASE == 0 || CLOCK_PHASE == 2)
|
|
_delay_us(DELAY_TIME_US);
|
|
writePin(_port, _pinClock, !CLOCK_POLARITY);
|
|
if constexpr (CLOCK_PHASE == 1 || CLOCK_PHASE == 2)
|
|
_delay_us(DELAY_TIME_US);
|
|
if constexpr (BIT_ORDER == 0)
|
|
recByte |= readPin(_pinReg, _pinIn) << i;
|
|
else
|
|
recByte |= readPin(_pinReg, _pinIn) << (7-i);
|
|
if constexpr (CLOCK_PHASE == 0 || CLOCK_PHASE == 2)
|
|
_delay_us(DELAY_TIME_US);
|
|
writePin(_port, _pinClock, CLOCK_POLARITY);
|
|
if constexpr (CLOCK_PHASE == 1 || CLOCK_PHASE == 2)
|
|
_delay_us(DELAY_TIME_US);
|
|
}
|
|
return recByte;
|
|
}
|
|
|
|
void SpiMaster::readWrite(uint8_t length, uint8_t* bufferIn, uint8_t* bufferOut)
|
|
{
|
|
for(uint8_t i = 0; i < length; ++i)
|
|
{
|
|
uint8_t outByte = 0;
|
|
if(bufferOut) outByte = bufferOut[i];
|
|
uint8_t inByte = readWrite(outByte);
|
|
if(bufferIn) bufferIn[i] = inByte;
|
|
}
|
|
}
|
|
|
|
|
|
void SpiMaster::prepare()
|
|
{
|
|
writePin(_port, _pinClock, CLOCK_POLARITY);
|
|
}
|
|
|
|
|
|
void SpiMaster::write(uint8_t in)
|
|
{
|
|
readWrite(in);
|
|
}
|
|
|
|
uint8_t SpiMaster::read()
|
|
{
|
|
return readWrite();
|
|
}
|