1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-18 03:02:20 +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:
Otto Winter
2021-09-20 11:47:51 +02:00
committed by GitHub
parent 1e8e471dec
commit ac0d921413
583 changed files with 9008 additions and 5420 deletions

View File

@@ -1,40 +1,20 @@
#include "ledc_output.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
#ifdef USE_ESP32
#ifdef USE_ARDUINO
#include <esp32-hal-ledc.h>
#endif
#ifdef USE_ESP_IDF
#include <driver/ledc.h>
#endif
namespace esphome {
namespace ledc {
static const char *const TAG = "ledc.output";
void LEDCOutput::write_state(float state) {
if (this->pin_->is_inverted())
state = 1.0f - state;
this->duty_ = state;
const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
const float duty_rounded = roundf(state * max_duty);
auto duty = static_cast<uint32_t>(duty_rounded);
ledcWrite(this->channel_, duty);
}
void LEDCOutput::setup() {
this->update_frequency(this->frequency_);
this->turn_off();
// Attach pin after setting default value
ledcAttachPin(this->pin_->get_pin(), this->channel_);
}
void LEDCOutput::dump_config() {
ESP_LOGCONFIG(TAG, "LEDC Output:");
LOG_PIN(" Pin ", this->pin_);
ESP_LOGCONFIG(TAG, " LEDC Channel: %u", this->channel_);
ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
}
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth) { return 80e6f / float(1 << bit_depth); }
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth) {
const float max_div_num = ((1 << 20) - 1) / 256.0f;
@@ -50,6 +30,67 @@ optional<uint8_t> ledc_bit_depth_for_frequency(float frequency) {
return {};
}
void LEDCOutput::write_state(float state) {
if (this->pin_->is_inverted())
state = 1.0f - state;
this->duty_ = state;
const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
const float duty_rounded = roundf(state * max_duty);
auto duty = static_cast<uint32_t>(duty_rounded);
#ifdef USE_ARDUINO
ledcWrite(this->channel_, duty);
#endif
#ifdef USE_ESP_IDF
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
ledc_set_duty(speed_mode, chan_num, duty);
ledc_update_duty(speed_mode, chan_num);
#endif
}
void LEDCOutput::setup() {
#ifdef USE_ARDUINO
this->update_frequency(this->frequency_);
this->turn_off();
// Attach pin after setting default value
ledcAttachPin(this->pin_->get_pin(), this->channel_);
#endif
#ifdef USE_ESP_IDF
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
bit_depth_ = *ledc_bit_depth_for_frequency(frequency_);
ledc_timer_config_t timer_conf{};
timer_conf.speed_mode = speed_mode;
timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bit_depth_);
timer_conf.timer_num = timer_num;
timer_conf.freq_hz = (uint32_t) frequency_;
timer_conf.clk_cfg = LEDC_AUTO_CLK;
ledc_timer_config(&timer_conf);
ledc_channel_config_t chan_conf{};
chan_conf.gpio_num = pin_->get_pin();
chan_conf.speed_mode = speed_mode;
chan_conf.channel = chan_num;
chan_conf.intr_type = LEDC_INTR_DISABLE;
chan_conf.timer_sel = timer_num;
chan_conf.duty = inverted_ == pin_->is_inverted() ? 0 : (1U << bit_depth_);
chan_conf.hpoint = 0;
ledc_channel_config(&chan_conf);
#endif
}
void LEDCOutput::dump_config() {
ESP_LOGCONFIG(TAG, "LEDC Output:");
LOG_PIN(" Pin ", this->pin_);
ESP_LOGCONFIG(TAG, " LEDC Channel: %u", this->channel_);
ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
}
void LEDCOutput::update_frequency(float frequency) {
auto bit_depth_opt = ledc_bit_depth_for_frequency(frequency);
if (!bit_depth_opt.has_value()) {
@@ -58,7 +99,21 @@ void LEDCOutput::update_frequency(float frequency) {
}
this->bit_depth_ = bit_depth_opt.value_or(8);
this->frequency_ = frequency;
#ifdef USE_ARDUINO
ledcSetup(this->channel_, frequency, this->bit_depth_);
#endif // USE_ARDUINO
#ifdef USE_ESP_IDF
auto speed_mode = channel_ < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE;
auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
ledc_timer_config_t timer_conf{};
timer_conf.speed_mode = speed_mode;
timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bit_depth_);
timer_conf.timer_num = timer_num;
timer_conf.freq_hz = (uint32_t) frequency_;
timer_conf.clk_cfg = LEDC_AUTO_CLK;
ledc_timer_config(&timer_conf);
#endif
// re-apply duty
this->write_state(this->duty_);
}

View File

@@ -1,11 +1,11 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/core/hal.h"
#include "esphome/core/automation.h"
#include "esphome/components/output/float_output.h"
#ifdef ARDUINO_ARCH_ESP32
#ifdef USE_ESP32
namespace esphome {
namespace ledc {
@@ -14,7 +14,7 @@ extern uint8_t next_ledc_channel;
class LEDCOutput : public output::FloatOutput, public Component {
public:
explicit LEDCOutput(GPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
explicit LEDCOutput(InternalGPIOPin *pin) : pin_(pin) { this->channel_ = next_ledc_channel++; }
void set_channel(uint8_t channel) { this->channel_ = channel; }
void set_frequency(float frequency) { this->frequency_ = frequency; }
@@ -31,7 +31,7 @@ class LEDCOutput : public output::FloatOutput, public Component {
void write_state(float state) override;
protected:
GPIOPin *pin_;
InternalGPIOPin *pin_;
uint8_t channel_{};
uint8_t bit_depth_{};
float frequency_{};

View File

@@ -7,10 +7,9 @@ from esphome.const import (
CONF_FREQUENCY,
CONF_ID,
CONF_PIN,
ESP_PLATFORM_ESP32,
)
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
DEPENDENCIES = ["esp32"]
def calc_max_frequency(bit_depth):