I am using XiP with partial code running from MX25. I am facing issue where I am getting hard faults when Erase is happening and at the same time the ISR from XiP region is triggered.
But in scenarios where Erase is happening and code executing from XIP is called then there is no hard fault, why?
This is my understanding until now:
ISRs are triggered asynchronously and immediately, regardless of flash state - whereas context switches happen under controlled condition, usually after the flash operation completes or from RAM/internal flash.
-
ISRs are truly asynchronous and can interrupt a flash operation
-
if an interrupt fires during a flash erase/write and the ISR is in external flash then:
-
CPU tries to fetch instruction from external flash while it’s busy.
-
Result: instruction fetch fails → Hard fault/ Bus fault
-
-
-
Context switches are usually software driven
-
Zephyr usually triggers a context switch:
-
Inside the kernel thread loop
-
After a system call (k_sleep(), k_yield() etc.)
-
From a schedular decision after an interrupt returns.
-
-
These typically happen after an ongoing flash operation finishes or from running internal flash or RAM.
-
So when the schedular does k_context_swithc(), the instruction fetches for the context switch logic, and even the thread code, may not collide with a write/erase in progress.
-
-
ISRs happen at any time
-
if a flash write takes 40ms and a HW interrupt fires at 10ms:
-
ISR jumps immediately to external flash
-
-
Context switches don’t happen “at any time” - they occur between tasks, and Zephyr ensures (unless interrupted) that the system is in a consistent state.
-
-
Stack and code location differences
-
ISRs are more likely to run from external flash directly if not relocated
-
Context switch mechanism/kernel code resides in internal flash.
-
please let me know if there is gap in my understanding and also give the exact answer.
I am using Nordic 2.7.0 SDK