ADC1-DMA feature

This commit is contained in:
semarnat committed 2026-05-18 23:47:52 -06:00
1 parent 84cd51fd89
commit 8a0cdd9b47
4 files changed
+127 -1

No files matched your search

+4
View File
@@ -1 +1,5 @@
.pio .pio
compile_commands.json
.clangd
.vscode
.cache
+12 -1
View File
@@ -9,6 +9,17 @@
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[env:esp32-s3-devkitc-1] [env:esp32-s3-devkitc-1]
platform = espressif32 platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
board = esp32-s3-devkitc-1 board = esp32-s3-devkitc-1
build_type = debug
framework = arduino framework = arduino
monitor_speed = 230400
board_build.arduino.memory_type = qio_opi
board_build.flash_mode = qio
board_build.psram_type = opi
board_upload.flash_size = 16MB
board_upload.maximum_size = 16777216
board_build.partitions = default_16MB.csv
board_build.extra_flags =
-DBOARD_HAS_PSRAM
-DCORE_DEBUG_LEVEL=5
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include "esp_adc/adc_continuous.h"
void ADC() {
adc_continuous_handle_t handle = NULL;
adc_continuous_handle_cfg_t adc_config = {
};
}
+101
View File
@@ -0,0 +1,101 @@
#include "Arduino.h"
#include "esp_adc/adc_continuous.h"
// API ESP-IDF para ADC modo continuo (uso de DMA)
#define MS_BLOQUE 10
#define HZ_ADC 1000 // Limitado a 611 - 83333 para el ESP32-S3
#define CANAL 3
const uint32_t MUESTRAS_BLOQUE = (HZ_ADC * MS_BLOQUE) / 1000;
const uint32_t FRAME_SIZE = MUESTRAS_BLOQUE * 4; // Constante física SOC_ADC_DIGI_RESULT_BYTES
const uint32_t POOL_SIZE = FRAME_SIZE * 4; // Buffer DMA de 4 veces el tamaño del FRAME_SIZE
uint8_t dma_buffer[FRAME_SIZE] = {0};
adc_channel_t channels[1] = {ADC_CHANNEL_3};
volatile bool dma_data_ready = false;
adc_continuous_handle_t handle_ADC1_1 = NULL;
// DEBUG
uint16_t DRAM_contador = 0;
bool IRAM_ATTR adc_dma_callback(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data){
dma_data_ready = true;
return false;
}
void setupADC(adc_channel_t *canales, uint8_t numChannels){
adc_continuous_handle_cfg_t ADC1_1_config = {
.max_store_buf_size = POOL_SIZE,
.conv_frame_size = FRAME_SIZE,
};
ESP_ERROR_CHECK(adc_continuous_new_handle(&ADC1_1_config, &handle_ADC1_1));
adc_digi_pattern_config_t channel_config = {0};
channel_config.channel = canales[0];
channel_config.atten = ADC_ATTEN_DB_12;
channel_config.bit_width = ADC_BITWIDTH_12;
channel_config.unit = ADC_UNIT_1;
adc_continuous_config_t adc_config = {
.pattern_num = numChannels,
.adc_pattern = &channel_config,
.sample_freq_hz = HZ_ADC,
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
};
ESP_ERROR_CHECK(adc_continuous_config(handle_ADC1_1, &adc_config));
adc_continuous_evt_cbs_t cb_config = {
.on_conv_done = adc_dma_callback,
.on_pool_ovf = NULL
};
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle_ADC1_1, &cb_config, NULL));
ESP_ERROR_CHECK(adc_continuous_start(handle_ADC1_1));
Serial.println("DMA Iniciado con sistema de bandera.");
}
void setup() {
Serial.begin(230400);
setupADC(channels, 1);
}
void loop() {
if (dma_data_ready) {
dma_data_ready = false;
uint32_t ret_num = 0;
// Leer los datos crudos del DMA
esp_err_t ret = adc_continuous_read(handle_ADC1_1, dma_buffer, FRAME_SIZE, &ret_num, 0);
if (ret == ESP_OK) {
adc_continuous_data_t parsed_data[MUESTRAS_BLOQUE];
uint32_t parsed_count = 0;
//API oficial documentada para extraer la información (reemplaza a los p->type2)
esp_err_t parse_ret = adc_continuous_parse_data(handle_ADC1_1,
dma_buffer,
ret_num,
parsed_data,
&parsed_count);
if (parse_ret == ESP_OK) {
for (uint32_t i = 0; i < parsed_count; i++) {
if (parsed_data[i].valid) {
DRAM_contador++;
// Imprimir resultados
Serial.printf("Unidad: ADC%u | Canal: %u | Valor: %04lu | Debug: %u\n",
parsed_data[i].unit + 1,
parsed_data[i].channel,
parsed_data[i].raw_data,
DRAM_contador);
DRAM_contador = (DRAM_contador >= HZ_ADC) ? 0 : DRAM_contador;
}
}
}
}
}
}