THANK YOUClick the blue text to follow me
In STM32 Flash memory, sectors (Sector), pages (Page), and blocks (Block) are hierarchical concepts based on storage management needs, with the core goal of balancing “erase efficiency” and “storage flexibility”—the physical characteristics of Flash dictate thatit can only be erased in fixed minimum units (data becomes all 1s after erasure) but can be programmed (written 1→0) by byte/half-word/word, thus requiring a layered design to accommodate different read and write requirements.
1. Core Premise: Physical Characteristics of Flash (Key to Understanding Layering)
STM32 Flash belongs toNOR Flash (some high-capacity models include NAND Flash, but system Flash is mostly NOR), with the following core constraints:
-
Programming (Write): Can only rewrite an erased “1” to “0”; cannot directly rewrite “0” to “1”;
-
Erasure (Erase): Must be executed according to a fixed “minimum erase unit”; after erasure, all bits in that unit revert to “1”;
-
Layered Design: The physical “minimum erase unit” may be large (e.g., 16KB), and directly operating on this would waste space, hence finer granularity “pages” are introduced for programming, while “sectors/blocks” are used for erasure, forming a separation between “physical erase units” and “logical programming units”.
2. Precise Definitions and Distinctions of the Three Concepts
| Concept | Physical / Logical Attributes | Core Function | Operation Limitations | Size Range (Typical Values for STM32) |
|---|---|---|---|---|
|
Page (Page) |
Logical / Physical Unit |
Minimum Programming Unit (write operation) |
Can only program (write 1→0), cannot be erased individually |
128B, 256B, 512B (e.g., STM32F1/F4) |
|
Sector (Sector) |
Physical Unit |
Minimum Erase Unit (basic level) |
Must erase the entire sector; after erasure, can program by page |
4KB, 8KB, 16KB, 64KB, 128KB |
|
Block (Block) |
Physical / Logical Unit |
Collection of multiple sectors (high-level erasure) |
Entire block erasure (more efficient than multiple sector erasures) |
64KB, 128KB, 256KB (e.g., STM32H7) |
Simple Summary:
-
Page = “Writing Grid”: The smallest unit of writing, each write operation occupies at least 1 page (even if only writing 1 byte, it occupies 1 page of space, and the remaining part cannot be written again unless erased);
-
Sector = “Whole Sheet of Paper”: The smallest “erasable paper”; to modify any “grid” (page) content on the paper, the entire sheet (sector) must first be erased (all 1s), then rewrite the required “grid”;
-
Block = “Stack of Paper”: A large unit made up of multiple “sectors”, suitable for batch erasure (e.g., erasing an entire application partition), more efficient.
3. Actual Division of Different STM32 Series (Key! Avoid Confusion)
Different STM32 series (F1/F4/F7/H7/L4, etc.) have significant differences in Flash sector/page/block sizes,must refer to the corresponding model’s reference manual (RM), below are typical divisions for the most commonly used series:
1. STM32F1 Series (Mid-Low End, e.g., F103)
-
No “block” concept, only divided intosectors, and sector sizes are uneven (smaller at the front, larger at the back, balancing small capacity storage and efficiency);
-
No separate “page” definition: programming is done in “half-word (2B)” as the minimum unit, but erasure must be done by sector;
-
Typical sector division (based on 512KB Flash):
-
-
Sectors 0~3: each 4KB (total 16KB, suitable for storing Bootloader, configuration parameters);
-
Sector 4: 16KB;
-
Sectors 5~11: each 64KB (total 448KB, suitable for storing application programs).
-
2. STM32F4 Series (Mid-High End, e.g., F407)
-
Introducespages (Page): Minimum programming unit is 2KB (some models 1KB);
-
Sectors = Collection of multiple pages: erasure can be done by “sector” or “entire Flash”;
-
Typical division (based on 1MB Flash):
-
-
Sectors 0~3: each 16KB (each sector contains 8 pages of 2KB);
-
Sector 4: 64KB (contains 32 pages of 2KB);
-
Sectors 5~11: each 128KB (contains 64 pages of 2KB).
-
3. STM32H7 Series (High End, e.g., H743)
-
Clearly distinguishesblocks (Block), sectors (Sector), and pages (Page), with clearer hierarchy;
-
Typical division (based on 2MB Flash):
-
-
1 Block = 256KB;
-
1 Block = 4 Sectors (each Sector = 64KB);
-
1 Sector = 16 Pages (each Page = 4KB);
-
-
Supports: programming by Page, erasing by Sector/Block/Entire Chip.
4. STM32L4 Series (Low Power, e.g., L476)
-
Page sizes are smaller (128B/256B), suitable for small batch data storage (e.g., parameters, logs);
-
Flexible sector division: for example, 256KB Flash can be divided into 128 sectors of 2KB each, or 64 sectors of 4KB each (can be selected via configuration registers).
5. Core Rules in Actual Operations (Key to Avoiding Pitfalls)
-
Must erase before programming: To modify data in a page, the corresponding “sector/block” must first be erased (since pages cannot be erased individually), then reprogram that page and other data in the sector that needs to be retained (backup sector data, erase, then restore + modify). Example: Modify data in a 2KB page of F407 → Backup all 8 pages of the 16KB sector → Erase the 16KB sector → Restore 7 pages that do not need modification + program the modified page.
-
Erase units cannot be smaller than the minimum erase unit: For example, the minimum erase unit for F103 is 4KB (Sector 0), cannot erase only 2KB; for H7, the minimum erase unit is 64KB (Sector), cannot erase only 4KB (Page).
-
Programming addresses must be aligned: When programming pages, addresses must be aligned to the page size (e.g., 2KB pages must align to 0x800, 0x1000, etc.); half-word/word programming must align to 2B/4B (STM32 Flash bus width is mostly 32 bits).
-
Blocks are for batch operations: When erasing an entire application partition, if that partition occupies multiple sectors, directly erasing the corresponding “block” is faster than erasing sector by sector (reducing the number of erase instructions).
6. Example Application Scenarios
| Scenario | Selected Unit | Reason |
|---|---|---|
|
Store Bootloader (small capacity, needs frequent updates) |
Small sectors (4KB/8KB) |
Fast erase speed, no wasted space |
|
Store application (large capacity, infrequent updates) |
Large sectors/blocks (64KB/128KB) |
Large capacity for single erase, high efficiency |
|
Store configuration parameters (100 bytes, needs frequent modifications) |
Small pages (128B/256B) |
Fine programming granularity, small amount of data for backup/restore |
|
Batch erase entire Flash (e.g., firmware upgrade) |
Full chip / large block erase |
Faster than erasing sector by sector, simplifies operation |
7. Conclusion
The “sector – page – block” of STM32 Flash isa logical layered design based on physical characteristics, with the core logic:
-
Page: Minimum programming unit → Enhances writing flexibility;
-
Sector: Minimum erase unit → Balances erase efficiency and space waste;
-
Block: Collection of sectors → Enhances batch erase efficiency.
In actual development,the first step must be to consult the corresponding model’s reference manual (RM) in the “Flash Memory” section to confirm the specific sizes and address ranges of pages/sectors/blocks, and then design storage partitions (such as Bootloader area, application area, parameter area) to avoid data loss or operation failure due to confusion over unit sizes.
Thank you for being with uson this journey