mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
b4c90c44d2
Due to HW limitation, we don't support this anymore. On s3 and c3, ADC2 under continuous mode is not stable. However, you can enable CONFIG_ADC_CONTINUOUS_FORCE_USE_ADC2_ON_C3_S3 to force use ADC2. Refer to errata to know more details: https://www.espressif.com/sites/default/files/documentation/esp32-s3_errata_en.pdf https://www.espressif.com/sites/default/files/documentation/esp32-c3_errata_en.pdf
158 lines
5.5 KiB
C
158 lines
5.5 KiB
C
/*
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "sdkconfig.h"
|
|
#include "esp_log.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include "esp_adc/adc_continuous.h"
|
|
|
|
#define EXAMPLE_READ_LEN 256
|
|
#define EXAMPLE_ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
|
#define EXAMPLE_ADC_USE_OUTPUT_TYPE1 1
|
|
#define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
|
|
#else
|
|
#define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
|
|
#endif
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
static adc_channel_t channel[2] = {ADC_CHANNEL_6, ADC_CHANNEL_7};
|
|
#else
|
|
static adc_channel_t channel[2] = {ADC_CHANNEL_2, ADC_CHANNEL_3};
|
|
#endif
|
|
|
|
static TaskHandle_t s_task_handle;
|
|
static const char *TAG = "EXAMPLE";
|
|
|
|
|
|
static bool IRAM_ATTR s_conv_done_cb(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data)
|
|
{
|
|
BaseType_t mustYield = pdFALSE;
|
|
//Notify that ADC continuous driver has done enough number of conversions
|
|
vTaskNotifyGiveFromISR(s_task_handle, &mustYield);
|
|
|
|
return (mustYield == pdTRUE);
|
|
}
|
|
|
|
static void continuous_adc_init(adc_channel_t *channel, uint8_t channel_num, adc_continuous_handle_t *out_handle)
|
|
{
|
|
adc_continuous_handle_t handle = NULL;
|
|
|
|
adc_continuous_handle_cfg_t adc_config = {
|
|
.max_store_buf_size = 1024,
|
|
.conv_frame_size = EXAMPLE_READ_LEN,
|
|
};
|
|
ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle));
|
|
|
|
adc_continuous_config_t dig_cfg = {
|
|
.sample_freq_hz = 20 * 1000,
|
|
.conv_mode = EXAMPLE_ADC_CONV_MODE,
|
|
.format = EXAMPLE_ADC_OUTPUT_TYPE,
|
|
};
|
|
|
|
adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
|
|
dig_cfg.pattern_num = channel_num;
|
|
for (int i = 0; i < channel_num; i++) {
|
|
uint8_t unit = ADC_UNIT_1;
|
|
uint8_t ch = channel[i] & 0x7;
|
|
adc_pattern[i].atten = ADC_ATTEN_DB_0;
|
|
adc_pattern[i].channel = ch;
|
|
adc_pattern[i].unit = unit;
|
|
adc_pattern[i].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
|
|
|
|
ESP_LOGI(TAG, "adc_pattern[%d].atten is :%x", i, adc_pattern[i].atten);
|
|
ESP_LOGI(TAG, "adc_pattern[%d].channel is :%x", i, adc_pattern[i].channel);
|
|
ESP_LOGI(TAG, "adc_pattern[%d].unit is :%x", i, adc_pattern[i].unit);
|
|
}
|
|
dig_cfg.adc_pattern = adc_pattern;
|
|
ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg));
|
|
|
|
*out_handle = handle;
|
|
}
|
|
|
|
static bool check_valid_data(const adc_digi_output_data_t *data)
|
|
{
|
|
#if EXAMPLE_ADC_USE_OUTPUT_TYPE1
|
|
if (data->type1.channel >= SOC_ADC_CHANNEL_NUM(ADC_UNIT_1)) {
|
|
return false;
|
|
}
|
|
#else
|
|
if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(ADC_UNIT_1)) {
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
esp_err_t ret;
|
|
uint32_t ret_num = 0;
|
|
uint8_t result[EXAMPLE_READ_LEN] = {0};
|
|
memset(result, 0xcc, EXAMPLE_READ_LEN);
|
|
|
|
s_task_handle = xTaskGetCurrentTaskHandle();
|
|
|
|
adc_continuous_handle_t handle = NULL;
|
|
continuous_adc_init(channel, sizeof(channel) / sizeof(adc_channel_t), &handle);
|
|
|
|
adc_continuous_evt_cbs_t cbs = {
|
|
.on_conv_done = s_conv_done_cb,
|
|
};
|
|
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
|
|
ESP_ERROR_CHECK(adc_continuous_start(handle));
|
|
|
|
while(1) {
|
|
|
|
/**
|
|
* This is to show you the way to use the ADC continuous mode driver event callback.
|
|
* This `ulTaskNotifyTake` will block when the data processing in the task is fast.
|
|
* However in this example, the data processing (print) is slow, so you barely block here.
|
|
*
|
|
* Without using this event callback (to notify this task), you can still just call
|
|
* `adc_continuous_read()` here in a loop, with/without a certain block timeout.
|
|
*/
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
while (1) {
|
|
ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0);
|
|
if (ret == ESP_OK) {
|
|
ESP_LOGI("TASK", "ret is %x, ret_num is %"PRIu32, ret, ret_num);
|
|
for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) {
|
|
adc_digi_output_data_t *p = (void*)&result[i];
|
|
if (check_valid_data(p)) {
|
|
#if EXAMPLE_ADC_USE_OUTPUT_TYPE1
|
|
ESP_LOGI(TAG, "Unit: %d, Channel: %d, Value: %x", 1, p->type1.channel, p->type1.data);
|
|
#else
|
|
ESP_LOGI(TAG, "Unit: %d,_Channel: %d, Value: %x", 1, p->type2.channel, p->type2.data);
|
|
#endif
|
|
} else {
|
|
ESP_LOGI(TAG, "Invalid data");
|
|
}
|
|
}
|
|
/**
|
|
* Because printing is slow, so every time you call `ulTaskNotifyTake`, it will immediately return.
|
|
* To avoid a task watchdog timeout, add a delay here. When you replace the way you process the data,
|
|
* usually you don't need this delay (as this task will block for a while).
|
|
*/
|
|
vTaskDelay(1);
|
|
} else if (ret == ESP_ERR_TIMEOUT) {
|
|
//We try to read `EXAMPLE_READ_LEN` until API returns timeout, which means there's no available data
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
ESP_ERROR_CHECK(adc_continuous_stop(handle));
|
|
ESP_ERROR_CHECK(adc_continuous_deinit(handle));
|
|
}
|