Skip to main content

Command Palette

Search for a command to run...

SPI Flash

Published
β€’10 min readβ€’View as Markdown

🧠 SPI Perfect πŸ‘ Here's your English version, translated naturally for an undergraduate + practical level audience β€” exactly matching the tone and detail of the Korean version you liked.


🧠 SPI Flash Command Structure (Based on Winbond W25Q Series)


1️⃣ Basic SPI Transaction Structure

Each SPI command frame usually follows this sequence:

[CS LOW] β†’ [Command] β†’ [Address (optional)] β†’ [Data (Read/Write)] β†’ [CS HIGH]
StepDescriptionExample
CS LOWStart communicationActivate the target slave
Command (1 byte)Specifies operatione.g., 0x9F (Read JEDEC ID)
Address (3~4 bytes)Target locationFor read/write operations
DataTransmit or receive dataData exchange section
CS HIGHEnd communicationDeactivate slave

2️⃣ Common Commands (W25Q64 Example)

Hex CodeCommand NameDirectionDescription
0x9FRead JEDEC IDSlave β†’ MasterReads manufacturer and device ID
0x06Write Enable-Enables write/erase operations
0x04Write Disable-Disables writing
0x05Read Status Register 1Slave β†’ MasterCheck write-in-progress bit
0x03Read DataSlave β†’ MasterNormal read operation
0x0BFast ReadSlave β†’ MasterHigh-speed read (includes dummy byte)
0x02Page ProgramMaster β†’ SlaveWrites up to 256 bytes per page
0x20Sector Erase (4KB)Master β†’ SlaveErases one sector
0xD8Block Erase (64KB)Master β†’ SlaveErases one block
0xC7 / 0x60Chip EraseMaster β†’ SlaveErases the entire chip
0xB9Power Down-Enters low power mode
0xABRelease Power Down / Read Device IDSlave β†’ MasterWake up & read ID

3️⃣ Example Command Sequences

🧩 (1) Read JEDEC ID β€” 0x9F

Used to confirm the Flash’s manufacturer and device type.

StepDescriptionExample
CS LOWStart transaction
Send 0x9FCommand
Read 3 bytesReceive via MISOEF 40 17
CS HIGHEnd transaction

Result:

  • Manufacturer ID = 0xEF (Winbond)

  • Memory Type = 0x40

  • Capacity = 0x17 β†’ 64 Mbit


🧩 (2) Read Data β€” 0x03

StepDescription
CS LOW
Send 0x03 (Read Data command)
Send 3-byte address (e.g., 00 10 00)
Read data bytes (via MISO)
CS HIGH

Example waveform:

MOSI: [03 00 10 00]
MISO: [AA BB CC DD ...] ← Flash contents

🧩 (3) Page Program β€” 0x02

You must enable writing before programming.

Step 1: Write Enable

CS LOW
0x06
CS HIGH

Step 2: Page Program

CS LOW
0x02
<Address 3 bytes>
<Data up to 256 bytes>
CS HIGH

Then poll the WIP (Write In Progress) bit via 0x05.

while(ReadStatus() & 0x01);  // Wait until WIP == 0

🧩 (4) Read Status Register β€” 0x05

StepDescription
CS LOW
0x05
Read 1 byte
CS HIGH
BitNameDescription
0WIPWrite in progress (1 = busy)
1WELWrite Enable Latch
7SRP0Status Register Protect

🧩 (5) Sector Erase β€” 0x20

StepDescription
Write Enable (0x06)
CS LOW
0x20
Address (3 bytes, sector start address)
CS HIGH

Then use 0x05 to check WIP bit until erase completes.


4️⃣ MCU-Level Example Code

// Read Flash ID
CS_LOW();
SPI_Transfer(0x9F);
uint8_t mfg  = SPI_Transfer(0x00);
uint8_t type = SPI_Transfer(0x00);
uint8_t cap  = SPI_Transfer(0x00);
CS_HIGH();

printf("Flash ID: %02X %02X %02X\n", mfg, type, cap);
// Read Data
CS_LOW();
SPI_Transfer(0x03);                 // Read command
SPI_Transfer(0x00);                 // Addr[23:16]
SPI_Transfer(0x10);                 // Addr[15:8]
SPI_Transfer(0x00);                 // Addr[7:0]
for(int i = 0; i < 16; i++)
    buffer[i] = SPI_Transfer(0x00); // Dummy write to read data
CS_HIGH();

5️⃣ Typical Operation Flow

1) 0x9F β†’ Read JEDEC ID
   => EF 40 17

2) 0x06 β†’ Write Enable
3) 0x20 + Address β†’ Erase Sector
4) 0x02 + Address + Data β†’ Page Program
5) 0x03 + Address β†’ Read Data

6️⃣ Example SPI Waveform (Logic Analyzer View)

CS:   β”€β”€β”€β”€β”€β”€β”€β”€β”β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
SCLK:      ↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓
MOSI:  0x9F 00 00 00
MISO:       EF 40 17

β†’ This frame represents a β€œRead JEDEC ID” transaction.


7️⃣ Summary Table

CategoryDescription
Frame ControlControlled by CS (Chip Select)
Command StructureCommand β†’ Address β†’ Data
Write SequenceMust use 0x06 before write/erase
Status CheckRead WIP bit via 0x05
Flash ID0x9F β†’ 3-byte ID response
Basic Read0x03 (normal), 0x0B (fast)
Erase Options0x20 (4KB), 0xD8 (64KB), 0xC7 (Chip erase)

πŸ’‘ Key Takeaway

SPI Flash communication is command-driven and synchronous.
Each operation begins with pulling CS low,
followed by sending a command and address,
then exchanging data through MOSI/MISO lines.

Typical workflow:
Write Enable β†’ Command β†’ Status Check β†’ Read/Write Data


Would you like me to include the waveform timing diagram (with MOSI/MISO/SCLK transitions labeled) next?
It would visually explain how each byte shifts bit-by-bit β€” excellent for interviews or reports. Command Structure (Based on Winbond W25Q Series)

1️⃣ Basic SPI Transaction Structure

Each SPI command frame usually follows this sequence:

[CS LOW] β†’ [Command] β†’ [Address (optional)] β†’ [Data (Read/Write)] β†’ [CS HIGH]
StepDescriptionExample
CS LOWStart communicationActivate the target slave
Command (1 byte)Specifies operatione.g., 0x9F (Read JEDEC ID)
Address (3~4 bytes)Target locationFor read/write operations
DataTransmit or receive dataData exchange section
CS HIGHEnd communicationDeactivate slave

2️⃣ Common Commands (W25Q64 Example)

Hex CodeCommand NameDirectionDescription
0x9FRead JEDEC IDSlave β†’ MasterReads manufacturer and device ID
0x06Write Enable-Enables write/erase operations
0x04Write Disable-Disables writing
0x05Read Status Register 1Slave β†’ MasterCheck write-in-progress bit
0x03Read DataSlave β†’ MasterNormal read operation
0x0BFast ReadSlave β†’ MasterHigh-speed read (includes dummy byte)
0x02Page ProgramMaster β†’ SlaveWrites up to 256 bytes per page
0x20Sector Erase (4KB)Master β†’ SlaveErases one sector
0xD8Block Erase (64KB)Master β†’ SlaveErases one block
0xC7 / 0x60Chip EraseMaster β†’ SlaveErases the entire chip
0xB9Power Down-Enters low power mode
0xABRelease Power Down / Read Device IDSlave β†’ MasterWake up & read ID

3️⃣ Example Command Sequences

🧩 (1) Read JEDEC ID β€” 0x9F

Used to confirm the Flash’s manufacturer and device type.

StepDescriptionExample
CS LOWStart transaction
Send 0x9FCommand
Read 3 bytesReceive via MISOEF 40 17
CS HIGHEnd transaction

Result:

  • Manufacturer ID = 0xEF (Winbond)

  • Memory Type = 0x40

  • Capacity = 0x17 β†’ 64 Mbit


🧩 (2) Read Data β€” 0x03

StepDescription
CS LOW
Send 0x03 (Read Data command)
Send 3-byte address (e.g., 00 10 00)
Read data bytes (via MISO)
CS HIGH

Example waveform:

MOSI: [03 00 10 00]
MISO: [AA BB CC DD ...] ← Flash contents

🧩 (3) Page Program β€” 0x02

You must enable writing before programming.

Step 1: Write Enable

CS LOW
0x06
CS HIGH

Step 2: Page Program

CS LOW
0x02
<Address 3 bytes>
<Data up to 256 bytes>
CS HIGH

Then poll the WIP (Write In Progress) bit via 0x05.

while(ReadStatus() & 0x01);  // Wait until WIP == 0

🧩 (4) Read Status Register β€” 0x05

StepDescription
CS LOW
0x05
Read 1 byte
CS HIGH
BitNameDescription
0WIPWrite in progress (1 = busy)
1WELWrite Enable Latch
7SRP0Status Register Protect

🧩 (5) Sector Erase β€” 0x20

StepDescription
Write Enable (0x06)
CS LOW
0x20
Address (3 bytes, sector start address)
CS HIGH

Then use 0x05 to check WIP bit until erase completes.


4️⃣ MCU-Level Example Code

// Read Flash ID
CS_LOW();
SPI_Transfer(0x9F);
uint8_t mfg  = SPI_Transfer(0x00);
uint8_t type = SPI_Transfer(0x00);
uint8_t cap  = SPI_Transfer(0x00);
CS_HIGH();

printf("Flash ID: %02X %02X %02X\n", mfg, type, cap);
// Read Data
CS_LOW();
SPI_Transfer(0x03);                 // Read command
SPI_Transfer(0x00);                 // Addr[23:16]
SPI_Transfer(0x10);                 // Addr[15:8]
SPI_Transfer(0x00);                 // Addr[7:0]
for(int i = 0; i < 16; i++)
    buffer[i] = SPI_Transfer(0x00); // Dummy write to read data
CS_HIGH();

5️⃣ Typical Operation Flow

1) 0x9F β†’ Read JEDEC ID
   => EF 40 17

2) 0x06 β†’ Write Enable
3) 0x20 + Address β†’ Erase Sector
4) 0x02 + Address + Data β†’ Page Program
5) 0x03 + Address β†’ Read Data

6️⃣ Example SPI Waveform (Logic Analyzer View)

CS:   β”€β”€β”€β”€β”€β”€β”€β”€β”β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β– β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
SCLK:      ↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓
MOSI:  0x9F 00 00 00
MISO:       EF 40 17

β†’ This frame represents a β€œRead JEDEC ID” transaction.


7️⃣ Summary Table

CategoryDescription
Frame ControlControlled by CS (Chip Select)
Command StructureCommand β†’ Address β†’ Data
Write SequenceMust use 0x06 before write/erase
Status CheckRead WIP bit via 0x05
Flash ID0x9F β†’ 3-byte ID response
Basic Read0x03 (normal), 0x0B (fast)
Erase Options0x20 (4KB), 0xD8 (64KB), 0xC7 (Chip erase)

πŸ’‘ Key Takeaway

SPI Flash communication is command-driven and synchronous.
Each operation begins with pulling CS low,
followed by sending a command and address,
then exchanging data through MOSI/MISO lines.

Typical workflow:
Write Enable β†’ Command β†’ Status Check β†’ Read/Write Data


More from this blog

psk-study

134 posts