2017-08-07 09:33:01 -04:00
|
|
|
/* ADC1 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 "driver/gpio.h"
|
|
|
|
#include "driver/adc.h"
|
2019-06-13 07:34:01 -04:00
|
|
|
#include "esp_adc_cal.h"
|
2019-06-20 04:13:47 -04:00
|
|
|
|
2018-01-24 04:22:13 -05:00
|
|
|
#define DEFAULT_VREF 1100 //Use adc2_vref_to_gpio() to obtain a better estimate
|
2018-02-13 07:47:18 -05:00
|
|
|
#define NO_OF_SAMPLES 64 //Multisampling
|
2018-01-24 04:22:13 -05:00
|
|
|
|
2018-02-13 07:47:18 -05:00
|
|
|
static esp_adc_cal_characteristics_t *adc_chars;
|
2020-07-09 08:55:52 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-02-13 07:47:18 -05:00
|
|
|
static const adc_channel_t channel = ADC_CHANNEL_6; //GPIO34 if ADC1, GPIO14 if ADC2
|
2020-02-25 09:19:48 -05:00
|
|
|
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
2019-06-20 04:13:47 -04:00
|
|
|
static const adc_channel_t channel = ADC_CHANNEL_6; // GPIO7 if ADC1, GPIO17 if ADC2
|
2020-02-25 09:19:48 -05:00
|
|
|
static const adc_bits_width_t width = ADC_WIDTH_BIT_13;
|
2020-01-16 22:47:08 -05:00
|
|
|
#endif
|
2018-02-13 07:47:18 -05:00
|
|
|
static const adc_atten_t atten = ADC_ATTEN_DB_0;
|
|
|
|
static const adc_unit_t unit = ADC_UNIT_1;
|
2017-08-07 09:33:01 -04:00
|
|
|
|
2020-07-09 08:55:52 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void check_efuse(void)
|
2017-08-07 09:33:01 -04:00
|
|
|
{
|
2020-07-09 08:55:52 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
//Check if TP is burned into eFuse
|
2018-02-13 07:47:18 -05:00
|
|
|
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
|
|
|
printf("eFuse Two Point: Supported\n");
|
|
|
|
} else {
|
|
|
|
printf("eFuse Two Point: NOT supported\n");
|
|
|
|
}
|
|
|
|
//Check Vref is burned into eFuse
|
|
|
|
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
|
|
|
|
printf("eFuse Vref: Supported\n");
|
|
|
|
} else {
|
|
|
|
printf("eFuse Vref: NOT supported\n");
|
|
|
|
}
|
2020-07-09 08:55:52 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
|
|
|
|
printf("eFuse Two Point: Supported\n");
|
|
|
|
} else {
|
|
|
|
printf("Cannot retrieve eFuse Two Point calibration values. Default calibration values will be used.\n");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#error "This example is configured for ESP32/ESP32S2."
|
|
|
|
#endif
|
2018-02-13 07:47:18 -05:00
|
|
|
}
|
|
|
|
|
2020-07-09 08:55:52 -04:00
|
|
|
|
2018-02-13 07:47:18 -05:00
|
|
|
static void print_char_val_type(esp_adc_cal_value_t val_type)
|
|
|
|
{
|
|
|
|
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
|
|
|
printf("Characterized using Two Point Value\n");
|
|
|
|
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
|
|
|
|
printf("Characterized using eFuse Vref\n");
|
|
|
|
} else {
|
|
|
|
printf("Characterized using Default Vref\n");
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 08:55:52 -04:00
|
|
|
|
2018-01-24 04:22:13 -05:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-02-13 07:47:18 -05:00
|
|
|
{
|
|
|
|
//Check if Two Point or Vref are burned into eFuse
|
|
|
|
check_efuse();
|
2018-01-24 04:22:13 -05:00
|
|
|
|
2018-02-13 07:47:18 -05:00
|
|
|
//Configure ADC
|
|
|
|
if (unit == ADC_UNIT_1) {
|
2020-02-25 09:19:48 -05:00
|
|
|
adc1_config_width(width);
|
2018-02-13 07:47:18 -05:00
|
|
|
adc1_config_channel_atten(channel, atten);
|
|
|
|
} else {
|
|
|
|
adc2_config_channel_atten((adc2_channel_t)channel, atten);
|
|
|
|
}
|
2018-01-24 04:22:13 -05:00
|
|
|
|
2018-02-13 07:47:18 -05:00
|
|
|
//Characterize ADC
|
|
|
|
adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
|
2020-02-25 09:19:48 -05:00
|
|
|
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, width, DEFAULT_VREF, adc_chars);
|
2018-02-13 07:47:18 -05:00
|
|
|
print_char_val_type(val_type);
|
2018-01-24 04:22:13 -05:00
|
|
|
|
|
|
|
//Continuously sample ADC1
|
2018-02-13 07:47:18 -05:00
|
|
|
while (1) {
|
|
|
|
uint32_t adc_reading = 0;
|
|
|
|
//Multisampling
|
|
|
|
for (int i = 0; i < NO_OF_SAMPLES; i++) {
|
|
|
|
if (unit == ADC_UNIT_1) {
|
|
|
|
adc_reading += adc1_get_raw((adc1_channel_t)channel);
|
|
|
|
} else {
|
|
|
|
int raw;
|
2020-02-25 09:19:48 -05:00
|
|
|
adc2_get_raw((adc2_channel_t)channel, width, &raw);
|
2018-02-13 07:47:18 -05:00
|
|
|
adc_reading += raw;
|
|
|
|
}
|
2018-01-24 04:22:13 -05:00
|
|
|
}
|
2018-02-13 07:47:18 -05:00
|
|
|
adc_reading /= NO_OF_SAMPLES;
|
|
|
|
//Convert adc_reading to voltage in mV
|
|
|
|
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
|
|
|
|
printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
|
2019-06-13 07:34:01 -04:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
}
|
|
|
|
}
|