AdvancedTopic 72 of 104Electronics & Embedded

Memory-mapped I/O

Peripherals appear as addresses in the memory map.

In depth

Peripheral registers sit at fixed addresses in the CPU's address space and are accessed like variables via pointers. Reads and writes have side effects, so use volatile and observe access sizes and ordering (memory barriers). Vendor headers provide structures.

Key points

  • Linker scripts define regions
  • Bit-banding
  • Read-modify-write races
Example

*(volatile uint32_t*)0x40020014 = 0x20; sets a GPIO output bit.

Why it matters

Memory-mapped I/O lets software control peripherals by reading and writing addresses, which is how nearly all embedded code talks to hardware.

Where this is used

  • Configuring a GPIO port by writing to its register at a fixed address.
  • Reading a sensor data register that a peripheral updates.
  • Setting a timer period by writing to its control register.

Frequently asked questions

What is Memory-mapped I/O?

Peripherals appear as addresses in the memory map.

Why is it important to understand memory-mapped I/O?

Memory-mapped I/O lets software control peripherals by reading and writing addresses, which is how nearly all embedded code talks to hardware.

What is a register map?

A document listing the addresses, names and bit fields of a peripheral's registers, and it is the first thing to read when writing a driver.

Why is the access type important?

Some registers are read-only or clear on read, so writing the wrong way can change the behaviour or lose status flags.

How is Memory-mapped I/O used in practice?

Typical applications include configuring a GPIO port by writing to its register at a fixed address, reading a sensor data register that a peripheral updates and setting a timer period by writing to its control register.

What should I learn before Memory-mapped I/O?

Study "Embedded C programming" first (topic 71 of 104 in Electronics & Embedded). It covers the groundwork this topic builds on.

What are the key points of Memory-mapped I/O?

Linker scripts define regions. Bit-banding. Read-modify-write races.

Can you give an example of Memory-mapped I/O?

*(volatile uint32_t*)0x40020014 = 0x20; sets a GPIO output bit.

What comes after Memory-mapped I/O?

The next topic is "Bootloaders and startup code", which builds directly on this one.

How difficult is Memory-mapped I/O?

It is classed as Advanced in Electronics & Embedded (topic 72 of 104). It is an advanced topic. It assumes you are comfortable with the intermediate topics in Electronics & Embedded.

Learning path

Want to practise? Open the interactive course for quick checks, quizzes, simulations and progress tracking.