mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 15:38:11 +01:00
Fix rp2040 pwm to use pico-sdk, not mbed (#4059)
This commit is contained in:
parent
f7096ab78e
commit
5c2c1560bb
2 changed files with 24 additions and 10 deletions
|
@ -6,7 +6,9 @@
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/core/macros.h"
|
#include "esphome/core/macros.h"
|
||||||
|
|
||||||
#include <PinNames.h>
|
#include <hardware/clocks.h>
|
||||||
|
#include <hardware/gpio.h>
|
||||||
|
#include <hardware/pwm.h>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace rp2040_pwm {
|
namespace rp2040_pwm {
|
||||||
|
@ -15,10 +17,17 @@ static const char *const TAG = "rp2040_pwm";
|
||||||
|
|
||||||
void RP2040PWM::setup() {
|
void RP2040PWM::setup() {
|
||||||
ESP_LOGCONFIG(TAG, "Setting up RP2040 PWM Output...");
|
ESP_LOGCONFIG(TAG, "Setting up RP2040 PWM Output...");
|
||||||
this->pin_->setup();
|
|
||||||
this->pwm_ = new mbed::PwmOut((PinName) this->pin_->get_pin());
|
this->setup_pwm_();
|
||||||
this->turn_off();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RP2040PWM::setup_pwm_() {
|
||||||
|
pwm_config config = pwm_get_default_config();
|
||||||
|
pwm_config_set_clkdiv(&config, clock_get_hz(clk_sys) / (255.0f * this->frequency_));
|
||||||
|
pwm_config_set_wrap(&config, 254);
|
||||||
|
pwm_init(pwm_gpio_to_slice_num(this->pin_->get_pin()), &config, true);
|
||||||
|
}
|
||||||
|
|
||||||
void RP2040PWM::dump_config() {
|
void RP2040PWM::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "RP2040 PWM:");
|
ESP_LOGCONFIG(TAG, "RP2040 PWM:");
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
LOG_PIN(" Pin: ", this->pin_);
|
||||||
|
@ -33,10 +42,13 @@ void HOT RP2040PWM::write_state(float state) {
|
||||||
state = 1.0f - state;
|
state = 1.0f - state;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto total_time_us = static_cast<uint32_t>(roundf(1e6f / this->frequency_));
|
if (frequency_changed_) {
|
||||||
|
this->setup_pwm_();
|
||||||
|
frequency_changed_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
this->pwm_->period_us(total_time_us);
|
gpio_set_function(this->pin_->get_pin(), GPIO_FUNC_PWM);
|
||||||
this->pwm_->write(state);
|
pwm_set_gpio_level(this->pin_->get_pin(), state * 255.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace rp2040_pwm
|
} // namespace rp2040_pwm
|
||||||
|
|
|
@ -7,11 +7,11 @@
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/hal.h"
|
#include "esphome/core/hal.h"
|
||||||
|
|
||||||
#include "drivers/PwmOut.h"
|
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace rp2040_pwm {
|
namespace rp2040_pwm {
|
||||||
|
|
||||||
|
static bool frequency_changed_ = false;
|
||||||
|
|
||||||
class RP2040PWM : public output::FloatOutput, public Component {
|
class RP2040PWM : public output::FloatOutput, public Component {
|
||||||
public:
|
public:
|
||||||
void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
|
void set_pin(InternalGPIOPin *pin) { pin_ = pin; }
|
||||||
|
@ -19,6 +19,7 @@ class RP2040PWM : public output::FloatOutput, public Component {
|
||||||
/// Dynamically update frequency
|
/// Dynamically update frequency
|
||||||
void update_frequency(float frequency) override {
|
void update_frequency(float frequency) override {
|
||||||
this->set_frequency(frequency);
|
this->set_frequency(frequency);
|
||||||
|
frequency_changed_ = true;
|
||||||
this->write_state(this->last_output_);
|
this->write_state(this->last_output_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,8 +32,9 @@ class RP2040PWM : public output::FloatOutput, public Component {
|
||||||
protected:
|
protected:
|
||||||
void write_state(float state) override;
|
void write_state(float state) override;
|
||||||
|
|
||||||
|
void setup_pwm_();
|
||||||
|
|
||||||
InternalGPIOPin *pin_;
|
InternalGPIOPin *pin_;
|
||||||
mbed::PwmOut *pwm_;
|
|
||||||
float frequency_{1000.0};
|
float frequency_{1000.0};
|
||||||
/// Cache last output level for dynamic frequency updating
|
/// Cache last output level for dynamic frequency updating
|
||||||
float last_output_{0.0};
|
float last_output_{0.0};
|
||||||
|
|
Loading…
Reference in a new issue