mirror of
https://github.com/esphome/esphome.git
synced 2025-09-27 23:52:28 +01:00
ESP-IDF support and generic target platforms (#2303)
* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -8,12 +8,13 @@ namespace spi {
|
||||
|
||||
static const char *const TAG = "spi";
|
||||
|
||||
void ICACHE_RAM_ATTR HOT SPIComponent::disable() {
|
||||
void IRAM_ATTR HOT SPIComponent::disable() {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
this->hw_spi_->endTransaction();
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
if (this->active_cs_) {
|
||||
ESP_LOGVV(TAG, "Disabling SPI Chip on pin %u...", this->active_cs_->get_pin());
|
||||
this->active_cs_->digital_write(true);
|
||||
this->active_cs_ = nullptr;
|
||||
}
|
||||
@@ -23,19 +24,37 @@ void SPIComponent::setup() {
|
||||
this->clk_->setup();
|
||||
this->clk_->digital_write(true);
|
||||
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
bool use_hw_spi = true;
|
||||
if (this->clk_->is_inverted())
|
||||
use_hw_spi = false;
|
||||
const bool has_miso = this->miso_ != nullptr;
|
||||
const bool has_mosi = this->mosi_ != nullptr;
|
||||
if (has_miso && this->miso_->is_inverted())
|
||||
int8_t clk_pin = -1, miso_pin = -1, mosi_pin = -1;
|
||||
|
||||
if (!this->clk_->is_internal())
|
||||
use_hw_spi = false;
|
||||
if (has_mosi && this->mosi_->is_inverted())
|
||||
if (has_miso && !miso_->is_internal())
|
||||
use_hw_spi = false;
|
||||
int8_t clk_pin = this->clk_->get_pin();
|
||||
int8_t miso_pin = has_miso ? this->miso_->get_pin() : -1;
|
||||
int8_t mosi_pin = has_mosi ? this->mosi_->get_pin() : -1;
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
if (has_mosi && !mosi_->is_internal())
|
||||
use_hw_spi = false;
|
||||
if (use_hw_spi) {
|
||||
auto *clk_internal = (InternalGPIOPin *) clk_;
|
||||
auto *miso_internal = (InternalGPIOPin *) miso_;
|
||||
auto *mosi_internal = (InternalGPIOPin *) mosi_;
|
||||
|
||||
if (clk_internal->is_inverted())
|
||||
use_hw_spi = false;
|
||||
if (has_miso && miso_internal->is_inverted())
|
||||
use_hw_spi = false;
|
||||
if (has_mosi && mosi_internal->is_inverted())
|
||||
use_hw_spi = false;
|
||||
|
||||
if (use_hw_spi) {
|
||||
clk_pin = clk_internal->get_pin();
|
||||
miso_pin = has_miso ? miso_internal->get_pin() : -1;
|
||||
mosi_pin = has_mosi ? mosi_internal->get_pin() : -1;
|
||||
}
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
if (clk_pin == 6 && miso_pin == 7 && mosi_pin == 8) {
|
||||
// pass
|
||||
} else if (clk_pin == 14 && (!has_miso || miso_pin == 12) && (!has_mosi || mosi_pin == 13)) {
|
||||
@@ -50,8 +69,8 @@ void SPIComponent::setup() {
|
||||
this->hw_spi_->begin();
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
#endif // USE_ESP8266
|
||||
#ifdef USE_ESP32
|
||||
static uint8_t spi_bus_num = 0;
|
||||
if (spi_bus_num >= 2) {
|
||||
use_hw_spi = false;
|
||||
@@ -67,7 +86,8 @@ void SPIComponent::setup() {
|
||||
this->hw_spi_->begin(clk_pin, miso_pin, mosi_pin);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#endif // USE_ESP32
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
|
||||
if (this->miso_ != nullptr) {
|
||||
this->miso_->setup();
|
||||
@@ -82,32 +102,28 @@ void SPIComponent::dump_config() {
|
||||
LOG_PIN(" CLK Pin: ", this->clk_);
|
||||
LOG_PIN(" MISO Pin: ", this->miso_);
|
||||
LOG_PIN(" MOSI Pin: ", this->mosi_);
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
ESP_LOGCONFIG(TAG, " Using HW SPI: %s", YESNO(this->hw_spi_ != nullptr));
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
}
|
||||
float SPIComponent::get_setup_priority() const { return setup_priority::BUS; }
|
||||
|
||||
void SPIComponent::debug_tx(uint8_t value) {
|
||||
ESP_LOGVV(TAG, " TX 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(value), value);
|
||||
}
|
||||
void SPIComponent::debug_rx(uint8_t value) {
|
||||
ESP_LOGVV(TAG, " RX 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", BYTE_TO_BINARY(value), value);
|
||||
}
|
||||
void SPIComponent::debug_enable(uint8_t pin) { ESP_LOGVV(TAG, "Enabling SPI Chip on pin %u...", pin); }
|
||||
|
||||
void SPIComponent::cycle_clock_(bool value) {
|
||||
uint32_t start = ESP.getCycleCount(); // NOLINT(readability-static-accessed-through-instance)
|
||||
while (start - ESP.getCycleCount() < this->wait_cycle_) // NOLINT(readability-static-accessed-through-instance)
|
||||
uint32_t start = arch_get_cpu_cycle_count();
|
||||
while (start - arch_get_cpu_cycle_count() < this->wait_cycle_)
|
||||
;
|
||||
this->clk_->digital_write(value);
|
||||
start += this->wait_cycle_;
|
||||
while (start - ESP.getCycleCount() < this->wait_cycle_) // NOLINT(readability-static-accessed-through-instance)
|
||||
while (start - arch_get_cpu_cycle_count() < this->wait_cycle_)
|
||||
;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE
|
||||
#ifndef CLANG_TIDY
|
||||
#pragma GCC optimize("unroll-loops")
|
||||
// NOLINTNEXTLINE
|
||||
#pragma GCC optimize("O2")
|
||||
#endif // CLANG_TIDY
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, bool READ, bool WRITE>
|
||||
uint8_t HOT SPIComponent::transfer_(uint8_t data) {
|
||||
@@ -153,15 +169,6 @@ uint8_t HOT SPIComponent::transfer_(uint8_t data) {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
|
||||
if (WRITE) {
|
||||
SPIComponent::debug_tx(data);
|
||||
}
|
||||
if (READ) {
|
||||
SPIComponent::debug_rx(out_data);
|
||||
}
|
||||
#endif
|
||||
|
||||
App.feed_wdt();
|
||||
|
||||
return out_data;
|
||||
|
@@ -1,8 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#define USE_SPI_ARDUINO_BACKEND
|
||||
#endif
|
||||
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
#include <SPI.h>
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
namespace spi {
|
||||
@@ -72,18 +80,22 @@ class SPIComponent : public Component {
|
||||
void dump_config() override;
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE> uint8_t read_byte() {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
return this->hw_spi_->transfer(0x00);
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, false>(0x00);
|
||||
}
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
void read_array(uint8_t *data, size_t length) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
this->hw_spi_->transfer(data, length);
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
data[i] = this->read_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>();
|
||||
}
|
||||
@@ -91,19 +103,23 @@ class SPIComponent : public Component {
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
void write_byte(uint8_t data) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
this->hw_spi_->write(data);
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
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) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
this->hw_spi_->write16(data);
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
|
||||
this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data >> 8);
|
||||
this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
|
||||
@@ -111,12 +127,14 @@ class SPIComponent : public Component {
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
void write_array16(const uint16_t *data, size_t length) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
this->hw_spi_->write16(data[i]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
this->write_byte16<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
|
||||
}
|
||||
@@ -124,11 +142,13 @@ class SPIComponent : public Component {
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
void write_array(const uint8_t *data, size_t length) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
auto *data_c = const_cast<uint8_t *>(data);
|
||||
this->hw_spi_->writeBytes(data_c, length);
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data[i]);
|
||||
}
|
||||
@@ -136,6 +156,7 @@ class SPIComponent : public Component {
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
uint8_t transfer_byte(uint8_t data) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->miso_ != nullptr) {
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
return this->hw_spi_->transfer(data);
|
||||
@@ -143,12 +164,14 @@ class SPIComponent : public Component {
|
||||
return this->transfer_<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE, true, true>(data);
|
||||
}
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
this->write_byte<BIT_ORDER, CLOCK_POLARITY, CLOCK_PHASE>(data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE>
|
||||
void transfer_array(uint8_t *data, size_t length) {
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
if (this->miso_ != nullptr) {
|
||||
this->hw_spi_->transfer(data, length);
|
||||
@@ -157,6 +180,7 @@ class SPIComponent : public Component {
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
|
||||
if (this->miso_ != nullptr) {
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
@@ -169,18 +193,19 @@ class SPIComponent : public Component {
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, uint32_t DATA_RATE>
|
||||
void enable(GPIOPin *cs) {
|
||||
if (cs != nullptr) {
|
||||
SPIComponent::debug_enable(cs->get_pin());
|
||||
}
|
||||
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
if (this->hw_spi_ != nullptr) {
|
||||
uint8_t data_mode = (uint8_t(CLOCK_POLARITY) << 1) | uint8_t(CLOCK_PHASE);
|
||||
SPISettings settings(DATA_RATE, BIT_ORDER, data_mode);
|
||||
this->hw_spi_->beginTransaction(settings);
|
||||
} else {
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
this->clk_->digital_write(CLOCK_POLARITY);
|
||||
this->wait_cycle_ = uint32_t(F_CPU) / DATA_RATE / 2ULL;
|
||||
uint32_t cpu_freq_hz = arch_get_cpu_freq_hz();
|
||||
this->wait_cycle_ = uint32_t(cpu_freq_hz) / DATA_RATE / 2ULL;
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
}
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
|
||||
if (cs != nullptr) {
|
||||
this->active_cs_ = cs;
|
||||
@@ -195,10 +220,6 @@ class SPIComponent : public Component {
|
||||
protected:
|
||||
inline void cycle_clock_(bool value);
|
||||
|
||||
static void debug_enable(uint8_t pin);
|
||||
static void debug_tx(uint8_t value);
|
||||
static void debug_rx(uint8_t value);
|
||||
|
||||
template<SPIBitOrder BIT_ORDER, SPIClockPolarity CLOCK_POLARITY, SPIClockPhase CLOCK_PHASE, bool READ, bool WRITE>
|
||||
uint8_t transfer_(uint8_t data);
|
||||
|
||||
@@ -206,7 +227,9 @@ class SPIComponent : public Component {
|
||||
GPIOPin *miso_{nullptr};
|
||||
GPIOPin *mosi_{nullptr};
|
||||
GPIOPin *active_cs_{nullptr};
|
||||
#ifdef USE_SPI_ARDUINO_BACKEND
|
||||
SPIClass *hw_spi_{nullptr};
|
||||
#endif // USE_SPI_ARDUINO_BACKEND
|
||||
uint32_t wait_cycle_;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user