mirror of
https://github.com/espressif/esp-idf.git
synced 2025-11-26 20:53:11 +00:00
adc: change the way data is formattted in adc_dma_demo
The way the adc_dma_demo data was formatted caused it go get parsed as markdown syntax. Changed it be more "human" readable as well as put all ADC examples in a common folder
This commit is contained in:
6
examples/peripherals/adc/adc2/CMakeLists.txt
Normal file
6
examples/peripherals/adc/adc2/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(adc2)
|
||||
8
examples/peripherals/adc/adc2/Makefile
Normal file
8
examples/peripherals/adc/adc2/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := adc2
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
96
examples/peripherals/adc/adc2/README.md
Normal file
96
examples/peripherals/adc/adc2/README.md
Normal file
@@ -0,0 +1,96 @@
|
||||
| Supported Targets | ESP32 |
|
||||
| ----------------- | ----- |
|
||||
|
||||
# ADC2 Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
In this example, we use ADC2 to measure the output of DAC.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
#### ESP32 platform
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
We use ADC1_CHANNEL_7 (GPIO27) and DAC_CHANNEL_1 (GPIO25) by default, you need to short the two GPIOs (if you have changed the ADC2 and DAC channel, please refer to Chapter 4.11 of the `ESP32 Technical Reference Manual` to get the pin number).
|
||||
|
||||
#### ESP32-S2 platform
|
||||
|
||||
* A development board with ESP32S2 SoC
|
||||
* A USB cable for power supply and programming
|
||||
|
||||
We use ADC1_CHANNEL_7 (GPIO18) and DAC_CHANNEL_1 (GPIO17) by default, you need to short the two GPIOs (if you have changed the ADC2 and DAC channel, please refer to the `ESP32S2 Technical Reference Manual` to get the pin number).
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
* Set ADC2 and DAC channel in "Example Configuration"
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Running this example, you will see the following log output on the serial monitor:
|
||||
|
||||
### ESP32 platform
|
||||
|
||||
```
|
||||
ADC channel 7 @ GPIO 27, DAC channel 1 @ GPIO 25.
|
||||
adc2_init...
|
||||
start conversion.
|
||||
1: 150
|
||||
2: 203
|
||||
3: 250
|
||||
4: 300
|
||||
5: 351
|
||||
6: 400
|
||||
7: 441
|
||||
8: 491
|
||||
9: 547
|
||||
10: 595
|
||||
...
|
||||
```
|
||||
|
||||
#### ESP32-S2 platform
|
||||
|
||||
```
|
||||
ADC channel 7 @ GPIO 18, DAC channel 1 @ GPIO 17.
|
||||
adc2_init...
|
||||
start conversion.
|
||||
1: 150
|
||||
2: 203
|
||||
3: 250
|
||||
4: 300
|
||||
5: 351
|
||||
6: 400
|
||||
7: 441
|
||||
8: 491
|
||||
9: 547
|
||||
10: 595
|
||||
...
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* program upload failure
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
||||
2
examples/peripherals/adc/adc2/main/CMakeLists.txt
Normal file
2
examples/peripherals/adc/adc2/main/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "adc2_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
105
examples/peripherals/adc/adc2/main/Kconfig.projbuild
Normal file
105
examples/peripherals/adc/adc2/main/Kconfig.projbuild
Normal file
@@ -0,0 +1,105 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
choice EXAMPLE_ADC2_CHANNEL
|
||||
bool "ADC2 Channel Num"
|
||||
depends on IDF_TARGET_ESP32
|
||||
default EXAMPLE_ADC2_CHANNEL_7
|
||||
help
|
||||
The channel of ADC2 used in this example.
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL_0
|
||||
bool "ADC2 Channel 0 (GPIO 4)"
|
||||
config EXAMPLE_ADC2_CHANNEL_1
|
||||
bool "ADC2 Channel 1 (GPIO 0)"
|
||||
config EXAMPLE_ADC2_CHANNEL_2
|
||||
bool "ADC2 Channel 2 (GPIO 2)"
|
||||
config EXAMPLE_ADC2_CHANNEL_3
|
||||
bool "ADC2 Channel 3 (GPIO 15)"
|
||||
config EXAMPLE_ADC2_CHANNEL_4
|
||||
bool "ADC2 Channel 4 (GPIO 13)"
|
||||
config EXAMPLE_ADC2_CHANNEL_5
|
||||
bool "ADC2 Channel 5 (GPIO 12)"
|
||||
config EXAMPLE_ADC2_CHANNEL_6
|
||||
bool "ADC2 Channel 6 (GPIO 14)"
|
||||
config EXAMPLE_ADC2_CHANNEL_7
|
||||
bool "ADC2 Channel 7 (GPIO 27)"
|
||||
config EXAMPLE_ADC2_CHANNEL_8
|
||||
bool "ADC2 Channel 8 (GPIO 25)"
|
||||
config EXAMPLE_ADC2_CHANNEL_9
|
||||
bool "ADC2 Channel 9 (GPIO 26)"
|
||||
endchoice
|
||||
|
||||
choice EXAMPLE_ADC2_CHANNEL
|
||||
bool "ADC2 Channel Num"
|
||||
depends on IDF_TARGET_ESP32S2
|
||||
default EXAMPLE_ADC2_CHANNEL_7
|
||||
help
|
||||
The channel of ADC2 used in this example.
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL_0
|
||||
bool "ADC2 Channel 0 (GPIO 11)"
|
||||
config EXAMPLE_ADC2_CHANNEL_1
|
||||
bool "ADC2 Channel 1 (GPIO 12)"
|
||||
config EXAMPLE_ADC2_CHANNEL_2
|
||||
bool "ADC2 Channel 2 (GPIO 13)"
|
||||
config EXAMPLE_ADC2_CHANNEL_3
|
||||
bool "ADC2 Channel 3 (GPIO 14)"
|
||||
config EXAMPLE_ADC2_CHANNEL_4
|
||||
bool "ADC2 Channel 4 (GPIO 15)"
|
||||
config EXAMPLE_ADC2_CHANNEL_5
|
||||
bool "ADC2 Channel 5 (GPIO 16)"
|
||||
config EXAMPLE_ADC2_CHANNEL_6
|
||||
bool "ADC2 Channel 6 (GPIO 17)"
|
||||
config EXAMPLE_ADC2_CHANNEL_7
|
||||
bool "ADC2 Channel 7 (GPIO 18)"
|
||||
config EXAMPLE_ADC2_CHANNEL_8
|
||||
bool "ADC2 Channel 8 (GPIO 19)"
|
||||
config EXAMPLE_ADC2_CHANNEL_9
|
||||
bool "ADC2 Channel 9 (GPIO 20)"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_ADC2_CHANNEL
|
||||
int
|
||||
default 0 if EXAMPLE_ADC2_CHANNEL_0
|
||||
default 1 if EXAMPLE_ADC2_CHANNEL_1
|
||||
default 2 if EXAMPLE_ADC2_CHANNEL_2
|
||||
default 3 if EXAMPLE_ADC2_CHANNEL_3
|
||||
default 4 if EXAMPLE_ADC2_CHANNEL_4
|
||||
default 5 if EXAMPLE_ADC2_CHANNEL_5
|
||||
default 6 if EXAMPLE_ADC2_CHANNEL_6
|
||||
default 7 if EXAMPLE_ADC2_CHANNEL_7
|
||||
default 8 if EXAMPLE_ADC2_CHANNEL_8
|
||||
default 9 if EXAMPLE_ADC2_CHANNEL_9
|
||||
|
||||
choice EXAMPLE_DAC_CHANNEL
|
||||
bool "DAC Channel Num"
|
||||
depends on IDF_TARGET_ESP32
|
||||
default EXAMPLE_DAC_CHANNEL_1
|
||||
help
|
||||
The channel of DAC used in this example.
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL_1
|
||||
bool "DAC Channel 1 (GPIO25)"
|
||||
config EXAMPLE_DAC_CHANNEL_2
|
||||
bool "DAC Channel 2 (GPIO26)"
|
||||
endchoice
|
||||
|
||||
choice EXAMPLE_DAC_CHANNEL
|
||||
bool "DAC Channel Num"
|
||||
depends on IDF_TARGET_ESP32S2
|
||||
default EXAMPLE_DAC_CHANNEL_1
|
||||
help
|
||||
The channel of DAC used in this example.
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL_1
|
||||
bool "DAC Channel 1 (GPIO17)"
|
||||
config EXAMPLE_DAC_CHANNEL_2
|
||||
bool "DAC Channel 2 (GPIO18)"
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_DAC_CHANNEL
|
||||
int
|
||||
default 0 if EXAMPLE_DAC_CHANNEL_1
|
||||
default 1 if EXAMPLE_DAC_CHANNEL_2
|
||||
|
||||
endmenu
|
||||
69
examples/peripherals/adc/adc2/main/adc2_example_main.c
Normal file
69
examples/peripherals/adc/adc2/main/adc2_example_main.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ADC2 Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/adc.h"
|
||||
#include "driver/dac.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#define DAC_EXAMPLE_CHANNEL CONFIG_EXAMPLE_DAC_CHANNEL
|
||||
#define ADC2_EXAMPLE_CHANNEL CONFIG_EXAMPLE_ADC2_CHANNEL
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
|
||||
#endif
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
uint8_t output_data=0;
|
||||
int read_raw;
|
||||
esp_err_t r;
|
||||
|
||||
gpio_num_t adc_gpio_num, dac_gpio_num;
|
||||
|
||||
r = adc2_pad_get_io_num( ADC2_EXAMPLE_CHANNEL, &adc_gpio_num );
|
||||
assert( r == ESP_OK );
|
||||
r = dac_pad_get_io_num( DAC_EXAMPLE_CHANNEL, &dac_gpio_num );
|
||||
assert( r == ESP_OK );
|
||||
|
||||
printf("ADC2 channel %d @ GPIO %d, DAC channel %d @ GPIO %d.\n", ADC2_EXAMPLE_CHANNEL, adc_gpio_num,
|
||||
DAC_EXAMPLE_CHANNEL + 1, dac_gpio_num );
|
||||
|
||||
dac_output_enable( DAC_EXAMPLE_CHANNEL );
|
||||
|
||||
//be sure to do the init before using adc2.
|
||||
printf("adc2_init...\n");
|
||||
adc2_config_channel_atten( ADC2_EXAMPLE_CHANNEL, ADC_ATTEN_11db );
|
||||
|
||||
vTaskDelay(2 * portTICK_PERIOD_MS);
|
||||
|
||||
printf("start conversion.\n");
|
||||
while(1) {
|
||||
dac_output_voltage( DAC_EXAMPLE_CHANNEL, output_data++ );
|
||||
r = adc2_get_raw( ADC2_EXAMPLE_CHANNEL, width, &read_raw);
|
||||
if ( r == ESP_OK ) {
|
||||
printf("%d: %d\n", output_data, read_raw );
|
||||
} else if ( r == ESP_ERR_INVALID_STATE ) {
|
||||
printf("%s: ADC2 not initialized yet.\n", esp_err_to_name(r));
|
||||
} else if ( r == ESP_ERR_TIMEOUT ) {
|
||||
//This can not happen in this example. But if WiFi is in use, such error code could be returned.
|
||||
printf("%s: ADC2 is in use by Wi-Fi.\n", esp_err_to_name(r));
|
||||
} else {
|
||||
printf("%s\n", esp_err_to_name(r));
|
||||
}
|
||||
|
||||
vTaskDelay( 2 * portTICK_PERIOD_MS );
|
||||
}
|
||||
}
|
||||
3
examples/peripherals/adc/adc2/main/component.mk
Normal file
3
examples/peripherals/adc/adc2/main/component.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
6
examples/peripherals/adc/adc_dma_demo/CMakeLists.txt
Normal file
6
examples/peripherals/adc/adc_dma_demo/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(adc)
|
||||
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "adc_dma_example_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/adc.h"
|
||||
|
||||
#define TIMES 256
|
||||
#define DMA_CHANNEL 0
|
||||
|
||||
static void continuous_adc_init(uint16_t adc1_chan_mask, uint16_t adc2_chan_mask, adc_channel_t *channel, uint8_t channel_num)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
assert(ret == ESP_OK);
|
||||
|
||||
adc_digi_init_config_t adc_dma_config = {
|
||||
.max_store_buf_size = 1024,
|
||||
.conv_num_each_intr = 256,
|
||||
.dma_chan = SOC_GDMA_ADC_DMA_CHANNEL,
|
||||
.adc1_chan_mask = adc1_chan_mask,
|
||||
.adc2_chan_mask = adc2_chan_mask,
|
||||
};
|
||||
ret = adc_digi_initialize(&adc_dma_config);
|
||||
assert(ret == ESP_OK);
|
||||
|
||||
adc_digi_pattern_table_t adc_pattern[10];
|
||||
adc_digi_config_t dig_cfg = {
|
||||
.conv_limit_en = 0,
|
||||
.conv_limit_num = 250,
|
||||
.interval = 40,
|
||||
.dig_clk.use_apll = 0,
|
||||
.dig_clk.div_num = 15,
|
||||
.dig_clk.div_a = 0,
|
||||
.dig_clk.div_b = 1,
|
||||
};
|
||||
|
||||
dig_cfg.adc_pattern_len = channel_num;
|
||||
for (int i = 0; i < channel_num; i++) {
|
||||
uint8_t unit = ((channel[i] >> 3) & 0x1);
|
||||
uint8_t ch = channel[i] & 0x7;
|
||||
adc_pattern[i].atten = ADC_ATTEN_DB_0;
|
||||
adc_pattern[i].channel = ch;
|
||||
adc_pattern[i].unit = unit;
|
||||
}
|
||||
dig_cfg.adc_pattern = adc_pattern;
|
||||
ret = adc_digi_controller_config(&dig_cfg);
|
||||
assert(ret == ESP_OK);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static void continuous_read_demo(void *arg)
|
||||
{
|
||||
esp_err_t ret;
|
||||
uint32_t ret_num = 0;
|
||||
uint8_t result[TIMES] = {0};
|
||||
memset(result, 0xcc, TIMES);
|
||||
|
||||
uint16_t adc1_chan_mask = BIT(0) | BIT(1);
|
||||
uint16_t adc2_chan_mask = BIT(0);
|
||||
adc_channel_t channel[3] = {ADC1_CHANNEL_0, ADC1_CHANNEL_1, (ADC2_CHANNEL_0 | 1 << 3)};
|
||||
|
||||
continuous_adc_init(adc1_chan_mask, adc2_chan_mask, channel, sizeof(channel) / sizeof(adc_channel_t));
|
||||
adc_digi_start();
|
||||
|
||||
int n = 20;
|
||||
while(n--) {
|
||||
ret = adc_digi_read_bytes(result, TIMES, &ret_num, ADC_MAX_DELAY);
|
||||
for (int i = 0; i < ret_num; i+=4) {
|
||||
adc_digi_output_data_t *p = (void*)&result[i];
|
||||
if (check_valid_data(p)) {
|
||||
printf("ADC%d_CH%d: %x\n", p->type2.unit+1, p->type2.channel, p->type2.data);
|
||||
} else {
|
||||
printf("Invalid data [%d_%d_%x]\n", p->type2.unit+1, p->type2.channel, p->type2.data);
|
||||
}
|
||||
}
|
||||
// If you see task WDT in this task, it means the conversion is too fast for the task to handle
|
||||
}
|
||||
|
||||
adc_digi_stop();
|
||||
ret = adc_digi_deinitialize();
|
||||
assert(ret == ESP_OK);
|
||||
}
|
||||
|
||||
static void single_read_demo(void *arg)
|
||||
{
|
||||
esp_err_t ret;
|
||||
int adc1_reading[3] = {0xcc};
|
||||
int adc2_reading[1] = {0xcc};
|
||||
|
||||
const char TAG_CH[][10] = {"ADC1_CH2", "ADC1_CH3","ADC1_CH4", "ADC2_CH0"};
|
||||
|
||||
adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
|
||||
adc1_config_channel_atten(ADC1_CHANNEL_2, ADC_ATTEN_DB_0);
|
||||
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_6);
|
||||
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_0);
|
||||
adc2_config_channel_atten(ADC2_CHANNEL_0, ADC_ATTEN_DB_0);
|
||||
|
||||
int n = 20;
|
||||
while (n--) {
|
||||
|
||||
adc1_reading[0] = adc1_get_raw(ADC1_CHANNEL_2);
|
||||
adc1_reading[1] = adc1_get_raw(ADC1_CHANNEL_3);
|
||||
adc1_reading[2] = adc1_get_raw(ADC1_CHANNEL_4);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
ESP_LOGI(TAG_CH[i], "%x", adc1_reading[i]);
|
||||
}
|
||||
|
||||
ret = adc2_get_raw(ADC2_CHANNEL_0, ADC_WIDTH_BIT_12, &adc2_reading[0]);
|
||||
assert(ret == ESP_OK);
|
||||
ESP_LOGI(TAG_CH[3], "%x", adc2_reading[0]);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
single_read_demo(NULL);
|
||||
continuous_read_demo(NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user