AceSPI  0.1.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 <typename T_SPI>
55  private:
56  // The following constants are defined without including <SPI.h> to avoid
57  // pulling in the global SPI instance into applications which don't use SPI.
58  // They may become template parameters in the future.
59 
61  static const uint32_t kClockSpeed = 8000000;
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:
74  HardSpiInterface(T_SPI& spi, uint8_t latchPin) :
75  mSpi(spi),
76  mLatchPin(latchPin)
77  {}
78 
83  void begin() const {
84  // To use Hardware SPI on ESP8266, we must set the SCK and MOSI pins to
85  // 'SPECIAL' instead of 'OUTPUT'. This is performed by calling
86  // SPI.begin(). Also, unlike other Arduino platforms, the SPIClass on
87  // the ESP8266 defaults to controlling the SS/CS pin itself, instead of
88  // letting the application code control it. The setHwCs(false) let's
89  // HardSpiInterface control the CS/SS pin.
90  // https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
91  #if defined(ESP8266)
92  mSpi.setHwCs(false);
93  #endif
94 
95  pinMode(mLatchPin, OUTPUT);
96  }
97 
98  void end() const {
99  pinMode(mLatchPin, INPUT);
100  }
101 
103  void send8(uint8_t value) const {
104  mSpi.beginTransaction(SPISettings(kClockSpeed, kBitOrder, kSpiMode));
105  digitalWrite(mLatchPin, LOW);
106  mSpi.transfer(value);
107  digitalWrite(mLatchPin, HIGH);
108  mSpi.endTransaction();
109  }
110 
112  void send16(uint16_t value) const {
113  mSpi.beginTransaction(SPISettings(kClockSpeed, kBitOrder, kSpiMode));
114  digitalWrite(mLatchPin, LOW);
115  mSpi.transfer16(value);
116  digitalWrite(mLatchPin, HIGH);
117  mSpi.endTransaction();
118  }
119 
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  private:
130  T_SPI& mSpi;
131  uint8_t const mLatchPin;
132 };
133 
134 } // ace_spi
135 
136 #endif
ace_spi::HardSpiInterface::send8
void send8(uint8_t value) const
Send 8 bits, including latching LOW and HIGH.
Definition: HardSpiInterface.h:103
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:124
ace_spi::HardSpiInterface::begin
void begin() const
Initialize the HardSpiInterface.
Definition: HardSpiInterface.h:83
ace_spi::HardSpiInterface
Hardware SPI interface to talk to one or two 74HC595 Shift Register chip(s), using the predefined SPI...
Definition: HardSpiInterface.h:54
ace_spi::HardSpiInterface::send16
void send16(uint16_t value) const
Send 16 bits, including latching LOW and HIGH.
Definition: HardSpiInterface.h:112