Reconfigure GPIO as output after analog read

zampano
Posts: 10
Joined: Sun Feb 17, 2019 9:53 pm

Reconfigure GPIO as output after analog read

Postby zampano » Tue Oct 11, 2022 12:10 pm

Hi,
to automatically configure firmware I need to read the voltage of a pin (GPIO13) that is normally used as output. Unfortunately it's not useful to read the logic level (same value for the two hardware configurations) so I need to use ADC.
The code is:

#define PIN_LED_RELAY2 13

int adc_raw;

ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));

ESP_ERROR_CHECK(adc2_config_channel_atten(ADC2_CHANNEL_4, ADC_ATTEN_11db));
vTaskDelay(100 / portTICK_PERIOD_MS);
adc2_get_raw(ADC2_CHANNEL_4, ADC_WIDTH_BIT_DEFAULT, &adc_raw);
printf("IO13: %d\n", adc_raw);
if (adc_raw > 1500)
nRelay = 2;

// reconfigure GPIO as output
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[PIN_LED_RELAY2], PIN_FUNC_GPIO);
gpio_set_direction(PIN_LED_RELAY2, GPIO_MODE_OUTPUT);
gpio_set_level(PIN_LED_RELAY3, 1);

But in this way setting or resetting GPIO13 has no effect while it works if I comment analog read section.
What can I do to reconfigure GPIO13 as output?

username
Posts: 538
Joined: Thu May 03, 2018 1:18 pm

Re: Reconfigure GPIO as output after analog read

Postby username » Wed Oct 12, 2022 3:25 am

Code: Select all

PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[PIN_LED_RELAY2], PIN_FUNC_GPIO);
gpio_set_direction(PIN_LED_RELAY2, GPIO_MODE_OUTPUT);
gpio_set_level(PIN_LED_RELAY3, 1);
Did you have a type here? Shouldn't it be PIN_LED_RELAY2 ?
gpio_set_level(PIN_LED_RELAY3, 1);


try...

gpio_reset_pin(PIN_LED_RELAY2);
gpio_set_direction(PIN_LED_RELAY2, GPIO_MODE_OUTPUT);
gpio_set_level(PIN_LED_RELAY2, 1);

zampano
Posts: 10
Joined: Sun Feb 17, 2019 9:53 pm

Re: Reconfigure GPIO as output after analog read

Postby zampano » Wed Oct 12, 2022 7:21 am

Solved:
it's necessary to call
rtc_gpio_deinit(PIN_LED_RELAY3);

In this way the pin is connected again to GPIO Matrix instead of RTC Matrix.

Who is online

Users browsing this forum: No registered users and 116 guests