ESP32 "Cache disabled but cached memory region accessed"

While the ESP32 writes or erases its flash, code and constants in flash cannot be read. An interrupt that fires then and touches flash ends in this panic.

Guru Meditation Error: Core  0 panic'ed (Cache error).
Cache disabled but cached memory region accessed

Newer chips can print a more specific reason in place of the second line, with the address that was accessed.

What it means

From Espressif's Fatal Errors guide: when ESP-IDF reads, writes, erases or maps flash, it turns the flash cache off. Tasks are suspended and interrupt handlers that are not registered with ESP_INTR_FLAG_IRAM are disabled. An IRAM interrupt handler that runs during that time must have all its code and data in internal RAM.

The usual causes

Find the line

The Backtrace: line is a list of PC:SP pairs, innermost call first. To turn the addresses into functions and lines you need the ELF of exactly the build that crashed:

When it happens in the field

On your desk the crash prints to the serial monitor. On a device in someone else's building it prints to a port nobody is reading, the chip restarts, and all you hear is that it "sometimes hangs". Pinscope keeps the panic reason and backtrace across the restart, sends them on the next connection and decodes them with the ELF of the build that crashed, so you get the function and line without anyone at the device. Free for three devices. How crash reporting from the field works.

Related errors: Interrupt wdt timeout · IllegalInstruction · Task watchdog got triggered · rst:0x… reset reasons. All ESP32 errors: the list. Messages and causes from ESP-IDF's panic handler and Espressif's Fatal Errors guide.

More