diff --git a/dependencies.lock b/dependencies.lock index aec1905..4d8d5e2 100755 --- a/dependencies.lock +++ b/dependencies.lock @@ -2,7 +2,7 @@ dependencies: idf: source: type: idf - version: 5.3.1 + version: 5.3.0 manifest_hash: 482e2222071e855d99a96f5a61a37a4f589f24e7994d6610de1e65027e5a15a9 target: esp32 version: 2.0.0 diff --git a/main/app_main.c b/main/app_main.c index d16dc51..fee121d 100755 --- a/main/app_main.c +++ b/main/app_main.c @@ -39,16 +39,20 @@ void app_main(void) bool RFE_Pulled = false; uint16_t menu_counter = 0; float duty = (float)CONFIG_DUTY_PWM; + duty = 75.0; char display_message[50]; // Puffer für die Nachricht bool enc_but_state = false; bool in_menu = false; + uint16_t mcpwm_freq = CONFIG_FREQ_PWM; configure_GPIO_dir(); SSD1306_t *dev_pt = configure_OLED(); mcpwm_init(); - set_mcpwm_output(PHASE_U,PHASE_V,duty); + set_mcpwm_output(PHASE_U,PHASE_V); set_enc_in_counter(menu_counter); - + mcpwm_freq = 40000; + set_mcpwm_duty(duty); + set_mcpwm_frequenzy(mcpwm_freq); //gpio_set_level(CONFIG_HIN_V_GPIO, 1); while (1) { @@ -100,10 +104,10 @@ void app_main(void) snprintf(display_message, sizeof(display_message), "PWM-Param."); ssd1306_display_text(dev_pt, 1, display_message, strlen(display_message), false); - snprintf(display_message, sizeof(display_message), "PWMFreq.: %ik ", (CONFIG_FREQ_PWM/1000)); + snprintf(display_message, sizeof(display_message), "PWMFreq.: %ik ", (mcpwm_freq/1000)); ssd1306_display_text(dev_pt, 3, display_message, 14, !(menu_counter)); - snprintf(display_message, sizeof(display_message), "Duty: %.1f%% ", duty); + snprintf(display_message, sizeof(display_message), "Duty: %.1f%% ", get_duty()); ssd1306_display_text(dev_pt, 4, display_message, 14, !(menu_counter-1)); snprintf(display_message, sizeof(display_message), "DeadTime: %i ", CONFIG_DEAD_TIME_PWM); diff --git a/main/include/mcpwm.h b/main/include/mcpwm.h index 844c7df..7b843ce 100755 --- a/main/include/mcpwm.h +++ b/main/include/mcpwm.h @@ -9,7 +9,9 @@ typedef enum { } Phase; void mcpwm_init(); -void set_mcpwm_output(Phase highside, Phase lowside, float Duty); -void set_mcpwm_duty(float Duty); +esp_err_t set_mcpwm_output(Phase highside, Phase lowside); +esp_err_t set_mcpwm_duty(float duty); +esp_err_t set_mcpwm_frequenzy(uint16_t frequenzy); void get_comps(mcpwm_cmpr_handle_t comps[3]); +float get_duty(); #endif \ No newline at end of file diff --git a/main/mcpwm.c b/main/mcpwm.c index 927b8af..c5b9fc7 100755 --- a/main/mcpwm.c +++ b/main/mcpwm.c @@ -28,6 +28,7 @@ static mcpwm_gen_handle_t generator_W_LIN = NULL; static Phase HighsidePhase; static Phase LowsidePhase; uint32_t periode_ticks = CONFIG_TIMER_BASE_FREQ/CONFIG_FREQ_PWM; +float duty = CONFIG_DUTY_PWM; /*############################################*/ /*############### MCPWM-Setup ################*/ /*############################################*/ @@ -45,7 +46,7 @@ void mcpwm_init(){ .clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT, .resolution_hz = 40000000, //40MHz .period_ticks = periode_ticks, //40MHz/2KHz = 20KHz - .count_mode = MCPWM_TIMER_COUNT_MODE_UP, + .count_mode = MCPWM_TIMER_COUNT_MODE_UP_DOWN, }; ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer_U)); ESP_ERROR_CHECK(mcpwm_new_timer(&timer_config, &timer_V)); @@ -72,14 +73,14 @@ void mcpwm_init(){ mcpwm_timer_sync_phase_config_t sync_phase_V_config = { .sync_src = sync_signal, - .count_value = periode_ticks/3, //120 degree delayed + .count_value = periode_ticks/6, //120 degree delayed }; ESP_ERROR_CHECK(mcpwm_timer_set_phase_on_sync(timer_V,&sync_phase_V_config)); //set Timer_W as an Slave of Timer_U with another phase mcpwm_timer_sync_phase_config_t sync_phase_W_config = { .sync_src = sync_signal, - .count_value = periode_ticks*2/3, //240 degree delayed + .count_value = periode_ticks*2/6, //240 degree delayed }; ESP_ERROR_CHECK(mcpwm_timer_set_phase_on_sync(timer_W,&sync_phase_W_config)); @@ -105,13 +106,8 @@ void mcpwm_init(){ .flags.update_cmp_on_tez = true, }; ESP_ERROR_CHECK(mcpwm_new_comparator(operator_U, &comparator_config,&comperator_U)); - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_U, periode_ticks*CONFIG_DUTY_PWM/100));//Duty_cycle from Config - ESP_ERROR_CHECK(mcpwm_new_comparator(operator_V, &comparator_config,&comperator_V)); - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_V, periode_ticks*CONFIG_DUTY_PWM/100)); - ESP_ERROR_CHECK(mcpwm_new_comparator(operator_W, &comparator_config,&comperator_W)); - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_W,periode_ticks*CONFIG_DUTY_PWM/100)); mcpwm_gen_handle_t *mcpwm_gens[] ={&generator_U_HIN,&generator_U_LIN,&generator_V_HIN,&generator_V_LIN,&generator_W_HIN,&generator_W_LIN}; @@ -181,24 +177,28 @@ static void set_gen(Phase phase){ switch (phase) { case PHASE_U: - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_U_HIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_U_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_U, MCPWM_GEN_ACTION_LOW))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_U_LIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_U_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_U, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_U_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_U, MCPWM_GEN_ACTION_LOW))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_U_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_U, MCPWM_GEN_ACTION_HIGH))); + + break; case PHASE_V: - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_V_HIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_V_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_V, MCPWM_GEN_ACTION_LOW))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_V_LIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_V_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_V, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_V_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_V, MCPWM_GEN_ACTION_LOW))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_V_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_V, MCPWM_GEN_ACTION_HIGH))); + + break; case PHASE_W: - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_W_HIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_W_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_W, MCPWM_GEN_ACTION_LOW))); - ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(generator_W_LIN, MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_W_HIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_W, MCPWM_GEN_ACTION_HIGH))); ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_W_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, comperator_W, MCPWM_GEN_ACTION_LOW))); + ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(generator_W_LIN, MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_DOWN, comperator_W, MCPWM_GEN_ACTION_HIGH))); break; default: @@ -224,18 +224,18 @@ static void set_lowside(Phase lowside){ break; } } -static void set_highside(Phase highside, float Duty){ +static void set_highside(Phase highside){ HighsidePhase = highside; switch (highside){ case PHASE_U: - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_U, periode_ticks*Duty/100)); + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_U, (periode_ticks*duty/100)/2)); break; case PHASE_V: - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_V, periode_ticks*Duty/100)); + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_V, (periode_ticks*duty/100)/2)); break; case PHASE_W: - ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_W, periode_ticks*Duty/100)); + ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_W, (periode_ticks*duty/100)/2)); break; default: @@ -245,19 +245,60 @@ static void set_highside(Phase highside, float Duty){ } -void set_mcpwm_output(Phase highside, Phase lowside, float Duty){ - set_highside(highside, Duty); +esp_err_t set_mcpwm_output(Phase highside, Phase lowside){ + if (timer_U == NULL) { + return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde + } + set_highside(highside); set_gen(highside); set_lowside(lowside); set_gen(lowside); + return ESP_OK; } -void set_mcpwm_duty(float Duty){ - set_highside(HighsidePhase, Duty); +esp_err_t set_mcpwm_duty(float new_duty){ + if (timer_U == NULL) { + return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde + } + duty = new_duty; + set_highside(HighsidePhase); + return ESP_OK; +} + +esp_err_t set_mcpwm_frequenzy(uint16_t frequency){ + + if (timer_U == NULL) { + return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde + } + periode_ticks = CONFIG_TIMER_BASE_FREQ/frequency; + + // Timer stoppen, wenn er läuft + ESP_ERROR_CHECK(mcpwm_timer_disable(timer_U)); + ESP_ERROR_CHECK(mcpwm_timer_disable(timer_V)); + ESP_ERROR_CHECK(mcpwm_timer_disable(timer_W)); + + // Neue Konfiguration anwenden + + ESP_ERROR_CHECK(mcpwm_timer_set_period(timer_U, periode_ticks)); + ESP_ERROR_CHECK(mcpwm_timer_set_period(timer_V, periode_ticks)); + ESP_ERROR_CHECK(mcpwm_timer_set_period(timer_W, periode_ticks)); + + // dutycycle an neue Frequenz anpassen + set_mcpwm_duty(duty); + + // Timer wieder starten + ESP_ERROR_CHECK(mcpwm_timer_enable(timer_U)); + ESP_ERROR_CHECK(mcpwm_timer_enable(timer_V)); + ESP_ERROR_CHECK(mcpwm_timer_enable(timer_W)); + + return ESP_OK; } void get_comps(mcpwm_cmpr_handle_t comps[3]) { comps[0] = comperator_U; comps[1] = comperator_V; comps[2] = comperator_W; +} +float get_duty() { + return duty; } \ No newline at end of file diff --git a/sdkconfig b/sdkconfig index dc33583..04d037b 100755 --- a/sdkconfig +++ b/sdkconfig @@ -1,6 +1,6 @@ # # Automatically generated file. DO NOT EDIT. -# Espressif IoT Development Framework (ESP-IDF) 5.3.1 Project Configuration +# Espressif IoT Development Framework (ESP-IDF) 5.3.0 Project Configuration # CONFIG_SOC_BROWNOUT_RESET_SUPPORTED="Not determined" CONFIG_SOC_TWAI_BRP_DIV_SUPPORTED="Not determined" @@ -174,7 +174,7 @@ CONFIG_SOC_TIMER_GROUP_TOTAL_TIMERS=4 CONFIG_SOC_TIMER_GROUP_SUPPORT_APB=y CONFIG_SOC_TOUCH_SENSOR_VERSION=1 CONFIG_SOC_TOUCH_SENSOR_NUM=10 -CONFIG_SOC_TOUCH_SAMPLE_CFG_NUM=1 +CONFIG_SOC_TOUCH_SAMPLER_NUM=1 CONFIG_SOC_TWAI_CONTROLLER_NUM=1 CONFIG_SOC_TWAI_BRP_MIN=2 CONFIG_SOC_TWAI_CLK_SUPPORT_APB=y @@ -733,14 +733,6 @@ CONFIG_SPI_MASTER_ISR_IN_IRAM=y CONFIG_SPI_SLAVE_ISR_IN_IRAM=y # end of ESP-Driver:SPI Configurations -# -# ESP-Driver:Touch Sensor Configurations -# -# CONFIG_TOUCH_CTRL_FUNC_IN_IRAM is not set -# CONFIG_TOUCH_ISR_IRAM_SAFE is not set -# CONFIG_TOUCH_ENABLE_DEBUG_LOG is not set -# end of ESP-Driver:Touch Sensor Configurations - # # ESP-Driver:UART Configurations #