Full name: Microcontroller unit.
Definition
A microcontroller (MCU, short for microcontroller unit) is a complete small computer built as a single chip. The chip contains a processor, memory that keeps the program, working memory, and peripherals: the circuits that connect the processor to the outside world, such as timers, input and output pins and serial ports. A microcontroller is built into a product and runs one fixed program, called firmware, that controls that product. It differs from a general-purpose computer, which spreads these parts over many chips and loads and runs many separate programs.
The idea
A thermostat reads a temperature sensor, compares the reading with a setting and switches a heater. A desktop computer could do that job, but it is far too large and costly, draws power all day and takes many seconds to start. The product needs a computer that is cheap, runs for years on a small battery, starts at once and reacts to an event within a known time.
A microcontroller meets that need by putting the whole computer on one chip and giving it only what one job requires. Memory is small, often only kilobytes. There is no disk and no program loader §12.49: the program is stored in the chip and starts when power arrives. The program operates the hardware directly. A kitchen timer next to a smartphone is a fair picture: one device, one job, always ready. The picture stops in one respect. A microcontroller is still a programmable computer, and its firmware can be replaced.
How it works
A typical microcontroller has these parts on one chip.
Processor core: the circuit that executes instructions.
Program memory: usually flash memory §6.6, which keeps its content without power and holds the firmware.
Working memory: SRAM §6.7 (static random-access memory), which loses its content without power and holds the data that changes while the firmware runs.
Peripherals: timers, pins, serial ports, converters for analog signals. The firmware controls a peripheral through its registers, small stores for settings and data that appear at fixed places in memory. This arrangement is called memory-mapped I/O §4.35 (input/output).
Interrupt §6.11 controller: a circuit that pauses the running code when a peripheral signals an event and starts the routine that handles the event. Such a pause is called an interrupt.
After power-on, this happens.
A reset circuit holds the processor until the supply voltage is high enough.
The processor starts executing the firmware directly from flash memory. It runs the code in place; nothing is loaded from a disk.
Start-up code prepares the working memory and the peripherals.
The main program runs as long as power lasts. It mostly waits for events, such as a timer expiring or a byte arriving.
Larger firmware often includes a real-time operating system (RTOS). Its kernel, the core part of an operating system, lets several threads take turns on the processor; a thread is one independent sequence of instructions. Kernel, threads and device drivers §6.38, which are the code that operates the peripherals, remain one program. In the Zephyr RTOS §6.33, a thread by default has access to the entire memory and to the peripherals.
A general-purpose computer works differently. Its operating system, such as Linux or Windows, starts each program as a process: a running program with a private view of memory, called a virtual address space. An address is the number of a location in memory. A hardware unit, the memory management unit §6.9 (MMU), translates every address that a process uses into a real location and refuses addresses the process was not given. A microcontroller usually has no MMU. Many have a memory protection unit §6.8 (MPU) instead, which translates nothing and only checks each access against a few address ranges that the firmware has programmed.
Worked example
Two microcontrollers show the scale. The ATmega328P from Microchip has an 8-bit processor, 32 KiB of flash memory and 2 KiB of SRAM. The nRF52840 §6.4 from Nordic Semiconductor has a 32-bit processor, 1 MiB of flash memory and 256 KiB of RAM. A laptop with 16 GiB of working memory holds KiB, which is times the working memory of the nRF52840.
Now a bug. Firmware on a microcontroller that uses no memory protection keeps a 4-byte counter at address 0x20000100. Directly after the counter, at 0x20000100 + 4 = 0x20000104, the kernel keeps a 4-byte pointer (a stored address) to the next thread. A faulty loop writes two 4-byte values starting at the counter, where it should write one. The second value overwrites the kernel’s pointer. No hardware check stops the write, because all code shares one address space. The failure appears later, when the kernel follows the damaged pointer.
The same bug in a process on the laptop damages only that process. The process has no access to the kernel’s memory or to the memory of other processes. If the faulty loop runs on into addresses the process was never given, the MMU reports a fault and the operating system ends that one process.
Stated precisely
The word microcontroller names a class of chips by what they integrate, not by size: no fixed limit on memory or speed separates a microcontroller from a system on chip §6.2 (SoC).
For Armv7-M, an Arm architecture for microcontrollers, the architecture manual states that the processor uses a single, flat address space of bytes and that every address is a physical address; virtual addresses do not exist. Such a processor has two privilege levels §6.10, privileged and unprivileged, and only privileged code may program the MPU. Hardware confines a piece of code to chosen memory ranges only if three conditions hold.
The chip contains an MPU.
The firmware has programmed the MPU with those ranges and has enabled it.
The code runs unprivileged.
If condition 1 or 2 fails, the ranges are not enforced at all. If only condition 3 fails, the code can reprogram the MPU or switch it off, so the ranges do not confine it.
Limits and common mistakes
A microcontroller is not defined by the absence of protection. Some have an MPU and two privilege levels, and firmware can use them to separate its parts. What a microcontroller normally lacks is an MMU and the processes built on it.
A thread of an RTOS is not a process. Threads share one address space unless the RTOS is configured to separate them.
The absence of an MMU does not rule out confinement. A virtual machine §2.3, a computer defined in software, can check every memory access of the program it runs (its bytecode §2.4) and so confine that code in software. The virtual machine itself must then be correct.
Questions answered
How does an MCU differ from a general-purpose computer running a process OS?
An MCU (microcontroller unit) is a single chip that runs one program, its firmware, directly on the hardware, in little memory that all of its code shares. A general-purpose computer runs an operating system that starts many programs as processes, each with private memory, and has gigabytes of memory.
Integration and scale. Processor, program memory, working memory and input/output circuits are on one chip. The nRF52840 microcontroller has 256 KiB of working memory; a laptop with 16 GiB has 65,536 times as much.
Program model. The firmware is one program, stored in the chip and executed in place.
Isolation. A process operating system uses a memory management unit (MMU) to give each process private memory, so a faulty process cannot damage another process or the operating system. A microcontroller usually has no MMU. All of its code shares the same memory addresses, and a wrong write anywhere can corrupt anything, unless the firmware confines part of the code with a memory protection unit (MPU) or a software sandbox §2.30, a checked environment that the code cannot leave.
Timing. The firmware handles hardware events itself, through interrupts, the signals by which hardware pauses the running code, and is built to react within a known time.
Related entries: Memory protection unit (MPU) (§6.8), Memory management unit (MMU) (§6.9), Virtual machine (VM) (§2.3), System on chip (SoC) (§6.2), Flash memory (§6.6), SRAM (§6.7), Privilege level (§6.10), Zephyr RTOS (§6.33). Further reading: [13, 20, 81].