ESP32 Register Programming

Paul79
Posts: 1
Joined: Fri Apr 25, 2025 7:48 am

ESP32 Register Programming

Postby Paul79 » Fri Apr 25, 2025 8:16 am

Hey Guys,

im new to ESP32 Programming, i have knowledge in Atmega328p programming on register level. So now i want to programm the ESP32 on register level, but the technical reference is not that easy to read and follow.

I have written some code for ESP32 so activate the ESP32 permanently, but the code wont work, maybe you could help me. What have i done wrong?

#include "soc/gpio_reg.h"
#include "soc/io_mux_reg.h"
#include "driver/gpio.h"
#include <stdio.h>

void app_main(void){
REG_WRITE(GPIO_FUNC16_OUT_SEL_CFG_REG, (1 << GPIO_FUNC16_OEN_SEL)); // Das ist der richtige Shift-Wert

// Aktiviere GPIO16 als Ausgang
REG_WRITE(GPIO_ENABLE_REG, (1 << 16));

// Setze GPIO16 auf HIGH
REG_WRITE(GPIO_OUT_REG, (1 << 16));
}

MicroController
Posts: 2705
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 Register Programming

Postby MicroController » Fri Apr 25, 2025 10:49 am

So now i want to programm the ESP32 on register level
The ESPs don't 'work' that way. As you may have noticed, the Technical Reference Manual isn't as complete as you may be used to from Atmel or STM. The 'philosophy' behind this is that you're supposed to just use the provided ESP-IDF drivers instead. Which makes sense from a productivity point-of-view: Why write down what code you'd have to write to use a peripheral when I can provide the necessary code directly?

To understand how some aspect of a peripheral works in detail, the drivers' source code is usually a definitive source of information.

Also worth noting is that the ESPs are much more complex devices than the AVRs, which inherently makes working 'bare metal' much harder.

If you want to use direct register accesses, e.g. for maximum performance, I recommend at least using the IDF driver for initialization of the peripheral you want to use, and after that use specific registers only as much as necessary.

Also pay attention to multi-threading issues when 'bypassing' the drivers.
I have written some code for ESP32 so activate the ESP32 permanently,
What does that mean?
Looking at your code, I assume you're trying to output a high level on GPIO16 and disable the outputs of all other GPIO0...31 at the same time? - Not a good idea because you'd kill the MCU's access to the flash memory in the process. (And apparently, GPIO16 is used for the flash/PSRAM's CE# signal, so not to be messed with anyway.)

Code: Select all

... (1 << GPIO_FUNC16_OEN_SEL)); // Das ist der richtige Shift-Wert
No, it's not ;-)
GPIO_FUNC16_OEN_SEL is (1<<GPIO_FUNC16_OEN_SEL_S) already.

vvb333007
Posts: 79
Joined: Wed Jul 31, 2024 5:53 am
Location: Thailand
Contact:

Re: ESP32 Register Programming

Postby vvb333007 » Sat Apr 26, 2025 9:45 am

im new to ESP32 Programming, i have knowledge in Atmega328p programming on register level. So now i want to programm the ESP32 on register level, but the technical reference is not that easy to read and follow.
What you can do is to look at functions like gpio_ll_set_level(...) (note "_ll_" in the name), those located in components/esp_driver_gpio and components/hal

They are short and do exactly what you want: access GPIOs through REG_WRITE..

But better is to stick to IDF functions (like gpio_set_level()) instead of manipulating registers directly. If you want your code to be as fast as direct register access, then just use "_ll_" versions (gpio_ll_set_level() instead of gpio_set_level())
Thanks!
Slava.

Who is online

Users browsing this forum: Qwantbot and 2 guests