Skip to content

Commit

Permalink
add (default) support for non AVR platforms; minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Apr 15, 2020
1 parent 0c17a1f commit 98b988b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
59 changes: 31 additions & 28 deletions FastShiftIn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
pinMode(datapin, INPUT);
pinMode(clockpin, OUTPUT);

#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);
Expand All @@ -27,40 +29,27 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
uint8_t _clockport = digitalPinToPort(clockpin);
_clockin = portOutputRegister(_clockport);
_clockbit = digitalPinToBitMask(clockpin);
}

#else // reference implementation

// reuse these vars as pin to save some space
_databit = datapin;
_clockbit = clockpin;

#endif
}

int FastShiftIn::read()
{
uint8_t value = 0;
uint8_t cbmask1 = _clockbit;
uint8_t cbmask2 = ~_clockbit;
uint8_t dbmask = _databit;

for (uint8_t i = 0, m = 1, n = 128; i < 8; i++, m <<= 1, n >>= 1)
{
uint8_t oldSREG = SREG;
cli();
*_clockin |= cbmask1;

if ((*_datain & dbmask) > 0)
{
if (_bitorder == LSBFIRST)
{
value |= m;
}
else
{
value |= n;
}
}
*_clockin &= cbmask2;
SREG = oldSREG;
}
_value = value;
return _value;
if (_bitorder == LSBFIRST)
{
return readLSBFIRST();
}
return readMSBFIRST();
}

#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)

int FastShiftIn::readLSBFIRST()
{
uint8_t value = 0;
Expand Down Expand Up @@ -108,4 +97,18 @@ int FastShiftIn::readMSBFIRST()
return _value;
}

#else // reference implementation // note this has no cli()

int FastShiftIn::readLSBFIRST()
{
return shiftIn(_databit, _clockbit, LSBFIRST);
}

int FastShiftIn::readMSBFIRST()
{
return shiftIn(_databit, _clockbit, MSBFIRST);
}

#endif

// -- END OF FILE --
9 changes: 1 addition & 8 deletions FastShiftIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@
// URL: https://github.com/RobTillaart/FastShiftIn.git
//

#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
#else
#error "FastShiftIn: only AVR supported,"
#endif

#include "Arduino.h"

#define FASTSHIFTIN_LIB_VERSION "0.2.0"

class FastShiftIn
{
public:
// bitorder = { LSBFIRST, MSBFIRST }; // bitorder will become obsolete in the future
// bitorder = { LSBFIRST, MSBFIRST };
FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder = LSBFIRST);

// read() will become obsolete in the future
int read(void);

// overrule bitorder (most optimized).
Expand Down
13 changes: 6 additions & 7 deletions performance.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ example fastShiftIn: 0.2.0


Performance - time in us : read()
FastShiftIn1: 22.00
FastShiftIn2: 42.87
Delta: 20.87
FastShiftIn1: 20.43
FastShiftIn2: 39.72
Delta: 19.29


Performance - time in us : readLSBFIRST()
Expand All @@ -30,13 +30,12 @@ FastShiftIn2: 38.84


Performance - time in us : reference shiftIn()
Standard shiftIn1: 108.58
Standard shiftIn2: 216.44
Delta: 107.86
Standard shiftIn1: 108.61
Standard shiftIn2: 216.43
Delta: 107.82

done...


==============================

tested IDE 1.8.12
Expand Down

0 comments on commit 98b988b

Please sign in to comment.