Skip to content

Commit 98b988b

Browse files
committed
add (default) support for non AVR platforms; minor refactor
1 parent 0c17a1f commit 98b988b

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

FastShiftIn.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
1616
pinMode(datapin, INPUT);
1717
pinMode(clockpin, OUTPUT);
1818

19+
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
20+
1921
// uint8_t _datatimer = digitalPinToTimer(datapin);
2022
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
2123
uint8_t _dataport = digitalPinToPort(datapin);
@@ -27,40 +29,27 @@ FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const ui
2729
uint8_t _clockport = digitalPinToPort(clockpin);
2830
_clockin = portOutputRegister(_clockport);
2931
_clockbit = digitalPinToBitMask(clockpin);
30-
}
3132

33+
#else // reference implementation
34+
35+
// reuse these vars as pin to save some space
36+
_databit = datapin;
37+
_clockbit = clockpin;
38+
39+
#endif
40+
}
3241

3342
int FastShiftIn::read()
3443
{
35-
uint8_t value = 0;
36-
uint8_t cbmask1 = _clockbit;
37-
uint8_t cbmask2 = ~_clockbit;
38-
uint8_t dbmask = _databit;
39-
40-
for (uint8_t i = 0, m = 1, n = 128; i < 8; i++, m <<= 1, n >>= 1)
41-
{
42-
uint8_t oldSREG = SREG;
43-
cli();
44-
*_clockin |= cbmask1;
45-
46-
if ((*_datain & dbmask) > 0)
47-
{
48-
if (_bitorder == LSBFIRST)
49-
{
50-
value |= m;
51-
}
52-
else
53-
{
54-
value |= n;
55-
}
56-
}
57-
*_clockin &= cbmask2;
58-
SREG = oldSREG;
59-
}
60-
_value = value;
61-
return _value;
44+
if (_bitorder == LSBFIRST)
45+
{
46+
return readLSBFIRST();
47+
}
48+
return readMSBFIRST();
6249
}
6350

51+
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
52+
6453
int FastShiftIn::readLSBFIRST()
6554
{
6655
uint8_t value = 0;
@@ -108,4 +97,18 @@ int FastShiftIn::readMSBFIRST()
10897
return _value;
10998
}
11099

100+
#else // reference implementation // note this has no cli()
101+
102+
int FastShiftIn::readLSBFIRST()
103+
{
104+
return shiftIn(_databit, _clockbit, LSBFIRST);
105+
}
106+
107+
int FastShiftIn::readMSBFIRST()
108+
{
109+
return shiftIn(_databit, _clockbit, MSBFIRST);
110+
}
111+
112+
#endif
113+
111114
// -- END OF FILE --

FastShiftIn.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@
88
// URL: https://github.com/RobTillaart/FastShiftIn.git
99
//
1010

11-
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
12-
#else
13-
#error "FastShiftIn: only AVR supported,"
14-
#endif
15-
1611
#include "Arduino.h"
1712

1813
#define FASTSHIFTIN_LIB_VERSION "0.2.0"
1914

2015
class FastShiftIn
2116
{
2217
public:
23-
// bitorder = { LSBFIRST, MSBFIRST }; // bitorder will become obsolete in the future
18+
// bitorder = { LSBFIRST, MSBFIRST };
2419
FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder = LSBFIRST);
25-
26-
// read() will become obsolete in the future
2720
int read(void);
2821

2922
// overrule bitorder (most optimized).

performance.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ example fastShiftIn: 0.2.0
1212

1313

1414
Performance - time in us : read()
15-
FastShiftIn1: 22.00
16-
FastShiftIn2: 42.87
17-
Delta: 20.87
15+
FastShiftIn1: 20.43
16+
FastShiftIn2: 39.72
17+
Delta: 19.29
1818

1919

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

3131

3232
Performance - time in us : reference shiftIn()
33-
Standard shiftIn1: 108.58
34-
Standard shiftIn2: 216.44
35-
Delta: 107.86
33+
Standard shiftIn1: 108.61
34+
Standard shiftIn2: 216.43
35+
Delta: 107.82
3636

3737
done...
3838

39-
4039
==============================
4140

4241
tested IDE 1.8.12

0 commit comments

Comments
 (0)