8 const char *
const TAG =
"spi";
20 if (this->devices_.count(device) != 0) {
21 ESP_LOGE(TAG,
"SPI device already registered");
22 return this->devices_[device];
24 SPIDelegate *delegate = this->spi_bus_->get_delegate(data_rate, bit_order, mode, cs_pin);
25 this->devices_[device] = delegate;
30 if (this->devices_.count(device) == 0) {
31 esph_log_e(TAG,
"SPI device not registered");
34 delete this->devices_[device];
35 this->devices_.erase(device);
39 ESP_LOGD(TAG,
"Setting up SPI bus...");
41 if (this->sdo_pin_ ==
nullptr)
43 if (this->sdi_pin_ ==
nullptr)
45 if (this->clk_pin_ ==
nullptr) {
46 ESP_LOGE(TAG,
"No clock pin for SPI");
51 if (this->using_hw_) {
53 SPIComponent::get_bus(this->interface_, this->clk_pin_, this->sdo_pin_, this->sdi_pin_, this->data_pins_);
54 if (this->spi_bus_ ==
nullptr) {
55 ESP_LOGE(TAG,
"Unable to allocate SPI interface");
59 this->spi_bus_ =
new SPIBus(this->clk_pin_, this->sdo_pin_, this->sdi_pin_);
60 this->clk_pin_->setup();
61 this->clk_pin_->digital_write(
true);
62 this->sdo_pin_->setup();
63 this->sdi_pin_->setup();
68 ESP_LOGCONFIG(TAG,
"SPI bus:");
69 LOG_PIN(
" CLK Pin: ", this->clk_pin_)
70 LOG_PIN(
" SDI Pin: ", this->sdi_pin_)
71 LOG_PIN(
" SDO Pin: ", this->sdo_pin_)
72 for (
size_t i = 0; i != this->data_pins_.size(); i++) {
73 ESP_LOGCONFIG(TAG,
" Data pin %u: GPIO%d", i, this->data_pins_[i]);
75 if (this->spi_bus_->is_hw()) {
76 ESP_LOGCONFIG(TAG,
" Using HW SPI: %s", this->interface_name_);
78 ESP_LOGCONFIG(TAG,
" Using software SPI");
90 this->clk_pin_->digital_write(clock_polarity_);
91 uint16_t out_data = 0;
93 for (uint8_t i = 0; i != num_bits; i++) {
96 shift = num_bits - 1 - i;
103 this->sdo_pin_->digital_write(data & (1 << shift));
104 this->cycle_clock_();
105 out_data |= uint16_t(this->sdi_pin_->digital_read()) << shift;
106 this->clk_pin_->digital_write(!this->clock_polarity_);
107 this->cycle_clock_();
108 this->clk_pin_->digital_write(this->clock_polarity_);
111 this->cycle_clock_();
112 this->clk_pin_->digital_write(!this->clock_polarity_);
113 this->sdo_pin_->digital_write(data & (1 << shift));
114 this->cycle_clock_();
115 out_data |= uint16_t(this->sdi_pin_->digital_read()) << shift;
116 this->clk_pin_->digital_write(this->clock_polarity_);
void unregister_device(SPIClient *device)
The data is sampled on a leading clock edge. (CPHA=0)
A pin to replace those that don't exist.
SPIDelegate * register_device(SPIClient *device, SPIMode mode, SPIBitOrder bit_order, uint32_t data_rate, GPIOPin *cs_pin)
The most significant bit is transmitted/received first.
void write(uint16_t data, size_t num_bits) override
uint16_t transfer_(uint16_t data, size_t num_bits)
void dump_config() override
uint8_t transfer(uint8_t data) override
BedjetMode mode
BedJet operating mode.
SPIMode
Modes mapping to clock phase and polarity.
Application App
Global storage of Application pointer - only one Application can exist.
Base class for SPIDevice, un-templated.
void begin_transaction() override
static SPIDelegate *const NULL_DELEGATE
static SPIBus * get_bus(SPIInterface interface, GPIOPin *clk, GPIOPin *sdo, GPIOPin *sdi, const std::vector< uint8_t > &data_pins)
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
A dummy SPIDelegate that complains if it's used.
Implementation of SPI Controller mode.
static GPIOPin *const NULL_PIN