#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/queue.h" #include "driver/ledc.h" #include "driver/pcnt.h" #include "esp_attr.h" #include "esp_log.h" #include "config.h" #ifdef CCFG_GEN_PULSE /* Configure LED PWM Controller * to output sample pulses at 1 Hz with duty of about 10% */ void ledc_init(void) { // Prepare and then apply the LEDC PWM timer configuration ledc_timer_config_t ledc_timer; ledc_timer.speed_mode = LEDC_LOW_SPEED_MODE; ledc_timer.timer_num = LEDC_TIMER_0; ledc_timer.duty_resolution = LEDC_TIMER_13_BIT; ledc_timer.freq_hz = 100; // Should be 2 for some kind of max kWh ledc_timer.clk_cfg = LEDC_AUTO_CLK; ledc_timer_config(&ledc_timer); // Prepare and then apply the LEDC PWM channel configuration ledc_channel_config_t ledc_channel; ledc_channel.speed_mode = LEDC_LOW_SPEED_MODE; ledc_channel.channel = LEDC_CHANNEL_0; ledc_channel.timer_sel = LEDC_TIMER_0; ledc_channel.intr_type = LEDC_INTR_DISABLE; ledc_channel.gpio_num = LEDC_OUTPUT_IO; ledc_channel.duty = 0; // set duty at about 10% ledc_channel.hpoint = 0; ledc_channel_config(&ledc_channel); // Set duty to 5% ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 409)); // Set duty to 5%. ((2 ** 13) - 1) * 50% = 409 // Update duty to apply the new value ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); } #endif