Hi all.
The code modified from a main.c try to read analog value by ESP32, got compiling error.
it passed compiling when commented out:
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
but output nothing after uploaded into ESP32 S3.
How to fix this please?
Thanks
Adam
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_system.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
typedef long i32;
static esp_adc_cal_characteristics_t *adc_chars;
static const adc1_channel_t channel = ADC1_CHANNEL_0; /// DECLEARED BY ADC.h; typedef enum { ADC1_CHANNEL_0 = 0, /*!< ADC1 channel 0 is GPIO1 */ ;;; // WAS: static const adc1_channel_t channel = ADC1_CHANNEL_6;
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
static const adc_atten_t atten = ADC_ATTEN_DB_11;
static uint8_t adc_configured = 0;
static uint8_t adc_reading = 0;
uint8_t voltage = 0;
void setup()
{
Serial.begin(115200);
Serial.println("setup!");
delay(1000);
Serial.println("setup!");
delay(1000);
Serial.println("setup!");
delay(1000);
Serial.println("setup!");
}
void loop()
{
Serial.println("loop!");
adc_init();
for (;;) {
ESP_LOGI(TAG, "VBat: %ld", adc_read());
vTaskDelay(1000);
}
}
void adc_init() {
/* gpio config */
esp_err_t err;
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = 1ULL << GPIO_NUM_7;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
err = gpio_config(&io_conf);
/* config attenuation */
adc1_config_channel_atten(channel, atten);
/* config bit width */
adc1_config_width(width);
/// adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1,
atten,
width,
1100,
adc_chars);
}
i32 adc_read() {
#define BATADC_SAMPLE_SZ 32
// multisampling
for (uint8_t i = 0; i < BATADC_SAMPLE_SZ; i++) {
i32 raw = adc1_get_raw((adc1_channel_t)channel);
adc_reading += raw;
delay(20); /// was: sys_delay_us(20);
}
adc_reading /= (BATADC_SAMPLE_SZ); /// 'adc_reading' was not declared in this scope suggested alternative: 'adc_read'
voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars) * 2u;
return voltage;
}
ERROR:
Arduino: 1.8.19 (Windows 10), Board: "ESP32S3 Dev Module, Disabled, Disabled, QIO 80MHz, 4MB (32Mb), Core 1, Core 1, Hardware CDC and JTAG, Enabled, Disabled, Disabled, UART0 / Hardware CDC, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi), 921600, None, Disabled"
C:\Users\Xinzhou\Documents\Arduino\ESP32-S3_ADC_reading_issue\ESP32-S3_ADC_reading_issue.ino: In function 'void adc_init()':
ESP32-S3_ADC_reading_issue:72:26: error: invalid conversion from 'int' to 'gpio_pulldown_t' [-fpermissive]
io_conf.pull_down_en = 0; /// io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; /// https://github.com/espressif/esp-idf/issues/285
^
ESP32-S3_ADC_reading_issue:73:24: error: invalid conversion from 'int' to 'gpio_pullup_t' [-fpermissive]
io_conf.pull_up_en = 0;
^
exit status 1
invalid conversion from 'int' to 'gpio_pulldown_t' [-fpermissive]
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
J-M-L
April 29, 2023, 7:24am
2
This means there is strong typing for the type gpio_pulldown_t that you need to honor.
You canβt use 0 you need to use one of the enumerators
enum gpio_pull_mode_t
enumerator GPIO_PULLUP_ONLY β Pad pull up
enumerator GPIO_PULLDOWN_ONLY β Pad pull down
enumerator GPIO_PULLUP_PULLDOWN β Pad pull up + pull down
enumerator GPIO_FLOATING β Pad floating
1 Like
J-M-L:
enum gpio_pull_mode_t
is not gpio_pullup_t
or gpio_pulldown_t
.
Here ase the correct enums.
typedef enum {
GPIO_PULLUP_DISABLE = 0x0, /*!< Disable GPIO pull-up resistor */
GPIO_PULLUP_ENABLE = 0x1, /*!< Enable GPIO pull-up resistor */
} gpio_pullup_t;
typedef enum {
GPIO_PULLDOWN_DISABLE = 0x0, /*!< Disable GPIO pull-down resistor */
GPIO_PULLDOWN_ENABLE = 0x1, /*!< Enable GPIO pull-down resistor */
} gpio_pulldown_t;
1 Like
J-M-L
April 29, 2023, 10:44am
4
oops
indeed thanks - I copied the wrong enum
1 Like
Great!
Thank you.
I got compiling ERROR of: cannot convert 'adc1_channel_t' to 'adc_channel_t' in initialization
after added:
static uint16_t adc1_chan_mask = BIT(2) | BIT(3) | BIT(4);
static uint16_t adc2_chan_mask = BIT(0);
static adc_channel_t channel[4] = {ADC1_CHANNEL_2, ADC1_CHANNEL_3, ADC1_CHANNEL_4, (ADC2_CHANNEL_0 | 1 << 3)};
new code and error code below.
Thanks for help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_system.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"
//............................... new added
static uint16_t adc1_chan_mask = BIT(2) | BIT(3) | BIT(4);
static uint16_t adc2_chan_mask = BIT(0);
static adc_channel_t channel[4] = {ADC1_CHANNEL_2, ADC1_CHANNEL_3, ADC1_CHANNEL_4, (ADC2_CHANNEL_0 | 1 << 3)};
//..............................
typedef long i32;
static esp_adc_cal_characteristics_t *adc_chars;
static const adc1_channel_t channel = ADC1_CHANNEL_0; /// DECLEARED BY ADC.h; typedef enum { ADC1_CHANNEL_0 = 0, /*!< ADC1 channel 0 is GPIO1 */ ;;; // WAS: static const adc1_channel_t channel = ADC1_CHANNEL_6;
static const adc_bits_width_t width = ADC_WIDTH_BIT_12;
static const adc_atten_t atten = ADC_ATTEN_DB_11;
static uint8_t adc_configured = 0;
static uint8_t adc_reading = 0;
uint8_t voltage = 0;
static int adc_raw[2][10];
static const char *TAG = "ADC SINGLE";
void setup()
{
Serial.begin(115200);
Serial.println("setup!");
delay(1000);
adc_init();
/* for (;;) {
ESP_LOGI(TAG, "VBat: %ld", adc_read());
vTaskDelay(1000);
} */
}
void loop()
{
// Serial.println("loop!");
/* adc_init();
for (;;) {
ESP_LOGI(TAG, "VBat: %ld", adc_read());
vTaskDelay(1000);
} */
adc_read();
Serial.print("voltage=");
Serial.println(voltage);
}
void adc_init() {
/* gpio config */
esp_err_t err;
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = 1ULL << GPIO_NUM_7;
//// io_conf.pull_down_en = 0; /// io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; /// https://github.com/espressif/esp-idf/issues/285
//// io_conf.pull_up_en = 0;
err = gpio_config(&io_conf);
/* config attenuation */
adc1_config_channel_atten(channel, atten);
/* config bit width */
adc1_config_width(width);
/// adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
adc_chars = (esp_adc_cal_characteristics_t *)calloc(1, sizeof(esp_adc_cal_characteristics_t));
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1,
atten,
width,
1100,
adc_chars);
//..........................
////Here ase the correct enums.
typedef enum {
GPIO_PULLUP_DISABLE = 0x0, /*!< Disable GPIO pull-up resistor */
GPIO_PULLUP_ENABLE = 0x1, /*!< Enable GPIO pull-up resistor */
} gpio_pullup_t;
typedef enum {
GPIO_PULLDOWN_DISABLE = 0x0, /*!< Disable GPIO pull-down resistor */
GPIO_PULLDOWN_ENABLE = 0x1, /*!< Enable GPIO pull-down resistor */
} gpio_pulldown_t;
//..........................
}
void adc_read() {
#define BATADC_SAMPLE_SZ 32
// multisampling
for (uint8_t i = 0; i < BATADC_SAMPLE_SZ; i++) {
i32 raw = adc1_get_raw((adc1_channel_t)channel);
/// adc_raw = adc1_get_raw((adc1_channel_t)channel);
adc_reading += raw;
delay(20); /// was: sys_delay_us(20);
}
adc_reading /= (BATADC_SAMPLE_SZ); /// 'adc_reading' was not declared in this scope suggested alternative: 'adc_read'
voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars) * 2u;
/// return voltage;
Serial.print("voltage =");
Serial.print("voltage =");
}
system
Closed
October 26, 2023, 2:57pm
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.