ESP32 Port Manipulation

mineirinRLV
Posts: 3
Joined: Thu Sep 10, 2020 5:28 pm

ESP32 Port Manipulation

Postby mineirinRLV » Thu Sep 10, 2020 6:48 pm

Hi Guys,
this is my first forum thread.
If I am doing something against the forum rules, please inform me and I will correct it.

My difficulty is ESP32 port manipulation .

I'm using a DOIT ESP32 DEVKIT V1 board with the arduino IDE.

For write a sketch I got information from the ESP32 Technical Reference Manual Version 4.2

5.3.3 Simple GPIO Output

The GPIO Matrix can also be used for simple GPIO output - setting a bit in the GPIO_OUT_DATA register will
write to the corresponding GPIO pad.
To configure a pad as simple GPIO output, the GPIO Matrix GPIO_FUNCx_OUT_SEL register is configured with a special peripheral index value (0x100).

I wrote this test sketch.

void setup ()
{
REG_WRITE (GPIO_ENABLE_REG, BIT13); // Define GPIO13 as output
REG_WRITE (GPIO_FUNC2_OUT_SEL_CFG_REG, 0x100); // Special peripheral index value (0x100)
REG_WRITE (GPIO_ENABLE_REG, BIT2); // Define GPIO2 as output
REG_WRITE (GPIO_FUNC13_OUT_SEL_CFG_REG, 0x100); // Special peripheral index value (0x100)
}

void loop ()
{
REG_WRITE (GPIO_OUT_W1TS_REG, BIT2); // GPIO2 HIGH (set)
REG_WRITE (GPIO_OUT_W1TS_REG, BIT13); // GPIO13 HIGH (set)
delay (50);
REG_WRITE (GPIO_OUT_W1TC_REG, BIT2); // GPIO2 LOW (clear)
REG_WRITE (GPIO_OUT_W1TS_REG, BIT13); // GPIO13 LOW (clear)
delay (50);
}

In this way it works normally and the BUILT_IN LED blinks.

But if I change the setup () lines , spinning as below, doesn't work.

REG_WRITE (GPIO_ENABLE_REG, BIT2); // Define GPIO2 as output
REG_WRITE (GPIO_FUNC2_OUT_SEL_CFG_REG, 0x100); // Special peripheral index value (0x100)
REG_WRITE (GPIO_ENABLE_REG, BIT13); // Define GPI13 as output
REG_WRITE (GPIO_FUNC13_OUT_SEL_CFG_REG, 0x100); // Special peripheral index value (0x100)

I ask where I'm going wrong.
Best regards
mRV

ESP_Sprite
Posts: 9591
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Port Manipulation

Postby ESP_Sprite » Fri Sep 11, 2020 8:40 am

I think you're making a mistake here:

Code: Select all

REG_WRITE (GPIO_ENABLE_REG, BIT2); // Define GPIO2 as output
This indeed defines GPIO2 as output, but also overwrites all the other bits with 0, making all other pins an input. So if you later on run this line with GPIO13, you're converting GPIO2 back to an input.
You probably want to do

Code: Select all

REG_SET_BIT(GPIO_ENABLE_REG, BIT2);
instead.

mineirinRLV
Posts: 3
Joined: Thu Sep 10, 2020 5:28 pm

Re: ESP32 Port Manipulation

Postby mineirinRLV » Fri Sep 11, 2020 7:33 pm

Hi ESP_Sprite,
thank you very much for your attention.

To facilitate the understanding of the problem, I have modified the sketch as shown below.
I ran the sketch individually for each GPIO (bit_x) with an LED on the output.
The result appears in the list above the sketch.
I wonder why mainly GPIOs 12.13, and 14 do not work with
port manipulation, but work with the "blink" example of the Arduino IDE.

Best regards mRV

/*
OK blink LED NO no blink LED

GPIO15 NO
GPIO2 OK
GPIO4 NO
GPIO16 OK RX0
GPIO17 OK TX0
GPIO5 OK
GPIO18 OK
GPIO19 OK
GPIO21 OK
GPIO22 OK
GPIO23 OK

GPIO13 NO
GPIO12 NO
GPIO14 NO
GPIO27 OK
GPIO26 OK
GPIO25 OK
GPIO33 INPUT ONLY
GPIO32 INPUT ONLY
GPIO35 INPUT ONLY
GPIO34 INPUT ONLY
*/

#define bitUsed BIT1
void setup()
{
REG_WRITE(GPIO_ENABLE_REG, bitUsed); //Define GPIO as output
}

void loop()
{
REG_WRITE(GPIO_OUT_W1TS_REG, bitUsed); //GPIO HIGH (set)
delay(50);
REG_WRITE(GPIO_OUT_W1TC_REG, bitUsed); //GPIO LOW (clear)
delay(50);
}

ESP_Sprite
Posts: 9591
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Port Manipulation

Postby ESP_Sprite » Sun Sep 13, 2020 8:10 am

They don't work because those pins are initially used as JTAG pins. There are a few signals with properties that make them unfeasible to run through the GPIO matrix (JTAG, but also e.g. high-speed SPI, SDIO, and a few others) so they get switched in the (per-pin) IO mux instead, and this setting bypasses the GPIO matrix. To fix this, you probably want to mess with the MCU_SEL field of the IO_MUX_x_REG registers of those pins to correct that.

mineirinRLV
Posts: 3
Joined: Thu Sep 10, 2020 5:28 pm

Re: ESP32 Port Manipulation

Postby mineirinRLV » Sun Sep 20, 2020 10:03 pm

Hi ESP_Sprite,
thank you very much for your attention again.
OK, I undertund your expanation,
I will study more about ESP32 GPIOS.
Tks

rafa_wildfire
Posts: 1
Joined: Tue Feb 01, 2022 10:51 pm

Re: ESP32 Port Manipulation

Postby rafa_wildfire » Tue Feb 01, 2022 10:54 pm

Where can i find the documentation that can show me these functions "REG_WRITE", "RED_READ", etc.. ?

ESP_Sprite
Posts: 9591
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Port Manipulation

Postby ESP_Sprite » Wed Feb 02, 2022 3:17 am

I don't think they're properly documented as they're internal shortcuts for writing to and reading from memory that makes it clear to the reader it's a peripheral register we're accessing; there's no real functionality in them. If you're asking about the arguments (the registers and the values), you want the ESP32 TRM for that.

Who is online

Users browsing this forum: zilizii and 169 guests