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

346 lines
8.5 KiB
C++
Raw Normal View History

//
// FILE: FastShiftIn.cpp
// AUTHOR: Rob Tillaart
2024-09-19 08:18:47 -04:00
// VERSION: 0.4.0
2020-11-27 05:16:22 -05:00
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
// 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
#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)
{
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/
2024-07-23 07:32:24 -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
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
}
2021-12-17 09:14:55 -05:00
2023-02-20 11:23:35 -05:00
uint16_t FastShiftIn::read()
{
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()
{
if (_bitOrder == LSBFIRST)
{
2024-07-23 07:32:24 -04:00
uint16_t rv = readLSBFIRST();
2023-02-20 11:23:35 -05:00
rv += uint16_t(readLSBFIRST()) << 8;
return rv;
}
2024-07-23 07:32:24 -04:00
uint16_t rv = readMSBFIRST();
2023-02-20 11:23:35 -05:00
rv <<= 8;
rv += readMSBFIRST();
return rv;
}
uint32_t FastShiftIn::read24()
{
if (_bitOrder == LSBFIRST)
{
2024-07-23 07:32:24 -04:00
uint32_t rv = readLSBFIRST();
2023-02-20 11:23:35 -05:00
rv += uint32_t(readLSBFIRST()) << 8;
rv += uint32_t(readLSBFIRST()) << 16;
return rv;
}
2024-07-23 07:32:24 -04:00
uint32_t rv = readMSBFIRST();
2023-02-20 11:23:35 -05:00
rv <<= 8;
rv += readMSBFIRST();
rv <<= 8;
rv += readMSBFIRST();
return rv;
}
uint32_t FastShiftIn::read32()
{
if (_bitOrder == LSBFIRST)
{
2024-07-23 07:32:24 -04:00
uint32_t rv = readLSBFIRST();
2023-02-20 11:23:35 -05:00
rv += uint32_t(readLSBFIRST()) << 8;
rv += uint32_t(readLSBFIRST()) << 16;
rv += uint32_t(readLSBFIRST()) << 24;
return rv;
}
2024-07-23 07:32:24 -04:00
uint32_t rv = readMSBFIRST();
2023-02-20 11:23:35 -05:00
rv <<= 8;
rv += readMSBFIRST();
rv <<= 8;
rv += readMSBFIRST();
rv <<= 8;
rv += readMSBFIRST();
return rv;
}
2024-07-23 07:32:24 -04:00
uint32_t FastShiftIn::lastRead(void)
{
return _lastValue;
}
void FastShiftIn::read(uint8_t * array, uint8_t size)
{
if (_bitOrder == LSBFIRST)
{
for (uint8_t i = 0; i < size; i++)
{
array[size - i - 1] = readLSBFIRST();
}
return;
}
for (uint8_t i = 0; i < size; i++)
{
array[i] = readMSBFIRST();
}
return;
}
bool FastShiftIn::setBitOrder(const uint8_t bitOrder)
{
if ((bitOrder == LSBFIRST) || (bitOrder == MSBFIRST))
{
_bitOrder = bitOrder;
return true;
};
return false;
}
uint8_t FastShiftIn::getBitOrder(void)
{
return _bitOrder;
}
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)
2024-09-19 08:18:47 -04:00
#if defined(FASTSHIFTIN_AVR_LOOP_UNROLLED) // AVR SPEED OPTIMIZED #17
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t inmask1 = _dataInBit;
volatile uint8_t* localDataInRegister = _dataInRegister;
volatile uint8_t* localClockRegister = _clockRegister;
// disable interrupts (for all bits)
uint8_t oldSREG = SREG;
noInterrupts();
uint8_t r = *localClockRegister;
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x01; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x02; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x04; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x08; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x10; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x20; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x40; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x80; // read one bit
*localClockRegister = r; // clock pulse LOW
// restore interrupt state
SREG = oldSREG;
_lastValue = rv;
#else // AVR SIZE OPTIMIZED
2022-11-05 13:50:25 -04:00
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t inmask1 = _dataInBit;
2020-11-27 05:16:22 -05:00
2024-09-19 08:18:47 -04:00
volatile uint8_t* localDataInRegister = _dataInRegister;
volatile uint8_t* localClockRegister = _clockRegister;
// disable interrupts (for all bits)
uint8_t oldSREG = SREG;
noInterrupts();
uint8_t r = *localClockRegister;
2024-07-23 07:32:24 -04:00
for (uint8_t m = 0x01; m > 0; m <<= 1)
2020-11-27 05:16:22 -05:00
{
2022-11-16 10:07:44 -05:00
// clock pulse HIGH
2024-09-19 08:18:47 -04:00
*localClockRegister |= cbmask1;
2022-11-05 13:50:25 -04:00
// read one bit
2024-09-19 08:18:47 -04:00
if ((*localDataInRegister & inmask1) > 0) rv |= m;
2022-11-16 10:07:44 -05:00
// clock pulse LOW
2024-09-19 08:18:47 -04:00
*localClockRegister = r;
2020-11-27 05:16:22 -05:00
}
2024-09-19 08:18:47 -04:00
// reset interrupts flag to previous state
SREG = oldSREG;
2023-02-20 11:23:35 -05:00
_lastValue = rv;
2021-01-29 06:31:58 -05:00
2024-09-19 08:18:47 -04:00
#endif // if (AVR)
#else // other platforms reference shiftOut()
2022-11-05 13:50:25 -04:00
2023-02-20 11:23:35 -05:00
// reference implementation
_lastValue = shiftIn(_dataPinIn, _clockPin, LSBFIRST);
2022-11-05 13:50:25 -04:00
2021-01-29 06:31:58 -05:00
#endif
2024-09-19 08:18:47 -04:00
// all paths will return _lastValue.
return _lastValue;
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)
2024-09-19 08:18:47 -04:00
#if defined(FASTSHIFTIN_AVR_LOOP_UNROLLED) // AVR SPEED OPTIMIZED
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t inmask1 = _dataInBit;
volatile uint8_t* localDataInRegister = _dataInRegister;
volatile uint8_t* localClockRegister = _clockRegister;
// disable interrupts (for all bits)
uint8_t oldSREG = SREG;
noInterrupts();
uint8_t r = *localClockRegister;
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x80; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x40; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x20; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x10; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x08; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x04; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x02; // read one bit
*localClockRegister = r; // clock pulse LOW
*localClockRegister |= cbmask1; // clock pulse HIGH
if ((*localDataInRegister & inmask1) > 0) rv |= 0x01; // read one bit
*localClockRegister = r; // clock pulse LOW
// restore interrupt state
SREG = oldSREG;
2024-09-19 08:18:47 -04:00
_lastValue = rv;
#else // AVR SIZE OPTIMIZED
uint8_t rv = 0;
uint8_t cbmask1 = _clockBit;
uint8_t inmask1 = _dataInBit;
volatile uint8_t* localDataInRegister = _dataInRegister;
volatile uint8_t* localClockRegister = _clockRegister;
// disable interrupts (for all bits)
uint8_t oldSREG = SREG;
noInterrupts();
uint8_t r = *localClockRegister;
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
{
2022-11-16 10:07:44 -05:00
// clock pulse HIGH
2024-09-19 08:18:47 -04:00
*localClockRegister |= cbmask1;
2022-11-05 13:50:25 -04:00
// read one bit
2024-09-19 08:18:47 -04:00
if ((*localDataInRegister & inmask1) > 0) rv |= m;
2022-11-16 10:07:44 -05:00
// clock pulse LOW
2024-09-19 08:18:47 -04:00
*localClockRegister = r;
2020-11-27 05:16:22 -05:00
}
2024-09-19 08:18:47 -04:00
// reset interrupts flag to previous state
SREG = oldSREG;
2023-02-20 11:23:35 -05:00
_lastValue = rv;
2020-11-27 05:16:22 -05:00
2024-09-19 08:18:47 -04:00
#endif // if (AVR)
#else // other platforms reference shiftOut()
2022-11-05 13:50:25 -04:00
2023-02-20 11:23:35 -05:00
// reference implementation
_lastValue = shiftIn(_dataPinIn, _clockPin, MSBFIRST);
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
2024-09-19 08:18:47 -04:00
// all paths will return _lastValue.
return _lastValue;
}
2020-11-27 05:16:22 -05:00
2021-12-17 09:14:55 -05:00
2023-02-20 11:23:35 -05:00
// -- END OF FILE --
2022-11-05 13:50:25 -04:00