Documente Academic
Documente Profesional
Documente Cultură
com
Preface
Embedded Systems are dedicated systems that do specialized tasks and often
contain hardware, software and
components are embedded in many applications and situations that cover virtually
every aspect of our daily lives, ranging from the use of simple household appliances
(such as alarm clocks, toaster ovens, room temperature control panels, microwave
ovens) to transportation systems (e.g., automobiles, traffic lights, airplanes), and many
communication, recreation and entertainment products (e.g., cellular phones,
electronic organizers, exercise machines, video games, TV set-top boxes). To
appreciate these advances, and to productively contribute to future advances of such
systems, a critical appreciation of the underlying principles of embedded systems is
necessary. The goal of this course is to develop a comprehensive understanding of the
basic concepts involving the development of embedded systems. The students will
develop an appreciation of the technology capabilities and limitations of the software
and hardware components for building embedded systems.
UNIT 1
Contents
1.1
Introduction
1.2
Objectives
1.3
1.3.2
1.3.3
1.3.4
1.3.5
1.3.6
1.4
1.5
1.5.2
1.5.3
1.5.4
1.5.5
1.5.6
1.6
1.7
1.8
Summary
1.9
Self Test
1.10
Questions
1.1 Introduction
An Embedded system is a specialized computer system that is part of a larger
system or machine. It is embedded into some device for some specific purpose other
than to provide general purpose computing. A typical embedded system consists of a
single-board microcomputer with software in ROM, which starts running some
special purpose application program as soon as it is turned on and will not stop until it
is turned off (if ever).
An embedded system may include some kind of operating system but often it
will be simple enough to be written as a single program. It will not usually have any
of the normal peripherals such as a keyboard, monitor, serial connections, mass
storage, etc. or any kind of user interface software unless these are required by the
overall system of which it is a part. Often it must provide real-time response.
1.2 Objectives
In this unit you will learn
What are embedded systems and their characterstics?
Functions and classification of embedded systems
Types of embedded systems
Real time systems
Embedded system design requirements
Embedded system implementation types
"Embedded" reflects the fact that they are an integral part of the system. In
many cases their embeddedness may be such that their presence is far from obvious to
the casual observer and even the more technically skilled might need to examine the
operation of a piece of equipment for some time before being able to co nclude that an
embedded control system was involved in its functioning. At the other extreme a
general-purpose computer may be used to control the operation of a large complex
processing plant, and its presence will be obvious.
compute. Even if the same computer was connected to a set of instruments, which it
then controlled, that would not be an embedded system. If, however, the same
computer was built permanently into an identifiable system, and customized so that its
sole purpose was to control the one system (which may mean losing such apparently
essential features as its case, keyboard, screen, or disk drives), then it would form part
of an embedded system.
Embedded systems come in many forms. They are extremely common in the
home, the motor vehicle and the workplace. Most modern domestic appliances
washing machines, dishwashers, ovens, central heating and burglar alarms are
embedded systems. The motorcar is full of them, in engine management, security (for
example locking and anti-theft devices), air-conditioning, brakes, radio, and so on.
They are found across industry and commerce, in machine control, factory
Downloaded from www.pencilji.com
Figure 1.2: An embedded system encompasses the CPU as well as many other
resources.
In addition to the CPU and memory hierarchy, there are a variety of interfaces
that enable the system to measure, manipulate, and otherwise interact with the
external environment. Some differences with desktop computing may be:
The diagnostic port may be used for diagnosing the system that is being
controlled -- not just for diagnosing the computer.
The microprocessor was an overnight success, and its use increased steadily
over the next decade. Early embedded applications included unmanned space probes,
computerized traffic lights, and aircraft flight control systems. In the 1980s,
embedded systems quietly rode the waves of the microcomputer age and brought
microprocessors into every part of our personal and professional lives.
Very early in their development, and certainly by the end of the 1970s, two
trends were emerging for these remarkable devices. One was to scale down, in size if
not computing power, the general-purpose computer; this led quickly to the first
desktop machines. The other, much more revolutionary, was to place the
microprocessor in products, which apparently had nothing to do with computing.
They began to find their way into photocopiers, grocery scales, washing machines,
and a host of other products, wherever there was a requirement to exercise some
control function. While the first trend led to an inexorable demand for faster a nd
bigger processors with increasingly sophisticated mathematical capability, the second
placed lower demands on computational power and speed. It wanted physically small
and cheap devices, with as much functionality of the system as possible squeezed onto
one integrated circuit.
Control Laws
PID (Proportional-Integral- Derivative) control
Fuzzy logic
Sequencing logic
Finite state machines
Switching modes between control laws
Signal processing
Multimedia data compression
Digital filtering
Downloaded from www.pencilji.com
Fault response
Detection & reconfiguration
Diagnosis
Using these four examples to illustrate points, the following sections describe
the different areas of concern for embedded system design: computer design, systemlevel design, life-cycle support, business model support, and design culture
adaptation.
While traditional digital design (CAD, Computer Aided Design) tools can
make a computer designer more efficient, they may not deal with the central issue -embedded design is about the system, not about the computer. In desktop computing,
design often focuses on building the fastest CPU, then supporting it as required for
maximum computing speed. In embedded systems the combination of the external
interfaces (sensors, actuators) and the control or sequencing algorithms is or primary
importance. The CPU simply exists as a way to implement those functions. The
following experiment should serve to illustrate this point: ask a roomful of peop le
what kind of CPU is in the personal computer or workstation they use. Then ask the
In high-end embedded systems, the tools used for desktop computer design are
invaluable. However, many embedded systems both large and small must meet
additional requirements that are beyond the scope of what is typically handled by
design automation. These additional needs fall into the categories of special computer
design requirements, system- level requirements, life-cycle support issues, business
model compatibility, and design culture issues.
Timeliness
The microcontroller in an embedded system must be able to respond fast
enough to keep its operation within a safe region. This is a characteristic of operating
in real time; the controller must be able to respond to inputs as they happe n and
make responses within the time frame set by the controlled system. This style of
operation is different from the mode of operation, for example, of a personal
computer. While it may be annoying, you can tolerate waiting for your computer to
refresh the graphics display or complete a computation. You cannot tolerate waiting
while your cars antiskid braking system decides whether or not to apply the brakes!
Some embedded systems operate within absolutely rigid time demands; for others the
demands are less stringent. They all, however, exhibit the characteristics of
timeliness: a need for the designer to understand fully the time demands of the
controlled system and be responsive to them.
System interconnection
While some embedded systems clearly need only one controller, others are
likely to use several or many, each to control one sub-system. Necessary shared
information is then passed between them by a simple network, devised to suit the
needs of the overall system. A good example of this is the modern motor car. Though
each of the embedded sub-systems in it may be controlled by one microcontroller,
they can all be linked together to form one overall interconnected system. This
approach is made more attractive due to the extremely low cost of most co mmercial
microcontrollers. A network of low-cost microcontrollers is often cheaper, and
simpler to develop, than a single complex computer undertaking many tasks. With the
advent of the Internet, a generation of Internet-compatible embedded systems is
emerging. The cooker, television and washing machine may soon be communicating
together! It is anticipated that within a few years even the most simple of devices may
be Internet- linked. The truly standalone device will then exist in a dwindling minority.
The market-place
The market that the embedded system sells into is very competitive. As with
other hi-tech markets, the challenge is increased greatly by the very rapid advances
of technology. New products may quickly be rendered obsolete by technological
change, and thus potentially have very short life c ycles. This lays the stress on
excellent design and development strategy.
Adding all these features together, a second definition of the embedded system
now follows:
An embedded system is a microcontroller-based, software-driven, reliable, real-time
control system, autonomous, or human or network interactive, operating on diverse
physical variables and in diverse environments, and sold into a competitive and cost
conscious market.
Thus embedded systems are application specific systems, meant for mass
production, usually with a static structure. They may be DSP systems, Real Time
Systems, Reactive systems or Distributed systems.
All of the topics presented in this book are applicable to the designers of realtime systems. However, the designer of a real-time system must be more diligent in
his work. He must guarantee reliable operation of the software and hardware under all
possible conditions. And, to the degree that human lives depend upon the system's
proper execution, this guarantee must be backed by engineering calculations and
descriptive paperwork.
On Line
Response Time
strict
soft
Pacing
environment
computer
predictable
degraded
Error detection
system
user
Safety
often critical
non-critical
Redundancy
active
standby
Data Integrity
short term
long term
Hardware
Operating System
Host
Compiler, Debugger,
Loader, Simulator,
Shell, vxSim, etc.
Target System
Application
Tasks
WinNT OS
(or Solaris)
Real-Time OS
(pOSEK)
Hardware
(C167CR)
Input
Output
Pentium PC
(SUN workstation)
RS-232
Ethernet
Figure 1.5: The real time system development
task management,
memory management,
time management,
device drivers,
interrupt service.
RTOS Examples:
POSIX, VxWorks, OS- 9, pSOSystem, Linux, Eonics, Windows CE, QNX Neutrino
Deadline is a point in time at which a result should have been made available
by the computer system.
Soft deadline: if result still has some utility after the deadline
Strict deadline: if result has no utility after the deadline
Hard deadline: if missing of a strict deadline can have catastrophic consequences
hard RT
hard - required
predictable
environment
often critical
small/medium
active
short-term
autonomous
soft RT (on-line)
soft - desired
degraded
computer
non-critical
large
checkpoint-recovery
long-term
user-assisted
Low Power
Battery power for 8+ hours (laptops often last only 2 hours)
Limited cooling may limit power even if AC power availab le
Harsh environment
Heat, vibration, shock
Power fluctuations, RF interference, lightning
Water, corrosion, physical abuse
8- bit microcontrolle r
low cost applications
include on- chip memory and I/ O controllers
Embedded Microprocessors
Intel 8051
TI TMS320C10, C60
Motorola 68HC11
Motorola Coldfire
SHARC DSP
Fujitsu FR- V
IBM Power PC
MIPS
ARM, Thumb
Intel StrongARM
SandCraft SR1- GX
Perhaps the greatest strength of C - and the thing that sets it apart from
languages like Pascal and FORTRAN - is that it is a very "low- level" high- level
language. C gives embedded programmers an extraordinary degree of direct hardware
control without sacrificing the benefits of high- level languages. The "low- level"
nature of C was a clear intention of the language's creators. In fact, Kernighan and
Ritchie included the following comment in the opening pages of their book The C
Programming Language:
In the early days, embedded software was written exclusively in the assembly
language of the target processor. This gave programmers complete control of the
processor and other hardware, but at a price. Assembly languages have many
disadvantages, not the least of which are higher software development costs and a
lack of code portability. In addition, finding skilled asse mbly programmers has
become much more difficult in recent years. Assembly is now used primarily as an
Assembly
- Processor specific, Small and fast (compared to high level languages)
- High development time and cost
C /C++
- Popular and Cs efficiency is close to assembly
- Well established and suitable for general embedded applications..
Ada
- Was mandated by DoD for many of their projects.
- Chosen by companies for safety critical applications, e.g., Airbus and
Boeing 777 flight control.
- Try to help enforce good software engineering practices.
- Not widely supported.
Java
- Simple, portable, multi- threaded.
- Gets rid of some of worst features of C.
- Real time garbage collection are being solved
Programming Environment
Host + target: develop and compiled at the host, download and executed at the
target.
In real world embedded applications, you often develop and test in a self- host
All embedded systems also contain some type of inputs and outputs. For
example, in a microwave oven the inputs are the buttons on the front panel and a
temperature probe, and the outputs are the human-readable display and the microwave
radiation. It is almost always the case that the outputs of the embedded system are a
With the exception of these few common features, the rest of the embedded
hardware is usually unique. This variation is the result of many competing design
criteria. Each system must meet a completely different set of requirements, any or all
of which can affect the compromises and tradeoffs made during the development and
the product. For example, if the system must have a production cost of less than $10,
then other things--like processing power and system reliability- might need to be
sacrificed in order to meet that goal.
Of course, production cost is only one of the possible constraints under which
embedded hardware designers work. Other common design requirements include the
following:
Processing powe r
The amount of processing power necessary to get the job done. A common
way to compare processing power is the MIPS (millions of instructions per second)
rating. If two processors have ratings of 25 MIPS and 40 MIPS, the latter is said to be
the more powerful of the two. However, other important features of the processor
need to be considered. One of these is the register width, which typically ranges from
8 to 64 bits. Today's general-purpose computers use 32- and 64-bit processors
Memory
The amount of memory (ROM and RAM) required to hold the executable
software and the data it manipulates. Here the hardware designer must usually make
his best estimate up front and be prepared to increase or decrease the actual amount as
the software is being developed. The amount o f memory required can also affect the
processor selection. In general, the register width of a processor establishes the upper
limit of the amount of memory it can access (e.g., an 8-bit address register can select
one of only 256 unique memory locations).
Development Cost
The cost of the hardware and software design processes. This is a fixed,
onetime cost, so it might be that money is no object (usually for high- volume
products) or that this is the only accurate measure of system cost On the case of a
small number of units produced).
Number of units
The tradeoff between production cost and development cost is affected most
by the number of units expected to be produced and sold. For example, it is usually
undesirable to develop your own custom hardware components for low-volume
product.
Expected lifetime
How long must the system continue to function (on average)? A month, a
year, or a decade? This affects all sorts of design decisions from the selection of
hardware components to how much the system may cost to develop and produce.
Reliability
How reliable must the final product be? If it is a children's toy, it doesn't
always have to work right, but if it's a part of a space shuttle or a car, it had sure better
do what it is supposed to each and every time.
Table 1-1 illustrates the range of possible values for each of the previous
design requirements. These are only estimates and should not be taken too seriously.
In some cases, two or more of the criteria are linked. For example, increases in
processing power could lead to increased production costs. Conversely, we might
imagine that the same increase ip processing power would have the effect of
decreasing the development costs-by reducing the complexity of the hardware and
software design. So the values in a particular column do not necessarily go together.
Low
Medium
High
Processor
4- o r 8-bit
16-bit
32- or 64-b it
Memory
< 16 KB
64 KB to 1 M B
> 1 MB
< $100,000
$100,000 to
> $1,000,000
$1,000,000
Production cost
$ 10
$10 to $1,000
> $1,000
Nu mber of un its
< 100
100-10,000
> 10,000
Expected lifetime
days, weeks,
years
decades
must be
or months
Reliab ility
may occasionally
fail
fail-proof
1.5.5 Implementations of ES
ASIC (Application Specific IC) based: hard-wired approach
fast
highly integrated
difficult to design (rigidity)
Microprocessor based
Control functions defined by S/W
flexible: re-programmable, upgradable
Slow but may be improved by using multiprocessor
Hardware/Software Co-Design
System-on-a-Chip
Communication
The communication system provides the interconnection between
different subsystems or different components of the embedded systems. An
efficient communication systems plays an important role in the functioning of
an embedded system, especially in a networked environment. RS232 is one
such system.
Interface
This subsystem is responsible for providing interface to the outside
world and may include devices like keyboard and monitors. How an
embedded system interfaces with the outside world or other embedded
systems in case of a networked environment plays crucial role. Different I/O
devices are part of this unit.
Auxiliary Units
Some auxiliary units like timer units, auxiliary memory systems are
necessary for the smooth functioning of the embedded systems
1.8 Summary
An embedded system incorporates a computing element, typically a
microprocessor or microcontroller, to perform a control function. Many embedded
systems are small and low-cost, and are aimed towards the volume market. They
apply recognized hardware and software principles to meet the particular
requirements of the embedded environment. An embedded system consists of
different subsystems like: Communication, Data Acquisition and Processing, System
Logic and Control Algorithm , Interface, Auxiliary Units
2.
b) Eonics
e) none of these
4.
b)java
c)assembly
d)c++
5.
6.
b)medium
c)small/medium
d)large
7.
8.
b) unpredictable
Ans wers
1. True
2. e
3.
a,c
4. Fail-Safe
5. 64 KB to 1MB
6. c
7. microprocessor based
8. a
1.10 Questions
1. What is an Embedded System? Give examples
2. What are the functions of Embedded Systems?
3. What are the characterstics of Embedded Systems?
4. Explain any 3 classifications of Embedded Systems
5. What are the challenges of Embedded Systems design and development?
6. What are different implementation types of Embedded Systems?
UNIT 2
2.1 Introduction
2.2 Objectives
In this unit you will learn:
Basics of microprocessor
Applications of a microcontroller
Examples of microcontrollers
the microprocessor
some contact with the outside world (through the input/output port)
a means of interconnecting these elements (i.e. data and address bus, together
with some control lines)
Program memory is usually stored in a form of memory called ROM ReadOnly Memory. Data memory is usually stored in a type of memory called RAM
Random Access Memory. ROM retains its contents when the system is powered
Downloaded from www.pencilji.com
In normal system operation the processor works down the list of instructions
which make up the program. It fetches each one from program memory, decodes it
with its Instruction Decode circuit, and then executes it. The instruction is in many
cases accompanied by further pieces of code, also stored in program memory, which
are treated as operand data, or addresses where the operand data may be found.
The microprocessor keeps its place in the program by means of the Program
Counter, which always holds the address of the next instruction to be executed. In
order to fetch the next instruction, the processor places the value held in the Program
Counter on the address bus, and signals through the control lines that it wishes to read
data. Memory corresponding to that address will, upon receiving the address and
control signals, place the instruction word on the da ta bus, which the processor can
then read. As each word is read from program memory, the Program Counter is
incremented.
Figure 2.3 illustrates this sequence of activities, for the processor of Figure 2.2
and for a certain instruction, as a timing diagram. It can be seen that there are four
clock cycles in each machine cycle. The first cycle shown is an instruction fetch
cycle. The address of the instruction to be fetched is placed on the address bus, and
the R/W line indicates that the data transfer is to be a read. In response the addressed
memory places data onto the bus. This is received by the microprocessor and decoded
by the Instruction Decode circuit.
In the second machine cycle the instruction is executed; the example illustrates
a data move from processor to memory. The processor sets values on the address and
data buses, and signals a write by setting the R/W line low. The DAV line goes high
to indicate that the bus data is valid. The falling edge of this signal is used to latch the
data into memory. This particular instruction has taken two machine cycles to
complete. It is then followed by the Instruction Fetch cycle of the next instruction. It
follows that simple microprocessor operation can be seen as a relentless cycle of
instruction fetch, decode and execute.
Each processor (or processor family) has its own instruction set, from which
the program is written. Each instruction is a binary word, known individually as the
op code (operation code), or collectively as machine code. The processor CPU can
recognize and respond to these codes. The instruction set is the collection of all these
op codes. It uses the basic ALU operations listed earlier, and adds to these certain data
transfer and branch instructions. This gives an instruction set the following typical
instruction categories:
Data transfer: instructions, which move data from one register or memory
location to another.
a carry bit, indicating whether there was a carry from the most significant bit
(msb) of the accumulator, also used as a borrow in subtraction
a sign, or negative bit, indicating whether the result was negative (interpreting
the result in twos complement arithmetic(Twos complement is a means of
expressing negative numbers in binary. ) hence this bit is simply set to the
msb of the result
a half-carry bit, indicating whether there was a carry between the lower and
higher nibbles of the result this is useful for Binary Coded Decimal (BCD)
arithmetic
an ove rflow bit, indicating whether the twos complement range has been
exceeded. It is set if there has been a carry out of bit 7 but not bit 6, or a carry
out of bit 6 but not bit 7
a parity flag, indicating whether an odd or even number of 1 bits are in the
accumulator
As there are not usually enough condition code flags to fill an 8-bit register,
many processors use the remaining few bits for other purposes, for example interrupt
mask bits or register bank address bits.
Fi gure 2.5: (a) The convent i onal von Neumann st ruct ure; (b) t he Harvard
structure.
Once the overall memory space is defined, it is up to the user to decide which area is
allocated to data, and which to program. The structure does however lead to the von
Neumann bottleneck; time-sharing the data bus between both instruction and data
means that maximum speed of executing a program will always be limited, as each
has to use the bus in turn.
It is, however, possible to have more than one address and data bus, and hence
to place data and program memory in different memory maps. This approach,
sometimes called a Harvard structure, is shown in simple form in Figure 2.5(b).
Instructions can now be fetched independently from, and if necessary simultaneously
with instruction execution, thereby eliminating the von Neuma nn bottleneck. The two
data buses can now be of different sizes, as can the two address buses. This allows
each to be optimized for its own use, and has important implications in certain
processor structures. The structure facilitates pipelining (see below), and also
enhances program security. It is less likely that an errant processor will attempt to
overwrite its own program, or jump into data memory and start interpreting data as
instructions.
With its multiplicity of buses, this architecture does lead to a more complex
hardware realization than conventional von Neumann. Moreover, not every memory
use is clearly divided into data or program. Look-up tables (i.e. tables of constant
data, defined within the program), for example, may be embedded in pro gram
memory, but required for use as data.
RISC machines have the advantages of simplicity and speed, but carry the
apparent disadvantage that their program code is almost invariably longer and more
complex. With memory becoming ever cheaper and of higher density, and with more
efficient compilers for program code generation, this disadvantage is diminishing.
In order to work, all the instructions of the processor must have the same
duration of execution, and it must be possible to split the fetchdecodeexecute cycle
A microcontroller has its program stored internally, and the ALU reads an
instruction from memory. This instruction is decoded by the ALU and executed. At
the completion of the execution of the instruction, the next instruction is fetched from
memory and it is executed. This procedure is repeated until the end of the program is
found, or the program gets into a loop where it is instructed to branch back to a
beginning point. In this case, the machine will stay in the loop forever or until
something happens to release it from the never-ending loop.
There are three ways for a machine locked in a loop to be removed from the
loop so it can execute code outside of the loop. These operations are called
exceptions. The first is to reset the part with a reset signal. A reset signal usually
requires connecting the reset pin of the part to a logic low signal. A logic low is
usually ground. When this condition is detected, several internal registers are set to
Downloaded from www.pencilji.com
A second means of forcing the part out of the loop is for the part to detect an
external interrupt. An external interrupt occurs when the interrupt request (IRQ) pin
on the part is set low. This pin is tested at the beginning of the execution of each
instruction. Therefore, if an instruction is being executed when an IRQ is asserted, the
instruction will complete before the IRQ signal is processed. Processing for the IRQ
consists of first determining if IRQs are enabled. If they are, the status of the machine
is saved. All interrupts are disabled by setting the interrupt mask bit in the status
register of the microcontroller. Then the address stored in the IRQ vector location is
fetched. This address, the address of the interrupt service routine (ISR), is placed in
the program counter. The ISR then executes.
The process of saving the status of the machine is to push the contents of all
machine registers onto the machine stack. Therefore, the ISR can safely use any of the
central machine resources without disrupting the operation of the main line of code
when control is returned. When exiting an ISR, it is necessary to use a special
instruction called a return from interrupt or a return from exception. This instruction
restores the status of the machine from the stack and picks up execution of the code
from the instruction following the one where the interrupt occurred.
The third means for exiting the main loop of the program is from internal
interrupts. The microcontroller peripherals can often cause interrupts to occur. An
internal interrupt causes exactly the same sequence of operations to occur as an
external interrupt. Different interrupt vectors are used for each of the several internal
peripheral parts so the cause of the interrupt is generally known and control is directed
to the specific ISR for each of the several possible internal interrupts.
There are at least two means for the end user of the microcontroller to place
the program memory into the chip. The first is called erasable programmable readonly memory (EPROM). EPROM is a memory technology that can be erased by
exposing it to high-energy ultraviolet light. The EPROM requires the application of a
high voltage to be programmed. The memory can be programmed with either a
development system or a special programming board designed specifically to program
the microcontroller.
Packages that contain EPROM have a quartz glass window through which the
ultraviolet light can pass with minimum attenuation. These packages are quite
expensive, and therefore, microcontrollers with EPROM are usually too expensive to
use. EPROM was used for development purposes in the past, but it is just too
expensive in light of more recent developments to be used for that purpose today.
For limited production purposes, a less expensive version of the EPROM chip
is available. This is the one time programmable (OTP) chip. An OTP chip has the
exact same silicon component as an EPROM, but it is packaged in a standard plastic
package. This package is much less expensive than the windowed package discussed
previously. However, once the chip has been programmed, the program contents
cannot be changed.
There is yet another means of storing programs or, in some instances, data in a
microcontroller. This technique is called electrically erasable programmable read-only
memory (EEPROM). EEPROM is programmable from instructions within the
microcontroller. EEPROM also requires a high programming voltage. If there are
2.5.3 Input/Output
Usually microcontrollers use an architecture called memory mapped I/O. Each
I/O device input and output registers, its control registers, and status registers are
mapped into memory locations. I/O transactions require no special computer
instructions. It is merely necessary to know the memory locations of the pertinent
registers and the uses of the register bits to be able to handle any I/O function. Listed
below are brief descriptions of several microcontroller I/O peripherals found on most
of the microcontrollers. Not all of these peripheral systems are found on each
microcontroller. It is possible to pick and choose between needs for the several
peripheral systems and select a microcontroller that has exactly those peripherals
required.
A second class of timer is the 16-bit timer. This timer is often called a general
purpose timer. These timers contain a 16-bit counter that is clocked by the system
clock. There are two associated subsystems: the first is ca lled an input capture
system, and the second is the output compare.
The input capture system simply captures the value of the system timer
counter when an input occurs. These inputs can set a flag or request an interrupt so the
input can be processed either synchronously or asynchronously. The important fact is
The output compare system allows the programmer to specify a time relative
to the 16-bit counter when an output is to occur. This time is calculated by adding the
time offset value to the current value of the 16-bit counter. This result is stored in the
output compare register. When the 16-bit counter counts to the value in the output
compare register, the output occurs, a bit is set, and an interrupt can be processed if
desired.
Input capture and output compare functions are sometimes called high-speed
inputs and outputs. The number of input captures and output compare systems vary
from as few as one each to as many as 16 programmable timers, each of which can be
either input capture or output compare.
Some I/O pins are multiplexed and serve multiple functions. For example,
microcontrollers with analog-to-digital converters, ADC, usually allow the ADC pins
to serve as digital input pins as well. In that case you need mere ly read the input port,
and those pins that are above the high threshold will indicate one, and those below the
low threshold will indicate zero. Reading the port does not affect the ADC operation
at all.
Many
microcontrollers
have
both
asynchronous
and
synchronous
physically small
low cost
In many microcontroller applications either or both of the following features
a low power capability, and features which ease the use of battery power.
Microcontroller Applications
There is a huge range of microcontroller applications. Some are drawn from
volume markets the motor car, domestic appliances, mobile phones and toys. These
applications are sold in such high volume that dedicated controllers are frequently
developed for them. Others, like medical or scientific instruments, are sold in smaller
numbers, and are more likely to make use of the wide variety of general-purpose
Downloaded from www.pencilji.com
Every microcontroller is different, and each has its own unique combination of
core and peripherals. Figure 2.8 shows, in block diagram form and with no
interconnections, the features, which might be found in a simple general-purpose
controller. The core is the element that remains constant for the whole family built
around it. Ideally all memory is on-chip, and several different memory technologies
may be applied to meet the differing needs of program and data storage.
Interconnection to the outside world is through a number of parallel and serial ports.
A counter/timer is available for event counting, or to measure or generate timing
intervals.
As Intel was the first company to produce a microprocessor, it seems right that
it was also the first to produce a microcontroller. It did this in 1976 with the MCS-48
(appearing in three versions, the 8035, 8048 and 8748). In its time the MCS-48 was
revolutionary. The 8748 had on-chip ultraviolet erasable programmable read-only
memory (EPROM), 64 bytes of RAM, and three input/output ports. It attracted many
developed many
variants. The 80C51 is a CMOS version of the 8051, and Philips has extended this
into a wide-ranging family. Its practice has been to use the whole of the 80C51 as
core, and to add further peripherals to this.
The Motorola 68HC05/08 microcontrollers
Motorola was early in the microprocessor field, but was not the first. By the
time it entered, with the 6800, it was able to offer a device which enjoyed remarkable
longevity. From the 6800 it developed further 8-bit conventional microprocessors
(e.g. the 6809), and also a number of single-chip controllers, starting with the 6801.
These led to the 68HC11, a sophisticated and widely used microcontroller. The HC
infix
Semiconductor) technology with which it is made. From the 68HC11 the 68HC12 and
68HC16, both 16-bit controllers, have been developed.
An indirect development of the 6800 family was the 6805 (M146805 in full),
available initially in HMOS (High-Density N-Channel MOS) and CMOS versions.
Here the CPU was simplified, for example by the removal of the second (B)
accumulator of the 6800, reduction in addressing capability, and consequent reduction
of certain register sizes. As one of the earlier CMOS controllers, the 6805 had a great
impact on low-power applications. The 6805 was subsequently upgrade and reissued
using HC CMOS technology. This has enjoyed very widespread use as a simple and
low-cost microcontroller. Motorola claims that over 2 billion (2 109) units of the
68HC05 have been sold. The number of variants are too many to list, but contain
devices targeted specifically for automotive, computer, consumer, industrial,
telecommunications, TV and video applications.
Since the late 1990s the 68HC05 has been in the process of being replaced by
the 68HC08, which provides a direct upgrade. Both the 05 and the 08 use the 7
Downloaded from www.pencilji.com
C compilers for the microcontrollers have been available long enough that
they are thoroughly tested and do a good job of creating proper code. Anyone who has
programmed a microcontroller in assembly language knows that the programs must
be very direct and have no fancy overhead. Memory is strictly limited, and the
compiler must generate assembly code that is as resourceful as can be created by any
thoroughly qualified assembly language programmer for the machine.
All of the above capabilities are available with the development boards.
Another level of capability is available. These devices are box level, and usually have
a built- in power supply. Most development systems require a host computer, and
usually they come with special software to interface with the host computer. These
systems have all of the capabilities outlined above plus some significant
improvements. The breakpoint capability of these systems is much improved over the
simple address breakpoint above. Here a complicated breakpoint can be employed that will break the
program operation on read or write, at any data or address location, on access of data or program, or access
of a range of data or address locations. Also, the breakpoint can occur after a specified number of
occurrences of the breakpoint conditions.
The data in the trace buffer can be displayed in several different manners. The
simplest, of course, is to print to the computer screen the I/O pattern of all the lines
captured. This type of display is extremely difficult to interpret, but it is useful in
some cases. To help the programmer determine where the microcontroller is
operating, it is possible to read the data bits and display a disassembled version of the
code being read into the microcontroller. This display is also quite useful in
debugging the code.
2.9 Summary
2.11 Questions
1. Explain the working of a microprocessor-based system.
2. Briefly explain the different microprocessor design optio ns. Which one will
you choose? Why?
3. With a block diagram explain a microcontroller.
4. Explain the different subsystems of microcontrollers.
5. What are the characteristics of microcontroller?
6. How
is
microcontroller
programming
different
from
conventional
programming?
UNIT 3
Contents
3.1 Introduction
3.2 Objectives
3.3 Example Embedded Program
3.4 Compiling, Linking and Locating
3.5 Downloading and Debugging
3.6 Summary
3.7 Self Test
3.8 Questions
3.1 Introduction
Embedded systems programming is the development of programs, intended to
be part of a larger operating system or, in a somewhat different usage, to be
incorporated on a microprocessor that can then be included as part of a variety of
In this unit we'll discuss the basic tools required for embedded programming.
Also we will look at an example program to have a feel of what embedded
programming is all about. The program we'll look at is similar in spirit to the "Hello,
World!" example found in the beginning of most other programming books. Unlike
any other programming, the execution of a particular program in embedded systems
depends on the target hardware. We will discuss the parts of the example program that
are dependent on the target hardware.
3.2 Objectives
In this unit you will learn
So you won't find an actual "Hello, World!" program in this unit. Every
embedded system will usually have at least one LED that could be controlled by
software. So, as a substitute for the "Hello, World!" program, we can think of a
program that blinks an LED at a rate of 1 Hz (one complete on-off cycle per second).
We will assume that only the basic syntax of C is available for our example.
The blinking LED program is shown below. This part of the program is
hardware-independent. However, it relies on the hardware-dependent functions
toggleLed and delay to change the state of the LED and to handle the timing,
respectively.
/*************************************************************
* Function: main ()
* Description: Blink the green LED once a second.
* Notes:
delay(500) ;
}
} /* main() */
toggleLed
We will consider the Arcom board to explain a few hardware dependent
concepts. An Arcom board will usually have two LEDs: one red and one green. The
state of each LED is controlled by a bit in a register called the Port 2 I/O Latch
Register (P2LTCH, for short). This register is located within the very same chip as the
CPU and takes its name from the fact that it contains the latched state of eight I/O
pins found on the exterior of that chip. Collectively, these pins are known as I/O Port
2. And each of the eight bits in the P2LTCH register is associated with the voltage on
one of the I/O pins. For example, bit 6 controls the voltage going to the green LED:
#define LED_GREEN 0x40
By modifying this bit, it is possible to change the voltage on the external pin
and, thus, the state of the green LED. As shown in Figure 3.1, when bit 6 of the
P2LTCH register is 1 the LED is off; when it is 0 the LED is on.
The exact syntax of inline assembly varies from compiler to compiler. In this
example, the format used is the one preferred by the Borland c++ compiler. Borland's
inline assembly format is one of the best because it supports references to variables
and constants that are defined within the C code.
#define
P2LTCH
0xFF5E
/*******************************************************
* Function: toggleLed ()
* Description: Toggle the state of one or both LEDs .
* Notes: This function is specific to Arcoms Target188EB board.
* Returns: None defined.
*********************************************************/
void
toggleLed(unsigned char ledMask)
{
asm {
mov dx, P2LTCH
xor al, ah
out dx, a1
};
}
/* toggleLed() */
delay
We also need to implement a half-second (500 ms) delay between LED
toggles. This is done by busy-waiting within the delay routine shown below. This
routine accepts the length of the requested delay, in milliseconds, as its only
parameter. It then multiplies that number by the constant CYCLES_PER_MS to
obtain the total number of while- loop iterations required to delay for the requested
time period
/ **********************************************************
* Function: delay ()
* Description: Busy-wait for the requested number of milliseconds .
* Notes:
* Returns:
None defined.
************************************************************/
void
delay(unsigned int nMilliseconds)
{
#define
CYCLES_PER_MS 260
/* delay() */
That's all there is to the Blinking LED program. The three functions main
toggleLed, and delay do the whole job. If you want to port this program to some other
embedded system, you should read the documentation that came with your hardware,
rewrite toggleLed as necessary, and change the value of CYCLES_PER_MS. In the
next few sections we will see how to build and execute this example program. Before
that we will see what is the role of infinite loops in embedded systems.
In addition, most embedded systems have only one piece of software running
on them. And although the hardware is important, it is not a digital watch or a cellular
phone or a microwave oven without that embedded software. If the software stops
running, the hardware is rendered useless. So the functional parts of an embedded
program are almost always surrounded by an infinite loop that ensures that they will
run forever.
Compiling
Of course, each processor has its own unique machine language, so you need
to choose a compiler that is capable of producing programs for your specific ta rget
processor. In the embedded systems case, this compiler almost always runs on host
computer. It simply doesn't make sense to execute the compiler on embedded system
itself. A compiler such as this - that runs on one computer platform and produces code
for another - is called a cross-compiler. The use of cross-compiler is one of the
defining features of embedded software development.
The GNU C/C++ compiler (gcc) and assembler (as) can be configured as
either native compilers or cross-compilers. As cross-compilers these tools support
impressive set of host-target combinations. Table 3-1 lists some of the most popular
of the supported hosts and targets. Of course, the selections of host platform and
target processor are independent; these tools can be configured for any combination.
Host Platforms
HP 9000/700 HP-UX
Fujitsu SPARClite
Hitachi SH
IBM/Motorola Power PC
Intel i960
Motorola 68k
Sun SPARC, MicroSPARC
Toshiba TX39
Regardless of the input language (C/C++, assembly, or any other), the output
of the cross-compiler will be an object file. This is a specially formatted binary file
that contains the set of instructions and data resulting from the language translation
process. Although parts of this file contain executable code, the object file is not
intended to be executed directly. In fact, the internal structure of an object file
emphasizes the incompleteness of the larger program.
The contents of an object file can be thought of as a very large, flexible data
structure. The structure of the file is usually defined by a standard format like the
Common Object File Format (COFF) or Extended Linker Format (ELF). If you'll be
using more than one compiler (i.e., You'll be writing parts of your program in
different source languages), you need to make sure that each is capable of producing
object files in the same format. Although many compilers (particularly those that run
on Unix platforms) support standard object file formats like COFF and ELF (gcc
supports both), there are also some others that produce object files only in proprietary
formats. If you're using one of the compilers in the latter group, you might find that
you need to buy all of your other development tools from the same vendor.
Most object files begin with a header that describes the sections that follow.
Each of these sections contains one or more blocks of code or data that originated
within the original source file. However, these blocks have been regrouped by the
Downloaded from www.pencilji.com
There is also usually a symbol table somewhere in the object file that contains
the names and locations of all the variables and functions referenced within the source
file. Parts of this table may be incomplete, however, because not all of the variables
and functions are always defined in the same file. These are the symbols that refer to
variables and functions defined in other source files. And it is up to the linker to
resolve such unresolved references.
Linking
All of the object files resulting from step1 must be combined in a special way
before the program can be executed. The object files themselves are individually
incomplete, most notably in that some of the internal variable and function references
have not yet been resolved. The job of the linker is to combine these object files and,
in the process, solve all of the unresolved symbols.
The output of the linker is a new object file that contains all of the code and
data from the input object files and is in the same object file format. It does this by
merging the text, data, and bss sections of the input files. So, when the linker is
finished executing, all of the machine language code from all of the input object files
will be in the text section of the new file, and all of .the initialized and uninitialized
variables will reside in the new data and bss sections, respectively.
While the linker is in the process of merging the section contents, it is also on
the lookout for unresolved symbols. For example, if one object file contains an
unresolved reference to a variable named foo and a variable with that same name is
declared in one of the other object files, the linker will match them up. The
unresolved reference will be replaced with a reference to the actual variable. In other
words, if foo is located at offset 14 of the output data section, its entry in the symbol
table will now contain that address.
If the same symbol is declared in more than one object file, the linker is unable
to proceed. It will likely appeal to the programmer - by displaying an error message and exit. However, if a symbol reference instead remains unresolved after all of the
object files have been merged, the linker will try to resolve the reference on its own.
The reference might be to a function that is part of the standard library, so the linker
will open each of the libraries described to it on the command line (in the order
provided) and examine their symbol tables. If it finds function with that name, the
reference will be resolved by including the associated code and data sections within
the output object file (ie static linking).
Unfortunately, the standard library routines often require some changes before
they can be used in an embedded program. The problem here is that the standard
libraries provided with most software development tool suites arrive only in object
form. So you rarely have access to the library source code to make the necessary
changes yourself.
Startup Code
One of the things that traditional software development tools do automatically
is to insert startup code. Startup code is a small block of assembly language code that
prepares the way for the execution of software written in a high- level language. Each
high- level language has its own set of expectatio ns about the runtime environment.
For example, C and C++ both utilize an implicit stack. Space for the stack has to be
allocated and initialized before software written in either language can be properly
executed. That is just one of the responsibilities assigned to startup code for C/C++
programs.
Most cross-compilers for embedded systems include an assembly language
file called startup.asm, crtO.S (short for C runtime), or something similar. The
location and contents of this file are usually described in the documentation supplied
with the compiler.
But embedded programmers aren't generally finished with the build process at
this point. Even if your embedded system includes an operating system, you'll
probably still need an absolutely located binary image. In fact, if there is an operating
system the code and data of which it consists are most likely within the relocatab le
program too. The entire embedded application - including the operating system - is
almost always statically linked together and executed as a single binary image.
Locating
The tool that performs the conversion from relocatable program to executable
binary image is called a locator. It takes responsibility for the easiest step of the three.
In fact, you will have to do most of the work in this step yourself, by providing
Downloaded from www.pencilji.com
The memory information required by the GNU linker can be passed to it in the
form of a linker script. Such scripts are sometimes used to control the exact order of
the code and data sections within the relocatable program. But here, we want to do
more than just control the order; we also want to establish the location of each section
in memory.
text rom :
{
*( .text)
}
}
This script informs the GNU linker's built- in locator about the memory on the
target board and instructs it to locate the data and bss sections in RAM (starting at
address 0x00000) and the text section in ROM (starting at 0x80000). However, the
initial values of the variables in the data segment will be made a part of the ROM
image by the addition of >rom at the end of that section's definition.
All of the names that begin with underscores (_TopOfStack, for example) are
variables that can be referenced from within your source code. The linker will use
these symbols to resolve references in the input object files. So, for example, there
might be a part of the embedded software (usually within the startup code) that copies
the initial values of the initialized variables from ROM to the data section in RAM.
The start and stop addresses for this operation can be established symbolically, by
referring to the integer variables _DataStart and _DataEnd.
The result of this final step of the build process is an absolutely located binary
image that can be downloaded to the embedded system or programmed into a readonly memory device. In the previous example, this memory image would be exactly 1
MB in size. However, because the initial values for the initialized data section are
stored in ROM, the lower 512 kilobytes of this image will contain only zeros, so only
the upper half of this image is significant. You'll see how to download and execute
such memory images in the next sections.
Building the Example Program
The blinking LED program can be built using Borland's C++ Compiler and
Turbo Assembler. These tools can be run on any DOS or Windows-based PC. The
following command are used:
This command is repeated as many times as the number of source modules are
there (i.e. compile each of the source module),. The result of the above command is
the creation of an object file that has the same prefix as the .c file and the extension
.obj.
We must also include some startup code for the C program. The following
command can be used for that purpose:
The command that's actually used to link all object files together is shown
here. Beware that the order of the object files on the command line does matter in this
case: the startup code must be placed first for proper linkage.
tlink /m /v /s startup_code_object_file soucemodule_object_files,
new_exe_file_name, map_file_name
As a result of the tlink command, Borland's Turbo Linker will produce two
new files: new_exe_file_name and map_file_name in the working directory. The first
file contains the relocatable program and the second contains a human-readable
program map. The map file provides information similar to the contents of the linker
script described earlier. However, these are results and, therefore, include the lengths
of the sections and the names and locations of the public symbols found in the
relocatable program.
One more tool must be used to make the Blinking LED program executable: a
locator. The locating tool we'll be using is provided by Arcom, as part of the
SourceVIEW development and debugging package included with the board. Because
Downloaded from www.pencilji.com
The tcrom locator massages the contents of the relocatable input file-assigning
base addresses to each section-and outputs the file new_exe_file_name.rom. This file
contains an absolutely located binary image that is ready to be loaded directly into
ROM. But rather than load it into the ROM with a device programmer, we'll create a
special ASCII version of the binary image that can be downloaded to the ROM over a
a serial port. For this we will use a utility provided by Arcom, called bin2hex. Here is
the syntax of the command:
This extra step creates a new file, called new_exe_file_name.hex, that contains
exactly the same information as new_exe_file_name.rom, but in an ASCII
representation called Intel Hex Format.
When in ROM
One of the most obvious ways to download your embedded software is to load
the binary image into a read-only memory device and insert that chip into a socket on
After you program the ROM, it is ready to be inserted into its socket on the
board. Of course, this shouldn't be done while the embedded system is still powered
on. The power should be turned off and then reapplied only after the chip has been
carefully inserted. As soon as power is applied to it, the processor will begin to fetch
and execute the code that is stored inside the ROM. However, beware that each type
of processor has its own rules about the location of its first instruction. For example,
when the Intel 80188EB processor is reset, it begins by fetching and executing
whatever is stored at physical address FFFF0h. This is called the reset address and the
instructions located there are collectively known as the reset code. We must always
ensure that the binary image we've loaded into the ROM satisfies the target
processor's reset rules.
Led Debugging: One of the most primitive debugging techniques available is the use
of an LED as indicator of success or failure. The basic idea is to slowly walk the LED
enable code through the larger program. In other words, you first begin with the LED
enable code at the reset address. If the LED turns on, then you can edit the program,
moving the LED enable code to just after the next execution milestone, rebuild, and
test. This works best for very simple, linearly executed programs like the startup code.
All the host computer needs to talk to the monitor program is a serial port and
a terminal program. Instructions for loading an Hex Format file, into the Flash
memory device are usually provided in the user manuals give n for that hardware.
Re mo te Deb uggers
If available, a remote debugger can be used to download, execute, and debug
embedded software over a serial port or network connection between the host and
target. The frontend of a remote debugger looks just like any other debugger that you
might have used. It usually has a text or GUI-based main window and several smaller
windows for the source code, register contents, and other relevant information about
the executing program. However, in the case of embedded systems, the debugger and
the software being debugged are executing on two different computer systems.
A remote debugger actually consists of two pieces of software. The fro ntend
runs on the host computer and provides the human interface just described. But there
is also a hidden backend that runs on the target processor and communicates with the
frontend over a communications link of some sort. The backend provides for low-
The debug monitor resides in ROM - having been placed there in the manner
described earlier (either by you or at the factory}- and is automatically started
whenever the target processor is reset. It monitors the communications link to the host
computer and responds to requests from the remote debugger running there. Of
course, these requests and the monitor's responses must conform to some predefined
communications protocol and are typically of' a very low- level nature. Examples of
requests the remote debugger can make are "read register x," "modify register y,"
"read n bytes of memory starting at address," and "modify the data at address." The
remote debugger combines sequences of these low- level commands to accomplish
high- level debugging tasks like downloading a program, single-stepping through it,
and setting breakpoints.
One such debugger is the GNU debugger (gdb). Like the other GNU tools, it
was originally designed for use as a native debugger and was later given the ability to
perform cross-platform debugging. So you can build a version of the GDB frontend
that runs on any supported host and yet understands the opcodes and register names of
any supported target. Source code for a compatible debug monitor is included within
the GDB package and must be ported to the target platform. However, beware that
this port can be tricky, particularly if you only have LED debugging at your disposal.
Communication between the GDB frontend and the debug monitor is byteoriented and designed for transmission over a serial connection. The command format
Downloaded from www.pencilji.com
Request Format
Response Format
Read registers
data
Write registers
Gdata
OK
maddress,length
data
Maddress,length:data
OK
Start/restart execution
Ssignal
Ssignal
Single step
Ssignal
saddress
Ssignal
Reset/kill program
no response
Remote debuggers are one of the most commonly used downloading and
testing tools during development of embedded software. This is mainly because of
their low cost. Embedded software developers already have the requisite host
computer.
In addition, the price of a remote debugger frontend does not add significantly
to the cost of a suite of cross-development tools (compiler, linker, locator, etc.).
Finally, the suppliers of remote debuggers often desire to give away the source code
for their debug monitors, in order to increase the size of their installed user base.
Emulato rs
Remote debuggers are helpful for monitoring and controllin8 the state of
embedded software, but only an in-circuit emulator (ICE) allows you to examine the
state of the processor on which that program is running. In fact, an ICE actually takes
the place of - or emulates - the processor on your target board. It is itself an embedded
system, with its own copy of the target processor, RAM, ROM, and its own embedded
software. As a result, in-circuit emulators are usually pretty expensive - often more
Like a debug monitor, an emulator uses a remote debugger for its human
interface. In some cases, it is even possible to use the same debugger frontend for
both. But because the emulator has its own copy of the target processor it is possible
to monitor and control the state of the processor in real time. This allows the emulator
to support such powerful debugging features as hardware breakpoints and real-time
tracing, in addition to the features provided by any debug monitor. With a debug
monitor, you can set breakpoints in your program. However, these software
breakpoints are restricted to instruction fetches-the equivalent of the command "stop
execution if this instruction is about to be fetched." Emulators, by contrast, also
support hardware breakpoints. Hardware breakpoints allow you to stop execution in
response to a wide variety of events. These events include not only instruction
fetches, but also memory and I/O reads and writes, and interrupts. For example, you
might set a hardware breakpoint on the event "variable foo contains 15 and register
AX becomes 0."
Once you have access to your target hardware-and especially during the
hardware debugging- logic analyzers and oscilloscopes can be indispensable
debugging tools. They are most useful for debugging the interactions between the
processor and other chips on the board. Because they can only view signals that lie
outside the processor, however, they cannot control the flow of execution of your
software like a debugger or an emulator can. This makes these tools significantly less
useful by themselves. But coupled with a software-debugging tool like a remote
debugger or an emulator, they can be extremely valuable.
Most of the debugging tools described in this chapter will be used at some
point or another in every embedded project. Oscilloscopes and logic analyzers are
most often used to debug hardware problems-simulators during early stages of the
software development, and debug monitors and emulators during the actual software
debugging.
3.6 Summary
Embedded systems are among the most difficult computer platforms for
programmers to work with. Most embedded systems lack a monitor or analogous
output device. Embedded programmers must be self- reliant. They must always begin
each new project with the assumption that nothing works - that all they can rely on is
the basic syntax of their programming language.
(True/False)
Answers
1. cross compiler
2. object file
3. d) All the above
4. target processor
3.8 Questions
1. Briefly explain the steps involved in building an embedded program
2. What is an Emulator? What are the advantages of a ROM emulator?
3. What are the challenges an embedded programmer has to face
4. List out the tools used to build an embedded program along with their
functionalities.
5. What is the role of infinite loop in embedded systems?
6. Explain the process of linking and locating
UNIT 4
Contents
4.1 Introduction
4.2 Objectives
4.3 Embedded Operating System
4.3.1 Tasks
4.3.2 Scheduler
4.3.3 Synchronization
4.4 RTOS design issues
4.4.1
What is a RTOS?
Applications of RTOSs
4.4.3
Design options
4.4.3.1 Polled Loop Systems
4.4.3.2 Phase/State-Driven Systems
4.4.3.3 Interrupt Driven Systems
4.4.3.3.1
4.4.3.3.2
Hybrid System
Background Processing
4.4.3.4.2
Initialization
4.1 Introduction
All but the most trivial of embedded programs will benefit from the inclusion
of an operating system. This can range from a small kernel to a full- featured
commercial operating system. Either way, you'll need to know what features are the
most important and how their implementation will affect the rest of your software.
Embedded
applications start running special purpose programs at power on and will not stop
until turned off. These applications will not usually have any peripheral support
(keyboard, monitor, serial connections, mass storage, etc.) or a user interface. Often
an embedded OS must provide real-time response to perform its requirements.
4.2 Objectives
In this unit you will learn
What is RTOS?
VxWorks
Modern operating systems add to this the ability to execute multiple software
tasks simultaneously on a single processor. Each such task is a piece of the software
that can be separated from and run independently of the rest. A set of embedded
software requirements can usually be decomposed into a small number of such
independent pieces.
Actually embedded operating systems are easier to write than their desktop
counterparts - the required functionality is smaller and better defined. Embedded
operating systems are small because they lack many of the things you would expect to
find on your desktop computer. For example, embedded systems rarely have disk
drives or graphical displays, and hence they need no file system or graphical user
interface in their operating systems. In addition, there is only one "user" (i.e., all of
the tasks that comprise the embedded software cooperate), so the security features of
multiuser operating systems do not apply. All of these are features that could be part
of an embedded operating system but are unnecessary in the majority of cases.
4.3.1 Tasks
Task is a piece of software that can be executed independently of others. In
multitasking many tasks are executed simultaneously. An operating system makes it
possible to execute multiple programs at the same time. In actuality, the tasks are not
executed at the same time. Rather, they are executed in pseudoparallel. They merely
take turns using the processor.
Operating system is responsible for deciding which task gets to use the
processor at a particular moment. In addition, it maintains information about the state
of each task. This information is called the task's context. A task's context records the
state of the processor just prior to another task's taking control of it. This usually
consists of a pointer to the next instruction to be executed (the instructio n pointer), the
address of the current top of the stack (the stack pointer), and the contents of the
processor's flag and general-purpose registers. On 16-bit 80x86 processors, these are
the registers CS and IP, SS and SP, Flags, and DS, ES, SI, Dl, AX, BX, CX, and DX
respectively.
In order to keep tasks and their contexts organized, the operating system
maintains a bit of information about each task. Operating systems often keep this
Contents of register n
Other context
Running
Ready
Waiting
Sometimes some part of the code of important tasks needs to be exec uted with
out an interruption. We can put such code in critical sections. A critical section is a
part of the program that must be executed atomically. That is, the instructions that
make up that part must be executed in order and without interruption. Bec ause an
interrupt can occur at any time, the only way to make such a guarantee is to disable
interrupts for the duration of the critical section.
4.3.2 Scheduler
The important element of any operating system is its scheduler. This is the
piece of the operating system that decides which of the ready tasks has the right to use
the processor at a given time. Some of the more common scheduling algorithms used
are: first- in-first-out, shortest job first, and round robin. These are simple scheduling
algorithms that are used in non-embedded systems.
First- in- first-out (FIFO) scheduling describes an operating system like DOS,
which is not a multitasking operating system at all. In this each task runs until it is
finished, and only after that is the next task started. However, in DOS a task can
suspend itself, thus freeing up the processor for the next task. And that's precisely how
older version of the Windows operating system permitted users to switch from one
task to another. True multitasking wasn't a part of a ny Microsoft operating system
before Windows NT.
Shortest job first describes a similar scheduling algorithm. The only difference
is that each time the running task completes or suspends itself, the next task selected
Round robin is the only scheduling algorithm of the three in which the running
task can be preempted, that is, interrupted while it is running. In this case, each task
runs for some predetermined amount of time. After that time interval has elapsed, the
running task is preempted by the operating system and the next task in line gets its
chance to run. The preempted task doesn't get to run again until all of the other tasks
have had their chances in that round.
This means that at any given moment the task that is currently using the
processor is guaranteed to be the highest-priority task that is ready to do so. Lowerpriority tasks must wait until higher-priority tasks are finished using the processor
before resuming their work. The word preemptive adds tha t any running task can be
interrupted by the operating system if a task of higher priority becomes ready. The
scheduler detects such conditions as a finite set of time instants called scheduling
points.
Schedul i ng poi nt s
Simply stated, the scheduling points are the set of operating system events that
result in an invocation of the scheduler. Two such events are : task creation and task
deletion. During these events the next task to run is selected. If the currently executing
Downloaded from www.pencilji.com
A third scheduling point is called the clock tick. The clock tick is a periodic
event that is triggered by a timer interrupt. The clock tick provides an opportunity to
awake tasks that are waiting for a software timer to expire. In fact, support for
software timers is a common feature of embedded operating systems. During the
clock tick, the operating system decrements and checks each of the active software
timers. When a timer expires, all of the tasks that are waiting for it to complete are
changed from the waiting state to the ready state. Then the scheduler is invoked to see
if one of these newly awakened tasks has a higher priority than the task that was
running prior to the timer interrupt.
Ready list
The scheduler uses a data structure called the ready list to track the tasks that
are in the ready state. Usually, the ready list is implemented as an ordered linked list
or ordinary linked list. So the head of this list is always the highest Priority task that is
ready to run. Following a call to the scheduler, this will be the same as the currently
running task. In fact, the only time that won't be the case is during a reschedule.
Figure 4.3 below shows what the ready list might look like while the operating
systems ID use.
Idle task
If there are no tasks in the ready state when the scheduler is called, the idle
task will be executed. The idle task looks the same in every operating system. It is
simply that does nothing. Usually in an OS, the idle task is completely hidden from
the application developer. It does, however, will have a valid task ID and priority
(both of which are zero, by the way). The idle task is always considered to be in the
Downloaded from www.pencilji.com
Context Switch
The actual process of changing from one task to another is called a context
switch. Because contexts are processor-specific, so is the code that implements the
context switch. That means it must always be written in assembly language.
Pieces of code that access shared resources contain critical sections. We've
already seen something similar inside the operating system. There, we simply
disabled interrupts during the critical section. But tasks cannot (wisely) disable
interrupts. If they were allowed to do so, other tasks - even higher-priority tasks that
didn't share the same resource - would not be able to execute during that interval. So
we want and need a mechanism to protect critical sections within tasks without
disabling interrupts. And mutexes provide that mechanism.
This situation is illustrated in Figure 4.4. Here there are three tasks: high
priority, medium priority and low priority. Low becomes ready first (indicated by the
rising edge) and, shortly thereafter, takes the mutex. Now, when high becomes ready,
it must block (indicated by the shaded region) until low is done with their shared
resource. The problem is that Medium, which does not even require access to that
resource, gets to preempt Low and run even though it will delay High's use of the
processor. Many solutions to this problem have been proposed, the most common of
which is called "priority inheritance." This solution has Low's priority increased to
that of High as soon as High begins waiting for the mutex. Some operating systems
include this "fix" within their mutex implementation.
//Do Forever
If (IsProcessReady == True){
RunProcess( );
IsProcessReady = False;
//End if
//End Do forever
Polled loop is not the way to go if more than one process needs to be executed.
However, some systems may have to wait a fixed amount of time before the switching
of the flag becomes stable. This can be handled via an interrupt, and may not be
needed depending on the bounce of the switch from false to true. If an interrupt is
used inside of the polled- loop it is now a polled-loop with interrupt.
There are advantages of a polled-loop system. They are only useful if a
processor is dedicated to handling one process and the event does not occur at a high
using a case statement that runs a state of the process and then
performs a context switch to the next process. The case statement may look like the
following example:
void Process( ) {
Switch (State)
Case 1:
State1 ( );
State = 2;
Switch( );
State2 ( );
State = 3;
Switch( );
break;
Case 2:
break;
default:
//Reset Process
Switch ( );
This method will work as long as the states are kept small and can run in a
reasonable amount of time. It would also be difficult to implement more than two
processes without also implementing a kernel to schedule and dispatch the processes.
Some processes may not be able to be broken down into states and this could arise in
some processes taking longer than others. With no method of preemption, some high
priority processes may not execute in time resulting in system failure. To implement
4.4.3.4.2 Initialization
To initialize the system, a background process should be run first that sets up
all the data structures for each process, sets up any necessary registers and vector
jump tables needed for interrupts. Finally, interrupts should be enabled and the system
should begin running processes.
This problem can be solved by giving each process its own unique set of data
that completely defines the process. This block is referred to as a PCB or process
control block (similar to task control block). This block should have a unique ID
number for each process, a pointer to the processes stack, and the state of the process.
The PCB idea can be extended to become a node for a list. Then, a pointer to the next
node of the list will also need to be in the PCB. This structure will allow the OS to
skip processes or execute processes depending on the state their in.
The microprocessor services the event 3which makes a higher priority task ready
for execution. Upon completion, the ISR invokes the kernel, which decides to run the
more important task 4. The higher priority task executes to completion unless it also
gets interrupted 5. At the end of the task, the kernel resumes the lower priority task 6.
The lower priority task continues to execute 7.
A preemptive kernel can ensure that time critical tasks are performed first. In
other words, without a kernel we may not get to perform a task that is time critical
because we would be working on something that is not. Furthermore, execution of
time critical tasks are deterministic and are almost insensitive to software changes.
TASKS
A task is basically an infinite loop and looks as shown below. In order for the
kernel to allow other tasks to perform work, you must invoke a service provided by
At any given time, a task is in one of six states: Dormant, Ready, Running,
Delayed, Waiting for an event or Interrupted. The dormant state corresponds to a
task which resides in memory but has not been made available to the multitasking
kernel. A task is ready when it can execute but its priority is less than the current
running task. A task is running when it has control of the CPU. A task is delayed
when the task is waiting for time to expire. A task is waiting for an event when it
requires the occurrence of an event: waiting for an I/O operation to complete, a shared
resource to be available, a timing pulse to occur, etc. Finally, a task is interrupted
when an interrupt occurred and the CPU is in the process of servicing the interrupt.
You must invoke a service provided by the kernel to have the kernel manage
each task. This service creates a task by taking it from its dormant state to the
ready state. Each task requires its own stack and where the task is concerned, it has
access to most CPU registers. The function prototype for a task creation function is
shown below.
void OSTaskCreate(void (*task)(void), void *stack, UBYTE priority)
task is
pointer to the top-of-stack of the stack to be used by the task. priority is the tasks
relative priority - its importance with respect to all other tasks in the system.
The kernel keeps track of the state of each task by using a data structure called
the Task Control Block (TCB). The TCB contains the status of the task (Ready,
Delayed or Waiting for an event), the task priority, the pointer to the tasks top-ofstack and other kernel related information. Figure 4.6 shows the relationship between
the task stacks, the TCBs and the CPU registers. Based on events, the kernel will
Figure 4.6: The relationship between the task stacks, the TCBs and
the CPU registers.
EVENT MANAGEMENT
The kernel provides services to allow a task to suspend exec ution until an
event occurs. The most common type of event to wait for is the semaphore. A
semaphore is used to either control access to a shared resource (mutual exclusion),
signal the occurrence of an event or allow two tasks to synchronize their activit ies. A
semaphore generally consist of a value (an unsigned 16-bits variable) and a waiting
list (see figure 4.8).
SYSTEM TICK
Every kernel provides a mechanism to keep track of time. This is handled by a
hardware timer which interrupts the CPU periodically. The ISR for this timer invokes
a service provided by the kernel which is responsible for updating inter nal time
dependant variables. Specifically, a number of services rely on timeouts. This ISR is
generally called the clock tick ISR. The clock tick interrupt is usually set to occur
between 10 and 100 times per second. A task can thus suspend its executio n for an
integral number of clock ticks. A special list is used to keep track of tasks that are
waiting for time to expire. This list is called the delayed task list. The delayed task list
can be set up as a delta list which basically orders delayed tasks so that the first task
in the list has the least amount of time remaining to expire. For example, if you had
five tasks with values of 10, 14, 21, 32 and 39 tenths of a second remaining to timeout
then, the list would contain 10, 4, 7, 11 and 7. The tota l time before the first task to
expire is 10, the second is 10+4, the third is 10+4+7, the fourth is 10+4+7+11 and
finally, the fifth task would be 10+4+7+11+7.
INTERRUPT PROCESSING
Kernels provide services that are accessible from ISRs to notify tasks about
the occurrence of events. In order to use these services, however, your ISRs must save
all CPU registers and notify (through a function) the kernel that an ISR has been
entered. In this case, the kernel simply increments a nesting counter (generally a n 8bit variable). The nesting counter is used to determine the nesting level of interrupts
(how many interrupts were interrupted). Upon completion of the ISR code, your ISR
must invoke another service provided by the kernel to notify it that the ISR has
completed. This service basically decrements the nesting counter. When all interrupts
have returned to the first interrupt nesting level, the kernel determines if a higher
priority task has been made ready-to- run by one of the ISRs. If the interrupted task is
The designers of real- time systems spend a large amount of their time
worrying about worst-case performance. They must constantly ask themselves
questions like the following: What is the worst-case time between the human operator
pressing the brake pedal and an interrupt signal arriving at the processor? What is the
worst-case interrupt latency, the time between interrupt arrival and the start of the
associated interrupt service routine (ISR)? And what is the worst-case time for the
software to respond by triggering the braking mechanism? Average or expected-case
analysis simply will not suffice in such systems.
Interrupt latency is the total length of time from an interrupt signal's arrival at
the processor to the start of the associate interrupt service routine. When an interrupt
occurs, the processor must take several steps before executing the ISR. First, the
processor must finish executing the current instruction. That probably takes less than
one clock cycle, but some complex instructions require more time than that. Next, the
Downloaded from www.pencilji.com
Of course, if interrupts are ever disabled within the operating system, the
worst-case interrupt latency increases by the maximum amount of time that they are
turned off. But as we have just seen, there are many places where interrupts are
disabled. These are the critical sections we talked about earlier, and there are no
alternative methods for protecting them. Each operating system will disable interrupts
for a different length of time, so it is important that you know what your systern's
requirements are. One real- time project might require a guaranteed interrupt response
time as short as 1 microsecond, while another requires only 100 microseconds.
The VxWorks operating system consists of a small kernel (which controls the
execution of tasks) and surrounding facilities provided for application use. Many
surrounding facilities (and some parts of the kernel) may be scaled out of the
VxWorks image if the application does not need them. Some facilities (e.g.
networking) may be useful during development even if they are not needed in the
final product; it is easy to remove such facilities from the shipping image.
M emory
M anagement
Device
Support
File
Systems
Scheduling,
System Clock
Facilities, M utual
Exclusion,
Synchronization
&ITC
I/O
System
Networking Support
Tornado Environment
Tornado is an integrated development environment for cross-development of
real-time and embedded applications.(running on Host Machine)
RS 232
Development System
with standard OS &
RTOS tools
Network Connection
Tornado includes:
The VxWorks real-time operating system, running on the target computer.
The Tornado Tools, which allow developers to download, test, and debug
their applications.
The GNU compiler tool chain (compiler, assembler, linker, make, and binary
utilities). These tools run on the host, and produce code for the target.
The Target Server (host) and WDB agent (target), which together allow the
tools to communicate with and control the target.
The WDB agent interprets the request and calls the appropriate functionality.
The result of calling that functionality is then packaged and sent back to the Target
Server on the host, which then passes the results to the tool, which initiated the
transaction. The target server and WDB Agent communicate via the Wind
Debug(WDB) protocol.
Wtxregd is the Wind Registry daemon, which tracks available target servers .It
must be running on the host. The wtxregd manages the information a tool needs to
connect to a target server. The wtxregd must be started before target server and tools
Tools initially contact the registry to find out how to contact a particular target server.
The cross compiler is an executable on the host, generating an image for the
target hardware. Using cross compilers and linkers, compile and link the two sources,
and generate an image. There would be a mechanism provided (by the OS and your
hardware) to move the image from the host machine to the target hardware
The Operating System Source tree consists of the source shipped by the RTOS
vendor, and usually includes, OS primitives ( task/thread libraries / IPC support) and
Framework and library support for developers to support newer devices. The boardspecific code for initializing and managing a boards hardware is called the BSP. The
BSP provides VxWorks with standard hardware interface functions which allow it to
run on a board.
Tornado includes an integrated target simulator, which allows application
development prior to hardware availability.
The wind kernel also provides message queues, pipes, sockets, and signals
for intertask communication. It comes with an optional component called VxMP
that provides shared- memory objects as a communication mechanism for tasks
executing on different CPUs. The wind kernel uses interrupt-driven, preemptive
priority-based task scheduling with 256 priorities and round robin scheduling as
shown below:
Every subroutine is a separate task in itself and has its own context and
resource stack. Context switching is required between tasks to transfer information
between tasks. Wind kernel features fast deterministic context switching with low
interrupt latency. Other task control facilities provided by the kerne l are: suspend,
resume, delete, delay, and move. Task synchronization and mutual exclusion is done
Using the VxWorks POSIX API, new application could be developed for
VxWorks target and port existing applications or application components to VxWorks
systems from other development and target platforms. The POSIX functions invoke
the VxWorks kernel and other routines to communicate between tasks and routines
and to coordinate the use of system resources.
I/O System: VxWorks has seven basic I/O routines: create(), remove(), open(),
close(), read(), write(), and ioctl(). Higher- level I/O routines such as ANSI C
compatible print f() and scanf() routines are also provided. It also has a standard
buffered I/O package (stdio) that includes ANSI C-compatible routines such as
fopen(), fclose(), fread(), fwrite(), getc(), and putc(). The VxWorks I/O system also
includes POSIX compliant asynchronous I/O that includes a library of routines that
perform I/O operations along with a tasks other activities. VxWorks includes device
drivers for serial communication, disks, RAM disks, SCSI tape devices, intertask
communication devices (called pipes), and devices on a network. It also provides
facilities for additional drivers to be written by the developer. It allows dynamic
installation and removal of drivers without rebooting the system. When a I/O request
is sent by the user, the I/O system does very little processing of the request and gives
control of the request to the device driver. It works like a switch in transferring the
Downloaded from www.pencilji.com
Shared Memory: VxWorks provides memory sharing. But memory sharing can
pose a threat of data corruption when two or more processes try to access the shared
memory simultaneously as shown below:
This requires task synchronization and mutual exclusion, which is done using
Semaphores in VxWorks. Semaphores coordinate the several tasks that are sharing the
same memory and avoid data corruption. There are four kinds of semaphores in
VxWorks:
1. Binary Semaphores: For synchronization and mutual exclusion. These are the
fastest and most commonly used semaphores.
2. Counting Semaphores: For mutual exclusion specifically.
3. Mutual-Exclusion Semaphores: Similar to Binary Semaphores. In addition it keeps
track of number of times semaphore is given.
4. POSIX semaphores: POSIX defines both named and unnamed semaphores. The
POSIX semaphore library provides routines for creating, opening, and destroying both
named and unnamed semaphores.
The VxMP option with VxWorks facilitates sharing of message queues and
semaphores. With the VxMP option, the above- mentioned inter processor
communication functions are made available to entire system. The developer may
design applications using shared memory for simple sharing of data, message queues
Local File System: VxWorks provides fast file systems tailored to real-time
applications. It provides the following compatible file system:
File system compatible with the MS-DOS file system dosFs: Allows efficient
organization of files and permits arbitrary number of files to be created. It provides
the ability to specify contiguous file allocation for any file for enhanced performance.
Provides compatibility with other storage media like diskette and hard drives created
with dosFs. Name lengths not restricted to eight characters like is the case in MSDOS.
File system compatible with the RT-11 file system rt11Fs: This file system is used
for real-time applications because all files are contiguous,which results in very high
performance. VxWorks provides rt11Fs compatibility including byte-addressable
random access to all files. Eachopen file has a block buffer for optimized reading and
writing.
File system compatible with the raw disk file system rawFs: In rawFs the
entire disk is considered to be one large file. It permits reading and writing portions of
the disk, which is specified by a byte offset and performs simple buffering. When
only simple, low- level disk I/O is required, it has the advantages of size and speed.
File system compatible with SCSI tape devices tapeFs: In tapeFs compatibility,
VxWorks does not provide a standard file or directory structure on tape. Instead the
tape is considered as one large file. The higher- level layer does data organization on
the tape.
formatted in accordance with ISO 9660 file system standards can be read in
VxWorks. Data on a CD-ROM device can be accessed using the standard POSIX I/O
calls.
C++ Development Support: VxWorks provides support for C++ libraries including
the i/o stream class library and the standard template library. The optional component
Wind Foundation Classes adds the VxWorks Wrapper Class library and Tools.h++
library from Rogue Wave.
Utility Libraries: VxWorks provides an extensive set of over 1100 utility routines for
the help of application developers in solving common problems. Some o f the most
commonly used utility routines are given below:
Interrupt handling support: VxWorks supplies routines for handling hardware
interrupts and software trap. Interrupt handling is nested and prioritized. An interrupt
runs in a special context outside any task, which makes it faster. In addition, routines
are provided for connecting C routines to hardware interrupt vectors, and to
manipulate the processor interrupt level.
Watchdog timers: A watchdog timer can be used to delay the execution of a task.
When the watchdog timer is activated, the task goes into a delayed state. When the
delay time period elapse, an interrupt service routine is called that returns control to
the task, unless the watchdog timer is canceled first.
Message logging: The message logging facility allows applications to log a formatted
error or status message to a system- wide logging device, such as the system console,
disk, or accessible memory.
Memory allocation: Memory management is one of the most critical aspects of
realtime systems. VxWorks provides a memory management facility that allocates,
frees, and reallocates blocks of memory from a memory pool.
Downloaded from www.pencilji.com
Board Support Packages: Board Support Packages (BSPs) are available with
VxWorks for a variety of boards and provide routines for hardware initialization,
interrupt setup, timers, memory mapping, and so on. Two target-specific libraries,
sysLib and sysALib, are included with each port of VxWorks. These libraries provide
an identical software interface to the hardware functions of all boards.
All VxWorks network facilities comply with standard Internet protocols, both
loosely coupled over serial lines or standard Ethernet connections and tightly coupled
over a backplane bus using shared memory.
and
remote
procedure
calls,
for
network-transparent
intertask
communication.
Signals, for exception handling.
Once a pipe is created, a task can read or write from it by using the read() and
write() routines. The created pipe is a normally named I/O device. Tasks can use the
standard I/O routines to open, read, and write pipes, and invoke ioctl routines. Similar
to message queues, if a task tries to write to a pipe that is full, the task is pended until
Host platform supported by VxWorks: Sun3, Sun4, HP9000, IBM RS-6000, DEC,
SGI, and MIPS.
Applications of VxWorks:
Aerospace Industry: Flight Simulators, Satellite Tracking Systems etc.
Transportation Industry: ABS, Realtime Suspension, Traffic Control Systems etc.
4.8 Summary
An embedded system runs on embedded platform. The basic elements of a
simple embedded operating system are the scheduler and scheduling points, context
switch routine, definition of a task, and a mechanism for intertask communication. A
real-time operating system (RTOS) is an embedded operating system that guarantees
a certain capability within a specified time constraint. A real time kernel is software
that manages the time of a microprocessor to ensure that all time critical events are
processed as efficiently as possible. VxWorks is one of the most popular commercial
real-time operating system available in the market.
1.
.. Kernels require that each task odes something to explicitly give up the
control of CPU.
2.
3.
Scheduling is the process of picking up the TCB at the head of ready list.
(True/ False)
4.
5.
6.
7.
8.
Answers
1. Non-preemptive
2. ready list
3. True
4. True
5. a
6. Foreground
7. True
8. d
4.11 Questions
1. Briefly explain the important elements of Embedded OS
2. What is a RTOS? How does it differ from a general Embedded OS?
3. Explain Foreground/Background Systems.
4. How are the events managed in real time kernels?
5. What are the types of kernel? Briefly explain
6. Explain the performance characterstics of an RTOS.
References