Skip to content

Commit

Permalink
8266 hardware spi enable with just 3 pins (esphome#1617)
Browse files Browse the repository at this point in the history
  • Loading branch information
SenexCrenshaw authored Mar 18, 2021
1 parent a96b6e7 commit b5b2036
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion esphome/components/spi/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void SPIComponent::setup() {
#ifdef ARDUINO_ARCH_ESP8266
if (clk_pin == 6 && miso_pin == 7 && mosi_pin == 8) {
// pass
} else if (clk_pin == 14 && miso_pin == 12 && mosi_pin == 13) {
} else if (clk_pin == 14 && (!has_miso || miso_pin == 12) && (!has_mosi || mosi_pin == 13)) {
// pass
} else {
use_hw_spi = false;
Expand Down
32 changes: 32 additions & 0 deletions esphome/components/spi/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ class SPIComponent : public Component {
this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, false, true>(data);
}

template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
void write_byte16(const uint16_t data) {
if (this->hw_spi_ != nullptr) {
this->hw_spi_->write16(data);
return;
}

this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data >> 8);
this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
}

template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
void write_array16(const uint16_t *data, size_t length) {
if (this->hw_spi_ != nullptr) {
for (size_t i = 0; i < length; i++) {
this->hw_spi_->write16(data[i]);
}
return;
}
for (size_t i = 0; i < length; i++) {
this->write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
}
}

template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
void write_array(const uint8_t *data, size_t length) {
if (this->hw_spi_ != nullptr) {
Expand Down Expand Up @@ -222,6 +246,14 @@ class SPIDevice {
return this->parent_->template write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
}

void write_byte16(uint8_t data) {
return this->parent_->template write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
}

void write_array16(const uint16_t *data, size_t length) {
this->parent_->template write_array16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
}

void write_array(const uint8_t *data, size_t length) {
this->parent_->template write_array<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data, length);
}
Expand Down

0 comments on commit b5b2036

Please sign in to comment.