AceSPI  0.1.0
Unified interface for selecting hardware or software SPI implementations on Arduino platforms
SoftSpiFastInterface.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_SPI_SOFT_SPI_FAST_INTERFACE_H
26 #define ACE_SPI_SOFT_SPI_FAST_INTERFACE_H
27 
28 // This header file requires the digitalWriteFast library on AVR, or the
29 // EpoxyMockDigitalWriteFast library on EpoxyDuino.
30 #if defined(ARDUINO_ARCH_AVR) || defined(EPOXY_DUINO)
31 
32 #include <stdint.h>
33 #include <Arduino.h> // OUTPUT, INPUT
34 
35 namespace ace_spi {
36 
45 template <uint8_t T_LATCH_PIN, uint8_t T_DATA_PIN, uint8_t T_CLOCK_PIN>
46 class SoftSpiFastInterface {
47  public:
48  SoftSpiFastInterface() = default;
49 
50  void begin() const {
51  pinModeFast(T_LATCH_PIN, OUTPUT);
52  pinModeFast(T_DATA_PIN, OUTPUT);
53  pinModeFast(T_CLOCK_PIN, OUTPUT);
54  }
55 
56  void end() const {
57  pinModeFast(T_LATCH_PIN, INPUT);
58  pinModeFast(T_DATA_PIN, INPUT);
59  pinModeFast(T_CLOCK_PIN, INPUT);
60  }
61 
63  void send8(uint8_t value) const {
64  digitalWriteFast(T_LATCH_PIN, LOW);
65  shiftOutFast(value);
66  digitalWriteFast(T_LATCH_PIN, HIGH);
67  }
68 
70  void send16(uint16_t value) const {
71  uint8_t msb = (value & 0xff00) >> 8;
72  uint8_t lsb = (value & 0xff);
73  send16(msb, lsb);
74  }
75 
77  void send16(uint8_t msb, uint8_t lsb) const {
78  digitalWriteFast(T_LATCH_PIN, LOW);
79  shiftOutFast(msb);
80  shiftOutFast(lsb);
81  digitalWriteFast(T_LATCH_PIN, HIGH);
82  }
83 
84  private:
85  static void shiftOutFast(uint8_t output) {
86  uint8_t mask = 0x80; // start with the MSB
87  for (uint8_t i = 0; i < 8; i++) {
88  digitalWriteFast(T_CLOCK_PIN, LOW);
89  if (output & mask) {
90  digitalWriteFast(T_DATA_PIN, HIGH);
91  } else {
92  digitalWriteFast(T_DATA_PIN, LOW);
93  }
94  digitalWriteFast(T_CLOCK_PIN, HIGH);
95  mask >>= 1;
96  }
97  }
98 };
99 
100 } // ace_spi
101 
102 #endif // defined(ARDUINO_ARCH_AVR)
103 
104 #endif