Flash Memory Principles and Embedded Read/Write Practices: NOR vs. NAND, EEPROM Comparison, and SPI Flash Driver Design

    This article explains how Flash memory works and how to read from and write to it in embedded systems. It covers the difference between NOR and NAND Flash, a feature comparison between Flash and EEPROM in terms of density, speed, erase granularity, and protocol complexity, the step-by-step command sequences for reading and writing a 32 Mbit SPI NOR Flash device, and the low-level driver functions (SPI byte transfer, busy polling, read, and write) needed to build a reliable off-chip storage layer.

    1. Flash Memory Overview

    Flash memory, commonly called flash, is a non-volatile memory technology that retains data after power is removed. Based on differences in structure and access method, Flash is mainly divided into two categories: NOR Flash and NAND Flash. NOR Flash supports random access and offers fast reads, so it is typically used to store program code. NAND Flash provides higher storage density at a lower cost per unit, which makes it suitable for large-capacity data storage.

    Flash is now present in nearly every modern electronic system. In consumer electronics, phones, tablets, and digital cameras rely on it heavily; in industrial settings, PLC controllers and data acquisition terminals depend on it; and automotive electronics and aerospace equipment use it as well. The on-chip program memory of current mainstream microcontrollers is also built on Flash technology, which shows that Flash has become the storage foundation of embedded systems.

    2. Flash vs. EEPROM: Key Differences

    In embedded applications, EEPROM is another common non-volatile storage option. Understanding the differences between the two is the prerequisite for making a sound selection.

    2.1 Storage Density and Capacity

    Flash has a clear advantage in storage density. Take a common 8051 development board as an example: its onboard SPI Flash chip has a capacity of 32 Mbit (4 MB), while a comparable I2C EEPROM device typically offers only 2 Kbit (256 bytes). The capacities differ by more than four orders of magnitude, yet the price differs by only a few times. For scenarios that need to store large blocks of data such as font libraries, images, and logs, Flash is the more economical choice.

    2.2 Read and Write Speed

    Flash is generally faster to read and write than EEPROM. NOR Flash in particular reads at speeds close to ordinary SRAM, which makes it well suited to cases that require frequent reads or fast loading of code and data.

    2.3 Erase Granularity and Flexibility

    EEPROM allows writes at byte granularity, so any single byte can be modified flexibly. Flash, by contrast, must be erased before it can be written, and erasure happens at sector granularity (typically 4096 bytes). After erasure the entire sector is set to all 1s, and only then can new data be written. This means that changing a single byte in Flash theoretically requires a “read the whole sector - erase - modify - write back” sequence. As a result:

    • EEPROM fits applications that require frequent, small-granularity updates, such as system configuration parameters.
    • Flash fits applications with bulk writes and a read-heavy, write-light pattern, such as firmware, image resources, and archived measurement data.

    2.4 Protocol Complexity

    The read and write protocol of Flash is somewhat more complex than that of EEPROM. Before each operation, the host controller must first send a specific command to the Flash device that states whether the operation is a read or a write, and in the case of a write, whether it targets the status register or the data array. In addition, every write operation must be preceded by a “write enable” command; otherwise the write has no effect. The device datasheet contains a complete command table with a description of each command, so implementations should follow it strictly during development.

    3. Reading from SPI Flash

    Using a 32 Mbit SPI NOR Flash device as an example, the read sequence is as follows:

    1. Check the busy status: read the status register to confirm the chip is not in the middle of an erase or program operation.
    2. Pull the chip select pin low and send the “read data” command (typically 0x03).
    3. Send the 24-bit starting address. Note that 32 Mbit equals 4 M bytes, so the address space is 0x000000 to 0x3FFFFF. The address must therefore be sent in three separate bytes, most significant byte first.
    4. Keep the clock running and read out the requested number of data bytes in sequence.
    5. Pull the chip select pin high to end the read.

    A read operation can continue across page and sector boundaries. As long as the clock keeps running, the chip automatically increments its internal address pointer.

    4. Writing to SPI Flash

    Writes to Flash are constrained by the page. A page is usually 256 bytes, and the data written by a single page program command must not cross a page boundary; otherwise the address wraps back to the start of the same page and overwrites existing data. The typical sequence is:

    1. Check the busy status to confirm the chip is idle.
    2. Send the “write enable” command to set the write enable flag in the status register.
    3. Send the “page program” command.
    4. Send the 24-bit starting address.
    5. Send the data to be written continuously (no more than one page at a time).
    6. Pull chip select high, then wait for programming to complete by polling the busy status.

    If more than one page of data must be written, or if the target sector already holds old data, the software has to handle paging and schedule a “read - erase - write” cycle.

    5. Designing the Low-Level Functions

    In real driver development, the sequences above are usually wrapped into a few basic functions:

    • SPIRead / SPIWrite: send and receive a single byte according to SPI timing. These are the lowest-level communication primitives.
    • FlashBusyWait: the busy-status polling function. Flash needs a certain amount of time (from milliseconds to seconds) to complete page programming, sector erasure, and block erasure. During that time the chip is busy and does not respond to any read or write command. This function should therefore be called before every read and write to poll the busy flag in the status register and ensure reliable operation.
    • FlashRead(addr, buf, len): wraps the complete read sequence.
    • FlashWrite(addr, buf, len): wraps write enable, page programming, and the cross-page splitting logic.

    Illustrative pseudocode:

    1. void FlashWrite(uint32_t addr, uint8_t *buf, uint16_t len)
    2. {
    3. while (len > 0) {
    4. FlashBusyWait(); // wait until idle
    5. FlashWriteEnable(); // write enable
    6. CS_LOW();
    7. SPIWrite(CMD_PAGE_PROGRAM);
    8. SPIWrite(addr >> 16); // 24-bit address
    9. SPIWrite(addr >> 8);
    10. SPIWrite(addr);
    11. while (len-- && offset++ < PAGE_SIZE) {
    12. SPIWrite(*buf++);
    13. }
    14. CS_HIGH();
    15. addr += offset;
    16. }
    17. }

    6. Summary

    With high density, fast reads, and low cost, Flash has become an indispensable storage medium in embedded systems, while EEPROM still holds its place for parameter storage thanks to byte-level erase and write flexibility. Developers should choose between them based on how frequently data is updated and how much capacity is required, follow the operation timing of “busy check - write enable - command - address - data” strictly, and properly handle sector erasure and cross-page writes in software. Only then can a stable and reliable off-chip storage solution be built.