Page 1 of 1

What happens to GPIO output pin during reboot?

Posted: Wed Nov 27, 2024 6:06 pm
by jcolebaker
Hi,

We are using a ESP32 WROOM module and the design uses a "power enable" signal to keep the power supply turned on. This is wired to GPIO21 on our ESP32.

I set this pin high at start up using a bootloader hook, to make sure that the power supply stays on. It's also set high by the main firmware when it boots up. This works well.

However, I want to be sure about what happens when the ESP32 is rebooted with "esp_restart()", which triggers a reboot with the boot reason SW_CPU_RESET. If GPIO21 goes low for more than ~1.5 microseconds, the power supply will shut down and the user will need to press the power button again. This is undesirable (e.g. when installing a firmware update).

The technical reference manual says that pin GPIO21 is set up as an input after reset (oe=0, ie=0 at reset, then oe=0, ie=1). This means I would expect the pin to briefly toggle LOW when the reset happened. However, I can't see this on my scope; it seems to remain high.

In other words, it is working OK but I would like to be sure. Would the output pin be expected to reliably hold its state while the ESP32 reboots?

Re: What happens to GPIO output pin during reboot?

Posted: Thu Nov 28, 2024 1:05 am
by igormoo
Any chance you can share a snippet of your bootloader hook?

Re: What happens to GPIO output pin during reboot?

Posted: Thu Nov 28, 2024 5:52 am
by boarchuz
CPU reset does not affect GPIOs. See TRM 3.1.1.

Re: What happens to GPIO output pin during reboot?

Posted: Thu Nov 28, 2024 8:37 pm
by MicroController
You may want to look into gpio_hold_en() &c. to make sure nothing messes with your desired pin state.

Re: What happens to GPIO output pin during reboot?

Posted: Mon Dec 02, 2024 3:16 am
by jcolebaker
igormoo wrote:
Thu Nov 28, 2024 1:05 am
Any chance you can share a snippet of your bootloader hook?
Here's my bootloader hook to set GPIO 21 high at boot time.

Code: Select all

// Base address of GPIO registers:
#define GPIO_REG_BASE_ADDR 0x3FF44000

// GPIO Output Register Offsets:
#define GPIO_OUT_REG      (GPIO_REG_BASE_ADDR + 0x04)  // Write GPIO values
#define GPIO_ENABLE_REG   (GPIO_REG_BASE_ADDR + 0x20)  // Enable GPIO pins

// Bit position for GPIO 21 (POWER_HOLD):
#define GPIO21_BIT (1 << 21)


/**
 * @brief Empty function to force this file to be included by the linker.
 * The bootloader hooks are declared as "weak", so they will be ignored unless
 * "bootloader_hooks_include" is also declared.
 */
void bootloader_hooks_include(void) {}


/**
 * @brief Bootloader "Before Init" hook implementation.
 * This function will be called before the bootloader initialisation starts
 * (i.e. at the start of the 2nd stage bootloader).
 */
void bootloader_before_init(void)
{
    // Enable the GPIO pin (set it as an output):
    *(volatile uint32_t *)GPIO_ENABLE_REG |= GPIO21_BIT;

    // Set the GPIO pin high:
    *(volatile uint32_t *)GPIO_OUT_REG |= GPIO21_BIT;
}
It's pretty crude - just toggles register bits to make the pin an output and set it high. It works well, though.

This is placed in a file called bootloader_components/boot_hooks/hooks.c

The bootloader_components/boot_hooks folder also contains a CMakeLists.txt file with the following:

Code: Select all

idf_component_register(SRCS "hooks.c")

Re: What happens to GPIO output pin during reboot?

Posted: Mon Dec 02, 2024 3:25 am
by jcolebaker
MicroController wrote:
Thu Nov 28, 2024 8:37 pm
You may want to look into gpio_hold_en() &c. to make sure nothing messes with your desired pin state.
Interesting - thanks!

Re: What happens to GPIO output pin during reboot?

Posted: Mon Dec 02, 2024 3:26 am
by jcolebaker
boarchuz wrote:
Thu Nov 28, 2024 5:52 am
CPU reset does not affect GPIOs. See TRM 3.1.1.
Thanks - I think it's TRM section 4.1.1? (System Reset). I had missed that bit.