Characteristics of Microcontrollers
Traditional computers compared to Microcontrollers
A traditional computer is composed of various distinct components which have to work together in order to get the desired functionality of the computer. It is a multi-purpose tool. A microcontroller is a chip which requires a minimal amount of (or even none at all) external electronic components to become functional. A lot of features are implemented on the chip. It is designed to be cost-effective and simple to use. The first sub-section will expand on this general differences. The following section highlights some of the most important technical differences.
Purpose, Operation and Software
Traditional Computer | Microcontroller |
---|---|
A traditional computer is usually designed to be a "universal" tool: the user decides for which specific task the computer should be used by running specific software. The same computer is usually used for many different tasks. For this the user writes or buys dedicated software. The computer itself runs an operating system (OS) which provides some (more or less) basic services. The OS is also used by the applications running on the computer (e.g. displaying graphics on the screen, taking input from the keyboard and the mouse, communicating over the network, ...). For this the operating system runs a so called "kernel". In general the kernel can be extended by loading "modules" or "drivers" which contain software to access specific hardware modules which can be plugged into a computer. These drivers are often sold together with the dedicated hardware which should be operated by the computer (and of course in Linux the drivers are mostly freely available from the open source community). Examples of operating system services are:
|
Microcontrollers are usually built for a specific purpose. They are integrated as "embedded systems" into larger systems to perform specific control tasks in various machines. (Control the programs of a washing machine, coffee machine, ...). Interfaces to the user should be intuitive, simple and responsive. We all know how true this is:"...sorry for being late, but I had to reboot my door-lock and apply the latest patch from the company, but this required a more recent version of Android on my phone, and when I tried to update my phone it told me my hardware is not supported any more..." |
CPU (Central Processing Unit)
The CPU (Central Processing Unit) is considered the heart of the computer. The CPU reads commands from the memory, interprets them and executes them. Typical operations are arithmetic operations, moving data from one place to another or changing the program flow based on some criteria. The CPU contains internal registers to hold data temporarily, and to point to important locations in the memory (a pointer is called a register which contains a memory address). For example the "Instruction Pointer" is a pointer to the next instruction to be executed. The "Stack Pointer" points to a memory region referred to as the "stack". A stack works like ... a stack ... : You can put data on top of the stack and you can pull data from the stack: usually you pull off the data from the top of the stack first (i.e. the data which has been put on the stack last). But you can also access data in the middle of the stack when you know where they are (as you can pull out a paper from the middle of a stack of papers on your working desk... you have to be organised if you do this :-). When a subroutine is executed, the processor puts the current value of the Instruction Pointer onto the stack so that it knows where to jump back once the subroutine execution ends. Similarly it saves important internal registers onto the stack before it jumps to the subroutine. When it returns from the subroutine, all registers are restored by pulling the original values from the stack and work can continue where it has been interrupted by the subroutine call. The subroutine is free to use the CPU with its registers on its own, since the registers have been saved on the stack. Finally the stack is also used when local variables are defined in a function/subroutine (i.e. variables which are only valid within the function and when the function exits, the memory is released to the system and these variables do not exist any more.)
Also microcontrollers are built around a CPU. However, they have different characteristics than those used in Computers. Here some key differences are listed:
Traditional Computer | Microcontroller |
---|---|
CPUs are complex objects with a huge instructions set. Modern CPUs contain multiple cores: each core can run a program or a task, hence multiple programs (or tasks) can run in parallel. The CPUs run at high clock speeds (multiple GHz) to achieve high computing power but this leads to high power consumption (An Intel Core i9-12900K with 16 Cores is designed to consume 125W permanently and can consume a maximum of 241W over short periods). These CPUs have dedicated cache memories for each core ("level 1" and "level 2") and shared level 3 caches. | Also Microcontrollers are designed around a CPU. Normally the CPU in microcontrollers features a smaller instruction set than those of Computers. They run at lower frequencies (~10-240 MHz depending on the architecture) and hence consume less power. They contain sleep modes with highly reduced power consumption (a few μA) and thereofre are well suited for battery powered devices. The data words they work with are smaller (usually 8 to 32 bits and not 64 or 128 bits.) Often processors in Microcontrollers have many internal registers which makes programming more efficient (since memory outside of the CPU does not need to be addressed so often). Microcontrollers (as of today) have 1 or 2 cores. In general they do not have cache memories, however, this starts to change in recent generations of microcontrollers: STM32 has a 16kB cache, RP2040 has a small cache when executing programs from external memory. Similarly the ESP32 has a 32kB instruction cache when executing programs from the Flash Memory.) |
Memory and Memory Management
Memory types
Memory is necessary to hold the instructions which are executed in a program and to hold data. Memory in a computer is mostly RAM (Random Access Memory). This means the contents can be changed (i.e. written and read back) and the memory can be randomly accessed. Some data might be contained in ROM memories (Read Only Memory) or EEPROM (Electrically Erasable Programmable Read Only Memory). EEPROMs in "normal" operation behave like Read Only Memories (i.e. memories which can be read but not modified.) However, with special software (contained somewhere in the computer) the contents can be erased and re-written. This is a slow operation and is meant to happen "rarely". During programming only bits which are at '1' can be changed to '0' but not the other way round. This is why there is a special "erase" operation for EEPROMs, which allows to set the bits in a data word of the EEPROM to '1'. This is why it is always necessary to erase EEPROMS before they can be programmed (at least the region which should be programmed needs to be erased.) "Flash memory" is a special kind of EEPROM. The memory is divided into blocks (called "sectors") and only entire sectors can be erased (however, single words can be programmed.)
An example where flash memories are used is the BIOS of the computer: the BIOS contains very hardware specific code which is loaded into a Flash memory by the vendor of the computer. This code is used by Operating Systems (OS) during the booting of the computer. Sometimes the vendor fixes bugs or releases enhancements of the BIOS code and provides users with a new BIOS and the software to programme the BIOS into the flash memory.
At today's processor speeds (multiple GHz clock frequency) care has to be taken to design a computer such that the memory does not become the bottleneck and leads to the CPU waiting for data from the memory most of the time. However, the fastest memory types, which would have to be used avoid this, are very expensive and small in capacity: computers would be huge and forbiddingly expensive if they used only this kind of memory. For this the concept of the cache memory has been invented. Cache memory is high speed memory but small in size and hence affordable. The hardware of the computer loads the cache from the memory with bytes which are currently being used by the CPU: this can be instructions or data. The CPU hardware tries to "guess" what data will be used next. If the guess is right, things can be processed extremely fast. If the guess is wrong and the data needed is not in the cache (a cache "miss" opposed to a cache "hit") the data first has to be loaded from memory into the cache before processing can resume. Computer hardware has become very sophisticated to optimise this guessing. In addition there exist multiple levels of caches to optimise things (normally three levels from the smallest but fastest 1st level cache close to the processor to the largest slowest (but still much faster that the "normal" memory) 3rd level cache.) Computer or processor prices vary as a function of the available amount of cache memory.
In the table above we have seen that now also microcontroller start to have small simple caches (for instructions).
Memory Management Unit (MMU)
Memory management is very complex in large computers: Many processes request a lot of memory for their activities. Once they are done and the application is closed the memory is released and can be used by other applications (which are either running or are being started.) If the computer runs long enough this leads to memory fragmentation: Imagine continuously applications are started and terminated. They always ask for differently sized blocks of memory. This leads to the situation that the memory is fragmented into many small blocks of used bytes and free bytes. It might happen that it is not any more possible to find a reasonably large block of contiguous memory which can be given to a newly started application even though the total amount of free memory would be large enough. It would be extremely inefficient for an applications to work with a huge number of small memory blocks scattered around the address space: the application would do nothing else but the bookkeeping of these blocks. Here the Memory Management Unit (MMU) provides the solution: The MMU translates the addresses of memory blocks in the memory chips to other address ranges by adding offsets to the value of the address. With this technique it can create large memory blocks (i.e. a large block to the software application looks like one chunk of memory with "normally" increasing addresses) from multiple small memory blocks scattered around in the memory chips of the computer. How this works is described in more detail in the section Memory Management and it is probably worth reading this section.
The MMU also serves to isolate the different processes from each other, to limit the damage they can do to each other and to implement computer security (a game should not be able to spy on the application you use to do internet banking...). The MMU hardware makes it impossible for a process to interfere with the memory space of another process. If the process tries to access an address which is not within its virtual address space (and hence not mapped to physical memory) the MMU will generate an alarm to the operating system (a segmentation fault) and the process will be terminated.
Traditional Computer | Microcontroller |
---|---|
Today computers have multiple Gigabytes of RAM in order to perform multiple complex tasks at the same time. They can deal with huge amounts of data. (film editing, machine learning, big databases, ...). Memory organization is very complex: Complex MMUs with cached address translation tables manage the available system memory. Multiple levels of caches guarantee that the overall system performance is not slowed down even when using the fastest available processors. Today's computers are mostly built in a "von Neumann" architecture where there is one memory space shared for instructions and data (with common electrical address and data buses to access the memory.) | In microcontrollers we find two kind of memory types: the flash memory contains the application software and configuration parameters. It is written by the programmer before running the application. Typical sizes of the flash memory range from a few kB (e.g. ATMEL controllers) to a few MB. The RAM memory for data ranges from several bytes (e.g. 64 bytes) to several hundred kB. Some controllers support external RAM chips with some MB. Simple MMUs start to appear in these controllers, too. The same is valid for small cache memories in some controllers. Often, but not always, the memory is on the same chip with the rest of the microcontroller. Usually microcontrollers are built in a Havard architecture where the memory for instructions and data are physically separated and feature separate bus systems. Often the instruction memory is implemented by a flash memory and the memory for data RAM (integrated in the microcontroller chip and in some cases extended by external chips). |
Peripherals
Several peripherals are necessary to make the computers and microcontrollers interact with it's environment. We will dedicate a chapter to this topic since peripherals are a key feature of microcontrollers.
Traditional Computer |
Microcontroller |
---|---|
Computers usually have a set of "standard peripherals" and possibly some more exotic ones. These peripherals are external components or components which have to be plugged into the computer in some way. They are not integrated by the vendor into the computer motherboard.
|
Microcontrollers are used for specific applications and often have to interact with specific hardware components they control (washing machine, coffee machine, small TFT displays, sensors...). The chip designers integrate a set of standard peripheral components into the microcontroller so that for many applications almost no additional hardware needs to be added to the microcontroller. For example, microcontrollers often contain analogue to digital converters which allows them to measure analogue electrical voltages. Usually also programmable counters are available: they allow to measure time or count events. Combining both techniques allow to measure rates and frequencies. In addition industry has developed a set of standardised hardware interfaces which are supported by the microcontrollers and allow them to communicate efficiently with other digital electronics chips. |
Other differences
Traditional Computer | Microcontroller |
---|---|
Size: from laptop to rack mounted server PC. Some specialised "supercomputers" still exist and are even larger. | Normally fit into a hand. The main microcontroller board sometimes can be as small as few square cm. |
Price tag: wide range but starting from several 100 Euros to serval 10k of Euros for "of the shelf" server PCs. | Cheap (around 10 Euros) so that it can be incorporated into cheap consumer products. |
Updates: Users are supposed to update the operating system for security fixes or to get new features. Same for the applications. Often users have to pay for the update (at least if new features are promised) | Often there is no way for the user to update the firmware. Firmware updates might be given out to users if they can be played into the system in a simple way (e.g. via a web interface). Often they are not essential. But sometimes you purchase (without knowing) products which are not yet mature and have buggy firmware and you can hope that the producer fixes them. In general these updates are free of charge. |