From 48f5e8f397565140581d7b3f03bc57cc10d06c9d Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 26 Jun 2023 18:30:09 +0200 Subject: [PATCH] PWM --- main/Alim1000W.c | 17 ++++++++++++++--- main/BLE.c | 2 +- main/CMakeLists.txt | 2 +- main/PWM.c | 27 +++++++++++++++++++++++++++ main/PWM.h | 1 + sdkconfig | 4 ++-- sdkconfig.old | 3 ++- 7 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 main/PWM.c create mode 100644 main/PWM.h diff --git a/main/Alim1000W.c b/main/Alim1000W.c index e2ab70b..b385e80 100644 --- a/main/Alim1000W.c +++ b/main/Alim1000W.c @@ -1,7 +1,9 @@ #include #include +#include #include "BLEh.h" +#include "driver/ledc.h" #include "esp_err.h" #include "esp_log.h" @@ -9,6 +11,7 @@ #include "freertos/projdefs.h" #include "freertos/task.h" #include "freertos/portmacro.h" +#include "hal/ledc_types.h" #include "host/ble_uuid.h" #include "nvs_flash.h" @@ -17,11 +20,12 @@ #include "BLE.h" #include "measure.h" +#include "PWM.h" #define TAG "Main" static uint32_t switching_frequency = CONFIG_SW_FREQ; -static uint32_t duty_cycle = 50000000; +static uint32_t duty_cycle = 0; static uint8_t adc_pins[] = {CONFIG_OUT_VOLT_PIN, CONFIG_OUT_CURR_PIN, CONFIG_PHASE1_CURR_PIN, CONFIG_PHASE2_CURR_PIN, CONFIG_PHASE3_CURR_PIN}; @@ -54,12 +58,17 @@ int on_char_write_handler(int svc_ind, int chr_ind, struct os_mbuf* os){ ; rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL); switching_frequency = val; - // TODO update running val + ESP_ERROR_CHECK(ledc_set_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0, switching_frequency)); break; case DUTY_CYCLE_CHR_ID: rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL); duty_cycle = val; - // TODO update running val + + uint32_t max_value = 1 << CONFIG_SW_RES; + uint32_t raw_value = max_value * duty_cycle / 100000; + ESP_LOGI(TAG, "setting duty cycle %lu/%lu", raw_value, max_value); + ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, raw_value)); + ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0)); break; } break; @@ -72,6 +81,8 @@ void app_main(void){ init_adc(adc_pins); init_adc_cali(); adc_start(); + + init_pwm(); nvs_flash_init(); diff --git a/main/BLE.c b/main/BLE.c index e9d674e..dc493be 100644 --- a/main/BLE.c +++ b/main/BLE.c @@ -32,7 +32,7 @@ struct dsc_content switching_frequency_dsc = { struct char_pres_format duty_cycle_char_pres_format = { .format = FORMAT_UINT32, .unit = UNITLESS_UNIT_UUID, - .exponent = -6, + .exponent = -3, .namespc = 1, .descrH = NSP_DESC_MAIN & 0xff, .descrL = (NSP_DESC_MAIN>>8) & 0xff, diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 47a7b82..7cba32d 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,4 +1,4 @@ idf_component_register( - SRCS "Alim1000W.c" "BLE.c" "measure.c" + SRCS "Alim1000W.c" "BLE.c" "measure.c" "PWM.c" INCLUDE_DIRS "." ) diff --git a/main/PWM.c b/main/PWM.c new file mode 100644 index 0000000..bda6269 --- /dev/null +++ b/main/PWM.c @@ -0,0 +1,27 @@ +#include "driver/ledc.h" +#include "hal/ledc_types.h" + +#include "sdkconfig.h" + +void init_pwm(){ + ledc_timer_config_t ledc_timer = { + .speed_mode = LEDC_LOW_SPEED_MODE, + .timer_num = LEDC_TIMER_0, + .duty_resolution = CONFIG_SW_RES, + .freq_hz = CONFIG_SW_FREQ, + .clk_cfg = LEDC_AUTO_CLK, + }; + + ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer)); + + ledc_channel_config_t ledc_channel = { + .speed_mode = LEDC_LOW_SPEED_MODE, + .channel = 0, + .timer_sel = LEDC_TIMER_0, + .intr_type = LEDC_INTR_DISABLE, + .gpio_num = CONFIG_PHASE1_CON_PIN, + .duty = 0, + .hpoint = 0, + }; + ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel)); +} \ No newline at end of file diff --git a/main/PWM.h b/main/PWM.h new file mode 100644 index 0000000..cee6f58 --- /dev/null +++ b/main/PWM.h @@ -0,0 +1 @@ +void init_pwm(); diff --git a/sdkconfig b/sdkconfig index cc86b8b..161d635 100644 --- a/sdkconfig +++ b/sdkconfig @@ -1659,8 +1659,8 @@ CONFIG_PHASE3_CURR_PIN=4 # # Phases control signal pins # -CONFIG_PHASE1_CON_PIN=6 -CONFIG_PHASE2_CON_PIN=7 +CONFIG_PHASE1_CON_PIN=7 +CONFIG_PHASE2_CON_PIN=6 CONFIG_PHASE3_CON_PIN=8 # end of Phases control signal pins # end of Pins definition diff --git a/sdkconfig.old b/sdkconfig.old index 6916e95..d489008 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -1641,6 +1641,7 @@ CONFIG_WPA_MBEDTLS_TLS_CLIENT=y # # Pins definition # +CONFIG_ADC_IN_PIN_NUM=5 CONFIG_OUT_VOLT_PIN=2 CONFIG_OUT_OVERVOLT_PIN=5 CONFIG_OUT_EN_PIN=9 @@ -1669,7 +1670,7 @@ CONFIG_PHASE3_CON_PIN=8 # CONFIG_SW_FREQ=800000 CONFIG_SW_RES=6 -CONFIG_UPD_DELAY=100 +CONFIG_UPD_DELAY=1000 # end of Controller parameters # end of This project config # end of Component config