AceSPI  0.3
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 it is not
58  // possible to avoid the dependency on <SPI.h>
59 
61  #if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_SAMD)
62  static const BitOrder kBitOrder = MSBFIRST;
63  #else
64  static const uint8_t kBitOrder = MSBFIRST;
65  #endif
66 
68  static const uint8_t kSpiMode = SPI_MODE0;
69 
70  public:
78  explicit HardSpiFastInterface(T_SPI& spi) : mSpi(spi) {}
79 
84  void begin() const {
85  // To use Hardware SPI on ESP8266, we must set the SCK and MOSI pins to
86  // 'SPECIAL' instead of 'OUTPUT'. This is performed by calling
87  // SPI.begin(). Also, unlike other Arduino platforms, the SPIClass on
88  // the ESP8266 defaults to controlling the SS/CS pin itself, instead of
89  // letting the application code control it. The setHwCs(false) lets
90  // HardSpiFastInterface control the CS/SS pin.
91  // https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
92  #if defined(ESP8266)
93  mSpi.setHwCs(false);
94  #endif
95 
96  pinModeFast(T_LATCH_PIN, OUTPUT);
97  }
98 
100  void end() const {
101  pinModeFast(T_LATCH_PIN, INPUT);
102  }
103 
105  void beginTransaction() const {
106  mSpi.beginTransaction(SPISettings(T_CLOCK_SPEED, kBitOrder, kSpiMode));
107  digitalWriteFast(T_LATCH_PIN, LOW);
108  }
109 
111  void endTransaction() const {
112  digitalWriteFast(T_LATCH_PIN, HIGH);
113  mSpi.endTransaction();
114  }
115 
117  void transfer(uint8_t value) const {
118  mSpi.transfer(value);
119  }
120 
122  void transfer16(uint16_t value) const {
123  mSpi.transfer16(value);
124  }
125 
127  void send8(uint8_t value) const {
129  transfer(value);
130  endTransaction();
131  }
132 
134  void send16(uint16_t value) const {
136  transfer16(value);
137  endTransaction();
138  }
139 
141  void send16(uint8_t msb, uint8_t lsb) const {
143  uint16_t value = ((uint16_t) msb) << 8 | (uint16_t) lsb;
144  transfer16(value);
145  endTransaction();
146  }
147 
148  // Use default copy constructor and assignment operator.
149  HardSpiFastInterface(const HardSpiFastInterface&) = default;
150  HardSpiFastInterface& operator=(const HardSpiFastInterface&) = default;
151 
152  private:
153  T_SPI& mSpi;
154 };
155 
156 } // ace_spi
157 
158 #endif
ace_spi::HardSpiFastInterface::send16
void send16(uint8_t msb, uint8_t lsb) const
Convenience method to send 16 bits a single transaction.
Definition: HardSpiFastInterface.h:141
ace_spi::HardSpiFastInterface::end
void end() const
Clean up the object.
Definition: HardSpiFastInterface.h:100
ace_spi::HardSpiFastInterface::transfer16
void transfer16(uint16_t value) const
Transfer 16 bits.
Definition: HardSpiFastInterface.h:122
ace_spi::HardSpiFastInterface::transfer
void transfer(uint8_t value) const
Transfer 8 bits.
Definition: HardSpiFastInterface.h:117
ace_spi::HardSpiFastInterface::send16
void send16(uint16_t value) const
Convenience method to send 16 bits a single transaction.
Definition: HardSpiFastInterface.h:134
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::beginTransaction
void beginTransaction() const
Begin SPI transaction.
Definition: HardSpiFastInterface.h:105
ace_spi::HardSpiFastInterface::HardSpiFastInterface
HardSpiFastInterface(T_SPI &spi)
Constructor.
Definition: HardSpiFastInterface.h:78
ace_spi::HardSpiFastInterface::send8
void send8(uint8_t value) const
Convenience method to send 8 bits a single transaction.
Definition: HardSpiFastInterface.h:127
ace_spi::HardSpiFastInterface::endTransaction
void endTransaction() const
End SPI transaction.
Definition: HardSpiFastInterface.h:111
ace_spi::HardSpiFastInterface::begin
void begin() const
Initialize the HardSpiFastInterface.
Definition: HardSpiFastInterface.h:84