AceSPI  0.3
Unified interface for selecting hardware or software SPI implementations on Arduino platforms
SimpleSpiFastInterface.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_SIMPLE_SPI_FAST_INTERFACE_H
26 #define ACE_SPI_SIMPLE_SPI_FAST_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // OUTPUT, INPUT
30 
31 namespace ace_spi {
32 
41 template <uint8_t T_LATCH_PIN, uint8_t T_DATA_PIN, uint8_t T_CLOCK_PIN>
43  public:
45  explicit SimpleSpiFastInterface() = default;
46 
48  void begin() const {
49  pinModeFast(T_LATCH_PIN, OUTPUT);
50  pinModeFast(T_DATA_PIN, OUTPUT);
51  pinModeFast(T_CLOCK_PIN, OUTPUT);
52  }
53 
55  void end() const {
56  pinModeFast(T_LATCH_PIN, INPUT);
57  pinModeFast(T_DATA_PIN, INPUT);
58  pinModeFast(T_CLOCK_PIN, INPUT);
59  }
60 
62  void beginTransaction() const {
63  digitalWriteFast(T_LATCH_PIN, LOW);
64  }
65 
67  void endTransaction() const {
68  digitalWriteFast(T_LATCH_PIN, HIGH);
69  }
70 
72  void transfer(uint8_t value) const {
73  shiftOutFast(value);
74  }
75 
77  void transfer16(uint16_t value) const {
78  uint8_t msb = (value & 0xff00) >> 8;
79  uint8_t lsb = (value & 0xff);
80  shiftOutFast(msb);
81  shiftOutFast(lsb);
82  }
83 
85  void send8(uint8_t value) const {
87  transfer(value);
89  }
90 
92  void send16(uint16_t value) const {
94  transfer16(value);
96  }
97 
99  void send16(uint8_t msb, uint8_t lsb) const {
101  shiftOutFast(msb);
102  shiftOutFast(lsb);
103  endTransaction();
104  }
105 
106  // Use default copy constructor and assignment operator.
108  SimpleSpiFastInterface& operator=(const SimpleSpiFastInterface&) = default;
109 
110  private:
111  static void shiftOutFast(uint8_t output) {
112  uint8_t mask = 0x80; // start with the MSB
113  for (uint8_t i = 0; i < 8; i++) {
114  digitalWriteFast(T_CLOCK_PIN, LOW);
115  if (output & mask) {
116  digitalWriteFast(T_DATA_PIN, HIGH);
117  } else {
118  digitalWriteFast(T_DATA_PIN, LOW);
119  }
120  digitalWriteFast(T_CLOCK_PIN, HIGH);
121  mask >>= 1;
122  }
123  }
124 };
125 
126 } // ace_spi
127 
128 #endif
ace_spi::SimpleSpiFastInterface
Software SPI using pinModeFast(), digitalWriteFast() and shiftOutFast() from https://github....
Definition: SimpleSpiFastInterface.h:42
ace_spi::SimpleSpiFastInterface::end
void end() const
Reset the various pins.
Definition: SimpleSpiFastInterface.h:55
ace_spi::SimpleSpiFastInterface::begin
void begin() const
Initialize the various pins.
Definition: SimpleSpiFastInterface.h:48
ace_spi::SimpleSpiFastInterface::SimpleSpiFastInterface
SimpleSpiFastInterface()=default
Constructor.
ace_spi::SimpleSpiFastInterface::endTransaction
void endTransaction() const
End SPI transaction.
Definition: SimpleSpiFastInterface.h:67
ace_spi::SimpleSpiFastInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Convenience method to send 16 bits a single transaction.
Definition: SimpleSpiFastInterface.h:99
ace_spi::SimpleSpiFastInterface::transfer16
void transfer16(uint16_t value) const
Transfer 16 bits.
Definition: SimpleSpiFastInterface.h:77
ace_spi::SimpleSpiFastInterface::beginTransaction
void beginTransaction() const
Begin SPI transaction.
Definition: SimpleSpiFastInterface.h:62
ace_spi::SimpleSpiFastInterface::send16
void send16(uint16_t value) const
Convenience method to send 16 bits a single transaction.
Definition: SimpleSpiFastInterface.h:92
ace_spi::SimpleSpiFastInterface::send8
void send8(uint8_t value) const
Convenience method to send 8 bits a single transaction.
Definition: SimpleSpiFastInterface.h:85
ace_spi::SimpleSpiFastInterface::transfer
void transfer(uint8_t value) const
Transfer 8 bits.
Definition: SimpleSpiFastInterface.h:72