Feat: add loop to main to read adc data

Refs: #4
adc-init
rkcf 2023-02-21 06:26:47 -06:00
parent 2abdd7be0c
commit 284f9e4d71
1 changed files with 27 additions and 1 deletions

View File

@ -10,6 +10,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "hal/adc_types.h" #include "hal/adc_types.h"
#include "soc/soc_caps.h"
/* ADC input pins */ /* ADC input pins */
#define ADC_INPUT_1_PIN 4 #define ADC_INPUT_1_PIN 4
@ -17,6 +18,8 @@
#define ADC_INPUT_3_PIN 9 /* TODO Reroute from pin 46 to 9 on schematic, cant do ADC */ #define ADC_INPUT_3_PIN 9 /* TODO Reroute from pin 46 to 9 on schematic, cant do ADC */
#define ADC_INPUT_4_PIN 10 #define ADC_INPUT_4_PIN 10
#define ADC_READ_LEN 256
/* ADC channels */ /* ADC channels */
static adc_channel_t channel[4] = {ADC_CHANNEL_3, ADC_CHANNEL_5, ADC_CHANNEL_8, ADC_CHANNEL_9}; static adc_channel_t channel[4] = {ADC_CHANNEL_3, ADC_CHANNEL_5, ADC_CHANNEL_8, ADC_CHANNEL_9};
@ -67,7 +70,7 @@ static void adc_cont_init(void)
/* Create continuous mode driver handle */ /* Create continuous mode driver handle */
adc_continuous_handle_cfg_t adc_handle_config = { adc_continuous_handle_cfg_t adc_handle_config = {
.max_store_buf_size = 1024, .max_store_buf_size = 1024,
.conv_frame_size = 100 .conv_frame_size = ADC_READ_LEN
}; };
ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_handle_config, &adc_handle)); ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_handle_config, &adc_handle));
@ -108,6 +111,29 @@ void app_main(void)
adc_task_handle = xTaskGetCurrentTaskHandle(); adc_task_handle = xTaskGetCurrentTaskHandle();
ESP_ERROR_CHECK(adc_continuous_start(adc_handle)); ESP_ERROR_CHECK(adc_continuous_start(adc_handle));
esp_err_t adc_ret;
uint32_t ret_num = 0;
uint8_t result[ADC_READ_LEN] = {0};
while (1) {
/* Perform a read */
adc_ret = adc_continuous_read(adc_handle, result, ADC_READ_LEN, &ret_num, 0);
if (adc_ret == ESP_OK) {
for (int i = 0; i < ret_num; i+= SOC_ADC_DIGI_RESULT_BYTES) {
/* Grab the data and channel number */
adc_digi_output_data_t *p_data = (void*)&result[i];
uint32_t chan = ((p_data)->type2.channel);
uint32_t data = ((p_data)->type2.data);
/* Check channel number to see if data is bogus */
if (chan < SOC_ADC_CHANNEL_NUM(ADC_UNIT_1)) {
ESP_LOGI("adc", "ADC_1_%"PRIu32" value: %"PRIx32, chan, data);
} else {
ESP_LOGW("adc", "Invalid data from ADC_1_%"PRIu32, chan);
}
}
}
}
/* Cleanup ADC */ /* Cleanup ADC */
adc_cleanup(); adc_cleanup();
} }