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

132 lines
2.8 KiB
C++
Raw Normal View History

//
// FILE: FastShiftIn.cpp
// AUTHOR: Rob Tillaart
2021-01-29 06:31:58 -05:00
// VERSION: 0.2.2
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
FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
{
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/
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
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
}
int FastShiftIn::read()
{
2020-11-27 05:16:22 -05:00
if (_bitorder == LSBFIRST)
{
return readLSBFIRST();
}
return readMSBFIRST();
}
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
}
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;
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)
{
2020-11-27 05:16:22 -05:00
value |= n;
}
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
2020-11-27 05:16:22 -05:00
2021-01-29 06:31:58 -05:00
// reference implementation
_value = shiftIn(_databit, _clockbit, MSBFIRST);
return _value;
#endif
}
2020-11-27 05:16:22 -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))
{
_bitorder = bitOrder;
return true;
};
return false;
2020-11-27 05:16:22 -05:00
}
// -- END OF FILE --