Non-Volatile Memory (Part 1): Flash in ATmega

Опубликовано: 05 Август 2026
на канале: Arduino under the Hood
19
0

📥 RESOURCES & DOWNLOADS
• avr/pgmspace.h: Program Space Utilities: https://www.nongnu.org/avr-libc/user-...
• avr/boot.h: Bootloader Support Utilities: https://www.nongnu.org/avr-libc/user-...
• AVR® Fuse Calculator: https://www.engbedded.com/fusecalc/
• AVR Fuse Calculator for the ATmega328P: http://eleccelerator.com/fusecalc/fus...
• Practical Examples
– read_data_from_flash.zip: https://drive.google.com/file/d/14yb5...
– read_fuses.zip: https://drive.google.com/file/d/1DPF2...
– write_data_flash.zip: https://drive.google.com/file/d/1tjOV...
– last_address_in_flash.zip: https://drive.google.com/file/d/1D7GD...

✏️ EXERCISES
• Program the Flash and store constants in the Flash (Program Memory): https://drive.google.com/drive/folder...

🧠 QUIZ
https://forms.gle/3yAjrv5p8UnCoUvQ6

Have you ever needed to store configuration data, calibration values, or large constants that survive power cycles? In this comprehensive section, we explore the non-volatile memory options in the ATmega328P - the 32KB Flash memory and 1KB EEPROM. You will learn how to store large read-only data in Flash to save precious SRAM, and how to persist runtime data in EEPROM that survives resets.

We begin with architecture fundamentals: unlike PCs that use von-Neumann architecture (shared program/data memory), ATmega uses Harvard architecture with separate program memory (Flash) and data memory (SRAM). This separation enables different memory technologies optimized for their purpose - fast volatile SRAM for runtime data, persistent Flash for program code.

Flash memory organization is explored in detail: 32KB organized as 16384 16-bit words in 256 pages of 64 words (128 bytes) each. The critical limitation: Flash is page-writable, not byte-writable - changing one byte requires erasing and rewriting the entire page. Flash endures approximately 10,000 write cycles, making it unsuitable for frequently-changing data but perfect for programs and constants.

Storing data in Flash using PROGMEM is demonstrated: the PROGMEM macro places arrays in program space instead of SRAM, essential for large data like sound samples or lookup tables. Reading requires pgm_read_byte() or similar functions since normal array access assumes SRAM addresses. The __flash keyword provides an alternative to PROGMEM with cleaner syntax.

The Flash sections are explained: No-Read-While-Write (NRWW) and Read-While-Write (RWW) sections. Writing to RWW requires code executing from NRWW (typically the bootloader area). BOOTSZ fuse bits determine bootloader size (512B to 4KB). Writing to Flash at runtime is complex - you must ensure no program code is overwritten and interrupts are disabled during writes.

#Arduino #ATmega #Flash #EEPROM #NonVolatile #PROGMEM #HarvardArchitecture #BootLoader #NRW #RWW #PersistentStorage #EmbeddedSystems #MemoryManagement #datastorage

📖 CHAPTERS
0:00 Introduction
1:09 Memory in different Architectures
2:43 Flash Memory in AVR
4:20 Storing static Data in the Flash during Compilation
14:12 Retrieving static Data from the Flash
16:34 Storing Data during Program Execution
24:17 The BOOTSZ Fuses
26:57 Example
32:41 Locating Free Pages in the Flash
37:37 Practical Example
50:24 Aligning Flash Pages
57:09 Reading Flash with avrdude