8 const char *
const TAG =
"spi";
16 if (this->devices_.count(device) != 0) {
17 ESP_LOGE(TAG,
"SPI device already registered");
18 return this->devices_[device];
20 SPIDelegate *delegate = this->spi_bus_->get_delegate(data_rate, bit_order, mode, cs_pin);
21 this->devices_[device] = delegate;
26 if (this->devices_.count(device) == 0) {
27 esph_log_e(TAG,
"SPI device not registered");
30 delete this->devices_[device];
31 this->devices_.erase(device);
35 ESP_LOGD(TAG,
"Setting up SPI bus...");
37 if (this->sdo_pin_ ==
nullptr)
39 if (this->sdi_pin_ ==
nullptr)
41 if (this->clk_pin_ ==
nullptr) {
42 ESP_LOGE(TAG,
"No clock pin for SPI");
47 if (this->using_hw_) {
49 SPIComponent::get_bus(this->interface_, this->clk_pin_, this->sdo_pin_, this->sdi_pin_, this->data_pins_);
50 if (this->spi_bus_ ==
nullptr) {
51 ESP_LOGE(TAG,
"Unable to allocate SPI interface");
55 this->spi_bus_ =
new SPIBus(this->clk_pin_, this->sdo_pin_, this->sdi_pin_);
56 this->clk_pin_->setup();
57 this->clk_pin_->digital_write(
true);
58 this->sdo_pin_->setup();
59 this->sdi_pin_->setup();
64 ESP_LOGCONFIG(TAG,
"SPI bus:");
65 LOG_PIN(
" CLK Pin: ", this->clk_pin_)
66 LOG_PIN(
" SDI Pin: ", this->sdi_pin_)
67 LOG_PIN(
" SDO Pin: ", this->sdo_pin_)
68 for (
size_t i = 0; i != this->data_pins_.size(); i++) {
69 ESP_LOGCONFIG(TAG,
" Data pin %u: GPIO%d", i, this->data_pins_[i]);
71 if (this->spi_bus_->is_hw()) {
72 ESP_LOGCONFIG(TAG,
" Using HW SPI: %s", this->interface_name_);
74 ESP_LOGCONFIG(TAG,
" Using software SPI");
84 this->clk_pin_->digital_write(clock_polarity_);
87 for (uint8_t i = 0; i != num_bits; i++) {
90 shift = num_bits - 1 - i;
97 this->sdo_pin_->digital_write(data & (1 << shift));
99 out_data |= uint16_t(this->sdi_pin_->digital_read()) << shift;
100 this->clk_pin_->digital_write(!this->clock_polarity_);
101 this->cycle_clock_();
102 this->clk_pin_->digital_write(this->clock_polarity_);
105 this->cycle_clock_();
106 this->clk_pin_->digital_write(!this->clock_polarity_);
107 this->sdo_pin_->digital_write(data & (1 << shift));
108 this->cycle_clock_();
109 out_data |= uint16_t(this->sdi_pin_->digital_read()) << shift;
110 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.
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...
Implementation of SPI Controller mode.
static GPIOPin *const NULL_PIN