The memory map: flash, RAM and peripherals
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A processor reaches everything by address, and a memory map says which addresses lead where: to the program in flash, to variables in SRAM, or to registers wired to hardware. On an Arduino Uno R3 you will find the ATmega328P's 32 KB of flash, its 2 KB of SRAM and the I/O registers in between, then light the LED on pin 13 by writing to addresses 0x24 and 0x25 through plain pointers, and see what digitalWrite() adds on top. The C++ lesson “Pointers” explains the pointers used here, and the Hardware lessons “Registers” and “ROM and flash: memory that remembers” explain the parts themselves.
Every byte has an address
A processor finds everything by number. Each byte it can reach has an address, like a house number on a street. A memory map is the chart of which addresses lead where: to the program, to variables, or to hardware.
address a number that picks one byte memory map which addresses lead where ATmega328P data map, 0x0000 to 0x08FF: 0x0000 to 0x001F 32 working registers 0x0020 to 0x005F 64 I/O registers 0x0060 to 0x00FF 160 extended I/O registers 0x0100 to 0x08FF 2,048 bytes of SRAM
اقرأ الدرس خطوة بخطوة
Every byte has an address
A processor finds everything by number. Each byte it can reach has an address, like a house number on a street. A memory map is the chart of which addresses lead where: to the program, to variables, or to hardware.
address a number that picks one byte memory map which addresses lead where ATmega328P data map, 0x0000 to 0x08FF: 0x0000 to 0x001F 32 working registers 0x0020 to 0x005F 64 I/O registers 0x0060 to 0x00FF 160 extended I/O registers 0x0100 to 0x08FF 2,048 bytes of SRAMFlash holds the program
On an Arduino Uno R3 the ATmega328P keeps its program in 32 KB of flash, byte addresses 0x0000 to 0x7FFF. Flash keeps the code with the power off. This chip gives flash a map of its own, apart from data.
flash 32 KB = 32,768 bytes, 0x0000 to 0x7FFF the compiled program (and constant tables) kept with the power off program map and data map are separate on the AVR (the datasheet counts 16-bit words: 0x0000 to 0x3FFF)SRAM holds the variables
Variables live in a second map, the data map. Its top part, 0x0100 to 0x08FF, is 2,048 bytes of SRAM: fast, but wiped at power off. Globals, the stack and the heap all share it.
SRAM 0x0100 to 0x08FF 2,048 bytes = 2 KB globals, the stack, the heap lost when the power goes int x = 7; -> the CPU stores 7 at an address in hereSome addresses are hardware
Lower down, 0x0020 to 0x00FF, sit 224 addresses that are not memory at all. They are I/O registers, wired to the pins, timers and serial port. Writing one changes hardware. This is called memory-mapped I/O.
0x0000 to 0x001F CPU working registers r0..r31 0x0020 to 0x005F 64 I/O registers 0x0060 to 0x00FF 160 extended I/O registers 224 addresses wired to peripherals memory-mapped I/O: hardware you reach by addressPin 13 lives in port B
The Uno's LED is on pin 13, which is wired to port B, bit 5 of the chip (PB5). Two 8-bit registers control it: DDRB at 0x24 picks each pin's direction, and PORTB at 0x25 sets it HIGH or LOW.
pin 13 = PB5 (port B, bit 5) DDRB 0x24 bit = 1: output, 0: input PORTB 0x25 bit = 1: HIGH, 0: LOW PINB 0x23 reads the pins bits: b7 b6 b5 b4 b3 b2 b1 b0A pointer to an address
In C++ a pointer holds an address. Lines 2 and 3 turn the numbers 0x24 and 0x25 into pointers to one byte. volatile tells the compiler to really do every read and write. Now *portb is the PORTB register.
line 2 ddrb -> 0x24 (DDRB) line 3 portb -> 0x25 (PORTB) both in 0x0020 to 0x005F: the I/O registers volatile: never skip or merge these accessesMake pin 13 an output
*ddrb |= 1 << 5 reads DDRB, sets bit 5 and writes it back. 1 << 5 is 0b00100000, or 0x20. DDRB bit 5 becomes 1, so pin 13 is now an output: what pinMode(13, OUTPUT) does.
line 4 *ddrb |= 1 << 5; 1 << 5 = 0b00100000 = 0x20 DDRB 0b00000000 | 0b00100000 = 0b00100000 pin 13 is an outputWrite a 1, light the LED
*portb |= 1 << 5 sends address 0x25 and the new byte from the CPU. PORTB bit 5 becomes 1, so pin 13 is driven to 5 V and the LED lights. No library call: a write to an address changed a pin.
line 5 *portb |= 1 << 5; address 0x25 -> PORTB PORTB 0b00100000 pin 13 HIGH = 5 V -> LED onWhat digitalWrite() adds
digitalWrite(13, HIGH) ends with the same write. First it reads pin 13's port and bit from tables kept in flash, turns off PWM if the pin has it, and blocks interrupts during the write. Safer, but slower.
digitalWrite(13, HIGH): read tables in flash pin 13 -> port B, bit 5, no timer turn off PWM (only on PWM pins) save SREG, cli() no interrupt mid-write *out |= bit PORTB |= 0x20 restore SREGOther chips, one map
Arm chips like the STM32F4 use one map for everything: flash at 0x0800 0000, SRAM at 0x2000 0000, peripherals from 0x4000 0000. Its GPIOD port starts at 0x4002 0C00. Different numbers, same idea: the address picks memory or hardware.
STM32F4: one map, 32-bit addresses 0x08000000 flash 0x20000000 SRAM 0x40000000 peripherals start 0x40020C00 GPIOD (output register at +0x14)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What is a memory map?
Every byte the processor can reach has an address. The memory map says what each range of addresses leads to: flash, SRAM or peripheral registers.
02On the ATmega328P, which address range is SRAM, where your variables live?
SRAM is the top of the data map, 0x0100 to 0x08FF: 2,048 bytes. The lower addresses are the CPU's working registers and the I/O registers.
03What happens when the CPU writes to an address in the I/O register range?
I/O registers are wired to peripherals. Writing one is memory-mapped I/O: the same kind of store instruction, but it changes a pin, a timer or the serial port.
04DDRB bit 5 is already 1. Which line drives pin 13 HIGH?
PORTB is at 0x25, and 1 << 5 is 0x20, which sets only bit 5. The third option ORs in the number 5 (0b00000101), which sets bits 0 and 2 instead.
05digitalWrite(13, HIGH) and the PORTB write both turn the LED on. What does digitalWrite() do in addition?
The Arduino core's digitalWrite() reads the pin's port, bit mask and timer from PROGMEM tables, turns off PWM on pins that have it, and saves SREG and clears interrupts around the write. That makes it safe for any pin, but slower than a direct register write.
المسرد
- Address
- A number that picks one byte (or one register) the processor can read or write.
- Memory map
- The chart of which address ranges lead to flash, to SRAM and to peripheral registers on a chip.
- Flash
- Non-volatile memory that holds the program; it keeps its contents with the power off.
- SRAM
- Fast working memory for variables, the stack and the heap; its contents are lost at power off.
- Register (I/O)
- A small location at a fixed address that is wired to hardware: writing it changes a pin, timer or serial port.
- Memory-mapped I/O
- Reaching peripherals by reading and writing addresses, exactly as you reach memory.
- Bit mask
- A number with only the bits you want set, such as 1 << 5 = 0x20, used with | to set them or & ~ to clear them.
- volatile
- A C/C++ qualifier that tells the compiler every read and write must really happen, because something outside the program can see or change the location.
الملاحظات والمصادر
- The card shows the body of setup(). The real names DDRB and PORTB from <avr/io.h> expand to exactly this kind of volatile pointer access: avr-libc adds 0x20 to the I/O address, so PORTB at I/O 0x05 is data address 0x25.
- The datasheet numbers flash in 16-bit words, 0x0000 to 0x3FFF, because every AVR instruction is 16 or 32 bits wide. The C compiler and avr-libc (FLASHEND = 0x7FFF) count bytes, which is what this lesson uses.
- The AVR keeps program and data in separate maps, so address 0x0100 means one thing in flash and another in SRAM. Reading a constant table from flash needs special instructions (LPM, wrapped by pgm_read_byte), which is why Arduino marks its pin tables PROGMEM.
- Not every I/O address behaves like memory: some bits are read-only, and writing a 1 to a PINx bit toggles the matching PORTx bit instead of storing a 1. The datasheet's register summary is the only safe guide.
- The STM32F4 addresses come from ST's CMSIS header stm32f407xx.h: GPIOD_BASE = PERIPH_BASE + 0x00020000 + 0x0C00. Its output data register (ODR) is at offset 0x14, so 0x40020C14.
- Microchip (Atmel) ATmega328P datasheet 7810D, §7 AVR Memories and register summary
- avr-libc: include/avr/iom328p.h (DDRB, PORTB, RAMEND, FLASHEND)
- avr-libc: include/avr/sfr_defs.h (_SFR_IO8 and the 0x20 offset)
- Arduino AVR core: variants/standard/pins_arduino.h (pin 13 = PB5, PROGMEM tables)
- Arduino AVR core: cores/arduino/wiring_digital.c (pinMode and digitalWrite)
- Arduino language reference: digitalWrite() (source)
- STMicroelectronics CMSIS device header stm32f407xx.h (FLASH_BASE, SRAM1_BASE, PERIPH_BASE, GPIOD_BASE)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 6: Microcontroller ports (UT Austin)
اختبار يتاح بعد الخطوة الأخيرة.