ENع
→Memory map
الدرس 31 · Registers and the memory map

The memory map: flash, RAM and peripherals

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 31 · تفاعلي
01 / 10

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Flash 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)
  3. 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 here
  4. Some 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 address
  5. Pin 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 b0
  6. A 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 accesses
  7. Make 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 output
  8. Write 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 on
  9. What 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 SREG
  10. Other 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)

اختبار يتاح بعد الخطوة الأخيرة.