ESPHome 2026.1.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
7static const char *const TAG = "spi-esp-arduino";
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_RP2040
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_RP2040
45 this->channel_->transfer(ptr, nullptr, length);
46#elif defined(USE_ESP8266)
47 // ESP8266 SPI library requires the pointer to be word aligned, but the data may not be
48 // so we need to copy the data to a temporary buffer
49 if (reinterpret_cast<uintptr_t>(ptr) & 0x3) {
50 ESP_LOGVV(TAG, "SPI write buffer not word aligned, copying to temporary buffer");
51 auto txbuf = std::vector<uint8_t>(length);
52 memcpy(txbuf.data(), ptr, length);
53 this->channel_->writeBytes(txbuf.data(), length);
54 } else {
55 this->channel_->writeBytes(ptr, length);
56 }
57#else
58 this->channel_->writeBytes(ptr, length);
59#endif
60 }
61
62 void read_array(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
63
64 protected:
65 SPIInterface channel_{};
66};
67
68class SPIBusHw : public SPIBus {
69 public:
70 SPIBusHw(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, SPIInterface channel) : SPIBus(clk, sdo, sdi), channel_(channel) {
71#ifdef USE_ESP8266
72 channel->pins(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
73 channel->begin();
74#endif // USE_ESP8266
75#ifdef USE_ESP32
76 channel->begin(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
77#endif
78#ifdef USE_RP2040
79 if (Utility::get_pin_no(sdi) != -1)
80 channel->setRX(Utility::get_pin_no(sdi));
81 if (Utility::get_pin_no(sdo) != -1)
82 channel->setTX(Utility::get_pin_no(sdo));
83 channel->setSCK(Utility::get_pin_no(clk));
84 channel->begin();
85#endif
86 }
87
88 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
89 bool release_device, bool write_only) override {
90 return new SPIDelegateHw(this->channel_, data_rate, bit_order, mode, cs_pin);
91 }
92
93 protected:
94 SPIInterface channel_{};
95 bool is_hw() override { return true; }
96};
97
99 const std::vector<uint8_t> &data_pins) {
100 return new SPIBusHw(clk, sdo, sdi, interface);
101}
102
103#endif // USE_ARDUINO && !USE_ESP32
104} // 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)
virtual void end_transaction()
Definition spi.h:192
virtual void begin_transaction()
Definition spi.h:189
SPIBitOrder bit_order_
Definition spi.h:251
static int get_pin_no(GPIOPin *pin)
Definition spi.h:132
if(packet==nullptr)
Implementation of SPI Controller mode.
Definition spi.cpp:5
SPIMode
Modes mapping to clock phase and polarity.
Definition spi.h:76
const char *const TAG
Definition spi.cpp:7
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition spi.h:38
spi_host_device_t SPIInterface
Definition spi.h:14
uint16_t length
Definition tt21100.cpp:0