Skip to content

Commit 3475813

Browse files
committed
0.2.0 version
1 parent 581974f commit 3475813

File tree

11 files changed

+718
-2
lines changed

11 files changed

+718
-2
lines changed

FastShiftIn.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// FILE: FastShiftIn.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.2.0
5+
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
6+
// DATE: 2013-09-29
7+
// URL: https://github.com/RobTillaart/FastShiftIn.git
8+
//
9+
10+
#include "FastShiftIn.h"
11+
12+
FastShiftIn::FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder)
13+
{
14+
_bitorder = bitOrder;
15+
16+
pinMode(datapin, INPUT);
17+
pinMode(clockpin, INPUT);
18+
19+
20+
// uint8_t _datatimer = digitalPinToTimer(datapin);
21+
// if (_datatimer != NOT_ON_TIMER) turnOffPWM(_datatimer); TODO
22+
uint8_t _dataport = digitalPinToPort(datapin);
23+
_datain = portOutputRegister(_dataport);
24+
_databit = digitalPinToBitMask(datapin);
25+
26+
// uint8_t _clocktimer = digitalPinToTimer(clockpin);
27+
// if (_clocktimer != NOT_ON_TIMER) turnOffPWM(_clocktimer);
28+
uint8_t _clockport = digitalPinToPort(clockpin);
29+
_clockin = portOutputRegister(_clockport);
30+
_clockbit = digitalPinToBitMask(clockpin);
31+
}
32+
33+
34+
int FastShiftIn::read()
35+
{
36+
uint8_t value = 0;
37+
uint8_t cbmask1 = _clockbit;
38+
uint8_t cbmask2 = ~_clockbit;
39+
uint8_t dbmask = _databit;
40+
41+
for (uint8_t i = 0, m = 1, n = 128; i < 8; i++, m <<= 1, n >>= 1)
42+
{
43+
uint8_t oldSREG = SREG;
44+
cli();
45+
*_clockin |= cbmask1;
46+
47+
if ((*_datain & dbmask) > 0)
48+
{
49+
if (_bitorder == LSBFIRST)
50+
{
51+
value |= m;
52+
}
53+
else
54+
{
55+
value |= n;
56+
}
57+
}
58+
*_clockin &= cbmask2;
59+
SREG = oldSREG;
60+
}
61+
_value = value;
62+
return _value;
63+
}
64+
65+
int FastShiftIn::readLSBFIRST()
66+
{
67+
uint8_t value = 0;
68+
uint8_t cbmask1 = _clockbit;
69+
uint8_t cbmask2 = ~_clockbit;
70+
uint8_t dbmask = _databit;
71+
72+
for (uint8_t m = 1; m > 0; m <<= 1)
73+
{
74+
uint8_t oldSREG = SREG;
75+
cli();
76+
*_clockin |= cbmask1;
77+
if ((*_datain & dbmask) > 0)
78+
{
79+
value |= m;
80+
}
81+
*_clockin &= cbmask2;
82+
SREG = oldSREG;
83+
}
84+
_value = value;
85+
return _value;
86+
}
87+
88+
int FastShiftIn::readMSBFIRST()
89+
{
90+
uint8_t value = 0;
91+
uint8_t cbmask1 = _clockbit;
92+
uint8_t cbmask2 = ~cbmask1;
93+
uint8_t dbmask = _databit;
94+
95+
for (uint8_t n = 128; n > 0; n >>= 1)
96+
{
97+
uint8_t oldSREG = SREG;
98+
cli();
99+
*_clockin |= cbmask1;
100+
101+
if ((*_datain & dbmask) > 0)
102+
{
103+
value |= n;
104+
}
105+
*_clockin &= cbmask2;
106+
SREG = oldSREG;
107+
}
108+
_value = value;
109+
return _value;
110+
}
111+
112+
// -- END OF FILE --

FastShiftIn.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
//
3+
// FILE: FastShiftIn.h
4+
// AUTHOR: Rob Tillaart
5+
// VERSION: 0.2.0
6+
// PURPOSE: Fast ShiftIn for 74HC165 register, AVR optimized
7+
// DATE: 2013-09-29
8+
// URL: https://github.com/RobTillaart/FastShiftIn.git
9+
//
10+
11+
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
12+
#else
13+
#error "FastShiftIn: only AVR supported,"
14+
#endif
15+
16+
#include "Arduino.h"
17+
18+
#define FASTSHIFTIN_LIB_VERSION "0.2.0"
19+
20+
class FastShiftIn
21+
{
22+
public:
23+
// bitorder = { LSBFIRST, MSBFIRST }; // bitorder will become obsolete in the future
24+
FastShiftIn(const uint8_t datapin, const uint8_t clockpin, const uint8_t bitOrder = LSBFIRST);
25+
26+
// read() will become obsolete in the future
27+
int read(void);
28+
29+
// overrule bitorder (most optimized).
30+
int readLSBFIRST(void);
31+
int readMSBFIRST(void);
32+
33+
private:
34+
uint8_t _bitorder;
35+
int _value;
36+
37+
uint8_t _databit;
38+
volatile uint8_t *_datain;
39+
40+
uint8_t _clockbit;
41+
volatile uint8_t *_clockin;
42+
};
43+
44+
// -- END OF FILE --

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Rob Tillaart
3+
Copyright (c) 2013-2020 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# FastShiftIn
2-
Arduino library for (AVR) optimized shiftIn - e.g. 74HC165
2+
3+
Arduino library for (AVR) optimized shiftIn - e.g. for 74HC165
4+
5+
A library for FastShiftOut also exist.
6+
7+
## Description
8+
9+
FastShiftIn is a class that speeds up the shifting of the bits by using predetermined ports and masks.
10+
These are predetermined inthe constructor of the FastShiftIN object.
11+
12+
The performance of **read()** is substantially faster than the default Arduino **shiftIn()**,
13+
but not as fast as HW SPI. Exact how big the performance gain is can be seen with the example sketch.
14+
It does a comparison and shows how the class is to be used.
15+
16+
Version 0.2.0 added **readLSBFIRST()** and **readMSBFIRST()** to squeeze max performance.
17+
The next stop might be assembly.
18+
19+
**Note**
20+
The optimizations are AVR only for now, other platforms may follow.
21+
22+
**Note**
23+
The 74HC165 needs 0.1uF caps and the data and clock lines
24+
may need pull up resistors, especially if wires are exceeding 10 cm (4").
25+
26+
## Operation
27+
28+
See examples
29+

examples/fastShiftIn/fastShiftIn.ino

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// FILE: fastShiftIn.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.01
5+
// PURPOSE: test sketch
6+
// URL:
7+
//
8+
// Released to the public domain
9+
//
10+
11+
#include "FastShiftIn.h"
12+
13+
FastShiftIn FSI(12, 13, LSBFIRST);
14+
15+
volatile int x = 0;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.print("example fastShiftIn: ");
21+
Serial.println(FASTSHIFTIN_LIB_VERSION);
22+
23+
digitalWrite(12, HIGH);
24+
Serial.println("\n 8 bits HIGH\n");
25+
26+
Serial.println("\nPerformance - time in us");
27+
uint32_t start = micros();
28+
for (int i=0; i<1000; i++)
29+
{
30+
x = FSI.read();
31+
}
32+
uint32_t duration1 = micros() - start;
33+
Serial.print("FastShiftIn1: ");
34+
Serial.println(duration1 * 0.001);
35+
36+
start = micros();
37+
for (int i=0; i<1000; i++)
38+
{
39+
x = FSI.read();
40+
x = FSI.read();
41+
}
42+
uint32_t duration2 = micros() - start;
43+
Serial.print("FastShiftIn2: ");
44+
Serial.println(duration2 * 0.001);
45+
Serial.print(" Delta: ");
46+
Serial.println((duration2-duration1)* 0.001);
47+
Serial.println();
48+
49+
50+
start = micros();
51+
for (int i=0; i<1000; i++)
52+
{
53+
x = shiftIn(12, 13, LSBFIRST);
54+
}
55+
duration1 = micros() - start;
56+
Serial.print("Standard shiftIn1: ");
57+
Serial.println(duration1* 0.001);
58+
59+
start = micros();
60+
for (int i=0; i<1000; i++)
61+
{
62+
x = shiftIn(12, 13, LSBFIRST);
63+
x = shiftIn(12, 13, LSBFIRST);
64+
}
65+
duration2 = micros() - start;
66+
Serial.print("Standard shiftIn2: ");
67+
Serial.println(duration2 * 0.001);
68+
Serial.print(" Delta: ");
69+
Serial.println((duration2-duration1) * 0.001);
70+
Serial.println();
71+
72+
Serial.println("done...");
73+
}
74+
75+
void loop()
76+
{
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// FILE: fastShiftIn_readLSBFIRST.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test sketch
6+
// URL: https://github.com/RobTillaart/FastShiftIn
7+
//
8+
9+
#include "FastShiftIn.h"
10+
11+
FastShiftIn FSI(12, 13);
12+
13+
volatile int x = 0;
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
Serial.print("example fastShiftIn: ");
19+
Serial.println(FASTSHIFTIN_LIB_VERSION);
20+
21+
digitalWrite(12, HIGH);
22+
Serial.println("\n 8 bits HIGH - readLSBFIRST\n");
23+
24+
Serial.println("\nPerformance - time in us");
25+
uint32_t start = micros();
26+
for (int i = 0; i < 1000; i++)
27+
{
28+
x = FSI.readLSBFIRST();
29+
}
30+
uint32_t duration1 = micros() - start;
31+
Serial.print("FastShiftIn1: ");
32+
Serial.println(duration1 * 0.001);
33+
34+
start = micros();
35+
for (int i = 0; i < 1000; i++)
36+
{
37+
x = FSI.readLSBFIRST();
38+
x = FSI.readLSBFIRST();
39+
}
40+
uint32_t duration2 = micros() - start;
41+
Serial.print("FastShiftIn2: ");
42+
Serial.println(duration2 * 0.001);
43+
Serial.print(" Delta: ");
44+
Serial.println((duration2 - duration1) * 0.001);
45+
Serial.println();
46+
47+
48+
start = micros();
49+
for (int i = 0; i < 1000; i++)
50+
{
51+
x = shiftIn(12, 13, LSBFIRST);
52+
}
53+
duration1 = micros() - start;
54+
Serial.print("Standard shiftIn1: ");
55+
Serial.println(duration1 * 0.001);
56+
57+
start = micros();
58+
for (int i = 0; i < 1000; i++)
59+
{
60+
x = shiftIn(12, 13, LSBFIRST);
61+
x = shiftIn(12, 13, LSBFIRST);
62+
}
63+
duration2 = micros() - start;
64+
Serial.print("Standard shiftIn2: ");
65+
Serial.println(duration2 * 0.001);
66+
Serial.print(" Delta: ");
67+
Serial.println((duration2 - duration1) * 0.001);
68+
Serial.println();
69+
70+
Serial.println("done...");
71+
}
72+
73+
void loop()
74+
{
75+
}
76+
77+
// -- END OF FILE --

0 commit comments

Comments
 (0)