Arduino-SPI
Serial Peripheral Interface (SPI) library for Arduino
ShiftIn.ino
Go to the documentation of this file.
1 #include "GPIO.h"
2 #include "SRPI.h"
3 #include "SPI.h"
4 #include "benchmark.h"
5 
6 // Configuration: SPI/BITORDER
7 #define USE_SOFTWARE_SPI
8 // #define BITORDER LSBFIRST
9 #define BITORDER MSBFIRST
10 
11 #if defined(USE_SOFTWARE_SPI)
12 #include "Software/SPI.h"
13 #if defined(ARDUINO_attiny)
14 Software::Serial<BOARD::D0> Serial;
15 GPIO<BOARD::D1> ss;
18 SRPI<BITORDER, BOARD::D2, BOARD::D3> srpi;
19 #else
20 GPIO<BOARD::D10> ss;
23 SRPI<BITORDER, BOARD::D12, BOARD::D13> srpi;
24 #endif
25 
26 #else
27 #include "Hardware/SPI.h"
28 GPIO<BOARD::D10> ss;
31 SRPI<BITORDER, BOARD::D12, BOARD::D13> srpi;
32 #endif
33 
34 #if defined(ARDUINO_attiny)
35 #define MOSI_PIN 1
36 #define MISO_PIN 2
37 #define SCK_PIN 3
38 #else
39 #define MOSI_PIN 11
40 #define MISO_PIN 12
41 #define SCK_PIN 13
42 #endif
43 
44 void setup()
45 {
46  Serial.begin(57600);
47  while (!Serial);
48  ss.output();
49  spi.acquire(0, BITORDER, spi.prescale(SPI::MAX_FREQ));
50  BENCHMARK_BASELINE(1000);
51 }
52 
53 void loop()
54 {
55  uint8_t value;
56 
57  BENCHMARK("1. Arduino core shiftIn", 1000) {
58  ss.toggle();
59  value = shiftIn(MISO_PIN, SCK_PIN, BITORDER);
60  ss.toggle();
61  }
62 
63  BENCHMARK("2. SPI input operator", 1000) {
64  ss.toggle();
65  spi >> value;
66  ss.toggle();
67  }
68 
69  BENCHMARK("3, SRPI input operator", 1000) {
70  ss.toggle();
71  srpi >> value;
72  ss.toggle();
73  }
74 
75  Serial.println();
76  delay(2000);
77 }
Software::SPI< BOARD::D11, BOARD::D12, BOARD::D13 > spi
Definition: ShiftIn.ino:21
virtual void acquire(uint8_t mode, uint8_t bitorder, uint8_t prescale)
Definition: SPI.h:70
SPI::Device< 0, BITORDER, SPI::MAX_FREQ, BOARD::D10 > dev(spi)
void setup()
Definition: ShiftIn.ino:44
static const uint32_t MAX_FREQ
Definition: SPI.h:30
virtual uint8_t prescale(uint32_t frequency)
Definition: SPI.h:51
#define BITORDER
Definition: ShiftIn.ino:9
GPIO< BOARD::D10 > ss
Definition: ShiftIn.ino:20
#define MISO_PIN
Definition: ShiftIn.ino:40
void loop()
Definition: ShiftIn.ino:53
SRPI< BITORDER, BOARD::D12, BOARD::D13 > srpi
Definition: ShiftIn.ino:23
#define SCK_PIN
Definition: ShiftIn.ino:41