ESPHome 2025.6.0
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 // avoid overwriting the supplied buffer. Use vector for automatic deallocation
47 auto rxbuf = std::vector<uint8_t>(length);
48 memcpy(rxbuf.data(), ptr, length);
49 this->channel_->transfer((void *) rxbuf.data(), length);
50#elif defined(USE_ESP8266)
51 // ESP8266 SPI library requires the pointer to be word aligned, but the data may not be
52 // so we need to copy the data to a temporary buffer
53 if (reinterpret_cast<uintptr_t>(ptr) & 0x3) {
54 ESP_LOGVV(TAG, "SPI write buffer not word aligned, copying to temporary buffer");
55 auto txbuf = std::vector<uint8_t>(length);
56 memcpy(txbuf.data(), ptr, length);
57 this->channel_->writeBytes(txbuf.data(), length);
58 } else {
59 this->channel_->writeBytes(ptr, length);
60 }
61#else
62 this->channel_->writeBytes(ptr, length);
63#endif
64 }
65
66 void read_array(uint8_t *ptr, size_t length) override { this->channel_->transfer(ptr, length); }
67
68 protected:
69 SPIInterface channel_{};
70};
71
72class SPIBusHw : public SPIBus {
73 public:
74 SPIBusHw(GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, SPIInterface channel) : SPIBus(clk, sdo, sdi), channel_(channel) {
75#ifdef USE_ESP8266
76 channel->pins(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
77 channel->begin();
78#endif // USE_ESP8266
79#ifdef USE_ESP32
80 channel->begin(Utility::get_pin_no(clk), Utility::get_pin_no(sdi), Utility::get_pin_no(sdo), -1);
81#endif
82#ifdef USE_RP2040
83 if (Utility::get_pin_no(sdi) != -1)
84 channel->setRX(Utility::get_pin_no(sdi));
85 if (Utility::get_pin_no(sdo) != -1)
86 channel->setTX(Utility::get_pin_no(sdo));
87 channel->setSCK(Utility::get_pin_no(clk));
88 channel->begin();
89#endif
90 }
91
92 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin) override {
93 return new SPIDelegateHw(this->channel_, data_rate, bit_order, mode, cs_pin);
94 }
95
96 protected:
97 SPIInterface channel_{};
98 bool is_hw() override { return true; }
99};
100
102 const std::vector<uint8_t> &data_pins) {
103 return new SPIBusHw(clk, sdo, sdi, interface);
104}
105
106#endif // USE_ARDUINO
107} // namespace spi
108} // 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
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