When a flash is beeing written/erased, it can not be read at the same time.
But to execute some code from a flash, a CPU need to fetch (read) the code from this flash.
So as a result, to erase or write a flash when the code is running from the same flash :
- the small code launching the real erase/write and waiting for completion must be copied to RAM run from RAM. The code jumps from flash to this code in RAM, then returns to flash when done.
- the interrupts must be disabled to prevent an interrupt from branching the CPU to an interrupt handler in flash, and to prevent the task from beeing preempted by another running from flash.