9 #include <hardware/clocks.h> 10 #include <hardware/gpio.h> 11 #include <hardware/pwm.h> 15 namespace rp2040_pwm {
17 static const char *
const TAG =
"rp2040_pwm";
20 ESP_LOGCONFIG(TAG,
"Setting up RP2040 PWM Output...");
26 pwm_config config = pwm_get_default_config();
28 uint32_t clock = clock_get_hz(clk_sys);
29 float divider = ceil(clock / (4096 * this->
frequency_)) / 16.0f;
33 uint16_t wrap = clock / divider / this->
frequency_ - 1;
35 ESP_LOGD(TAG,
"divider=%.5f, wrap=%d, clock=%d", divider, wrap, clock);
37 pwm_config_set_clkdiv(&config, divider);
38 pwm_config_set_wrap(&config, wrap);
39 pwm_init(pwm_gpio_to_slice_num(this->
pin_->
get_pin()), &config,
true);
43 ESP_LOGCONFIG(TAG,
"RP2040 PWM:");
44 LOG_PIN(
" Pin: ", this->
pin_);
45 ESP_LOGCONFIG(TAG,
" Frequency: %.1f Hz", this->
frequency_);
46 LOG_FLOAT_OUTPUT(
this);
61 gpio_set_function(this->
pin_->
get_pin(), GPIO_FUNC_PWM);
void dump_config() override
void setup() override
Initialize pin.
virtual uint8_t get_pin() const =0
void write_state(float state) override
Implementation of SPI Controller mode.
float last_output_
Cache last output level for dynamic frequency updating.
virtual bool is_inverted() const =0