AceSPI  0.2.0
Unified interface for selecting hardware or software SPI implementations on Arduino platforms
HardSpiFastInterface.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_HARD_SPI_FAST_INTERFACE_H
26 #define ACE_SPI_HARD_SPI_FAST_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h>
30 #include <SPI.h>
31 
32 namespace ace_spi {
33 
50 template <
51  typename T_SPI,
52  uint8_t T_LATCH_PIN,
53  uint32_t T_CLOCK_SPEED = 8000000
54 >
56  private:
57  // Some of the following constants are defined in <SPI.h> so unfortunately,
58  // it is not possible to avoid pulling in the global SPI instance into
59  // applications which don't use SPI.
60 
62  #if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_SAMD)
63  static const BitOrder kBitOrder = MSBFIRST;
64  #else
65  static const uint8_t kBitOrder = MSBFIRST;
66  #endif
67 
69  static const uint8_t kSpiMode = SPI_MODE0;
70 
71  public:
79  explicit HardSpiFastInterface(T_SPI& spi) : mSpi(spi) {}
80 
85  void begin() const {
86  // To use Hardware SPI on ESP8266, we must set the SCK and MOSI pins to
87  // 'SPECIAL' instead of 'OUTPUT'. This is performed by calling
88  // SPI.begin(). Also, unlike other Arduino platforms, the SPIClass on
89  // the ESP8266 defaults to controlling the SS/CS pin itself, instead of
90  // letting the application code control it. The setHwCs(false) lets
91  // HardSpiFastInterface control the CS/SS pin.
92  // https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
93  #if defined(ESP8266)
94  mSpi.setHwCs(false);
95  #endif
96 
97  pinModeFast(T_LATCH_PIN, OUTPUT);
98  }
99 
101  void end() const {
102  pinModeFast(T_LATCH_PIN, INPUT);
103  }
104 
106  void send8(uint8_t value) const {
107  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
108  digitalWriteFast(T_LATCH_PIN, LOW);
109  mSpi.transfer(value);
110  digitalWriteFast(T_LATCH_PIN, HIGH);
111  mSpi.endTransaction();
112  }
113 
115  void send16(uint16_t value) const {
116  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
117  digitalWriteFast(T_LATCH_PIN, LOW);
118  mSpi.transfer16(value);
119  digitalWriteFast(T_LATCH_PIN, HIGH);
120  mSpi.endTransaction();
121  }
122 
124  void send16(uint8_t msb, uint8_t lsb) const {
125  uint16_t value = ((uint16_t) msb) << 8 | (uint16_t) lsb;
126  send16(value);
127  }
128 
129  // Use default copy constructor and assignment operator.
130  HardSpiFastInterface(const HardSpiFastInterface&) = default;
131  HardSpiFastInterface& operator=(const HardSpiFastInterface&) = default;
132 
133  private:
134  T_SPI& mSpi;
135 };
136 
137 } // ace_spi
138 
139 #endif
ace_spi::HardSpiFastInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Send 2 bytes as 16-bit stream, including latching LOW and HIGH.
Definition: HardSpiFastInterface.h:124
ace_spi::HardSpiFastInterface::end
void end() const
Clean up the object.
Definition: HardSpiFastInterface.h:101
ace_spi::HardSpiFastInterface::send16
void send16(uint16_t value) const
Send 16 bits, including latching LOW and HIGH.
Definition: HardSpiFastInterface.h:115
ace_spi::HardSpiFastInterface
This class is functionally identical to HardSpiInterface except that the GPIO pins are controlled by ...
Definition: HardSpiFastInterface.h:55
ace_spi::HardSpiFastInterface::HardSpiFastInterface
HardSpiFastInterface(T_SPI &spi)
Constructor.
Definition: HardSpiFastInterface.h:79
ace_spi::HardSpiFastInterface::send8
void send8(uint8_t value) const
Send 8 bits, including latching LOW and HIGH.
Definition: HardSpiFastInterface.h:106
ace_spi::HardSpiFastInterface::begin
void begin() const
Initialize the HardSpiFastInterface.
Definition: HardSpiFastInterface.h:85