ESPHome  1.15.3
spi.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/esphal.h"
5 #include <SPI.h>
6 
7 namespace esphome {
8 namespace spi {
9 
16 };
32 };
42 };
51 enum SPIDataRate : uint32_t {
53  DATA_RATE_200KHZ = 200000,
54  DATA_RATE_1MHZ = 1000000,
55  DATA_RATE_2MHZ = 2000000,
56  DATA_RATE_4MHZ = 4000000,
57  DATA_RATE_8MHZ = 8000000,
58 };
59 
60 class SPIComponent : public Component {
61  public:
62  void set_clk(GPIOPin *clk) { clk_ = clk; }
63  void set_miso(GPIOPin *miso) { miso_ = miso; }
64  void set_mosi(GPIOPin *mosi) { mosi_ = mosi; }
65 
66  void setup() override;
67 
68  void dump_config() override;
69 
70  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE> uint8_t read_byte() {
71  if (this->hw_spi_ != nullptr) {
72  return this->hw_spi_->transfer(0x00);
73  }
74  return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, false>(0x00);
75  }
76 
77  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
78  void read_array(uint8_t *data, size_t length) {
79  if (this->hw_spi_ != nullptr) {
80  this->hw_spi_->transfer(data, length);
81  return;
82  }
83  for (size_t i = 0; i < length; i++) {
84  data[i] = this->read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>();
85  }
86  }
87 
88  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
89  void write_byte(uint8_t data) {
90  if (this->hw_spi_ != nullptr) {
91  this->hw_spi_->write(data);
92  return;
93  }
94  this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, false, true>(data);
95  }
96 
97  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
98  void write_array(const uint8_t *data, size_t length) {
99  if (this->hw_spi_ != nullptr) {
100  auto *data_c = const_cast<uint8_t *>(data);
101  this->hw_spi_->writeBytes(data_c, length);
102  return;
103  }
104  for (size_t i = 0; i < length; i++) {
105  this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
106  }
107  }
108 
109  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
110  uint8_t transfer_byte(uint8_t data) {
111  if (this->hw_spi_ != nullptr) {
112  return this->hw_spi_->transfer(data);
113  }
114  return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, true>(data);
115  }
116 
117  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
118  void transfer_array(uint8_t *data, size_t length) {
119  if (this->hw_spi_ != nullptr) {
120  this->hw_spi_->transfer(data, length);
121  return;
122  }
123  for (size_t i = 0; i < length; i++) {
124  data[i] = this->transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
125  }
126  }
127 
128  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, uint32_t DATA_RATE>
129  void enable(GPIOPin *cs) {
130  if (cs != nullptr) {
132  }
133 
134  if (this->hw_spi_ != nullptr) {
135  uint8_t data_mode = (uint8_t(CLOCK_POLARITY) << 1) | uint8_t(CLOCK_PHASE);
136  SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
137  this->hw_spi_->beginTransaction(settings);
138  } else {
139  this->clk_->digital_write(CLOCK_POLARITY);
140  this->wait_cycle_ = uint32_t(F_CPU) / DATA_RATE / 2ULL;
141  }
142 
143  if (cs != nullptr) {
144  this->active_cs_ = cs;
145  this->active_cs_->digital_write(false);
146  }
147  }
148 
149  void disable();
150 
151  float get_setup_priority() const override;
152 
153  protected:
154  inline void cycle_clock_(bool value);
155 
156  static void debug_enable(uint8_t pin);
157  static void debug_tx(uint8_t value);
158  static void debug_rx(uint8_t value);
159 
160  template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, bool READ, bool WRITE>
161  uint8_t transfer_(uint8_t data);
162 
164  GPIOPin *miso_{nullptr};
165  GPIOPin *mosi_{nullptr};
166  GPIOPin *active_cs_{nullptr};
167  SPIClass *hw_spi_{nullptr};
168  uint32_t wait_cycle_;
169 };
170 
171 template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, SPIDataRate DATA_RATE>
172 class SPIDevice {
173  public:
174  SPIDevice() = default;
175  SPIDevice(SPIComponent *parent, GPIOPin *cs) : parent_(parent), cs_(cs) {}
176 
177  void set_spi_parent(SPIComponent *parent) { parent_ = parent; }
178  void set_cs_pin(GPIOPin *cs) { cs_ = cs; }
179 
180  void spi_setup() {
181  if (this->cs_) {
182  this->cs_->setup();
183  this->cs_->digital_write(true);
184  }
185  }
186 
187  void enable() { this->parent_->template enable<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, DATA_RATE>(this->cs_); }
188 
189  void disable() { this->parent_->disable(); }
190 
191  uint8_t read_byte() { return this->parent_->template read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(); }
192 
193  void read_array(uint8_t *data, size_t length) {
194  return this->parent_->template read_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
195  }
196 
197  template<size_t N> std::array<uint8_t, N> read_array() {
198  std::array<uint8_t, N> data;
199  this->read_array(data.data(), N);
200  return data;
201  }
202 
203  void write_byte(uint8_t data) {
204  return this->parent_->template write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
205  }
206 
207  void write_array(const uint8_t *data, size_t length) {
208  this->parent_->template write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
209  }
210 
211  template<size_t N> void write_array(const std::array<uint8_t, N> &data) { this->write_array(data.data(), N); }
212 
213  void write_array(const std::vector<uint8_t> &data) { this->write_array(data.data(), data.size()); }
214 
215  uint8_t transfer_byte(uint8_t data) {
216  return this->parent_->template transfer_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
217  }
218 
219  void transfer_array(uint8_t *data, size_t length) {
220  this->parent_->template transfer_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
221  }
222 
223  template<size_t N> void transfer_array(std::array<uint8_t, N> &data) { this->transfer_array(data.data(), N); }
224 
225  protected:
226  SPIComponent *parent_{nullptr};
227  GPIOPin *cs_{nullptr};
228 };
229 
230 } // namespace spi
231 } // namespace esphome
void transfer_array(uint8_t *data, size_t length)
Definition: spi.h:219
void cycle_clock_(bool value)
Definition: spi.cpp:97
void transfer_array(uint8_t *data, size_t length)
Definition: spi.h:118
void set_miso(GPIOPin *miso)
Definition: spi.h:63
uint32_t wait_cycle_
Definition: spi.h:168
SPIDataRate
The SPI clock signal data rate.
Definition: spi.h:51
std::array< uint8_t, N > read_array()
Definition: spi.h:197
The data is sampled on a leading clock edge. (CPHA=0)
Definition: spi.h:39
The clock signal idles on HIGH.
Definition: spi.h:31
The most significant bit is transmitted/received first.
Definition: spi.h:15
void set_cs_pin(GPIOPin *cs)
Definition: spi.h:178
uint8_t transfer_byte(uint8_t data)
Definition: spi.h:215
The clock signal idles on LOW.
Definition: spi.h:26
static void debug_tx(uint8_t value)
Definition: spi.cpp:89
void write_array(const std::vector< uint8_t > &data)
Definition: spi.h:213
void write_byte(uint8_t data)
Definition: spi.h:203
void read_array(uint8_t *data, size_t length)
Definition: spi.h:78
static void debug_rx(uint8_t value)
Definition: spi.cpp:92
SPIClockPolarity
The SPI clock signal polarity,.
Definition: spi.h:21
uint8_t read_byte()
Definition: spi.h:191
void dump_config() override
Definition: spi.cpp:80
void write_array(const std::array< uint8_t, N > &data)
Definition: spi.h:211
void read_array(uint8_t *data, size_t length)
Definition: spi.h:193
uint8_t read_byte()
Definition: spi.h:70
The data is sampled on a trailing clock edge. (CPHA=1)
Definition: spi.h:41
static void debug_enable(uint8_t pin)
Definition: spi.cpp:95
virtual void digital_write(bool value)
Write the binary value to this pin using digitalWrite (and inverts automatically).
Definition: esphal.cpp:128
void set_mosi(GPIOPin *mosi)
Definition: spi.h:64
SPIClass * hw_spi_
Definition: spi.h:167
SPIDevice(SPIComponent *parent, GPIOPin *cs)
Definition: spi.h:175
void write_byte(uint8_t data)
Definition: spi.h:89
GPIOPin * active_cs_
Definition: spi.h:166
void transfer_array(std::array< uint8_t, N > &data)
Definition: spi.h:223
uint8_t transfer_(uint8_t data)
uint8_t get_pin() const
Get the GPIO pin number.
void setup() override
Definition: spi.cpp:21
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition: spi.h:11
void write_array(const uint8_t *data, size_t length)
Definition: spi.h:207
Definition: a4988.cpp:4
The least significant bit is transmitted/received first.
Definition: spi.h:13
void set_spi_parent(SPIComponent *parent)
Definition: spi.h:177
SPIClockPhase
The SPI clock signal phase.
Definition: spi.h:37
void set_clk(GPIOPin *clk)
Definition: spi.h:62
uint8_t transfer_byte(uint8_t data)
Definition: spi.h:110
void enable(GPIOPin *cs)
Definition: spi.h:129
void write_array(const uint8_t *data, size_t length)
Definition: spi.h:98
A high-level abstraction class that can expose a pin together with useful options like pinMode...
Definition: esphal.h:69
float get_setup_priority() const override
Definition: spi.cpp:87