Flash memory is a widely used non-volatile storage technology found in microcontrollers, SSDs, USB drives, and many other electronic devices. Non-volatile means it retains data even when the power is turned off. However, there is an important limitation: Flash memory erase before write is mandatory. This means that before you can write new data to a flash memory block, you must first erase the existing data.
But why is erasing necessary before writing?
In this post, we’ll explain the reason behind this requirement in simple and easy-to-understand terms. Before diving into the details, let’s take a quick look at what flash memory is and how it works.
What Is Flash Memory?
Flash memory uses tiny cells to store data as electrical charges. Each cell holds a 0 or 1, depending on the charge. Cells are grouped into pages (small chunks) and blocks (larger chunks).
How Does Flash Memory Store Data?
Flash memory stores data using electrical charges in tiny cells made of floating-gate transistors. Each cell can trap or release electrons to represent binary values—0s and 1s—which form the basis of all data stored in flash memory.
- In a Single-Level Cell (SLC), each cell stores one bit of data.
- In Multi-Level Cell (MLC) and Triple-Level Cell (TLC) designs, each cell can store multiple bits by distinguishing more charge levels.
The logic behind storing bits is based on the presence or absence of charge in the floating gate:
- A bit set to 1 means no charge is stored in the floating gate.
- A bit set to 0 means the charge is trapped in the gate.
There are two main types of flash memory:
- NOR Flash: Fast for reading, used in code storage (like firmware).
- NAND Flash: Better for large data storage (like USB drives and SSDs).
Note: Flash memory is non-volatile, which means it retains data even when the power is turned off.
Heat Generation: Writing 0s vs 1s?
Writing a 0 (which involves injecting charge into the floating gate) generally consumes more energy and can generate more heat than writing a 1 (which involves leaving the gate uncharged). This is because programming a cell to 0 requires a high-voltage process called Fowler-Nordheim tunneling, where electrons tunnel through an insulating layer to become trapped in the floating gate—a power-intensive operation. Writing a 1 does not require this charge injection, making it less energy-demanding.
You can also check this paper.
Now, let’s come to the points that explain why flash memory needs to be erased before writing.
Unlike traditional hard drives or RAM, flash memory cannot overwrite existing data directly. To write new data, the memory cells must first be erased to reset them to a known state. This is because of how flash memory stores and handles electrical charges.
Why Can’t Flash Overwrite Data Directly?
- Data is Stored as Electrical Charge:
Each flash memory cell holds data by trapping electrical charge in a tiny part called the floating gate. A charged gate might mean a ‘0’, while no charge might mean a ‘1’ (or vice versa). - One-Way Charge Manipulation:
Writing data usually means adding electrons to the floating gate to change a bit from 1 to 0. However, flash memory hardware cannot selectively remove electrons from individual cells during a write. Removing a charge can only be done by erasing. - Erasing Happens in Blocks:
Flash memory erases entire blocks or sectors at once, not individual cells. Erasing clears all the charge in that block, resetting all bits to ‘1’. After this, new data can be written by adding a charge to specific cells.
This erase-before-write rule is a key part of how flash memory works. It also explains why flash devices use special techniques like wear leveling and garbage collection to keep them working well and lasting longer.
❌ What Happens If You Skip Erasing?
If you try to write to an already-written location:
- Any bits already 0 cannot return to 1.
- The resulting data may be corrupted or invalid.
For example:
Original Byte | New Data | Result | Valid? |
---|---|---|---|
0xFF | 0x55 | 0x55 | ✅ Yes |
0x55 | 0xAA | 0x00 | ❌ No |
This is why writing to flash is not like writing to RAM—it’s more like writing with a permanent marker: you can darken it further, but you can’t erase it unless you clean the board.
Flash Erase Process:
Erasing a flash memory block involves applying a high voltage to force electrons out of the floating gates, resetting the cells to their “erased” state (all 1s). This is done using a process called Fowler-Nordheim tunneling, which clears the charge across an entire block. You can refer to the video mentioned above for more details. Only after this can new data be written by selectively charging cells to represent 0s.
🧠Flash Write Cycle: The Correct Sequence:
- Erase the flash sector or block.
- Program (write) the desired data.
- Verify the write if necessary.
Skipping the erase step risks data corruption and unpredictable behavior, especially in embedded applications.