【ESP32 IDF】ADC多通道DMA读取

ADC多通道DMA读取

1. 问题的引出

  • 我需要用到两个adc采集电压,项目有用的wifi功能(所以只能用adc1,ADC2wifi功能要用)。
  • 但是ESP32只有两个adc所以只能用adc多通道读取(有会的私我)。
  • 用下面的函数不知道怎么配置多通道,所以只能官方demo里面的dma_read更改。
  • 官方demo里面的esp32不是多通道。
read_raw = adc1_get_raw(ADC1_TEST_CHANNEL)

2. 踩坑

  • demo 里面 dma读取要把接受到的数据处理了才能打印。
  • adc_digi_read_bytes(result, TIMES, &ret_num, ADC_MAX_DELAY) // 读取的
  • adc1_channel_5_value = p->type1.data; //处理

3. 源码

/*
 * SPDX-FileCopyrightText: 2021 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 "driver/adc.h"

#define TIMES 256
#define GET_UNIT(x) ((x >> 3) & 0x1)

static volatile int16_t adc1_channel_5_value;
static volatile int16_t adc1_channel_7_value;

#define ADC_RESULT_BYTE 2
#define ADC_CONV_LIMIT_EN 1                  // 对于ESP32,应始终将其设置为1
#define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1 // ESP32仅支持ADC_CONV_SINGLE_UNIT_1模式
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1

static uint16_t adc1_chan_mask = BIT(5) | BIT(7);
static uint16_t adc2_chan_mask = 0;
static adc_channel_t channel[2] = {ADC1_CHANNEL_5, ADC1_CHANNEL_7};

static const char *TAG = "ADC DMA";

static void continuous_adc_init(uint16_t adc1_chan_mask, uint16_t adc2_chan_mask, adc_channel_t *channel, uint8_t channel_num)
{
    adc_digi_init_config_t adc_dma_config = {
        .max_store_buf_size = 1024,
        .conv_num_each_intr = TIMES,
        .adc1_chan_mask = adc1_chan_mask,
        .adc2_chan_mask = adc2_chan_mask,
    };
    ESP_ERROR_CHECK(adc_digi_initialize(&adc_dma_config));

    adc_digi_configuration_t dig_cfg = {
        .conv_limit_en = ADC_CONV_LIMIT_EN,
        .conv_limit_num = 250,
        .sample_freq_hz = 20 * 1000,
        .conv_mode = ADC_CONV_MODE,
        .format = 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 = GET_UNIT(channel[i]);
        uint8_t ch = channel[i] & 0x7;
        adc_pattern[i].atten = ADC_ATTEN_DB_11;
        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_digi_controller_configure(&dig_cfg));
}

static bool check_valid_data(const adc_digi_output_data_t *data)
{
    const unsigned int unit = data->type2.unit;
    if (unit > 2)
        return false;
    if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit))
        return false;
    return true;
}

// 打印值
void LOGTASK(void *param)
{
    while (1)
    {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG, "adc1_channel_5_value: %d,adc1_channel_7_value: %d", adc1_channel_5_value, adc1_channel_7_value);
    }
}

void app_main(void)
{
    esp_err_t ret;
    uint32_t ret_num = 0;
    uint8_t result[TIMES] = {0};
    memset(result, 0xcc, TIMES);
    xTaskCreate(LOGTASK, "LOGTASK", 5 * 1024, NULL, 2, NULL);

    continuous_adc_init(adc1_chan_mask, adc2_chan_mask, channel, sizeof(channel) / sizeof(adc_channel_t));
    adc_digi_start();

    while (1)
    {
        ret = adc_digi_read_bytes(result, TIMES, &ret_num, ADC_MAX_DELAY);
        if (ret == ESP_OK || ret == ESP_ERR_INVALID_STATE)
        {
            if (ret == ESP_ERR_INVALID_STATE)
            {
            }

            // ESP_LOGI("TASK:", "ret is %x, ret_num is %d", ret, ret_num);
            for (int i = 0; i < ret_num; i += ADC_RESULT_BYTE)
            {
                adc_digi_output_data_t *p = (void *)&result[i];

                {
                    if (p->type1.channel == ADC1_CHANNEL_5 || p->type1.channel == ADC1_CHANNEL_7)
                    {
                        switch (p->type1.channel)
                        {
                        case ADC1_CHANNEL_5:
                            adc1_channel_5_value = p->type1.data;
                            break;

                        case ADC1_CHANNEL_7:
                            adc1_channel_7_value = p->type1.data;
                            break;
                        default:
                            break;
                        }
                    }
                }
                // ESP_LOGI(TAG, "Unit: %d, Channel: %d, Value: %x", 1, p->type1.channel, p->type1.data);
                vTaskDelay(1);
            }
            if (ret == ESP_ERR_TIMEOUT)
            {
                /**
                 * ``ESP_ERR_TIMEOUT``: If ADC conversion is not finished until Timeout, you'll get this return error.
                 * Here we set Timeout ``portMAX_DELAY``, so you'll never reach this branch.
                 */
                ESP_LOGW(TAG, "No data, increase timeout or reduce conv_num_each_intr");
                vTaskDelay(1000);
            }
        }
    }
    adc_digi_stop();
    ret = adc_digi_deinitialize();
    assert(ret == ESP_OK);
}

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guistar~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值