AceSPI  0.3
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 beginTransaction() const {
113  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
114  digitalWrite(mLatchPin, LOW);
115  }
116 
118  void endTransaction() const {
119  digitalWrite(mLatchPin, HIGH);
120  mSpi.endTransaction();
121  }
122 
124  void transfer(uint8_t value) const {
125  mSpi.transfer(value);
126  }
127 
129  void transfer16(uint16_t value) const {
130  mSpi.transfer16(value);
131  }
132 
134  void send8(uint8_t value) const {
136  transfer(value);
137  endTransaction();
138  }
139 
141  void send16(uint16_t value) const {
143  transfer16(value);
144  endTransaction();
145  }
146 
148  void send16(uint8_t msb, uint8_t lsb) const {
150  uint16_t value = ((uint16_t) msb) << 8 | (uint16_t) lsb;
151  transfer16(value);
152  endTransaction();
153  }
154 
155  // Use default copy constructor and assignment operator.
156  HardSpiInterface(const HardSpiInterface&) = default;
157  HardSpiInterface& operator=(const HardSpiInterface&) = default;
158 
159  private:
160  T_SPI& mSpi;
161  uint8_t const mLatchPin;
162 };
163 
164 } // ace_spi
165 
166 #endif
ace_spi::HardSpiInterface::HardSpiInterface
HardSpiInterface(T_SPI &spi, uint8_t latchPin)
Constructor.
Definition: HardSpiInterface.h:82
ace_spi::HardSpiInterface::endTransaction
void endTransaction() const
End SPI transaction.
Definition: HardSpiInterface.h:118
ace_spi::HardSpiInterface::send8
void send8(uint8_t value) const
Convenience method to send 8 bits a single transaction.
Definition: HardSpiInterface.h:134
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::transfer
void transfer(uint8_t value) const
Transfer 8 bits.
Definition: HardSpiInterface.h:124
ace_spi::HardSpiInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Convenience method to send 16 bits a single transaction.
Definition: HardSpiInterface.h:148
ace_spi::HardSpiInterface::send16
void send16(uint16_t value) const
Convenience method to send 16 bits a single transaction.
Definition: HardSpiInterface.h:141
ace_spi::HardSpiInterface::beginTransaction
void beginTransaction() const
Begin SPI transaction.
Definition: HardSpiInterface.h:112
ace_spi::HardSpiInterface::transfer16
void transfer16(uint16_t value) const
Transfer 16 bits.
Definition: HardSpiInterface.h:129