fix ade7758
This commit is contained in:
@@ -18,4 +18,4 @@ set(srcs
|
||||
idf_component_register(SRCS "${srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES nvs_flash driver esp_adc esp_timer
|
||||
REQUIRES config evse api ntc_driver)
|
||||
REQUIRES config evse api ntc_driver spi_bus_manager)
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
|
||||
static const char* TAG = "ac_relay";
|
||||
|
||||
// Memoization do estado atual do relé (salvo em RAM)
|
||||
static int last_state = -1;
|
||||
|
||||
/**
|
||||
* @brief Initialize the AC relay GPIO.
|
||||
*
|
||||
@@ -16,7 +19,7 @@ void ac_relay_init(void)
|
||||
gpio_config_t conf = {
|
||||
.pin_bit_mask = BIT64(board_config.ac_relay_gpio),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE, ///< Disabled unless required
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
@@ -27,7 +30,8 @@ void ac_relay_init(void)
|
||||
return;
|
||||
}
|
||||
|
||||
gpio_set_level(board_config.ac_relay_gpio, false); ///< Ensure relay starts OFF
|
||||
gpio_set_level(board_config.ac_relay_gpio, 0); ///< Ensure relay starts OFF
|
||||
last_state = 0;
|
||||
ESP_LOGI(TAG, "AC relay initialized. Pin: %d", board_config.ac_relay_gpio);
|
||||
}
|
||||
|
||||
@@ -38,9 +42,15 @@ void ac_relay_init(void)
|
||||
*/
|
||||
void ac_relay_set_state(bool state)
|
||||
{
|
||||
if (state == last_state) {
|
||||
// Estado não mudou; evita log e escrita desnecessária.
|
||||
return;
|
||||
}
|
||||
last_state = state;
|
||||
|
||||
ESP_LOGI(TAG, "Setting AC relay state: Pin: %d, State: %d", board_config.ac_relay_gpio, state);
|
||||
|
||||
esp_err_t ret = gpio_set_level(board_config.ac_relay_gpio, state);
|
||||
|
||||
esp_err_t ret = gpio_set_level(board_config.ac_relay_gpio, state ? 1 : 0);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set GPIO level (error: %s)", esp_err_to_name(ret));
|
||||
}
|
||||
@@ -55,5 +65,5 @@ bool ac_relay_get_state(void)
|
||||
{
|
||||
int level = gpio_get_level(board_config.ac_relay_gpio);
|
||||
ESP_LOGD(TAG, "Current AC relay state: Pin: %d, State: %d", board_config.ac_relay_gpio, level);
|
||||
return level;
|
||||
return (level != 0);
|
||||
}
|
||||
|
||||
@@ -1,53 +1,64 @@
|
||||
#include "driver/spi_master.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "adc121s021_dma.h"
|
||||
#include "spi_bus_manager.h"
|
||||
|
||||
#define TAG "adc_dma"
|
||||
|
||||
#define PIN_NUM_MOSI 23
|
||||
#define PIN_NUM_MISO 19
|
||||
#define PIN_NUM_CLK 18
|
||||
#define PIN_NUM_CS 5
|
||||
#define PIN_NUM_CS 5
|
||||
#define SAMPLE_SIZE_BYTES 2
|
||||
#define ADC_BITS 12
|
||||
#define SPI_CLOCK_HZ (6 * 1000 * 1000) // 6 MHz
|
||||
|
||||
#define SPI_HOST_USED SPI2_HOST
|
||||
#define SAMPLE_SIZE_BYTES 2
|
||||
#define ADC_BITS 12
|
||||
|
||||
static spi_device_handle_t adc_spi;
|
||||
static spi_device_handle_t adc_spi = NULL;
|
||||
|
||||
void adc121s021_dma_init(void)
|
||||
{
|
||||
spi_bus_config_t buscfg = {
|
||||
.mosi_io_num = PIN_NUM_MOSI,
|
||||
.miso_io_num = PIN_NUM_MISO,
|
||||
.sclk_io_num = PIN_NUM_CLK,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
.max_transfer_sz = SAMPLE_SIZE_BYTES,
|
||||
};
|
||||
if (adc_spi) {
|
||||
ESP_LOGW(TAG, "ADC121S021 já foi inicializado.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!spi_bus_manager_is_initialized()) {
|
||||
ESP_LOGI(TAG, "SPI bus não inicializado. Inicializando...");
|
||||
esp_err_t err = spi_bus_manager_init(); // 🔧 CORRIGIDO: sem argumentos
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Falha ao inicializar o SPI bus: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.clock_speed_hz = 6000000, // 6 MHz
|
||||
.clock_speed_hz = SPI_CLOCK_HZ,
|
||||
.mode = 0,
|
||||
.spics_io_num = PIN_NUM_CS,
|
||||
.queue_size = 2,
|
||||
.flags = SPI_DEVICE_NO_DUMMY,
|
||||
.pre_cb = NULL,
|
||||
.post_cb = NULL,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST_USED, &buscfg, SPI_DMA_CH_AUTO));
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST_USED, &devcfg, &adc_spi));
|
||||
esp_err_t err = spi_bus_add_device(spi_bus_manager_get_host(), &devcfg, &adc_spi);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Falha ao registrar ADC121S021 no SPI: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "ADC121S021 registrado no SPI com sucesso.");
|
||||
}
|
||||
|
||||
bool adc121s021_dma_get_sample(uint16_t *sample)
|
||||
{
|
||||
uint8_t tx_buffer[2] = {0x00, 0x00}; // Dummy TX
|
||||
if (!adc_spi) {
|
||||
ESP_LOGE(TAG, "ADC SPI não inicializado!");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t tx_buffer[2] = {0x00, 0x00}; // Dummy
|
||||
uint8_t rx_buffer[2] = {0};
|
||||
|
||||
spi_transaction_t t = {
|
||||
.length = 16, // 16 bits
|
||||
.length = 16,
|
||||
.tx_buffer = tx_buffer,
|
||||
.rx_buffer = rx_buffer,
|
||||
.flags = 0
|
||||
@@ -55,12 +66,10 @@ bool adc121s021_dma_get_sample(uint16_t *sample)
|
||||
|
||||
esp_err_t err = spi_device_transmit(adc_spi, &t);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "SPI transmit error: %s", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Erro na transmissão SPI: %s", esp_err_to_name(err));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extrai os 12 bits significativos da resposta do ADC121S021
|
||||
*sample = ((rx_buffer[0] << 8) | rx_buffer[1]) & 0x0FFF;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ static void buzzer_worker_task(void *arg) {
|
||||
|
||||
while (true) {
|
||||
if (xQueueReceive(buzzer_queue, &pattern_id, portMAX_DELAY)) {
|
||||
//buzzer_execute_pattern(pattern_id);
|
||||
buzzer_execute_pattern(pattern_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user