#include "evse_error.h" #include "evse_config.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "ntc_sensor.h" static const char *TAG = "evse_error"; static uint32_t error_bits = 0; static TickType_t auto_clear_timeout = 0; static bool error_cleared = false; void evse_error_init(void) { // Inicialização do sistema de erros } void evse_error_check(pilot_voltage_t pilot_voltage, bool is_n12v) { ESP_LOGD(TAG, "Verificando erro: pilot_voltage = %d, is_n12v = %s", pilot_voltage, is_n12v ? "true" : "false"); // Falha elétrica geral no pilot if (pilot_voltage == PILOT_VOLTAGE_1) { if (!(error_bits & EVSE_ERR_PILOT_FAULT_BIT)) { // Verifica se o erro já foi registrado evse_error_set(EVSE_ERR_PILOT_FAULT_BIT); ESP_LOGW(TAG, "Erro: pilot abaixo de 2V (falha)"); } } // Falta de -12V durante PWM (C ou D) if ((pilot_voltage == PILOT_VOLTAGE_6 || pilot_voltage == PILOT_VOLTAGE_3) && !is_n12v) { if (!(error_bits & EVSE_ERR_DIODE_SHORT_BIT)) { // Verifica se o erro já foi registrado evse_error_set(EVSE_ERR_DIODE_SHORT_BIT); ESP_LOGW(TAG, "Erro: ausência de -12V no PWM (sem diodo)"); ESP_LOGW(TAG, "Verificando erro: pilot_voltage = %d, is_n12v = %s", pilot_voltage, is_n12v ? "true" : "false"); } } } void evse_temperature_check(void) { float temp_c = ntc_temp_sensor(); // leitura atual (última medida válida) uint8_t threshold = evse_get_temp_threshold(); // padrão 60°C, configurável // Log informativo com os valores ESP_LOGD(TAG, "Verificando temperatura: atual = %.2f °C, limite = %d °C", temp_c, threshold); // Se a temperatura parecer inválida, aplica erro de sensor if (temp_c < -40.0f || temp_c > 150.0f) { if (!(error_bits & EVSE_ERR_TEMPERATURE_FAULT_BIT)) { // Verifica se o erro já foi registrado evse_error_set(EVSE_ERR_TEMPERATURE_FAULT_BIT); ESP_LOGW(TAG, "Sensor NTC falhou ou está desconectado"); } return; } evse_error_clear(EVSE_ERR_TEMPERATURE_FAULT_BIT); // leitura válida if (temp_c >= threshold) { if (!(error_bits & EVSE_ERR_TEMPERATURE_HIGH_BIT)) { // Verifica se o erro já foi registrado evse_error_set(EVSE_ERR_TEMPERATURE_HIGH_BIT); ESP_LOGW(TAG, "Temperatura acima do limite: %.2f °C ≥ %d °C", temp_c, threshold); } } else { evse_error_clear(EVSE_ERR_TEMPERATURE_HIGH_BIT); } } uint32_t evse_get_error(void) { return error_bits; } bool evse_is_error_cleared(void) { return error_cleared; } void evse_mark_error_cleared(void) { error_cleared = false; } // Já existentes void evse_error_set(uint32_t bitmask) { error_bits |= bitmask; if (bitmask & EVSE_ERR_AUTO_CLEAR_BITS) { auto_clear_timeout = xTaskGetTickCount() + pdMS_TO_TICKS(60000); // 60s } } void evse_error_clear(uint32_t bitmask) { bool had_error = error_bits != 0; error_bits &= ~bitmask; if (had_error && error_bits == 0) { error_cleared = true; } } void evse_error_tick(void) { if ((error_bits & EVSE_ERR_AUTO_CLEAR_BITS) && xTaskGetTickCount() >= auto_clear_timeout) { evse_error_clear(EVSE_ERR_AUTO_CLEAR_BITS); auto_clear_timeout = 0; } } bool evse_error_is_active(void) { return error_bits != 0; } uint32_t evse_error_get_bits(void) { return error_bits; } void evse_error_reset_flag(void) { error_cleared = false; } bool evse_error_cleared_flag(void) { return error_cleared; }