Mainmenu done; confog. Menu started
This commit is contained in:
27
main/GPIO.c
27
main/GPIO.c
@@ -6,7 +6,8 @@
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
#include "driver/uart.h"
|
||||
#include "soc/io_mux_reg.h"
|
||||
//external Encoder
|
||||
static void IRAM_ATTR index_isr_handler(void *arg);
|
||||
static void IRAM_ATTR enc_ab_isr_handler(void *arg);
|
||||
@@ -34,7 +35,13 @@ static volatile uint8_t last_state = 0;
|
||||
void configure_GPIO_dir()
|
||||
{
|
||||
/* reset every used GPIO-pin */
|
||||
|
||||
uart_driver_delete(UART_NUM_0);
|
||||
// GPIO1 als GPIO konfigurieren (anstatt als UART0 TX)
|
||||
PIN_FUNC_SELECT(IO_MUX_GPIO1_REG, PIN_FUNC_GPIO);
|
||||
|
||||
// GPIO3 als GPIO konfigurieren (anstatt als UART0 RX)
|
||||
PIN_FUNC_SELECT(IO_MUX_GPIO3_REG, PIN_FUNC_GPIO);
|
||||
|
||||
gpio_reset_pin(CONFIG_HALL_A_GPIO);
|
||||
gpio_reset_pin(CONFIG_HALL_B_GPIO);
|
||||
gpio_reset_pin(CONFIG_HALL_C_GPIO);
|
||||
@@ -43,7 +50,7 @@ void configure_GPIO_dir()
|
||||
gpio_reset_pin(CONFIG_IN_ENC_B_GPIO);
|
||||
gpio_reset_pin(CONFIG_IN_ENC_BUT_GPIO);
|
||||
|
||||
|
||||
|
||||
gpio_reset_pin(CONFIG_EXT_ENC_LEFT_GPIO);
|
||||
gpio_reset_pin(CONFIG_EXT_ENC_RIGHT_GPIO);
|
||||
|
||||
@@ -58,16 +65,17 @@ void configure_GPIO_dir()
|
||||
|
||||
/* Set the GPIO as a push/pull output*/
|
||||
|
||||
|
||||
gpio_set_direction(CONFIG_HALL_A_GPIO, GPIO_MODE_INPUT);
|
||||
gpio_set_direction(CONFIG_HALL_B_GPIO, GPIO_MODE_INPUT);
|
||||
gpio_set_direction(CONFIG_HALL_C_GPIO, GPIO_MODE_INPUT);
|
||||
|
||||
gpio_set_direction(CONFIG_IN_ENC_A_GPIO, GPIO_MODE_INPUT);
|
||||
gpio_set_direction(CONFIG_IN_ENC_B_GPIO, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(CONFIG_IN_ENC_A_GPIO, GPIO_PULLUP_ENABLE);
|
||||
gpio_set_pull_mode(CONFIG_IN_ENC_B_GPIO, GPIO_PULLUP_ENABLE);
|
||||
gpio_set_direction(CONFIG_IN_ENC_BUT_GPIO, GPIO_MODE_INPUT);
|
||||
//gpio_set_direction(CONFIG_BUTTON_GPIO, GPIO_MODE_INPUT);
|
||||
|
||||
|
||||
|
||||
gpio_set_direction(CONFIG_EXT_ENC_LEFT_GPIO, GPIO_MODE_INPUT);
|
||||
gpio_set_direction(CONFIG_EXT_ENC_RIGHT_GPIO, GPIO_MODE_INPUT);
|
||||
@@ -94,9 +102,7 @@ void configure_GPIO_dir()
|
||||
gpio_install_isr_service(0);
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_EXT_ENC_INDX_GPIO, index_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_HALL_A_GPIO, enc_ab_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_A_GPIO, enc_in_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_B_GPIO, enc_in_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_BUT_GPIO, enc_in_but_isr_handler, NULL));
|
||||
|
||||
}
|
||||
|
||||
/*############################################*/
|
||||
@@ -158,8 +164,7 @@ int get_direction(){//-1=Error,0=right,1=left
|
||||
|
||||
/*############################################*/
|
||||
/*############ Internal Encoder ##############*/
|
||||
/*############################################*/
|
||||
|
||||
/*############################################*
|
||||
|
||||
static void IRAM_ATTR enc_in_isr_handler(void *arg) {
|
||||
static uint64_t last_interrupt_time = 0;
|
||||
@@ -216,4 +221,4 @@ bool get_enc_in_but(){
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
@@ -3,12 +3,12 @@ This is the first try of a Test-Software for the DIY Power PCB by Fabian Zaske
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "functions.h"
|
||||
|
||||
#include "GPIO.h"
|
||||
#include "mcpwm.h"
|
||||
#include "ADC.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "menu.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "string.h"
|
||||
@@ -46,17 +46,16 @@ void app_main(void)
|
||||
uint16_t mcpwm_freq = CONFIG_FREQ_PWM;
|
||||
|
||||
configure_GPIO_dir();
|
||||
SSD1306_t *dev_pt = configure_OLED();
|
||||
configure_OLED();
|
||||
config_internal_Encoder();
|
||||
mcpwm_init();
|
||||
set_mcpwm_output(PHASE_U, PHASE_V, PHASE_W);
|
||||
set_enc_in_counter(menu_counter);
|
||||
mcpwm_freq = 20000;
|
||||
set_mcpwm_duty(duty);
|
||||
set_mcpwm_frequency(mcpwm_freq);
|
||||
set_mcpwm_output(PHASE_U, PHASE_W, PHASE_V);
|
||||
|
||||
|
||||
|
||||
|
||||
//gpio_set_level(CONFIG_HIN_V_GPIO, 1);
|
||||
while (1) {
|
||||
menu_loop();
|
||||
//ssd1306_clear_screen(dev_pt, false);
|
||||
/* Die Anzeige der OLED mit der richtigen Nachricht
|
||||
Torque = get_torque();
|
||||
@@ -68,7 +67,7 @@ void app_main(void)
|
||||
/* Hall_A_On = get_Hall(CONFIG_HALL_A_GPIO);
|
||||
Hall_B_On = get_Hall(CONFIG_HALL_B_GPIO);
|
||||
Hall_C_On = get_Hall(CONFIG_HALL_C_GPIO);
|
||||
*/
|
||||
|
||||
|
||||
//Speed_indx = get_speed_index();
|
||||
//Speed_AB = get_speed_AB();
|
||||
@@ -103,7 +102,7 @@ void app_main(void)
|
||||
}
|
||||
ssd1306_display_text(dev_pt, 7, display_message, 14, !(menu_counter-3));
|
||||
|
||||
/* snprintf(display_message, sizeof(display_message), "Torque: %lu", Torque);
|
||||
snprintf(display_message, sizeof(display_message), "Torque: %lu", Torque);
|
||||
ssd1306_display_text(dev_pt, 2, display_message, 11, false);
|
||||
|
||||
snprintf(display_message, sizeof(display_message), "Voltage: %lu",Voltage_IN);
|
||||
@@ -120,9 +119,9 @@ void app_main(void)
|
||||
|
||||
snprintf(display_message, sizeof(display_message), "W: %ldmA",Current_W);
|
||||
ssd1306_display_text(dev_pt, 7, display_message, strlen(display_message), false);
|
||||
*///gpio_set_level(CONFIG_RFE_GPIO,0);
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS); // Verzögerung für die Task-Schleife
|
||||
///gpio_set_level(CONFIG_RFE_GPIO,0);
|
||||
*/
|
||||
//vTaskDelay(100 / portTICK_PERIOD_MS); // Verzögerung für die Task-Schleife
|
||||
//i++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/*############################################*/
|
||||
/*############## Display-Setup ###############*/
|
||||
/*############################################*/
|
||||
SSD1306_t *configure_OLED()
|
||||
SSD1306_t *configure_OLED_old()
|
||||
{
|
||||
static SSD1306_t dev;
|
||||
//int center, top, bottom;
|
||||
|
||||
@@ -12,8 +12,8 @@ float get_speed_index();
|
||||
float get_speed_AB();
|
||||
|
||||
//functions for internal Encoder
|
||||
int16_t get_enc_in_counter();
|
||||
void set_enc_in_counter(int16_t inital_value);
|
||||
bool get_enc_in_but();
|
||||
//int16_t get_enc_in_counter();
|
||||
//void set_enc_in_counter(int16_t inital_value);
|
||||
//bool get_enc_in_but();
|
||||
|
||||
#endif
|
||||
|
||||
@@ -8,6 +8,6 @@
|
||||
|
||||
|
||||
bool get_Hall(int HallSensorGPIO);
|
||||
SSD1306_t *configure_OLED();
|
||||
SSD1306_t *configure_OLED_old();
|
||||
|
||||
#endif
|
||||
@@ -8,10 +8,26 @@ typedef enum {
|
||||
PHASE_W
|
||||
} Phase;
|
||||
|
||||
typedef enum {
|
||||
OUT_U_V,
|
||||
OUT_U_W,
|
||||
OUT_V_W,
|
||||
OUT_V_U,
|
||||
OUT_W_U,
|
||||
OUT_W_V,
|
||||
OUT_U,
|
||||
OUT_V,
|
||||
OUT_W,
|
||||
COMBI_COUNT
|
||||
}OutCombis;
|
||||
|
||||
void mcpwm_init();
|
||||
esp_err_t set_mcpwm_output(Phase highside, Phase lowside, Phase inactive);
|
||||
void stop_mcpwm_output();
|
||||
void configure_mcpwm_output(OutCombis out_combi);
|
||||
esp_err_t start_mcpwm_output();
|
||||
esp_err_t set_mcpwm_duty(float duty);
|
||||
esp_err_t set_mcpwm_frequency(uint16_t frequency);
|
||||
void get_comps(mcpwm_cmpr_handle_t comps[3]);
|
||||
float get_duty();
|
||||
uint16_t get_frequency();
|
||||
#endif
|
||||
@@ -1,5 +1,13 @@
|
||||
//Zum Erstellen der gesamten Benutzeroberfläche. Samt einlesen des Inkrementalgebers, und Übergabe von Parametern an "Backend"
|
||||
#ifndef MENU_H
|
||||
#define MENU_H
|
||||
|
||||
|
||||
|
||||
extern const char *OutCombi_names[];
|
||||
|
||||
void configure_OLED();
|
||||
void config_internal_Encoder();
|
||||
void menu_loop();
|
||||
|
||||
#endif
|
||||
176
main/mcpwm.c
176
main/mcpwm.c
@@ -25,15 +25,69 @@ static mcpwm_gen_handle_t generator_U_LIN = NULL;
|
||||
static mcpwm_gen_handle_t generator_V_LIN = NULL;
|
||||
static mcpwm_gen_handle_t generator_W_LIN = NULL;
|
||||
|
||||
static Phase HighsidePhase;
|
||||
static Phase LowsidePhase;
|
||||
static Phase InactivePhase;
|
||||
typedef enum {
|
||||
Highside,
|
||||
Lowside,
|
||||
OFF
|
||||
} Phase_state;
|
||||
|
||||
typedef struct {
|
||||
Phase phase;
|
||||
Phase_state state;
|
||||
} PhaseConfiguration;
|
||||
PhaseConfiguration phase_configurations[3] = {
|
||||
{ PHASE_U, Highside },
|
||||
{ PHASE_V, Lowside },
|
||||
{ PHASE_W, OFF }
|
||||
};
|
||||
uint16_t mcpwm_frequency = CONFIG_FREQ_PWM;
|
||||
uint32_t periode_ticks = CONFIG_TIMER_BASE_FREQ/CONFIG_FREQ_PWM;
|
||||
float duty = CONFIG_DUTY_PWM;
|
||||
|
||||
/*############################################*/
|
||||
/*############### MCPWM-Setup ################*/
|
||||
/*############################################*/
|
||||
|
||||
static void conf_gens(){
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
}
|
||||
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;
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_U, (periode_ticks*duty/100)/2));
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_V, (periode_ticks*duty/100)/2));
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_W, (periode_ticks*duty/100)/2));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void stop_mcpwm_output(){
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_U_HIN, 0,true));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_U_LIN, 1,true));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_V_HIN, 0,true));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_V_LIN, 1,true));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_W_HIN, 0,true));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_W_LIN, 1,true));
|
||||
}
|
||||
void mcpwm_init(){
|
||||
ESP_LOGI("MCPWM","started");
|
||||
double tick_period_ns = 1e9 / CONFIG_TIMER_BASE_FREQ; // Zeit pro Tick in ns
|
||||
@@ -175,30 +229,12 @@ void mcpwm_init(){
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_dead_time(generator_V_HIN, generator_V_LIN, &deadtime_config));
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_dead_time(generator_W_HIN, generator_W_LIN, &deadtime_config));
|
||||
|
||||
conf_gens();
|
||||
stop_mcpwm_output();
|
||||
set_mcpwm_duty(duty);
|
||||
}
|
||||
static void conf_gens(){
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
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_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)));
|
||||
|
||||
|
||||
}
|
||||
static void set_lowside(Phase lowside){
|
||||
LowsidePhase = lowside;
|
||||
switch (lowside){
|
||||
case PHASE_U:
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_U_HIN, 0,true));
|
||||
@@ -219,7 +255,6 @@ static void set_lowside(Phase lowside){
|
||||
}
|
||||
}
|
||||
static void set_highside(Phase highside){
|
||||
HighsidePhase = highside;
|
||||
switch (highside){
|
||||
|
||||
case PHASE_U:
|
||||
@@ -241,7 +276,6 @@ static void set_highside(Phase highside){
|
||||
}
|
||||
}
|
||||
static void set_inactive(Phase inactive){
|
||||
InactivePhase = inactive;
|
||||
switch (inactive){
|
||||
case PHASE_U:
|
||||
ESP_ERROR_CHECK(mcpwm_generator_set_force_level(generator_U_HIN, 0,true));
|
||||
@@ -261,36 +295,87 @@ static void set_inactive(Phase inactive){
|
||||
break;
|
||||
}
|
||||
}
|
||||
esp_err_t set_mcpwm_output(Phase highside, Phase lowside, Phase inactive){
|
||||
|
||||
esp_err_t start_mcpwm_output(){
|
||||
if (timer_U == NULL) {
|
||||
return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde
|
||||
}
|
||||
set_mcpwm_duty(duty);
|
||||
set_inactive(inactive);
|
||||
set_highside(highside);
|
||||
set_lowside(lowside);
|
||||
conf_gens();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
switch (phase_configurations[i].state) {
|
||||
case Highside:
|
||||
set_highside(phase_configurations[i].phase);
|
||||
break;
|
||||
case Lowside:
|
||||
set_lowside(phase_configurations[i].phase);
|
||||
break;
|
||||
case OFF:
|
||||
set_inactive(phase_configurations[i].phase);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t set_mcpwm_duty(float new_duty){
|
||||
if (timer_U == NULL) {
|
||||
return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde
|
||||
void configure_mcpwm_output(OutCombis out_combi) {
|
||||
switch (out_combi) {
|
||||
case OUT_U_V:
|
||||
phase_configurations[0].state = Highside;
|
||||
phase_configurations[1].state = Lowside;
|
||||
phase_configurations[2].state = OFF;
|
||||
break;
|
||||
case OUT_U_W:
|
||||
phase_configurations[0].state = Highside;
|
||||
phase_configurations[1].state = OFF;
|
||||
phase_configurations[2].state = Lowside;
|
||||
break;
|
||||
case OUT_V_W:
|
||||
phase_configurations[0].state = OFF;
|
||||
phase_configurations[1].state = Highside;
|
||||
phase_configurations[2].state = Lowside;
|
||||
break;
|
||||
case OUT_V_U:
|
||||
phase_configurations[0].state = Lowside;
|
||||
phase_configurations[1].state = Highside;
|
||||
phase_configurations[2].state = OFF;
|
||||
break;
|
||||
case OUT_W_U:
|
||||
phase_configurations[0].state = Lowside;
|
||||
phase_configurations[1].state = OFF;
|
||||
phase_configurations[2].state = Highside;
|
||||
break;
|
||||
case OUT_W_V:
|
||||
phase_configurations[0].state = OFF;
|
||||
phase_configurations[1].state = Lowside;
|
||||
phase_configurations[2].state = Highside;
|
||||
break;
|
||||
case OUT_U:
|
||||
phase_configurations[0].state = Highside;
|
||||
phase_configurations[1].state = OFF;
|
||||
phase_configurations[2].state = OFF;
|
||||
break;
|
||||
case OUT_V:
|
||||
phase_configurations[0].state = OFF;
|
||||
phase_configurations[1].state = Highside;
|
||||
phase_configurations[2].state = OFF;
|
||||
break;
|
||||
case OUT_W:
|
||||
phase_configurations[0].state = OFF;
|
||||
phase_configurations[1].state = OFF;
|
||||
phase_configurations[2].state = Highside;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
duty = new_duty;
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_U, (periode_ticks*duty/100)/2));
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_V, (periode_ticks*duty/100)/2));
|
||||
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(comperator_W, (periode_ticks*duty/100)/2));
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t set_mcpwm_frequency(uint16_t frequency){
|
||||
|
||||
esp_err_t set_mcpwm_frequency(uint16_t frequency_new){
|
||||
|
||||
if (timer_U == NULL) {
|
||||
return ESP_ERR_INVALID_STATE; // Fehlerbehandlung, wenn mcpwm nicht initialisiert wurde
|
||||
}
|
||||
periode_ticks = CONFIG_TIMER_BASE_FREQ/frequency;
|
||||
|
||||
periode_ticks = CONFIG_TIMER_BASE_FREQ/frequency_new;
|
||||
mcpwm_frequency = frequency_new;
|
||||
// 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));
|
||||
@@ -309,4 +394,7 @@ void get_comps(mcpwm_cmpr_handle_t comps[3]) {
|
||||
}
|
||||
float get_duty() {
|
||||
return duty;
|
||||
}
|
||||
uint16_t get_frequency(){
|
||||
return mcpwm_frequency;
|
||||
}
|
||||
308
main/menu.c
308
main/menu.c
@@ -0,0 +1,308 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "esp_log.h"
|
||||
#include "ssd1306.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "parsed_pins.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "menu.h"
|
||||
#include "esp_timer.h"
|
||||
#include "mcpwm.h"
|
||||
|
||||
|
||||
/*############################################*/
|
||||
/*############## Display-Setup ###############*/
|
||||
/*############################################*/
|
||||
static SSD1306_t dev;
|
||||
void configure_OLED()
|
||||
{
|
||||
i2c_master_init(&dev, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, -1);
|
||||
ESP_LOGI("OLED", "Panel is 128x64");
|
||||
ssd1306_init(&dev, 128, 64);
|
||||
ssd1306_clear_screen(&dev, false);
|
||||
ssd1306_contrast(&dev, 0xff);
|
||||
ssd1306_display_text_x3(&dev, 0, "Power", 5, false);
|
||||
ssd1306_display_text_x3(&dev, 4, " PCB", 4, false);
|
||||
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
ssd1306_clear_screen(&dev, false);
|
||||
}
|
||||
|
||||
/*############################################*/
|
||||
/*############ Internal Encoder ##############*/
|
||||
/*############################################*/
|
||||
//Variablen
|
||||
static volatile int enc_in_counter = 0;
|
||||
static volatile int64_t last_interrupt_time = 0;
|
||||
static volatile uint16_t last_interrupt_time_but = 0;
|
||||
static volatile bool enc_in_button_flag = false;
|
||||
static volatile uint8_t last_state = 0;
|
||||
|
||||
static void IRAM_ATTR enc_in_isr_handler(void *arg) {
|
||||
static uint64_t last_interrupt_time = 0;
|
||||
|
||||
// Aktueller Zustand der Pins lesen
|
||||
uint8_t current_state = (gpio_get_level(CONFIG_IN_ENC_A_GPIO) << 1) | gpio_get_level(CONFIG_IN_ENC_B_GPIO);
|
||||
uint64_t interrupt_time = esp_timer_get_time();
|
||||
|
||||
// Zustandswechsel-Logik (FSM) ohne starre Entprellzeit
|
||||
if (current_state != last_state) {
|
||||
// Nur wenn der Wechsel signifikant verzögert ist (gute Flanke)
|
||||
if ((interrupt_time - last_interrupt_time) > CONFIG_IN_ENCODER_DEBOUNCE_TIME) {
|
||||
if ((last_state == 0b01 && current_state == 0b11) ||
|
||||
(last_state == 0b10 && current_state == 0b00)) {
|
||||
enc_in_counter++; // Vorwärtsdrehen
|
||||
} else if ((last_state == 0b10 && current_state == 0b11) ||
|
||||
(last_state == 0b01 && current_state == 0b00)) {
|
||||
enc_in_counter--; // Rückwärtsdrehen
|
||||
}
|
||||
last_state = current_state; // Zustand aktualisieren
|
||||
last_interrupt_time = interrupt_time;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void IRAM_ATTR enc_in_but_isr_handler(void *arg) {
|
||||
uint64_t interrupt_time = esp_timer_get_time();
|
||||
|
||||
// Entprellung: Verhindert die Erfassung von Störungen aufgrund von Prellung
|
||||
if (interrupt_time - last_interrupt_time_but > (CONFIG_IN_ENCODER_DEBOUNCE_TIME*1000)) { // Entprellungszeit
|
||||
last_interrupt_time_but = interrupt_time; // Entprellzeit zurücksetzen
|
||||
if (gpio_get_level(CONFIG_IN_ENC_A_GPIO)) {
|
||||
enc_in_button_flag = true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void config_internal_Encoder(){
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_A_GPIO, enc_in_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_B_GPIO, enc_in_isr_handler, NULL));
|
||||
ESP_ERROR_CHECK(gpio_isr_handler_add(CONFIG_IN_ENC_BUT_GPIO, enc_in_but_isr_handler, NULL));
|
||||
}
|
||||
/*############################################*/
|
||||
/*############### Menu-Setup #################*/
|
||||
/*############################################*/
|
||||
typedef enum {
|
||||
MAIN_MENU,
|
||||
CONFIGURE_MENU,
|
||||
MORE_INFO_MENU
|
||||
} MenuState;
|
||||
|
||||
typedef enum {
|
||||
MCPWM_MODE,
|
||||
BLDC_MODE,
|
||||
DC_BRUSHED_MODE,
|
||||
MODE_COUNT
|
||||
}OperationMode;
|
||||
|
||||
const char *mode_names[] = {
|
||||
"MCPWM ",
|
||||
"BLDC ",
|
||||
"DC Brush"
|
||||
};
|
||||
|
||||
const char *OutCombi_names[]= {
|
||||
"+U -V",
|
||||
"+U -W",
|
||||
"+V -W",
|
||||
"+V -U",
|
||||
"+W -U",
|
||||
"+W -V",
|
||||
" +U ",
|
||||
" +V ",
|
||||
" +W "
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
STATE_ACTIVE,
|
||||
STATE_DEAKTIVE,
|
||||
STATE_UV,
|
||||
STATE_OC
|
||||
} BridgeState;
|
||||
|
||||
const char *state_names[] = {
|
||||
"Active ",
|
||||
"Deaktive ",
|
||||
"UV ",
|
||||
"RFE set(OC)"
|
||||
};
|
||||
|
||||
|
||||
//Globalevariablen
|
||||
volatile MenuState current_menu = MAIN_MENU;
|
||||
volatile OperationMode current_mode = MCPWM_MODE;
|
||||
volatile BridgeState current_bridge_state = STATE_DEAKTIVE;
|
||||
volatile OutCombis current_out_combi = OUT_U_V;
|
||||
|
||||
volatile bool ShouldState = false; //false==deaktive
|
||||
int cursor_position = 0;
|
||||
int max_cursor_pos = 0;
|
||||
bool in_editing = false;
|
||||
char display_message[20]; // Puffer für die Nachricht
|
||||
bool flag;
|
||||
static void check_button_pressed(){
|
||||
if (enc_in_button_flag){
|
||||
enc_in_button_flag=false;
|
||||
|
||||
switch (current_menu){
|
||||
|
||||
case MAIN_MENU:
|
||||
switch(cursor_position){
|
||||
case 0:
|
||||
current_mode = (current_mode+1)% MODE_COUNT;
|
||||
break;
|
||||
case 1:
|
||||
ShouldState = !ShouldState;
|
||||
if(ShouldState){
|
||||
start_mcpwm_output();
|
||||
}else{
|
||||
stop_mcpwm_output();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
current_menu = CONFIGURE_MENU;
|
||||
enc_in_counter=0;
|
||||
break;
|
||||
case 3:
|
||||
current_menu = MORE_INFO_MENU;
|
||||
enc_in_counter=0;
|
||||
break;
|
||||
case 4:
|
||||
current_out_combi =(current_out_combi+1)%6;
|
||||
stop_mcpwm_output();
|
||||
configure_mcpwm_output(current_out_combi);
|
||||
ShouldState = false;
|
||||
break;
|
||||
default:
|
||||
snprintf(display_message, sizeof(display_message), "ERROR");
|
||||
ssd1306_display_text(&dev, 7, display_message, strlen(display_message), false);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case CONFIGURE_MENU:
|
||||
switch(cursor_position){
|
||||
|
||||
}
|
||||
|
||||
default:
|
||||
ESP_LOGE("Menu","Not yet programmed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void getset_bridge_state(){
|
||||
|
||||
bool RFE_Pulled = !(gpio_get_level(CONFIG_RFE_GPIO));
|
||||
|
||||
if (RFE_Pulled){
|
||||
current_bridge_state=STATE_OC;
|
||||
}else if(!ShouldState){
|
||||
current_bridge_state=STATE_DEAKTIVE;
|
||||
}else{
|
||||
current_bridge_state=STATE_ACTIVE;
|
||||
}
|
||||
}
|
||||
|
||||
static void render_main_menu(){
|
||||
max_cursor_pos = 4;
|
||||
//Mode
|
||||
snprintf(display_message, sizeof(display_message), "Mode: %s", mode_names[current_mode]);
|
||||
ssd1306_display_text(&dev, 1, display_message, strlen(display_message), cursor_position == 0);
|
||||
|
||||
//ShouldState Started oder Stopped
|
||||
snprintf(display_message, sizeof(display_message), "%s", ShouldState ? "Started" : "Stopped");
|
||||
ssd1306_display_text(&dev, 2, display_message, strlen(display_message), cursor_position == 1);
|
||||
|
||||
//Configure_Menu
|
||||
snprintf(display_message, sizeof(display_message), "Configure ->");
|
||||
ssd1306_display_text(&dev, 3, display_message, strlen(display_message), cursor_position == 2);
|
||||
|
||||
//More_Info_Menu
|
||||
snprintf(display_message, sizeof(display_message), "Sensor Info ->");
|
||||
ssd1306_display_text(&dev, 4, display_message, strlen(display_message), cursor_position == 3);
|
||||
|
||||
//Output Selection
|
||||
snprintf(display_message, sizeof(display_message), "Out: %s",OutCombi_names[current_out_combi]);
|
||||
ssd1306_display_text(&dev, 5, display_message, strlen(display_message), cursor_position == 4);
|
||||
|
||||
|
||||
//State
|
||||
getset_bridge_state();
|
||||
snprintf(display_message, sizeof(display_message), "State: %s",state_names[current_bridge_state]);
|
||||
ssd1306_display_text(&dev, 6, display_message, strlen(display_message), true);
|
||||
|
||||
|
||||
/*snprintf(display_message, sizeof(display_message), "cursor: %i %s",cursor_position,enc_in_button_flag ?"in" : "out");
|
||||
ssd1306_display_text(&dev, 7, display_message, strlen(display_message), true);
|
||||
*/}
|
||||
|
||||
static void render_config_menu(){
|
||||
max_cursor_pos = 3;
|
||||
switch(current_mode){
|
||||
case MCPWM_MODE:
|
||||
//Titel
|
||||
snprintf(display_message, sizeof(display_message), "Conf. MCPWM");
|
||||
ssd1306_display_text(&dev, 1, display_message, strlen(display_message), false);
|
||||
|
||||
//Frequenz
|
||||
snprintf(display_message, sizeof(display_message), "PWMFreq.: %ik ", (get_frequency()/1000));
|
||||
ssd1306_display_text(&dev, 2, display_message, strlen(display_message), cursor_position == 0);
|
||||
|
||||
//Duty cycle
|
||||
snprintf(display_message, sizeof(display_message), "Duty: %.1f%% ", get_duty());
|
||||
ssd1306_display_text(&dev, 3, display_message, strlen(display_message), cursor_position == 1);
|
||||
|
||||
//Todzeit
|
||||
snprintf(display_message, sizeof(display_message), "DeadTime: %ins ", CONFIG_DEAD_TIME_PWM);
|
||||
ssd1306_display_text(&dev, 4, display_message, strlen(display_message), cursor_position == 2);
|
||||
|
||||
//Back
|
||||
snprintf(display_message, sizeof(display_message), " Back ");
|
||||
ssd1306_display_text(&dev, 5, display_message, strlen(display_message), cursor_position == 3);
|
||||
|
||||
//State
|
||||
getset_bridge_state();
|
||||
snprintf(display_message, sizeof(display_message), "State: %s",state_names[current_bridge_state]);
|
||||
ssd1306_display_text(&dev, 6, display_message, strlen(display_message), true);
|
||||
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void render_info_menu(){
|
||||
max_cursor_pos = 8;
|
||||
}
|
||||
|
||||
|
||||
void menu_loop(){
|
||||
if (enc_in_counter<0){
|
||||
enc_in_counter = max_cursor_pos;
|
||||
}
|
||||
if (enc_in_counter> max_cursor_pos){
|
||||
enc_in_counter =0;
|
||||
}
|
||||
cursor_position = enc_in_counter;
|
||||
switch (current_menu)
|
||||
{
|
||||
case MAIN_MENU:
|
||||
render_main_menu();
|
||||
break;
|
||||
case CONFIGURE_MENU:
|
||||
render_config_menu();
|
||||
break;
|
||||
case MORE_INFO_MENU:
|
||||
render_info_menu();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
check_button_pressed();
|
||||
}
|
||||
Reference in New Issue
Block a user