Understanding Embedded System Hardware
First step in learning embedded system programming, is to understand the
embedded board or embedded hardware. But note that embedded hardware is
nothing but computer hardware only (In fact DEPIK is using and recommending
computer motherboard as embedded board). So both embedded hardware and
computer hardware contains following components:
- CPU or Microprocessor
- Non-volatile memory chips such as Flash/EEPROM/PROM
- Volatile memory chips such as static RAM or Dynamic RAM
- I/O controller or Peripheral chips
- I/O devices
In the above components, first four are semiconductor chips, the last one
i.e. I/O devices are LCD, keypad, speaker, microphone etc..
Depending the hardware, the first four components could be physically
residing in a single chip or multiple chips.
In embedded hardware, many types of CPUs are used. But the internal
workings of all most all the CPUs are identical. In the following paragraphs,
i am trying to explain various features of CPUs by giving examples from 8051,
ARM and x86 processors.
When power is applied, the microprocessor will get reset, and its internal
registers will get initialized to some default values. Most important register
is Program Counter (PC) register. This register holds the address of
instruction to execute. So on reset, this register contains the address of
first instruction it is going to execute. For most of the microprocessors the
default value for PC is zero.
Microprocessor reads its first instruction from memory by using address
present in PC (i.e. zero). Next it increments PC to the next instruction
address. After reading the instruction, it decodes and executes it. Next it
reads, increments PC, decodes and executes the next instruction. This goes
on and on forever, as long as power is given to the board.
Very simple 8 bit microprocessor to 32 or 64 bit processor like Pentium does
the same thing. That is reading instructions from memory and executing them.
Don't you think this is simple to understand?
In fact it is very simple to understand how a microprocessor works. But you
are not going to understand how microprocessor is executing each instruction.
That is not much important to you as software developer. That may be required
for chip designer. But as embedded software developer you should understand,
all the instructions supported by at least one microprocessor.
The 8051 is the best processor to start learning microprocessor fundamentals,
embedded system fundamentals and even computer architecture fundamentals.
This is the reason DEPIK recommends strongly every electronics engineer
student to own DEPIK's 8051 kit and experiment with it.
Even though 8051 is the oldest processor, it is most widely used processor
in embedded systems, even today. It is eight bit processor and reasonably
simple to understand. Understanding the workings of even simple processor
is enough, as all the powerful processors works more or less on similar lines.
To learn other powerful processors just concentrate on the differences.
In this way understanding 8051 gives you the solid foundation, based on this
foundation, you can learn other processors.
8051 is an 8 bit processor. We say a processor is 8 bit, if its accumulator
is an 8 bit register. That means it is capable of doing operations on 8
bits at a time.
Bus and Bus cycles
Every microprocessor has to interact with memory chips and Peripheral chips.
Interaction is required to read and write to these chips. Microprocessor is
interfaced (connected) to these chips by using its address lines, data lines
and control lines. All these three types of lines put together, we can call
it a bus. Individual types also referred as address bus, data bus and control
bus.
In general we can say that, every microprocessor interfaces to memory and
peripheral chips through bus. Each type of interaction over a bus is called
bus cycle. Common bus cycles performed by CPU are:
- Instruction fetch bus cycle
- Memory read bus cycle
- Memory write bus cycle
Each bus cycle is started by the CPU first by placing the address on the
address bus. If it is a Instruction fetch cycle or memory read cycle it
activates the read line of the control bus. The address will select a
specific location, the read line will cause that location content to be
placed on the data bus. Finally CPU reads the data placed on the data bus.
In the case of write cycle, after placing the address, CPU places the data
on the data bus. Next CPU activates the write line of control bus. This
write line makes, the data to get written to the location selected by the
address.
The timing information for all the bus cycles is derived from the CPU clock.
For example, CPU may place the address in one clock cycle. In the second
clock cycle it may activate read line, finally in the third clock cycle it
may read data from the data bus. So here bus cycle is taking 3 clock cycles.
Types of Addresses or Address spaces
Some processors support a single address space. Most modem processors
support a single address space. Where as most of old processors support
multiple address spaces. So CPU after placing the address, it also activates
some lines that specify the type of address space. The 8051 supports three
types of addresses or address spaces. These are:
- Program memory
- Internal RAM
- External RAM
So when CPU has placed the address 100 on the address lines, it uses
additional lines to specify the type of addresses.
The ARM processor supports only a single address space.
The Intel 8085, 8086 to Pentium type of CPUs are support two types of address
spaces:
- Memory address
- I/O address
Register Set
Every processor will have a set of registers. Some registers are essential
and almost all processors will have the following registers:
- Program counter(PC) or Instruction Pointer (IP)
- Program Status Register or Flags registers
- Stack Pointer (SP)
The remaining registers are called general-purpose registers.
Instruction Set Architecture
Most interesting thing in a CPU for embedded software engineers is the types of
instructions supported by the CPU. Each CPU will have its one instruction set.
By understanding each instruction of the CPU, we know the complete
capabilities of the CPU. Instructions can be classified into the following
groups:
- Arithmetic
- Data movement
- Logical
- Control flow
- Bit operations
Every instruction, when stored in memory, it contains opcode byte and
followed by one or more operand bytes. Some times opcode byte may also
includes the operand.
Memory
Every embedded or computer board contains two types of memory chips.
One is non-volatile memory such as Flash, PROM or EPROM chips. Other is
volatile memory like RAM. Within RAM there are two types. One is static RAM
and other is Dynamic RAM.
As mentioned at the beginning, these memory chips and CPU could be physically
placed in a single chip, or on different chips.
These memory chips are mapped on to the memory address space of CPU.
So CPU accesses memory locations by using memory addresses.
Peripheral Chips or I/O controllers
Peripheral chips are a must in every computer hardware. Typical peripheral
chips that present in most of the hardware are digital I/O controllers,
Timers, Serial controller etc..
Every controller chip contains a set of registers called I/O registers or
I/O ports. These registers are also mapped on to the memory address space
or I/O address space of the CPU. CPU can access (read or write) these I/O
registers by using memory address or I/O address depending on the mapping.
As discussed in the "Address Spaces" section, some CPUs like ARM may support
only memory space. In such CPUs, both I/O controllers and memory chips are
mapped on to memory space. Intel processors support both memory address
space and I/O address space, so I/O controller are mapped on to I/O space.
Interrupt Support
Every CPU supports interrupt mechanism. Under interrupt mechanism, CPU will
have one or more lines (pins) called interrupt pins. The peripheral chips,
when ready for data, they will activate the interrupt pin of CPU. Whenever
interrupt Pin becomes active, the CPU will completes the execution of current
instruction, saves the address of next instruction on stack and jumps to some
address. At this address interrupt function will be present. After
execution of interrupt function, the execution continues where it was
interrupted.
How many interrupt lines are present and on interrupt, exactly to which
address CPU jumps, varies from CPU to CPU. The 8051 CPU has got support
for 4 to 6 interrupts. For each interrupt it will jump to some fixed address.
ARM CPU got only one interrupt line, it always jumps to one fixed address
on interrupt. The Pentium uses interrupt table that gives the interrupt
address to jump.
Direct Memory Access (DMA) support
DMA offloads the I/O work from CPU. CPU can do I/O operations, like reading
data from I/O chip to memory, or writing data from memory to I/O chip.
CPU takes more time to perform these I/O operations. Instead DMA chip is
specialized to perform I/O operations and can do them very quickly. However
a co-ordination is required between CPU and DMA to share the bus lines for
I/O transfer.
So CPU should have mechanism to support DMA chips. Most of the CPUs are
built with this support.
Cache Support
Cache is a special RAM that is present inside the CPU. CPU keeps all the
recently accessed data variables and instructions inside this cache memory.
This makes accessing much faster and improves the performance. For example
when CPU is executing a for loop, after the first execution, all the
instructions of the loop will present in cache. All the remaining iterations
are executed from the cache itself.
Now a days almost all 32 bit CPUs will have cache inside them. Some CPUs will
have separate cache memory for data and instructions.
Floating Point Support
Most of the small CPUs does not support floating point operations. But
high-end processors will have a separate floating-point co-processor that
performs floating-point operations. Typically embedded systems do not
require floating point calculations. If required, it can be done through
software emulation.
Memory Management Unit (MMU) for virtual memory support
Memory management unit (MMU) supports paging and virtual memory. Most of the
simple embedded CPUs do not support MMU. All high-end processors like ARM-9
and Pentium supports MMU. Operating systems like Linux and Windows require
MMU support. Most of the RTOSes like VxWorks and Nucleus do not need MMU
support.