ESP32C3 forever loop at boot

zwer97
Posts: 11
Joined: Wed Dec 25, 2019 5:43 pm

ESP32C3 forever loop at boot

Postby zwer97 » Fri Aug 19, 2022 11:53 am

I have a custom board on ESP32C3, with possibility to fall into a deep sleep by external signal. As a source for power in deep sleep a 0.22F super capacitor is used. But sometimes when ESP goes out of a deep sleep when it's powered from super capacitor only, it falls into a forever loop on the next power up. Here are logs:

Code: Select all

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x5 (DSLEEP),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
Valid secure boot key blocks: 0
ÿESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0xf (BROWNOUT_RST),boot:0x0 (USB_BOOT)
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
To start it again I have to wait until super capacitor is fully discharged, then power it up again.
I use ESP-IDF V4.3.2. IO9 (boot selection) is pulled up to the ESP VDD with 5.1k.
Secure boot V2 and flash encryption are enabled.
How can I avoid this behavior? Maybe it is possible to disable USB_BOOT?

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

Re: ESP32C3 forever loop at boot

Postby ESP_Sprite » Fri Aug 19, 2022 12:48 pm

BROWNOUT_RST <- too little voltage on the ESP32 power pins to properly boot.

zwer97
Posts: 11
Joined: Wed Dec 25, 2019 5:43 pm

Re: ESP32C3 forever loop at boot

Postby zwer97 » Fri Aug 19, 2022 1:54 pm

I know that the battery was discharged and why BROWNOUT_RST was triggered. But later I've connected normal voltage and got stuck in forever loop:

Code: Select all

rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
This is potentially dangerous situation, when device will hang with power connected.
The question is why it hangs in forever loop and can't boot the image?
Here is the log with comments what I did:

Code: Select all

/*Here I disconnect voltage source, ESP got sleep external signal and fall in DEEP SLEEP on super capacitor*/
/*but some noise signal triggered ESP wake up right after falling to sleep*/

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x5 (DSLEEP),boot:0xc (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
Valid secure boot key blocks: 0
ÿESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021

/*obviously super capacitor discharged and I've got brownout reset. External power supply is still disconnected*/

rst:0xf (BROWNOUT_RST),boot:0x0 (USB_BOOT)
Invalid mode for secure boot download

/*here, after couple seconds I connect external power supply again, but ESP hangs in forever loop*/
/*the only way to out it is to disconnect power supply, fully discharge super capacitor and connect power again*/

ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x0 (USB_BOOT)
Saved PC:0x40048b82
Invalid mode for secure boot download

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

Re: ESP32C3 forever loop at boot

Postby ESP_Sprite » Sat Aug 20, 2022 2:34 am

How are your bootstrap pins (GPIO9 specifically) connected?

zwer97
Posts: 11
Joined: Wed Dec 25, 2019 5:43 pm

Re: ESP32C3 forever loop at boot

Postby zwer97 » Sat Aug 20, 2022 2:46 pm

Here is a simplified schematic. IO9 is pulled up with 5.1k. ESP_EN is pulled up with 5.1k + capacitor to add delay for enable signal. IO8 and IO2 may have undefined states at startup in my schematic, as IO2 is used for analog measurements and IO8 for LED control.

P.S. I've tried this, but it didn't help.

Code: Select all

esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE); /*bricked device with secure boot v2 enabled*/
esp_efuse_write_field_bit(ESP_EFUSE_DIS_USB_DOWNLOAD_MODE); /*didn't help*/
esp_efuse_write_field_bit(ESP_EFUSE_DIS_FORCE_DOWNLOAD); /*didn't help*/
esp_efuse_write_field_bit(ESP_EFUSE_DIS_USB); /*didn't help*/
esp_efuse_write_field_bit(ESP_EFUSE_DIS_LEGACY_SPI_BOOT);/*didn't help*/
Also tried disabling Hardware brownout detect & reset in menuconfig, didn't help either.

I've noticed that this problem happens only when super capacitor is not fully discharged, so maybe at brownout reset it falls into a boot mode somehow. I need to disable this behavior, but how?
Attachments
Screenshot from 2022-08-20 17-43-22.png
Screenshot from 2022-08-20 17-43-22.png (74.41 KiB) Viewed 9404 times

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

Re: ESP32C3 forever loop at boot

Postby ESP_Sprite » Sun Aug 21, 2022 2:14 am

Well, at least one issue I can find is that the simple RC circuit you have on EN may not give you a proper reset signal when the voltage on your supercap is gradually increasing/decreasing. The thing is that if the voltage on the ESP32C3 is lower than a given level, its state becomes random and it needs a proper reset (as in: EN taken low and high again) before it's guaranteed to function. The RC circuit is generally used because in most situations, this only happens on powerup (as in: the 3.3V rail suddenly goes from 0V to 3.3V) but in your situation, you may need something more complicated like a voltage monitor.

It's still odd that you get the USB download mode as the bootup mode even with GPIO9 connected high, but given that the bootstrap pins are read on hardware reset, it's not entirely unexpected either.

Note that disabling the brownout detector likely won't help you there, as the ESP32 will still reset (but in a way more unpredictable way, likely by crashing) if the power level goes too low.

zwer97
Posts: 11
Joined: Wed Dec 25, 2019 5:43 pm

Re: ESP32C3 forever loop at boot

Postby zwer97 » Sun Aug 21, 2022 3:42 pm

Aha, now I understand the problem from the hardware side. So may it be somehow solved by software with no hardware changes? Maybe some efuse?
Also how from the hardware side would it be correct to provide deep sleep on a super capacitor? I can't generate a reset signal at every wake up, otherwise there will be no use from the deep sleep itself.

Edit: I've made couple measurements of the IO9 and ESP_EN signals, and it looks like the problem happens when power is disconnected.
first_power_on.png
first_power_on.png (1008.01 KiB) Viewed 9293 times
second_power_on.png
second_power_on.png (959.11 KiB) Viewed 9293 times
third_power_on_longer.png
third_power_on_longer.png (891.52 KiB) Viewed 9292 times

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

Re: ESP32C3 forever loop at boot

Postby ESP_Sprite » Mon Aug 22, 2022 1:19 am

Can you explain a bit more about your use case, as in: what device are you trying to build here and how should it behave?

zwer97
Posts: 11
Joined: Wed Dec 25, 2019 5:43 pm

Re: ESP32C3 forever loop at boot

Postby zwer97 » Mon Aug 22, 2022 1:42 pm

This is a 220V AC powered device. It contains an AC to DC converter and should fall into a deep sleep when AC power is disconnected. There is a tested schematic, which generates a signal when AC voltage disappears to go to sleep, and generates signal to wake up when AC voltage is back. But sometimes a noise signal may lead the device to wake up when no AC power is present, in this case a described bug may appear. The schematic is nearly finished and prepared for production, so it would be great to have a software solution.

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

Re: ESP32C3 forever loop at boot

Postby ESP_Sprite » Tue Aug 23, 2022 1:40 am

Sorry, I don't see how a software solution is possible here. First of all, you're already running the ESP32C3 outside of its specs: the datasheet says 3.0-3.6V and according to the scope trace, you're running it on 2.5V, or even 2.0V if the supercap goes empty. That means any guarantee the datasheet implies wrt behaviour goes out of the window. Secondly, the entire issue is that the internal state of the ESP32C3 gets corrupted: even if software would be able to get you out of this problem, you have no guarantee it can properly run in this state.

I'm decently sure at this point you need some hardware modifications. I'd suggest at least a voltage monitor that keeps the ESP32C3 in reset if the voltage is too low, so you know it's always working within specs if not in reset. That does mean that you can't store variables in RTC memory anymore, so you may need some other solution for that as well. If you do want to make use of deep sleep, you need to make sure the software and hardware works in such a way that the ESP32 is guaranteed to go into deep sleep fast after power is gone, and that deep sleep is only exited when the power is back properly. Even then, beware of edge conditions: you may have a power cut again just at the point where the ESP32 comes out of deep sleep, so you need to make sure you have enough power to let it start up, detect there's no power anymore, and go to deep sleep.

Who is online

Users browsing this forum: No registered users and 86 guests