2013-09-29 13:10:03 -04:00
|
|
|
//
|
|
|
|
// FILE: FastShiftIn.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-17 09:14:55 -05:00
|
|
|
// VERSION: 0.2.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
|
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
|
|
|
|
2015-03-06 09:09:11 -05:00
|
|
|
FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
|
2013-09-29 13:10:03 -04:00
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
_bitorder = bitOrder;
|
2021-01-29 06:31:58 -05:00
|
|
|
_value = 0;
|
2020-11-27 05:16:22 -05:00
|
|
|
pinMode(datapin, INPUT);
|
|
|
|
pinMode(clockpin, OUTPUT);
|
2021-01-29 06:31:58 -05:00
|
|
|
// https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/
|
2021-12-17 09:14:55 -05: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)
|
|
|
|
|
|
|
|
// uint8_t _datatimer = digitalPinToTimer(datapin);
|
|
|
|
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
|
|
|
|
uint8_t _dataport = digitalPinToPort(datapin);
|
|
|
|
_datain = portOutputRegister(_dataport);
|
|
|
|
_databit = digitalPinToBitMask(datapin);
|
|
|
|
|
|
|
|
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
|
|
|
|
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
|
|
|
|
uint8_t _clockport = digitalPinToPort(clockpin);
|
|
|
|
_clockin = portOutputRegister(_clockport);
|
|
|
|
_clockbit = digitalPinToBitMask(clockpin);
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#else
|
2013-09-29 13:10:03 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// reference implementation
|
|
|
|
// reuse these local vars as pin to save some space
|
2020-11-27 05:16:22 -05:00
|
|
|
_databit = datapin;
|
|
|
|
_clockbit = clockpin;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2013-09-29 13:10:03 -04:00
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
|
|
|
|
2013-09-29 13:10:03 -04:00
|
|
|
int FastShiftIn::read()
|
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
if (_bitorder == LSBFIRST)
|
|
|
|
{
|
|
|
|
return readLSBFIRST();
|
|
|
|
}
|
|
|
|
return readMSBFIRST();
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
int FastShiftIn::readLSBFIRST()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
|
|
|
uint8_t value = 0;
|
2020-11-27 05:16:22 -05:00
|
|
|
uint8_t cbmask1 = _clockbit;
|
|
|
|
uint8_t cbmask2 = ~_clockbit;
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t dbmask = _databit;
|
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();
|
2020-11-27 05:16:22 -05:00
|
|
|
*_clockin |= cbmask1;
|
|
|
|
if ((*_datain & dbmask) > 0)
|
|
|
|
{
|
|
|
|
value |= m;
|
|
|
|
}
|
|
|
|
*_clockin &= cbmask2;
|
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
|
|
|
_value = value;
|
|
|
|
return _value;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#else
|
|
|
|
// reference implementation
|
|
|
|
_value = shiftIn(_databit, _clockbit, LSBFIRST);
|
|
|
|
return _value;
|
|
|
|
#endif
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 09:14:55 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
int FastShiftIn::readMSBFIRST()
|
|
|
|
{
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
|
|
|
|
|
|
|
|
uint8_t value = 0;
|
2020-11-27 05:16:22 -05:00
|
|
|
uint8_t cbmask1 = _clockbit;
|
|
|
|
uint8_t cbmask2 = ~cbmask1;
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t dbmask = _databit;
|
2015-03-06 09:09:11 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
for (uint8_t n = 128; n > 0; n >>= 1)
|
|
|
|
{
|
|
|
|
uint8_t oldSREG = SREG;
|
2021-01-29 06:31:58 -05:00
|
|
|
noInterrupts();
|
2020-11-27 05:16:22 -05:00
|
|
|
*_clockin |= cbmask1;
|
|
|
|
if ((*_datain & dbmask) > 0)
|
2013-09-29 13:10:03 -04:00
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
value |= n;
|
2015-03-06 09:09:11 -05:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
*_clockin &= cbmask2;
|
|
|
|
SREG = oldSREG;
|
|
|
|
}
|
|
|
|
_value = value;
|
|
|
|
return _value;
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#else
|
|
|
|
// reference implementation
|
|
|
|
_value = shiftIn(_databit, _clockbit, MSBFIRST);
|
|
|
|
return _value;
|
|
|
|
#endif
|
2013-09-29 13:10:03 -04:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2021-12-17 09:14:55 -05: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))
|
|
|
|
{
|
2021-12-17 09:14:55 -05: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
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
// -- END OF FILE --
|