Section 6.6

Flash memory

Definition

Flash memory is memory that keeps its content without power and is changed by two separate operations. An erase sets every bit of a whole block to 1, the usual erased value. A program operation, also called a write, then changes selected bits from 1 to 0. It cannot change a 0 back to 1. Each block survives only a limited number of erases. A microcontroller §6.1, a small computer on one chip, keeps its program, called firmware, and often its lasting data in flash memory.

The idea

With working memory, a program stores a value at an address, any number of times, and the old value is gone. Flash memory does not work that way, and firmware that treats it like working memory loses data.

A sheet of paper and an ink stamp give the right picture. Ink can be added at any spot at any time: that is programming a bit from 1 to 0. The only way to remove ink is to bleach the whole sheet, which is the erase, and a sheet survives only so many bleachings. The cause lies in how a bit is stored: as electric charge trapped on an insulated part of a transistor; one such transistor is a cell. Programming puts charge on single cells. Erasing removes the charge, and the chip can do that only for a whole block of cells at once. The picture stops in one respect. A half-bleached sheet still shows what was on it. A block whose erase was cut short by a power failure holds undefined content.

How it works

A few numbers describe a flash device. The values here are those of the flash memory in the nRF52840 §6.4, a chip from Nordic Semiconductor. Its specification allows any number of reads.

Changing stored data in place takes these steps whenever some bit has to go from 0 to 1.

  1. Copy the bytes of the erase block that must survive to another place, usually RAM.

  2. Erase the block. Every bit in it becomes 1.

  3. Program the new content and the saved bytes.

From step 2 until step 3 is finished, the old data exists nowhere in flash memory; a power failure loses it. Storage software therefore avoids changing data in place: it writes the new version to a part that is already erased, marks it valid when the write is complete, and erases the old version later. The entry on crash consistency §8.4 treats such designs.

The two main kinds, NOR flash and NAND flash, differ in how the cells are wired together. NOR flash can be read directly at any address, so a processor can run a program straight from it. NAND flash packs more bits into the same area and suits bulk storage; it is read in larger units, can contain bad blocks and needs error-correcting codes, stored check bits that repair wrong bits.

Worked example

A byte in flash memory holds 0xA5, in bits 1010 0101.

On the nRF52840, storing 0xC3 there needs an erase and a rewrite of the page that contains the byte. The erase takes up to 85 ms. Writing the 1024 words of the page takes up to 1024×41=41,9841024 \times 41 = 41{,}984 microseconds. Changing one byte costs up to 127 ms and one of the page’s 10,000 guaranteed erases. The ratio of bytes written to bytes changed, here 4096 to 1, is called write amplification §8.18.

Firmware that stores a counter once a minute, and erases the page each time, uses up 10,000 erases in 10,000 minutes, about 6.9 days. Firmware that appends each new 8-byte value to the page, and erases only when the page is full, stores 4096/8=5124096 / 8 = 512 values per erase. It reaches the limit after 512×10,000=5,120,000512 \times 10{,}000 = 5{,}120{,}000 minutes, about 9.7 years.

Stated precisely

Let oo be the bits that a write unit holds and nn the bits to be stored. A program operation leaves the unit holding o&no \mathbin{\&} n, where &\& is the bitwise AND §4.50: a result bit is 1 only if that bit is 1 in both oo and nn. The unit holds nn afterwards exactly when o&n=n,o \mathbin{\&} n = n, that is, when nn has no 1 in a position where oo has a 0. In every other case the erase block that contains the unit must be erased first.

This rule assumes that erased bits read as 1, as they do on the nRF52840. The sizes, limits and times of a device, and what it promises when power fails during an operation, come from its datasheet. The times given here for the nRF52840 hold while its clock comes from a quartz crystal. Unless the datasheet promises more, a unit whose erase or program operation was cut short holds unknown content. Durability §8.2, the property that stored data survives a power failure, begins only when the operation that stores the data has completed.

Limits and common mistakes

Flash memory cannot be written like a variable. On the nRF52840 it is read-only until the firmware switches the flash controller, the circuit that erases and writes, to write mode or to erase mode.

Data that waits in a RAM buffer is not stored yet. A power failure loses it.

A checksum §4.32, a short check value stored with the data, can detect a torn write §8.19, which is a write that was cut short. It does not bring the old data back.

Count erases per block over the whole life of the product, not writes per byte; see the entry on flash wear and endurance §8.17.

Content kept without power is not kept for ever. The product specification of the nRF52840 states a data retention of 10 years at 85 degrees Celsius.

Questions answered

Can a byte be rewritten arbitrarily without erasing its containing region?

No: without an erase, a write to flash memory can only change bits from 1 to 0. A stored byte can therefore be changed in place only to a value that has no 1 where the stored byte has a 0: 0xA5 can become 0xA4 or 0x00, but not 0xC3. Every other change needs an erase of the whole erase block that contains the byte, which sets all bits of the block to 1. On the nRF52840 chip that block is a page of 4096 bytes. Writing the byte without the erase leaves a mixture: a bit is 1 only where both the old and the new value have a 1.

The device restricts even the permitted changes. The nRF52840 writes whole 32-bit words at addresses divisible by 4, and it allows a word to be written at most twice between two erases of its page. Because an erase destroys the old content before the new content is written, a power failure in between loses both. Storage software therefore writes the new value to a place that is already erased.

Related entries: Crash consistency (§8.4), Flash wear and endurance (§8.17), Torn write (§8.19), Write ordering (§8.20), Durability (§8.2), Write amplification (§8.18), NVMC (Non-Volatile Memory Controller) (§16.3), SRAM (§6.7). Further reading: [56].