AceSPI  0.2.0
Unified interface for selecting hardware or software SPI implementations on Arduino platforms
HardSpiInterface.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_INTERFACE_H
26 #define ACE_SPI_HARD_SPI_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // digitalWrite()
30 #include <SPI.h>
31 
32 namespace ace_spi {
33 
53 template <
54  typename T_SPI,
55  uint32_t T_CLOCK_SPEED = 8000000
56 >
58  private:
59  // Some of the following constants are defined in <SPI.h> so unfortunately,
60  // it is not possible to avoid pulling in the global SPI instance into
61  // applications which don't use SPI.
62 
64  #if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_SAMD)
65  static const BitOrder kBitOrder = MSBFIRST;
66  #else
67  static const uint8_t kBitOrder = MSBFIRST;
68  #endif
69 
71  static const uint8_t kSpiMode = SPI_MODE0;
72 
73  public:
82  explicit HardSpiInterface(T_SPI& spi, uint8_t latchPin) :
83  mSpi(spi),
84  mLatchPin(latchPin)
85  {}
86 
91  void begin() const {
92  // To use Hardware SPI on ESP8266, we must set the SCK and MOSI pins to
93  // 'SPECIAL' instead of 'OUTPUT'. This is performed by calling
94  // SPI.begin(). Also, unlike other Arduino platforms, the SPIClass on
95  // the ESP8266 defaults to controlling the SS/CS pin itself, instead of
96  // letting the application code control it. The setHwCs(false) let's
97  // HardSpiInterface control the CS/SS pin.
98  // https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
99  #if defined(ESP8266)
100  mSpi.setHwCs(false);
101  #endif
102 
103  pinMode(mLatchPin, OUTPUT);
104  }
105 
107  void end() const {
108  pinMode(mLatchPin, INPUT);
109  }
110 
112  void send8(uint8_t value) const {
113  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
114  digitalWrite(mLatchPin, LOW);
115  mSpi.transfer(value);
116  digitalWrite(mLatchPin, HIGH);
117  mSpi.endTransaction();
118  }
119 
121  void send16(uint16_t value) const {
122  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
123  digitalWrite(mLatchPin, LOW);
124  mSpi.transfer16(value);
125  digitalWrite(mLatchPin, HIGH);
126  mSpi.endTransaction();
127  }
128 
133  void send16(uint8_t msb, uint8_t lsb) const {
134  uint16_t value = ((uint16_t) msb) << 8 | (uint16_t) lsb;
135  send16(value);
136  }
137 
138  // Use default copy constructor and assignment operator.
139  HardSpiInterface(const HardSpiInterface&) = default;
140  HardSpiInterface& operator=(const HardSpiInterface&) = default;
141 
142  private:
143  T_SPI& mSpi;
144  uint8_t const mLatchPin;
145 };
146 
147 } // ace_spi
148 
149 #endif
ace_spi::HardSpiInterface::HardSpiInterface
HardSpiInterface(T_SPI &spi, uint8_t latchPin)
Constructor.
Definition: HardSpiInterface.h:82
ace_spi::HardSpiInterface::send8
void send8(uint8_t value) const
Send 8 bits, including latching LOW and HIGH.
Definition: HardSpiInterface.h:112
ace_spi::HardSpiInterface::begin
void begin() const
Initialize the HardSpiInterface.
Definition: HardSpiInterface.h:91
ace_spi::HardSpiInterface
Hardware SPI interface to talk to SPI peripherals.
Definition: HardSpiInterface.h:57
ace_spi::HardSpiInterface::end
void end() const
Clean up the object.
Definition: HardSpiInterface.h:107
ace_spi::HardSpiInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Send two 8-bit bytes as a single 16-bit stream, including latching LOW and HIGH.
Definition: HardSpiInterface.h:133
ace_spi::HardSpiInterface::send16
void send16(uint16_t value) const
Send 16 bits, including latching LOW and HIGH.
Definition: HardSpiInterface.h:121