mirror of
https://github.com/esphome/esphome.git
synced 2025-09-15 17:52:19 +01:00
Initial Support for RP2040 platform (#3284)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
0
esphome/components/rp2040_pwm/__init__.py
Normal file
0
esphome/components/rp2040_pwm/__init__.py
Normal file
55
esphome/components/rp2040_pwm/output.py
Normal file
55
esphome/components/rp2040_pwm/output.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from esphome import pins, automation
|
||||
from esphome.components import output
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.const import (
|
||||
CONF_FREQUENCY,
|
||||
CONF_ID,
|
||||
CONF_PIN,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
DEPENDENCIES = ["rp2040"]
|
||||
|
||||
|
||||
rp2040_pwm_ns = cg.esphome_ns.namespace("rp2040_pwm")
|
||||
RP2040PWM = rp2040_pwm_ns.class_("RP2040PWM", output.FloatOutput, cg.Component)
|
||||
SetFrequencyAction = rp2040_pwm_ns.class_("SetFrequencyAction", automation.Action)
|
||||
validate_frequency = cv.All(cv.frequency, cv.Range(min=1.0e-6))
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(RP2040PWM),
|
||||
cv.Required(CONF_PIN): pins.internal_gpio_output_pin_schema,
|
||||
cv.Optional(CONF_FREQUENCY, default="1kHz"): validate_frequency,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await output.register_output(var, config)
|
||||
|
||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||
cg.add(var.set_pin(pin))
|
||||
|
||||
cg.add(var.set_frequency(config[CONF_FREQUENCY]))
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"output.rp2040_pwm.set_frequency",
|
||||
SetFrequencyAction,
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(RP2040PWM),
|
||||
cv.Required(CONF_FREQUENCY): cv.templatable(validate_frequency),
|
||||
}
|
||||
),
|
||||
)
|
||||
async def rp2040_set_frequency_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = await cg.templatable(config[CONF_FREQUENCY], args, float)
|
||||
cg.add(var.set_frequency(template_))
|
||||
return var
|
45
esphome/components/rp2040_pwm/rp2040_pwm.cpp
Normal file
45
esphome/components/rp2040_pwm/rp2040_pwm.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifdef USE_RP2040
|
||||
|
||||
#include "rp2040_pwm.h"
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/macros.h"
|
||||
|
||||
#include <PinNames.h>
|
||||
|
||||
namespace esphome {
|
||||
namespace rp2040_pwm {
|
||||
|
||||
static const char *const TAG = "rp2040_pwm";
|
||||
|
||||
void RP2040PWM::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up RP2040 PWM Output...");
|
||||
this->pin_->setup();
|
||||
this->pwm_ = new mbed::PwmOut((PinName) this->pin_->get_pin());
|
||||
this->turn_off();
|
||||
}
|
||||
void RP2040PWM::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "RP2040 PWM:");
|
||||
LOG_PIN(" Pin: ", this->pin_);
|
||||
ESP_LOGCONFIG(TAG, " Frequency: %.1f Hz", this->frequency_);
|
||||
LOG_FLOAT_OUTPUT(this);
|
||||
}
|
||||
void HOT RP2040PWM::write_state(float state) {
|
||||
this->last_output_ = state;
|
||||
|
||||
// Also check pin inversion
|
||||
if (this->pin_->is_inverted()) {
|
||||
state = 1.0f - state;
|
||||
}
|
||||
|
||||
auto total_time_us = static_cast<uint32_t>(roundf(1e6f / this->frequency_));
|
||||
|
||||
this->pwm_->period_us(total_time_us);
|
||||
this->pwm_->write(state);
|
||||
}
|
||||
|
||||
} // namespace rp2040_pwm
|
||||
} // namespace esphome
|
||||
|
||||
#endif
|
57
esphome/components/rp2040_pwm/rp2040_pwm.h
Normal file
57
esphome/components/rp2040_pwm/rp2040_pwm.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_RP2040
|
||||
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#include "drivers/PwmOut.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace rp2040_pwm {
|
||||
|
||||
class RP2040PWM : public output::FloatOutput, public Component {
|
||||
public:
|
||||
void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
|
||||
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
||||
/// Dynamically update frequency
|
||||
void update_frequency(float frequency) override {
|
||||
this->set_frequency(frequency);
|
||||
this->write_state(this->last_output_);
|
||||
}
|
||||
|
||||
/// Initialize pin
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
/// HARDWARE setup_priority
|
||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||
|
||||
protected:
|
||||
void write_state(float state) override;
|
||||
|
||||
InternalGPIOPin *pin_;
|
||||
mbed::PwmOut *pwm_;
|
||||
float frequency_{1000.0};
|
||||
/// Cache last output level for dynamic frequency updating
|
||||
float last_output_{0.0};
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
||||
public:
|
||||
SetFrequencyAction(RP2040PWM *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(Ts... x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
RP2040PWM *parent_;
|
||||
};
|
||||
|
||||
} // namespace rp2040_pwm
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_RP2040
|
Reference in New Issue
Block a user