ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
spi_arduino.cpp
Go to the documentation of this file.
1#include "spi.h"
2#include <vector>
3
4namespace esphome::spi {
5#if defined(USE_ARDUINO) && !defined(USE_ESP32)
6
7ESPHOME_LOG_TAG(TAG, "spi");
8class SPIDelegateHw : public SPIDelegate {
9 public:
10 SPIDelegateHw(SPIInterface channel, uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin)
11 : SPIDelegate(data_rate, bit_order, mode, cs_pin), channel_(channel) {}
12
13 void begin_transaction() override {
14#ifdef USE_RP2
15 SPISettings const settings(this->data_rate_, static_cast<BitOrder>(this->bit_order_), this->mode_);
16#elif defined(ESP8266)
17 // Arduino ESP8266 library has mangled values for SPI modes :-(
18 auto mode = (this->mode_ & 0x01) + ((this->mode_ & 0x02) << 3);
19 ESP_LOGVV(TAG, "8266 mangled SPI mode 0x%X", mode);
20 SPISettings const settings(this->data_rate_, this->bit_order_, mode);
21#else
22 SPISettings const settings(this->data_rate_, this->bit_order_, this->mode_);
23#endif
24 this->channel_->beginTransaction(settings);
26 }
27
28 void transfer(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
29
30 void end_transaction() override {
31 this->channel_->endTransaction();
33 }
34
35 uint8_t transfer(uint8_t data) override { return this->channel_->transfer(data); }
36
37 void write16(uint16_t data) override { this->channel_->transfer16(data); }
38
39 void write_array(const uint8_t *ptr, size_t length) override {
40 if (length == 1) {
41 this->channel_->transfer(*ptr);
42 return;
43 }
44#ifdef USE_RP2
45 this->channel_->transfer(ptr, nullptr, length);
46#elif defined(USE_ESP8266)
47 // writeBytes() needs a word aligned pointer; transferBytes() bounces unaligned chunks through a stack buffer
48 this->channel_->transferBytes(ptr, nullptr, length);
49#else
50 this->channel_->writeBytes(ptr, length);
51#endif
52 }
53
54 void read_array(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
55
56 protected:
57 SPIInterface channel_{};
58};
59
60class SPIBusHw : public SPIBus {
61 public:
62 SPIBusHw(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, SPIInterface channel) : SPIBus(clk, sdo, sdi), channel_(channel) {
63#ifdef USE_ESP8266
64 channel->pins(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
65 channel->begin();
66#endif // USE_ESP8266
67#ifdef USE_ESP32
68 channel->begin(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
69#endif
70#ifdef USE_RP2
71 if (Utility::get_pin_no(sdi) != -1)
72 channel->setRX(Utility::get_pin_no(sdi));
73 if (Utility::get_pin_no(sdo) != -1)
74 channel->setTX(Utility::get_pin_no(sdo));
75 channel->setSCK(Utility::get_pin_no(clk));
76 channel->begin();
77#endif
78 }
79
80 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
81 bool release_device, bool write_only) override {
82 return new SPIDelegateHw(this->channel_, data_rate, bit_order, mode, cs_pin);
83 }
84
85 protected:
86 SPIInterface channel_{};
87 bool is_hw() override { return true; }
88};
89
90SPIBus *SPIComponent::get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi,
91 const std::vector<uint8_t> &data_pins) {
92 return new SPIBusHw(clk, sdo, sdi, interface);
93}
94
95#endif // USE_ARDUINO && !USE_ESP32
96} // namespace esphome::spi
BedjetMode mode
BedJet operating mode.
static SPIBus * get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, const std::vector< uint8_t > &data_pins)
Definition spi.cpp:136
virtual void end_transaction()
Definition spi.h:205
virtual void begin_transaction()
Definition spi.h:202
SPIBitOrder bit_order_
Definition spi.h:267
static int get_pin_no(GPIOPin *pin)
Definition spi.h:144
Implementation of SPI Controller mode.
Definition spi.cpp:9
SPIMode
Modes mapping to clock phase and polarity.
Definition spi.h:84
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition spi.h:46
ESPHOME_LOG_TAG(TAG, "spi")
if(written< 0)
Definition helpers.h:1264
static void uint32_t
spi_host_device_t SPIInterface
Definition spi.h:15
uint16_t length
Definition tt21100.cpp:0