first commit

This commit is contained in:
2025-12-10 15:26:47 +02:00
commit c767b541be
6 changed files with 4663 additions and 0 deletions

2
main/CMakeLists.txt Normal file
View File

@@ -0,0 +1,2 @@
idf_component_register(SRCS "esp-pwm.c"
INCLUDE_DIRS ".")

55
main/esp-pwm.c Normal file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include <math.h>
#include "driver/ledc.h"
#include "hal/ledc_types.h"
#include "soc/clk_tree_defs.h"
#include "esp_clk_tree.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
const char* TAG = "MAIN_CPP";
void app_main() {
ESP_LOGI(TAG, "BOOT\n");
const uint32_t timer_freq = 2731;
uint32_t clk_freq = 0;
ESP_ERROR_CHECK(esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_RC_FAST, ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT, &clk_freq));
uint32_t duty_resolution = ledc_find_suitable_duty_resolution(clk_freq, timer_freq);
if (duty_resolution == 0) {
ESP_LOGE(TAG, "Could not determine optimal duty resolution.\n");
return;
}
ledc_timer_config_t ledc_config = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.freq_hz = timer_freq,
.duty_resolution = duty_resolution,
.clk_cfg = LEDC_USE_RC_FAST_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_config));
ledc_channel_config_t ledc_ch_config = {
.gpio_num = 9,
.speed_mode = LEDC_LOW_SPEED_MODE,
.channel = LEDC_CHANNEL_0,
.timer_sel = LEDC_TIMER_0,
.duty = (uint32_t) (pow(2, duty_resolution) / 2),
// .hpoint = ((uint32_t) pow(2, duty_resolution)) - 1,
.hpoint = 0,
.sleep_mode = LEDC_SLEEP_MODE_KEEP_ALIVE,
.flags.output_invert = false
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_ch_config));
while (true) {
vTaskDelay(10000);
}
}