new upgrade

This commit is contained in:
2025-12-21 23:28:26 +00:00
parent 82fa194bd8
commit 023644a887
99 changed files with 7457 additions and 7079 deletions

View File

@@ -33,9 +33,10 @@
#include "ocpp.h"
#include "led.h"
#include "scheduler.h"
#include "storage_service.h"
#define AP_CONNECTION_TIMEOUT 120000
#define RESET_HOLD_TIME 30000
#define RESET_HOLD_TIME 30000 // ms
#define DEBOUNCE_TIME_MS 50
#define PRESS_BIT BIT0
@@ -48,6 +49,9 @@ static TickType_t press_tick = 0;
static volatile TickType_t last_interrupt_tick = 0;
static bool pressed = false;
// Spinlock para garantir debounce seguro na ISR
static portMUX_TYPE button_spinlock = portMUX_INITIALIZER_UNLOCKED;
//
// File system (SPIFFS) init and info
//
@@ -56,9 +60,15 @@ static void fs_info(esp_vfs_spiffs_conf_t *conf)
size_t total = 0, used = 0;
esp_err_t ret = esp_spiffs_info(conf->partition_label, &total, &used);
if (ret == ESP_OK)
ESP_LOGI(TAG, "Partition %s: total: %d, used: %d", conf->partition_label, total, used);
{
ESP_LOGI(TAG, "Partition %s: total: %d, used: %d",
conf->partition_label, (int)total, (int)used);
}
else
ESP_LOGE(TAG, "Failed to get SPIFFS info: %s", esp_err_to_name(ret));
{
ESP_LOGE(TAG, "Failed to get SPIFFS info (%s): %s",
conf->partition_label, esp_err_to_name(ret));
}
}
static void fs_init(void)
@@ -87,6 +97,7 @@ static void fs_init(void)
//
static void wifi_event_task_func(void *param)
{
(void)param;
EventBits_t mode_bits;
for (;;)
{
@@ -107,6 +118,7 @@ static void wifi_event_task_func(void *param)
pdMS_TO_TICKS(AP_CONNECTION_TIMEOUT)) &
WIFI_AP_CONNECTED_BIT)
{
// Espera sair do AP
xEventGroupWaitBits(
wifi_event_group,
WIFI_AP_DISCONNECTED_BIT,
@@ -118,12 +130,15 @@ static void wifi_event_task_func(void *param)
{
if (xEventGroupGetBits(wifi_event_group) & WIFI_AP_MODE_BIT)
{
// Timeout sem cliente ligado
// wifi_ap_stop();
ESP_LOGW(TAG, "AP timeout sem conexões");
}
}
}
else if (mode_bits & WIFI_STA_MODE_BIT)
{
// Apenas aguarda desconexão STA
xEventGroupWaitBits(
wifi_event_group,
WIFI_STA_DISCONNECTED_BIT,
@@ -135,21 +150,30 @@ static void wifi_event_task_func(void *param)
}
//
// Button press handler
// Button press handler (short press => AP)
//
static void handle_button_press(void)
{
// Pode ser chamado cedo demais se a ordem de init mudar
if (wifi_event_group == NULL)
{
ESP_LOGW(TAG, "Wi-Fi ainda não inicializado, ignorando botão Wi-Fi");
return;
}
if (!(xEventGroupGetBits(wifi_event_group) & WIFI_AP_MODE_BIT))
{
ESP_LOGI(TAG, "Starting Wi-Fi AP mode");
ESP_LOGI(TAG, "Starting Wi-Fi AP mode (short press)");
wifi_ap_start();
}
}
// Task to handle button press/release notifications
// Task para lidar com notificações de botão (PRESS / RELEASE)
static void user_input_task_func(void *param)
{
(void)param;
uint32_t notification;
for (;;)
{
if (xTaskNotifyWait(
@@ -163,60 +187,64 @@ static void user_input_task_func(void *param)
press_tick = xTaskGetTickCount();
pressed = true;
ESP_LOGI(TAG, "Button Pressed");
handle_button_press();
// Decisão (short/long) é feita na soltura
}
if ((notification & RELEASED_BIT) && pressed)
{
pressed = false;
TickType_t held = xTaskGetTickCount() - press_tick;
ESP_LOGI(TAG, "Button Released (held %u ms)", (unsigned)pdTICKS_TO_MS(held));
TickType_t held_ticks = xTaskGetTickCount() - press_tick;
uint32_t held_ms = pdTICKS_TO_MS(held_ticks);
if (held >= pdMS_TO_TICKS(RESET_HOLD_TIME))
ESP_LOGI(TAG, "Button Released (held %u ms)", (unsigned)held_ms);
if (held_ms >= RESET_HOLD_TIME)
{
ESP_LOGW(TAG, "Long press: erasing NVS + reboot");
nvs_flash_erase();
esp_restart();
}
else
{
// Short press: apenas habilita modo AP
handle_button_press();
}
}
}
}
}
// ISR for button GPIO interrupt (active-low)
// ISR para GPIO do botão (ativo em nível baixo)
static void IRAM_ATTR button_isr_handler(void *arg)
{
(void)arg;
BaseType_t higher_task_woken = pdFALSE;
TickType_t now = xTaskGetTickCountFromISR();
portENTER_CRITICAL_ISR(&button_spinlock);
if (now - last_interrupt_tick < pdMS_TO_TICKS(DEBOUNCE_TIME_MS))
{
portEXIT_CRITICAL_ISR(&button_spinlock);
return;
}
last_interrupt_tick = now;
if (user_input_task == NULL)
{
portEXIT_CRITICAL_ISR(&button_spinlock);
return;
}
int level = gpio_get_level(board_config.button_wifi_gpio);
if (level == 0)
{
xTaskNotifyFromISR(
user_input_task,
PRESS_BIT,
eSetBits,
&higher_task_woken);
}
else
{
xTaskNotifyFromISR(
user_input_task,
RELEASED_BIT,
eSetBits,
&higher_task_woken);
}
uint32_t bits = (level == 0) ? PRESS_BIT : RELEASED_BIT;
xTaskNotifyFromISR(
user_input_task,
bits,
eSetBits,
&higher_task_woken);
portEXIT_CRITICAL_ISR(&button_spinlock);
if (higher_task_woken)
{
@@ -242,12 +270,15 @@ static void button_init(void)
//
static void init_modules(void)
{
ESP_ERROR_CHECK(storage_service_init());
peripherals_init();
led_init();
wifi_ini();
wifi_ini(); // garante wifi_event_group inicializado
buzzer_init();
ESP_ERROR_CHECK(rest_server_init("/data"));
protocols_init();
evse_manager_init();
evse_init();
auth_init();
@@ -257,6 +288,7 @@ static void init_modules(void)
evse_link_init();
ocpp_start();
scheduler_init();
protocols_init();
}
//
@@ -286,17 +318,20 @@ void app_main(void)
board_config_load();
// 1) cria a task que recebe notificações do botão
xTaskCreate(user_input_task_func, "user_input_task", 4 * 1024, NULL, 3, &user_input_task);
// 2) agora é seguro registrar ISR do botão
button_init();
// 3) inicia o resto do sistema
// 1) Inicia todos os módulos (inclui Wi-Fi, EVSE, etc.)
init_modules();
// 4) tasks auxiliares
xTaskCreate(wifi_event_task_func, "wifi_event_task", 8 * 1024, NULL, 3, NULL);
// 2) Cria a task que recebe notificações do botão
BaseType_t rc;
rc = xTaskCreate(user_input_task_func, "user_input_task", 4 * 1024, NULL, 3, &user_input_task);
configASSERT(rc == pdPASS);
// 3) Agora é seguro registrar ISR do botão
button_init();
// 4) Task auxiliar para eventos Wi-Fi
rc = xTaskCreate(wifi_event_task_func, "wifi_event_task", 8 * 1024, NULL, 3, NULL);
configASSERT(rc == pdPASS);
}
// === Fim de: main/main.c ===