ESPHome 2025.9.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 {
5namespace spi {
6#ifdef USE_ARDUINO
7
8static const char *const TAG = "spi-esp-arduino";
9class SPIDelegateHw : public SPIDelegate {
10 public:
11 SPIDelegateHw(SPIInterface channel, uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin)
12 : SPIDelegate(data_rate, bit_order, mode, cs_pin), channel_(channel) {}
13
14 void begin_transaction() override {
15#ifdef USE_RP2040
16 SPISettings const settings(this->data_rate_, static_cast<BitOrder>(this->bit_order_), this->mode_);
17#elif defined(ESP8266)
18 // Arduino ESP8266 library has mangled values for SPI modes :-(
19 auto mode = (this->mode_ & 0x01) + ((this->mode_ & 0x02) << 3);
20 ESP_LOGVV(TAG, "8266 mangled SPI mode 0x%X", mode);
21 SPISettings const settings(this->data_rate_, this->bit_order_, mode);
22#else
23 SPISettings const settings(this->data_rate_, this->bit_order_, this->mode_);
24#endif
25 this->channel_->beginTransaction(settings);
27 }
28
29 void transfer(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
30
31 void end_transaction() override {
32 this->channel_->endTransaction();
34 }
35
36 uint8_t transfer(uint8_t data) override { return this->channel_->transfer(data); }
37
38 void write16(uint16_t data) override { this->channel_->transfer16(data); }
39
40 void write_array(const uint8_t *ptr, size_t length) override {
41 if (length == 1) {
42 this->channel_->transfer(*ptr);
43 return;
44 }
45#ifdef USE_RP2040
46 this->channel_->transfer(ptr, nullptr, length);
47#elif defined(USE_ESP8266)
48 // ESP8266 SPI library requires the pointer to be word aligned, but the data may not be
49 // so we need to copy the data to a temporary buffer
50 if (reinterpret_cast<uintptr_t>(ptr) & 0x3) {
51 ESP_LOGVV(TAG, "SPI write buffer not word aligned, copying to temporary buffer");
52 auto txbuf = std::vector<uint8_t>(length);
53 memcpy(txbuf.data(), ptr, length);
54 this->channel_->writeBytes(txbuf.data(), length);
55 } else {
56 this->channel_->writeBytes(ptr, length);
57 }
58#else
59 this->channel_->writeBytes(ptr, length);
60#endif
61 }
62
63 void read_array(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
64
65 protected:
66 SPIInterface channel_{};
67};
68
69class SPIBusHw : public SPIBus {
70 public:
71 SPIBusHw(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, SPIInterface channel) : SPIBus(clk, sdo, sdi), channel_(channel) {
72#ifdef USE_ESP8266
73 channel->pins(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
74 channel->begin();
75#endif // USE_ESP8266
76#ifdef USE_ESP32
77 channel->begin(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
78#endif
79#ifdef USE_RP2040
80 if (Utility::get_pin_no(sdi) != -1)
81 channel->setRX(Utility::get_pin_no(sdi));
82 if (Utility::get_pin_no(sdo) != -1)
83 channel->setTX(Utility::get_pin_no(sdo));
84 channel->setSCK(Utility::get_pin_no(clk));
85 channel->begin();
86#endif
87 }
88
89 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
90 bool release_device, bool write_only) override {
91 return new SPIDelegateHw(this->channel_, data_rate, bit_order, mode, cs_pin);
92 }
93
94 protected:
95 SPIInterface channel_{};
96 bool is_hw() override { return true; }
97};
98
100 const std::vector<uint8_t> &data_pins) {
101 return new SPIBusHw(clk, sdo, sdi, interface);
102}
103
104#endif // USE_ARDUINO
105} // namespace spi
106} // namespace esphome
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:196
virtual void begin_transaction()
Definition spi.h:193
SPIBitOrder bit_order_
Definition spi.h:255
static int get_pin_no(GPIOPin *pin)
Definition spi.h:136
if(packet==nullptr)
SPIMode
Modes mapping to clock phase and polarity.
Definition spi.h:80
const char *const TAG
Definition spi.cpp:8
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition spi.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
SPIClassRP2040 * SPIInterface
Definition spi.h:15
uint16_t length
Definition tt21100.cpp:0