GY-63_MS5611/libraries/FastShiftInOut/FastShiftInOut.cpp

190 lines
4.0 KiB
C++
Raw Normal View History

2022-11-05 05:55:35 -04:00
//
// FILE: FastShiftInOut.cpp
// AUTHOR: Rob Tillaart
2022-11-06 14:27:03 -05:00
// VERSION: 0.1.2
2022-11-05 05:55:35 -04:00
// PURPOSE: Arduino library for (AVR) optimized shiftInOut (simultaneously)
// URL: https://github.com/RobTillaart/FastShiftInOut
2022-11-05 13:42:15 -04:00
//
// HISTORY: see changelog.md
2022-11-05 05:55:35 -04:00
#include "FastShiftInOut.h"
FastShiftInOut::FastShiftInOut(uint8_t dataIn, uint8_t dataOut, uint8_t clockPin, uint8_t bitOrder)
{
_bitOrder = bitOrder;
pinMode(dataIn, INPUT);
pinMode(dataOut, OUTPUT);
pinMode(clockPin, OUTPUT);
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftout/
2022-11-05 13:42:15 -04:00
digitalWrite(clockPin, LOW); // assume rising pulses from clock
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t _port = digitalPinToPort(dataIn);
_dataInRegister = portInputRegister(_port);
_dataInBit = digitalPinToBitMask(dataIn);
_port = digitalPinToPort(dataOut);
_dataOutRegister = portOutputRegister(_port);
_dataOutBit = digitalPinToBitMask(dataOut);
_port = digitalPinToPort(clockPin);
_clockRegister = portOutputRegister(_port);
_clockBit = digitalPinToBitMask(clockPin);
#else
2022-11-05 05:55:35 -04:00
_dataPinIn = dataIn;
_dataPinOut = dataOut;
_clockPin = clockPin;
2022-11-05 13:42:15 -04:00
#endif
2022-11-05 05:55:35 -04:00
}
uint8_t FastShiftInOut::write(uint8_t data)
{
_value = data;
if (_bitOrder == LSBFIRST)
{
return writeLSBFIRST(_value);
}
return writeMSBFIRST(_value);
}
uint8_t FastShiftInOut::writeLSBFIRST(uint8_t data)
{
uint8_t rv = 0;
uint8_t value = data;
2022-11-05 13:42:15 -04:00
_value = value;
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t inmask1 = _dataInBit;
uint8_t outmask1 = _dataOutBit;
uint8_t outmask2 = ~_dataOutBit;
for (uint8_t m = 1; m > 0; m <<= 1)
{
uint8_t oldSREG = SREG;
noInterrupts();
2022-11-06 14:27:03 -05:00
// write one bit
2022-11-05 13:42:15 -04:00
if ((value & m) == 0) *_dataOutRegister &= outmask2;
else *_dataOutRegister |= outmask1;
2022-11-06 14:27:03 -05:00
// clock pulse HIGH
*_clockRegister |= cbmask1;
2022-11-05 13:42:15 -04:00
// read one bit
if ((*_dataInRegister & inmask1) > 0) rv |= m;
2022-11-06 14:27:03 -05:00
// clock pulse LOW
2022-11-05 13:42:15 -04:00
*_clockRegister &= cbmask2;
SREG = oldSREG;
}
return rv;
#else
2022-11-05 05:55:35 -04:00
for (uint8_t i = 0; i < 8; i++)
{
// write one bit
digitalWrite(_dataPinOut, value & 0x01);
value >>= 1;
2022-11-06 14:27:03 -05:00
// clock pulse
digitalWrite(_clockPin, HIGH);
2022-11-05 05:55:35 -04:00
// read one bit
rv >>= 1;
if (digitalRead(_dataPinIn) == HIGH) rv |= 0x80;
2022-11-06 14:27:03 -05:00
// clock pulse
2022-11-05 05:55:35 -04:00
digitalWrite(_clockPin, LOW);
}
2022-11-05 13:42:15 -04:00
#endif
2022-11-05 05:55:35 -04:00
return rv;
}
uint8_t FastShiftInOut::writeMSBFIRST(uint8_t data)
{
uint8_t rv = 0;
uint8_t value = data;
2022-11-05 13:42:15 -04:00
_value = value;
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t cbmask1 = _clockBit;
uint8_t cbmask2 = ~_clockBit;
uint8_t inmask1 = _dataInBit;
uint8_t outmask1 = _dataOutBit;
uint8_t outmask2 = ~_dataOutBit;
for (uint8_t m = 0x80; m > 0; m >>= 1)
{
uint8_t oldSREG = SREG;
noInterrupts();
2022-11-06 14:27:03 -05:00
// write one bit
2022-11-05 13:42:15 -04:00
if ((value & m) == 0) *_dataOutRegister &= outmask2;
else *_dataOutRegister |= outmask1;
2022-11-06 14:27:03 -05:00
// clock pulse HIGH
*_clockRegister |= cbmask1;
2022-11-05 13:42:15 -04:00
// read one bit
if ((*_dataInRegister & inmask1) > 0) rv |= m;
2022-11-06 14:27:03 -05:00
// clock pulse LOW
2022-11-05 13:42:15 -04:00
*_clockRegister &= cbmask2;
SREG = oldSREG;
}
return rv;
#else
2022-11-05 05:55:35 -04:00
for (uint8_t i = 0; i < 8; i++)
{
// write one bit
digitalWrite(_dataPinOut, value & 0x80);
value <<= 1;
2022-11-06 14:27:03 -05:00
// clock pulse
digitalWrite(_clockPin, HIGH);
2022-11-05 05:55:35 -04:00
// read one bit
rv <<= 1;
if (digitalRead(_dataPinIn) == HIGH) rv |= 1;
2022-11-06 14:27:03 -05:00
// clock pulse
2022-11-05 05:55:35 -04:00
digitalWrite(_clockPin, LOW);
}
2022-11-05 13:42:15 -04:00
#endif
2022-11-05 05:55:35 -04:00
return rv;
}
2022-11-05 13:42:15 -04:00
2022-11-05 05:55:35 -04:00
uint8_t FastShiftInOut::lastWritten(void)
{
return _value;
};
2022-11-05 13:42:15 -04:00
2022-11-05 05:55:35 -04:00
bool FastShiftInOut::setBitOrder(uint8_t bitOrder)
{
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
2022-11-05 13:42:15 -04:00
_bitOrder = bitOrder;
2022-11-05 05:55:35 -04:00
return true;
};
return false;
}
uint8_t FastShiftInOut::getBitOrder(void)
{
2022-11-05 13:42:15 -04:00
return _bitOrder;
2022-11-05 05:55:35 -04:00
};
// -- END OF FILE --