ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
spi_zephyr.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ZEPHYR
4
5#include "spi.h"
6#include <zephyr/drivers/spi.h>
7
8namespace esphome::spi {
9
10class ZephyrSPIDelegate;
11
12class ZephyrSPIBus : public SPIBus {
13 friend class ZephyrSPIDelegate;
14
15 public:
16 explicit ZephyrSPIBus(const device *dev) : dev_(dev) {
17 this->idle_cfg_.frequency = IDLE_FREQUENCY;
18 this->idle_cfg_.operation = SPI_OP_MODE_MASTER | SPI_WORD_SET(8);
19 }
20
21 bool is_hw() override { return true; }
22
23 SPIDelegate *get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin,
24 bool release_device, bool write_only) override;
25
26 protected:
27 // The lowest rate the SPIM driver accepts; only ever applied with no device selected
28 static constexpr uint32_t IDLE_FREQUENCY = 125000;
29
30 const device *dev_;
31 // Delegate whose mode and frequency the driver last applied
32 const ZephyrSPIDelegate *active_{nullptr};
33 // Applied when the active delegate is deleted, so the driver never keeps a pointer to a freed config
34 spi_config idle_cfg_{};
35};
36
38 public:
39 ZephyrSPIDelegate(ZephyrSPIBus *bus, uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin);
40 ~ZephyrSPIDelegate() override;
41
42 void begin_transaction() override;
43
44 uint8_t transfer(uint8_t data) override;
45 void transfer(const uint8_t *txbuf, uint8_t *rxbuf, size_t length) override;
46 void write_array(const uint8_t *ptr, size_t length) override;
47 void write_array16(const uint16_t *data, size_t length) override;
48 void read_array(uint8_t *ptr, size_t length) override;
49
50 protected:
52 void transceive_(const uint8_t *tx, uint8_t *rx, size_t length);
53
55 // CS is driven by ESPHome via SPIDelegate::cs_pin_; cfg_.cs stays unset so
56 // the Zephyr driver never touches a devicetree cs-gpios pin itself.
57 spi_config cfg_{};
58};
59
60} // namespace esphome::spi
61
62#endif // USE_ZEPHYR
BedjetMode mode
BedJet operating mode.
const ZephyrSPIDelegate * active_
Definition spi_zephyr.h:32
bool is_hw() override
Definition spi_zephyr.h:21
ZephyrSPIBus(const device *dev)
Definition spi_zephyr.h:16
static constexpr uint32_t IDLE_FREQUENCY
Definition spi_zephyr.h:28
SPIDelegate * get_delegate(uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin, bool release_device, bool write_only) override
void write_array16(const uint16_t *data, size_t length) override
void read_array(uint8_t *ptr, size_t length) override
ZephyrSPIDelegate(ZephyrSPIBus *bus, uint32_t data_rate, SPIBitOrder bit_order, SPIMode mode, GPIOPin *cs_pin)
void transceive_(const uint8_t *tx, uint8_t *rx, size_t length)
One spi_transceive; rx (if given) is zeroed when the transfer fails.
void write_array(const uint8_t *ptr, size_t length) override
uint8_t transfer(uint8_t data) override
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
static void uint32_t
uint16_t length
Definition tt21100.cpp:0