2013-09-29 13:10:03 -04:00
|
|
|
//
|
|
|
|
// FILE: FastShiftIn.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-01 07:23:10 -04:00
|
|
|
// VERSION: 0.3.3
|
2020-11-27 05:16:22 -05:00
|
|
|
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
|
2013-09-29 13:10:03 -04:00
|
|
|
// DATE: 2013-09-29
|
2020-11-27 05:16:22 -05:00
|
|
|
// URL: https://github.com/RobTillaart/FastShiftIn
|
2022-11-05 07:41:07 -04:00
|
|
|
//
|
|
|
|
// HISTORY: see changelog.md
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2013-09-29 13:10:03 -04:00
|
|
|
|
|
|
|
#include "FastShiftIn.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
FastShiftIn::FastShiftIn(uint8_t dataIn, uint8_t clockPin, uint8_t bitOrder)
|
2013-09-29 13:10:03 -04:00
|
|
|
{
|
2023-02-20 11:23:35 -05:00
|
|
|
_bitOrder = bitOrder;
|
|
|
|
_lastValue = 0;
|
2022-11-05 13:50:25 -04:00
|
|
|
pinMode(dataIn, INPUT);
|
|
|
|
pinMode(clockPin, OUTPUT);
|
2022-11-05 07:41:07 -04:00
|
|
|
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
|
2022-11-05 13:50:25 -04:00
|
|
|
digitalWrite(clockPin, LOW); // assume rising pulses from clock
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
uint8_t _port = digitalPinToPort(dataIn);
|
|
|
|
_dataInRegister = portInputRegister(_port);
|
|
|
|
_dataInBit = digitalPinToBitMask(dataIn);
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
_port = digitalPinToPort(clockPin);
|
|
|
|
_clockRegister = portOutputRegister(_port);
|
|
|
|
_clockBit = digitalPinToBitMask(clockPin);
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#else
|
2013-09-29 13:10:03 -04:00
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
_dataPinIn = dataIn;
|
|
|
|
_clockPin = clockPin;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
#endif
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
2013-09-29 13:10:03 -04:00
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
uint16_t FastShiftIn::read()
|
2013-09-29 13:10:03 -04:00
|
|
|
{
|
2022-11-05 13:50:25 -04:00
|
|
|
if (_bitOrder == LSBFIRST)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
return readLSBFIRST();
|
|
|
|
}
|
|
|
|
return readMSBFIRST();
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
uint16_t FastShiftIn::read16()
|
|
|
|
{
|
|
|
|
uint16_t rv;
|
|
|
|
if (_bitOrder == LSBFIRST)
|
|
|
|
{
|
|
|
|
rv = readLSBFIRST();
|
|
|
|
rv += uint16_t(readLSBFIRST()) << 8;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t FastShiftIn::read24()
|
|
|
|
{
|
|
|
|
uint32_t rv;
|
|
|
|
if (_bitOrder == LSBFIRST)
|
|
|
|
{
|
|
|
|
rv = readLSBFIRST();
|
|
|
|
rv += uint32_t(readLSBFIRST()) << 8;
|
|
|
|
rv += uint32_t(readLSBFIRST()) << 16;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t FastShiftIn::read32()
|
|
|
|
{
|
|
|
|
uint32_t rv;
|
|
|
|
if (_bitOrder == LSBFIRST)
|
|
|
|
{
|
|
|
|
rv = readLSBFIRST();
|
|
|
|
rv += uint32_t(readLSBFIRST()) << 8;
|
|
|
|
rv += uint32_t(readLSBFIRST()) << 16;
|
|
|
|
rv += uint32_t(readLSBFIRST()) << 24;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
rv <<= 8;
|
|
|
|
rv += readMSBFIRST();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
uint8_t FastShiftIn::readLSBFIRST()
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
uint8_t rv = 0;
|
|
|
|
uint8_t cbmask1 = _clockBit;
|
|
|
|
uint8_t cbmask2 = ~_clockBit;
|
|
|
|
uint8_t inmask1 = _dataInBit;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
|
|
|
for (uint8_t m = 1; m > 0; m <<= 1)
|
|
|
|
{
|
|
|
|
uint8_t oldSREG = SREG;
|
2021-01-29 06:31:58 -05:00
|
|
|
noInterrupts();
|
2022-11-16 10:07:44 -05:00
|
|
|
// clock pulse HIGH
|
2022-11-05 13:50:25 -04:00
|
|
|
*_clockRegister |= cbmask1;
|
|
|
|
// read one bit
|
|
|
|
if ((*_dataInRegister & inmask1) > 0) rv |= m;
|
2022-11-16 10:07:44 -05:00
|
|
|
// clock pulse LOW
|
2022-11-05 13:50:25 -04:00
|
|
|
*_clockRegister &= cbmask2;
|
2020-11-27 05:16:22 -05:00
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
2023-02-20 11:23:35 -05:00
|
|
|
_lastValue = rv;
|
2022-11-05 13:50:25 -04:00
|
|
|
return rv;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#else
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
// reference implementation
|
|
|
|
_lastValue = shiftIn(_dataPinIn, _clockPin, LSBFIRST);
|
|
|
|
return _lastValue;
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
uint8_t FastShiftIn::readMSBFIRST()
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
uint8_t rv = 0;
|
|
|
|
uint8_t cbmask1 = _clockBit;
|
|
|
|
uint8_t cbmask2 = ~_clockBit;
|
|
|
|
uint8_t inmask1 = _dataInBit;
|
2015-03-06 09:09:11 -05:00
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
for (uint8_t m = 0x80; m > 0; m >>= 1)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
|
|
|
uint8_t oldSREG = SREG;
|
2021-01-29 06:31:58 -05:00
|
|
|
noInterrupts();
|
2022-11-16 10:07:44 -05:00
|
|
|
// clock pulse HIGH
|
2022-11-05 13:50:25 -04:00
|
|
|
*_clockRegister |= cbmask1;
|
|
|
|
// read one bit
|
|
|
|
if ((*_dataInRegister & inmask1) > 0) rv |= m;
|
2022-11-16 10:07:44 -05:00
|
|
|
// clock pulse LOW
|
2022-11-05 13:50:25 -04:00
|
|
|
*_clockRegister &= cbmask2;
|
2020-11-27 05:16:22 -05:00
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
2023-02-20 11:23:35 -05:00
|
|
|
_lastValue = rv;
|
2022-11-05 13:50:25 -04:00
|
|
|
return rv;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#else
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
// reference implementation
|
|
|
|
_lastValue = shiftIn(_dataPinIn, _clockPin, MSBFIRST);
|
|
|
|
return _lastValue;
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#endif
|
2022-11-05 13:50:25 -04:00
|
|
|
|
2013-09-29 13:10:03 -04:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
uint32_t FastShiftIn::lastRead(void)
|
2022-11-05 13:50:25 -04:00
|
|
|
{
|
2023-02-20 11:23:35 -05:00
|
|
|
return _lastValue;
|
2022-11-05 13:50:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool FastShiftIn::setBitOrder(const uint8_t bitOrder)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
|
|
|
|
{
|
2022-11-05 13:50:25 -04:00
|
|
|
_bitOrder = bitOrder;
|
2021-01-29 06:31:58 -05:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
return false;
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2022-11-05 13:50:25 -04:00
|
|
|
uint8_t FastShiftIn::getBitOrder(void)
|
|
|
|
{
|
|
|
|
return _bitOrder;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-02-20 11:23:35 -05:00
|
|
|
// -- END OF FILE --
|
2022-11-05 13:50:25 -04:00
|
|
|
|