K. Shibu-Introduction To Embedded Systems-Tmh (2009) - Text
K. Shibu-Introduction To Embedded Systems-Tmh (2009) - Text
EMBEDDED
SYSTEMS
Introduction to
Embedded Systems
ABOUT THE AUTHOR
Shibu has over nine years of hands-on experience in the Embedded & Real Time System domain with
solid background on all phases of Embedded Product Development Life Cycle. He holds a B-Tech
Degree (Hons) in Instrumentation & Control Engineering from the University of Calicut. He started his
professional career as Research Associate in the VLSI and Embedded Systems Group of India’s prime
Electronics Research & Development Centre (ER&DCI.—under the ministry of Information & Com¬
munication Technology, Govt, of India) Thiruvananthapuram Unit. He has conducted research activities
in the embedded domain for Contactless Smart Card Technology and Radio Frequency Identification
(RFID). He has developed a variety of products in the Contactless Smart Card & RFID Technology
platform using industry’s most popular 8-bit microcontroller—8051.
Shibu has sound working knowledge in Embedded Hardware development using EDA Tools and
firmware development in Assembly Language and Embedded C using a variety of IDEs and cross-
compilers for various 8/16/32 bit microprocessors/microcontrollers and System on Chips. He is also an
expert in RTOS based Embedded System design for various RTOSs including Windows CE, VxWorks,
MicroC/OS-II and RTX-51. He is well versed in various industry standard protocols and bus interfaces.
Presently, he is engaged with the Embedded Systems & Product Engineering practice unit of Infosys
Technologies Ltd fwww.infosvs.com), Thiruvananthapuram Unit.
Introduction to
Embedded Systems
Shibu KV
Technical Architect
Mobility & Embedded Systems Practice
Infosys Technologies Ltd.,
Trivandrum Unit, Kerala
Me
Graw
Hill
Education
No part of this publication may be reproduced or distributed in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise or stored in a database or
retrieval system without the prior written permission of the publishers. The program listings
(if any) may be entered, stored and executed in a computer system, but they may not be
reproduced for publication.
Information contained in this work has been obtained by McGraw Hill Education (India),
from sources believed to be reliable. However, neither McGraw Hill Education (India) nor
its authors guarantee the accuracy or completeness of any information published herein, and
neither McGraw Hill Education (India) nor its authors shall be responsible for any errors,
omissions, or damages arising out of use of this information. This work is published with the
understanding that McGraw Hill Education (India) and its authors are supplying information
but are not attempting to render engineering or other professional services. If such services are
required, the assistance of an appropriate professional should be sought.
Typeset at The Composers, 260, C.A. Apt., Paschim Vihar, New Delhi 110063 and printed at
Sanat Printers, 312 EPIP, HSIDC, Kundli, Sonepat, Haryana
I dedicate this book to all Infoscions, my wife, my unborn kid,
my parents, my friends and my loved'ones
Preface
Acknowledgements
Part 1
Embedded System: Understanding the Basic Concepts
1. Introduction to Embedded Systems
1.1 What is an Embedded System? 4
1.2 Embedded Systems vs. General Computing Systems 4
1.3 Ehstory of Embedded Systems 5
1.4 Classification of Embedded Systems 6
1.5 Major Application Areas of Embedded Systems 7
1.6 Purpose of Embedded Systems 8
1.7 ‘ Smart’ Running Shoes from Adidas—The Innovative Bonding of
Lifestyle with Embedded Technology 11
Summary 13
Keywords 13
Objective Questions 14
Review Questions 14
Objective Questions 67
Review Questions 69
Lab Assignments 71
Keywords 221
Objective Questions 222
Review Questions 223
Lab Assignments 224
Part 2
Design and Development of Embedded Product
Index
There exists a common misconception amongst students and young practising engineers that embed¬
ded system design is ‘writing ‘C’ code’. This belief is absolutely wrong and I strongly emphasise that
embedded system design refers to the design of embedded hardware, embedded firmware in ‘C’ or other
supporting languages, integrating the hardware and firmware, and testing the functionalities of both.
Embedded system design is a highly specialised branch of Electronics Engineering where the techno^
logical advances of electronics and the design expertise of mechanical engineering work hand-in-hand
to deliver cutting edge technologies and high-end products to a variety of diverse domains including
consumer electronics, telecommunications, automobile, retail and banking applications. Embedded sys¬
tems represent an integration of computer hardware, software along with programming concepts for
developing special-purpose computer systems, designed to perform one or a few dedicated functions.
The embedded domain offers tremendous job opportunities worldwide. Design of embedded system
is an art, and it demands talented people to take up the design challenges keeping the time frames in
mind. The biggest challenge faced by the embedded industry today is the lack of skilled manpower
in the domain. Though most of our fresh electronics and computer science engineering graduates are
highly talented, they lack proper training and knowledge in the embedded domain. Lack of suitable
books on the subject is one of the major reasons for this crisis. Although various books on embedded
technology are available, almost none of them are capable of delivering the basic lessons in a simple
and structured way. They are written from a high-end perspective, which are appropriate only for the
practising engineers.
This book ‘Introduction to Embedded Systems’ is the first-of-its-kind, which will appeal as a com¬
prehensive introduction to students, and as a guide to practising engineers and project managers. It has
been specially designed for undergraduate courses in Computer Science & Engineering, Information
Technology, Electrical Engineering, Electronics & Communication Engineering and Instrumentation
& Control Engineering. Also, it will be a valuable reference for the students of BSc / MSc / MTech
(CS/IT/Electronics), MCA and DOEACC ‘B’ level courses.
The book has been organised in such a way to provide the fundamentals of embedded systems;
the steps involved in the design and development of embedded hardware and firmware, and their inte¬
gration; and the life cycle management of embedded system development. Chapters 1 to 4 have been
structured to give the readers a basic idea of an embedded system. Chapters 5 to 13 have been organised
to give an in-depth knowledge on designing the embedded hardware and firmware. They would be very
helpful to practising embedded system engineers. Chapter 15 dealing with the design life cycle of an
embedded system would be beneficial to both practising engineers as well as project managers. Each
chapter begins with learning objectives, presenting the concepts in simple language supplemented with
ample tables, figures and solved examples. An important part of this book comes at the end of each
chapter where you will find a brief summary, list of keywords, objective questions (in multiple-choice
format) and review questions. To aid students commence experimentation in the laboratory, lab assign¬
ments have been provided in relevant chapters. An extremely beneficial segment at the end of the book
is the overview of PIC & AVR Family of Microcontrollers & ARM Processors as well as innovative
design case studies presenting real-world situations.
For Instructors
• PowerPoint slides (chapter-wise)
• A brief chapter on Embedded Programming Language C++/ Java
« Case studies that are given in the book and one new case study on heart beat monitoring system
• Solution manual (chapter-wise)
® Short questions, quizzes in the category of fill in the blanks, true/false and multiple choice ques¬
tions (25); programming questions with solution (5). (Level of difficulty: hard)
• Chapter-wise links to important websites and important text materials
Preface
For Students
• Chapter-wise tutorials
• A brief chapter on Embedded Programming Language C++/ Java
• Case studies that are given in the book and one new case study on heart beat monitoring system
• Answers for objective questions/selected review questions and hints for lab assignments provided
in the book.
• Short questions, self-test quizzes in the category of fill in the blanks, true/false and multiple choice
questions (25); programming questions with solutions (5). (Level of difficulty: easy/medium)
• List of project ideas
• Chapter-wise links to important websites and important text materials.
This book is written purely on the basis of my working knowledge in the field of embedded hardware
and firmware design, and expertise in dealing with the life cycle management of embedded systems.
A few descriptions and images used in this book are taken from websites with prior written copyright
permissions from the owner of the site/author of the articles.
The design references and data sheets of devices including the parametric reference used in the il¬
lustrative part of this book are taken from the following websites. Readers are requested to visit these
sites for getting updates and more information on design articles. Also, you can order some free samples
from some of the sites for your design.
www.intel.com Intel Corporation
www.maxim-ic.com Maxim/Dallas Semiconductor
www.atmel.com Atmel Corporation
www.analog.com Analog Devices
www.microchip.com Microchip Technology
www.ti.com Texas Instruments
www.nxp.com NXP Semiconductors
www.national.com National Semiconductor
www.fairchildsemi.com Fairchild Semiconductor
www.intersil.com Intersil Corporation
www.freescale.com Freescale Semiconductor
www.xilinx.com Xilinx (Programmable Devices)
www.orcad.com Cadence Systems (Oread Tool)
www.keil.com Keil (MicroVision 3 IDE)
www.embedded.com Online Embedded Magazine
www.elecdesign.com Electronic Design Magazine
I would be looking forward to suggestions for further improvement of the book. You may contact me
at the following email id—[email protected] .com. Kindly mention the title and author name in
the subject line. Wish you luck as you embark on an exhilarating career path!
ShibuKV
Acknowledgements
I take this opportunity to thank Mr Mohammed Rijas (Group Project Manager, Mobility and Embedded
Systems. Practice, Infosys Technologies • Ltd Thiruvananthapuram) and Mr Shafeer Badharudeen
(Senior Project Manager, Mobility and Embedded Systems Practice, Infosys Technologies Ltd
Thiruvananthapuram) for their valuable suggestions and guidance in writing this book. I am also grateful
to my team and all other members of the Embedded Systems Group, Infosys Technologies for inspiring'
me to write this book. I acknowledge my senior management team at the Embedded Systems and
Mobility practice, Infosys Technologies—Rohit P, Srinivasa Rao M, Tadimeti Srinivasan, Darshan
Shankavaram and Ramesh Adiga—for their constant encouragement and appreciation of my efforts.
I am immensely grateful to Mr R Ravindra Kumar (Senior Director, CDAC Thiruvananthapuram) for
giving me an opportunity to work with the Hardware Design Group of CDAC (Erstwhile ER&Dd),
Mrs K G Sulochana (Joint Director CDAC Thiruvananthapuram) for giving me the first embedded
project to kick start my professional career and also for all the support provided to me during my tenure
with CDAC. I convey my appreciation to MrBiju C Oommen (Addl. Director, Hardware Design Group,
CDAC Thiruvananthapuram), who is my great source of inspiration, for giving me the basic lessons of
embedded technology, Mr S Krishna Kumar Rao, Mr Sanju Gopal, Ms Deepa R S, Mr Shaji NM and
Mr Suresh R Pillai for their helping hand during my research activities at CDAC, and Mr Praveen VL
whose contribution in designing the graphics of this book is noteworthy. I extend my heartfelt gratitude
to all my friends and ex-colleagues of Hardware Design Group CDAC Thiruvananthapuram—without
their prayers and support, this book would not have been a reality. Last but not the least, I acknowledge
my beloved friend Dr Sreeja for all the moral support provided to me during this endeavor, and my
family members for their understanding and support during the writing of this book.
A token of special appreciation to Mr S Krishnakumar Rao (Deputy Director, Hardware Design
Group, CDAC Thiruvananthapuram) for helping me in compiling the section on VLSI Design and
Mr Shameerudheen P T for his help in compiling the section on PCB Layout Design using Oread
Layout Tool.
Acknowledgements
I would like to extend my special thanks to the following persons for coordinating and providing
timely feedback on all requests to the concerned product development/service companies, semiconductor
manufacturers and informative web pages.
A note of acknowledgement is due to the following reviewers for their valuable suggestions for the
book.
Prabhat Ranjart
Dhirubhai Ambani Institute of Information and Communication Technology (D AIICT), Gandhinagar
Lyla B Das
National Institute of Technology Calicut (NITC), Calicut
Finally, I thank the publishing team at McGraw-Hill Education India, more specifically Vibha Mahajan,
Shalini Jha, Nilanjan Chakravarty, Surbhi Suman, Dipika Dey, Anjali Razdan and Baldev Raj for their
praiseworthy initiatives and efficient management of the book.
Shibu K V
Learning Objectives
\f LEARNING OBJECTIVES
Each chapter has been neatly divided 6.2.1 Data Transfer Instructions
into Sections and Subsections so Data transfer instructions transfer data between a source and destination. The source can be an internal
data memory, a register, external data memory, code memory or immediate data. Destination may be an
internal data memory, external data memory or a register.
that the subject matter is studied'ih 6.2.1.1 Internal Data 7Yansfer Operations Internal data transfer instructions perform the move¬
a logical 'progression of ideas and ment of data between, register, memory location, accumulator and stack. MOV instruction is used for
data transfer between register, memory location and accumulator. PUSH and POP instructions transfer
data between memory location/register and stack. The following table summarises the various internal
concepts.- data transfer instructions
c Photographs bom" embe<fded*sys(cms*slor*eThe coTlcctccfdata for processing an"danalyses* £uch*sys{cms*incor- •
^/ate a buill-in/plug-in storage memory for storing the captured data. Some of them give the user a
meaningful representation of the collected data
by visual (graphical/quantilative) or audible
means using display units [Liquid Crystal Dis¬
....
'
descriptions bring the subject .to of the same to the user, whereas the data is used
for internal processing.
A digital camera is a typical example of an
life. , embedded system with data colleclion/storage/
representation of data. Images are captured and
the captured image may be stored within the
memory of the camera. The captured image can
also be presented to the user through a graphic
LCD unit.
laimi mmk
Illustrations
5.3.2.I The Program (Code) Memory The basic 8051 architecture provides lowest 4K bytes
of program memory as on-chip memory (built-in chip memory). In 8031, the ROMless counterpart of
8051, all program memory' is external to the chip. Switching between the internal program memory and
external program memory is accomplished by changing the logic level of the pin External Access (EA\)
Tying EA\ pin to logic 1 (I'), configures the chip to execute instructions from program memory^up to
4K (program memory location up to OFFFH) from internal memory and 4K (program memory’ location
from 1000H) onwards from external memory, while connecting EA\ pin to logic 0 (GND) configures the
chip to external program execution mode, where the entire code memory is executed from the external
memory. Remember External Access ptn is an active low pin (Normally referred as EA\). The control
signal for external program memory execution is PSEN\ (Program Strobe Enable). For internal program
Chapter Summary and Keywords
y | Summary 1
'SCAD A' 'fv and Data Acquisition Systcm/A data acquisition system used ip indus-
1 Objective Questions ]
9
(e) All of these (0 Noncof these
A digital multi meter is an example of an embedded system for
students to apply and integrate
(a) Data communication (b) Monitoring (c) Control (d) All of I
(e) None of these chapter content.
10 Which of the following is an (arc) cxample(s) of an embedded system for signal processing0
(a) Apple iPOD (media player device) (b) SanDisk USB mass storage device
(c) Bolh (a) and (b) (d) None of these
t. Write a *C’ program U> find the CTiiAonneu of the processor in which the program isrunning. If the processor
is big endian,'print “The'processor architecture is Blgendian”, else print “The processor architecture is Little
endian”- on lhc .oonsolc;,wmdowl:^Execute- the .program . separatelyon a PC-with Windows, Linux and MAC
operalingsystems, ffi '■i’/'A&c..-k ,, 1. _
2. Drawlheirderfacingdiagrara for connecting an LED to the port pin of a microcontroller. The LED is turned ON
whmtte:^aocoRlroHet'i^|uiis atXogic^d*: Calculate(he resistance required to conncct'in series with the
■ 1£Dfpr.^.fii|lowids'daigR:eannwtei^;4 .■■< , vs .■
To aid students conduct experiments (a) 'LED yoltage'd^.ohc<Mdiicting=:l.7V'" '' :
•• (b) L£D cufreht.rating=20mA C- , , , >
have been provided at the end of .ji -.‘i-; - < 'i ' •' hi
4. Design ;a;zener diode and (ran^or based brown-out prtrtectjori circuit with active low reset pulse for the
following design parameters. - v >- >
relevant chapters.
i^DiGitjLCirb^k
A comprehensive set of five Case
Design and Implement a Digital Clock as per the requirements given below. Studies, at the. end of the: Took
t. Use a 16 character 2-line display for displaying the current Time. Display the time in DAY HH:MM:SS format
on the first line. Display the message ‘Have A Nice Day! ’ on the second line. DAY represents the day of the Week
(SUN, MON, TUE, WED, THU, FRJ and SAT). HH represents the hour in 2 digit format. Depending on the/ormat
demonstrate theL applications of
settings it may va^ from 00 to 12 (12 Hour Format) or from 00 to 23 (24 Hour format). MM represents the minute
in 2 digit format. It varies from 00 to 59. SS represents the seconds in 2 digit format. It varies from 00 to 59. The theoretical Concepts.::
alignment of display character should be centre justified (Meaning if the characters to display arc 12, pack it with
2 blank space each at right and left and then display).
2. Interface a Hitachi HD44780 compatible LCD with 8051 microcontroller as per the following interface details
Data Bus: Port P2
Register Select Control line (RS): Port Pin PI .4
Read/Write Control Signal (RW) Port Pin P1.5
LCD Enable (E): P 1.6
3. The Backlight of the LCD should be always ON
4. Use AT89C51/52 or AT89S8252 microcontroller. Use a crystal Oscillator with frequency 12.00 MHz.
This will provide accurate timing of 1 microsecond/machine cycle.
5. A2 x 2 matrix key board (4 keys) is interfaced to Port PI of the microcontroller. The key details are MENU key
connected to Row 0, and Column 0 of the matrix; ESC key connected to Row 0, and Column 1 of the matrix,
UP key connected to Row 1, and Column 0 of the matrix; DOWN key connected to Row 1, and Column 1 or the
matrix. The Rows 0, I and columns 0, 1 of matrix keyboard are interfaced to Port pins Pl.O, PI.I, P1.2 and Pl-3
PIC1® is a popular 8/16/32 bit RISC microcontroller family from Microchip Technology (www.microchip.com). The 8bit
PIC family comprises the products PIC10F, PICI2F, PICI6F and PICI8F They differ in the amount of program memory
supported, performance, instruction length and pm count. Based on the architecture, the 8bii PIC family is grouped into
ifeseiine Products based on Die original PIC architecture They support I2bil instruction set with very limned fea¬
to give an overview of PIC & AVR tures They are available in 6 to 40 pin packages. The 6 pin I0F series, some 12F series (8 pin 12F629) and some 16F
series (The 20-pin 16F690 and 40-pin 16F887) falls under this category •
family of microcontrollers & ARM Mid-Range This is an extension to the baseline architecture with added features like support for interrupts, on-chip
peripherals (Like A/D converter, Serial Interfaces, LCD interface, etc.), increased memory, etc. The instruction set for
this architecture is Mbit wide. They arc available in 8 to 64 pin packages with operating voltage in the range 1.8V to
processor. 5.5 V. Some products of the I2F (The 8 pin 12F629) and the 16F (20-pin 16F690 and 40-pin 16F887) senes comes under
this category
High Performance The PIC 18F J and K scries comes under this category The memory density far these devices
is very high (Up to 128KB program memory and 4KB data memory). They provide buill-m support for advanced periph¬
erals and communication interfaces like USB, CAN, etc The instruction set for this architecture is I6bit wide They are
capable of delivering a speed of 16MIPS.
As mentioned earlier wc have a bunch of devices to select for our design, from the PIC 8 bit family. Some of them have
limited set of I/O capabilities, on-chip Peripherals, data memory (SRAM) and program memory (FLASH) targeting low
cr.d applications The 12F}08 controller is a typical example for this. It contains 512 x 12 bits of program memory. 25
bytes of RAM. 6 I/O lines and comes in an 8-pin package. On the other hand some of the PIC family devices are feature
rich with a bunch of on-chip peripherals (Like ADC, UART, I2C, SPI, etc.), higher data and program storage memory,
targeting high end applications. The I6F877 controller is a typical example for this. It contains 8192 x 14 biisofFLASH
program memory, 368 bytes of RAM, 256 bytes ofEEPROM, 33 I/O lines. On-chip peripherals like 10-bit A/D converter,
3-Timer units, USART, Interrupt Controller, etc. it comes in a 40-pin package
Here, the 16F877 device is selected as the candidate for illustrating the generic PIC architecture. Figure Al l illus- 1
* ►r-4''!e'V •••••••«*••••••••••••••«•••*
Understanding the basic concepts is essential in the learning of any subject. Designing an Embedded
System is not a herculean task if you know the fundamentals. Like any general computing systems,
Embedded Systems also possess a set of characteristics which are unique to the embedded system under
consideration. In contrast to the general computing systems, Embedded Systems are highly domain and
application specific in nature, meaning; they are specifically designed for certain set of applications in
certain domains like consumer electronics, telecom, automotive, industrial control, measurement sys¬
tems etc. Unlike general computing systems it is not possible to replace an embedded system which is
specifically designed for an application catering to a specific domain with another embedded system
catering to another domain. The designer of the embedded system should be aware of its characteristics,
and its domain and application specific nature.
An embedded system is an electrical/electro mechanical system which is specifically designed for an
application catering to a specific domain. It is a combination of specialised hardware and firmware (soft¬
ware), which is tailored to meet the requirements of the application under consideration. An embedded
system contains a processing unit which can be a microprocessor or a microcontroller or a System on
Chip (SoC) or an Application Specific Integrated Circuit (ASIC)/Application Specific Standard Product
(ASSP) or a Programmable Logic Device (PLD) like FPGA or CPLD, an I/O subsystem which facili¬
tates the interfacing of sensors and actuators which acts as the messengers from and to the ‘Real world’
to which the embedded system is interacting, on-board and external communication interfaces for com¬
municating between the various on-board subsystems and chips which builds the embedded system and
external systems "to"which the embedded system interacts, and other supervisory systems and support
units like watchdog timers, reset circuits, brown-out protection circuits, regulated power supply unit,
clock generation circuit etc. which empower and monitor the functioning of the embedded system. The
design of embedded system has two aspects: The hardware design which takes care of the selection of
the processing unit, the various I/O sub systems and communication interface and the inter connec¬
tion among them, and the design of the embedded firmware which deals with configuring the various
sub systems, implementing data communication and processing/controlling algorithm requirements.
Depending on the response requirements and the type of applications for which the embedded system
is designed, the embedded system can be a Real-time or a Non-real time system. The response require¬
ments for a real-time system like Flight Control System, Airbag Deployment System for Automotive etc,
are time critical and the hardware and firmware design aspects for such systems should take these into
account, whereas the response requirements for non-real time systems like Automatic Teller Machines
(ATM), Media Playback Systems etc, need not be time critical and missing deadlines may be acceptable
in such systems.
Like any other systems, embedded systems also possess a set of quality attributes, which are the
non-functional requirements like security, scalability, availability, maintainability, safety, portability etc.
The non-functional requirements for the embedded system should be identified and should be addressed
properly in the system design. The designer of the embedded system should be aware of the different
non-functional requirement for the embedded system and should handle this properly to ensure high
quality.
This section of the book is dedicated for describing the basic concepts of Embedded Systems. The
chapters in this section are organised in a way to give the readers a comprehensive introduction to 'Em¬
bedded Systems, their application areas and their role in real life’, ‘ The different elements of a typical
Embedded System’, the basic lessons on ‘The characteristics and quality attributes of Embedded Sys¬
tems’, ‘Domain and Application specific usage examples for Embedded-Systems’ and the ‘Hardware and
Software Co-design approach for Embedded System Design’, and a detailed introduction to ’The archi¬
tecture and programming concepts for 8051 Microcontroller - The 8bit Microcontroller selected for our
design examples’. We will start the section with the chapter on ‘Introduction to Embedded Systems’
d
3,
v LEARNING OBJECTIVES
m
« -V :• S, " 1 - 1 * S ' > ‘ 1 U V? .
•. - -.' .■■■•....■■ -: : v.; • ' . .. - .' ■ -,:.. - • ';. .
Our day-to-day life is becoming-more and more dependent on “embedded systems” and digital
techniques. Embedded technologies are bonding into our daily activities even without our knowledge.
Do you know the fact that the refrigerator, washing machine, microwave oven, air conditioner, televi¬
sion, DVD players, and music systems that we use in our home are built around an embedded system?
You may be traveling by a.‘Honda’ or a ‘Toyota’ or a ‘Ford’ vehicle, but have you ever thought of the
genius players working behind the special features and security systems offered by the vehicle to you? It
is nothing but an intelligent embedded system. In your vehicle itself the presence of specialised embed¬
ded systems vary from intelligent head lamp controllers, engine controllers and ignition control systems
to complex air bag control systems to protect you in case of a severe accident. People experience the
power of embedded systems and enjoy the features and comfort provided by them. Most of us are to¬
tally unaware or ignorant of the intelligent embedded systems giving us so much comfort and security.
Embedded systems are like reliable servants-they don’t like to reveal their identity and neither they
complain about their workloads to their owners or bosses. They are always sitting in a hidden place and
are dedicated to their assigned task till their last breath. This book gives you an overview of embedded
systems, the various steps involved in their design and development,alpd the major domains where they
are deployed.
Introduction to Embedded Systems
Contains a General Purpose Operating System (GPOS) May or may hot contain an operating : system; for
:;r . > functioning -- ' A "'*? ***’
tThe illustration given here is based on the processor details available till Dec 2008. Since processor technology is undergoing rapid
changes, the processor names mentioned here may not be relevant in future.
Wcwiftvi
>k-:d ti't (>:■ •ietcnariurti ;< *-*iv.utk>n hcbavf.'u tatecatirw N*ha^>w !-, tk^roir.i^i^ tv certafi. typo .-t
embedded systems -d;c 'Hard Refit Tmio’ kystenfs
However, the demarcation between desktop systems and embedded systems in certain areas of
embedded applications are shrinking in certain contexts. Smart phones are typical examples of this.
Nowadays smart phones are available with RAM up to 256 MB and users can extend most of their
desktop applications to the smart phones and it waives the clause “Embedded systems are designed for
a specific application” from the characteristics of the embedded system for the mobile embedded device
category. However, smart phones come with a built-in operating system and it is not modifiable by the
end user. It makes the clause: “The firmware of the embedded system is unalterable by the end user”,
still a valid clause in the mobile embedded device category.
RY OF EMBEDDED SYS'
Embedded systems were in existence even before the IT revolution. In the olden days embedded systems
were built around the old vacuum tube and transistor technologies and the embedded algorithm was
developed in low level languages. Advances in semiconductor and nano-technology and IT revolution
gave way to the development of miniature embedded systems. The first recognised modem embedded
system is the Apollo Guidance Computer (AGC) developed by the MIT Instrumentation Laboratory for
the lunar expedition. They ran the inertial guidance systems of both the Command Module (CM) and.
the Lunar Excursion Module (LEM). The Command Module was designed to encircle the moon while
the Lunar Module and its crew were designed to go down to the moon surface and land there safely. The
Lunar Module featured in total 18 engines. There were 16 reaction control thrusters, a descent engine
and an ascent engine. The descent engine was ‘designed to’ provide thrust to the lunar module out of
the lunar orbit and land it safely on the moon. MIT’s original design was based on 4K words of fixed
memory (Read Only Memory) and 256 words of erasable memory (Random Access Memory). By June
1963, the figures reached 10K of fixed and IK of erasable memory. The final configuration was 36K
words of fixed memory and 2K words of erasable memory. The clock frequency of the. first microchip
proto model used in AGC was 1.024 MHz and it was derived from a 2.048 MHz crystal clock. The
computing unit of AGC consisted of approximately 11 instructions and 16 bit word logic. Around 5000
ICs (3-input NOR gates, RTL logic) supplied by Fairchild Semiconductor were used in this design. The
user interface unit of AGC is known as DSKY (display/keyboard). DSKY looked like a calculator type
keypad with an array of numerals. It was used for inputting the commands to the module numerically.
Introduction to Embedded Systems
The first mass-produced embedded system was the guidance computer for the Minuteman-I mis¬
sile in 1961. It was the ‘Autonetics D-l7 guidance computer, built using discrete transistor logic and a
hard-disk for main memory. The first integrated circuit was produced in September 1958 but comput¬
ers using them didn’t begin to appear until 1963. Some of their early uses were in embedded systems,
notably used by NASA for the Apollo Guidance Computer and by the US military in the Minuteman-II
intercontinental ballistic missile.
It is possible to have a multitude of classifications for embedded systems, based on different criteria.
Some of the criteria used in the classification of embedded systems are:
1. Based on generation
2. Complexity and performance requirements
3. Based on deterministic behaviour
4. Based on triggering.
The classification based on deterministic system behaviour is applicable for ‘Real Time’ systems.
The application/task execution behaviour for an embedded system can be either deterministic or non-
deterministic. Based on the execution behaviour, Real Time embedded systems are classified into Hard
and Soft. We will discuss about hard and soft real time systems in a later chapter. Embedded Systems
which are ‘Reactive’ in nature (Like process control systems in industrial control applications) can be
classified based on the trigger. Reactive systems can be either event triggered or time triggered.
1.4.1.1 First Generation The early embedded systems were built around 8bit microprocessors
like 8085 and Z80, and 4bit microcontrollers. Simple in hardware circuits with firmware developed in
Assembly code. Digital telephone keypads, stepper motor control units etc. are examples of this.
1.4.1.2 Second Generation These are embedded systems built around 16bit microprocessors and
8 or 16 bit microcontrollers, following the first generation embedded systems. The instruction set for
the second generation processors/controllers were much more complex and powerful than the first gen¬
eration processors/controllers. Some of the second generation embedded systems contained embedded
operating systems for their operation. Data Acquisition Systems, SCADA systems, etc. are examples of
second generation embedded systems.
1.4.1.3 Third Generation With advances in processor technology, embedded system developers
started making use of powerful 32bit processors and 16bit microcontrollers for their design. A new con¬
cept of application and domain specific processors/controllers like Digital Signal Processors (DSP) and
Application Specific Integrated Circuits (ASICs) came into the picture. The instruction set of processors
became more complex and powerful and the concept of instruction pipelining also evolved. The proces¬
sor market was flooded with different types of processors from different vendors. Processors like Intel
Pentium, Motorola 68K, etc. gained attention in high performance embedded requirements. Dedicated
embedded real time and general purpose operating systems entered into the embedded market. Embed¬
ded systems spread its ground to areas like robotics, media, industrial process control, networking, etc.
Introduction to Embedded Systems
1.4.1.4 Fourth Generation The advent of System on Chips (SoC), reconfigurable processors and
multicore processors are bringing high performance, tight integration and miniaturisation into the em¬
bedded device market. The SoC technique implements a total system on a chip by integrating different
functionalities with a processor core on an integrated circuit. We will discuss about SoCs in a later chap¬
ter. The fourth generation embedded systems are making use of high performance real time embedded
operating systems for their functioning. Smart phone devices, mobile internet devices (MIDs), etc. are
examples of fourth generation embedded systems.
1.4.1.5 What Next? The processor and embedded market is highly dynamic and demanding. So
‘what will be the next smart move in the next embedded generation?’ Let’s wait and see.
instrumentation domain, collects data and gives a meaningful representation of the collected data by
means of graphical representation or quantity value and deletes the collected data when new data arrives
at the data collection terminal. Analog and digital CROs without storage memory are typical examples
of this. Any measuring equipment used in the medical domain for monitoring without storage function¬
ality also comes under this category.
Some embedded systems store the collected data for processing and analysis. Such systems incor¬
porate a built-in/plug-in storage memory for storing the captured data. Some of them give the user a
meaningful representation of the collected data
by visual (graphical/quantitative) or audible
means using display units [Liquid Crystal Dis¬
play (LCD), Light Emitting Diode (LED), etc.]
buzzers, alarms, etc. Examples are: measuring
instruments with storage memory and monitor¬
ing instruments with storage memory used in
medical applications. Certain embedded systems
store the data and will not give a representation
of the same to the user, whereas the data is used
for internal processing.
A digital camera is a typical example of an
. embedded system with data collection/storage/
representation of data. Images are captured and ( Fig. l.l] A digital camera for image capturing/ <
storage/display
the captured image may be stored within the (Photo courtesy of Casio-Model EXIL1M ex-Z850
memory of the camera. The captured image can (wwwi casio. com))
also be presented to the user through a graphic
LCD unit.
modules (Bluetooth, ZigBee, Wi-Fi, EDGE, GPRS, etc.) or wire-line modules (RS-232C, USB, TCP/IP,
PS2, etc.). Certain embedded systems act as a dedicated transmission unit between the sending and
receiving terminal's, offering sophisticated functionalities like data packetizing, encrypting and decrypt¬
ing. Network hubs, routers, switches, etc. are typical examples of dedicated data transmission embedded
systems. They act as mediators in data communication and provide various features like data security,
monitoring etc.
Embedded systems falling under this category are spe¬ (Siemens TRIANO 3 Digital hearing:aid;l- i '
1.6.5 Control
Embedded systems with control functionalities
[ Fig. 1.4) A patient monitoring system for
impose control over some variables according to the
monitoring heartbeat
changes in input variables. A system with control (Photo courtesy of Philips Medical Systems
functionality contains both sensors and actuators. (www.medicalphilips.com/))
the changes in environmental variable or measuring variable. The actuators connected to the output port
are controlled according to the changes in input variable to put an impact on the controlling variable to
bring the controlled variable to the specified range.
Air conditioner system used in our home to control the room temperature to a specified limit is a typi¬
cal example for embedded system for control purpose. An airconditioner contains a room temperature¬
sensing element (sensor) which may be a therm¬
istor and a handheld unit for setting up (feeding)
the desired temperature. The handheld unit may
be connected to the central embedded unit resid¬
ing inside the airconditioner through a wireless link
or through a wired link. The air compressor unit ESG21HR1A
(www.electrblux.com/au) )
perature and the controlled variable is also the room
temperature. The controlling variable is cool air flow by the compressor unit. If the controlled variable
and input variable are not at the same value, the controlling variable tries to equalise them through
taking actions on the cool air flow.
TECHNOLOGY .
A hall effect sensor is positioned at the top of the “cushioning element”, and the magnet is placed at
the bottom of the element. As the cushioning compresses on each impact, the sensor measures the dis¬
tance from top to bottom of mid-sole (accurate to 0.1 mm). About 1000 readings per second are taken
and relayed to the shoe’s microprocessor. The Microprocessor (MPU) is positioned under the arch of
the shoe. It runs an algorithm that compares the compression messages received from the sensor to a
preset range of proper cushioning levels, so it understands if the shoe is too soft or too firm. Then the
MPU sends a command to a micro motor, housed in the mid-foot. The micro motor turns a lead screw
to lengthen or shorten a cable secured to the walls of a plastic-cushioning element. When the cable is
shortened, the cushioning element is pulled taut and compresses very little. A longer cable allows for
a more cushioned feel. A replaceable 3-V battery powers the motor and lasts for about 100 hours of
running.
The Portland, Ore-based Adidas Innovation Team that developed the shoe was led by Christian
DiBenedetto. It also included electromechanical engineer Mark Oleson, as well as a footwear developer
and two industrial designers. Oleson explains that the teain chose a magnetic sensor because it could
measure the amount of compression in addition to the time it took to reach full compression. Gather¬
ing sensor data, he says, meant little without building a comparative “running context”. So one of the
first steps in developing the MPU algorithms was building this database. Runners wore test shoes that
gathered information about various compression levels during a run. Then the runners were interviewed
to learn their thoughts about the different cushion
levels. “When the two matched up, that helped
validate our sensor,” says Oleson.
Adaptations in the cushioning element account
for the change of running surface and pace of the
runner, and they’re made gradually over an aver¬
age of four running steps. The goal is for the run¬
ner not to feel any sudden changes. Adaptations
are made during the “swing” phase rather than the
“stance” phase of the stride (i.e. when the foot is
off the ground). If the shoe’s owner prefers a more
cushioned or a firmer “ride,” adjustments can be
made via “+” and buttons that also activate the
intelligent functions of the shoe.
LED indicators confirm when the electronics
are turned on (The lights do not remain on when
the shoes are in use). If the shoes aren’t turned on,
they operate like old-fashioned “manual” running
shoes. The shoes turn off if their owner is either
inactive or at a walking pace for 10 minutes.
Source Electronic Design
www.electronicdesign.com/Articles/Index. Electronics-enabled “Smart” running
cfm?AD=l&ArticleID=10113 shoes from Adidas
(Photo courtesy of Adidas - Salomon AC
Re-printed with permission
(www.adidas.com))
Introduction to Embedded Systems
~ Summary
S Based on the complexity and performance requirements, embedded systems are classified into small-scale,
medium-scale and large-scale/complex.
V The presence of embedded systems vary from simple-electronic toys to complex flight and missile control
systems. . ?■. - -" , * - k ,Vj- ■
V Embedded systems are designed to serve the purpose of any one or a combination of data collection/storage/
representation, data communication, data (signal) processing, monitoring, control or application specific user
interface. !: -
Keywords
Embedded system An electronie/electro-mechanical system which is designed to perform a specific function and
is a combination of both hardware and firmware ‘ ., ' '
Microprocessor A silicon chip representing a Central Processing Unit (CPI!) .
Microcontroller A highly integrated chip that contains a CPU, scratchpad RAM. special and general purpose
register arrays and integrated peripherals
Digital Signal Processor is a powerful special purpose 8/16/32 bit microprocessor designed
specifically to meet the computational demands and power constraints V: •;
Application Specific Integrated Circuit is a microchip designed to perform a specific or unique
application
Sensor A transducer device that converts energy from one form to another for any measurement or
control purpose
Actuator A form of transducer device (mechanical or electrical) which converts signals to correspond¬
ing physical action (motion) •
Light Emitting Diode. An output device producing visual indication in the form of light in
accordance with current flow
Buzzer Apiezo-electric device for generating audio indication. It contains a piezo-electric diaphragm
which produces audible sound in response to the voltage applied to it
Operating system A piece of software designed to manage and allocate system resources and execute other
pieces of software
Electro Cardiogram An embedded device for heartbeat monitoring
(ECG)
SCADA Supervisory Control and Data Acquisition System. A data acquisition system used in indus¬
trial control applications
Random Access memory. Volatile memory
: Analog to Digital Converter. An integrated circuit which converts analog signals to digital
form
Introduction to Embedded Systems
Bluetooth : A low cost, low power, short range wireless technology for data and voice communication
' : ^beless'Eidelity is'the popular wireless communication technique for networked communis
cation of devices
Objective Questions
/ Learn the building blocks of a typical Embedded System •> ' ;V:F,f1 .■
; [earn about General-Purpose Processors (GPPs), Application Specific Instruction Set Processors (ASIPs), Micropro-
cessdrs, Microcontrollers, Digital Signal Processors, RISC & CISC processors,- Harvard and-Von-Neumann Processor
Architecture, Big-endian v/s Little endian processors, Load Store:operation and Instruction pipelining
PS Learn about different PLDs like Complex Programmable Logic Devices (CPLDs), Field Programmable Gate Arrays
(FPGAs), etc. . ' i ifcP,
f** Learn about the different memory technologies and memory types used in^embedded system development ,
✓ learn about Masked ROM (MROM), PROM, OTP, EPROM, EL PROM and FLASH memory for embedded firmware storage
■/ Learn about Serial Access Memory (SAM), Static Random'Access Memory (SRAM), Dynamic Random Access Memory
(DRAM) and Nonvolatile SRAM (NVRAM)
S Understand the'different factors to be considered in the selection of memory for embedded systems
/ Understand the role of sensors, actuators and their interfacing with the I/O subsystems of an embedded system
/ Learn about the interfacing of LEDs, 7-segment LED Displays, Piezo Buzzer, Stepper Motor,- Relays, Optocouplers,
Matrix keyboard, Push button switches, Programmable Peripheral Interface Device (e.g. 8255 PPI), etc.-with the
I/O subsystem of the embedded system \
M Learn about the different communication interfaces of an embedded system
S Understand the various chip level communication interfaces like I2C, SPI, UART, 1-wire, parallel bus, etc.
M Understand the different wired and wireless external communication interfaces like RS-232C, RS-485, Parallel Port,
USB, IEEE1394, Infrared (IrDA), Bluetooth, Wi-Fi, ZigBee, GPRS, etc.
Z Know what embedded firmware is and its role in embedded systems
Z Understand the different system components like Reset Circuit, Brown-out protection circuit, Oscillator Unit, Real-
Time Clock (RTC) and Watchdog Timer unit
■M Understand the role of PCB in embedded systems
A typical embedded system (Fig. 2.1) contains a single chip controller, which acts as the master brain
of the system. The controller can be a Microprocessor' (e.g. Intel 8085) or a microcontroller (e.g. Atmel
AT89C51) or a Field Programmable Gate Array (FPGA) device (e.g. Xilinx Spartan) or a Digital Signal
Processor (DSP) (e.g. Blackfin® Processors from Analog Devices) or an Application Specific Integrated
Introduction to Embedded Systems
FPGA/ASIC/DSP/SoC
Embedded
Microprocessor/controller
Firmware
Real World
Circuit (ASIC)/Application Specific Standard Product (ASSP) (e.g. ADE7760 Single Phase Energy
Metreing IC from) Analog Devices for energy metering applications).
Embedded hardware/software systems are basically designed to regulate a physical variable or to ma¬
nipulate the state of some devices by sending some control signals to the Actuators or devices connected
to the O/p ports of the system, in response to the input signals provided by the end users or Sensors
which are connected to the input ports. Hence an embedded system can be viewed as a reactive system.
The control is achieved by processing the information coming from the sensors and user interfaces, and
controlling some actuators that regulate the physical variable.
Key boards, push button switches, etc. are examples for common user interface input devices where¬
as LEDs, liquid crystal displays, piezoelectric buzzers, etc. are examples for common user interface
output devices for a typical embedded system. It should be noted that it is not necessary that all embed¬
ded systems should incorporate these I/O user interfaces. It solely depends on the type of the application
for which the embedded system is designed. For example, if the embedded system is designed for any
handheld application, such as a mobile handset application, then the system should contain user inter¬
faces like a keyboard for performing input operations and display unit for providing users the status of
various activities in progress.
Some embedded systems do not require any manual intervention for their operation. They automati¬
cally sense the variations in the input parameters in accordance with the changes in the real world, to
which they are interacting through the sensors which are connected to the input port of the system. The
The Typical Embedded System
sensor information is passed to the processor after signal conditioning and digitisation. Upon receiving
the sensor data the processor or brain of the embedded system performs some pre-defined operations
with the help of the firmware embedded in the system and sends some actuating signals to the actua¬
tor connected to the output port of the embedded system, which in turn acts on the controlling variable
to bring the controlled variable to the desired level to make the embedded system work in the desired
manner.
The Memory of the system is responsible for holding the control algorithm and other important con¬
figuration details. For most of embedded systems, the memory for storing the algorithm or configuration
data is of fixed type, which is a kind of Read Only Memory (ROM) and it is not available for the end
user for modifications, which means the memory is protected from unwanted user interaction by imple¬
menting some kind of memory protection mechanism. The most common types of memories used in
embedded systems for control algorithm storage are OTP, PROM, UVEPROM, EEPROM and FLASH.
Depending on the control application, the memory size may vary from a few bytes to megabytes. We
will discuss them in detail in the coming sections. Sometimes the system requires temporary memory
for performing arithmetic operations or control algorithm execution and this type of memory is known
as “working memory”. Random Access Memory (RAM) is used in most of the systems as the working
memory. Various types of RAM like SRAM, DRAM and NVRAM are used for this purpose. The size
of the RAM also varies from a few bytes to kilobytes or megabytes depending on the application. The
details given under the section “Memory” will give you a more detailed description of the working
memory.
An embedded system without a control algorithm implemented memory is just like a new bom baby.
It is having all the peripherals but is not capable of making any decision depending on the situational as
well as real world changes. Ihe only difference is that the memory of a new bom baby is self-adaptive,
meaning that the baby will tiy to learn from the surroundings and from the mistakes committed. For
embedded systems it is the responsibility of the designer to impart intelligence to the system.
In a controller-based embedded system, the controller may contain internal memory for storing the
control algorithm and it may be an EEPROM or FLASH memory varying from a few kilobytes to mega¬
bytes. Such controllers are called controllers with on-chip ROM, e.g. Atmel AT89C51. Some controllers
may not contain on-chip memory and they require an external (off-chip) memory for holding the control
algorithm, e.g. Intel 8031 AH.
Different instruction set and system architecture are available for the design of a microprocessor.
Harvard and Von-Neumann are the two common system architectures for processor design. Processors
based on Harvard architecture contains separate buses for program memory and data memory, whereas
processors based on Von-Neumann architecture shares a single system bus for program and data memo¬
ry. We will discuss more about these architectures later, under a separate topic. Reduced Instruction Set
Computing (RISC) and Complex Instruction Set Computing (CISC) are the two common Instruction
Set Architectures (ISA) available for processor design. We will discuss the same under-a separate topic
in this section..
2.1.1.2 General Purpose Processor (GPP) vs. Application-Specific Instruction Set Processor
(ASIP) A General Purpose Processor or GPP is a processor designed for general computational tasks.
The processor running inside your laptop or desktop (Pentium 4/AMD Athlon, etc.) is a typical ex¬
ample for general purpose processor. They are produced in large volumes and targeting the general
market. Due to the high volume production, the per unit cost for a chip is low compared to ASIC or
other specific ICs. A typical general purpose processor contains an Arithmetic and Logic Unit (ALU)
and Control Unit (CU). On the other hand, Application Specific Instruction Set Processors (ASIPs)
are processors with architecture and instruction set optimised to specific-domain/application require¬
ments like network processing, automotive, telecom, media applications, digital signal processing, con¬
trol applications, etc. ASIPs fill the architectural spectmm between general purpose processors and
Application Specific Integrated Circuits (ASICs). The need for an ASIP arises when the traditional
general purpose processor are unable to meet the increasing application needs. Most of the embedded
systems are built around application specific instruction set processors. Some microcontrollers (like
automotive AVR, USB AVR from Atmel), system on chips, digital signal processors, etc. are examples
for application specific instruction set processors (ASIPs). ASIPs incorporate a processor and on-chip
peripherals, demanded by the application requirement, program and data memory.
2.1.1.3 Microcon trollers A Microcontroller is a highly integrated chip that contains a CPU, scratch
pad RAM, special and general purpose register arrays, on chip ROM/FLASH memory for program stor¬
age, timer and interrupt control units and dedicated I/O ports. Microcontrollers can be considered as a
super set of microprocessors. Since a microcontroller contains all the necessary functional blocks for
independent working, they found greater place in the embedded domain in place of microprocessors.
Apart from this, they are cheap, cost effective and are readily available in the market.
Texas Instrument’s TMS1000 is considered as the world’s first microcontroller. We cannot say it as a
fully functional microcontroller when we compare it with modem microcontrollers. TI followed Intel’s
4004/4040, 4 bit processor design and added some amount of RAM, program storage memory (ROM)
and I/O support on a single chip, there by eliminated the requirement of multiple hardware chips for
self-functioning. Provision to add custom instructions to the CPU was another innovative feature of
TMS 1000. TMS 1000 was released in 1974.
In 1977 Intel entered the microcontroller market with a family of controllers coming under one
umbrella named MCS-48™ family. The processors came under this family were /8038HL, 8039HL,
8040AHL, 8048H, 8049H and 8050AH. Intel 8048 is recognised as Intel’s first microcontroller and it
was the most prominent member in the MCS-48™^ family. It was used in the original IBM PC key¬
board. The inspiration behind 8048 was Fairchild’s F8 microprocessor and Intel’s goal of developing a
low cost and small size processor. The design of 8048 adopted a tme Harvard architecture where pro¬
gram and data memory shared the same address bus and is differentiated by the related control signals.
Eventually Intel came out with its most fruitful design in the 8bit microcontroller domain-the 8051
family and its derivatives. It is the most popular and powerful 8bit microcontroller ever built. It was
developed in the 1980s and was put under the family MCS-51. Almost 75% of the microcontrollers
used in the embedded domain were 8051 family based controllers during the 1980—90s. 8051 proces¬
sor cores are used in more than 100 devices by more than 20 independent manufacturers like Maxim,
Philips, Atmel, etc. under the license from Intel. Due to the low cost, wide availability, memory efficient
instruction set, mature development tools and Boolean processing (bit manipulation operation) capabil¬
ity, 8051 family derivative microcontrollers are much used in high-volume consumer electronic de'vices,
entertainment industry7 and other gadgets where cost-cutting is essential.
Another important family of microcontrollers used in industrial control and embedded applications is
the PIC family micro controllers from Microchip Technologies (It will be discussed in detail in a later
section of this book) . It is a high performance RISC microcontroller complementing the CISC (complex
instruction set computing) features of 8051. The terms RISC and CISC will be explained in detail in a
separate heading.
Some embedded system applications require only 8bit controllers whereas some embedded applica¬
tions requiring superior performance and computational needs demand 16/32bit microcontrollers. Infi¬
neon, Freescale, Philips, Atmel, Maxim, Microchip etc. are the key suppliers of 16bit microcontrollers.
Philips tried to extend the 8051 family microcontrollers to use for 16bit applications by developing the
Philips XA (extended Architecture) microcontroller series.
8bit microcontrollers are commonly used in embedded systems where the processing power is not
a big constraint. As mentioned earlier, more than 20 companies are producing different flavours of the
8051 family microcontroller. They try to add more and more functionalities like built in SPI, I2C serial
buses, USB controller, ADC, Networking capability, etc. So the competitive market is driving towards
a one-stop solution chip in microcontroller domain. High processing speed microcontroller families
like ARM11 series are also available in the market, which provides solution to applications requiring
hardware acceleration and high processing capability.
Freescale, NEC, Zilog, Hitachi, Mitsubishi, Infineon, ST Micro Electronics, National, Texas Instru¬
ments, Toshiba, Philips, Microchip, Analog Devices, Daewoo, Intel, Maxim, Sharp, Silicon Laborato¬
ries, TDK, Triscend, Winbond, Atmel, etc. are the key players in the microcontroller market. Of these
Atmel has got speciaLsignificance. They are the manufacturers of a variety of Flash memory based
microcontrollers. They also provide In-System Programmability (which will be discussed in detail in a
later section of this book) for the controller. The Flash memory technique helps in fast reprogramming
of the chip and thereby reduces the product development time. Atmel also provides another special fam¬
ily of microcontroller called AVR (it will be discussed in detail in a later chapter), an 8bit RISC Flash
microcontroller, fast enough to execute powerful instructions in a single clock cycle and provide the
latitude you need to optimise power consumption.
The instruction set architecture of a microcontroller can be either RISC or CISC. Microcontrollers
are designed for either general purpose application requirement (general purpose controller) or domain-
specific application requirement (application specific instruction set processor). The Intel 8051 micro¬
controller is a typical example for a general purpose microcontroller, whereas the automotive AVR
microcontroller family from Atmel Corporation is a typical example for ASIP specifically designed for
the automotive domain.
Microprocessor Microcontroller
A silicon chip representing a central processing A microcontrailer is a highly integrated chip that
unit (CPU), which is capable of performing arith- contains a GPU, scratchpad RAM, special and
metic as well as logical operations according to a general purpose register arrays, on chip ROM/
pre-defmed set of instructions -•» '■ FLASH memory, for program storage, timer and
; - , interrupt control units and dedicated I/O ports
It is a dependent unit. It requires the combina- It is a self-contained Unit and it doesn't require
tiod of other chips ‘like timers, program and dataexternal‘interrupt controller, timer, U ART, etc; for
memory chips, interrupt controllers, etc. for tune- its functioning
tioning . 7: 7j\.v Z . ■ V jr77::b;
Most of the time general purpose in design and Mostly application-oriented or domain-specific
operation
Targeted for high end market where performance Targeted for embedded market where perfor
is important; W W.tSrg W'CfZib" ' mance is not so critical (At present this demarca-
! ~£~ , v ’ . tion is invalid) !
2.1.1.5 Digital Signal Processors Digital Signal Processors (DSPs) are powerful special purpose
8/16/32 bit microprocessors designed specifically to meet the computational demands and power con¬
straints of today’s embedded audio, video, and communications applications. Digital signal processors
are 2 to 3 times faster than the general purpose microprocessors in signal processing applications. This
is because of the architectural difference between the two. DSPs implement algorithms in hardware
which speeds up the execution whereas general purpose processors implement the algorithm in firm¬
ware and the speed of execution depends primarily on the clock for the processors. In general, DSP can
be viewed as a microchip designed for performing high speed computational operations for ‘addition’,
‘subtraction’, ‘multiplication’ and ‘division’. A typical digital signal processor incorporates the follow¬
ing key units:
Program Memory Memory for storing the program required by DSP to process the data
Data Memory Working memory for storing temporary variables and data/signal to be processed.
Computational Engine Perfonus the signal processing in accordance with the stored program
memory. Computational Engine incorporates many specialised arithmetic units and each of them oper¬
ates simultaneously to increase the execution speed. It also incorporates multiple hardware shifters for
shifting operands and thereby saves execution time.
I/O Unit Acts as an interface between the outside world and DSP. It is responsible for capturing sig¬
nals to be processed and delivering the processed signals.
Introduction to Embedded Systems
Audio video signal processing, telecommunication and multimedia applications are typical examples
where DSP is employed. Digital signal processing employs a large amount of real-time calculations.
Sum of products (SOP) calculation, convolution, fast fourier transform (FFT), discrete fourier transfonn
(DFT), etc, are some of the operations performed by digital signal processors.
B lack fin®+ processors from Analog Devices is an example of DSP which delivers breakthrough
signal-processing performance and power efficiency while also offering a full 32-bit RISC MCU pro¬
gramming model. Blackfin processors present high-performance, homogeneous software targets, which
allows flexible resource allocation between hard real-tftne signal processing tasks and non real-time
control tasks. System control tasks can often run in the shadow of demanding signal processing and
multimedia tasks.
2.1.1.6 RISC vs. CISC Processors/Controllers The term RISC stands for Reduced Instruction
Set Computing. As the name implies, all RISC processors/controllers possess lesser number of instruc¬
tions, typically in the range of 30 to 40. CISC stands for Complex Instruction Set Computing. From
the definition itself it is clear that the instruction set is complex and instructions are high in number.
From a programmers point of view RISC processors are comfortable since s/he needs to learn only a
few instructions, whereas for a CISC processor s/he needs to learn more number of instructions and
should understand the context of usage of each instruction (This scenario is explained on the basis of
a programmer following Assembly Language coding. For a programmer following C coding it doesn’t
matter since the cross-compiler is responsible for the conversion of the high level language instructions
to machine dependent code). Atmel AVR microcontroller is an example for a RISC processor and its in¬
struction set contains only 32 instructions. The original version of 8051 microcontroller (e.g. AT89C51)
is a CISC controller and its instruction set contains 255 instructions. Remember it is not the number of
instructions that determines whether a processor/controller is CISC or RISC. There are some other fac¬
tors like pipelining features, instruction set type, etc. for determining the RISC/CISC criteria. Some of
the important criteria are listed below:
The following table highlights the differences between Harvard and Von-Neumann architecture
,
-hitpptnrp
Han ai d Architects | Von-Neumann A) chitccturc
Separate buses for instruction and data fetching Single shared bus for instruction and data fetching
Easier to pipeline, so high performance can be achieved Low performance compared to Harvard architecture
Comparatively high cost Cheaper / : ' y "■ ^ ; ^ ?- .
No, memory alignment problems - Allows scli. modifying codes' , 'A ./
Since data memory and program memory are stored Since data memory and program memory are stored
physically in different locations, ho chances for accidental physically in the same chip, Chances for accidental
corruption of program memory corruption of program memory
Little-endian (Fig. 2.3) means the lower-order byte of the data is stored in memory at the lowest ad¬
dress, and the higher-order byte at the highest address. (The little end comes first.) For example, a 4 byte
long integer Byte3 Byte2 Bytel ByteO will be stored in the memory as shown below:
BiliaiiljM
Base Address + 2 Byte 2 0x20002 (Base Address + 2)
|pjm^ sjp |-
■
Base Address + 3 Byte 3 T■ _■ 0x20003 (Base Address + 3)
Big-endian (Fig. 2.4) means the higher-order byte of the data is stored in memory at the lowest address,
and the lower-order byte at the highest address. (The big end comes first.) For example, a 4 byte long
integer Byte3 Byte2 Bytel ByteO will be stored in the memory as follows^:
msam
Base Address + 2 B- ' I ': 0x20002 (Base Address + 2)
2.1.1.9 Load Store Operation and Instruction Pipelining As mentioned earlier, the RISC pro¬
cessor instruction set is orthogonal, meaning it operates on registers. The memory access related opera¬
tions are perforated by the special instructions load and store. If the operand is specified as memory
location, the content of it is loaded to a register using the load instmction. The instruction store stores
data from a specified register to a specified memory location. The concept of Load Store Architecture
)s illustrated with the following example:
Suppose x, y and z are memory locations and we want to add the contents of x and y and store the
result in location z. Under the load store architecture the same is achieved with 4 instructions as shown
in Fig. 2.5.
The first instruction load Rl, x loads the register R1 with the content of memory location x, the sec¬
ond instruction load R2,y loads the register R2 with the content of memory location y. The instruction
add R3, Rl, R2 adds the content of registers Rl and R2 and stores the result in register R3. The next
instruction store R3,z stores the content of register R3 in memory location z.
The conventional instruction execution by the processor follows the fetch-decode-execute sequence.
Where the ‘fetch’ part fetches the instruction from program memory or code memory and the decode
part decodes the instruction to generate the necessary control signals. The execute stage reads the oper¬
ands, perform ALU operations and stores the result. In conventional program execution, the fetch and
decode operations are performed in sequence. For simplicity let’s consider decode and execution togeth¬
er. During the decode operation the memory address bus is available and if it is possible to effectively
utilise it for an instruction fetch, the processing speed can be increased. In its simplest form instruction
pipelining refers to the overlapped execution of instructions. Under normal program execution flow it
is meaningful to fetch the next instmction to execute, while the decoding and execution of the current
instmction is in progress. If the current instmction in progress is a program control flow transfer instruc¬
tion like jump or call instmction, there is no meaning in fetching the instmction following the current
instmction. In such cases the instmction fetched is flushed and a new instmction fetch is performed to
fetch the instmction. Whenever the current instmction is executing the program counter will be loaded
with the address of the next instmction. In case of jump or branch instmction, the new location is known
only after completion of the jump or branch instmction. Depending on the stages involved in an instmc¬
tion (fetch, read register and decode, execute instmction, access an operand in data memory, write back
the result to register, etc.), there can be multiple levels of instmction pipelining. Figure 2.6 illustrates the
concept of Instmction pipelining for single stage pipelining.
_TLTL
Machine cycle 1 Machine cycle 2
_T Machine cycle 3
Fetch (PC)
Execute (PC -1) Fetch (PC +1)
Execute(PC) Fetch (PC + 2)
PC: Program Counter Execute (PC + .1)
2.1.3.1 CPLDs and FPGAs The two major types of programmable logic devices are Field Program¬
mable Gate Arrays (FPGAs) and Complex Programmable Logic Devices (CPLDs). Of the two, FPGAs
The Typical Embedded System
offer the highest amount of logic density, the most features, and the highest performance. The largest
FPGA now shipping, part of the Xilinx Virtex™^ line of devices, provides eight million “system gates”
(the relative density of logic). These advanced devices also offer features such as built-in hardwired
processors (such as the IBM power PC), substantial amounts of memory, clock management systems,
and support for many of the latest, very fast device-to-device signaling technologies. FPGAs are used
in a wide variety of applications ranging from data processing and storage, to instrumentation, telecom¬
munications, and digital signal processing.
CPLDs, by contrast, offer much smaller amounts of logic-up to about 10,000 gates. But CPLDs offer
very predictable timing characteristics and are therefore ideal for critical control applications. CPLDs
such as the Xilinx CoolRunner™^ series also require extremely low amounts of power and are very in¬
expensive, making them ideal for cost-sensitive, battery-operated, portable applications such as mobile
phones and digital handheld assistants.
Advantages of PLD Programmable logic devices offer a number of important advantages over fixed
logic devices, including:
• PLDs offer customers much more flexibility during the design cycle because design iterations are
simply a matter of changing the programming file, and the results of design changes can be seen
immediately in working parts.
• PLDs do not require long lead times for prototypes or production parts-the PLDs are already on a
distributor’s shelf and ready for shipment.
• PLDs do not require customers to pay for large NRE costs and purchase expensive mask sets-PLD
suppliers incur those costs when they design their programmable devices and are able to amortize
those costs over the multi-year lifespan of a given line of PLDs.
• PLDs allow customers to order just the number of parts they need, when they need them, allowing
them to control inventory. Customers who use fixed logic devices often end up with-excess inven¬
tory which must be scrapped, or if demand for their product surges, they may be caught short of
parts and face production delays.
• PLDs can be reprogrammed even after a piece of equipment is shipped to a customer. In fact,
thanks to programmable logic devices, a number of equipment manufacturers now tout the ability
to add new features or upgrade products that already are in the field. To do this, they simply upload
a new programming file to the PLD, via the Internet, creating-new hardware logic in the system.
Over the last few years programmable logic suppliers have made such phenomenal technical ad¬
vances that PLDs are now seen as the logic solution of choice from many designers. One reason for
this is that PLD suppliers such as Xilinx are “fabless” companies; instead of owning chip manufactur¬
ing foundries, Xilinx outsource that job to partners like Toshiba and UMC, whose chief occupation is
making chips. This strategy allows Xilinx to focus on designing new product architectures, software
tools, and intellectual property cores while having access to the most advanced semiconductor process
technologies. Advanced process technologies help PLDs in a number of key areas: faster performance,
integration of more features, reduced power consumption, and lower cost.
FPGAs are especially popular for prototyping ASIC designs where the designer can test his design by
downloading the design file into an FPGA device. Once the design is set, hardwired chips are produced
for faster performance.
Just a few years ago, for example, the largest FPGA was measured in tens of thousands of system
gates and operated at 40 MHz. Older FPGAs also were relatively expensive, costing often more than
$150 for the most advanced parts at the time. Today, however, FPGAs with advanced features offer
millions of gates of logic capacity, operate at 300 MHz, can cost less than $10, and offer a new level of
integrated functions such as processors and memory.
Memory is an important part of a processor/controller based embedded systems. Some of the proces¬
sors/controllers contain built in memory and this memory is referred as on-chip memory. Others do
not contain any memory inside the chip and requires external memory to be connected with the control¬
ler/processor to store the control algorithm. It is called off-chip memory. Also some working memory
is required for holding data temporarily during certain operations. This section deals with the different
types of memory used in embedded system applications.
The Typical Embedded System
The code memory retains its contents even after the power to it is; turned off. It is generally known as
non-volatile storage memory. Depending on the fabrication, erasing and programming techniques they
are classified into the following types.
2.2.1.1 Masked ROM (MROM) Masked ROM is a one-time programmable device. Masked ROM
makes use of the hardwired technology for storing data. The device is factory programmed by masking
and metallisation process at the time of production itself, according to the data provided by the end user.
The primary advantage of this is low cost for high volume production. They are the least expensive type
of solid state memory. Different mechanisms are used for the masking process of the ROM, like
1. Creation of an enhancement or depletion mode transistor through channel implant.
2. By creating the memory cell either using a standard transistor or a high threshold transistor. In the
high threshold mode, the supply voltage required to turn ON the transistor is above the normal
ROM IC operating voltage. This ensures that the transistor is always off and the memory cell
stores always logic 0.
Masked ROM is a good candidate for storing the embedded firmware for low cost embedded devices.
Once the design is proven and the firmware requirements are tested and frozen, the binary data (The
firmware cross compiled/assembled to. target processor specific machine code) corresponding to it can
be given to the MROM fabricator. The limitation with MROM based firmware storage is the inability to
modify the device firmware against firmware upgrades. Since the MROM is permanent in bit storage, it
is not possible to alter the bit information.
2.2.1.2 Programmable Read Only Memory (PROM) / (OTP) Unlike Masked ROM Memory,
One Time Programmable Memory (OTP) or PROM is not pre-programmed by the manufacturer. The
end user is responsible for programming these devices. This memory has nichrome or polysilicon wires
arranged in a matrix. These wires can be functionally viewed as fuses. It is programmed by a PROM
programmer which selectively bums the fuses according to the bit pattern to be stored. Fuses which are
not blown/burned represents a logic “1” whereas fuses which are blown/bumed represents a logic “0”.
The default state is logic “1”. OTP is widely used for commercial production of embedded systems
whose proto-typed versions are proven and the code is finalised. It is a low cost solution for commercial
production. OTPs cannot be reprogrammed.
Introduction to Embedded Systems
2.2.1.3 Erasable Programmable Read Only Memory (EPROM) OTPs are not useful and
worth for development purpose. During the development phase the code is subject to continuous chang¬
es and using an OTP each time to load the code is not economical. Erasable Programmable Read Only
Memory (EPROM) gives the flexibility to re-program the same chip. EPROM stores the bit information
by charging the floating gate of an FET. Bit information is stored by using an EPROM programmer,
which applies high voltage to charge the floating gate. EPROM contains a quartz crystal window for
erasing the stored information. If the. window is exposed to ultraviolet rays for a fixed duration, the
entire memory will be erased. Even though the EPROM chip is flexible in terms of re-programmability, i
it needs to be taken out of the circuit board and put in a UV eraser device for 20 to 30 minutes. So it is
a tedious and time-consuming process.
2.2.1 A Electrically Erasable Programmable Read Only Memory (EEPROM) As the name
indicates, the information contained in the EEPROM memory can be altered by using electrical signals
at the register/Byte level. They can be erased and reprogrammed in-circuit. These chips include a chip
erase mode and in this mode they can be erased in a few milliseconds. It provides greater flexibility for
system design. The only limitation is their capacity is limited when compared with the standard ROM
(A few kilobytes).
2.2.1.5 FLASH FLASH is the latest ROM technology and is the most popular ROM technology used
in today’s embedded designs. FLASH memory is a variation of EEPROM technology. It combines the
re-programmability of EEPROM and the high capacity of standard ROMs. FLASH memory is organ¬
ised as sectors (blocks) or pages. FLASH memory stores information in an array of floating gate MOS-
FET transistors. The erasing of memory can be done at sector level or page level without affecting the
other sectors or pages. Each sector/page should be erased before re-programming. The typical erasable
capacity' of FLASH is 1000 cycles. W27C512 from WINBOND (www.winbond.coml is an example of
64KB FLASH memory.
2.2.1.6 NVRAM Non-volatile RAM is a random access memory with battery backup. It contains 5
static RAM based memory and a minute battery for providing supply to the memory in the absence of
external power supply. The memory and battery are packed together in a single package. The life span
of NVRAM is expected to be around 10 years. DS1644 from Maxim/Dallas is an example of 32KB i
NVRAM.
2.2.2.1 Static RAM (SRAM) Static RAM stores data in the form of voltage. They are made up of
flip-flops. Static RAM is the fastest form of RAM available. In typical implementation, an SRAM cell
(bit) is realised using six transistors (or 6 MOSFETs). Four of the transistors are used for building the
The Typical Embedded System
latch (flip-flop) part of the memory cell and two for controlling the access. SRAM is fast in operation
due to its resistive networking and switching capabilities. In its simplest representation an SRAM cell
can be visualised as shown in Fig. 2.10:
This implementation in its simpler form can be Write control Read control
visualised as two-cross coupled inverters with read/
write control through transistors. The four transis¬
tors in the middle form the cross-coupled inverters.
This can he visualised as shown in Fig. 2.11.
From the SRAM implementation diagram, it is
clear that access to the memory cell is controlled
by the line Word Line, which controls the access
transistors (MOSFETs) Q5 and Q6. The access tran¬
sistors control the connection to bit lines B & BY In
order to write a value to the memory cell, apply the desired value to the bit control lines (For writing 1,
make B = 1 and B\ =0; For writing 0, make B = 0 and B\ =1) and assert the Word Line (Make Word line
Introduction to Embedded Systems
high). This operation latches the bit written in the flip-flop. For reading the content of the memory cell,
assert both B and B\ bit lines to 1 and set the Word line to 1.
The major limitations of SRAM are low capacity and high cost. Since a minimum of six transistors
are required to build a single memory cell, imagine how many memory cells we can fabricate on a sili¬
con wafer.
2.2.2.2 Dynamic RAM (DRAM) DynamicRAM stores data
Bit line B
in the form of charge. They are made up of MOS transistor
gates. The advantages of DRAM are its high density and low
cost compared to SRAM. The disadvantage is that since the
information is stored as charge it gets leaked off with time and
to prevent this they need to be refreshed periodically. Special
circuits called DRAM controllers are used for the refreshing
operation. The refresh operation is done periodically in milli¬
seconds interval. Figure 2.12 illustrates the typical implementa¬
tion of a DRAM cell.
The MOSFET acts as the gate for the incoming and outgo¬
[Fig. 2.12) DRAM cell implementation
ing data whereas the capacitor acts as the bit storage unit. Table
given below summarises the relative merits and demerits of
SRAM and DRAM technology.
2.2.2.3 NVRAM Non-volatile RAM is a random access memory with battery backup. It contains
static RAM based memory and'a minute battery for providing supply to the memory in the absence
of external power supply. The memory and battery are packed together in a single package. NVRAM
is used for the non-volatile storage of results of operations or for setting up of flags, etc. The life span
of NVRAM islexpected to be around 10 years. DS1744 from Maxim/Dallas is an example for 32KB
NVRAM.
that of a parallel interface memory is expressed in terms of kilobytes. Atmel Corporations AT24C512
is an example for serial memory with capacity 512 kilobits and 2-wire interface. Please refer to the
section ‘Communication Interface’ for more details on I2C, SPI and 1-Wire Bus.
the RTOS files are copied from the program storage memory, decompressed if required and then loaded
to the RAM for execution. The supplier of the RTOS usually gives a rough estimate on the run time
RAM requirements and program memory' requirements for the RTOS. On top of this add the RAM
requirements for executing user tasks and ROM for storing user applications. On a safer side, always
add a buffer value to the total estimated RAM and ROM size requirements. A smart phone device with
Windows mobile operating system is a typical example for embedded device with OS. Say 64MB RAM
and 128MB ROM are the minimum requirements for miming the Windows mobile device, indeed you
need extra RAM and ROM for running user applications. So while building the system, count the
memory for that also and arrive at a value which is always at the safer side, so that you won’t end up in
a situation where you don’t have sufficient memory to install and run user applications. There are two
parameters for representing a memory. The first one is the size of the memory chip (Memory density
expressed in terms of number of memory bytes per chip). There is no option to get a memory chip with
the exact required number of bytes. Memory chips come in standard sizes like 512bytes, 1024bytes
(1 kilobyte), 2048bytes (2 kilobytes), 4Kb,f 8Kb, 16Kb, 32Kb, 64Kb, 128Kb, 256Kb, 512Kb, 1024Kb
(1 megabytes), etc. Suppose your embedded application requires only 750 bytes of RAM, you don’t
have the option of getting a memory chip with size 750 bytes, the only option left with is to choose
the memory chip with a size closer to the size needed. Here 1024 bytes is the least possible option. We
cannot go for 512 bytes, because the minimum requirement is 750 bytes. While you select a memory
size, always keep in mind the address range supported by your processor. For example, for a processor/
controller with 16 bit address bus, the maximum number of memory locations that can be addressed is
216 = 65536 bytes = 64Kb. Hence it is meaningless to select a 128Kb memory chip for a processor with
16bit wide address bus. Also, the entire memory range supported by the processor/controller may not
be available to the memory chip alone. It may be shared between I/O, other ICs and memory. Suppose
the address bus is 16bit wide and only the lower 32Kb address range is assigned to the memory chip,
the memory size maximum required is 32Kb only. It is not worth to use a memory chip with size 64Kb
in such a situation. The second parameter that needs to be considered in selecting a memory is the word
size of the memory. The word size refers to the number of memory bits that can be read/write together
at a time. 4, 8, 12, 16, 24, 32, etc. are the word sizes supported by memory chips. Ensure that the word
size supported by the memory chip matches with the data bus width of the processor/controller.
FLASH memory is the popular choice for ROM (program storage memory) in embedded applica¬
tions. It is a powerful and cost-effective solid-state storage technology for mobile electronics devices
and other consumer applications. FLASH memory comes in two major variants, namely, NAND and
NOR FLASH. NAND FLASH is a high-density low cost non-volatile storage memory.' On the other
hand, NOR FLASH is less dense and slightly expensive. But it supports the Execute in Place (XIP)
technique for program execution. The XIP technology allows the execution of code memory from ROM
itself without the need for copying it to the RAM as in the case of conventional execution method. It is
a good practice to use a combination of NOR and NAND memory for’storage memory requirements,
where NAND can be used for storing the program code and or data like the data captured in a camera
device. NAND FLASH doesn’t support XIP and if NAND FLASH is used for storing program code, a
DRAM can be used for copying and executing the program code. NOR FLASH supports XIP and it can
be used as the memory for bootloader or for even storing the complete program code.
The EEPROM data storage memory is available as either serial interface or parallel interface chip. If
the processor/controller of the device supports serial interface and the amount of data to write and read
to and from the device is less, it is better to have a serial EEPROM.chip. The serial EEPROM saves the
address space of the total system. The memory capacity of the serial EEPROM is usually expressed in
fKb—Kilobytes
The Typical Embedded System
bits or kilobits. 512 bits, lKbits, 2Kbits, 4Kbits, etc. are examples for serial EEPROM memory repre¬
sentation. For embedded systems with low power requirements like portable devices, choose low power
memory devices. Certain embedded devices may be targeted for operating at extreme environmental
conditions like high temperature, high humid area, etc. Select an industrial grade memory chip in place
of the commercial grade chip for such devices.
At the very beginning of this chapter it is already mentioned that an embedded system is in constant
interaction with the Real world and the controlling/monitoring functions executed by the embedded
system is achieved in accordance with the changes happening to the Real world. The changes in sys¬
tem environment or variables are detected by the sensors connected to the input port of the embedded
system. If the embedded system is designed for any controlling purpose, the system will produce some
changes in the controlling variable to bring the controlled variable to the desired value. It is achieved
through an actuator connected to the output port of the embedded system. If the embedded system is
designed for monitoring purpose only, then there is no need for including an actuator in the system. For
example, take the case of an ECG machine. It is designed to monitor the heart beat status of a patient
and it cannot impose a control over the patient’s heart beat and its order. The sensors used here are the
different electrode sets connected to the body of the patient. The variations are captured and presented
to the user (may be a doctor) through a visual display or some printed chart.
2.3.1 Sensors
A sensoris a transducer device that converts energy from one form to another for any measurement or
control purpose. This is what I “by-hearted” during my engineering degree from the transducers paper.
If we look back to the “Smart” running shoe example given at the end of Chapter 1, we can identify
that the sensor which measures the distance between the cushion and magnet in the smart running shoe
is a magnetic hall effect sensor (Please refer back).
2.3.2 Actuators
Actuator is a form of transducer device (mechanical or electrical) which converts signals to correspond¬
ing physical action (motion).. Actuator acts as an output device.
Looking back to the “Smart” running shoe example given at the end of Chapter 1, we can see that the
actuator used for adjusting the position of the cushioning element is a micro stepper motor (Please refer
back).
2.3.3.1 Light Emitting Diode (LED) Light Emitting Diode (LED) is an important output device
for visual indication in any embedded system. LED can be used as an indicator for the status of various
signals or situations. Typical examples are indicating the presence of power conditions like ‘Device
ON’, ‘Battery low’ or ‘Charging of battery’ for a battery operated handheld
embedded devices.
Light Emitting Diode is a p-n junction diode (Refer Analog Electron¬
ics fundamentals to refresh your memory for p-n junction diode ©) and it
contains an anode and a cathode. For proper functioning of the LED, the
anode of it should be connected to +ve terminal of the supply voltage and
cathode to the -ve terminal of supply voltage. The current flowing through
the LED must be limited to a value below the maximum current that it can
conduct. A resister is used in series between the power supply and the LED
to limit the current through the LED. The ideal LED interfacing circuit is
shown in Fig. 2.13.
LEDs can be interfaced to the port pin of a processor/controller in two ways. In the first method, the
anode is directly connected to the port pin and the port pin drives the LED. In this approach the port pin
‘sources’ current to the LED when the port pin is at logic High (Logic ‘1’). In the second method, the
cathode of the LED is connected to the port pin of the processor/controller and the anode to the sup¬
ply voltage through a current limiting resistor. The LED is turned on when the port pin is at logic Low
(Logic ‘0’). Here the port pin ‘sinks’ current. If the LED is directly connected to the port pin, depending
on the maximum current that a port pin can source, the brightness of LED may not be to the required
level. In the second approach, the current is directly sourced by the power supply and the port pin acts
as the sink for current. Here we will get the required brightness for the LED.
2.3.3.2 7-Segment LED Display The 7-segment
LED display is an output device for displaying alpha
numeric characters. It contains 8 light-emitting diode
(LED) segments arranged in a special form. Out of the 8
LED segments, 7 are used for displaying alpha numeric
characters and 1 is used for representing ‘decimal point’
in decimal number display. Figure 2.14 explains the ar¬
rangement of LED segments in a 7-segment LED display.
The LED segments are named A to G and the deci¬
mal point LED segment is named as DR The LED seg¬
ments A to G and DP should be lit accordingly to display
numbers and characters. For example, for displaying the
number 4, the segments F, G, B and C are lit, For dis¬
playing 3, the segments A, B, C, D, G and DP are lit. For
displaying the character ‘d’, the segments B, C, D, E and G are lit. All these 8 LED segments need to
be connected to one port of the processor/controller for displaying alpha numeric digits. The 7-segment
LED displays are available in two different configurations, namely; Common Anode and Common
Cathode. In the common anode configuration, the anodes of the 8 segments are connected commonly
whereas in the common cathode configuration, the 8 LED segments share a common cathode line.
Figure 2.15 illustrates the Common Anode and Cathode configurations.
Based on the configuration of the 7-segment LED unit, the LED segment’s anode or cathode is con¬
nected to the port of the processor/controller in the order ‘A’ segment to the least significant port pin and
DP segment to the most significant port pin.
The Typical Embedded System
The current flow through each of the LED segments should be limited to the maximum value sup¬
ported by the LED display unit. The typical value for the current falls within the range of 20mA. The
current through each segment can be limited by connecting a current limiting resistor to the anode or
cathode of each segment. The value for the current limiting resistors can be calculated using the current
value from the electrical parameter listing of the LED display.
For common cathode configurations, the anode of each LED segment is connected to the port pins of
the port to which the display is interfaced. The anode of the common anode LED display is connected to
the 5V supply voltage through a current limiting resistor and the cathode of each LED segment is con¬
nected to the respective port pin lines. For an LED segment to lit in the Common anode LED configura¬
tion, the port pin to which the cathode of the LED segment is
connected should be set at logic 0.
7-segment LED display is a popular choice for low cost X LED
embedded applications like, Public telephone call monitoring I/O interface
devices, point of sale terminals, etc. I/O interface
of optocoupler in input circuit and output circuit of an embedded system with a microcontroller as the
system core.
Optocoupler is available as ICs from different semiconductor manufacturers. The MCT2MIC from
Fairchild semiconductor (http://www.fairchildsemi.com/) is an example for optocoupler IC.
2.3.3.4 Stepper Motor A stepper motor is an electro-mechanical device which generates discrete
displacement (motion) in response, to dc electrical signals. It differs from the normal dc motor in its
operation. The dc motor produces continuous rotation on applying dc voltage whereas a stepper motor
produces discrete rotation in response to the dc voltage applied to it. Stepper motors are widely used in
industrial embedded applications, consumer electronic products and robotics control systems. The paper
feed mechanism of a printer/fax makes use of stepper motors for its functioning.
Based on the coil winding arrangements, a two-phase stepper motor is classified into two. They are:
1. Unipolar
2. Bipolar
1. Unipolar A unipolar stepper motor contains two windings per
phase. The direction of rotation (clockwise or anticlockwise) of a
stepper motor is controlled by changing the direction of current
flow. Current in one direction flows through one coil and in the op¬
posite direction flows through the other coil. It is easy to shift the
direction of rotation by just switching the terminals to which the
coils are connected. Figure 2.18 illustrates the working of a two-
phase unipolar stepper motor.
The coils are represented as A, B, C and D. Coils A and C carry
current in opposite directions for phase 1 (only one of them will be
carrying current at a time). Similarly, B and D carry current in opposite directions for phase 2 (only one
of them will be carrying current at a time).
2. Bipolar A bipolar stepper motor contains single winding per phase. For reversing the motor rota¬
tion the current flow through the windings is reversed dynamically. It requires complex circuitry for
current flow reversal. The stator winding details for a two phase unipolar stepper motor is shown in
Fig. 2.19.
The stepping of stepper motor can be implemented in different ways by changing the sequence of ac¬
tivation of the stator windings. The different stepping modes supported by stepper motor are explained
below.
Full Step. In the full step mode both the phases are energised simultaneously. The coils A, B, C and D
are energised in the following order:
It should be noted that out of the two windings, only one winding of a phase is energised at a time.
The Typical Embedded System
A C B D
I Fig. 2.19j Stator Winding details for a 2 Phase unipolar stepper motor
Wave Step In the wave step mode only one phase is energised at a time and each coils of the phase is
energised alternatively. The coils A, B, C and D are energised in the following order:
Half Step It uses the combination of wave and lull step. It has the highest torque and stability. The
coil energising sequence for half step is given below.
AAT ' H L L L
,;^s| H L L ■
K
t !...<•% : ,i? 1. l
The rotation of the stepper motor can be reversed by reversing the order in which the coil is
energised.
Two-phase unipolar stepper motors are the popular choice for embedded applications. The current
requirement for stepper motor is little high and hence the port pins of a microcontroller/processor may'
not be able to drive themj directly. Also the supply voltage required to operate stepper motor varies
normally in the range 5V to 24 V. Depending on the current and voltage requirements, special driving
circuits are required to interface the stepper motor with microcontroller/processors. Commercial off-
the-shelf stepper motor driver ICs are available in the market and they can be directly interfaced to the
microcontroller port. ULN2803 is an octal peripheral driver array available from ON semiconductors
and ST microelectronics for driving a 5V stepper motor. Simple driving circuit can also be built using
transistors.
The following circuit diagram (Fig. 2.20) illustrates the interfacing of a stepper motor through a
driver circuit connected to the port pins of a microcontroller/processor.
2.3.3.5 Relay Relay is an electro-mechanical device. In embedded application, the ‘Relay’ unit acts
as dynamic path selectors for signals and power. The ‘Relay’ unit contains a relay coil made up of in¬
sulated wire on a metal core and a metal armature with one or more contacts.
‘Relay’ works on electromagnetic principle. When a voltage is applied to the relay coil, current flows
through the coil, which in turn generates a magnetic field. The magnetic field attracts the armature core
and moves the contact point. The movement of the contact point changes the power/signal flow path.
‘Relays’ are available in different configurations. Figure 2.21 given below illustrates the widely used
relay configurations for embedded applications.
The Single Pole Single Throw configuration has only one path for information flow. The path is
either open or closed in normal condition. For normally Open Single Pole Single Throw relay, the cir¬
cuit is normally open and it becomes closed when the relay is energised. For normally closed Single
Pole Single Throw configuration, the circuit is normally closed and it becomes open when the relay is
energised. For Single Pole Double Throw Relay, there are two paths for information flow and they are
selected by energising or de-energising the relay.
The Relay is normally controlled using a relay driver circuit connected to the port pin of the proces¬
sor/controller. A transistor is used for building the relay driver circuit. Figure 2.22 illustrates the same.
A free-wheeling diode is used for free-wheeling the voltage produced in the opposite direction
when the relay coil is de-energised. The freewheeling diode is essential for protecting the relay and the
transistor. .
Most of the industrial relays are bulky and requires high voltage to operate. Special relays called
‘Reed’ relays are available for embedded application requiring switching of low voltage DC signals.
2.3.3.6 Piezo Buzzer Piezo buzzer is a piezoelectric device for generating audio indications in em¬
bedded application. A piezoelectric buzzer contains a piezoelectric diaphragm which produces audible
sound in response to the voltage applied to it. Piezoelectric buzzers are available in two types. ‘Self-
driving’ and ‘External driving’. The ‘Self-driving’ circuit contains all the necessary components to gen¬
erate sound at a predefined tone. It will generate a tone on applying the voltage. External driving piezo
buzzers supports the generation of different tones. The tone can be varied by applying a variable pulse
train to the piezoelectric buzzer. A piezo buzzer can be directly interfaced to the port pin of the proces¬
sor/control. Depending on the driving current requirements, the piezo buzzer can also be interfaced
using a transistor based driver circuit as in the case of a ‘Relay’.
2.3.3.7 Push Button Switch It is an input device. Push button switch comes in two configurations,
namely ‘Push to Make’ and ‘Push to Break’. In the ‘Push to Make’ configuration, the switch is normally
in the open state and it makes a circuit contact when it is pushed or pressed. In the ‘Push to Break’ con¬
figuration, the switch is normally in the closed state and it breaks the circuit contact when it is pushed
or pressed. The push button stays in the ‘closed’ (For Push to Make type) or ‘open’ (For Push to Break
type) state as long as it is kept in the pushed state and it breaks/makes the circuit connection when it
Introduction to Embedded Systems
The table given below explains the meaning and use of each bit.
|4 f|kiDi¥:| 1 Setsj^ortEkas^^mp^port
fe/A-:ADISets DorfB as outbut d()
ictions
ode selector for port C ]■ v
D7 r-: 1; I/O mode. Tf . .
D7 = 0; Bit set/reset (BSR) mode. Functions as the control/status fines for ports A and B.
The bits of port C can be set or reset just as if they were output ports. .
Processor/
82C55A'":
Controller
Data bus D0....D7 D0....D7
Data bus port ■
Pins 34 to 27
AO Pin 9,
A1 Pin 8 PA0....PA7
Port A ,
- A2....A7 PB0....PB7
Higher ordef *
Port B m
Address bus Address bus Address! -
CS\ Pin 6
(A8....A15) decoder F PC0....PC7
The ports of 8255 can be configured for different modes of operation by the processor/controller.
The Typical Embedded System
Communication interface is essential for communicating with various subsystems of the embedded
system and with the external world. For an embedded product, the communication interface can be
viewed in two different perspectives; namely; Device/board level communication interface (Onboard
Communication Interface) and Product level communication, interface (External Communication Inter¬
face). Embedded product is a combination of different types of components (chips/devices) arranged on
a printed circuit board (PCB). The communication channel which interconnects the various components
within an embedded product is referred as device/board level communication interface (onboard com¬
munication interface). Serial interfaces like I2C, SPI, UART, 1-Wire, etc and parallel bus interface, ptz
examples of ‘Onboard Communication Interface’.
Some embedded systems are self-contained units and they don’t require any interaction and data
transfer with other sub-systems or external world. On the other hand, certain embedded systems may be
a part of a large distributed system and they require interaction and data transfer between various devic¬
es and sub-modules. The ‘Product level communication interface’ (External Communication Interface.)
is responsible for data transfer between the embedded system and other devices or modules. The exter¬
nal communication interface can be either a wired media or a wireless media and it can be a serial or a
parallel interface. Infrared (IR), Bluetooth (BT), Wireless LAN (Wi-Fi), Radio Frequency waves (RF),
GPRS, etc. are examples for wireless communication interface. RS-232C/RS-422/RS-485, USB, Eth¬
ernet IEEE 1394 port, Parallel port, CF-II interface, SDIO, PCMCIA, etc. are examples for wired inter¬
faces. It is not mandatory that an embedded system should contain an external communication interface.
Mobile communication equipment is an example for embedded system with external communication
interface.
The following section gives you an overview of the various ‘Onboard’ mid ‘External’ communica¬
tion interfaces for an embedded product. We will discuss about the various physical interface, firmware
requirements and initialisation and communication sequence for these interfaces in a dedicated book
titled ‘Device Interfacing’, which is planned under this series.
from the master and respond upon receiving the commands. ‘Master’ and ‘Slave’ devices can act as
either transmitter or receiver. Regardless whether a master is acting as transmitter or receiver, the syn¬
chronisation clock signal is generated by the ‘Master’ device only. I2C supports multi masters on the
same bus. The following bus interface diagram shown in Fig. 2.26 illustrates the connection of master
and slave devices on the I2C bus.
SJ
I2C bus
The I2C bus interface is built around an input buffer and an open drain or collector transistor. When
the bus is in the idle state, the open drain/collector transistor will be in the floating state and the output
lines (SDA and SCL) switch to the ‘High Impedance’ state. For proper operation of the bus, both the bus
lines should be pulled to the supply voltage (+5V for TTL family and +3.3V for CMOS family:devices)
using pull-up resistors. The typical value of resistors used in pull-up is 2.2K. With pull-up resistors, the
output lines of the bus in the idle state will be ‘HIGH’.
The address of a I2C device is assigned by hardwiring the address lines of the device to the desired
logic level. The address to various I2C devices in an embedded device is assigned and hardwired at the
time of designing the embedded hardware. The sequence of operations for communicating with an I2C
slave device is listed below:
1. The master device pulls the clock line (SCL) of the bus to ‘HIGH’
2. The master device pulls the data line (SDA) ‘LOW’, when the SCL line is at logic ‘HIGH’ (This
is the ‘Start’ condition for data transfer)
3. The master device sends the address (7 bit or 10 bit wide) of the ‘slave’ device to which it wants
to communicate, over the SDA line. Clock pulses are generated at the SCL line for synchronising
the bit reception by the slave device. The MSB of the data is always transmitted first. The data in
the bus is valid during the ‘HIGH’ period of the clock signal
The Typical Embedded System
4. The master device sends the Read or Write bit (Bit value = 1 Read operation; Bit value = 0 Write
operation) according to the requirement
5. The master device waits for the acknowledgement bit from the slave device whose address is sent
on the bus along with the Read/Write operation command. Slave devices connected to the bus
compares the address received with the address assigned to them
6. The slave device with the address requested by the master device responds by sending an ac¬
knowledge bit (Bit value = 1) over the SDA line
7. Upon receiving the acknowledge bit, the Master device sends the 8bit data to the slave device over
SDA line, if the requested operation is ‘Write to device’. If the requested operation is ‘Read from
device’, the slave device sends data to the master over the SDA line
8. The master device waits for the acknowledgement bit from the device upon byte transfer complete
for a write operation and sends an acknowledge bit to the Slave device for a read operation
9. The master device terminates the transfer by pulling the SDA line ‘HIGH’ when the clock line
SCL is at logic ‘HIGH’ (Indicating the ‘STOP’ condition)
I2C bus supports three different data rates. They are: Standard mode (Data rate up to lOOkbits/sec
(100 kbps)), Fast mode (Data rate up to 400kbits/sec (400 kbps)) and High speed mode (Data rate up to
3.4Mbits/sec (3.4 Mbps)). The first generation I2C devices were designed to support data rates only up
to 100kbps. The new generation I2C devices are designed to operate at data rates up to 3.4Mbits/sec.
2.4.1.2 Serial Peripheral Interface (SPI) Bus The Serial Peripheral Interface Bus (SPI) is a syn¬
chronous bi-directional full duplex four-wire serial interface bus. The concept of SPI was introduced by
Motorola. SPI is a single master multi-slave system. It is possible to have a system where more than one
SPI device can be master, provided the condition only one master device is active at any given point of
time, is satisfied. SPI requires four signal lines for communication. They are:
Master Out Slave In (MOSI): Signal line carrying the data from master to slave device. It is
also known as Slave Input/Slave Data In (SI/SDI)
Master In Slave Out (MISO): Signal line carrying the data from slave to master device. It is
also known as Slave Output (SO/SDO)
Serial Clock (SCLK): Signal line carrying the clock signals
Slave Select (SS): Signal line for slave device select. It is an active low signal
The bus interface diagram shown in Fig. 2.27 illustrates the connection of master and slave devices
on the SPI bus.
The master device is responsible for generating the clock signal. It selects the required slave device
by asserting the corresponding slave device’s slave select signal ‘LOW’. The data out line (MISO) of all
the slave devices when not selected floats at high impedance state.
The serial data transmission through SPI bus is fully configurable. SPI devices contain a certain set
of registers for holding these configurations. The serial peripheral control register holds the various con¬
figuration parameters like master/slave selection for the device, baudrate selection for communication,
clock signal control, etc. The status register holds the status of various conditions for transmission and
reception.
SPI works on the principle of ‘Shift Register’. The master and slave devices contain a special shift
register for the data to transmit or receive. The size of the shift register is device dependent. Normally
it is a multiple of 8. During transmission from the master to slave, the data in the master’s shift register
is shifted out to the MOSI pin and it enters the shift register of the slave device through the MOSI pin
of the slave device. At the same time the shifted out data bit from the slave device’s shift register enters
Introduction to Embedded Systems
M1S0
MOS! MOSI
SCL
MISQ
ss\
MOSI
SCL'
MISQ
SSI
ss\
SPIbus
SPI bus interfacing
the shift register of the master device through MISO pin. In summary, the shift registers of ‘master’ and
‘slave’ devices form a circular buffer. For some devices, the decision on whether the LS/MS bit of data
needs to be sent out first is configurable through configuration register (e.g. LSBF bit of the SPI control
register for Motorola’s 68HC12 controller).
When compared to I2C, SPI bus is most suitable for applications requiring transfer of data in ‘streams’.
The only limitation is SPI doesn’t support an acknowledgement mechanism.
Every 1-wire device contains a globally unique 64bit identification number stored within it. This
unique identification number can be used for addressing individual devices present on the bus in case
there are multiple slave devices connected to the 1 -wire bus. The identifier has three parts: an 8bit family
code, a 48bit serial number and an 8bit CRC computed from the first 56 bits. The sequence of operation
for communicating with a 1-wire slave device is listed below.
1. The master device sends a ‘Reset’ pulse on the 1-wire bus.
2. The slave device(s) present on the bus respond with a ‘Presence’ pulse.
3. The master device sends a ROM command (Net Address Command followed by the 64bit address
of the device). This addresses the slave device(s) to which it wants to initiate a communication.
4. The master device sends a read/write fimction command to read/write the internal memory or
register of the slave device.
5. The master initiates a Read data/Write data from the device or to the device
All communication over the 1-wire bus is master initiated. The communication over the 1-wire bus is
divided into timeslots of 60 microseconds. The ‘Reset’ pulse occupies 8 time slots. For starting-a com¬
munication, the master asserts the reset pulse by pulling the 1-wire bus ‘LOW’ for at least 8 time slots
(480us). If a ‘slave’ device is present on the bus and is ready for communication it should respond to the
master with a ‘Presence’ pulse, within 60ps of the release of the ‘Reset’ pulse by the master. The slave
device(s) responds with a ‘Presence’ pulse by pulling the 1-wire bus ‘LOW’ for a minimum of 1 time
slot (60|is). For writing a bit value of 1 on the 1-wire bus, the bus master pulls the bus for 1 to 15lis and
then releases the bus for the rest of the time slot. A bit value of ‘0’ is written on the bus by master pulling
the bus for a minimum of 1 time slot (60ps) and a maximum of 2 time slots (120|is). To Read a bit from
the slave device, the master pulls the bus ‘LOW’ for 1 to 15qs. If the slave wants to send a bit value ‘1’
in response to the read request from the master, it simply releases the bus for the rest of the time slot. If
the slave wants to send a bit value ‘O’, it pulls the bus ‘LOW’ for the rest of the time slot.
2.4.1.5 Parallel Interface The on-board parallel interface is normally used for communicating with
peripheral devices which are memory mapped to the host of the system. The host processor/controller
of the embedded system contains a parallel bus and the device which supports parallel bus can directly
connect to this bus system. The communication through the parallel bus is controlled by the control sig¬
nal interface between the device and the host. The ‘Control Signals’ for communication includes ‘Read/
Write’ signal and device select signal. The device normally contains a device select line and the device
becomes active only when this line is asserted by the host processor. The direction of data transfer (Host
to Device or Device to Host) can be controlled through the control signal lines for ‘Read’ and ‘Write’.
Only the host processor has control over the ‘Read’ and ‘Write’ control signals. The device is normally
memory mapped to the host processor and a range of address is assigned to it. An address decoder circuit
is used for generating the chip select signal for the device. When the address selected by the processor
is within the range assigned for the device, the decoder circuit activates the chip select line and thereby
the device becomes active. The processor then can read or write from or to the device by asserting the
corresponding control line (RD\ and WR\ respectively). Strict timing characteristics are followed for
parallel communication. As mentioned earlier, parallel communication is host processor initiated. If a
device wants to initiate the communication, it can inform the same to the processor through interrupts.
For this, the interrupt line of the device is connected to the interrupt line of the processor and the cor¬
responding interrupt is enabled in the host processor. The width of the parallel interface is determined
by the data bus width of the host processor. It can be 4bit, 8bit, 16bit, 32bit or 64bit etc. The bus width
supported by the device should be same as that of the host processor. The bus interface diagram shown
in Fig. 2.30 illustrates the interfacing of devices through parallel interface.
2.4.2 External Communication Interfaces
The External Communication Interface refers to the different communication channels/buses used by
the embedded system to communicate with the external world. The following section gives an overview
of the various interfaces for external communication.
2.4.2.1 RS-232 C & RS-485 RS-232 C (Recommended Standard number 232, revision C from the
Electronic Industry Association) is a legacy, full duplex, wired, asynchronous serial communication
interface. The RS-232 interface is developed by the Electronics Industries Association (EIA) during the
early 1960s. RS-232 extends the UART communication signals for external data communication.
UART uses the standard TTL/CMOS logic (Logic ‘High’ corresponds to bit value 1 and Logic ‘Low’
corresponds to bit value 0) for bit transmission whereas-RS-232 follows the EIA standard for bit trans¬
mission. As per the EIA standard, a logic ‘0’ is represented with voltage between +3 and +25V and a
logic ‘ 1’ is represented with voltage between -3 and -25V. In EIA standard, logic ‘0’ is known as ‘Space’
and logic ‘1 ’ as ‘Mark’. The RS-232 interface defines various handshaking and control signals for com¬
munication apart from the ‘Transmit’ and.‘Receive’ signal lines for data communication. RS-232. sup¬
ports two different types of connectors, namely; DB-9: 9-Pin comiector and DB-25: 25-Pin connector.
Figure 2.31 illustrates the connector details for DB-9 and DB-25.
t
!
The pin details for the two connectors are explained in the following table:
Pin Name Pin no: (For DB-9 Pin no: (For DB-25 • . Description
:Sj. > Connector);' ' 55 ' Connector) -” ' - '’' .y-L-L ‘CL2:''-:Lv:^-x-Lyy*cLy.L'LL: L
TXD 3 2 Transmit Pin for Transmitting Serial Data
RXD 2 xNf-r 3 Receive Pin for Receiving Serial Data - ■L'L
RI 9 ; 22 Ring Indicator
y • 2 .-a ■ yu; -yi
FG
* s1' h» ' * 'it: “,•£ - - |gg
frame Ground ; '
• > L x"
; • - . yvXLLLL
SDCD 12 Secondary DCD
Lit'll • ’£■"/* .ipr °
'SCTS s»> Lk „ * j, 13 Secondary CTS.,
< S<>> * ’i ,
STXD >
14 Secondary TXD
L ZL T i *1?^:;; } - 1,“
L -.Lx
TC L 15 fjgj r Transmission Signal Element Timing r. J>1 % * :r
21 :F y
Signal Quality detector ‘ ,, y , ’ .-
NC 9 No Connection
NC V 10 No Connection p-L. c X ;/ !=‘‘' L tL$y y :
NC 11 No Connection
1
NC tff' ; 5 18 No Connection
NC 23 No Connection j
v‘s ' U
NC 24 No Connection
NC 25 No Connection
RS-232 is a point-to-point communication interface and the devices involved in RS-232 communica¬
tion are called ‘Data Terminal Equipment (DTE)’ and ‘Data Communication Equipment (DCE)’. If no
data flow control is required, only TXD and RXD signal lines and ground line (GND) are required for
data transmission and reception. The RXD pin of DCE should be connected to the TXD pin of DTE and
vice versa for proper data transmission.
If hardware data flow control is required for serial transmission, various control signal lines of the
RS-232 connection are used appropriately. The control signals are implemented mainly for modem
communication and some of them may not be relevant for other type of devices. The Request To Send
(RTS) and Clear To Send (CTS) signals co-ordinate the communication between DTE and DCE. When¬
ever the DTE has a data to send, it activates the RTS line and if the DCE is ready to accept the data, it
activates the CTS line.
The Typical Embedded System
The Data Terminal Ready (DTR) signal is activated by DTE when it is ready to accept data. The Data
Set Ready (DSR) is activated by DCE when it is ready for establishing a communication link. DTR
should be in the activated state before the activation of DSR.
The Data Carrier Detect (DCD) control signal is used by the DCE to indicate the DTE that a good
signal is being received.
Ring Indicator (RI) is a modem specific signal line for indicating an incoming call on the telephone
line.
The 25 pin DB connector contains two sets of signal lines for transmit, receive and control lines.
Nowadays DB-25 connector is obsolete and most of the desktop systems are available with DB-9 con¬
nectors only.
As per the EIA standard RS-232 C supports baudrates up to 20Kbps (Upper limit 19.2 Kbps) The com¬
monly used baudrates by devices are 300bps, 1200bps, 2400bps, 9600bps, 11.52Kbps and 19.2Kbps.
9600 is the popular baudrate setting used for PC communication. The maximum operating distance sup¬
ported by RS-232 is 50 feet at the highest supported baudrate.
Embedded devices contain a UART for serial communication and they generate signal levels con¬
forming to TTL/CMOS logic. A level translator IC like MAX 232 from Maxim Dallas semiconductor
is used for converting the signal lines from the UART to RS-232 signal lines for communication. On
the receiving side the received data is converted back to digital logic level by a converter IC. Converter
chips contain converters for both transmitter and receiver.
Though RS-232 was the most popular communication interface during the olden days, the advent of
other communication techniques like Bluetooth, USB, Firewire, etc are pushing down RS-232 from the
scenes. Still RS-232 is popular in certain legacy industrial applications.
RS-232 supports only point-to-point communication and not suitable for multi-drop communication.
It uses single ended, data transfer technique for signal transmission and thereby more susceptible to
noise and it greatly reduces the operating distance.
RS-422 is another serial interface standard from EIA for differential data communication. It supports
data rates up to 100Kbps and distance up to 400 ft. The same RS-232 connector is used at the device
end and an RS-232 to RS-422 converter is plugged in the transmission line. At the receiver end the
conversion from RS-422 to RS-232 is performed. RS-422 supports multi-drop communication with one
transmitter device and receiver devices up to 10.
RS-485 is the enhanced version of RS-422 and it supports multi-drop communication with up to 32
transmitting devices (drivers) and 32 receiving devices on the bus. The communication between devices
in the bus uses the ‘addressing’ mechanism to identify slave devices.
2.4.2.2 Universal Serial Bus (USB) Universal Serial Bus (USB) is a wired high speed serial bus
for data communication. The first version of USB (USB 1.0) was released in 1995 and was created by
the USB core group members consisting of Intel, Microsoft, IBM, Compaq, Digital and Northern Tele¬
com. The USB communication system follows a star topology with a USB host at the centre and one
or more USB peripheral devices/USB hosts connected to it. A USB host can support connections up to
127, including slave peripheral devices and other USB hosts. Figure 2.32 illustrates the star topology
for USB device connection.
USB transmits data in packet format. Each data packet has a standard format. The USB communica¬
tion is a host initiated one. The USB host contains a host controller which is responsible for controlling
the data communication, including establishing connectivity with USB slave devices, packetizing and
formatting the data. There are different standards for implementing the USB Host Control interface;
namely Open Host Control Interface (OHCI) and Universal Host Control Interface (UHCI).
Introduction to Embedded Systems
device 4 device 5
defines the specifications for Super Speed. USB 3.0 is expected to be in action by year 2009. There is a
move happening towards wireless USB for data transmission using Ultra Wide Band (UWB) technol¬
ogy. Some laptops are already available in the market with wireless USB support.
2.4.2.3 IEEE 1394 (Firewire) IEEE 1394 is a wired, isochronous high speed serial communica¬
tion bus. It is also known as High Performance Serial Bus (HPSB). The research on 1394 was started
by Apple Inc. in 1985 and the standard for this was coined by IEEE. The implementation of it is avail¬
able from, various players with different names. Apple Inc’s (www.apple.com) implementation of 1394
protocol is popularly known as Firewire. i.LINK is the 1394 implementation from Sony Corporation
(www.sony.net') and Lynx is the implementation from Texas Instruments (www.ti.com). 1394 supports
peer-to-peer connection and point-to-multipoint communication allowing 63 devices to be comiected
on the bus in a tree topology. 1394 is a wired serial interface and it can support a cable length of up to
15 feet for interconnection.
The 1394 standard has evolved a lot from the first version IEEE 1394-1995 released in 1995 to the
recent version IEEE 1394-2008 released in June 2008. The 1394 standard supports a data rate of 400
to 3200Mbits/second. The IEEE 1394 uses differential data transfer (The information is sent using dif¬
ferential signals through a pair of twisted cables. It increases the noise immunity) and the interface cable
supports 3 types of connectors, namely; 4-pin connector, 6-pin connector (alpha connector) and 9 pin
connector (beta connector). The 6 and 9 pin comiectors carry power also to support external devices
(In case an embedded device is connected to a PC through an IEEE 1394 cable with 6 or 9 pin connec¬
tor interface, it can operate from the power available through the connector.) It can supply unregulated
power in the range of 24 to 30V. (The Apple implementation is for battery operated devices and it can
supply a voltage in the range 9 to 12V.) The table given below illustrates the pin details for 4, 6 and 9
pin connectors.
There are two differential data transfer lines A and B per connector. In a 1394 cable, normally the dif¬
ferential lines of A are comiected to B (TPA+ to TPB+ and TPA-to TPB-) and vice versa.
1394 is a popular communication interface for connecting embedded devices like Digital Camera,
Camcorder, Scanners to desktop computers for data transfer and storage.
Unlike USB interface (Except USB OTG), IEEE 1394 doesn’t require a host for communicating
between devices. For example, you can directly connect a scanner with a printer for printing. The data-
Introduction to Embedded Systems
rate supported by 1394 is far higher than the one supported by USB2.0 interface. The 1394 hardware
implementation is much costlier than USB implementation.
2.4.2.4 Infrared (IrDA) Infrared (IrDA) is a serial, half duplex, line of sight based wireless tech¬
nology for data communication between devices. It is in use from the olden days of communication
and you may be very familiar with it. The remote control of your TV, VCD player, etc. works on In¬
frared data communication principle. Infrared communication technique uses infrared waves of the
electromagnetic spectrum for transmitting the data. IrDA supports point-point and point-to-multipoint
communication, provided all devices involved in the communication are within the line of sight. The
typical communication range for IrDA lies in the range 10 cm to 1 m. The range can be increased by
increasing the transmitting power of the IR device. IR supports data rates ranging from 9600bits/second
to 16Mbps. Depending on the speed of data transmission IR is classified into Serial IR (SIR), Medium
IR (MIR), Fast IR (FIR), Very Fast IR (VFIR) and Ultra Fast IR (UFIR). SIR supports transmission
rates ranging from 9600bps to 115.2kbps. MIR supports data rates of 0.576Mbps and 1.152Mbps. FIR
supports data rates up to 4Mbps. VFIR is designed to support high data rates up to 16Mbps. The UFIR
specs are under development and it is targeting a,data rate up to 100Mbps.
IrDA communication involves a transmitter unit for transmitting the data over IR and a receiver
for receiving the data. Infrared Light Emitting Diode (LED) is the IR source for transmitter and at the
receiving end a photodiode acts as the receiver. Both transmitter and receiver unit will be present in
each device supporting IrDA communication for bidirectional data transfer. Such IR units are known as
‘Transceiver’. Certain devices like a TV remote control always require unidirectional communication
and so they contain either the transmitter or receiver unit (The remote control unit contains the transmit¬
ter unit and TV contains the receiver unit).
‘Infra-red Data Association’ (IrDA - http://www.irda.org/) is the regulatory body responsible for de¬
fining and licensing the specifications for IR data communication. IrDA communication has two es¬
sential parts; a physical link part and a protocol part. The physical link is responsible for the physical
transmission of data between devices supporting IR communication and protocol part is responsible
for defining the rules of communication. The physical link works on the wireless principle making use
of Infrared for communication. The IrDA specifications include the standard for both physical link and
protocol layer.
The IrDA control protocol contains implementations for Physical Layer (PHY), Media Access Con¬
trol (MAC) and Logical Link Control (LLC). The Physical Layer defines the physical characteristics of
communication like range, data rates, power, etc.
IrDA is a popular interface for file exchange and data transfer in low cost devices. IrDA .was the
prominent communication channel in mobile phones before Bluetooth’s existence. Even now most of
the mobile phone devices support IrDA.
2.4.2.5 Bluetooth (BT) Bluetooth is a low cost, low power, short range wireless technology for data
and voice communication. Bluetooth was first proposed by ‘Ericsson’ in 1994. Bluetooth operates at
2.4GHz of the Radio Frequency spectrum and uses the Frequency Hopping Spread Spectrum (FHSS)
technique for communication. Literally it supports a data rate of up to 1Mbps and a range of approxi¬
mately 30 feet for data communication. Like IrDA, Bluetooth communication also has two essential
parts; a physical link part and a protocol part. The physical link is responsible for the physical trans¬
mission of data between devices supporting Bluetooth communication and protocol part is responsible
The Typical Embedded System
for defining the rules of communication. The physical link works on the wireless principle making use
of RF waves for communication. Bluetooth enabled devices essentially contain a Bluetooth wireless
radio for the transmission and reception of data. The rules governing the Bluetooth communication is
implemented in the ‘Bluetooth protocol stack’. The Bluetooth communication IC holds the stack. Each
Bluetooth device will have a 48 bit unique identification number. Bluetooth communication follows
packet based data transfer.
Bluetooth supports point-to-point (device to device) and point-to-multipoint (device to multiple
device broadcasting) wireless communication. The point-to-point communication follows the master-
slave relationship. A Bluetooth device can function as either master or slave. When a network is formed
with one Bluetooth device as master and more than one device as slaves, it is called a Piconet. A Piconet
supports a maximum of seven slave devices.
Bluetooth is the favourite choice for short range data communication in handheld embedded devices.
Bluetooth technology is very popular among cell phone users as they are the easiest communication
channel for transferring ringtones, music files, pictures, media files, etc. between neighbouring Blue¬
tooth enabled phones.
The Bluetooth standard specifies the minimum requirements that a Bluetooth device must support
for a specific usage scenario. The Generic Access Profile (GAP) defines the requirements for detecting
a Bluetooth device and establishing a connection with it. All other specific usage profiles are based on
GAP. Serial Port Profile (SPP) for serial data communication, File Transfer Profile (FTP) for file transfer
between devices, Human Interface Device (HID) for supporting human interface devices like keyboard
and mouse are examples for Bluetooth profiles.
The specifications for Bluetooth communication is defined and licensed by the standards body ‘Blue¬
tooth Special Interest Group (SIG)\ For more information, please visit the website www.bluetooth.org.
2.4.2.6 Wi-Fi Wi-Fi or Wireless Fidelity is the popular wireless communication technique for net¬
worked communication of devices. Wi-Fi follows the IEEE 802.11 standard. Wi-Fi is intended for net¬
work communication and it supports Internet Protocol (IP) based communication. It is essential to have
device identities in a multipoint communication to address specific devices for data communication. In
an IP based communication each device is identified by an IP address, which is unique to each device on
the network. Wi-Fi based communications require an intermediate agent called Wi-Fi router/Wireless
Access point to manage the communications. The Wi-Fi router is responsible for restricting the access to
a network, assigning IP address to devices on the network, routing data packets to the intended devices
on the network. Wi-Fi enabled devices contain a wireless adaptor for transmitting and receiving data in
the form of radio signals through an antenna. The hardware part of it is known as Wi-Fi Radio.
Wi-Fi operates at 2.4GHz or 5GHz of radio spectrum and they co-exist with other ISM band devices
like Bluetooth. Figure 2.33 illustrates the typical interfacing of devices in a Wi-Fi network.
For communicating with devices over a Wi-Fi network, the device when its Wi-Fi radio is turned
ON, searches the available Wi-Fi network in its vicinity and lists out the Service Set Identifier (SSID) of
the available networks. If the network is security enabled, a password may be required to connect to a
particular SSID. Wi-Fi employs different security mechanisms like Wired Equivalency Privacy (WEP)
Wireless Protected Access (WPA), etc. for securing the data communication.
Wi-Fi supports data rates ranging from 1Mbps to 150Mbps (Growing towards higher rates as technol¬
ogy progresses) depending on the standards (802.11 a/b/g/n) and access/modulation method. Depending
On the type of antenna and usage location (indoor/outdoor), Wi-Fi offers a range of 100 to 300 feet.
Introduction to Embedded Systems
supports a theoretical maximum transfer rate of 171.2kbps. In GPRS communication, the radio channel
is concurrently shared between several users instead of dedicating a radio channel to a cell phone user.
The GPRS communication divides the channel into 8 timeslots and transmits data over the available
channel. GPRS supports Internet Protocol (IP), Point to Point Protocol (PPP) and X.25 protocols for
communication.
GPRS is mainly used by mobile enabled embedded devices for data communication. The device
should support the necessary GPRS hardware like GPRS modem and GPRS radio. To accomplish
GPRS based communication, the carrier network also should have support for GPRS communication.
GPRS is an old technology and it is being replaced by new generation data communication techniques
like EDGE, High Speed Downlink Packet Access (HSDPA), etc. which offers higher bandwidths for
communication.
The embedded software development process in assembly language is tedious and time consuming.
The developer needs to know about all the instruction sets of the processor/controller or at least s/he
should carry an instruction set reference manual with her/him. A programmer using assembly language
technique writes the program according to his/her view and taste. Often he/she may be writing a method
or functionality which can be achieved through a single instruction as an experienced person’s point of
view, by two or three instructions in his/her own style. So the program will be highly dependent on the
developer. It is very difficult for a second person to understand the code written in Assembly even if it
is well documented.
We will discuss both approaches of embedded software development in a later chapter dealing with
design of embedded-firmware, in detail. Two types of control algorithm design exist in embedded firm¬
ware development. The first type of control algorithm development is known as the infinite loop or
‘super loop’ based approach, where the control flow runs from top to bottom and then jumps back to
the top of the program in a conventional procedure. It is similar to the while (1) {}; based technique
in C. The second method deals with splitting the functions to be executed into tasks and running these
tasks using a scheduler which is part of a General Purpose or Real Time Embedded Operating System
(GPOS/RTOS). We will discuss both of these approaches in separate chapters of this book.
reset circuitry and they don’t require external reset circuitry. Figure 2.35 illustrates a resistor capacitor
based passive reset circuit for active high and low configurations. The reset pulse width can be adjusted
by changing the resistance value R and capacitance value C.
Microcontroller jpViP1:05;
C: Capacitor
Y : Resonator
Crystal oscillator
Oscillator
Unit
Quartz crystal nJ1 Clock input pin
resonator L!1 =j=c
Y Oscillator 3 j
.unit: |
X7 Y7
(Fig. 2.37) Oscillator circuitry using quartz crystal and quartz crystal oscillator
the operations of the OS kernel. The RTC can interrupt the OS kernel by asserting the interrupt line
of the processor/controller to which the RTC interrupt line is connected. The OS kernel identifies the
interrupt in terms of the Interrupt Request (IRQ) number generated by an interrupt controller. One IRQ
can be assigned to the RTC interrupt and the kernel can perform necessary operations like system date
time updation, managing software timers etc when an RTC timer tick interrupt occurs. The RTC can be
configured to interrupt the processor at predefined intervals or to interrupt the processor when the RTC
register reaches a specified value (used as alarm interrupt).
tion of an external watchdog timer based microprocessor supervisor circuit for a small scale embedded
system.
Summary
l —
K The core of an embedded system is usually built around a commercial off-the-shelf component or an application ■
specific integrated circuit (ASIC) or a general purpose processor .like a .microprocessor or microcontroller or .
application -specific instruction set processors (ASH’ like DSP, Microcontroller, etc.) or a Programmable Logic
Device (PLD) or a System on Chip (SdC) ‘" _ . , \
K Processors/controllers support either Reduced Instruction Set Computing (RISC) or Complex Instruction Set
•*"' Computing (CISC) - *.' " : ' "’A, **
S Microprocessors/controllers based on the Harvard architecture will have separate data bus and instruction-bus,
whereas Mlcroprocessors/contrdllers based on the Von-Neumann architecture shares a single common bus for
fetching both instructions and data - ' A ‘ ‘ ’ ’ VJpA
■S The Big-endian processors store the higher-order byte of the data in memory at the lowest address, whereas
Little-endian processors store the lower-order byte of data in memory at the lowest address
/ Field Programmable Gate Arrays (FPGAs). and Complex Programmable Logic Devices (CPLDs) are the two
major types of programmable logic devices-
The Read Only Memory (ROM) is a non-volatile memory for storing the firmware and embedded OS files.
MROM, PROM (OTP), EPROM, EEPROM and FLASH are the commonly used firmware storage memory
■S Random.Access Memory (RAM) is a volatile memory for temporary data storage. RAM can be either Static
RAM (SRAM) or Dynamic RAM (DRAM). SRAM is made up of flip-flops, whereas DRAM is made up of MOS
Transistor and Capacitor
S The sensors connected to the input port of an embedded system senses the changes in input variables and the
actuators connected at the output port of an embedded system controls some variables in accordance with changes
in input
K Light Emitting Diode (LED), 7-Segment LED displays, Liquid Crystal Display (LCD), Piezo Buzzer, Speaker,
Optocoupler, Stepper Motor, Digital to Analog Converters (DAC), Relays etc are examples for output devices of
an embedded system
/ Keyboard, Touch screen, Push Button switches, Optocoupler, Analog to Digital Converter (ADC) etc are
examples for Input devices in an embedded system
The Typical Embedded System
Z The Programmable Peripheral Interface (PPI) device extends the I/O capabilities of the processor used in
embedded system
Z I2C, SPI, UART, 1-Wire, Parallel bus etc are examples for onboard communication interface and RS-232C,
RS-485, USB, IEEE1394 (FireWire), Wi-Fi, ZigBee, Infrared (EDA), Bluetooth, GPRS, etc. are examples for
external communication interface
Z The control algorithm for the embedded device is known as Embedded Firmware. Embedded firmware can be
developed on top .of an embedded operating system or without an operating system
Z The reset circuit ensures that the device is not operating at a voltage level where the device is not guaranteed
to operate, during system power ON. The reset signal brings the internal registers and the different hardware
systems of the processor/controller to .a known state and starts the firmware execution from the-reset vector /
Z The brown-out protection circuit prevents the processor/controller: from unexpected program execution behaviour
when the supply voltage to the processor/controller falls below a specified voltage •
/ The oscillator unit generates clock signals for synchronising the operations of the processor
Z The time keeping activity for the embedded system is performed by the Real Time Clock (RTC) of the system.
RTC holds current time, date, month, year, day of the week, etc.
Z The watchdog timer monitors the firmware execution and resets the processor or generates an Interrupt in case
the execution time for a task is exceeding the maximum allowed limit
Z Printed circuit board or PCB acts as a platform for mounting all the necessary hardware components as per the
design requfrement.
—I
Keywords
FPGA : Field Programmable Gate Array Device. A programmable logic device with reconfigurable
function. Popular for prototyping ASIC designs
MROM : Masked ROM is a one-time programmable memory, which uses the hardwired technology for
storing data
OTP : One Time Programmable Read Only Memory made up of nichrome or polysilicon wires
arranged in a matrix
EPROM : Erasable Programmable Read Only Memory. Reprogrammable ROM. Erased by exposing.to
UV light >v • ■ $£0
EEPROM : Electrically Erasable Programmable Read Only Memory. Reprogrammable ROM. Erased by
. applying.electrical signals, ; • . ■ . ■
FLASH : Electrically Erasable Programmable Read Only Memory. Same as EEPROM but wit)yit|gli|
capacity and support for block level memory erasing ; . .. ;
RAM : Random Access memory. Volatile memory . - . '■ i ' :
SRAM. : Static RAM. A type of RAM, made up of flip-flops ;
DRAM Dynamic RAM. A type of RAM, made up of MOS Transistor and Capacitor ,.
NVRAM : Non-volatile SRAM. Battery-backed SRAM ' •.
ADC ; Analog to Digital Converter. An integrated circuit which converts analog signals to digital
form - -vv A
LED : Light Emitting Diode. An output device which produces visual indication in the form of light in)
accordance with current flow " tv ■
7-Segment : The 7-segment LED display is an output device for displaying alpha numeric characters.
LED Display It contains 8 light-emitting diode (LED) segments arranged in a special form *
Optocoupler : A solid state device to isolate two parts of a circuit. Optocoupler combines an LED and a photo¬
transistor in a single housing (package) •'
Stepper motor :. An electro-mechanical device which generates discrete displacement (motion) in response to dc
electrical signals - X
Relay : An electro-mechanical device which acts as dynamic path selector for signals and power -J.
Piezo Buzzer : A piezo-electric device for generating audio indication. It contains a piezo-electric diaphragm
whichproduces audible sound in response to the voltage applied to it
Push Button : A mechanical device for electric circuit ‘make’ and ‘break’ operation
switch
PPI : Programmable Peripheral Interface is a device for extending the I/O capabilities of processors/
controllers
I2C : The Inter Integrated Circuit Bus (I2C-Pronounced ‘I square C’) is a synchronous bi-directional
half duplex two wire serial interface bus.
SPI : The Serial Peripheral Interface BusTSPI) is a synchronous bi-directional full duplex four wire
serial interface bus
UART : The Universal Asynchronous Receiver Transmitter is an asynchronous communication
implementation standard
1-Wire interface : An asynchronous half-duplex communication protocol developed by Maxim Dallas
Semiconductor. It is also known as Dallas 1 -Wire® protocol.
RS-232 C : Recommended Standard, number 232, revision C from the Electronic Industry Association, is a
legacy, full duplex, wired, asynchronous serial communication'interface
RS-485 : The enhanced version of RS-232, which supports multi-drop,’communication with up to 32
transmitting devices (drivers) and 32 receiving devices on the bus
USB : Universal Serial Bus is a wired high speed serial bus for data communication
IEEE 1394 : A wired, isochronous high speed serial communication bus ;
Firewire : The Apple Inc.’s implementation of the 1394 protocol , ' /
gCH Objective Questions )
Review Questions )
/
Introduction to Embedded Systems
8. What is processor architecture? What are the different processor architectures available for processor/controller
design? Give an example for each.
9. What is the difference between big-endian and little-endian processors? Give an example of each.
10. What is Programmable Logic Device (PLD)? What are the different types of PLDs? Explain the role of PLDs in
Embedded System design.
11. What is the difference between PLD and ASIC?
12. What are the advantages of PLD over fixed logic device?
13. What are the different types of memories used in Embedded System design? Explain the role of each.
14. What are the different types of memories used for Program storage in Embedded System Design?
15. What is the difference between Masked ROM and OTP?
16. What is the difference between PROM and EPROM?
17. What are the advantages of FLASH over other program storage memory in Embedded System design?
18. What is the difference between RAM and ROM?
19. What are the different types of RAM used for Embedded System design?
20. What is memory shadowing? What is its advantage?
21. What is Sensor? Explain its role in Embedded System Design? Illustrate with an example.
22. What is Actuator? Explain its role in Embedded System Design? Illustrate with an example.
23. What is Embedded Firmware? What are the different approaches available for Embedded Firmware development?
24. What is the difference between General Purpose Processor (GPP) and Application Specific Instruction Set
Processor (ASIP). Give an example for both.
25. Explain the concept of Load Store architecture and instruction pipelining.
26. Explain the operation of Static RAM (SRAM) cell.
27. Explain the merits and limitations of SRAM and DRAM as Random Access Memory.
28. Explain the difference between Serial Access Memory (SAM) and Random Access Memory (RAM). Give an
example for both.
29. Explain the different factors that needs to be considered in the selection of memory for Embedded Systems.
30. Explain the different types of FLASH memory and their relative merits and de-merits.
31. Explain the different Input and output subsystems of Embedded Systems.
32. What is stepper motor? How is it different from ordinary dc motor?
33. Explain the role of Stepper motor in embedded applications with examples.
34. Explain the different step modes for stepper motor.
35. What is Relay? What are the different types of relays available? Explain the role of relay in embedded
applications.
36. Explain the operation of the transistor based Relay driver circuit.
37. Explain the operation of a Matrix Keyboard.
38. What is Programmable Peripheral Interface (PPI) Device? Explain the interfacing of 8255 PPI with an 8bit
microprocessor/controller.
39. Explain the different on-board communication interfaces in brief.
40. Explain the-different external communication interfaces in brief.
41. Explain the sequence of operation for communicating with an I2C slave device.
42. Explain the difference between I2C and SPI communication interface.
43. Explain the sequence of operation for communicating with a 1-Wire slave device.
44. Explain the RS-232 C serial interface in detail.
45. Explain the merits and limitations of Parallel port over Serial RS-232 interface.
46. Explain the merits and limitations of IEEE1394 interface over USB,
47. Compare the operation of ZigBee and Wi-Fi network.
48. Explain the role of Reset circuit in Embedded System.
49. Explain the role of Real Time Clock (RTC) in Embedded System.
50. Explain the role of Watchdog-Timer in Embedded System.
X
The Typical Embedded System
Is below 4,7;
Characteristics and Quality Attributes
of Embedded Systems
LEARNING OBJECTIVES
3.1.4 Distributed
The tepi distributed means that embedded systems may be a part of larger systems. Many numbers of
such distributed embedded systems form a single large embedded control unit. An automatic vending
macl/ine is a typical example for this. The vending machine contains a card reader (for pre-paid vend¬
ing systems), a vending unit, etc. Each of them are independent embedded units but they work together
74 Introduction to Embedded Systems
to perform the overall vending function. Another example is the Automatic Teller Machine (ATM).
An ATM contains a card reader embedded unit, responsible for reading and validating the user’s ATM
card, transaction unit for performing transactions, a currency counter for dispatching/vending currency
to the authorised person and a printer unit for printing the transaction details. We can visualise theie as
independent embedded systems. But they work together to achieve a common goal.
Another typical example of a distributed embedded system is the Supervisory Control And Data
Acquisition (SCADA) system used in Control & Instrumentation applications, which contains physi¬
cally distributed individual embedded control units connected to a supervisory module.
Quality attributes are the non-functional requirements that need to be documented properly in any sys¬
tem design. If the quality attributes are more concrete and measurable it will give a positive impact on
the system development process and the end product. The various quality attributes that needs to be
addressed in any embedded system development are broadly classified into two, namely ‘Operational
Quality Attributes’ and ‘Non-Operational Quality Attributes’.
3.2.1.1 Response Response is a measure of quickness of the system. It gives you an idea about
how fast your system is tracking the changes in input variables. Most of the embedded systems demand
fast response which should be almost Real Time. For example, an embedded system deployed in flight
control application should respond in a Real Time manner. Any response delay in the system will cre¬
ate potential damages to the safety of the flight as well as the passengers. It is not necessary that all
embedded systems should be Real Time in response. For example, the response time requirement for
an electronic toy is not at all time-critical. There is no specific deadline that this system should respond
within this particular timeline.
3.2.1.2 Throughput Throughput deals with the efficiency of a system. In general it can be defined
as the rate of production or operation of a defined process over a stated period of time. The rates can be
expressed in terms of units of products, batches produced, or any other meaningful measurements. In
the case of a Card Reader, throughput means how many transactions the Reader can perform in a minute
or in an hour orin a day. Throughput is generally measured in terms of ‘Benchmark’. A ‘Benchmark’ is
a reference point by which something can be measured. Benchmark can be a set of performance criteria
that a product is expected to meet or a standard product that can be used for comparing other products
of the same product line.
3.2.1.3 Reliability Reliability is a measure of how much % you can rely upon the proper function¬
ing of the system or what is the % susceptibility of the system to failures.
Mean Time Between Failures (MTBF) and Mean Time To Repair (MTTR) are the terms used in de¬
fining system reliability. MTBF gives the frequency of failures in hours/weeks/months. MTTR specifies
how long the system is allowed to be out of order following a failure. For an embedded system with
critical application need, it should be of the order of minutes.
3.2.1.4 Maintainability Maintainability deals with support and maintenance to the end user or cli¬
ent in case of technical issues and product failures or on the basis of a routine system checkup. Reliability
and maintainability are considered as two complementary disciplines. A more reliable system means a
system with less corrective maintainability requirements and vice versa. As the reliability of the system
increases, the chances of failure and non-functioning also reduces, thereby the need for maintainabil¬
ity is also reduced. Maintainability is closely related to the system availability. Maintainability can be
broadly classified into two categories, namely, ‘Scheduled or Periodic Maintenance (preventive mainte¬
nance)’ and ‘Maintenance to unexpected failures (corrective maintenance)’. Some embedded products
may use consumable components or may contain components which are subject to wear and tear and
they should be replaced on a periodic basis. The period may be based on the total hours of the system us¬
age or the total output the system delivered. A printer is a typical example for illustrating the two types of
maintainability. An inkjet printer uses ink cartridges, which are consumable components and as per the
printer manufacturer the end user should replace the cartridge after each V number of printouts to get
quality prints. This is an example for ‘Scheduled or Periodic maintenance’. If the paper feeding part
of the printer fails the printer fails to print and it requires immediate repairs to rectify this problem. This
is an example of ‘Maintenance to unexpected failure’. In both of the maintenances (scheduled and
repair), the printer needs to be brought offline and during this time it will not be available for the user.
Hence it is obvious that maintainability is simply an indication of the availability of the product for use.
In any embedded system design, the ideal value for availability is expressed as
Introduction to Embedded Systems
A{ = MTBF/(MTBF + MTTR)
where A{ = Availability in the ideal condition, MTBF = Mean Time Between Failures, and MTTR =
Mean Time To Repair
3.2.1.5 Security Confidentiality, ‘Integrity’, and ‘Availability’ (The tenn ‘Availability’ mentioned
here is not related to the term ‘Availability’ mentioned under the ‘Maintainability’ section) are the three
major measures of information security. Confidentiality deals with the protection of data and application
from unauthorised disclosure. Integrity deals with the protection of data and application from unautho¬
rised modification. Availability deals with protection of data and application from- unauthorized users.
A very good example of the ‘Security’ aspect in an embedded product is a Personal Digital Assistant
(PDA). The PDA can be either a shared resource (e.g. PDAs used in LAB setups) or an individual
one. If it is a shared one there should be some mechanism in the form of a user name and password to
access into a particular person’s profile-This is an example of ‘Availability’. Also all data and applica¬
tions present in the PDA need not be accessible to all users. Some of them are specifically accessible to
administrators only. For achieving this, Administrator and user levels of security should be implemented
-An example of Confidentiality. Some data present in the PDA may be visible to all users but there may
not be necessary permissions to alter the data by the users. That is Read Only access is allocated to all
users-An example of Integrity.
3.2.1.6 Safety ‘Safety’ and ‘Security’ are two confusing tenns. Sometimes you may feel both of
them as a single attribute. But they represent two unique aspects in quality attributes. Safety deals with
the possible damages that can happen to the operators, public and the environment due to the breakdown
of an embedded system or due to the emission of radioactive or hazardous materials from'the embedded
products. The breakdown of an embedded system may occur due to a hardware failure or a firmware
failure. Safety analysis is a must in product engineering to evaluate the anticipated damages and deter¬
mine the best course of action to bring down the consequences of the damages to an acceptable level.
As stated before, some of the safety threats are sudden (like product breakdown) and some of them are
gradual (like hazardous emissions from the product).
3.2.2.1 Testability & Debug-ability Testability deals with how easily one can test his/her design,
application and by which means he/she can test it. For an embedded product, testability is applicable
to both the embedded hardware and firmware. Embedded hardware testing ensures that the peripherals
and the total hardware functions in the desired mamier, whereas firmware testing ensures that the firm¬
ware is functioning in the expected way. Debug-ability is a means of debugging the product as such for
figuring out the probable sources that create unexpected behaviour in the total system. Debug-ability
Characteristics and Quality Attributes of Embedded Systems
has two aspects in the embedded system development context, namely, hardware level debugging and
firmware level debugging. Hardware debugging is used for figuring out the issues created by hardware
problems whereas firmware debugging is employed to figure out the probable errors that appear as a
result of flaws in the firmware.
3.2.2.2 Evolvability Evolvability is a term which is closely related to Biology. '-Evolvability is
referred as the non-heritable variation. For an embedded system, .the quality attribute ‘Evolvability’
refers to the ease with which the embedded product (including firmware and hardware) can be modified
to take advantage of new firmware or hardware technologies.
can be brought down significantly. In order to shorten the time to prototype, make use of all possible
options like the use of off-the-shelf components, re-usable assets, etc.
3'.2.2.5 Per Unit Cost and Revenue Cost is a factor which is closely monitored by both end user
(those who buy the product) and product manufacturer (those who build the product). Cost is a highly
sensitive factor for commercial products. Any failure to position the cost of a commercial product at a
nominal rate, may lead to the failure of the product in the market. Proper market study and cost benefit
analysis should be carried out before taking a decision on the per-unit cost of the embedded product:
From a designer/product development company perspective the ultimate aim of a product is to generate
marginal profit. So the budget and total system cost should be properly balanced to provide a marginal
profit. Every embedded product has a product life cycle which starts with the design and development
phase. The product idea generation, prototyping, Roadmap definition, actual product design and devel¬
opment are the activities carried out during this phase. During the design and development phase there
is only investment and no returns. Once the product is ready to sell, it is introduced to the market. This
stage is known as the Product Introduction stage. During the initial period the sale£ and revenue will
be low. There won’t be much competition and the product sales and revenue increases with time. In the
growth phase, the product grabs high market share. During the maturity phase, the growth and sales will
be steady and the revenue reaches at its peak. The Product Retirement/Decline phase starts with the drop
in sales volume, market share and revenue. The decline happens due to various reasons like competition
from similar product with enhanced features or technology changes, etc. At some point of the decline
stage, the manufacturer announces discontinuing of the product. The different stages of the embedded
products life cycle-revenue, unit cost and profit in each stage-are represented in the following Product
Life-cycle graph.
From the graph, it is clear that the total revenue increases from the product introduction stage to the
product maturity stage. The revenue peaks at the maturity stage and starts falling in the decline/retire¬
ment stage. The unit cost is very high during the introductory stage (a typical example is cell phone; if
Characteristics and Quality Attributes of Embedded Systems
you buy a new model of cell phone during its launch time, the price will be high and you will get the
same model with a very reduced price after three or four months of its launching). The profit increases
with increase in sales and attains a steady value and then falls with a dip in sales. You can see a negative
value for profit during the initial period. It is because during the product development phase there is only
investment and no returns. Profit occurs only when the total returns exceed the investment and operating
cost.
Summary
S There exists a set of characteristics which are unique to each embedded system. T
C limbedded systems arc application and domain specific.
V Quality attributes of a system represents the non-function il requirements that need to be documented properly in
any system design. ..
* The operational quality attributes of an embedded system refers to the fron^functiom1 ie |iiiiemenls that needs
to be considered for the operational"mode of the system Response, Throughput,' Ke'i'bility M.muainability,"
Security^Safetyreternre-examples of operational quality attributes.
C .1 he non-operational quality attfibut.es of an embedded system refers to the non-functional refinements that
needs to be Considered for the non-operational mode of the system. Testability, debug-ability, evolvability,
portability, time-to-prototype and market, per unit cost arid revenue, etc. are examples of nb^aj^mW^HlBP^
attributes. ... _
y The product life cycle curve (PLC) is the graphical representation of the unit cost, product sales and profit with -
respect to the various life cycle stages of the product starting from conception to disposal. ; _ .
‘ ^ For a commercial embedded product, the unit cost is peak at the introductory stage and it falls in the. maturityr
stage.' c.# . •
f The revenue of a commercial embedded product is at the peak during the maturity stage .'
Keywords
Quality attributes The non-functional requirements that need to be addressed in any system design
Reactive system An embedded system which produces changes in output in response to the changes in
input
Real-Time system A system which adheres to strict timing behaviour and responds to. requests in a known
amount of time.
Response It is a measure of quickness of the system
Throughput The rate of production or operation of a defined process over a stated period of time
Reliability It is a measure of how much\% one can rely upon the proper functioning of a system
MTBF Mean Time Between Failures-The frequency of failures in hours/weeks/months
MTTR Mean Time To Repair-Specifies how long the system is allowed to be out of ordef.fol-
lowing a failure
Time-to-prototype A measure of the time required to prototype a design
Product life-cycle (PLC) The representation of the different stages of a product from its conception to disposal
Product life cycle curve The graphical representation- of the unit cost, product sales and profit with respect to the
various life cycle stages of the product starting from conception to disposal
Introduction to Embedded Systems
Objective Questions
Embedded systems are application and domain specific. State True or False
(a) True (b) False
2. Which of the following is true about Embedded Systems?
(a) Reactive and Real Time (b) Distributed (c) Operates in harsh environment
(d) All of these (e) None of these
Which of the following is a distributed embedded system?
(a) Cell phone (b) Notebook Computer (c) SCADA system (d) All of these
(e) None of these
Quality attributes of an embedded system are
(a) Functional requirements (b) Non-functional requirements
(c) Both (d) None of these
Response is a measure of
(a) Quickness of the system , (b) How fast the system tracks changes in Input
(c) Both (d) None of these
6. Throughput of an embedded system is a measure of
(a) The efficiency of the system (b) The output over a stated period of time
(c) Both (d) None of these
7. Benchmark is
(a) A reference point (b) A set of performance criteria
(c) (a) or(b) (d) None of these
8. Mean Time Between Failures (MTBF) and Mean Time To Repair (MTTR) defines the reliability of an embedded
system. State Tme or False
(a) Tme (b) False
9. MTBF gives the frequency of failures of an embedded system. State Tme or False
(a) Tme (b) False
10. Which of the following is tme about the quality attribute ‘maintainability’?
(a) The corrective maintainability requirement for a highly reliable embedded system is very less
(b) Availability of an embedded system is directly related to the maintainability of the system
(c) Both of these
(d) None of these ,
11. The Mean Time Between Failure (MTBF) for an embedded product is very high. This means:
(a) The product is highly reliable
(b) The availability of the product is very high
(c) The preventive maintenance requirement for the product is very less
(d) All of these
(e) None of these
12. The Mean Time Between Failure (MTBF) of an embedded product is 4 months and the Mean Time To Repair
-'(MTTR) of the product is 2 weeks. What is the availability of the product?
(a) 100% (b) 50% (c) 89% (d) 10%
13. Which of the following are the three measures of information security in embedded systems?
(a) Confidentiality, secrecy, integrity (b) Confidentiality, integrity, availability
(c) Confidentiality, transparency, availability (d) Integrity, transparency, availability
Characteristics and Quality Attributes of Embedded Systems
14. You are working on a mission critical embedded system development project for a client and the client and your
company has signed a Non Disclosure Agreement (NDA) on the disclosure of the project-related information. You
share the details of the project you are working with your friend. Which aspect of Information security you are
violating here?
(a) Integrity (b) Confidentiality (c) Availability (d) None of these
15. Which of the following is an example of ‘gradual’ safety threat from an embedded system?
(a) Product blast due to overheating of the battery (b) UV emission from the embedded product
(c) Both of these (d) None of these
16. Non operational quality attributes are
(a) Non-functional requirements .(b) Functional requirements
(c) Quality attributes for an offline product (d) (a) and (c)
(e) None of these’
17. Which of the following is (are) an operational quality attribute?
(a) Testability (b) Safety (c) Debug-ability • (d) Portability
(e) All of these
18. Which of the following is (are) non-operational quality attribute?
(a) Reliability (b) Safety (c) Maintainability (d) Portability
(e) All of these (f) None of these
19. In the Information security context, Confidentiality deals with the protection of data and application from unautho¬
rised disclosure. State True or False
(a) True (b) False
20. What are the two different aspects of debug-ability in the embedded system development context?
(a) Hardware & Firmware debug-ability (b) Firmware & Software debug-ability
(c) None of these
21. For an embedded system, the quality attribute ‘Evolvability’ refers to
(a) The upgradability of the product (b) JThe modifiability of the product
(c) Both of these (d) None of these
22. Portability is a measure of ‘system independence’. State True or False
(a) True (b) False
23. For a commercial embedded product the unit cost is high during
(a) Product launching (b) Product maturity
(c) Product growth (d) Product discontinuing
24. For a commercial embedded product the sales volume, is high during
(a) Product launching (b) Product maturity
(c) Product growth (d) Product discontinuing
7. Explain the quality attribute Reliability in the embedded system design context.
8. Explain the quality attribute Maintainability in the embedded system design context.
9. The availability of an embedded product is 90%. The Mean Time Between Failure (MTBF) of the product is 30
days. What is the Mean Time To Repair (MTTR) in days/hours for the product?
10. Explain the quality attribute Information Security in the embedded system design context.
11. Explain the quality attribute Safety in the embedded system design context.
12. Explain the significance of the quality attributes Testability and Debug-ability in the embedded system design
context.
13. Explain the quality attribute Portability in the embedded system design context.
14. Explain Time-to-marketl What is its significance in'product development?
15. Explain Time-to-prototypel What is its significance in product development?
16. Explain the Product Life-cycle curve of an embedded product development.
fm
• is
Embedded Systems—Application-
and Domain-Specific
LEARNING OBJECTIVES
/ Illustrate the domain and application specific aspect of embedded systems with examples f:
/ Know the presence of embedded systems, in automotive industry ^ . ... . t&5A- T VT*' .
✓ Learn about High Speed Electronic Control Units (HECUs) and Low Speed Electronic Control Units (LECUs) employed
in automotive applications
•K Learn about the Controller Area Network (CAN), Local Interconnect Network (LIN) and Media Oriented System
Transport (MOST) communication buses used in automotive applications
V Know.the semiconductor chip providers, tools and platform -providers and solution providers for automotive
embedded applications fiiJ:'- r
As mentioned in the previous chapter on the characteristics of embedded systems, embedded systems
are application and domain specific, meaning; they are specifically built for certain applications in cer¬
tain domains like consumer electronics, telecom, automotive, industrial control, etc. In general purpose
computing, it is possible to replace a system with another system which is closely matching with the
existing system, whereas it is not the case with embedded systems. Embedded systems are highly spe¬
cialised in functioning and are dedicated for a specific application. Hence it is not possible to replace
an embedded system developed for a specific application in a specific domain with another embedded
system designed for some other application in some other domain. The following sections are intended
to give the readers some idea on the application and domain specific characteristics of embedded
systems.
Washing machine comes in two models, namely, top loading and front loading machines. In top load¬
ing models the agitator of the machine twists back and forth and pulls the cloth down to the bottom of
the tub. On reaching the bottom of the tub the clothes work their way back up to the top of the tub where
the agitator grabs them again and repeats the mechanism. In the front loading machines, the clothes are
tumbled and plunged into the water over and over again. This is the first phase of washing.
In the second phase of washing, water is pumped out from the tub and the inner tub uses centrifugal
force to wring out more water from the clothes by spinning at several hundred Rotations Per Minute
(RPM). This is called a ‘Spin Phase’. If you look into the keyboard panel of your washing machine you
can see three buttons namely* Wash, Spin and Rinse. You can use these buttons to configure the washing
stages. As you can see from the picture, the inner tub of the machine contains a number of holes and
during the spin cycle the inner tub spins, and forces the water out through these holes to the stationary
outer tub from which it is drained off through the outlet pipe.
It is to be noted that the design of washing machines may vary from manufacturer to manufacturer,
but the general principle underlying in the working of the washing machine remains the same. The basic
controls consist of a timer, cycle selector mechanism, water temperature selector, load size selector and
start button. The mechanism includes the motor, transmission, clutch, pump, agitator, inner tub, outer
tub and water inlet valve. Water inlet valve connects to the water supply line using at home and regulates
the flow of water into the tub.
The integrated control panel consists of a microprocessor/controller based board with I/O interfaces
and a control algorithm running in it. Input interface includes the keyboard which consists of wash type
selector namely* Wash, Spin and Rinse, cloth type selector namely* Light, Medium, Heavy duty and
washing time setting, etc. The output interface consists of LED/LCD displays, status indication LEDs,
etc. connected to the I/O bus of the controller. It is to be noted that this interface may vary from manu¬
facturer to manufacturer and model to model. The other types of I/O interfaces which are invisible to the
end user are different kinds of sensor interfaces, namely, water temperature sensor, water level sensor,
etc. and actuator interface including motor control for agitator and tub movement control, inlet water
flow control, etc.
1. Instrumentation
10. Power windows
2. Engine control
8. Supension control
7. Wiper control
3. Fan control
6. ABS control
4. Fuel injection control
from 20 to 40 whereas a luxury vehicle like Mercedes S and BMW 7 may contain 75 to 100 numbers of
embedded controllers. Government regulations on fuel economy, environmental factors and emission
standards and increasing customer demands on safety, comfort and infotainment forces the automotive
manufactures to opt for sophisticated embedded control units within the vehicle. The first embedded
system used in automotive application was the microprocessor based fuel injection system introduced
by Volkswagen 1600 in 1968.
The various types of electronic'control units (ECUs) used in the automotive embedded industry can
be broadly classified into two-High-speed embedded control units and Low-speed embedded control
units.
4.2.1.1 High-speed Electronic Control Units (HECUs) High-speed electronic control units
(HECUs) are deployed in critical control units requiring fast response. They include fuel injection
systems, antilock brake systems, engine control, electronic throttle, steering controls, transmission
control unit and central control unit.
Atmel (www.atmel.com): Supplier of cost-effective high-density Flash controllers and memories. At-
mel provides a series of high performance microcontrollers, namely, ARM®1, AVR®2, and 80C51. A
wide range of Application Specific Standard Products (ASSPs) for chassis, body electronics, security,
safety and car infotainment and automotive networking products for CAN, LIN and FlexRay are also
supplied by Atmel.
Maxim/Dallas (www.maxim-ic.com): Supplier of world class analog, digital and mixed signal products
(Microcontrollers, ADC/DAC, amplifiers, comparators, regulators, etc), RF components, etc. for all
kinds of automotive solutions. 1
NXP semiconductor (www.nxp.com): Supplier of 8/16/32 Flash microcontrollers.
Renesas (www.renesas.com): Provider of high speed microcontrollers and Large Scale Integration (LSI)
technology for car navigation systems accommodating three transfer speeds: high, medium and low.
Texas Instruments (www.ti.com): Supplier of microcontrollers, digital signal processors and automo¬
tive communication control chips for Local Inter Connect (LIN) bus products. ' 1
Fujitsu (www.fmal.fujitsu.com): Supplier of fingerprint sensors for security applications, graphic dis¬
play controller for instmmentation application, AGPS/GPS for vehicle navigation system and different
types of microcontrollers for automotive control applications.
Infineon (www.infineon.com): Supplier of high performance microcontrollers and customised applica¬
tion specific chips.
NEC (www.nec.co.jp): Provider of high performance microcontrollers.
There are lots of other silicon manufactures which provides various automotive support systems like
power supply, sensors/actuators, optoelectronics, etc. Describing all of them is out of the scope of this
book. Readers are requested to use the Internet for finding more information on them.
4.2.3.2 Tools and Platform Providers Tools and platform providers are manufacturers and suppli¬
ers of various kinds of development tools and Real Time Embedded Operating Systems for developing
and debugging different control unit related applications. Tools fall into two categories, namely embed¬
ded software application development tools and embedded hardware development tools. Sometimes the
silicon suppliers provide the development suite for application development using their chip. Some third
party suppliers may also provide development kits and libraries. Some of the leading suppliers of tools
and platforms in automotive embedded applications are listed below.
ENEA (www.enea.com): Enea Embedded Technology is the developer of the OSE Real-Time operat¬
ing system. The OSE RTOS supports both CPU and DSP and has also been specially developed to sup¬
port multi-core and fault-tolerant system development.
The Math Works (www.mathworks.com): It is the world’s leading developer and supplier of technical
software. It offers a wide range of tools, consultancy and training for numeric computation, visualisa¬
tion, modelling and simulation across many different industries. MathWork’s breakthrough product is
MATLAB-a high-level programming language and environment for technical computation and numeri¬
cal analysis. Together MATLAB, SIMULINK, Stateflow and Real-Time Workshop provide top quality
tools for data analysis, test & measurement, application development and deployment, image processing
and development of dynamic and reactive systems for DSP and control applications.
Keil Software (www.keil.com): The Integrated Development Environment Keil Microvision from Keil
software is a powerful embedded software design tool for 8051 & Cl66 family of microcontrollers.
Lauterbach (http://www.lauterbach.com/): It is the world’s number one supplier of debug tools, pro¬
viding support for processors from multiple silicon vendors in the automotive market.
ARTiSAN (www.artisansw.com): Is the leading supplier of collaborative modelling tools for require¬
ment analysis, specification, design and development of complex applications.
Microsoft (www.microsoft.com): It is a platform provider for automotive embedded applications.
Microsoft’s WindowsCE is a powerful RTOS platform for automotive applications. Automotive features
are included in the new WinCE Version for providing support for automotive application developers.
4.2.3.3 Solution Providers Solution providers supply OEM and complete solution for automotive
applications making use of the chips, platforms and different development tools. The major players of
this domain are listed below.
Bosch Automotive (www.boschindia.com): Bosch is providing complete automotive solution ranging
from body electronics, diesel engine control, gasoline engine control, powertrain systems, safety systems,
in-car navigation systems and infotainment systems.
DENSO Automotive (www.globaldensoproducts.com): Denso is an Original Equipment Manufacturer
(OEM) and solution provider for engine management, climate control, body electronics, driving control
& safety, hybrid vehicles, embedded infotainment and communications.
Infosys Technologies (www.infosys.com): Infosys is a solution provider for automotive embedded hard¬
ware and software. Infosys provides the competitive edge in integrating technology change through cost-
effective solutions.
Delphi (www.delphi.com): Delphi is the complete solution provider for engine control, safety, infotain¬
ment, etc., and OEM for spark plugs, bearings, etc.
.and many more. The list is incomplete. Describing all providers is out of the scope of this book.
Summary
_
V Embedded systems designed for a particular application for a specific domain cannot be replaced with another
embedded system designed for another application for a different domain
S Consumer, industrial, automotive, telecom, etc. are the major application domains of embedded systems. Tele¬
com and automotive industry are the two segments holding a big market share of embedded systems
J Automotive embedded systems are normally built around microcontrollers or DSPs or a hybrid of the two and
are generally known as Electronic Control Units (ECUs)
^ High speed Electronic Control Units (HECUs) are deployed in critical control units requiring fast response, like
fuel injection systems, antilock brake system, etc.
V Low speed Electronic Control Units (LECUs) are deployed in applications where response time is not so critical.
They are generally built around low cost microprocessors/microcontrollers and digital signal processors. Audio
controllers, passenger and driver door locks, door glass controls, etc., are examples for LECUs.
S- Automotive applications use serial buses for communication. Controller Area Network (CAN), Local Intercon¬
nect Network (LIN), Media Oriented System Transport (MOST) bus, etc. are the important automotive commu¬
nication buses.
Introduction to Embedded Systems
V CAN is an event driven serial protocol interface with support for error handling in data transmission. It is gener¬
ally employed in safety system like airbag control, powertrain systems like engine control and Antilock Brake
| Systems^ABS),
V LIN bus is a single master multiple slave, (up to 16 independent slave nodes) communication interface: LIN is
a low speed, single wire communication interface with support for data rates up to 20Kbps and is used for sen-
sor/actuator interfacing.
v The Media Oriented System Transport (MOST) bus is targeted for automotive audio video equipment interfac¬
ing. MOST bus is a multimedia fibre-optic point-to-point network implemented in a star, ring or daisy-chained
topology over optical tibiss cables1
■ ■S- The key players of the automotive embedded market can be classified into ‘Silicon Providers’, ‘Tools and Plat-
’ >~f 'll'6 a
:A
JEGU : Electronic Control Unit. The generic term for the embedded control units in automotiye ^pli^ation |pf';
HECl1: High-speed Electronic Control Unit. The high-speed embedded control unit deployed in automotive appli¬
'
cations
LECU : Low-speed Electronic Control Unit. The low-speed embedded control unit deployed in automotive applica¬
tions , • . , - . ‘I |ft
CAN : Controller Area Network. An event driven serial protocol interface used primarily for automotive applica-
Objective Questions
Review Questions
Z' Understand the different factors that need to be considered while selecting a microcontroller for an embedded
1;:. design ■ '' " . . A ■ .. Z* • \
ZyKnow why 8051 is the popular choke for low cost tow performance embedded system design yfi
Z Learn the A to Z of 8051 microcontroller architecture ;A
Z Learn the Internals of the 8051 microcontroller ■ ■ "■ zyy
Z team the program memory and internal data ,memory organisation of8051
,-ZLearn the iPaged, Data memory accessand Von-Neumann memory model implementation for 8051 \
Z'tearn about the organisation of lower 128 bytes RAM'for data memory, upper 128 bytes RAM for SFR land upper s
128byies.RAM for Internal Data memory (IRAM) ‘ /■- f ; . - . . ' -vyj
Z Learn about the CPU registers and general purpose registers of8051 ‘ ' . -
Z Learn about the Oscillator unit and speed of execution of 8051
■A Learn about the different I/O ports, organisation of the ports) the internal implementation of the port pins, the
different registers associated with the ports and the opera tion of ports
Z Learn about interrupts and its significance in embedded applications
Z Learn about the Interrupt System of 8051, the interrupts supported by 8051, interrupt priorities, different registers
associated with configuring the interrupts, Interrupt Service Routine and their vector address
Z Learn about the Timer/Counter units supported by 8051 and configuring the Timer unit for Timer/Counter opera¬
tion. Learn the different registers associated with timer units and the different modes of operations supported by
Timer/Counter units
Z Learn about the Serial Port of the 8051, the different control!, status and data registers associated with it
Z Learn the different modes of operations supported by the Serial port and setting the baudratefor each mode
z Learn about the Power-On Reset circuit implementation for 8051
Z Learn about the different power saving modes supported by 8051
Z Learn the difference between 8051 and 8052
A recent survey on the microcontroller industry reveals that 8bit microcontrollers account for more than
40% of the total sales in the microcontroller industry. Among the 8bit microcontrollers, the 8051 family
is the most popular, cost effective and versatile device offering extensive support in the embedded appli-
DesigningEmhedded Systems with 8bit Microcontrollers—8051
cation domain. Looking back to the history of microcontrollers you can find that the 8bit microcontroller
industry has travelled a lot from its first popular model 8031AH built by Intel in 1977 to the advanced
8bit microcontroller built by Maxim/Dallas recently, which offers high performance 4 Clock, 75MHz
operation 8051 microcontroller core (Remember the original 8031 core was 12 Clock with support for
a maximum system clock of 6MHz, so a performance improvement of 3 times on the original version
in execution) with extensive support for networking by integrating 10/100 Ethernet MAC with IEEE
802.3 MMI, CAN bus for automotive application support, RS-232 C interface for legacy serial applica¬
tions, SPI serial interface for board level device inter connect, 1-wire interface for connecting to low
cost multi-drop sensors and actuators, and 16MB addressing space for code and data memory.
for prototyping and sample development stuffs to alleviate the development pains? Does the controller
support third party development tools? Does the manufacture provide technical support if necessary?
5.1.6 Availability
Availability is another important factor that should be taken into account for the selection process. Since
the product is entirely dependent on the pontroller, the product development time and time to market the
product solely depends on its availability. By technical terms it is referred to as Lead time. Lead time is
the time elapsed between the purchase order approval and the supply of the product.
5.1.8 Cost
Last but not least, cost is a big deciding factor in selecting a controller. The cost should be within the
reachable limit of the end user and the targeted user should not be high tech. Remember the ultimate aim
of a product is to gain marginal benefit.
8051 isa very versatile microcontroller featuring powerful Boolean processor which supports bit manip¬
ulation instructions for real time industrial control applications. The standard 8051 architecture supports
6 interrupts (2 external interrupts, 2 timer interrupts and 2 serial interrupts), two 16bit timers/counters,
32 I/O lines and a programmable full duplex serial interface. Another fascinating feature of 8051 is the
way it handles interrupts. The interrupts have two priority levels and each interrupt is allocated fixed 8
bytes of code memory. This approach is very efficient in real time application. Though 8051 is invented
■by Intel, today it is available in the market from more than 20 vendors and with more than 100 variants
of the original 8051 flavour, supporting CAN, USB, SPI and TCP/IP interfaces, integrated ADC/D AC,
LCD Controller and extended number of I/O ports. Another remarkable feature of 8051 is its low cost.
The 8051 flash microcontroller (.AT89C51) from Atmel is available in the market for less than 1US$ per
piece. So imagine its cost for high volume purchases.
5.3.2.1 The Program (Code) Memory The basic 8051 architecture provides lowest 4K bytes
of program memory as on-chip memory (built-in chip memory). In 8031, the ROMless counterpart of
8051, all program memory is external to the chip. Switching between the internal program memory and
external program memory is accomplished by changing the logic level of the pin External Access (EA\).
Tying EA\ pin to logic 1 (Vcc), configures the chip to execute instructions from program memory up to
4K (program memory location up to OFFFH) from internal memory and 4K (program memory location
from 1000H) onwards from external memory, while connecting EA\pin to logic 0 (GND) configures the
chip to external program execution mode, where the entire code memory is executed from the external
memory. Remember External Access pin is an active low pin (Normally referred as EA\). The control
signal for external program memory execution is PSEN\ (Program Strobe Enable). For internal program
Introduction to Embedded Systems
memory fetches PSEN\ is not activated. For 8031 controller without on-chip memory, the PSEN\ signal
is always activated during program memory execution. The External Access pin (EA\) configuration
and the corresponding code memory access are illustrated in Fig. 5.2.
FFFFH FFFFH [
i
mnx&rm' 1 RU ■' BH
|
1
1
-./■I 1000H I
- M OFFFHj. 1 ’.. .”3
1 - 'KE k ••• *>• A' iff-1* fr|
• ■
t- ■ ; ■ VjLtPft
IS ■
SBm
Pgp|r U.:y 0000H |1 . J■
EA\ = 0 EA\= 1
. External program Internal program
memory access memory access
If the program memory is external, 16 I/O lines are used for accessing the external memory. Port 0
and Port 2 are used for external memory accessing. Port 0 serves as multiplexed address/data bus for
external program memory access. Similar to the 8085 microprocessor, Port 0 emits the lower order
address first. This can be latched to an 8bit external latch with the Address Latch Enable (ALE) signal
emitted by 8051. Once the address outirig is over, Port 0 functions as input port for data transfer from
the corresponding memory location. The address from which the program instmction to be fetched is
supplied by the 16bit register, Program Counter (PC), which is part of the CPU. The Program Counter is
a 16bit register made up of two 8bit registers. The lower order byte of program counter register is held
by the PCL register and higher order by the PCH register. PCL and PCH in combination serve as a 16bit
register. During external program memory fetching, Port 0 emits the contents of PCL and Port 2 emits
the contents of PCH register. Port 0 emits the contents of PCL only for a fixed duration allowing .the
external latch to hold the content on arrival of the ALE signal. Afterwards Port 0 goes into high imped¬
ance state, waiting for the arrival of data from the corresponding memory location of external memory.
Whereas Port 2 continues emitting the contents of PCH register throughout the external memory fetch.
Once the PSEN\ signal is active, data from the program memory is clocked into Port 0. Remember, dur¬
ing external program memory access Port 0 and Port 2 are dedicated for it and cannot be used as general
purpose I/O ports. The interfacing of an external program memory chip is illustrated in Fig. 5,3.
5.3.2.2 The Data Memory The basic 8051 architecture supports 128 bytes of internal data memory
and 128 bytes of Special Function Register memory. Special Function Register memory is not avail¬
able for the user for general data memory applications. The address range for internal user data memory
is 00H to 7FH. Special Function Registers are residing at memory area 80H to FFH. 8051 supports
interface for 64 Kbytes of external data memory. The control signals used for external data memory
access are RD\ and WR\ and the 16bit register holding the address of external data memory address to
be accessed is Data Pointer (DPTR). Similar to the Program Counter, the Data Pointer is also made up
of two 8bit registers, namely, DPL (holding the lower order 8bit) and DPH (holding the higher order
8bit). The program counter is not accessible to the user whereas DPTR is accessible to the user and the
contents of DPTR register can be modified. In external data memory operations, Port 0 emits the content
of DPL and Port 2 emits-the content of DPH. Port 0 is address/data multiplexed in external data memory
operations also. The internal and external data memory model of 8051 is diagrammatically represented
in Fig. 5.4.
Internal data memory addresses are always one byte long. So it can accommodate up to 256 bytes
of internal data memory (Ranging from 0 to 255). However the addressing techniques used in 8051
' can accommodate 384 bytes using a simple memory addressing technique. The technique is: Direct
Introduction to Embedded Systems
addressing of data memory greater than 7FH will access one memory space, namely Special Function
Register memory and indirect addressing of memory address greater than 7FH will access another
memory space, the upper 128 bytes of data memory (Direct and indirect memory addressing will be dis¬
cussed in detail in a later section). Remember these techniques will work only if the upper data memory
is physically implemented in the chip. The basic version of 8051 does not implement the upper data
memory physically. However the 8052 family implements the upper data memory physically in the chip
and so the upper 128 byte memory is also available for the user as general purpose memory, if accessed
through indirect addressing.
External data memory address can be either one or two bytes long. As described earlier, Port 0 emits
the lower order 8bit address and, if the memory address is two bytes and if it ranges up to 64K, the entire
bits of Port 2 is used for holding the higher order value of data memory address. If the memory range
is 32K, only 7 bits of Port 2 is required for addressing the memory. For 16K, only 6 lines of Port 2 are
required for interfacing and so on. Thereby you can save some port pins of Port 2. The interfacing of an
external data memory chip is illustrated Fig. 5.5.
5.3.2.3 Paged Data Memory Access In paged mode addressing, the memory is arranged like the
lines of a notebook. The notebook may contain 100 to 200 pages and each page may contain a fixed
number of lines. You can access a specific line by knowing its page number and the line number. Mem¬
ory can also arrange like the lines of a notebook. By using 8bit address, memory up to 256 bytes can be
accessed. Imagine the situation where the memory is stacked of 256 bytes each. You can use port pins
(High order address rule) to signal the page number and the lower order 8 bits to indicate the memory
location corresponding to that page.
For example, take the case where paging is done using the port pin P2.0 and port 0 is used for holding
the lower address. The memory range will be
5.3.2.4 The Von-Neumann Memory Model for 8051 The code memory and data memory of
8051 can be combined together to give the Von-Neumann architectural benefit for 8051. A single mem¬
ory chip with read/write option can be used for this. The program memory can be allocated to the lower
memory space starting from OOOOH and data memory can be assigned to some other specific area after
the code memory. For program memory fetching and data memory read operations combine the PSEN\
and RD\ signals using an AND gate and connect it to the Output Enable (OE\) signal of the memory
chip as shown in Fig. 5.6.
The Von-Neumann memory model is very helpful in evaluation boards for the controller, which
allows modification of code memory on the fly. The major drawbacks of using a single chip for program
and data memory are
• Accidental corruption of program memory
• Reduction in total memory space. In separate program and data memory model the total available
memory is 128KB (64KB program memory + 64KB Data memory) whereas in the combined
model the total available memory is only 64KB
5.3.2.5 Lower 128 Byte Internal Data Memory (RAM) Organisation This memory area is
volatile; meaning the contents of these locations are not retained on power lose. On power up these
memory locations contain random data. The lowest 32 bytes of RAM (00H to 1FH) are grouped into 4
banks of 8 registers each. These registers are known as RO to R7 registers which are used as temporary
data storage registers during program execution. The effective usage of these registers reduces the code
memory requirement since register instructions are shorter than direct memory addressing instructions.
The next 16 bytes of RAM with address 20H to 2FH is a bit addressable memory area. It accommodates
128 bits (16 bytes x 8), which can be accessed by direct bit addressing. The address of bits ranges from
OOH to 7FH. This is very useful since 8051 is providing extensive support for Boolean operations (Bit
Manipulation Operations). Also it saves memory since flag variables can be set up with these bits and
Introduction to Embedded Systems
there is no need to waste one full byte of memory for setting up a flag variable. These 16 bytes can also
be used as byte variables. The context in which these bytes are used as either byte variable or bit vari¬
able is determined by the type of instruction. If the instruction is a bit manipulation instruction and the
operand is given as a direct address in the range 00H to 7FH, it is treated as a bit variable. The lower 128
bytes of internal RAM for 8051 family members is organised as shown in Fig. 5.7.
Scratchpad RAM
80 bytes 30H -7FH
BankO
(Tig. 5.?] Internal 128 bytes of lower order data memory organisation
Remember the byte-wise storage and bitwise storage in the area 20H to 2FH shares a common physi¬
cal memory area and you camiot use this for both byte storage and bit storage simultaneously. If you do
so, depending on the usage, the byte variables and bit variables may get corrupted and it may produce
unpredicted results in your application, This is explained more precisely using the following table.
B7 *>B6-
*
l * ms ■ B4 •< «B3| vv B2 B1 BO Byte Address
7FH ; 7EH ' 71)11 7CH 7BH 7AT1 79H 78H , ri 2FI1
•*»f r\
77H 1 • 76H 74H 7.3 H 72H 71TI ; . ’ 70H 2 EH
6FH 6HH 6DH 6CH 6BT1 ' 6 All .6911 68H 2DH
TFT 66H %5]H 64H 6 Hi 62H 61H t 60H ; -2CH ■
5FH 5EH 51)11 5CH 5BH 5 All 5911 58H 2BH
'711 ’ 56H 5511 54H 53H ; | | 52H • ! 51H A i 50H ’‘2AH'_
4FH 4EI1 4DH 4CH 4BH 4AH 49H 1 48H 29H-
-• 47H • ; 46H \t 1 45H 44H. 43H 42H - 41H - s aoh 1 » i'28H -
3FH 3EH' 3DH 3CH 3BH : 3 ah " 39H 38H 2711
2 16H.U
:.. ... . . >• -V. ■.
15H * v:.?*■ 14H
‘ifi•vYr-'^te.-v ’
A
•. V: 13H iin 1 III
'• - ur,.
:
U.i
10H _ r. •;
. 22H
0EH 0DH odi 0B11 0AI1 0911 0811 21H
0611 05H 04H - 03H V ASA OiH OOH 20H -
BO, Bl, B3 . .B7 represents the bit addresses. Now let’s have a look at the following piece of
Assembly code:
This piece of assembly code sets the bit with bit address OIH and loads the memory location 20H
with value FOH. Don’t worry about the different instructions used here. We will discuss about the 8051
instruction set in a later chapter. The MOV2OH, #00H instruction clears the memory location 20H. The
SETB OIH instruction stores logic 1 in the bit address OIH. In reality the bit address OIH is the bit 1 of
the memory location pointed by address 20H. Executing the instruction SETB OIH changes the contents
of memory location 20H to 02H (00000010b. Only bit 1 is in logic 1 state). The instruction MOV 20H,
ffiOH alters the content of memory location 20H with FOH (11110000b). This overwrites the informa¬
tion held by bit address 01H and leads to data corruption. So be careful while using bitwise storage and
byte-wise storage simultaneously.
The next 80 bytes of RAM with address space 30H to 7FH is used as general purpose scratch¬
pad RAM. Though memory spaces OOH to 2FH have specific usage, they can also be used as general
purpose scratchpad (Read/Write) RAM. The lower 128 byte RAM can be accessed by either direct
addressing or indirect addressing.
5.3.2.6 The Upper 128 bytes RAM (Special Function Registers) The upper 128 bytes of RAM
when accessed by direct addressing, accesses the Special Function Registers (SFRs). SFRs include port
latches, status and control bits, timer control and value registers, CPU registers, stack pointer, accumula¬
tor, etc. Some of the SFR registers are only byte level accessible and some of them are both byte-wise
and bit-wise accessible. SFRs with address ends in OH and 8H are both bit level and byte level acces¬
sible. In the standard 8051 architecture, among the 128 bytes, only a few bytes are occupied by the SFR
and the rest are left unused and are reserved for future implementations. The table given below explains
the SFR implementation for standard 8051 architecture.
SFR memory is not available to the user for general purpose scratchpad RAM usage. However the
user can modify the contents of some of the SFR according to the program requirements. Some of the
SFRs are Read Only. Some of the SFR memory spaces are not implemented in the basic 8051 version.
They are reserved for future use and users are instructed not to do anything with this reserved SFR
space. Reading from the unimplemented SFR memory address returns random data and writing to this
memory location will not produce any effect. Each of the SFRs will be discussed in detail in the sections
covering their usage.
5.3.2.7 Upper 128 Bytes of Scratchpad RAM (IRAM) Variants of 8051 and the 8052 architec¬
ture where the upper 128 bytes of RAM are physically implemented in the chip can be used as general
purpose scratchpad RAM by indirect addressing. They are generally known as IRAM. The address of
IRAM ranges from 80H to FFH and the access is indirect. Registers R0 and R1 are used for indirect ad¬
dressing. For example, for accessing the IRAM located at address 80H, load R0 or R1 with 80H and use
the indirect memory access instruction. The following piece of assembly code illustrates the same.
•MOV R0,ii80ii Load IRMt .address :':80H in indirect register
.MOV A, 0RO % ; Load Accumulator with IRAM content at .address 8OH ' iI
5.3.3 Registers
Registers of 8051 can be broadly classified into CPU Registers and Scratchpad Registers.
5.3.3.1 CPU Registers Accumulator, B register, Program Status Word (PSW), Stack Pointer (SP),
Data Pointer (DPTR, Combination of DPL and DPFI), and Program Counter (PC) constitute the CPU
registers. They are described in detail below.
Accumulator (ACC) (SFR-EOH) It is the most important CPU register which acts as the heart of all
CPU related Arithmetic operations. Accumulator is an implicit operand in most of the arithmetic opera¬
tions. Accumulator is a bit addressable register. ,
B Register (SFR-FOH) It is a CPU register that acts as an operand in multiply and division operations.
It also stores the remainder in division and MSB in multiplication Instruction. B can also be used as a
general purpose register for programming.
Program Status Word (PSW) (SFR-DOH) It is an 8-bit, bit addressable Special Function register
signalling the status of accumulator related operations and register bank selector for the scratch pad
registers R0 to R7. The bit details of PSW register is given below.
The table given below explains the meaning and use of each bit.
The following table illustrates the possible combinations for the register bank select bits, the cor¬
responding register bank number and the address for the scratchpad registers R0-R7 in the specified
register bank.
.
r,. - * L '- - 1 i r F >» J - -
liiiiiii!r . p-1
A.;?’ i;;
-. -1 ' * -■ ! ; ;
I SI
J- T
V:JBM88llLvlfl. ~ iflll-Dil
Mszt- SM-'D. •: *••• • r.-'
? slid Fit
The power-on reset value for the bits RSI and RS2 are 0 and the default register bank for scratchpad
registers RO to R7 is 0 and the address range for RO to R7 is 00H to 07H. A programmer can change the
register bank by changing the values of RSI and RSO. The following piece of assembly code illustrates
the selection of bank 2 for the scratchpad registers RO to R7.
CLR RSO ; Clear- bit RSI
S SETB RSI ; Set bit RSI. Bank 2 is selected ' . .' ;
Data Pointer (DPTR) (DPL: SFR-82H, DPH: SFR-83H) It is a combination of two 8-bit register
. namely DPL (Lower 8-bit holder of DPTR) and DPH (Higher order 8-bit holder of DPTR). DPTR holds
the 16-bit address of the external memory to be read or written in external data memory operations. DPH
and DPL can be used as two independent 8-bit general purpose registers for application programming.
Program Counter (PC) It is a 16-bit register holding the address of the code memory to be fetched.
It is an integral part of the CPU and it is hidden from the programmer (It is not accessible to the pro¬
grammer).
Stack Pointer (SP) (SFR-81H) It is an 8-bit register holding the current address of stack memory.
Stack memory stores the program counter address, other memory and register values during a sub rou¬
tine/function call. On power on reset the stack pointer register value is set as 07H. The stack pointer
address and bank 0, address of R7 is same when the controller is at reset, so care should be taken for
selecting SP address. It is the responsibility of the programmer to assign sufficient stack memory by
entering the starting address of stack into the Stack Pointer register. Care should be taken to avoid the
Introduction to Embedded Systems
overflow of stacks and merging of stack memory with data memory. This will result in un-predicted
program flow. The stack grows up in memory.
5.3.3.2 Scratchpad Registers (RO to R7) The scratchpad registers RO to R7 is located in the lower
32 bytes of internal RAM. It can be on one of the four banks, which is selected by the register selector
bits RSO and RS1 of the PS W register. On power on reset, by default, RSO and RS1 are 0 and the default
bank selected is bank 0. There are eight scratchpad registers and they are named as RO, R1.. ,R7. The
register names and their memory address corresponding to bank 0 are given below.
. . . . ... _ . _
* CMOS—Complementary metal-oxide-semiconductor field effect transistor technology for digital circuit design. CMOS features less
power consumption and high logic density on an integrated circuit.
** NMOS—n-type metal-oxide-semiconductor field effect transistor technology for digital circuit design. It is an old technology and pos¬
sesses the drawback of noise susceptibility and slow logic transition. In modern designs it is supplanted by CMOS technology.
Designing Embedded Systems with 8bit Microcontrollers—8051
You may be thinking why an oscillator circuit is required? The answer is-The microcontroller chip
is made up of digital combinational and sequential circuits and they require a clock to drive the digital
circuitry. The clock is supplied by this oscillator circuit and the operational speed of the chip is depen¬
dent on the clock speed.
5.3.4.1 Execution Speed The execution speed of the processor is directly proportional to the oscil¬
lator clock frequency. Increasing the clock speed will have direct impact on the speed of program execu¬
tion. But the internal processor core design will always have certain limitations on the maximum clock
frequency on which it can be operated. During program execution the instructions stored in the code
memory is fetched, decoded and corresponding action is initiated. Each instruction fetching consists of
the number of machine cycles. The instruction set of 8051 contains single cycle to four machine cycle
instructions.
Each machine cycle is made up of a sequence of states called Tstates. The original 8051 processor’s
machine cycle consists of 6 T states and is named SI, S2, S3...S6. Each T states in turn consist of two
oscillator periods (Clock cycles) and so one machine cycle contains 12 clock cycles. For a one machine
cycle instruction to execute, it takes 12 clock cycles. If the system clock frequency is 12MHz, it takes
lmicrosecond (1 pis) time to execute one machine cycle. The machine cycle, T state and clock cycle
relationship is illustrated in the following Fig. 5.9.
5.3.5 Port
Port is, a group of Input/Output (I/O) lines. Each port has its own port control unit, port driver and buf¬
fers. The original version of 8051 supports 321/0 lines grouped into 4 I/O ports, consisting of 8 I/O
lines per port. The ports are named as Port 0, Port l,Port 2 and Port 3. One output driver and one input
buffer is associated with each EO line. All four ports are bi-directional and an 8bit latch (Special Func¬
tion Register) is associated wi/h each port.
.5.3.5.1 Port 0 PORT 0 is a bi-directional/port, which is used as a multiplexed address/data bus in
external data memory/program memory operations. Port 0 pin organisation is illustrated in Fig. 5.10.
Each pin of Port 0 possesses a bit latch which is part of the Special Function Register (SFR) for Port
0, PO. The latch is a D flip flop and if docks in a logic value (either logic 1 or logic 0) from the internal
Introduction to Embedded Systems
Internal bus
bus when a write to the corresponding latch signal is issued by the internal control unit. Apart from the
write to latch operation you can read the contents of the corresponding Port 0 Pin latch by activating a
Read Latch signal. The Read Latch signal is asserted on executing an instruction with a read from the
corresponding ‘port latch’ instruction (e.g. ANL PO, A reads the PO latch, logical AND it with accumula¬
tor and loads the latch with the result. Similarly, the instruction MOV P0.0,C reads the Port 0 byte (8
bits of the PO latch) and modify bit 0 and write the new byte back to the PO latch. In summary all ‘Read -
When configured as 0/p port by writing Is to the Port 0 SFR, all Port 0 pins floats and it is said to be
in a high impedance state. Port 0 is a true bi-directional port.
Port 0 SFR (PO) (SFR-80H) Port 0 SFR is a bit addressable Special Function Register that acts as the
bit latch for each Port 0 pins.
During external memory operations PO SFR gets Is written into it. The reset value of Port 0 SFR is FFH
(All latch bits set to 1)
5.3.5.2 Port 1 PORT 1 is a bi-directional port which is used as a general purpose I/O port. The Port
1 pin Organisation is given in Fig. 5.11.
Internal bus
As seen in the pin organisation, Port 1 pin contains an internal pull-up resistor. In order to make the
Port 1 pins as input line, the corresponding SFR latch bit for Port 1 should be kept as 1. Writing a 1
into any of the PI SFR bit latch turns off the output driver FET and produces logic high at the corre¬
sponding port pin. The internal pull up for Port 1 is fixed and weak. When Port 1 pins are configured as
inputs (by writing a 1 to the corresponding Port 1 SFR bit latch) the pins are pulled high and they can
' source current when an externally connected device pulls the port pin to low, signaling a logic 0 at the
corresponding input line and places logic 0 to the internal bus in response to a Read Pin command. If
the externally connected device forces logic high, the Read Pin control signal generated by a Read Pin
related command (e.g. MOV A,PI, MOV C,P1.0, etc.) places logic high into the internal bus.
Since Port 1 holds fixed internal pull ups and are capable of sourcing current, it is known as Quasi
Bi-directional.
Port 1 SFR (PI) (SFR- 90H) It is also a bit addressable Special Function Register that acts as the bit
latch for each pin of Port 1. The bit details of Portl SFR is given below.
S3 •*&»<:
BIT 2 , BIT 1
a »iJwfc'XS&fc.v-'•i.-zM'-'S*. 'S’...-’ >>
BITO
PI.5 P1.4
The reset value of Port 1 SFR is FFH (All bit latches set to 1).
Introduction to Embedded Systems
5.3.5.3 Port 2 Port 2 is designed to operate in two different modes. It acts as general purpose I/O
port in normal operational mode and acts as higher order address bus in external data memory/program
memory operations. Figure 5.12 illustrates the Port 2 pin organisation.
Internal bus
Port 2 emits the higher order byte of external memory address if the address is 16 bits wide. As seen
in the figure, during 16-bit wide external memory operations the base drive for the O/p driver FET is
internally switched to the address line. If the address line is emitting a 1, the O/p driver FET is turned
off and the logic 1 is reflected on the O/p pin. If the address line is emitting a 0, the O/p driver FET is
turned on and the logic 0 is reflected at the corresponding pin.
The content of Port 2 SFR remains unchanged during external memory access and it holds the pre¬
vious content as such. It is To be noted that if Port 2 is in external memory operation it cannot be used
as general purpose I/O line. When not used for external memory access, Port 2 can be used as general
purpose I/O port. During normal operation mode, the internal multiplexer switches the base line (GATE)
of O/p FET to the D latch O/p of corresponding SFR bit latch. In normal operation mode when a 1 is
written into any of the P2 bit latch, the O/p driver FET is turned off and as in the case of Port 1 this line
acts as an I/p line. P2 is a Quasi bi-directional port.
Port 2 SFR (P2) (SFR- ROH) It is a bit addressable Special Function Register that acts as the bit latch
for each pins of Port 2. The reset value of Port 2 SFR is FFH (All bit latches set to 1).
5.3.5.4 Port 3 Port 3 is a general purpose I/O port which is also configurable for implementing alter¬
native functions. Port 3 Pin configuration is shown in Fig. 5.13.
Port 3 is identical to Port 1 in operation. All the settings that need to be done for configuring Port 1
as I/O port is applicable to Port 3 also. The only difference is that the SFR latch for Port 3 is P3. Port 3
supports alternate I/O functions. The alternate I/O functions supported by Port 3 and the pins used for
these alternate functions are tabulated below.
Designing Embedded Systems with 8bit Microcontrollers—8051
Port 3 SFR (P3) (SFR-BOH) It is a bit addressable Special Function Register that acts as the bit latch
for each pin of Port 3. Reset value of Port 3 SFR is FFH (All bit latches set to 1).
” ■ '■ .. :
©IT 7 V if BIT
£
't£dr'
6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1
• P3.7 P3.6 P3.2 P3.1 P3.0
5.3.5.5 ‘Read Port Latch’ and ‘Read Port Pin’ Operations As we discussed earlier, the ‘Port
Read’ operations fall into two categories namely, Read Latch and Read Pin. The Read Latch operation
reads the content of the corresponding port latch. The port architecture for all 4 ports contains neces¬
sary circuit for reading the Port Latch for all port pins. The read Port Latch operation is triggered by
the control signal Read Latch. The Read Latch control signal is generated internally on executing an
instruction implementing the Read Latch operation. The Read-Modify-Write instructions which reads
the port, modifies it and re-writes it to the port, operates on the port latch instead of port pins. The fol-
Introduction to Embedded Systems
lowing Read-Modify- Write instructions operate on port latch when the destination operand is a Port or
a Port bit.
ANL Px, <source> (x= 0,1,2,3. e.g. ANL P0, A)
ORL Px, <source> (x= 0,1,2,3. e.g. ORL PI, A)
XRL Px, <source> (x= 0,1,2,3. e.g. XRL P2, A)
JBC Px.y, LABEL (x= 0,1,2,3. y = 0 to 7 e.g. JBCP3.0, REPEAT)
CPL Px.y (x= 0,1,2,3. y = 0 to 7 e.g. CPL P0.2)
INC Px (x= 0,1,2,3. e.g. INCP0)
DEC Px (x= 0,1,2,3. e.g. DEC PI)
DJNZ Px, LABEL Px (x= 0,1,2,3. e.g. DJNZP0, REPEAT)
MOV Px.y, C (x= 0,1,2,3. y = 0 to 7 e.g. MOVP3.7, Q
CLRPx.y (x= 0,1,2,3. y = 0 to 7 e.g. CLR P1.0)
SETB Px.y (x= 0,1,2,3. y = 0 to 7 e.g. SETS P3.6)
The instructions MOV Px.y, C, CLR Px.y and SETB Px.y, read the Port x byte (8 bits of the Px latch)
and modify bity and write the new byte back to the Px latch.
The following assembly code snippet illustrates the Read Latch operation.
MOV P0,,#0FHj>
MOV A, #0FH k .*
ANL. PC, A y ; Read P0 latch, 1 Accumulator- y •< * •’
y ; -• y y y ^ y ’ - • . \V ~
; content and load h the result
Executing the instruction MOVP0, WFH loads the Port 0 latch with 0FH (The latches for port pins
PO.O to P0.3 are set). Now Port pins PO.O to P0.3 acts as input pins. Executing the instruction MOV A,
WFH loads the accumulator with 0FH. The ANL P0, A instruction reads the P0 latch and logical AND it
with accumulator and rewrites the P0 latch with the ANDed result. The status of port pins configured as
input port has no effect on the instruction ANL P0, A. Suppose PO.O pin (Not PO.O latch bit) is at logic
0 and pins P0.1 to P0.3 are at logic 1 at the time of executing the instruction ANL P0, A, still Port 0 latch
is loaded with 0FH and not 0EH.
The Read Pin operation reads the status of a port pin when the corresponding port pin is configured
as input pin (When the corresponding port latch bit is loaded with logic 1). The port architecture for all
4 ports contains necessary circuit for reading the Port Pin for all ports. The read Port Pin operation is
triggered by the control signal Read Pin. The Read Pin control signal is generated internally on execut¬
ing an instruction implementing the Read Pin operation. MOV A, Px, MOV C, Px.y are examples for
Read Pin instmctions. The following code snippet illustrates the ‘Read Pin’ operation.
MOV P0, #0.FH. ; Configure P0.C. t.o P0.3 pins as input pins
MOV A, P0 ; Load Accumulator with P0 -Port.pin status
Executing the instruction MOVLPO, WFH loads the. Port 0 latch with 0FH (The latches for port pins PO.O
to P0.3 are set). Now Port pins PO.O to P0.3 act as input pins. Executing the instruction MOV A, P0 loads
accumulator with the Pin status of pins PO.O P0.3. Suppose PO.O pin is at logic 0 and pins P0.1 to P0.3
are at logic 1 at the time of executing the instruction MOV A, P0, the accumulator is loaded with 0EH.
Source Current The term source current refers to how much current the 8051 port pin can supply to
drive an externally comiected device. The device can be an LED, a buzzer or a TTL logic device. For TTL
family of8051 devices the source current is defined in terms of TTL logic. TTL logic has two logic levels
Designing Embedded Systems with 8bit Microcontrollers—8051
namely logic 1 (High) and logic 0 (Low). The typical voltage levels for logic Low and High is given in the
following table.
The logic levels are defined for a TTL gate acting as input and output. For logic 0 the input voltage
level is defined as any voltage below 0.8V and the current is 1.6mA sinking current to ground through
a TTL input. According to the 8051 design reference, the maximum current that a port pin (For an LS
TTL logic based 8051 devices) can source is 60 |iA. •
I
Sink Current It refers to the maximum current that the 8051 port pin can absorb through a device
which is connected to an external supply. The device can be an LED, a buzzer or a TTL logic device
(For TTL logic based 8051 devices). Pins of Ports PI, P2 and P3 can sink a maximum current of 1.6
mA. Port 0 pins can sink currents up to 3.2 mA. Under steady state the maximum sink current is limited
by the criteria: Maximum Sink Current per port pin = 10 mA, Maximum Sink current per 8-bit port for
port 0 = 26 mA, Maximum Sink Current per 8-bit port for port 1,2, & 3 = 15 mA, Maximum total Sink
current for all output pin = 71 mA (As per the AT89C51 Datasheet). Figure 5.14 illustrates the circuits
for source, sink and ideal port interfacing for 8051 port pins.
Figure 5.14(a) illustrates the current sourcing for port pins. Since 8051 port pins are only capable
of sourcing less than 1 mA current, the brightness of LED will be very poor. Figure 5.14(b) illustrates
the current sinking for port’ pins. In this configuration, the forward voltage of LED while conducting is
approximately 2V and the supply voltage 5V (F ) is distributed across the LED and the internal TTL
. circuitry. The extra 3V has to be dropped across the internal TTL circuitry and this will lead to high
power dissipation, which in turn will result in the damage of the LED or the port pin. This type of design
is not recommended in embedded design. Instead the current through the LED is limited by connecting
the LED to the power supply through a resistor as shown in Fig. 5.14(c). In this configuration, the port
pin should be at Logic 0 for the LED to conduct. For a 2.2V LED, the drop across Resistor is calculated
as Supply voltage - (LED Forward Voltage + TTL Low Voltage) = 5 - (2.2 + 0.8) = 2.0V. The Resis¬
tance value is calculated as 2 V / (Required LED Current). Refer LED data sheet for LED current. If the
resistance value is not properly selected, it may lead to the flow of high current through the LED and
may damage the LED.
Introduction to Embedded Systems
Design an 8051 microcontroller based system for displaying the binary numbers from 0 to 255 using 8 LEDs as per the
specifications given below:
1. Use Atmel’s. AT89C51/52 or AT89S8252 (Flash microcontroller with In System Programming (ISP) support) for
designing the system.
2. Use a 12MHz crystal resonator for generating the necessary clock signal for the controller.
3. Use on-chip program memory for storing the program instructions.
4. The 8 LEDs are connected to the port pins P2.0 to P2.7 of the microcontroller and are arranged in a single row with
the L ED connected to P2.0 at the rightmost position (LSB) and the LED connected to P2.7 at the leftmost position
(MSB).
5. The LEDs are connected to the port pins through pull-up resistors of 470 ohms and will conduct only when the
corresponding port pin is at logic 0.
6. Each LED represents the corresponding binary bit of a byte and it reflects the logic levels of the bit through turning
ON and OFF the LED (The LED is turned on when the bit is at logic 1 and off when the LED is at logic 0).
7. The counting starts from 0 (All LEDs at turned OFF state) and increments by one. The counter is incremented at
the rate of 5 seconds.
8. When the counter is at 255 (OFFH, all LEDs are in the turn ON state), the next increment resets the counter to 00H
and the counting process is repeated. '
The design of this system has two parts. The first part is the design of the microcontroller based hardware circuit.
The hardware circuit part can be wired on a breadboard for simplifying the development. The controller for this can be
chosen as either AT89C51/52 or AT89S8252. Both of these controllers are from the 8051 family and are pin compatible.
Both of them contain built in program memory. The only difference is that for programming the AT89C51/52 device an
EEPROM/FLASH programmer device is required whereas AT89S8252 doesn’t require a special programmer. It can be
programmed through the In System Programming (ISP) utility running on the firmware development PC and through the
parallel port of.the PC. The In System Programming technique for AT89S8252 is described in OLC. For the controller to
work, a regulated 5V dc supply is required. For generating a regulated 5 V dc supply, a regulator IC is used. JEor the current
design the regulator IC LM7805 from National semiconductor is selected. The input voltage required for this regulator
IC is in the range of 9V to 12V dc. A wall mounted dc adaptor with ratings 9V or 12V, 250mA can be used for supply¬
ing the input power. It is better to use a 9V dc adaptor to avoid the excessive heating of the regulator IC. Excessive heat
production in the regulator IC leads to the requirement for heat sinks. The circuit details and the components required to
implement the counter is shown in Fig. 5.15.
The circuit shows the minimal components and the interconnection among them to make the controller operational.
As mentioned earlier, it requires a regulated 5V dc supply for powering the controller. The 12 MHZ crystal resonator in
combination with the external 22 picofarad (pF) capacitors drives the on-chip oscillator unit and generates the required
clock signal for the controller. The RC circuit connected to the RST pin of the controller provides Power-On reset for
the controller. The capacitor and resistor values are selected in such a way that the reset pulse is active (high) for at least
2 machine cycle duration. The diode in the reset circuitry is u^ed ad freewheeling diode and it is not mandatory. The 0.1
Microfarad (0.1 MFD) capacitor connected to the power supply line filters the spurious (noise) signals from the power
supply line. For proper driving, the LEDs should be connected to the respective port pins through pull-up resistors. The
pull-up resistor values are determined by the forward voltage of LEDs and the current rating of the LEDs. The current
design uses 470 ohms as the pull up resistor. If you are not sure about the forward voltage and current ratings of the LED,
it is better to start with a high value (say 8.2K) for the resistor and replace it with successive low value resistors (4.2 K,
2.7K, 1 K, 870 E, 470 E etc.) till you feel that the brightness of the LED while it is conducting is reasonably good. The
controller contains on-chip program memory and it can be used for storing the firmware. In order to use the on-chip pro¬
gram memory, the EA\ pin should be tied to Vcc Pulling the EA\ pin to V(X through a high value resistor ensures that the
pin draws very minimal amount of current.
The second part is the design and development of program code (firmware) for implementing the binary counter and
displaying the counter content using the LEDs interfaced to Port 2. The firmware requirement can be modelled in the
form of a flow chart as given in Fig. 5.16.
Introduction to Embedded Systems
Once the firmware requirements are modelled using the flow chart, the next step is implementing it in either proces¬
sor specific assembly code or high level language. For the time being let us implement the requirements in 8051 specific
assembly language. The firmware for implementing the binary counter in 8051 assembly language is given below.
;#########################,###########################################
;Binary_Counter.src
;Source code for implementing a binary counter and displaying the
; count through the L'EDs connected to P2.0 to P2.7 port pins
/The LED is turned on when the port line is at logic 0
;The counter value should be complemented to display the count
;using the LEDs connected at Port 2. Written by Shibu K V
/ Copyright ,<C) 2008 i « *£>.> y£- r •' , V" '
; # # # ##■#.# # # #:#■# ###### # # # #.## # # # # # #’# # # ###### # # # #■# # # # ######## #.-# ###########
ORG 0000H ; ; - .: ; Reset vector
. ,JMP 0050H'- 1 • / Jump to code, mem location 0050H
ORG 00G3K : /..External Interrupt- 0 I SR location
RETI : c Simply return. Do nothing
ORG OOOB.H ; Timer 0 Interrupt ISR location
RETI | ; Simply return. Do nothing
ORG 0013H / External Interrupt 1 ISR location
RETI / Simply return. Do nothing
ORG 001BH / Timer 1 Interrupt ISR location
RETI - - / Simply return. Do...nothing
ORG 0023H / Serial Interrupt.-ISR location
RETI / Simply return.' Do nothing
ORG 0050H ; Start of Program Execution
MOV P2., #0FFH / Turn off all LEDs
CLR EA / Disable All "interrupts ' " *-
MOV SP, ,#08H / Set stack at memory location 08H
MOV R7,#OOH / Set counter Register R7 to zero.
REPEAT: CALL DELAY Z Wait for 5 seconds
INC R7 / Increment binary counter
MOV A, R.7 / -
CPL A; The LED's are turned on when corresponding bit is 0
MOV P2,' A. / Display the count on LEDs connected at Port 2
JMP REPEAT / Repeat counting
/#*#############################################.#####################
/Routine for generating 5 seconds delay
/Delay generation is dependent on clock frequency
/This routine assumes a clock frequency of 12.00MHZ
/LOOP1 generates 248 x 2 Machine cycles (496microseconds) delay
/LOOP2 generates 200 x (496+2+1) Machine cycles (99800microseconds)
/delay. LOOP3 generate 50 x (99800+2+1) Machine cycles
/(4990150microseconds) delay. The routine generates a-
/precise delay of 4.99 seconds
;#######«##############.############«###############################
igna
Once the assembly code is written and checked for syntax errors, it is converted into a controller specific machine
code (hex file) using an assembler program. The conversion can be done using a freely/commercially available assembler
program for 8051 or an IDE based tool (like Keil microvison 3). The final stage is embedding the hex code in the program
memory of the controller. If the controller used is AT89C51, the program can be embedded using a FLASH programmer
device. For controllers supporting In System Programming (ISP), like AT89S8252, the hex file can be directly loaded into
the program memory of the controller using an ISP application running on the development PC.
Design an 8051 microcontroller based control system for controlling a 5V, 2-phase 6-wire stepper motor. The system
should satisfy the following:
1. Use Atmel’s AT89C51/52 or AT89S8252 (Flash microcontroller with In System Programming (ISP) support) for
designing the system.
2. Use a 12 MHz crystal resonator for generating the necessary clock signal for the controller.
3. Use on-chip program memory for storing the program instructions.
4. The wiresof the stepper motor are marked corresponding to the coils (A, B, C & D) and Ground (2 wires)
5. Use the octal peripheral driver IC ULN2803 from National semiconductors for driving the stepper motor.
6. Step the motor in ‘Full step’ mode with a delay of 1 sec between the steps.
7. Connect the coil drives to Port 1 in the order Coil A to P1.0, Coil B to Pl.l, Coil C to PI.2, and Coil D to PI.3
Refer to the description on stepper motors given in Chapter 2 to get an understanding of unipolar stepper motors and the
coil energising sequence for ‘Full step’ mode. ;
Figure 5.17 illustrates the interfacing of stepper motor through the driver circuit connected to Port 1 of 8051.
The flow chart given in Fig. 5.18 models the firmware requirements for interfacing the stepper motor.
From the pulse sequence for running the stepper motor in ‘Full step’ it is clear that the pulse sequence for next step
is obtained by right shifting the current pulse sequence. The initial pulse sequence required is H, H, L, L at coils A, B,
C & D respectively (Please refer to the stepper motor section in Chapter 2). In our case we have only 4 bits to shift and
our controller is an 8bit controller. Performing a right shift operation of the accumulator moves the LS bit of accumulator
to the MS bit position (Bit position 7 in 0-7 numbering). We want the LS bit to be available at 3rd bit position after each
rotation. This can be achieved by some bit manipulation operation. We can also achieve it by loading the MS nibble of ac¬
cumulator with the same initial sequence HULL. In this example we are not using Port PI for any other operation. Hence
the values of port pins P1.4 to P1.7 are irrelevant in our case. But in real life scenario it may not be the case always. The
firmware implementation for this is given below:
1MFD
He—>
__
10 COM ;
Jlf* OUT1 ■
.m SrMmTfrrfrt
' ,\ *H
V’:
„H'"H HH
W4 ULN2803
-e. ■
1 IN 1. ‘ H* j „
9 0ND , .
. 12MHz
;####################################################################
5 ;Stepper_motor.src. Firmware for Interfacing stepper motor
l ;The stator coils A, B, C and. Dare.controlled through Port pins P1.0,
: ;P1.1, Pi.2 and PI.3 respectively.
i, Accumulator -is used for generating the pulse sequence for 'Fullstep'
/The initial pulse sequence is represented by OCH
.''Written & Compiled for.ASl Assembler. Written by Shibu K. V
-Copyright (C) 2008
;#########################*#######-#f######## #■#### # f ##.###'##«#######’##
ORG. .00001: , , ; Reset vector -f ,, ■■ ? - - , .-.l •
JMP 010OH ; Jump to code mem location 0100H to start-
1,,. ■ , , ; execution , •.
ORG CCG3K ■. / External 'Interrupt 0 ISR .location .. . ;
RETI ; Simply return. . Do nothing ,
ORG 0CCBH . ; Timer 0 Interrupt. ISR location 1
' . RETI Simply return. ..Do nothing.. ■.
ORG 0013H .; External Interrupt 1 ISR location
; RETI ; Simply return. Do nothing
ORG-OOlBH , ; Timer 1 Interrupt ISR location
RETI ; Simply return. Do nothing
' .ORG 0023H , ; Serial Interrupt ISR location
RETI ; Simply return. Do nothing
; ##.###########################################.,:
; Start of main Program . ' 'f
Design an 8051 microcontroller based system for interfacing the Programmable Peripheral Interface (PPI) device 8255.
The system should satisfy the following:
1. Use Atmel’s AT89C51/52 or AT89S8252 (Flash microcontroller with In System Programming (ISP) support) for
designing the system
2. Use a 12 MHz crystal resonator for generating the necessary clock signal for the controller. Use on-chip program
memory for storing the program instructions
3. Use Intersil Corporation’s ('wwww.intersil.com') 82C55APPI device
4. Allocate the address space 8000H to FFFFH to 8255. Initialise the Port A, Port B and Port C of 8255 as Output ports
in ModeO
Here we are allocating the addressjpace 8000H to FFFFH to 8255. Hence the 8255 is activated when the’ 15th bit of
address line-becomes 1. Here we have to use a single NOT gate to invert the A15 line before applying it to the Chip Select
1 (CSV) line of 8255. In this configuration 8255 requires only four address space namely 8000H for Port A, 8001H for Port
B, 8002H for Port C and 8003H for the Control Register. Rest of the address space 8004H to FFFFH is left unused. Here
we have the luxury of using the entire address range since we don’t have any other devices to connect. In real life appli¬
cations it may not be the case. We may have multiple devices sharing the entire address space OOOOH to FFFFH and we
need to select each device in their own address space. In such scenarios the address lines A2 to A15 needs to be decoded
using a combination of logic gates (NAND, AND, NOT, OR, NOR) and decoders. Figure 5.19 illustrates the interfacing
of 8255 with 8051.
Designing Embedded Systems with 8bit Microcontrollers—8051
The Octal latch 74LS373 latches the lower order address bus (A0-A7) which is multiplexed with the data bus (DO¬
DD. A3 to 8 decoder chip, 74LS138, decodes the address bus to generate the Chip Select (CS\) signal for 8255. Here we
have only one address line (A15) to decode. The rest of the 2 input lines to the decoder (Pins 2 & 3) are grounded. Our
intention is to assert the CS\ signal of 8255 when A15 line is 1. The I/p condition corresponds to this is 001. The decoded
output for this is Output 1 (Yl). You can replace the decoder with a NOT Logic IC. The reset line of 8255 is controlled
through port pin P1.0. The Reset of 8255 is active high and when 8051 is initialised, the port pin P1.0 automatically
generates a reset high signal for 8255.
The flow chart given in Fig. 5.20 models the firmware requirements for interfacing 8255 with 8051 controller.
c Start
3
1. Reset P1.0 to hold the RESET line of 8255 inactive
2. Initialise Stack Pointer
3. Load Accumulator with the control word for configuring
8255 Ports
4. Load DPTR with the address-of 8255 control register (8003H)
5. Write the control word to 8255 Control Register
[fig. S.20)
c
Flow chart for Interfacing 8255 with 8051
End
3
The control word for configuring all the 8255 ports as output ports in mode 0 is shown below. Please refer to the
section on Programmable Peripheral Interface (PPI) given in Chapter 2 for more details on 8255’s control register.
& pg --.-r."gg
i o o o o o ' % r 0? o
The firmware implementation for this is given below.
5.3.6 Interrupts
Before going to the details of 8051 interrupt system, let us have a look at interrupts in general and how
interrupts work in microprocessor/controller systems.
5.3.6.1 What is Interrupt? As the name indicates, interrupt is something that produces some kind
of interruption. In microprocessor and microcontroller systems, an interrupt is defined as a signal that
initiates changes in normal program execution flow. The signal that generates changes in normal pro¬
gram execution flow may come from an external device connected to the microprocessor/controller,
requesting the system that it needs immediate attention or the interrupt signal may come from some of
the internal units of the processor/controller such as timer overflow indication signal. The first type of
interrupt signals are referred as external interrupts.
5.3.6.2 Why Interrupts? From a programmer point of view interrupt is a boon. Interrupts are very
useful in situations where you need to read or write some data from or to an externally comiected device.
Without interrupts, the normal procedure adopted is polling the device to get the status. You can write
your program in two ways to poll the device. In the first method your program polls the device continu¬
ously till the device is ready to send data to the controller or ready to accept data from tjie controller.
This technique achieves the desired objective effectively by sacrificing the processor time for that single
task. Also there is a chance for the program hang up and the total system to crash in certain situations
where the external device fails or stops functioning. Another approach for implementing the polling
technique is to schedule the polling operation on a time slice basis and allocate the total time on a shared
basis to rest of the tasks also. This leads to much more effective utilisation of the processor time. The
' biggest drawback of this approach is that there is a chance for missing some information coming from
the device if the total tasks are high in number. Your device polling will get another chance to poll the
device only after the other tasks are done at least once.
Here comes the role of interrupts. If the external device supports interrupt, you can connect the inter¬
rupt pin of the device to the interrupt line of the controller. Enable the corresponding interrupt in firm¬
ware. Write the code to handle the interrupt request service in a separate function and put the other tasks
in the main program code. Here the main program is executed normally and when the external device
asserts an interrupt, the main program is interrupted and the processor switches the program execution
to the interrupt request service. On finishing the execution of the interrupt request service, the program
flow is automatically diverted back to the main stream and the main program resumes its execution
exactly from the pbint where it got interrupted.
Designing Embedded Systems with 8bit Microcontrollers—8051
5.3.6.3 Use of Interrupts In any interrupt based systems, interrupts are mainly used for accom¬
plishing the following tasks:
1. I/O data transfer between peripheral devices and processor/controller
2. Timing applications
3. Handling emergency situations (e.g. switch off the system when the battery status falls below the
critical limit in battery operated systems)
4. Context switching/Multitasking/Real-Time application programming
5. Event driven programming
5.3.7.1 Enabling Interrupts The interrupt system of 8051 can be enabled or disabled totally under
software control. This is achieved by setting or clearing the global interrupt enable bit of the Special
Function Register Interrupt Enable (IE). Also, each interrupt can be enabled or disabled individually by
setting or clearing the corresponding interrupt enable bit in the SFR Interrupt Enable.
Interrupt Enable (IE) (SFR- A8H) The bit details of the Interrupt Enable Register is given below:
-EX1
The table given below explains the meaning and use of each bit.
_.Nam« - - Description -M ;■ A :
EA Enable All F,A “ 0 disable all interrupts. HA - 1 enable all interrupts, which are:individually
; ; , enabled by setting their corresponding enable bit in Interrupt Enable SFR.
RSD Reserved Unimplemcnted. .Reserved for future use ' • : v
ES ■ Enable Serial ■ ES -1 enables Serial Interrupt. F.S = 0 disables it
The following code snippet illustrates the enabling of Timer 0 interrupt and disabling of all other
interrupts.
ORL IE, #1000.001033 ; Set bits EA & ETO. Preserve other
; bits as such
ANL IE, #111000I0B ; Reset bits ES, ET1, EX1 and EXO.
; Preserve other bits as such
Introduction to Embedded Systems
The instruction ORL IE, #100000108 sets the global interrupt enabler bit EA(IE.7) and the Timer 0
Interrupt enabler bit ETO (IE. 1). The status of all other bits in the IE register is preserved. The instruc¬
tion ANL IE, #11100010B preserves the status of the global interrupt enabler bit EA (IE. 7), the RSI) (IE. 6
& IE. 5) bits and the Timer 0 Interrupt enabler bit ETO (IE.1) and resets the Serial interrupt enabler bit
ES(IE.4), Timer 1 Interrupt enabler bit ET1 (IE.3), External Interrupt 1 enabler bit EX1 (IE.2) and Ex¬
ternal Interrupt 0 enabler bit EXO (IE.0). This ensures that the reserved bits USD (IE.6 & IE.5) are left
untouched. The same can also be achieved by individually setting or clearing the corresponding bits of
IE register using SETB and CLR instructions. This requires more number of instructions to achieve the
result.
Note: Though the corresponding interrupt bits are ‘set’ in the IE register, the Interrupts will not be en¬
abled and serviced if the global interrupt enabler, EA bit of the Interrupt Enable Register (IE) is 0.
5.3.7.2 Setting Interrupt Priorities In a Real World application, interrupts can occur at any time
(asynchronous behaviour) and different interrupts may occur simultaneously. This may confuse the pro¬
cessor on deciding which interrupt is to be serviced first. This arbitration problem is resolved by setting
interrupt priorities. Interrupt priority is configured under software control. The Special Function Regis¬
ter Interrupt Priority (IP) Register is the one holding the interrupt priority settings for each interrupt.
Interrupt Priority Register (IP) (SFR-B8H) The bit details of the Interrupt Priority Register is ex¬
plained in the table below.
IR3
The table given below explains the meaning and use of each bit in the IP register.
The interrupt control system of 8051 contains latches to hold the status of each interrupt. The status
of each interrupt flags are latched and updated during S5P2 of every machine cycle (Refer to machine
cycles for information on S5P2). The latched samples are polled during the following machine cycle.
If the flag for an enabled interrupt is found to be set in S5P2 of the previous cycle, the interrupt system
transfers the program flow to the corresponding interrupt’s service routine in the code memory (Pro¬
vided none of the conditions described in section “Different cond itions blocking an interrupt” blocks the
vectoring of the interrupt). The Interrupt Service Routine address for each interrupt in the code memory
is listed below.
It is to be noted that each Interrupt Service Routine (ISR) is allocated 8 bytes of code memory in the
code memory space.
From the IP Register architecture it is obvious that each interrupt can be individually programmed to
one of two priority levels by setting or clearing the corresponding priority bit in the Interrupt Priority
Register. Some general info on 8051 interrupts is given below: ■
1. If two interrupt requests of different priority levels are received simultaneously, the request of
higher priority interrupt is serviced.
2. If interrupt requests of the same priority level are received simultaneously, the order in which the
interrupt flags are polled internally is served first. First polled first served. (Also known as internal
polling sequence.)
3. A low-priority interrupt can always be interrupted by a high priority interrupt.
4. A low-priority interrupt in progress can never be interrupted by another low priority interrupt.
5. A high priority interrupt in progress cannot be interrupted by a low priority interrupt or an
interrupt of equal priority.
5.3.7.3 Different conditions blocking an Interrupt It is not necessary that an interrupt should
be serviced immediately on request. The following situations can block an interrupt request or delay the
servicing of an interrupt request in 8051 architecture.
1. All interrupts are globally disabled by clearing the Enable All (EA) bit of Interrupt Enable regis¬
ter.
2. The interrupt is individually disabled by clearing its corresponding enable bit in the Interrupt
Enable Register (IE).
3. An interrupt of higher priority or equal priority is already in progress.
4. The current polling machine cycle is not the final cycle in the execution of the instruction in
progress (To ensure that the instruction in progress will be completed before vectoring to the inter¬
rupt service routine. In this state the LCALL generation to the ISR location is postponed till the
completion of the current instruction).
5. The instruction in execution is RETI or a write to the IE/IP Register. (Ensures the interrupt related
instructions will not make any conflicts).
In the first three conditions the interrupt is not serviced at all whereas conditions 4 and 5 services the
interrupt request with a delay.
•
5.3.7.4 Returning from an Interrupt Service Routine An Interrupt Service Routine should end
with an RETI instruction as the last executable instruction for the corresponding ISR. Executing the
RETI instruction informs the interrupt system that the service routine for the corresponding interrupt
is finished and it clears the corresponding priority-X (X=l High priority) interrupt in progress flag by
clearing the corresponding flip flop. This enables the system to accept any interrupts with low priority
or equal priority of the interrupt which was just serviced. Remember an interrupt of higher priority can
always interrupt a lower priority even if the corresponding priority’s interrupt in progress flag is set.
Executing the RETI instruction POPs (retrieves) the Program Counter (PC) content from stack and the
program flow is brought back to the point where the interruption occurred.
Introduction to Embedded Systems
In operation, RETI is similar to RET where RETI indicates return from an Interrupt Service Routine
and RET indicates return from a normal routine. RETI clears the interrupt in progress flag as well as
POPs (retrieves) the content of the Program Counter register to bring the program flow back to the point
where it got interrupted. RET instruction only POPs the content of the Program Counter register and
brings the program flow back to the point where the interruption occurred.
Will a non serviced Interrupt be serviced later? This is a genuine doubt raised by beginners in
the 8051 based system design. The answer is lNo\ Each interrupt polling sequence is a new one and it
happens at S5P2 of each machine cycle. If an interrupt is not serviced in a machine cycle for the reason
that it occurred simultaneously with another high priority interrupt, will be lost. There is no mechanism
in place for holding the non serviced interrupts in queue and marking them as pending interrupts and
servicing them later.
5.3.7.5 Priority Levels for 8051 Interrupts By default the 8051 architecture supports two levels
of priority which is already explained in the previous sections. The first priority level is determined by
the settings of the Interrupt Priority (IP) register. The second level is determined by the internal hard¬
ware polling sequence. The internal polling sequence based priority determination comes into action if
two or more interrupt requests of equal priority occurs simultaneously. The internal polling based prior¬
ity within the same level of priority is listed below in the descending order of priority.
V jffftna- iiit-fltiffit 0
5.3.7.6 What Happens when an Interrupt Occurs? On identifying the interrupt request num¬
ber, the following actions are generated by the processor:
1. Complete the execution of instruction in progress.
2. The Program Counter (PC) content which is the address of the next instruction in code memory
which will be executed in normal program flow is pushed automatically to The stack. Program
Counter Low byte (PCL) is pushed first and Program Counter High (PCH) byte is pushed next.
3. Clear the corresponding interrupt flags if the interrupt is a timer or external interrupt (only for
transition activated (edge triggered) configuration).
4. Set interrupt in progress flip flop.
5. Generate a long call (LCALL) to the corresponding Interrupt Service Routine address in the code
memory (Known as vectoring of interrupt).
5.3.7.7 Interrupt Latency In the 8051 architecture, the interrupt flags are sampled and latched at
S5P2 of each machine cycle. The latched samples are polled during S5P2 of the following machine
cycle to find out their state. If the polling process identifies a priority interrupt flag’s flip flop as set,
an LCALL to its ISR location is generated (If and only if none of the conditions listed under the topic
“Different conditions blocking an interrupt” blocks it). Interrupt latency is the time elapsed between
the assertion of the interrupt and the start of the ISR for the same.
Designing Embedded Systems with 8bit Microcontrollers—8051
Interrupt latency is highly significant in real-time applications and is very crucial in time-critical
applications. Interrupt latency can happen due to various reasons. For external interrupts there is no
synchronisation with the system (asynchronous in behaviqur) and so it can occur at any point of time.
But the processor latches each interrupt flag only at S5P2 of each machine cycle. So there is no point
even if the interrupt occurs at SIP 1 of the machine cycle. It is latched only at S5P2 of the current ma¬
chine cycle and the latched interrupts flags are polled at S5P2 of the following machine cycle and an
LCALL is generated to the corresponding ISR, if no other conditions block the call. So this delay itself
contributes a significant part in interrupt latency. Even if the ISR is entered some delay can be happened
by the number of register contents to be stored (PUSH instructions) and other actions to be taken before
executing the ISR task. The interrupt latency part which contributes the delay in servicing the ISR is the
sum of the following time delays.' ;
Time between the interrupt assertion to the start of state S5 of current machine cycle (polling cycle)
+ (Time for S5 & S6) + Remaining machine cycles for the current instruction in execution (The current
execution should not be RETI or instructions accessing registers IE or IP, if so there will be an additional
delay of the execution time for the next instruction) + LCALL generation time. The LCALL generation
time is 12 T States (2 Machine cycles).
If the current machine cycle (the polling cycle) is the last cycle of the current instruction in execution'
and the current instruction in execution is other than RETI or instruction accessing IE or IP register, the
interrupt vectoring happens at the shortest possible time and it is given as
Time between the interrupt asserted to start of state S5 of current machine cycle (polling cycle)
+ (S5+S6 T state + 12 clock)
= Time between the Interrupt Asserted to the start of state S5 + (((1+1+12) x 2)/f ) seconds.
(1 T state - 2 clock cycles and 1 clock cycle = 1/f )
The minimum time required to identify an interrupt by the system is one machine cycle, i.e. if an
interrupt occurs at S5 of a machine cycle, it is latched and it is identified as an interrupt at state S5
of next machine cycle (Polling cycle). Hence the minimum time from interrupt assertion to S5 of the
polling machine cycle is 1 machine cycle (12 clock periods). The maximum time required to identify
an interrupt by the system is approximately 2 machine cycles. Assume the interrupt is asserted at state
S6 of a machine cycle, it is latched at S5 of next machine cycle and the latched value is polled at S5 of
next machine cycle. Hence the minimum time between the ‘Interrupt Asserted’ to state S5 of the current
machine cycle (polling cycle) is 6 T States (1 machine cycle). That means State S6 of previous machine
cycle to state S5 of current machine cycle. Hence the minimum acknowledgement time will be 20 T
States (Since 1 machine cycle = 6 T states. The approximate response delay will be 3 machine cycles).
3 machine cycles is the minimum response delay for acknowledging an interrupt in a single inter¬
rupt system. There may have additional wait times which come as an addition to the minimum response
Introduction to Embedded Systems
delay, depending on some other conditions. If you look back to the section “Different conditions block¬
ing an Interrupt ’ you can see that if the current machine cycle when the interrupt asserted (The machine
cycle at which the interrupt is latched) is the last machine cycle of the current instruction in progress, the
interrupt vectoring wilkhappen only after completing the next instruction. If the instruction in progress
is not in its final machine cycle, the maximum additional waiting (waiting time excluding the LCALL
generation time) time required to vector the Interrupt cannot be more than 3 machine cycles since the
longest instmctions MUL AB and DIV AB are 4 cycle instructions. If the instruction in progress is a
RETI instruction or any access to IP or IE register then also the vectoring of the interrupt service routine
will be delayed till the execution of next instruction. If the next instmction following the RETI or IP/IE
register related instruction is MUL AB or DIV AB* the additional wait time will be 5 machine cycles
(RETI and IP/IE related instructions are 2 machine cycle instructions).
In brief, the response time for interrupt in 8051 system is always more than 3 machine cycles and less
than 9 machine cycles.
5.3.7.8 Configuring External Interrupts 8051 supports two external interrupts, namely. Exter¬
nal interrupt 0 and External interrupt 1. These are hardware interrupts. Two port pins of Port 3 serve the
purpose of external interrupt input line. External interrupts are usually used for connecting peripheral
devices. The external interrupt assertion can be either level triggered or edge triggered depending on
the external device connected to the interrupt line. From the 8051 side it is configurable and the con¬
figuration is done at the SFR Timer/Counter Control Register (TCON). Bits TCON.O and TCON.2 of
TCON register configures the same for External Interrupt 0 and 1 respectively. TCON.O is also known
as Interrupt 0 type control bit (ITO). Setting this bit to 1 configures the external interrupt 0 as falling edge
triggered. Clearing the bit configures the external interrupt 0 as low level triggered. Similarly, TCON.2
is known as Interrupt 1 type control bit (IT1). Setting this bit to 1 configures the external interrupt 1 as
falling edge triggered. Clearing this^bit configures the external interrupt 1 as low level triggered.
For external interrupts, the interrupt line should be asserted by the externally connected device to a
minimum time period of the interval between two consecutive latching, i.e. S6P1 of previous machine
cycle to S5P2 of current machine cycle (1 Machine cycle). Otherwise it may not be identified by the pro¬
cessor (Only interrupts which are found asserted during the latching time (S5P2) will be identified).
5.3.7.9 Single Stepping Program with the Help of External Interrupts Single stepping is
the process of executing the program instmction by instmction. This can be achieved with the help of
the external interrupt 0 or 1 and a small firmware modification. This works on the basic principle that
an interrupt request will not be acknowledged if an interrupt of equal priority is in progress and if the
instmction in progress when the interrupt is asserted is a RETI instmction, the vectoring will happen
•only after executing an instmction from the main program, which is interrupted by the interrupt. If the
external interrupt is in the asserted state, the interrupt vectoring happens after executing one instmction
from the main program. This execution switching between the main program and ISR can be achieved
by a simple ISR firmware modification. Connecting a push button switch to the external interrupt line
0 through a resistor is the only hardware change needed for a single step operation (Fig. 5.22). Config¬
ure INTO as level triggered in firmware. Activating the push button asserts the INTO interrupt and the
processor starts executing the ISR for interrupt 0. At the end, the ISR waits for a 1 to 0 transition at the
INTO line that asserts the external interrupt 0 again. The next instmction that is going to be executed on
asserting the INTO is RETI and according to the general interrupt vectoring principle the processor will
go back to the point in the main code where it got interrupted and after completing one instmction it will
again come back to the INTO ISR. This process is repeated.
Designing Embedded Systems with 8bit Microcontrollers—8051
Design an 8051 microcontroller based data acquisition system for interfacing the Analog to Digital Converter ADC,
ADC0801 from National semiconductors. The system should satisfy the following:
1. Use Atmel’s AT89C51/52 or AT89S8252 (Flash microcontroller with In System Programming (ISP) support) for
designing the system. Use a 12MHZ crystal resonator for generating the necessary clock signal for the controller.
Use on-chip program memory for storing the program instructions.
2. The data lines of the ADC is interfaced to Port 2 of the microcontroller. The control signals (RD\, WR\, CSV) to the
ADC is supplied through Port 3 pins of microcontroller.
3. The Analog voltage range for conversion is from OV to 5. The varying analog input voltage is generated from the
5 V supply voltage (Vcc) using a variable resistor (Potentiometer). The ADC asserts its interrupt line to interrupt the
microcontroller when the AD conversion is over and data -is available at the ADC data port.
4. The microcontroller reads the data on receiving the interrupt and stores it in the memory. The successive data con¬
version is initiated after reading the converted data for a sample. Only the most recent 16 samples are stored in the
microcontroller memory.
This example is a good starting point for a discussion on data converters and their use in embedded applications. The
processing/controlling unit (The core of the embedded system) of every embedded system is made up of digital systems
and they work only on digital data. The processor/controller is capable of dealing with binary (digital) data only. As men¬
tioned in the beginning, embedded systems are in constant interaction with the real world through sensors and actuators.
In most of the situations, the signals which are handled by embedded systems fall into the category ‘analog’. Analog
signals are continuous in nature. Most of the embedded systems used in control and instrumentation applications include
the monitoring or control of at least one analog signal. The thermocouple-based temperature sensing system used in in¬
dustrial control is a typical example for this. The thermocouple generates millivoltage (mV) in response to the changes in
temperature. This voltage signals are filtered and signal conditioned and then applied to the monitoring system for moni¬
toring and control purpose. Digital systems are not capable of handling analog signals as such for processing. The analog
input signal from sensors needs to be digitized (quantized) before inputting to the digital systems. In a similar fashion, the
output from digital systems are digital (discrete) in nature and they cannot be applied directly to analog systems which
require continuous signals for their operation. The problem of handling the analog and digital data in embedded systems
is handled by data converters. Data converters fall into two categories, namely; Analog to Digital Converters (ADC)
and Digital to Analog Converters (DAC). Analog to Digital Converter (ADC) converts analog signals to corresponding
digital representation, whereas Digital to Analog Converter (DAC) converts digital signals to the corresponding analog
representation.
ADCs are used at the input stage of the processor/controller and DACs are used at the output stage of the processor/
controller. ADCs and DACs are available in the form of Integrated Circuits (ICs) from different manufacturers. These
chips are used for interfacing the processor/controller with sensors/actuators which produces/requires analog signals.
Analog to digital conversion can be accomplished through different conversion techniques. Successive approxima¬
tion and integrator are the two commonly adopted analog to digital conversion techniques. In the successive approxi-
Introduction to Embedded Systems
mation technique, the input analog signal is compared against a reference voltage. The reference voltage is adjusted till
it matches the input analog voltage. The integrator technique changes the input voltage to time and compares the new
parameters with known values. Discussing each technique in detail is out of the scope of this book (The current scope is
limited to how data converters are used in embedded system designs) and readers are advised to refer books dedicated on
digital systems/data acquisition systems. The successive approximation technique is faster in data conversion and at the
same time less accurate, whereas, integrator technique is highly accurate but the conversion speed is slow compared to
successive approximation technique. The successive approximation ADCs are used in embedded systems like control and
instrumentation systems, which demands high conversion speed. The Integrator type ADCs are used in systems where the
accuracy of conversion required is high. A typical example is a digital multimeter.
ADCs convert analog voltages in a given range to a binary data within the range supported by the ADC register. For
example, for an Sbit ADC, the voltage range 0 to 5 V is represented with binary data 00H to FFH. The voltage range (5-0)
is split across the 256 (0 to 255) steps. Hence the resolution of the ADC is represented as 1/256, meaning the binary data
is incremented by one for a rise in 1/256 V in the input voltage. The resolution offered by the ADC depends on the input
voltage range and the register width of the ADC. ADC can be used for the conversion of +ve as well as -ve voltage and
range of I/p with offset from 0. It is the responsibility of the firmware developer to interpret all these conditions based on
the system design. For example, if the input voltage is in the range 1 to 5V, the ADC represents 1 as 00H and 5 as FFH.
The firmware developer should handle this appropriately.
The IC ADC0801ADC from National Semiconductor is an example for successive approximation ADC. The
ADC0801 is designed in such a way that its tri-stated output latches can directly drive the processor/controller data bus/
port. ADC0801 appears as a memory location or I/O port to the processor/controller and it doesn’t require any additional
bus interface logic. The logic inputs and outputs of ADC0801 meets both TTL and CMOS voltage level specifications and
it can be used with processors/controllers from the CMOS/TTL logic family without using any interface conversion logic.
ADC0801 supports input voltage range from 0 to 5V and two inputs, V (+) and VIN(-) for differential analog voltages.
The input signal is applied to VIN(+) and V (-) is grounded for converting single ended positive voltages. For single
ended negative voltage conversion, the input voltage is applied to V;N(-) and VJN(+) is grounded. ADC0801 provides an
option for adjusting the span (range) of input voltage for conversion. The span adjustment is achieved by applying a volt¬
age, which is half of the required span, at the VREp/2 (Pin NO. 9) of ADC0801. For example, if the required span is 3 V,
apply a voltage 1.5V at pin V^p/2. This converts the input voltage range 0V to 3V to digital data 00H to FFH. Keeping
the VREp./2 pin unconnected sets the span to 5V (The input varies from 0V to 5 V). If the span is less than 5 and the range
of input signal starts with an offset from 0V, the same can be achieved by applying corresponding voltages at pins VRpp/2
and V (-). As an example consider the requirement where, the span is 3V and range is from 0.5V to 3.5V, a voltage
of 1,5V is applied to Pin VREf/2 and 0.5V to pin VIN(-).The AD converter requires a clock signal for its operation. The
minimal hardware connection required to build the AD converter system is shown in Fig. 5.23.
The AD converter contains an internal clock generator circuit. For putting the chip into operation, either an ex¬
ternal clock or the built in clock generator can be used. ADC0801 provides a pin, CLK IN (Pin No. 4), for connect¬
ing external clock signal. The external clock signals can be generated using an oscillator circuit or the clock signal
available from the CLK OUT pin of the microcontroller can be applied to the CLK IN. The internal clock generator
circuit of the ADC can be used for generating the necessary clock signal to the system by connecting an external resis¬
tor and capacitor across the pins CLK IN and CLK R as shown in the schematic. The CLK IN pin is internally con¬
nected to the input pin of a Schmitt trigger and CLK R is connected to the o/p pin of the Schmitt triggers as shown
in Fig. 5.24.
The frequency of the circuit is represented as fCLK = 1/1.1 RC. The frequency range for ADC0801 is in the range
100 kHz to 800 kHz
ADC0801 requires three necessary control signals namely; RD\, WR\, CS\ for its operation. The CS\ signal is used for
activating the ADC chip. For starting a conversion, the CS\ and WR\ signals should be asserted low. The internal Succes¬
sive Approximation Register (SAR) of the chip is reset and the output data lines of the ADC enter high impedance state
on asserting the WR\ signal. The data conversion begins when the WR\ signal makes a transition from asserted state to
high. The ADC asserts the line INTR\ on completion of the conversion. This signal generates an interrupt at the processor.
The converted digital data is available on the data bus of the ADC from the moment when the INTR\ line is asserted low.
The INTR\ signal is reset when the RD\ signal is asserted low by the processor.
Designing Embedded Systems with 8bit Microcontrollers—8051
POT
10K
clk!r
In the current design, a potentiometer is used for varying the voltage from OV to 5V. The data lines of the ADC are
interfaced to Port 2 of the controller. The control signals to the ADC are supplied through the pins of Port 3. Port Pin P3.3
supplies the Chip Select (CSV) signal and Port Pin P3.0 supplies the RD\ Signal to the ADC. The WR\ signal to ADC is
Introduction to Embedded Systems
supplied through Port Pin P3.1. The 1NTR\ signal Line of ADC is interfaced to the External Interrupt 0 (INTOY) line (Port
Pin P3.2) of 8051. The flow chart given in Fig. 5.25 illustrates the firmware design for the system.
\_
ADC main routine
start
_ J
r~ External interrupt 0
routine
J
1. Initialise Port 2 as I/p port
2. Initialise stack pointer
3. Disable all Interrupts (Optional) 1. Read the data from ADC
2. Store the read data in memory location
3. Reset the ADC interrupt
4. Deselect the ADC chip
1. Select the ADC chip 5. Decrement the counter and reset the
2. Clear the interrupt line of ADC memory location to store the next sample
3. De-select ADC chip to the start address of storage memory if
4. Set the counter for sample counting the counter value is zero
5. Select the start address of storage 6. If the decremented counter value is non¬
memory for storing the first sample zero increment the memory location to
store the next sample
The firmware for interfacing the ADC in 8051 Assembly language is given below.
The following table explains the meaning and use of each bit in the TMOD register.
or I Counter or timer mode select bit. Ifthis bit is setto 1 in. the corresponding-
; timer configuration nibble of TMOD register, the corresponding tinier
will act as a counler. If the bit is cleared in the corresponding timer
configuration nibble oftMOD register, the corresponding timer will
• function as timer unit. ' ;
The values for bits MO and Ml and the corresponding mode of operation for the timer/counter is
explained in the table given below.
! - - - -
crip t ion
MO i
1 . . 1 lili Ss gsiii
' v •' *' ’e 0. 8bit timer with divided by 32 presealer. CO'|j
ode T. 16bit timer.;
1 " 0 Mode 2. Autoreload mode. Configures ti
t •• : : i W * ailto reload. The lower byte of timer regt
roll
; ' higher byte oftlte comespohdingtimefmgist^
reload value. Timerwean be viewed-as two .8
./'?• ?.• V k : e ■ . V. .; , must
t.- . where x -timer number (0 or 1) - *
1 1 Mode 3. Timer 1 in this mode simply holds its count. It is similar to disabling the
i TimerT run control bit TR1. Timer 0 in this mode configures as two 8bit registers *
TLO and THO. TLO is configured by the Timer 0 configuration nibble and THOis-
configured by the Timer 1 configuration nibble. c
Timer/Counter Control Register (TCON) (SFR-88H) It is a bit addressable special function reg¬
ister that holds the timer/counter interrupt flags and run control bits.
The fol lowing table explains the meaning and use of each bit in the TCON register.
TH0/TH1 TL0/TL1
( 3< \
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit l Bit 0 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
^ —-i
Machine cycle" |
Pose1 1 -S' | 13 bit Timer Register
C/T 1= 0
TH0/TH1 TL0/TL1
8 Bits C ■. 5 Bits
c/t r i ? I | L
Counter input (T0/T1 Pin)_ j | Timer interrupt
runs in its full 8bit mode and act as the 8bit counter whereas, only the 5 least significant bits of TL0/TL1
undergoes changes in accordance with machine cycle and so TL0/TL1 is said to be acting as a prescaler
32 for the 8bit counter TH0/TH1. The timer interrupt is generated when the 13bif timer register rolls
over from all Is to all Os, i.e. when THO count is FFH and TLO count rolls from 1FH to 00H. The count
range for mode 0 is 0000H to 1FFFH.
Machine cycle
' lfosc'12) 16bit Timer Register
It is advised to set the GATE control bit to 0 for timer operations. If the GATE control is set to 1, the
timer register is incremented in accordance with each machine cycle only if the corresponding Interrupt
line INTx is high (INTO for Timer 0 and INTI for Timer 1).
S1361 Introduction to Embedded Systems
i'.-r
If the counter mode is set, and the corresponding counter run control bit (TRO for counter 0 and TR1
for counter 1) is 1, the timer register ((THO & TLO) for Counter 0 and (TH1 & TL1) for Counter 1) is
incremented by one on each traceable logic transitions at the counter input pins (TO for Counter 0 and
T.l for Counter 1). The above said actions are followed only if the gating control bit GATE is set to 0. If
GATE is set to 1 the counter (timer) register is incremented by one for each traceable logic transitions
at the counter input pins only when the corresponding Interrupt line INTx is high (INTO for Counter 0
and INTI for Counter 1). The overflow process and interrupt vectoring are similar to that for the TimerO/
Timer 1 operation.
5.3.8.3 Timer/Counter in Mode 2 (Auto Reload Mode) Timer (Counter) 0/ Timer (Counter) 1
in mode 2 acts as 8bit timer/counter (TLO for Timer (Counter) 0 and TL1 for Timer (Counter) 1) with
auto reload on the timer/counter register overflow (TLO or TL1) (Fig. 5.29). The timer/counter mode se¬
lection is done by the bit C/T of the register TMOD. Timer (Counter) 0 & Timer (Counter) 1 has separate'
selection bits as described earlier. If Timer 0 is configured as timer and if the corresponding run control
bit for Timer 0 (TRO) in Timer Control Unit (TCON)) is set,, register TLO functions as 8bit register and
starts incrementing from its current value. The timer register is incremented by one on each machine
N cycle. When the Timer register rolls over from all Is to all Os (FFH to 00H), the corresponding timer
overflow flag TFO, present in TCON register is set and TLO is reloaded with the value from THO. THO
remains.unchanged. If Timer 0 interrupt is in the enabled state and no other conditions block the Timer 0
interrupt, Rds.vectored to the corresponding vector address 000BH. On vectoring the interrupt, the TFO
flag is automatically cleared. Operation of Timer 1 in mode 2 is same as that of Timer 0 except that for
Timer 1 the 8bit timer register is formed by the register TL1 and the corresponding Timer run control bit
is TR1. The Timer 1 overflow flag is TF1 and the corresponding interrupt vector location is 001BH. It is
advised to set the GATE control bit to 0 for timer operations. If the GATE control is set to 1, the timer
register is incremented in accordance with each machine cycle only if the corresponding Interrupt line
INTx is high (INTO for Timer 0 and INTI for Timer 1).
If the counter mode is set, and the corresponding counter run control bit (TRO/for counter 0 and TR1
for counter 1) is 1, the timer register (TLO for Counter 0 and TL1 for Counter 1) is incremented by one
on each traceable logic transitions at the counter input pins (TO for Counter 0 and T1 for Counter 1).
The above said actions are followed only when the gating controLbit GATE is set to 0. If GATE is set
to 1, the timer register is incremented by one for.each traceable logic transitions at the counter input
Designing Embedded Systems with 8bit Microcontrollers—8051
pins only if the corresponding Interrupt line INTx (INTO for Counter 0 and INTI for Counter 1) is high.
The overflow process and interrupt vectoring are similar to that of TimerO/Timerl operation. Timer 1 in
Mode 2 is generally used for baudrate generation in serial data transmission.
5.3.8.4 Timer/Counter in Mode 3 Timer/Counter 0 in mode 3 functions as two separate 8bit Tim¬
ers/Counters (Fig. 5.30). TLO acts as the timer/counter register for Timer/Counter 0 and THO acts as tire
timer/counter register for Timer/Counter 1. TLO uses the Timer 0 control bits namely TRO, GATE, C/T,
INTO and TFO. TLO can run in either counter/timer mode by setting or clearing the C/T bit. Counter
operation is controlled by GATE, INTO pin, TO pin and TRO bit as explained earlier. But the operation is
similar to that of Timer 0 in mode 3. In mode 3, THO supports only timer function and does not support
counter function. THO counts only the machine cycles, not external events.
H
Timer 1 ruq control (TR1) Timer 1
TF1 TV 3
interrupt
o
THO.
n><h
e>-
o
Machine cycle 8 Bits:
3
(W12) '
'its % % 3
n>
i-t
Counter input (T0/T1 Pin) Timer 0 o
<
Timer/Counter function select logic (C/T\) Interrupt Flag o
Timer 0
t*
Timer 0 run control (TRO) o
«-;--- TFO «—1 5-
interrupt
The actual Timer 1 (Till TL1), when configured to run in mode 3, by using the Timer 1 configuration
bits, stops its functioning and simply holds its count.
Thus mode 3 provides an extra 8bit timer. The original Timer 1 (TH1 TL1) can be turned on and off
by switching it out and into mode 3 and it can run in either mode 0, mode 1 or mode 2 apart from mode
3 since the acting Timer 1, THO in mode 3 is forced to run as an 8bit timer by default, by putting Timer
0 configuration bits to mode 3 (MO, Ml of Timer 0 Control Register = 1). The only shortcoming is that
since THO is responsible generating Timer 1 interrupt when Timer 0 is configured in mode 3, no inter¬
rupt is generated by the actual Timer 1 (TH1 TL1) when it runs in mode 0,1 or 2 with Timer 0 in mode
3. But still Timer 1 can use for applications that doesn’t require Timer interrupts like baudrate genera¬
tion.
1
——-
Example 1
Implement the example 1 given under ‘Ports’ section (Binary Counter implementation) using timer interrupts.
In the example given under the ‘Ports’ section, the display delay is implemented using a delay generator loop. The
delay generation can be easily implemented using a timer and the required operations following the delay can be imple¬
mented in the timer interrupt handling routine.
8051 provides two timer units,.and they can be operated in four different modes. The timer, units in mode 1 acts as a
16bit tiftter unit which counts machine cycles from 0 (OOOOH) to 65535 (FFFFH) and generates timer interrupt when rolls
over from 65535 (FFFFH) to 0 (0000H). If the system clock is 12MHz, the timer interrupt is asserted after every 65536
Introduction to Embedded Systems
microseconds (0.0655 seconds), if the counter is started with an initial count 00H. For generating a delay of 1 second,
the timer interrupt should be generated multiple times. If one timer interrupt generates 50000 microseconds, 100 such
interrupts are required to generate a delay of 5 seconds. For generating a delay of 50000 microseconds, the timer should
be loaded with a count 65536 - 50000 = 15536 (3CB0H). The following assembly code illustrates the use of timer and
timer interrupts for implementing the binary counter requirement. Timer 0 is used for this.
TJMEROJtNTR:,
djnz;ro. RETURN
INC R7 ; ’Increment binary counter
MOV A,' R7
CPL A; The LED's are turned on when corresponding bit is .0
“-MOV P2y--‘Af Display the count on LEDs connected at Port 2
MOV R0, #100 ; Load the multiplier for the time ’iflrila’y
RETURN:
7: MOV
MOV'THO,;
'TliO,' #3CH ’ ' .
„ > MOV TiiO, DB8H
fisl I >,RETI | 11
END - ; END of "I ssernbly Program
It should be noted that the Timer 0 ISR involves lot of activities including binary counter increment and display and
hence the delay between the successive count display may not be exactly 5 seconds. The timer count can be adjusted to
take these operations into account or the displaying of the count can be moved to the main routine and synchronization
between main routine and ISR can be implemented through a flag. It is left to the readers as an exercise.
The following table explains the meaning and use of each bit in the SCON register.
Serial port mode: selector Sets the serial port operation mode
SMI
■■■■ -SM2 = 1-Enable Multiprocessor communication
Multiprocessor communication flag "• ... , . ,. . . - - ■ . .
SM2 = 0 Disable Multiprocessor communication
; .REN - *1 Enables reception T
j Serial datarec
'REN = 0 Disables serial data reception
9th Daita bit that will be transmitted in Modes 2 & 3.
Selling/Clearing under software control
Introduction to Embedded Systems
xontrollal
■
Set by internal circuitry ai the end of transmission of the
Transmit interrupt 8th bit in Mode 0. For other modes it is set at the beginning
r»T’i.1-'.. /A,!, r-A ,rpr H :'Ct i7t r* *'■'
5.3,9.1 Serial Communication Modes As mentioned earlier, 8051 supports four different
modes of operation for Serial data communication. The mode is selected by the SMO and SMI bits of
SCON register. The table given below gives the different combinations of SMO and SMI and the serial
communication modes corresponding to it.
Mode 0 The Mode 0 operation of serial port is same as that of the operation of a clocked shift register.
In Mode 0 operation Pin RXD (Port Pin P3.0) is used for transmitting and receiving serial data and Pin
TXD (Port Pin P3.1) outputs the shift clock. 8 data bits are transmitted in this mode with LSB first. The
baudrate* is fixed for this mode and it is 1/12 of the oscillator frequency. Mode 0 is half duplex, meaning
it supports only unidirectional communication at a time. It can be either transmission or reception. Serial
data transmission is initiated by a write access to the SBUF register (Any Instruction that uses SBUF
as the destination register. E.g. MOV SBUF, A; ANL SBUF, A; MOV SBUF, #data etc). The write to
SBUF loads a 1 to the MSB of the transmit shift register which acts as the stop bit in the serial transmis¬
sion. The contents of SBUF register is shifted out to the RXD Pin in the order LSB first. The bit shifting
occurs in each machine cycle until all bits including the stop bit is shifted out. The bit shift rate is same
as the machine cycle rate and hence the transmission rate in mode 0 is 1/12th of the oscillator frequency.
The Transmit Interrupt TI is set by the Transmit Control unit when all the 8 bits are shifted out. The TXD
pin outputs the transmit shift clock during data transmission. Serial data reception is enabled only when
the reception enable bit, REN is set 1 and the Receive Interrupt bit, RI is in the cleared state. A ‘write
to SCON’ (Clear Rl/Set REN) initiates the data reception. The receive control unit samples the receive
pin, RXD during each machine cycle and places the sample at the LSB of the receive shift register and
left shifts the receive shift register. The Receive Interrupt (RI) is set when all the 8 data bits are received.
Pin TXD outputs the receive shift clock throughout the reception process.
Mode 1 In Mode 1, serial data pin TXD transmits the serial data and pin RXP( receives the serial
data. Mode 1 is a full duplex mode, meaning it supports simultaneous reception and transmission of
serial data. 10 bits are transmitted or received in Mode 1. The 10 bits are formed by the start bit, 8 data
bits and one stop bit. The stop bit on reception is moved to RB8 bit of SCON. The baudrate is variable
and it can be configured for different bauds. Similar to Mode 0, transmission is triggered by a write ac¬
cess to the SBUF register in Mode l.A ‘write to SBUF’ signal loads a 1, which acts as the stop bit, to
the 9th bit position of the transmit shift register and informs the transmit control unit that a serial data
transmission is requested. Unlike Mode 0 where transmission is synchronized to ‘write to SBUF’ signal,
in Mode 1, the transmission is synchronized to a divide-by-16 counter. The divide-by-16 counter count
rate is dependent on the timer 1 overflow rate. The transmission starts with sending the start bit (logic
0), when the divide-by-16 counter rolls over
after the ‘write to SBUF’ signal. The transmit
register contents are shifted out to the TXD
pin (P3.1 Port pin) at the successive rollover
of the divide-by-16 counter. The Transmit
Interrupt TI is set by the transmit control unit t = 0 Time
when all the 8 bits are Shifted out. Figure 5.31 Bit Transmission with res&ot to time inMod. 1
illustrates the bit transmission with respect to -J
time in Mode 1.
Serial data reception is triggered by a 1 to 0 transition detected at the Receive pin RXD. The RXD
pin is sampled at a rate of 16 times the baudrate. On detecting a 1 to 0 transition at the RXD pin, the di-
vided-by-16 counter is reset and 1FFH is loaded into the receive shift register. The divide-by-16 counter
divides the bit time of a bit (1/baudrate) into 16 time frames. In order to reduce noise in input data, the
RXD pin is sampled at 7th, 8th and 9th frames of the bit time. If the value remains the same for at least
two of the samples, it is taken as the data bit. If the value obtained for the first bit time (start bit) is not 0,
the receiver circuitry resets and it will look for another 1 to 0 transition as a valid start bit condition. On
receiving a valid start bit it is placed in the receive shift register at the rightmost position after shifting
the present contents of the receive shift register to the left by one. As data bits enter into the rightmost
position, the initially loaded Is in the receive shift register is shifted out through the left most position.
As the reception goes on, when the start bit (the first received 0 bit) reaches at the left most position of
the receive shift register, the Receiver unit is informed that only one more bit reception is required and
to load the SBUF with the contents of receive shift register after the reception of next bit, move the next
receiving bit (stop bit) into RB8 and set the Receive Interrupt RI. The signal to load SBUF and RB8 and
set RI is generated only when Receive Interrupt RI = 0 and Bit SM2 in SCON is 0 or received stop bit
= 1 at the time of generation of the final shift pulse. If both of these conditions are not met, the received
byte is not moved into SBUF else the received byte is moved into SBUF, the received stop bit is placed
in the RB8 bit, present in the SCON register and the Receive Interrupt (RI) is activated. After the recep¬
tion of the stop bit, irrespective of the above-mentioned meeting criteria, the receiver unit looks for the
next 1 to 0 transition at the RXD pin for catching the next byte.
Baudratesfor Mode 1 As mentioned earlier, the baudrate for Mode 1 is variable. Mode 1 baudrate is
dependent on the operating frequency and Timer 1 overflow rate. The bit transmission rate in mode 1 is
dependent on the divide-by-16 counter overflow rate. The count rate of divide-by-16 counter is depen¬
dent on the timer 1 overflow rate. Depending on the value of the SMOD bit, the divide-by-16 counter
is incremented directly on the overflow of Timer 1 or after the two consecutive overflow of the timer
1. If SMOD is 0, the divide-by-16 counter is incremented only after two consecutive overflow of timer
L If SMOD is 1, the divide-by-16 counter is incremented with the overflow of timer 1. The baudrate in
Mode 1 expressed as:
Introduction to Embedded Systems
Mode 1 baudrate 9600 is the most compatible to communicate with PC’s COM port.
Designing Embedded Systems with 8bit Microcontrollers—8051
Modes 2 & 3 11 bits are transmitted in Modes 2 & 3. The 11 bits ar£ formed by the 8 data bits, 1 start
bit (0), 1 stop bit (1) and a programmable bit that acts as the 9th data bit. Mode 2 & 3 are also full duplex
and serial data is transmitted through the pin TXD and reception is carried out through the pin RXD.
TB8 bit of SCON register is transmitted as the 9th bit. TB8 is programmable; it can be set to either 1 or
0. TB8 bit can be used as a parity bit for transmission. The parity bit of PSW reflects the parity of Ac¬
cumulator content. If the data byte to be sent is the Accumulator content, the parity bit of PSW can be
directly moved to TB8 bit of SCON for transmitting it as the 9th data bit, which will give the functional¬
ity of a parity enabled serial data transmission. On reception of serial data, the 9th data bit is moved to
the RB8 bit of SCON register. If the serial reception is also parity enabled, RB8 can be used for checking
the parity of the received byte. The bits of the received byte can be XORed and the resulting bit can be
cornpared to the received 9th bit (which is present in the RB8 bit of SCON) in firmware to check whether
the parity bit matches with the parity of the received byte. This ensures error free reception of data. The
received stop bit is ignored in Modes 2 & 3. Mode 2 and Mode 3 of Serial communication is similar in
all respect except that the Mode 2 and Mode 3 baudrates are different.
Mode 2 baudrate is fixed and is given as 2SM0D x fosc/64
Depending on the SMOD bit, it can be either fosc/64 or fosc/32
Mode 3 baudrate is same as that of Mode 1. Refer back to Mode 1 baudrate for details.
Modes 2 & 3 of 8051 serial transmission supports multiprocessor communication if the SM2 bit pres¬
ent in the SCON register is set for each controller. In Modes 2 & 3 operation, 9 data bits and a stop bit
are received. The received 9th data bit goes to the RB8 bit of SCON register. With SM2 set, the serial
port interrupt is activated on reception of the stop bit only if the received RB8 bit is 1. When the master
processor/controller wants to transmit a block of data to any one of the slave controllers, it sends out the
Introduction to Embedded Systems
address byte of the slave controller, to which it wants to communicate. The 9th data bit that will be send¬
ing along with the address byte will be 1. If the multiprocessor mode is enabled in slaves, on receiving
the address byte the receive interrupt is activated and each slave controller can use the interrupt handler
to examine whether the received address byte is matching to its assigned address (which is stored in the
firmware). If a slave finds that the received address byte is matching with its address, it clears its SM2
bit and waits to receive the data byte block from the master. The data bytes are sent along with a 9th data
bit which is 0. If the 9th data bit is 1 it implies that the incoming byte is an address byte and if it is 0, it
informs the controller that the incoming bytes are data bytes. Once a slave is selected, it clears its SM2
bit and generates interrupt for all incoming data bytes while all other slaves ignore the received bytes
since their SM2 bit is still in the set condition. To enable the multiprocessor communication again in the
selected slave which is presently involved in the transmission it should be informed the end of reception
of data bytes through' some mechanism. On receiving the end of data byte reception information, the
slave sets the SM2 bit and brings back the slave controller into multiprocessor communication enabled
mode.
Setting SM2 bit in Mode 0 will not produce any effect in transmission/reception. If SM2 is set in
Mode 1 the validity of the received stop bit is checked. If SM2 = 1 and Mode = 1, receive interrupt RI
is generated only if the received stop bit is 1.
r.. .“ . ~. . “ '\
Example ..lT'l
V_ ; - •_;__/
Design an 8051 microcontroller based system for serial data communication with a PC as per the requirements given
below.
1. Use Atmel’s AT89C51/52 or AT89S8252 (Flash microcontroller with In System Programming (ISP) support) for
designing the system.
2. The baudrate for communication is 9600.
3. The serial data transmission format is 1 Start bit, 8 data bits and 1 Stop bit.
4. The system echoes (sends back) the data byte received from the PC to the PC.
5. On receiving a data byte from the Serial port it is indicated through the flashing of an LED connected to a port
pin.
6. Use Maxim’s MAX232 RS-232 level shifter IC for converting the TTL serial data signal to RS-232 compatible
voltage levels.
7. Use on-chip program memory for storing the program instructions.
8. Use the ‘Hyper Terminal’ application provided by Windows Operating system for sending and receiving serial data
to and from the microcontroller.
The 8051 microcontroller has a built-in serial communication module which is capable of operating in different
modes with different data rates (baudrate). The only limitation is that the serial data is transmitted and received in either
TTL/CMOS logic. (The voltage levels are the one corresponding to logic 0 and logic 1 in the respective logic family.)
The serial communication supported by PC follows the RS-232 Serial communication standard and the voltage levels
are entirely different from the TTL/CMOS logic. The voltage levels for RS-232 is +/-12 V (We will discuss about the
RS-232 Protocol, voltage level and electrical characteristics in a dedicated book, which is planned under this book se¬
ries.) For translating the RS-232 voltage levels to the TTL/CMOS compatible voltage level, an RS-232 translator chip
is required between the PC interface and the microcontroller interface. The RS-232 translator chip is available from dif¬
ferent chip manufacturers and the MAX232 IC from Maxim Dallas Semiconductors is a very popular level translator IC.
The Serial data is usually routed to an RS-232 connector (DB Connector). The RS-232 connector comes in two different
Designing Embedded Systems with 8bit Microcontrollers—8051
pin counts namely; DB9 - 9 Pin connector and DB25 - 25-Pin connector. For proper data communication, the Transmit
pin of one device (Here either PC or microcontroller) should be connected to the receive pin of the other device and
vice versa. This can be done at the microcontroller board side by connecting the TXDOUT pin of MAX232 to the RX
(Receive) pin of DB connector and the RXDIN Pin of MAX232 to the TX (Transmit) pin of DB9 connector. An RS-232
cable is used for connecting the PC COM Port with DB Connector of the microcontroller board. For the above configura¬
tion, the RS-232 cable should be 1-1 wired meaning, the RXD and TXD lines of the connectors present at both ends are
1-1 connected. If the Serial lines are not used in the cross over mode in the microcontroller board, a twisted cable (cable
with RXD pin connected to TXD pin of the other end connector and TXD pin connected to RXD pin) can be used for
establishing the proper communication. The hardware connection for implementing the serial communication is given
in Fig. 5.33.
For implementing the serial data communication as 1 Start bit + 8 Data bits + 1 Stop bit with a data rate (baudrate)
of 9600, the serial port should be configured in mode 1. Timer 1 should be configured to run in auto reload mode (Mode
2) to generate the required baudrate. With an operating clock frequency of 11.0592 MHz, Timer 1 in mode 2 should be
reloaded with a count OFDH for generating a baudrate of 9600 (Refer section Baudrates for Mode 1: for more details
on baudrate generation for serial communication in mode 1).
The flow chart given in Fig. 5.34 illustrates the firmware design for implementing serial data communication.
The firmware for Implementing Serial communication in 8051 Assembly language is given below.
Introduction to Embedded Systems
;####################################################################
;serial_polling.src
;Firmware for Implementing Serial Communication with PC
/.Written by Shibu K V. Copyright (C) 2008
; #####.###############,##################################.##############
ORG 0000H ; Reset vector'
JMP 0050H ; Jump to start of main program
ORG 0003:1 ; External Interrupt 0 ISR location
RETI ; Simply return^ Do nothing
ORG 00.0BH ; Timer 0 Interrupt ISR location
RETI ; Simply return. . Do nothing - ’ r ; "
ORG 0013H ; External: Interrupt 1 ISR location
RETI ; Simp] y ret.urn. Do nothing
ORG 001BH ; Timer 1 Interrupt" ISR location : _
RETI •- ; Simply return. Do nothing
ORG 0'023H ; . Serial Interrupt ISR location
RETI ; Simply return. Do nothing
;###################################4################################
ORG; 0.05OH ; Start of main program
l SETB P2.0 ; Turn -off data reception indicator LED -
MOV SP, #08H ; "Set stack at memory location 0811
.- • ,/CLR TR1 .. ' ... ; Stop Timer ,1 \ •. .... , .
MOV TMOD, #00100000B ; Set Timer 1 in Mode 2 (Autorelo.ad)
I p- CLR EA- ; Disable all interrupts ,, . ... ■ , .
*- : . v MOV THl, #0FDH ; Reload count for Timer 2
MOV TL1, #0FDH
; ...Select Mode 1 for serial Communication
; SMO = 0r SMI = 1, SM2 =0
; Set REN
; TB8 = 0, RB8 =0, TI =0, RI =0
MOV SCON, #0101000OB
ANL PCON, lOlllllllB ; Reset baudrate doubler bit
SETB TR1 ; Start Timer 1
; Read Receive Interrupt (RI) flag
; Check whether data reception occurred
; If not, loop till RI=1
RECEIVE:JNB RI, RECEIVE
; Data received
CLR P2.0 . ; Turn ON LED to indicate data reception
; Read the received data
MOV A, SBUF
CLR RI ; Reset RI flag
MOV SBUF, A ; Transmit back the received data
; Read Transmit Interrupt (TI) flag
; Check whether data transmission completed
; If not, wait till completion (loop till TI=1)
JNB TI, $
; Data Transmitted
SETB P2.0 ; Turn off Indicator LED ,
CLR TI ; Reset Transmit Interrupt flag
JMP RECEIVE ; Wait for next data to arrive
END ; END of Assembly Program
Introduction to Embedded Systems
The firmware polls the serial interrupt flags and takes the corresponding actions when either the receive interrupt or
transmit interrupt flag is set. This approach is CPU-intensive and the CPU is dedicated for serial data communication.
Another approach in which the serial communication is handled is the interrupt based communication approach. The flow
chart given in Fig. 5.35 models the interrupt based serial communication.
V_
start
) handler
r Return
A
Perform other tasks or simply wait forever
J
Fig. 5.35] Flow chart for implementing interrupt based serial communication
The firmware implementation for the interrupt based serial data communication is given below.
This design will not be complete without explaining the PC side application responsible for sending and receiving se¬
rial data to and from the 8051 based system connected to the serial port (Communication Port 1, COM 1) of the PC. The
8051 based system should be connected to the serial port using one of the cables (twisted/ one-to-one), as per the design
requirement, as explained earlier. Figure 5.36 illustrates the interfacing of the microcontroller board to the COM port of
the PC.
Introduction to Embedded Systems
The Microsoft ® Windows Operating System (Windows 9x/XP is used in our example) provides a built-in serial com¬
munication terminal program called ‘Hyper terminal’. The ‘ Hyper germinal’ application can be launched through Start
All Programs Accessories Communications -> Hyper terminal
The Hyper terminal application will request Tor a Connection Description while launching a new instance of the ap¬
plication. Provide a connection description name (say 8051) in the Connection Description wizard. Discard the Connect
To wizard by Cancel. Select Properties tab from the File menu of the Hyper terminal application. A.new window describ¬
ing the Properties of-the Hyper terminal connection is displayed. From the Connect To tab of the new Window, select
the com number to which the 8051 system is connected, say COM1, against the Connect using: option. Now select the
Configure., button. A new configuration window is displayed for configuring the Port Settings for the selected COM port.
Configure the Bit per second: to 9600, Data bits: to 8, Parity: to None, Stop bits: to 1 and Flow control: to None. Save
the Port Settings through the Apply button. Exit the Port Settings window by invoking the OK button. Start the communi¬
cation by invoking the Call option from the Call menu (This can also be achieved by pressing the Telephone icon on the
hyper terminal window). Now you are ready to communicate with the 8051 system connected to the communication port
configured as per the requirement. Start sending data by typing on the hyper terminal application. You can see the data
echoed by the 8051 system on the hyper terminal application. The hyper terminal application can be stopped by invoking
the Disconnect option from the Call menu. (This can also be achieved by pressing the Telephone Hang-up icon on the
hyper terminal window.)
If one of the conditions is not satisfied, the microcontroller may not startup properly. To ensure a
good startup, the reset pulse width has to be wide enough to cover the period of time, where the elec¬
trical conditions are not met. Please refer to the note given on the ‘On line Learning Centre’, on the
important parameters that should be taken into account for determining the reset pulse width.
The C51 family of microcontrollers support two kinds of reset input. The first one is a pure input
which allows an external device or circuitry to reset the mi¬
crocontroller. The second one is bidirectional, in which the
microcontroller is capable of resetting an external device. Capacitor
In actual practice a power on reset (unidirectional input) 10MFD
is given to the 8051 controller by connecting an external
resistor and capacitor as shown in Fig. 5.37.
When the power supply is turned on, initially the capaci¬ Resistor
tor acts as short circuit and the full applied voltage appears 8.2K
across the resistor (RST pin becomes at logic 1). The ca¬
pacitor starts charging gradually and the applied voltage
is build across the capacitor (RST pin becomes at logic 0).
The time taken by the capacitor to get charged to a voltage ^ '
V which brings the RST pin to logic LOW, is calculated (Fig. 5.37) Power on reset circuitry for 8051
as
vc= Vf- (Vf- V.) x ert/RC
V = voltage at which RST pin logic high changes to LOW
Vf= final voltage (VDD =5 V)
V. = initial voltage (=0 V)
RC = time constant of resistor capacitor circuit (8.2 x 103 x 10 x 10-6)
The reset pulse width ‘P can be calculated from the above equation with V = The voltage across RST
pin which makes the RST pin at logic 0 and V = 5 V. The pulse width will be of the order of milliseconds
to seconds. This ensures that the RST pin remains HIGH for enough time to allow the oscillator to start
up (A few milliseconds), the power supply to stabilise plus two machine cycles. It is to be noted that the
port pins will be in a random state until the oscillator is started and the internal reset algorithm writes 1
to the corresponding port SFRs.
5.3.11.1 IDLE Mode The IDLE mode is activated by setting the bit PCON.O of the SFR power
control register (PCON). The instruction setting the PCON.O bit pushes the processor into an idle state
where the internal clock to the processor is temporarily suspended. Before putting the CPU into idle
state, the various CPU statuses like Stack Pointer (SP), Program Counter (PC), Program Status Word
(PSW) and Accumulator and all other register values are preserved as such. All port pins will hold the
logical states they had at the moment at which the idle mode is activated by setting the PCON.O bit.
ALE and PSEN remains at logic high during Idle Mode. The interrupt, Timer and Serial port sections
continue functioning since the clock signal is not withdrawn for the same. The Idle mode is terminated
by asserting any enabled interrupt. The interrupt can be any one of the external interrupt (INTO or INTI)
or Serial Interrupt. On. asserting the interrupt, the IDLbit (PCON.O) is cleared and the interrupt routine
Introduction to Embedded Systems
is serviced. After executing the RETI instruction, the next instruction executing will be the instruction
which follows immediately the instruction that put the processor in Idle Mode. Two general purpose
flag bits GFO & GF1 present in the PCON register can be used for giving an indication for whether an
interrupt occurred in normal mode or Idle Mode. Since PCON register is not a bit addressable register,
the instruction which sets the IDLE Mode control bit PCON.O can also set the flags either GFO or GF1
or both. When an interrupt occurs its sendee routine can examine whether the interrupt occurred in the
Idle mode or Normal mode by checking the status of GFO or GF1 bits of PCON (valid only if the Idle
mode enabling instruction sets the GF0/GF1 bit along with PCON.O).
The second method for terminating the idle mode is the application of a reset input signal at the RST
pin. Since the clock oscillator is already running and it is in the stable state, the RST pin need to be held
high for only 2 machine cycles. Resetting the processor clears the IDL bit (PCON.O) directly and at this
point the processor resumes program execution from where it left off, that is, at the instruction which
immediately follows the one that invoked the idle mode. When the internal circuitry undergoes the reset
algorithm following the Reset, two or three machine cycles of program execution may take place. The
On-chip hardware inhibits any read/write access to the scratchpad RAM during the internal reset algo¬
rithm execution, but access to the port pins are not inhibited. Hence it is advised to put a minimum of 3
NOP instructions which follows the instruction that triggers the idle mode.
Power Control Register (PCON) (SFR-87H) It is the Special Function Register holding the power
control bits and baudrate multiplier bit. PCON is not a bit addressable register. The details of PCON bits
are given below.
The following table explains the meaning and use of each bit in the PCON register.
The NMOS version of 8051 devices implement only SMOD bit in PCON register. The other 4 bits
are available only in CMOS versions. Reset value of PCON is OXXXOOOO (X denotes don’t care. It may
be either 0 or 1). PD bit always take precedence over IDL bit and if an instruction sets both IDL and PD
bits to 1, action corresponding to PD = 1 is performed and it masks the IDL bits activity.
5.3.11.2 Power Down Mode When the PD bit (PCON. 1) is set by an instruction, the Power Down
mode is activated. The CPU stops executing instructions; clock signal to all internal hardware including
timer unit, interrupt system and CPU is terminated. The contents of internal RAM and SFR are main¬
tained. All ports continue emitting their respective SFR contents. ALE and PSEN signals are held at low.
Power Down mode can only be terminated through hardware reset. All SFRs are brought into their reset
Designing Embedded Systems with 8bit Microcontrollers—8051
To CPU
PD IDL
values. However the on-chip RAM retains their contents to that of the values when the Power Down
mode is entered. The supply voltage. R to the controller can be brought down to as low as 2V when the
controller is in Power Down mode. However before terminating the Power Down mode with a hardware
reset, the supply voltage V should be brought into the normal operating level. If the hardware reset is
applied to the controller to terminate the processor before bringing the operating voltage to the nominal
level, the controller may behave in an unexpected way. Hence reset should not be activated before R
is brought to its normal operating level, and the reset signal should be held active till the oscillator is
re-started and becomes stabilised. ;;
5.3.11.3 Power Consumption V/s Oscillator Frequency Average Power consumption is di¬
rectly proportional to the operating frequency (Clock frequency). Increasing the clock frequency (sub¬
ject to the condition that it will not exceed the maximum-*-
supported frequency by the system core) increases the Typical 8051 Icc
execution speed of the controller. But it also has a. di- vs- Frecluency characteristics
rect irppact on the total power consumption. Figure 5.39 — 25 ITT TTTTTTTTTTTTI
illustrates the typical power curve for a generic 8051 o //
microcontroller. jj, 20
Generally, the current consumption v/s operating fre- g //
quency characteristics is linear with a dc offset. The dc |< 15--
quiescent current is generated by static on-chip circuit- g /
ry such as op amps, comparators,-etc.. This current is a g 10--
constant drain current typically in the range of 1mA. g /
From the power curve it is obvious that as the operating g 5 ^_:_
speed demand is high, the power consumption also goes u //
high. So there is always a trade-off between processing [ JJ
power and power consumption with the generic 8051 1 2 33 44 55 66 77 88 99 1011121314151617181920
12 1011121314151617181920
design. Some techniques used to achieve high process- j Operating frequency
Operating frequency (MHz)
(MHz)
mg speeds by maintaining the power consumption to the ,-
least possible are explained below. ’ 9) Typical
Typi^l Power
Power consumption
consumption curve
curve
r 1 8051
for generic 80S1
High-speed core The original 8051 design was based Copyright Maxim
Copyright Maxim Integrated
Integrated Products
Products
. , , , . . , „ . , . (http://www.maxim-ic.com) used by permis-
on a 12 clock/machine cycle and two fetches per machine sjon
cycle architecture. The high speed core uses 4 clocks or 1
clock per machine cycle design. Comparing this with the original 12 clock/machine cycle for the same
operating frequency, the performance will be approximately 3 times for the 4 clock core and 12 times
Introduction to Embedded Systems
is the use of a single chip that integrates the 8051 controller Performance relative to 1MHz 8051
and the related necessary chips in a single chip. Apart from-
from
significant reduction in power consumption, this approach [Fig. 5.40) Power Consumption curve for
also reduces the space required for placing the components normal cor& Ws Reduced Cloak;
on the PCB and thereby makes the embedded product much i
Copyngh t Maxim In tegra ted Products
more compact one.
Use of On-chip program memory and RAM Use *-,**<;%?>•
of external Program Memory (EEPROM/FLASH) and RAM requires an additional latch circuit (e.g.
74LS373) to latch the lower order address bus from the multiplexed address/data bus. Adding this chip
will again increase the total system power. Nowadays controllers with internal program memory rang¬
ing from 4K to 64K are available in the market. Some manufacturers provide extra RAM up-to 1 KB as
on-chip RAM. --——--
Waveform vs. 7CC Comparison
Clock source The standard 8051 design uses either an ex- 25 — — — — —
temal quartz crystal resonator to excite the internal on-chip
oscillator circuitry or an external standalone crystal oscillator. 20
5.3.11-4 On Circuit Emulation (ONCE) Mode The On Circuit Emulation (ONCE) Mode helps
in testing and debugging the system without removing the microcontroller from the circuit. The ONCE
mode is invoked through the following steps:
1. Pulling the ALE pin while the controller is in reset and PSEN is high
2. Holding the ALE pin low as Reset is de-activated
In ONCE mode, Port 0 pins stay in the floating state and other Port pins, ALE and PSEN are pulled
high weakly internally. The oscillator circuit remains active. With these conditions an emulator or an¬
other controller can be used for driving the circuit. Normal operation of the target controller can be
restored by applying a normal reset.
-5.5 8051/52VARIANTS «
Lot of variants are available for the basic 8051 architecture from different microcontroller manufactures
including Atmel, Maxim/Dallas, etc. We will have a glance through on some of them,
slow RAM/peripheral devices. It also contains 2Kbytes of FLASH bootloader containing the low level
FLASFI memory programming routines and default serial loader. The 89C51ED2 version is similar to
the RD2 version in all respect and it supports 2Kbytes of On-Chip EEPROM memory for non-volatile
data storage.
Summary )
v' Feature set, speed of operation, code memory space, data memory space, development support) 'availability,
power consumption, cost, etc. are the important factors that need to be considered in the selection of a microcon¬
troller for an embedded design *
/ The basic 8051 architecture consist of an 8bit CPU-with Boolean processing capability, oscillator driver unit,
4K bytes of pn-chip program memory, 128 bytes of internal data memory, 128 bytes of special function register
memory, area, 32_general purpose TO lines organised into four 8bit bi-directional ports, two 16bit timer units and
a full duplex programmable UART for serial data transmission with configurable baudrates
v' 8051 is built around the ‘Harvard’ processor architecture. The program and data memory of 8051 is logically
separated and they physically reside separately. Separate address spaces are assigned for data memory and pro¬
gram memory. 8051 ’s address bus is 16bit wide and it can address up to 64KB memory
v' The Program Strobe Enable (PSEN) signal is activated during the external program memory fetching operation,
whereas.it is not activated if the program memory is internal to the microcontroller. The Program Counter (PC)
Register supplies the address from which the program instruction to be fetched.
S The 8051 architecture implements 8 general purpose registers with names RO to R7 and they are available in 4
different banks
S The lower 128 byte RAM of 8051 is organised into register banks, Bit addressable memory and Scratchpad RAM
S Accumulator, B register, Program Status Word (PSW), Stack pointer (SP), Data pointer (DPTR, combination of
DPL and DPH), and Program Counter (PC) constitute the CPU registers of 8051
S The instruction fetch operation consists of a number of machine cycles and one machine cycle consists of 12
clock periods for the standard 8051 architecture
S 8051 supports 4 bi-directional ports. The ports are named as Port 0,1,2 and 3. Each port contains 8 bidirectional
port pins
J The ‘Read Latch’ operation for all port pins return the content of the corresponding pin’s latch, whereas the ‘Read
Pin’ operation returns the status of the port pin
v' The basic 8051 and its ROMless counterpart 803/AH supports five interrupt sources; namely two external inter¬
rupts, two timer intenupts and the serial interrupt. The serial interrupt is an ORed combination of the two serial
interrupts; Receive Interrupt (RI) and Transmit Interrupt (TI). An interrupt vector is assigned to each interrupts
in the program memory and 8 bytes of code memory location is allocated for writing the ISR corresponding to
each interrupt
Designing Embedded Systems with 8bit Microcontrollers—8051
/ The interrupt system of 8051 can be enabled or disabled totally under software control by setting or clearing the
global interrupt enable bit of the Special Function Register Interrupt Enable (IE).
/ The 8051 architecture supports two levels of interrupt priority. The first priority level is determined by the
settings of the Interrupt Priority (IP) register. The second level is determined by the internal hardware polling
sequence
/ An interrupt service routine always ends with the instruction RETI. The RET) instruction informs the interrupt
system that the service routine for the corresponding interrupt is finished and it clears the corresponding priority-
X interrupt in progress flag by clearing the Corresponding flip flop
The basic 8051. architecture supports two timer units namely Timer 0 and Timer 1. The timer units can be config¬
ured to operate as either timer or counter. The timers support four modes of operation namely Mode 0,1,2 and 3 ,
/ The standard 8051 supports a full duplex, receive buffered serial interface for serial data transmission rf f
/ The CMOS version of 8051 supports two power down modes, najnely, IDLE and POWERDOWN. They are
activated by setting the corresponding bit in Special Function Register PCON
/ The on circuit emulation (ONCE) mode of 8051 helps in testing and debugging the 8051 microcontroller based
system without removing the microcontroller from the circuit ■
/ The 8052 microcontroller architecture supports an additional 16bif timer and if supports capture mode for timer
operation in addition to the four modes supported by 8051. If also implements the upper 128bytes of user data
RAM physically Oil the chip ' ” ' -■ i-
W Keywords '
Machine The fimdamentalunit of instruction execution. One instruction execution may require
one or more machine cycles. Under standard 8(151 architecture, one machine cycle cor-
’ responds to 12 clock Denods '
)in can supply .to drive'an externally connected device
Sink Current
Objective Questions
1. What is the size of internal data memory supported by the standard 8051 architecture
(a) 64 bytes (b) 128 bytes (c) 256 bytes (d) 1024 bytes
(e) No internal data memory
2. What is the size of a ‘Special Function Register’ memory supported by the standard 8051 architecture
(a) 64 bytes (b) 128 bytes (c) 256 bytes (d) 1024 bytes
(e) No internal SFR memory
3. What is the size of an internal program memory supported by the standard 8051 architecture
(a) 128 bytes (b) 1024 bytes (c) 2 Kbytes (d) 4 Kbytes
(e) No internal program memory
4. What is the number of general purpose I/O lines supported by the standard 8051 architecture
(a) 8 (b) 16 (c) 32 (d) 64
5. The general purpose I/O lines of a standard 8051 is grouped into
(a) Two 8-bit bi-directional ports (b) Four 8-bit bi-directional ports
(c) Two 16-bit bi-directional ports (d) Four 16-bit bi-directional ports
6. What is the number of timer units supported by a standard 8051 architecture
(a) Two 16-bit timers (b) Three 16-bit timers (c) Two 8-bit timers (d) One 16-bit timer
7. The UART of the standard 8051 controller is
(a) Half duplex with configurable baudrate (b) Half duplex with fixed baudrate
(c) Full duplex with configurable baudrate (d) Full duplex with fixed baudrate
8. The standard 8051 controller is built around
(a) Harvard Architecture (b) Von Neumann Architecture (c) None of these
9. Which of the following is True for a 8051 controller?
(a) The program and data memory of 8051 is logically separated
Designing Embedded Systems with 8hit Microcontrollers—8051
(b) The program and data memory of 8051 physically resides separately.
(c) Separate address spaces are assigned for data memory and program memory
(d) All of these (e) None of these
10. The address bus of 8051 is
(a) 8-bit wide (b) 16-bit wide (c) 32-bit wide (d) 20-bit wide
11. Which of the following is True about external program memory access?
(a) Port 0 acts as the data bus and Port 2 acts as the higher order address bus
(b) Port 2 acts as the data bus and Port 0 acts as the higher order address bus
(c) Port 0 acts as the multiplexed address/data bus and Port 2 acts as the higher order address bus
(d) Port 2 acts as the multiplexed address/data bus and Port 0 acts as the higher order address bus
12. Name the register holding the address of the memory-location, holding the next instruction to fetch
(a) DPTR (b) PC (c) SP (d) None of these
13. Name the register holding the address of the external data memory to be accessed in. 16bit external data memory
operation
(a) DPTR (b) PC (c) SP * (d) None of these
14. For standard 8051 architecture, the internal data memory address is
(a) 8bitwide (b) 4bitwide (c) 16bitwide (d) None of these
15. The external data memory is 4 Kbytes in size and it is arranged in paged addressing mode where 1 page consists
of 256 bytes. Port 2 is used as the page selector. How many port bits are required for implementing the paging?
(a) 1 (b) 2 (c) 3 (d) 4
16. Which of the following conditions should be satisfied to implement the Von-Neumann memory model for 80511
(a) The data memory and code memory should be stored in a single memory chip (RAM/NVRAM)
(b) The external access pin should be tied to logic 0
(c) The external access pin should be tied to logic 1
(d) The PSEN\ and RD\ signals of 8051 should be ANDed to generate the output enable (OE\) of the memory
chip
(e) (a) (b) and (d) (f) (a) (c) and (d)
17. What is the number of general purpose registers supported by the standard 8051 architecture
(a) 1 (b) 8 (c) 16 (d) 32
18. What is the number of bit variables supported by 8051 in the internal data memory area?
(a) 8 (b) 16 (c) 32 (d) 64
(e) 128
19. Bit address 07H contains logic 1, what is the value of bit 07H after executing the instruction MOV 20H, #01H
(a) 0 (b) 1
20. Memory location 81H contains FOH. What will be the value of Accumulator after executing the instruction MOV
A, 81H
(a) 81H (b) 00H (c) FOH (d) Random data
21. The target controller is a standard 8051. Memory location 81H contains FOH. WhaPwill be the value of accumula¬
tor after executing the instructions
MOVRO, ffilH
MOV A, @R0
(a) 81H (b) 00H (c) FOH (e) Random data
22. Name the register signalling the status of accumulator related operations
(a) A (b) B (c) PSW (d) SP
(e) None of these
23. How many user programmable general purpose bits are available in the status register of80511
(a) 0 (b) 1 (c) 2 (d) 3
(e) 4
24. The accumulator content is 02H. What will be the status of the ‘parity bit P’ of the Status Register?
(a) 1 (b) 0
Introduction to Embedded Systems
25. The accumulator contains OFH. The overflow (OV) carry (C) flag and Auxiliary carry flag (AC) are in the set state.
What will be status of these flags after executing the instruction ADD A, #1
(a) OV = 0;C = 0;AC = 0 (b) OV = 0; C = 0; AC = 1
(c) OV= 1;C = 1;AC= 1 (d) OV= 1; C = 1;AC = 0
26. The register bank selector bits RSO, RSI are 0 and 1. What is the physical address of register R0?
(a) 00H (b) 08H (c) OFH (d) 10H
(e) 18H
27. The machine cycle for a standard 8051 controller consists of
(a) 2T States (b) 4T States (c) 6T States (d) 8T States
28. Which port of 8051 is ‘true-bidirectional’?
(a) Port 0 (b) Port 1 (c) Port 2 (d) Port 3
29. For configuring a port pin as input port, the corresponding port pins bit latch should be at
(a) Logic 0 (b) Logic 1
30. Port 2 latch contains A5H. What will be the value of Port 2 latch after executing the following instructions?
MOV DPTR, #0F00H
MOV A, #0FFH
MOVX @DPTR, A
(a) 00H (b) OFH (c) A5H (d) FFH
31. The alternate I/O function for the pins of Port 3 will come into action only when the corresponding bit latch is
(a) 1 (b) 0
32. The interrupts Timer 0 and serial interrupt are enabled individually in the interrupt enable register and high priority
is given to Timer 0 interrupt by setting the Timer 0 priority selector in the interrupt priority register. It is observed
that the serial interrupt is not at all occurring. What could be the reasons for this?
(a) The global interrupt enable bit (EA) is not in the set state
(b) The Serial interrupt always occurs with Timer 0 interrupt
(c) There is no Serial data transmission or reception activity happening in the system
(d) None of these (e) (a) or (b) or (c)
33. Timer 0 and External 0 interrupts are enabled in the system and are given a priority of 1. Incidentally, the Timer 0
interrupt and External 0 interrupt occurred simultaneously. Which interrupt will be serviced by the 8051 CPU?
(a) Timer 0 (b) External 0
(c) External 0 interrupt is serviced first and after completing it Timer 0 interrupt is serviced
(d) None of them are serviced
34. What is the maximum ISR size allocated for each interrupt in the standard 8051 Architecture
(a) 1 Byte (b) 4 Byte (c) 8 Byte (d) 16 Byte
35. What is the minimum interrupt acknowledgement latency in a single interrupt system for standard 8051 architec¬
ture
(a) 1 Machine cycle (b) 2 Machine cycle (c) 3 Machine cycle (d) 8 Machine cycle
36. External 0 interrupt is asserted and latched at S5P2 of the first machine cycle of the instruction MUL AB. What will
be the minimum interrupt acknowledgement latency time?
(a) 6 Machine cycles (b) 5 Machine cycles (c) 3 Machine cycles (d) 2 Machine cycles
37. What is the minimum duration in which the external interrupt line should be asserted to identify it as a valid
interrupt for level triggered configuration?
(a) 1 Machine cycle (b) 3 T States (c) 2 Machine cycles (d) 3 Machine cycles
38. What is the timer increment rate for timer operation for standard 8051 architecture
(a) Oscillator Frequency/6 (b) Oscillator Frequency/12
(c) Same as Oscillator Frequency (d) Oscillator Frequency/24
39. What is the maximum count rate for counting external events for standard 8051 architecture
(a) Oscillator Frequency/6 (b) Oscillator Frequency/12
(c) Same as Oscillator Frequency x (d) Oscillator Frequency/24
40. Which is the ‘Timer’ used for baudrate generation for serial communication?
(a) Timer 0 (b) Timer 1
Designing Embedded Systems with 8bit Microcontrollers—8051
Review Questions j
1. Explain the various factors to be considered while selecting a microcontroller for an embedded system design.
2. Explain the architecture of the 8051 microcontroller with a block diagram.
3. Explain the different operating modes of 8051 (Hint: normal operation mode, power saving mode and ONCE
mode)
4- Explain the code memory organisation for 8051 for internal and external program memory access.
5.. Explain the data memory organisation for standard 8051 controller.
6f Explain the Von-Neumann memory model implementation for 8051 based system. What are the merits and demer¬
its of using a Von-Neumann memory model? ,
/• Explain the memory organisation for lower 128 bytes of internal RAM for standard 8051 architecture.
Introduction to Embedded Systems
8. Explain how Port 0 acts as a normal I/O port and multiplexed address data bus for external data/'program memory
access?
9. What is the difference between Read Latch and Read Pin operation for port lines?
10. Why is Port 0 known as true-bidirectional?
11. Why is Port 1 known as quasi-bidirectionall
12. Explain how Port 2 acts as a normal I/O port and higher order address bus for external data/program memory
access?
13. Explain how Port 3 acts as a normal I/O port and an alternate EO function port?
14: What is the difference between RET and RETI instructions for indicating the end of a subroutine call?
15. Explain how a third priority level can be achieved in 8051 interrupt system?
16. What is interrupt latency? What is the minimum and maximum interrupt latency time for a single interrupt system
in standard 8051 architecture? Explain in detail.
17. Explain the different conditions that block or delay the vectoring of an interrupt.
18. Explain the different actions generated by the processor on identifying an interrupt request number.
19. The external interrupt INTO is enabled, and set to be level triggered. The Port pin P3.2 is set at logic 0. Explain the
behaviour of the system.
20. Explain the operation of Timer 0 in Mode 0. Why is Mode 0 operation known as 8-bit Timer with a divide by 32
prescaler?
21. Explain the auto reload mode of operation of a timer. Give an example for the usage of Timer 1 in auto reload
mode.
22. Explain the Mode 1 operation of serial communication. How is configurable baudrate achieved for serial commu¬
nication in Mode 11 What is the Timer 1 auto reload count for a baudrate of 9600 bits/seCond for a system working
at 11.0592MHz?
23. What is multiprocessor communication? Explain the multiprocessor communication for 8051 for serial communi¬
cation Mode 2.
24. How a ‘power on reset’ can be implemented for an 505/-based system? Explain the changes that happen to various
registers and internal RAM on a proper reset.
25. Explain the two power saving modes Idle and Power Down modes for 8051. What is the difference between the
two? Explain the methods of terminating each of the power saving modes.
26. Explain the Power consumption v/s Operating Frequency characteristics for an 8051 based system. Why do the
characteristics curve contain an offset from the origin?
27. What are the different techniques that can be adopted for reducing the power consumption of an 8051 based sys¬
tem?
28. What is On Circuit Emulation (ONCE) mode? How is ONCE mode enabled? What is the use ONCE mode in
system design?
29. Explain the difference between 8051 and 8052 microcontroller.
30. Only Timer 0 interrupt is enabled in the system and it is assigned a priority of 1. It is observed that the timer inter¬
rupt occurred only once and it is not occurring even though the timer roll over happens. The program does not
contain any instruction for disabling the global interrupt enable flag IE and Timer 0 interrupt. What could be the
reason?
Lab Assignments
1. Write an 8051 assembly language program for transmitting V byte data through the serial port with a ninth bit
which is uked as an even parity bit. Theprogram should configure the serial communication settings as: Baudrate
= 9600,1 start bit, 1 stop bit and also depending on the parity' of the data byte to be sent, the parity bit should be
set properly. ' ■ . - ■ ; "v'A- -
Designing Embedded Systems with 8bit Microcontrollers—8051
2. Write an 8051 assembly language program for receiving an even parity enabled 8-bit data through the serial port.
The program should configure the serial communication settings as: Baudrate = 9600,1 start bit, 1 stop bit. Upon.
■ receiving the data byte, the parity of-it is calculated andit is verified with the parity bit received. Use interrupts,
, :;/ for implementing the serial data reception ; i 1 ', ’ ' . . • . , §J y_..
3. Develop a hardware system to single step the program written for 805J. The firmware running,in the controller
RV’ senils the register contents A, B/PSW and R0 to R" on executing each instruction to a program running on I’C.
Develop a PC application to capture the register details'and display it. Use RS-232 IJART communication for
■ - sending the debug info to PC. Use the following configuration settings for the'RS-232 link: Baudrate = 9600, 1
‘ start bit, 1 stop bit, No parity. ' ' -
4. Develop an 5057-based system for detecting the keypress of a pushbutton switch connected to the port pin
P1.0 of the microcontroller. Implement the key de-bouncing for the push button in firmware (Upon detecting
a keypress, the firmware waits for 20 milliseconds and then checks the push button switch again, if the switch ’
remains in the depressed state, the key press is identified as a valid key depression): For a valid keypress, a buzzeK
connected to port pin PI T is activated for a period of 1 second. Use a BC547 transistor-based driver circuit for
driving the buzzer. •
5. Implement the above requirement using a hardware key de-bounce circuit and connecting the push button switch
to the external interrupt 0 pin of the controller. Implement the buzzer control logic in the Interrupt Service Rou¬
tine for External Interrupt 0.
, 6. Develop an 8051 -based system for delecting (lie keypress of an array of 8 pushbutton switches connected to the
• port pins PRO to PI.7 of the microcontroller. Using an AND gate generate an external interrupt signal when any
L one of the push button switch is depressed. Identify which push button is depressed by scanning the status of
rL the push button connected port pins, in the interrupt service routine for the external interrupt. The port pins are
• scanned in the order P1.0 to P1.7. Even if multiple push buttons are depressed, the first identified pushbutton
switch is recognised as the valid keypress. Use a hardware key de-bounce circuit/IC for implementing the key
de-bouncing for all push buttons. An 8 ohm speaker is connected to port pin P2.0, Generate different tones (by
varying the frequency (say 100Hz, 200Hz, 300Hz, 500Hz, etc) of the pulse generated at the port pin in which
the speaker is connected, corresponding to each push button press. Use BC547 transistor based driver circuit for
driving the speaker.
7. Design an 8051 based system for interfacing an RF transceiver (e.g. RF Modem from SUNROM Technolo¬
gies http://www.sunrom.com/index.php7main_page-product_info&cPath-89&productsJd=560) and a relay
driver circuit using an NPN transistor for driving a normally open relay with coil voltage 12V for switching
Alternating Current with ratings SAmps and 50Hz. Connect a 100W/240V bulb through the relay, 'burn ON and
OFF the bulb in response to the command received by the RF transceiver. The commands arc sent by an.applica¬
tion running on PC through an RE transceiver.
8. Implement the above system using infrared (IR) remote control and receiver, supporting the RC5 protocol (e.g.
TV remote and SFH 506-36 IR receiver unit (Use the reference design http://www.atmel.com/dvn/resources/
prod_documents/doc 1473 .pdf from Atrnel for SFH 506 interfacing)), in place of the RF based control implemen¬
tation.
9. Develop a miniature ‘Traffic Light Control’ system based on 8051 and LEDs for controlling the ‘Red’ ‘Green’
and ‘Yellow’ signal lights present at a junction where four roads meet. The ‘Green’ and ‘Red’ signal will be on
for a duration of 30 seconds and ‘Yellow’ for 5 seconds.
10. Develop a simple electronic calculator using 8051 microcontroller with 16 push button keys arranged in a 4x4
matrix for representing digits 0 to 9, operators +, x, %, = and ‘Clear’ button. Use two 7-segment LEDs for
displaying the result. Only single digit operations are allowed. The system requirements are listed below.
(a) Upon power on both the 7-Segment LEDs display 0
(b) When a numeric key is pressed, it is displayed on the rightmost 7-segment LED
(c) Enter the first operand (digit) by pressing the corresponding push button
(d) To perform an operation (+, x, %) press the corresponding push button
(e) Now press the second operand (digit) ,
(f) Press the‘=’operator for displaying the result of the operation
I (g) Pressing the ‘Clear’ key at any point clears the display and displays ‘0’ on both LED displays. For division
operation, display the quotient as result.
f
LEARNING OBJECTIVES
As you learned in the basic 8085/8088 microprocessor class, an Instruction consists of two parts name¬
ly; Opcode and Operand (s). The opcode tells the processor what to do on executing an instruction.
The operand(s) is the parameter(s) required by opcode to complete the action. The term Instruction Set
refers to the set of instructions supported by a processor/controller architecture. The instruction set of
8051 family microcontroller is broadly classified into five categories namely; Data transfer instruc¬
tions, Arithmetic instructions, Logical instructions, Boolean instructions and program control transfer
instructions. Before going to the details of each type of instructions, it is essential to understand the
different addressing modes supported by the 8051 microcontroller.
Programming the 8051 Microcontroller
Memory
A
address
B
RO -
Register RO is loaded with 55H (85 in decimal) on executing the first instruction. Here RO is used as
the indirect addressing register and data memory 55H is the memory variable on which the operations
are performed. Executing the second instmction moves the contents of memory address 55H to the ac¬
cumulator. This can be interpreted as MOV A, content @Memory Address 55H. Suppose 55H contains
255, executing the second instmction will load the accumulator with 255 (Fig. 6.2).
Introduction to Embedded Systems
Memory
address
255 (FFH)
I Fig. 6.2 1 Illustration of indirect memory addressing with 8bit indirect register (E.g. MOV A,
Among the scratchpad registers RO to R7, only RO and R1 can be used for indirect addressing. For
16bit external data memory/memory mapped register operations, 16bit register DPTR is used as the
indirect addressing register. The whole 64K bytes of the external memory (if present physically on chip)
can be accessed by indirect addressing.
‘E.g. MOV DPTR, #0055H Load DPTR register with 0055H, .The-
■ - . ... O' ; address of "the memory location • d ■ ..
MOVX A-,@DPTR ' ; Load Accumulator with the contents-
" . '- ■; of the ■ memory''location pointed By DPTR -
Executing these two instructions moves the content of external data memory at address 0055H to the
Accumulator (Fig. 6.3).
Illustration of Indirect memory addressing with 16bit Indirect Register (E.g. MOVX A, @DPTR)
Indirect addressing is same as the pointer concept in C Programming. Indirect addressing is com¬
monly used for operations similar to pointer based operations in C Programming like array, external
memory access, etc.
6.1.3.1 Register Instructions In register instructions, the register operand is specified by some
bits of the opcode itself. If scratchpad registers RO to R7 are used as operands, they can be specified
along with the opcode by using the last 3 bits of the opcode. Such instructions are code efficient since
the opcode itself specifies one operand; it eliminates the need for storing the operand as a byte in code
memory and also saves the time in fetching the operand. Generally register variable access is faster than
general memory access.
MOV RO,. 01H
This is a two byte instruction where the first operand RO is indicated by the last 3 bits of the opcode
byte.
Register to register data transfer is not allowed and hence instructions like MOVR1, R2 are invalid.
6.1.3.2 Register Specific Instructions Some of the 8051 instructions are specific to certain regis¬
ters. Examples are accumulator and data pointer specific instructions.
E.g. ADDA, ‘150 vV y ' y; - y::
During the assembly process, this instruction is converted to a two byte instruction in which A is
implicitly specified by the opcode corresponding to ADD.
The above example assumes that the lookup table is at memory location 2008H and so the base ad¬
dress of the lookup table is 2008H. The first instruction loads the base pointer address 2008H to the
base pointer register DPTR. The table index is provided by the accumulator register. To access the cor¬
responding entry of the table, load accumulator with the offset from the base address. For accessing the
base element the offset should be 0. The second instruction loads the offset to accumulator. For example,
if you want to access the entry which is just above the base entry, the accumulator should be loaded with
1. Executing the third instruction moves the content of the table entry at memory location 0+2008H to
the accumulator. Since the offset address register, accumulator, is an 8bit register, the maximum size of
the look up table is 256 bytes (Base address + 0 to Base address + 255).
Introduction to Embedded Systems
Another register, which is 16bit wide, used for indexed addressing is Program Counter (PC). The
only difference in using PC for indexed addressing when compared to DPTR is that the PC is not avail¬
able to user for direct manipulation and user cannot directly load the desired base address to the PC
register by a MOV instruction as in the case of DPTR. Instead the desired address is loaded to the PC by
diverting the program flow to the location where the Lookup table is held in the code memory by using
a CALL instruction.
First the desired table index is loaded to the accumulator register and a call is made to the location
where the lookup table is stored. For this type of tables, the index should be within 1 to 255 (including
both). 0 cannot be used as an index since the execution of MOVC A, @A +DPTR increments the PC to the
address of the RET instruction’s memory location. If you use a 0 index along with the PC, the contents
retrieved will be the binary data corresponding to the RET instruction. Code memory lookup tables are
usually used for storing string constants and other configuration data. Another use of indexed addressing
is the ‘"case jump’ instruction in 8051. Here the jump instruction’s destination address is calculated as the
sum of the base pointer (PC-) and the Accumulator data.
Write an assembly program to display the numbers from 0 to 9 on a 7-segment common-anode LED display which is
connected to Port 2 of the 8051. The seven segment codes for displaying the numerals 0 to 9 on the common anode LED
is stored as lookup table in the code memory starting at 0050H. Use DPTR register for holding the base address of the
table.
Please refer to the section on 7-segment LED Display given in Chapter 2 for more details on 7-segment LED Display.
Figure 6.4 illustrates the interfacing of common anode LED Display with Port 2 of 8051.
Programming the 8051 Microcontroller
Now we need to design the different bit patterns (code) to be outputted to the Port 2 pins to displayjlpie numbers from
0 to 9 on the LED Display. The following table explains the same. Refer to the LED segment arrangement diagram given
earlier for clarification.
is®
I -"aa ■
SftHfl,|‘iiii''tii'a<'accjacTa5.n«n;uiraiii
“ I
L
liflliEiilllli - jS|pa|fc
■BiiBi
’ K. ... ;4
* '■ .
mj !i ' j Ijj y : . . ■ :■
V'-.. ij; ■ ( ■ r| ,•» : . ■;.) r [ " j if:;': g.|j|i|.
lll| r
—
N'1.L :-
-":fs ■ - -■
■ li i s ■ ■-1
Now the codes for displaying the digits and the port interfacing for the LED display is complete.
The next step required for setting up the decimal counter for counting from 0 to 9 is the development of
firmware,The flow chart given in Fig. 6.5 illustrates the firmware requirements for building the decimal
counter.
As mentioned earlier, the 8051 Instruction Set is broadly classified into five categories namely; Data
transfer instructions, Arithmetic instructions, Logical instructions, Boolean instructions and Program
control transfer instructions. The following sections describe each of them in detail.
6.2.1.1 Internal Data Transfer Operations Internal data transfer instructions perform the move¬
ment of data between, register, memory location, accumulator and stack. MOV instruction is used for
data transfer between register, memory location and accumulator. PUSH and POP instructions transfer
data between memory location/register and stack. The following table summarises the various internal
data transfer instructions.
Introduction to Embedded Systems
i.Mj iS6«Jf H
MOV < dost >, A <c es the content. of A to destination. <dest> can
tgisters B, RO, Rl, ...R7, any SFR, or a direct
nal data memory or an internal data memory
ted indirectly by indirect addressing register
>rRl and <St .“ ■ . ■ • .
DPTR
MOV instruction copies the content of source to destination. Only the destination is modified. The
source data remains unchanged.
6.2.1 2, Stack Memory Related Data Transfer 'Stack' is an internal memory for storing variables
temponmih Stack memory is normally used for storing the data during subroutine calls. In 8051, by de¬
fault the slack memory' is allocated from the memory' location 07H onwards by loading the stack pointer
(SP) register with 07H on power on reset. PUSH instruction stores data on stack memory. The PUSH
instruction first increments the SP by one and copies the data to the memory location pointed by the SP
register PUP instruction retrieves the data, which is stored on the stack memory using PUSH instruc¬
tion. POP instruction copies the data stored in memory' location pointed by SP register to the variable
t Foi all data transfer instruction involving an immediate constant as the source, and scratchpad register RO to R7 as the destination
(e g M< A Rlt •-('(). MCA ’o RO. nOOi the execution cycle is 1 machine cycle.
Programming the 8051 Microcontroller
given along with POP instruction and decrements the SP by one. In 8051 architecture the stack memory
grows upward in memory and follows Last In First Out (LIFO) method. PUSH and POP instructions
use only direct addressing mode and hence the instructions PUSH RO, PUSH Rl, PUSH A, etc. are not
valid. They are valid only if the arguments are used-with absolute addressing. Hence the valid instruc¬
tions corresponding to them are PUSH ARO, PUSH AR1, and PUSH ACC, etc. The operation of PUSH
and POP instructions are illustrated in Fig. 6.6 and Fig. 6.7.
j_Load RO
with 55
MOV RO, #55
Get Contents of RO ■
w PUSH ARO Increment SP register
Store the Contents of
RO at Memory Location
Random Data Pointed by SP
The upper 128 bytes of RAM are not implemented physically in the basic 8051 architecture and its
ROMless counterpart 8031. With these devices, if the SP is set to a location in the upper 128 byte area,
the PUSHed bytes will be lost and POPed bytes will be indeterminate.
6.2.1.3 Data Exchange Instructions The data exchange instructions exchange data between
a memory location and the accumulator register. Data exchange instructions modify both memory
location and accumulator register. 8051 supports two data exchange instructions, namely, XCH A,
<memloc> and XCHD A, @Ri.
The XCH A, <memloc> instruction performs the exchange of the data at memory location ‘memloc’
with the accumulator. By using MOV instructions the XCH instruction can be implemented as
It requires three instructions and 4 machine cycles and a temporary variable RO to achieve this.
Whereas the XCH A, <memloc> accomplishes this task with a single instructionand one machine cycle.
Introduction to Embedded Systems
The XCHD A,@Ri (where i = 0 or 1) instruction exchanges the low nibbles between the accumulator
and the data memory pointed by the indirect memory register RO or R1. Both XCH and XCHD are
accumulator specific instructions. The operation oiXCH A, <memloc> and XCHD A,@Ri instructions
are illustrated in Figs 6.8 and 6.9. \
Registers
Memory Data memory
address I
6.2.1.4 External Data Memory Instructions External data memory instructions are used for
transferring data between external memory and processor. The registers involved in external data mem¬
ory instructions are the Data Pointer (DPTR) or the indirect addressing register R0/R1 and the accumu¬
lator. Only indirect addressing works on external data memory operations. If the external data memory
is 16bit wide, a 16bit register DPTR is used for holding the 16 bit address to function as the external
memory pointer. During 16bit external data memory operations Port 0 emits the content of DPT register
(Lower order 8bit address) and Port 2 emits the content of DPH register (Higher order 8 bit address).
Programming the 8051 Microcontroller
If the external data memory is only 8bit wide, either the DPTR or the indirect address register RO
or Rl can be used for holding the 8bit address to function as the external memory pointer. If DPTR is
used as the memory pointer register, Port 0 emits the content of DPL register (Lower order 8bit address)
and Port 2 emits the content of DPH register. If RO or Rl is used as the memory pointer register, Port 0
emits the contents of RO or Rl register (Lower order 8bit address) and Port 2 emits the contents of its
SFR register (P2 SFR). The instruction mnemonic used for external data transfer operation is MOVX
and depending on the memory pointer register the operand will be RO, Rl or DPTR. The accumulator
is the implicit operand in MOVX instruction. The direction of external data memory operation (Read
or write operation) is determined by the use of the accumulator register. If the accumulator register is
used as the source register, the external data memory operation is a Write operation and the WR\ signal
is also asserted. The external data memory operation is a Read operation if the accumulator is used as
destination register. This also generates the RD\ signal.
External data memory related instructions are listed in the table given below.
The following sample code illustrates how to read and write from and to an 8bit external memory.
; Method-1
; Using Indirect register RO
The following sample code illustrates how to read and write data from and to a 16bit external
memory.
Introduction to Embedded Systems
Below is given the Instruction fetch and execute sequence for the MOVXinstruction.
(Fig.6.10) Timing diagram for MOVX instruction when the program memory is external
MOVX is a two machine cycle instruction. The opcode fetching starts at state-1 (SI) of the first ma¬
chine cycle and it is latched to the instruction register. A second program memory fetch occurs at State 4
(S4) of the same machine cycle. Program memory fetches are skipped during the second machine cycle.
This is the only time program memory fetches are skipped. The instruction-fetch and execute sequence
and timing remains the same regardless the physical location of program memory (it can be either inter¬
nal or external). If the program memory is external, the Program Strobe Enable (PSEN) is asserted low
on each program memory fetch and it is activated twice per machine cycle (Fig. 6.10). Since there is
no program fetch associated with the second machine cycle of MOVX instruction, PSEN is not asserted
during this (two PSEN signals are skipped). During the second machine cycle of MOVX instruction, the
address and data bus are used for data memory access.
The lower order address bus is multiplexed with the data bus in 8051 and Port 0 outputs the lower
order address Bus during external memory operations and it is available at S5-S6 states of the first
Programming the 8051 Microcontroller
machine cycle. Since it is available only for a short duration of 1 to 1.5 states, it should be latched. The
trigger for latch is provided by the signal Address Latch Enable (ALE). ALE is asserted twice in each
machine cycle (during SI Phase 2 (S1P2) and S4 Phase 2 (S4P2)). Since data is outputted to or read
from the external memory at State 1 (SI) to State 3 (S3) of the second machine cycle, during a MOVX
instruction, ALE is not emitted at S1P2 of the second machine cycle. Without a MOVX instruction ALE
is emitted twice per each machine cycle and it can be used as a clock signal to any device.
The control signal PSEN is not asserted if the program memory is internal to the chip. However, ALE
signal is asserted even if the program memory is internal.
The external data memory timing for external data memory write is similar to the Read operation
except that the control signal used is WR\ instead of RD\ and the data for writing is available in the place
of 'Data in’ in the above timing diagram . The timing for all signals remains the same.
6.2.1.5 Code Memory Read Instructions Code memory read instruction is used for reading
from the code memory. There is only one instruction for code memory read operation and it is MOVC.
Detailed description of MOVC instruction is given in an earlier section with subtitle “IndexedAddress¬
ing”. Please refer back.
itruction
,, , .. Action
a; a a.\ ;;r
; 1 .y , , ;; . . , any SFRor an internal data memory or an £5 (5 A 1A S' f
internal data memory pointed indirectly
3 , . : :» t 5 by indirect addressing register RO or R!
. ; • ■ .. , . and®. . . , ■
6.2.2.1 Addition and Subtraction The 8051 supports the’addition of 8bit binary numbers and
stores the sum in the accumulator register. The instructions ADD A,<loc> and ADDC A,<loc> are used
for performing addition operations. Both of these instructions are accumulator specific, meaning the
accumulator is an implicit operand in these instructions. The ADD A, <loc> is used for performing a nor¬
mal addition and the cany flag gets modified accordingly on executing this instruction. If the addition
results in an output greater than FFH, the carry flag is set and the accumulator content will be the final
result-FFH + carry. The carry flag is reset when the result of an addition is less than or equal to FFH.
The ADDC A<loc> instruction operates in the same way as that of ADD A,<loc> instruction except
that it adds the cany flag with A and <loc>. The changes to the carry flag on executing this instruc¬
tion remains the same as that of executing the ADD A, <loc> instruction. Executing these instructions
modify the content of accumulator only and <loc> remains unchanged.
The instruction SUBB A, <loc> performs the subtraction operation. The carry flag acts as the borrow
indicator in the subtraction operation. The SUBB A, <loc> instniction subtracts the borrow flag and the
contents pointed by </oc> from accumulator and modifies the accumulator with the result. The content
of <loc> remains unchanged. For performing a normal subtraction operation, first clear the carry flag
Programming the 8051 Microcontroller
and then call the SUBBA, <loc> instruction. The carry (borrow) flag gets modified accordingly on ex¬
ecuting this instruction. If the subtraction results in a -ve number (‘A’ less than (<loc> + Carry)), the
borrow (carry) flag is set and the accumulator content will be. the 2’s complement* representation of the
result (e.g. Accumulator contains OFEH and borrow (carry) flag is 0, executing the instruction SUBB A,
#0FFH results in -1 which is represented in the accumulator by the 2’s complement form of 1. Hence
the content of accumulator becomes FFH (2’s complement form of 1) and the borrow (carry) flag is also
set). The carry flag is reset when the result of a subtraction is +ve (‘A’ is greater than (<loc> + Carry)).
The SUBBA, <loc> instruction is accumulator specific, meaning accumulator is an implicit operand for
this instruction.
Example 1".
The accumulator register contains 80H and B register contains 8FH. Add accumulator content with B register. Explain
the output of the summation and the status of the carry flag after the addition operation.
Adding 80H with 8FH results in 10FH. Since 1OFH requires more than 8 bits to represent, the accumulator holds only
the least significant 8 bits of the result (Flere OFH). The carry bit CY present in the Program Status Word (PSW) register
is set to indicate that the output of the addition operation resulted in a number greater than FFH. Figure 6.11 illustrates
the addition operation and the involvement of Carry bit (CY).
D7 D6 D5 D4 D3 D2 D1 DO
A CY 1 0 0 0 0 0 0 0 80H
ADD A,B +
B 1 0 0 0 1 1 1 8FH
1
jr
CY 1
A 0 0 0 1 1 1 OFH
=1 0
V —r
Example 2
l ' _/
Register RO contains OBH. Add the contents of register R0 with the sum obtained in the previous example using ADDC
instruction. Explain the output of the summation and the status of the carry flag after the addition operation.
The instruction ADDC A, RO adds accumulator content with contents of register RO and the carry flag (CY). Before
executing this instruction, the accumulator contains the sum of previous addition operation, which is OFH and the carry
flag (CY) is in the set state. Register RO contains OBH. Executing the instruction ADDC A, RO adds OFH with OBH and
1. The resulting output is 1BH which is less than FFH. This resets the carry flag. Accumulator register holds the sum. It
should be noted that each addition operation sets or resets the carry flag based on the result of addition, regardless of the
previous condition of the carry flag. Figure 6.12 explains the ADDC operation.
D7 D6 D5 D4 D3 D2 D1 DO
11 1 1
KJ'UKJ'U
The accumulator register contains 8FH and B register contains OFH. The borrow Flag (CY) is in the set state. Subtract
the contents of accumulator with borrow and B register. Explain what is the output of the subtraction and the status of the
borrow flag after the subtraction operation.
8051 supports only SUBB instruction for subtraction. The SUBB instruction subtracts the borrow flag (CY) and the
number to be subtracted, from accumulator. The subtraction operation is implemented by adding accumulator with the
2’s complement of borrow flag and the 2’s complement of the number to be subtracted.
The 2’s complement of borrow flag (CF=1) results in FFH. The 2’s complement of B register content (OFH) yields
F1H. The accumulator content is added with the 2’s complement of borrow flag (8FH + FFH) and the result (8E) is added
withJhe 2’s complement of B register (8EH + F1H). This results in 7FH with the borrow flag (CY) in the set state. The
final step in the subtraction operation is complementing the borrow flag (Carry Flag). A value of 1 for borrow flag after
complementing indicates that the result is negative whereas a value of 0 indicates that the result of subtraction is positive.
If the result is negative, accumulator contains the 2’s complement of the negative number. Figure 6.13 illustrates subtract
with borrow operation and the involvement of borrow bit (CY).
6.2.2.2 Multiplication and Division The instruction MUL AB multiplies two 8bit'unsigned num¬
bers and stores the 16bit result in the register pair A, B. The accumulator stores the lower order 8 bits of
the result and B register stores the higher order 8 bits of the result. The overflow flag (OV) is set when
the product is greater than OFFH and cleared otherwise. The carry flag becomes cleared state irrespec¬
tive of the product.
TheDIVAB instruction divides the content of accumulator register with the content of B register and
stores the integer part of the quotient (A/B) in accumulator and the modulus (A%B) in B register (e.g. If
A contains 03H and B contains 02H, executing the instruction DIVAB loads accumulator with 01H (3/2)
and B register with 01H (3%2). 8051 does not have a divide by zero interrupt mechanism to indicate a
divide by zero error. However 8051 indicates the divide by zero condition by setting the overflow flag
{OV) in the Program Status Word (PSW) register. The values returned in A and B are undefined for a
divide-by-zero operation.
The overflow flag (OV) and carry flag (CY) are cleared for normal division operation.
MUL and DIVinstructions are accumulator and register B specific instructions. They work only with
these two registers. If register B is not used for multiplication and division, it can be used for temporary
storage.
Programming the 8051 Microcontroller
l's Complement of
Carry
2's Complement of
Carry = l's
Complement of
Carry + 1
1111 1111
•UWXJ •UKJ'UKJ
Convert the binary number FFH present in the accumulator to unpacked BCD and store the resultant 3 digit BCD number
in consecutive memory locations starting from 20H
Introduction to Embedded Systems
BCD numbers are numbers with base 10. BCD numbers use digits 0 to 9 for representing a number. They are normally
used for representing a meaningful decimal number. Each digit in the BCD number represents the consecutive powers
of 10. For example, the BCD number 255 is interpreted as 2 x 102 + 5 x 101 + 5 x 10°. Binary numbers follow the base
2 numbering system. Regardless of the numbering system, digital systems internally represent the numbers in binary
format. For an 8bit word length controller/processor, the maximum number value that can be represented in binary' is
11111111, which is equivalent to FFH in hexadecimal and 255 in decimal. For retrieving the digits of a decimal number,
the number is divided with 10 repeatedly until the quotient is zero. The remainder from each division gives the succes¬
sive LSB digits of the corresponding BCD number. For example, for 255, the first division gives the quotient as 25 and
remainder as 5, hence the LSB digit for the BCD number is 5. The next division of the quotient by 10 gives 2 as quotient
and 5 as remainder. The remainder 5 forms the next LSB digit for the BCD. Performing one more division with 10 on
the quotient (2) gives 0 as quotient and 2 as remainder. This remainder acts as the MSB digit for the corresponding BCD
number. In fact for an 8bit binary number, only two divisions are required. The remainders from the two consecutive divi¬
sions form the consecutive LSB digits for the BCD number and the quotient after the second division becomes the MSB
digit for the corresponding BCD number. The following assembly code implements this principle. The DIVAB instruction
performs the successive divisions with 10.
Convert the packed BCD number ‘98’ stored in the accumulator to corresponding binary number and store the result in
accumulator.
The packed BCD number is stored using a single byte. The higher 4 bits of the byte store the most significant digit of
the packed BCD number, and the lower 4 bits store the least significant digit of the BCD number. Hence the accumulator
representation of paeked BCD is 10001000b (98H). The binary value corresponding to this packed BCD is the binary
representation of 9 x 10! + 8 x 10° = 9 x 10 + 8 x 1 = 90 + 8.
ft ;######### # #.§## I# #f’jl #:.# #,######### fr# #•###### t ft# # #'# # f # I#################
t /packedocd__to_ bi nary. src ■■ ■ • ■■ IT t. ', TjjXfe.
/Firmware, for converting packed BCD to binary
Ty-The BCD number to be converted is present Iri accumulator ‘ . 1;.
/The resultant .binary number is held in; Accumulator register , ■••.fen?
•/.Written & assembled for A51 -Assembler . :
/Written by Shibu -K V. Copyright (C) .2008 . . ,
6.2.2.3 Increment and Decrement The increment instruction (INC) increments the content of
the location pointed by the operand. The INC A instruction is an accumulator specific instruction which
increments the content of accumulator. Executing the instruction INC A will not modify the cany flag.
The INC <loc> instruction increments the content pointed by <loc>. If the content pointed by the
Introduction to Embedded Systems
operand of /ACjnstruction is FFH, on executing the INC instruction, the content simply rolls over to
00H. None of' the status bits are modified to indicate this rollover. INCDPTR instruction is DPTR regis¬
ter specific instruction and it increments the content of 16bit DPTR register by one. The following piece
of assembly code illustrates the usage of INC instruction.
cniory Location ,00H (RO) wi th 8Of?
iSili
The decrement instruction (DEC) decrements the content of the location pointed by the operand. The
DEC A instruction is an accumulator specific instruction which decrements the content of accumulator.
Executing the instruction DEC A will not modify the carry flag. The DEC <loc> instruction decrements
the content pointed by <loc>. If the content pointed by the operand of DEC instruction is 00H, execut¬
ing the DEC instruction simply rolls over it to FFH. None of the status bits are modified to indicate this.
The following code snippet illustrates the usage of DEC instruction.
MOV ,O0H,f80H ; .•/. Load
va;
Data .'.memory,
- - \-
location
. ;•>»
oOOH . (RO) with .8OH
'-y; ; .... •• s
There is no specific instruction to decrement the DPTR register content by one in 8051 instruction
set. However decrementing the DPTR can be achieved by using other instructions in the optimal way.
6.2.2.4 Decimal Adjust The DA A instruction adjusts the accumulator content to give a meaningful
BCD result in BCD arithmetic. Binary Coded Decimal (BCD) is a number representation in numeric
systems. In the BCD number system, numerals from 0 to 9 are used for representing a number. Based
on the number of BCD digits represented in a single byte, the BCD numbers are known as packed and
unpacked BCDs. In the unpacked BCD representation, a single BCD digit (0 to 9) is represented by a
single byte whereas in the packed BCD representation two BCD digits (00 to 99) are represented using
a single byte. ADD and ADDC instructions used for BCD addition should follow a DA A instruction to
bring the Accumulator content to a meaningful BCD result. It should be noted that the DA A instmction
will not convert a binary number to a BCD. The DA A instruction always follow the ADD instruction.
Example 1
Write an assembly program to add two packed BCD numbers ‘28’ and T2’ stored in consecutive memory locations start¬
ing from data memory address 50H. The result of the addition should be adjusted for BCD.
The Assembly code snippet shown below illustrates the implementation.
Programming the 8051 Microcontroller
;BCD_addition.src
;Firmware for adding two packed BCD :
' ;The BCD numbers to be added'are pre
W$m
Sgggjfcfv *-£«fe3? v
If the accumulator is not adjusted for decimal, after the addition, the accumulator content will be
3AH. Adjusting the accumulator for decimal, after the addition operation, produces a meaningful result,
40H (Which is the sum of the BCDs 28 and 12).
80H
80H
Similarly the ORL instruction performs the bitwise OR'mg operation. For example, let us assume that
the accumulator contains 01H and it is ORqd with an immediate data 80H. The result will be 81H and it
is stored in the accumulator. It is explained in Fig. 6.15.
:
Accumulator 0 0 0 0 1 01H
° 0
‘OR’ operation 1 i 1 1 1 1 1 1
Constant 1 0 0 0 0 0 0 0 80H
Result in
1 0 0 0 0 0 0 1 81H
accumulator
The XRL instruction performs the bitwise XORmg operation. The principle of XOR is that if both the
XORmg bits are same (both are either 0 or 1) the output bit is 0 and if the XORing bits are complement
to each other the output bit is 1. For example, if the accumulator content is 81H and an XOR operation
of the accumulator with an immediate constant 80H yields 01H as output. The XRL instmetion operation
is explained in Fig. 6.16.
The CLR A and CPL A instructions are accumulator specific instructions. The CLR A instruction
clears the content of accumulator (loads accumulator with 00H), whereas the CPL A instruction comple¬
ments the accumulator content (negates the accumulator content). Both CLR A and CPL A instructions
are single machine cycle instructions.
Assuming accumulator contains FOH, Fig. 6.17 illustrates the operation of CLR A and CPL A
instructions.
Rotate instructions rotates the bits of the Accumulator. All Rotate instructions are Accumulator spe¬
cific instructions and Accumulator is the implicit register for rotate operations. Accumulator bits can be
either rotated to left or right.
The RL A instruction rotates the accumulator bits to the left by one position and the MSB of the ac¬
cumulator comes in place of the LSB. For example, the accumulator contains 80H, after executing an
RLA instruction, the contents of the accumulator will become 01H. It is illustrated in Fig. 6.18.
The RLC A instruction is similar in operation to RL A instmetion except that in RLC A instmetion the
MSB of the Accumulator is shifted to the Carry bit (CY) and the content of carry flag at the moment
Programming the 8051 Microcontroller
Accumulator 1 1 1 1 0 0 0 0 FOH
Accumulator FOH
‘CPL’ operation
OFH
fFigT6.17 Illustration of (a) CLR A instruction operation (b) CPL A instruction operation:
of execution of the RLC A instruction is shifted to the LSB of Accumulator. The operation of RLC A
instruction is illustrated in Fig. 6.19. Assuming the Accumulator contains 80H and the carry bit is in
the cleared state (0). Executing the RLC A instruction sets the carry flag and modifies the Accumulator
content to 00H.
One more execution of the RLC A instruction .will change the accumulator content to 01H and resets
the cany^ flag.
Introduction to Embedded Systems
The RR A instruction rotates the accumulator bits to the right by one position and the LSB of the
accumulator comes in place of the MSB. For example if the accumulator contains 01H, after executing
the RR A instruction, the contents of the accumulator will become 80H (Fig. 6.20).
Accumulator 0 0.0 0 0 0 0
Result in Accumulator
after RR A instruction 1 0 0 0 0 0 0
execution
The RRC A instruction is similar to RLC A instruction except that in RRC A instruction the LSB of
the accumulator is shifted to the carry bit (CY) and the content of carry flag at the moment of execution
of RRC A instruction is shifted to the MSB of the accumulator. Assuming that the accumulator contains
01H and the carry bit is in the set state (1). Executing the RRC A instruction sets the carry flag and
modifies the accumulator content to 80H (Fig. 6.21).
The SWAP A instruction interchanges the lower and higher order nibbles of the Accumulator. Assum¬
ing the accumulator contains F5H, executing SWAP A instruction modifies the accumulator content to
5FH.
The SWAP A instruction is very helpful in BCD to binary conversion.
Accumulator F5H
Result in Accumulator
after SWAP A instruction 1 1 5FH
execution J L
Higher Nibble Lower Nibble
The cany bit ‘C\ present in the Special Function Register PSW, takes the role of accumulator in all
bit related operations. The various Boolean instructions supported by the 8051 CPU are listed below. It
is to be noted that all bit access is through direct addressing only.
Moves carry
Specified Bit
Logical Operations on Bi
Logical AND Carry flag with Bit and store the result
ANL C, Bit C = C & Bit 2*(fnq'/12)
in Cany flag . ...Pi./
" CiE& ■’.Logical;AND the complemented valu
ANL C,/Bit
(.NOT. BitT);, Carry flag and store the result in Carry
_ . . Logical OR Carry flag with Bit and store the result
ORL C, Bit C-
= C | Bit . „ „ •* .
tn Carry flag 2*(fosc/12)
.... . C-- C | (.NOT. Logical OR the complemented value of Bit with ' ■’ .
;/ 1 Bit i f) Carry flag. Result in Carry flag / .2%Sc/12)
1 The actual state of the bit remains unchanged. If bit OH is in the cleared state, executing the instruction ANL C, /OH will not change
the state of bit OH to set.
W The actual state of the Bit remains unchanged. If bit OH is in the cleared state, executing the instruction ORL C, /OH will not change
the state of bit OH to set.
Introduction to Embedded Systems
‘ Tf(C)
Jump to the relati ve address ‘rel’ if Carry flag is 1
rel
, „ • Mb .
The Boolean instructions are very useful in bit manipulation operation for bit-addressable SFRs.
Manipulation of port status registers (PO to P3) is a typical example. The usage of Boolean instructions
is illustrated below.
MOV C, P2.0 •- ;?.Load carry flag with Port-pin, 2.0' s status.
MOV P2.0, C ; Load Port 2.0 latch with content of carry flag
SETB C ; Set the carry flag " - ' _■ >
CLR C ; 'Clear carry flag, ,>r- '
ANL C, P.2.0 ; Logical AND-P2.0 Latch bit---with .Garry flag and
; load Carry bit with the result
The JC rel and JNC rel instructions has got special significance in comparison operation. The 8051
supports only one compare instruction, CJNE, which checks only for the equality of the register/data
pair compared. There is no built in instruction for checking above or below condition (Compare and
jump if above and Compare and jump if below). These conditions can be checked by using the JC and
JNC instructions in combination with the CJNE instruction. The CJNE instruction followed by the JNC
instruction implements the Compare and jump if above condition and the CJNE instruction followed by
the JC instruction implements the Compare and jump if below condition. The following code snippet
illustrates the same. Assume that the accumulator content is 50H and it is compared against 51H.
//Implementation of Compare and Jump if above
CJNE A, #51H,above?
; Check whether accumulator content is greater than 51H
; The Carry flag is cleared if Accumulator >= const
above?: JNC acc_high
; Accumulator < 51H. Do the rest of processing here
acc_high: ; Accumulator >= 51H. Do the rest' of processing here
S - A iffi 1 -1
r L*. ■
m K~-,V
•'
!■ r’ ^
I
- M ■Bii HU ' ‘ A h. ,i 8 IE .1 , w ~F 1
-: -- mr+r/"
- --i ' ■: - v “
1 iBB # k
m
M . -r CV.CU "’ r - c_
Wi
9
Jump Instructions The instruction JMP represents three type of jumps. They are SJMP, LJMP and
AJMP and they are used in appropriate contexts. If the programmer is not interested in analysing the
contexts, he/she can simply use the JMP instruction.
SJMP instruction stands for Short Jump. The destination address is given as an offset to the current
address held by the Program Counter (PC). The SJMP instruction is a two byte instruction consisting of
the opcode for SJMP and the relative offset address which is one byte. The jump distance for the SJMP
instruction is limited to the range of-128 to +127 bytes relative to the instruction following the jump.
LJMP instruction stands for Long Jump. The destination address for LJMP is given as the real physi¬
cal address of the code memory location to which the jump is intended. The LJMP instruction is a three
byte instruction consisting of the opcode for LJMP and the two byte physical address of the location to
which the program flow is to be diverted. The jump location can be anywhere within the 64K program
memory.
The third type of jump instruction AJMP stands for Absolute Jump. The AJMP instruction encodes
the destination address as 11-bit constant, The instruction is two byte long consisting of the opcodc,
which itself contains higher 3, bits of the 11-bit cpnstant. Rest 8 bits of the destination address is held by;
the second byte of the instruction. When an AJMP instruction is executed, these 11 bits are substituted
for the lower 11 bits in the "Program Counter (PC). The higher 5 bits of the Program Counter remains
the same. Hence the destination will be within the same 2K block of the current instruction. In general
AJMP is used for jumping within a memory block.
The programmer need not bother about handling these three jumps, what he/she can do is simply
provide the destination address as label or a 16 bit constant. The assembler converts the JMP instruction
to any of the three jump instruction depending on-the context.
Introduction to Embedded Systems
Case jumps for diverting program execution flow dynamically can be implemented using JMP
@A+DPTR instruction. The destination address is computed during run time as the sum of the DPTR
register content and accumulator. Normally DPTR is loaded with the address of a jump table which
holds jump instructions to the memory location corresponding to a ‘case’. For an n-way branching
requirement, the accumulator is loaded with 0 through n-1. The following code snippet illustrates the
implementation of a switch case statement using JMP @ A +DPTR.
//switch case in
switch (var)
CaseO:
// Action.
Case!:
)■/.
Case?:: J] *
// Action.
Locations CASEO, CASE1 and CASE2^ should contain the action item to be implemented for each
case statement.
The subroutine calling instruction CALL is of two types; namely LCALL and ACALL. LCALL is a
three byte instruction of which the first byte represents the opcode and the second and third bytes hold
the physical address of the program memory where the- subroutine, which is to be executed, is located.
For LCALL the subroutine can be anywhere within the 64K byte of program memory. ACALL is similar
to AJMP. ACALL is a two byte instruction of which the first byte holds the opcode as well as the higher
3 bits of the 11-bit address of the location where the subroutine is located. The second byte holds the
remaining 8 bits of the address location. ACALL is used for calling a subroutine which is within the 2K
block of the instruction, calling the subroutine.
RET instruction performs return from a subroutine call. Executing the 'RET instruction brings the
program flow back to the instruction following the CALL to the subroutine. RETI performs the return
from an interrupt routine. RETI instruction is similar to the RET instruction in all respect except that the
RETI informs the interrupt system that the interrupt in progress is serviced.
Programming the 8051 Microcontroller
The NOP instruction does not perform anything specific. It simply forces the CPU to wait for one
machine cycle. It is very useful for generating short waits. It simply eats up the processor time. For
example, if you need a short delay between the toggling of a control line/device select line or port pin,
simply add some NOP instructions. The following code snippet illustrates the usage of NOP instruction
in programming.
6.2.5.2 Conditional Program Control Transfer Instructions The conditional program control
transfer instructions transfer the program flow depending on certain conditions. The program flow is
diverted from the normal line only if a specified condition is met. The conditional program control trans¬
fer instructions supported by 8051 are listed below.
ruction
emonic
--"0)' " Jump to' the relative address fief if
‘rel’ Accumulator content is zero. V • -
0) ' Jump to the relative address ‘rel’ if
rel Accumulator content is non zero.
Decrement the contents of <loc> and
)C> 1 jmnps to the relative address ‘rel’ if
DJNZ <loc>, rel !-•()) content of <loc> is non zero <loe>
‘rel’ can be a direct memory or scratchpad
. registers R0 through R7 or any SFR.
Compares the content of: Accumulator
. . with the content, of <160 and jumps
‘rel’ to the relative address rel if both
-are not equal. <loc> can be a direct
? + memory' or an immediate constant.
Compares the content of <loc> with
an immediate constant and jumps to
CJNE<loc>, If (<loc>!=#data) the, relative address ‘rel’ if both are
2*(fosc/i2)
#data, rel Jump to‘rel’ not equal. <loo> can. be a memory
pointed indirectly or register R0
through R7 or. Accumulator.
All the conditional branching instructions specify the destination address by relative offset method
and so the jump locations are limited to -128 to +127 bytes from the instruction following the condi¬
tional jump instruction. Even if the user specifies the actual address, the assembler converts it to an
offset at the time of assembling the code.
Unlike the 8085 and 8086 CPU architecture, there is no zero flag for 8051. The instructions JZ and
JNZ instructions implement the zero flag check functionality by checking the accumulator content for
zero. DJNZ instruction is used for setting up loop control and generating delays. CJNE instruction com¬
pares two variables and check whether they are equal. CJNE instruction in combination with the carry
Introduction to Embedded Systems
flag can be used for testing the conditions ‘greater than’, ‘greater than or equal’ and ‘less than’. The two
bytes in the operand field of CJNE are taken as unsigned integers. If the first is less than the second,
the Carry bit is set (1). If the first is greater than or equal to the second, the carry bit is cleared. Boolean
instructions are also used for conditional program control transfer. Please refer to the “Program Control
Transfer based on bit status” under the Boolean Instructions section for more details.
Please refer to the Online Learning Centre for the Complete 8051 Instruction Set reference
Summary
Z An Instruction consists oftwo parts namely; Opcode and Operand(s). The Opcode tells the processor'what, to do /
on executing an instruction. Operand(s) is the parameter(s) required by opcode to complete the action i s
Z , Addressing1-Mode refers to the way in which an operand is specified in an instruction along withthe opcode
Z Direct Addressing, Indirect Addressing, Register Addressing, Immediate Addressing and Indexed Addressing are
. the addressing modes supported by 8051 '■/ " [
/' Instructions in which the register operand is implicitly specified by some bits of the opcode referred as register ’
Instructions, whereas instructions which implicitly work on certain specific, registers are known as'-Register
Specific Instructions ,.r 4
Z: The instruction set of 8051 family microcontroller is broadly classified into five categories, namely; Data transfer
, instructions. Arithmetic instructions, Logical instrimtions, Boolean instructions arid Program Control Transfer' ’
instructions . . ’ “ •L ; - V ’.. . .
Z- ‘Stack’is an internal memory for storing variables.temporarily. The instruction PUSH saves data to the stack and
the instruction POP retrieves the pushed data from.the stack memory . i. 1 |
Z Data transfer instructions transfer data between a source and destination. The data transfer can be between
register or memory (internal or external). Arithmetic instructions perform arithmetical operations like addition, A
subtraction, multiplication, division, increment.and decrement -■ ,, . -
Z Logical instructions perform logical operations such as ‘ORirig’, ‘ANDing’, ‘XORing’, complementing, cle,ar- •
: ing, bit rotation and swapping nibbles of a byte, etc |§i C
Y Boolean instructions perform various operations like bit transfer, bit manipulation, logical operations on bits,
program control transfer based on bit state, etc. The carry bit ‘C’, present in the Special Function Register PSW,
takes the role of Accumulator in all bit related operations •■ T
Z Program Control transfer instructions change the program execution flow. The 8051 instruction set supports two
types of program control transfer instructions namely; Unconditional program control instructions and Condi-
tipnal program control instructions
Z The unconditional program control instructions transfer the program flow to any desired location in the code
memory. JMP,. CALL, RET, RETI and NOP are the unconditional program control transfer instructions in 8051
irjstruction set
Z The conditional program control transfer instructions transfer the program flow depending on certain conditions.
The program flow is diverted from the normal line only if a specified condition is met. Jump on Zero (JZ), Jump
on! Non Zero (JNZ), Decrement and Jump if Non Zero (DJNZ), Compare and Jump if not equal .(CJfirk), Jump
. if specified bit is set (JB), Jump if specified bit is not set (JNB), Jump if the bit is set and clear (lie bit (JBC),
Jump if Carry flag is set (JC) and Jump if Carry flag is not set (JNC) are the conditional program control transfer
instructions in 8051 . ,
Z The DJNZ instruction is used for setting up loop control and generating delays. CJNE instruction compares two
variables and check whether they are equal. CJNE instruction in combination with the Carry flag can be used for
testing the conditions ‘greater than’, ‘greater than or equal’ and ‘less than’ J
Programming the 8051 Microcontroller
W Keywords ]
Opcode : The command that tells the processor what to do on executing an instruction
Operand(s) : The parameters) required by an opcode to complete the action
Addressing Mode : The way in which an operand is specified in an instruction along with opcode
Direct Addressing : The addressing mode in which the operand is specified as direct memory address .
Indirect Addressing : The addressing mode in which the operand is specified as memory address indi¬
rectly, using indirect addressing registers
Register Addressing : Addressing mode in which the operand is specified as Registers
Register lnstrndSons‘ ' : Instructions in which the register operand is implicit! y specified by some bits of the
.-opcode; .f.*}, g -,j■
Register Specific Instructions : Instructions which implicitly work on certain specific registers only
immediate Addressing : Addressing mode in which the operand is specified as immediate constants
Indexed Addressing : Addressing mode in which the operand is specified ffiroi
Data Transfer Instructions : Instructions for transferring data between a source and d
Stack " : -‘! : Internal memory for storing vanabfSTtemp^^^‘ _ J
PUSH : Instruction for pushing data to the stack memory
pop ::;"a u v : Instruction for retrieving the pushed data bytes firoin stack memory
Data Exchange Instructions : Instructions for exchanging data between a memory location anddhe accumulator;
register in 8051 architecture T . : -. ; . ;■ §£ 1
External Data Memory : Instruction foi transferring data between external memory and processor
• .... c~ cc ** » 4 ^ - * *-*, Ps .
Instruction / % >' \
!1 X -r t i VT£ *' 'XT'< 1-¥ ■
Arithmetic Instructions : Instructions for performing basic arithmetic operations including addition, subtrac¬
tion, multiplication, division, increment and decrement
2’s Complement : A binary data repp:sentati<m used in subtraction operation
Binary Coded Decimal (BCD) Numbers with base 10 Represented usmg digits 0 toy
Unpacked BCD A single BCD digit (0 to 9) represented in a single byte - ...
Packed BCD Two BCD digits (00 to 99) represented using a single byte
Decimal Adjust Accumulator An instruction for adjusting the accumulator content to give a meaningful BCD,”
(DAA) after BCD arithmetic
Logical Instructions Instmctions for performing logical operations such as lORing’, ‘ ANDing’, ‘XOR-
ing’, complementing, clearing, bit rotation and swapping nibbles of a byte, etc
Boolean Instructions Instructions for performing various operations like bit transfer bit manipulation,
logical operations on bits, program control transfer based on bit state, etc
Objective Questions
V
13. The Program Strobe Enable (PSEN) signal is asserted during program fetching if
(a) The program memory is external to the controller
(b) The Program memory is internal to the controller
(c) The Program memory is either internal or external to the controller
14. How many program fetches occur per machine cycle?
(a) 1 (b) 2 (c) 3 (d) 4
15. How many ‘program memory-fetches’ are skipped during the execution of MO ITT instruction?
(a) 1 - (b) 2 (?) 3 (d) 4
16. The Address Latch Enable (ALE) signal is asserted how many times in a machine cycle?
(a) 1 (b) 2 (c) 3 '(d) 4
17. How many times the ALE signal is skipped during the execution of a MOVX instruction?
(a) 1 (b) 2 (c) 3 (d) 4
18. Which of the following is true about MO VC instruction
(a) Used for reading from Program memory (b) Uses Indexed Addressing technique
(c) Botha&b (d) None of these
19. The content of Accumulator is FFH and the Carry Flag is in the cleared state. What will be the contents of Accu¬
mulator and carry flag after executing the instruction ADD A,#l
(a) Accumulator = 01H; Carry flag = 1 (b) Accumulator = 01H; Carry flag = 0
(c) Accumulator = 00H; Carry flag = 0 (d) Accumulator = 00H; Carry flag = 1
20. Accumulator register contains OFH and the Carry flag CY is in the set state. What will be the state of Carry flag
after executing the instruction ADD A, UOFOH
(a) 1 (b) 0 (c) Indeterminate
21. The content of the accumulator is FFH and the Carry flag is in the cleared state. What will be the contents of the
accumulator and carry flag after executing the instruction ADDC A, #1
(a) Accumulator = 01H; Carry flag = 1 (b) Accumulator = 01H; Carry flag = 0
(c) Accumulator = 00H; Carry flag = 0 (d) Accumulator = 00H; Carry flag = 1
22. Accumulator register contains OFH and the Carry flag CY is in the cleared state. What will be the contents of Carry
flag and Accumulator after executing the instruction SUBB A,#OFOH
(a) Accumulator = E1H; Carry flag = 1 (b) Accumulator = E1H; Carry flag = 0
(c) Accumulator = 1FH; Carry flag = 0 (d) Accumulator = 1FH; Carry flag = 1
23. Accumulator register contains FOH and the Carry flag CY is in the cleared state. What will be the contents of the
Carry flag and the accumulator after executing the instruction SUBB A,#0FH
(a) Accumulator = E1H; Carry flag = 1 (b) Accumulator = E1H; Carry flag = 0
(c) Accumulator = 1FH; Carry flag = 0 (d) Accumulator = 1FH; Carry flag = 1
24. Accumulator register contains OFFH and the B register contains 02H. What will be the contents of the Accumula¬
tor and B register after executing the instruction MULAB
(a) Accumulator = OFEH; B = 01H (b) Accumulator = 00H; B = OFEH
(c) Accumulator = OFEH; B = 00H • (d) Accumulator = 01H; B = OFEH
25. Accumulator register contains OFFH, B register contains 02H and the Carry flag is in the cleared state. What will
be the contents of the Carry flag and Overflow flag after executing the instruction MUL AB
(a) Carry flag = 1; Overflow flag = 0
(b) Carry flag = 0; Overflow flag = Remains same as the previous value
(c) Carry flag = 1; Overflow flag = 1
(d) Carry flag = 0; Overflow flag = 1
26. Accumulator register contains OFFH, B register contains 02H and the Overflow flag is in the cleared state. What
will be the contents of the Carry flag and Overflow flag after executing the instruction AFUL AB
(a) Carry flag = remains same as the previous value; Overflow flag - 0
(b) Carry flag = remains same as the previous value; Overflow flag = 1
(c) Carry flag = 1; Overflow flag = 1
(d) Carry flag = 0; Overflow flag = 1
ME Introduction to Embedded Systems
27. Accumulator contains OFFH and the B Register contains 02H. What will be the contents of the accumulator and B
register after executing the instruction DIVAB
(a) Accumulator = 01H; B = 7FH (b) Accumulator = 7FH; B = 01H
(c) Accumulator = 7FH; B = 00H (d) Accumulator = 00H; B = 7FH
28. Accumulator register contains OFFH and the B register contains OH. What will be the contents of the accumulator,
B register and Overflow flag after executing the instruction DIVAB
(a) Accumulator = 00H; B = 00H; Overflow flag = 1
(b) Accumulator = OFFH; B = 00H; Overflow flag = 0
(c) Accumulator = Undefined; B = Undefined; Overflow flag = 1
(d) Accumulator = Undefined; B = Undefined; Overflow flag = 0
29. Accumulator register contains OFFH and the Carry flag CY is in the cleared state. What will be the contents of
Carry flag and the accumulator after executing the instruction INC A
(a) Accumulator = 00H; Carry flag = 1 (b) Accumulator = OOH; Carry flag = 0
(c) Accumulator = 01H; Carry flag = 0 (d) Accumulator = 01H; Carry flag = 1
30. Accumulator register contains OOH and the Carry flag CY is in the cleared state. What will be the contents of Carry
flag and Accumulator after executing the instruction DEC A
(a) Accumulator = OOH; Carry flag = 1 (b) Accumulator = OOH; Carry flag = 0
(c) Accumulator = FFH; Carry flag = 0 (d) Accumulator = FFH; Carry flag = 1
31. DPTR contains 2050H. What will be the content of DPTR after executing the following instructions
XCH A, DPL
DEC A
CJNE A, itOFFH, skipjec
DEC DPH
skip dec:
XCH A, DPL
(a) 2050H (b) 2049H (c) 2051H (d) 204FH
32. Accumulator register contains OFH. What will be the content of the accumulator after executing the instruction DA A
(a) OFH (b) 15 H (c) 15 (d) OOH
33. Accumulator register contains the BCD number 28. What will be the content of the accumulator after executing the
instruction ADD A, #12H
(a) 40H (b) 3AH (c) 40 (d) None of these
34. Accumulator register contains the BCD number 28. What will be the content of the accumulator after executing
the following instructions
ADD A, #1211 '
DAA
(a) 40H (b) 3 AH (c) 40 (d) None of these
35. Accumulator register contains OFH. What will be the content of the accumulator after executing the instruction
ORLA, MFOH
(a) OFH (b) F0H (c) FFH (d) OOH *
36. Accumulator register contains OFH. What will be the content of the accumulator after executing the instruction
ANLA, WF0H
(a) OFH (b) F0H . (c) FFH (d) OOH
37. Accumulator register contains 0AAH. What will be the content of the accumulator after executing the instruction
XRL A, WD5H
(a) AAH (b) 7FH (c) D5H (d) FFH
38. Accumulator register contains 7FFI and carry flag is in the set state. What will be the contents of Accumulator and
carry flag after executing the instruction CLR A
(a) Accumulator = 7FH; Carry flag = 0 (b) Accumulator = 7FH; Carry flag = 1
(c) Accumulator = OOH; Carry flag = 0 (d) Accumulator = OOH; Carry flag = 1
Programming the 8051 Microcontroller
39. What changes will happen on executing the instruction CLR 00H
(a) The code memory location 00H becomes OOH
(b) The data memory location OOH becomes OOH
(c) The external data memory location OOH becomes OOH
(d) The LS Bit of the data held by data memory location 20H becomes 0
40. Accumulator register contains OFH and carry flag is in the set state. What will be the contents of the Accumulator
and carry flag after executing the instruction CPL A
(a) Accumulator = OFH; Carry flag = 0 (b) Accumulator = OFH; Carry flag = 1
(c) Accumulator = FOH; Carry flag = 0 (d) Accumulator = FOH; Carry flag = 1
41. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of Accumulator and
carry flag after executing the instmction RL A
(a) Accumulators 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 80H; Carry flag = 0 (d) Accumulator = 80H; Carry flag = 1
42. Accumulator register contains 01H and'carry flag is in the set state. What will be the contents of Accumulator and
carry flag after executing the instruction RLC A
(a) Accumulator = 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 03H; Carry flag = 0 (d) Accumulator = 03H; Carry flag = 1
43. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of the accumulator
and carry flag after executing the instruction RR A
(a) Accumulator = 02H; Carry flag = 0 (b) Accumulator = 02H; Carry flag = 1
(c) Accumulator = 80H; Carry flag = 0 (d) Accumulator = 80H; Carry flag - 1
44. Accumulator register contains 01H and carry flag is in the set state. What will be the contents of the accumulator
and carry flag after executing the instruction RRC A
(a) Accumulator = 80H; Carry flag = 0 (b) Accumulator = 80H; Carry flag = 1
(c) Accumulators 03H; Carry flag = 0 (d) Accumulator = 03 H; Carry flag = 1
45. Accumulator register contains 5FH. What will be the content of the accumulator after executing the instmction
SWAP A
(a) OOH (b) F5H (c) 5FH (d) OOH
46. What changes will happen on executing the instmction CPL OOH
(a) The code memory location OOH becomes OOH
(b) The data memory location OOH becomes OOH
(c) The external data memory location OOH becomes OOH
(d) The LS Bit of the data held by data memory location 20H is complemented
47. The carry bit is in the set state and the port status bit P 1.0 is in the cleared state. What v)ill be the values of Carry
bit and P1.0 after executing the instmction ANL C, /P 1.0
(a) Carry flag = 0; P 1.0 = 0 (b) Carry flag = 0; P 1.0 = 1
(c) Carry flag = 1; P1.0 = 0 (d) Carry flag - 1; P1.0 = 1
48. The Carry bit is in the cleared state and the Port status bit P 1.0 is in the cleared state. What will be the values of
Carry bit and P1.0 after executing the instmction ORL C, /P1.0
(a) Carry flag = 0; P 1.0 = 0 (b) Carry flag = 0; P1.0 = 1
(c) Carry Flag = 1; P1.0 = 0 (d) Carry flag = 1; P1.0 = 1
49. Which of the following Jump instmction is the optimal instmction if the offset (relative displacement of jump loca¬
tion from the current instmction location) of the jump location is greater than 127 and less than -128 and is within
the same 2K block of the current instmction
(a) AJMP '(b) SJMP (c) LJMP (d) All of these
50. The program execution needs to be diverted to a location within the 2K memory block of the current instmction
and the 11 least significant bits of the code memory .address to which the jump is intended is 050FH. The AJMP
instmction is used for implementing the jump. What is the machine code for implementing the AJMP instruction
for jumping to the specified location?
(a) 01H, OFH, 05H (b) 01H,05H,0FH (c) A1H, OFH (d) None of these
Introduction to Embedded Systems
51. Which of the following Jump instruction encodes the jump location as absolute memory address
(a) SJMP (b) AJMP . (c) LJMP (d) All of these
(e) only (b) and (c)
52. All the conditional branching instructions specify the destination address by
# (a) Relative offset method (b) Absolute address method
(c) Either relative or absolute address method (d) None of these
Review Questions
1. Explain with examples the different addressing modes supported by 8051 CPU
2. What is the difference between Register Instructions and Register Specific Instructions? Give an example for
each
3. What is the difference between Immediate addressing and Indexed addressing? State where these addressing tech¬
niques are used. Give an example for each.
4. What is Data transfer instruction? Explain the different data transfer instructions supported by 8051 CPU
5. Explain the arithmetic operations/instructions supported by 8051 CPU
6. Accumulator register contains 01H and Carry flag is in the state. What will be the contents of accumulator and
Carry flag on executing the instruction
RRA
RRCA
7. Examine the following piece of code and explain whether the program will work in the expected way. Justify your
statement
MOVPl.O, PI.2
8. In the following code snippet, the jump location is intended to a memory address with offset beyond +127. Suggest
a workaround to solve this issue —
CJNE A.tiOlH, JUMP HERE
;The relative address of label ‘ JUMP_HERE ' is greater than 127 JUMPJJERE: XCH A,B
9. Explain the stack memory related data transfer instructions in detail
10. Explain the data exchange instructions in detail with examples
11. Explain the timing diagram for the MOVX instruction execution when the program memory is internal to the
processor >
12. Explain the difference between ADD, ADDC and DAA instructions. Explain the significance of DAA instruction?
13. Explain the instruction for division operation. How is a divide by zero condition handled in the 8051 architecture?
14. Explain the different logical operations supported by 8051 and the corresponding instruction in detail
15. Explain the different Boolean instructions supported by 8051
16. Explain the different unconditional program control transfer instructions supported by 8051
17. Explain the different conditional program control transfer instructions supported by 8051
18. Explain the implementation of switchQ case statement using jump tables
19. The 8051 status register doesn’t contain a zero flag. Explain how the 8051 architecture implements the Jump on
zero and Jump on non-zero conditions
20. Explain the difference between LCALL and ACALL for subroutine invocation? Which one is faster in execution
and why?
21. Explain the difference between RET and RET1 instructions
22. Explain the different types of jumps [supported by 8051 architecture. Which one is faster in execution and why?
Programming the 8051 Microcontroller
Lab Assignments
A lookup table with 6 bytes is storedin cbde memory location starting from 800011. Write a small 8051 assembly
program to read the lookup table . ; • 1
Implement a BCD counter to count from 00 to 99 with a delay of 1 second between the successive mounts.
Display the count using two 7-Segment LED displays in multiplexed configuration ' ’ '’ '
Optimise the following piece of code (Rewrite the code with iristructions/opferations giving the same
1 functionality) for memory size and performance '
4. Write a 8051 assembly language program using timer interrupt for generating a 50 Hz square waveiatme portpin
f; PI.0. Thenscillator frequency applied to the microcontroller is 12.00MHz .
5. Write a 8051 assembly language program for generating a 5 kHz square wave at the pop pin PI.0. The
-• oscillator frequency applied to the microcontroller is 12.00 MHz. >■ ;
6. Write a 8051 Assembly language program to generate the Fibonacci series of a given number: Assume the
number whose Fibonacci number is to be calculated is received from the hyper terminal application-funning on
a PC to which the microcontroller is interfaced. Upon receiving the number from the hypepfeimmahapplication,
the Fibonacci series for the number is generated and it is sent to the hyper terminal application running on the
PC. The program also inserts the character Yt0 separate the two consecutive numbers in the series .while sending
it. The numbers are sent with their corresponding ASCII value (e.g. 0 is sent as 30H). The serial communica¬
tion parameter settings for both hyper terminal application and microcontroller program are: baudirate ^-^600,1
start bit, 8 data bits, 1 stop bit, No parity. Use a crystal resonator with frequency 11,0592MHz fordesigningthe
microcontroller hardware. ' , V' ' .iu ;!
7. Write a 8051 assembly language program to find the largest number from an array of 10 numbers. The array is
located in the data memory4 5 6 7 8 9 10 and the start address of the array is 20H. v " ;. A ‘ * '
8. Write a 8051 assembly language program to generate a PWM signal of ON time 100 microseconds and duty
cycle of 39.06%, at portpin PI.0 ;
9. Explain with code snippet how the 8051 timer can be used for the measurement of the width of an unknown
pulse
10. It is required by an experimental setup (to count the number of pulses arrived during a time period of 50 millisec¬
onds. Explain with code snippets how it can'be implemented using the 8051 timers. *-7.: J t
LEARNING OBJECTIVES
A Learn about the co-design approach for embedded hardware and firmware development ' ■■■-■■
S Know the fundamental issues in model, architecture and language selection for hardware software co-design and
partitioning the system requirements into hardware and software (firmware)
V Loom about the different computational models used in Embedded System design
/ Learn about Data Flow Graphs (DEC) Model, Control Data Flow Graph lCDFG), State Machine Model, Sequential
Program Model, Concurrent/Communicating Model and Object Oriented Model for embedded design
A Learn about Unified Modelling Language (UML) for system modelling, the building blocks of UML, different types of
/ diagrams supported by UML for requirements modelling and design' "• f i.
A Learn about the different tools for UML modelling
A Learn about the trade-offs like performance, cost, etc. to be considered in the partitioning of a system requirement
into either hardware or software ■ ■m-.rg4.erMm
In the traditional embedded system development approach, the hardware software partitioning is done
at an early stage and engineers from the software group take care of the software architecture develop¬
ment and implementation, whereas engineers from the hardware group are responsible for building the
hardware required for the product. There is less interaction between the two teams and the development
happens either serially or in parallel. Once the hardware and software are ready, the integration is per¬
formed. The increasing competition in the commercial market and need for reduced ‘time-to-market’
the product calls for a novel approach for embedded system design in which the hardware and software
are co-developed instead of independently developing both.
During the co-design process, the product requirements captured from the customer are converted
into system level needs or processing requirements. At this point of time it is not segregated as either
hardware requirement or software requirement, instead it is specified as functional requirement. The
system level processing requirements are then transferred into functions which can be simulated and
verified against performance and functionality. The Architecture design follows the system design. The
partition of system level processing requirements into hardware and software takes place during the ar¬
chitecture design phase. Each system level processing requirement is mapped as either hardware and/or
Hardware Software Co-Design and Program Modelling
software requirement. The partitioning is performed based on the hardware-software trade-offs. We will
discuss the various hardware software tradeoffs in hardware software co-design in a separate topic. The
architectural design results in the detailed behavioural description of the hardware requirement and the
definition of the software required for the hardware. The processing requirement behaviour is usually
captured using computational models and ultimately the models representing the software processing
requirements are translated into firmware implementation using programming languages.
The hardware software co-design is a problem statement and when we try to solve this problem state¬
ment in real life we may come across multiple issues in the design. The following section illustrates
some of the fundamental issues in hardware software co-design.
Selecting the model In hardware software co-design, models are used for capturing and describing
the system characteristics. A model is a formal system consisting of objects and composition rules. It is
hard to make a decision on which model should be followed in a particular system design. Most often
designers switch between a variety of models from the requirements specification to the implementation
aspect of the system design. The reason being, the objective varies with each phase; for example at the
specification stage, only the functionality of the system is in focus and not the implementation informa¬
tion. When the design moves to the implementation aspect, the information about the system compo¬
nents is revealed and the designer has to switch to a model capable of capturing the system’s structure.
We will discuss about the different models in a later section of this chapter.
Selecting the Architecture A model only captures the system characteristics and does not provide
information on ‘how the system can be manufactured?’'. The architecture specifies how _a .system is
going to implement in terms of the number and types of different components and the interconnec¬
tion among them. Controller architecture, Datapath Architecture, Complex Instmction Set Computing
(CISC), Reduced Instmction Set Computing (RISC), Very Long Instmction Word Computing (VLIW),
Single Instmction Multiple Data (SIMD), Multiple Instmction Multiple Data (MIMD), etc. are the
commonly used architectures in system design. Some of them fall into Application Specific Architec¬
ture Class (like controller architecture), while others fall into either general purpose architecture class
(CISC, RISC, etc.) or Parallel processing class (like VLIW, SIMD, MIMD, etc.).
The controller architecture implements the finite state machine model (which we will discuss in a
later section) using a state register and two combinational circuits (we will discuss about combinational
circuits in a later chapter). The state register holds the present state and the combinational circuits imple¬
ment the logic for next state and output.
The datapath architecture is best suited for implementing the data flow graph model where the out¬
put is generated as a result of a set of predefined computations on the input data. A datapath represents
a channel between the input and output and in datapath architecture the datapath may contain registers,
counters, register files, memories and ports along with high speed arithmetic units. Ports connect the
datapath to multjple buses. Most of the time the arithmetic units are connected in parallel with pipelin¬
ing support for bringing high performance.
The Finite State Machine Datapath (FSMD) architecture combines the controller architecture with
datapath architecture. It implements a controller with datapath. The controller generates the control in¬
put whereas the datapath processes the data. The datapath contains two types of I/O ports, out of which
one acts as the control port for receiving/sending the .control signals from/to the controller unit and the
Introduction to Embedded Systems
second I/O port interfaces the datapath with' external world for data input and data output. Normally the
datapath is implemented in a chip and the I/O pins of the chip acts as the data input output ports for the
chip resident data path.
The Complex Instruction Set Computing (CISC) architecture uses an instruction set representing
complex operations. It is possible for a CISC instruction set to perform a large complex operation (e.g.
Reading a register value and comparing it with a given value and then transfer the program execution
to a new address location (The CJNE instruction for 8051 ISA)) with a single instruction. The use of a
single complex instruction in place of multiple simple instructions greatly reduces the program memory
access and program memory size requirement. However it requires additional silicon for implementing
microcode decoder for decoding the CISC instruction. The datapath for the . CISC processor is com¬
plex. On the other hand, Reduced Instruction Set Computing (RISC) architecture uses instruction set
representing simple operations and it requires the execution of multiple RISC instructions to perform a
complex operation. The data path of RISC architecture contains a large register file for storing the op¬
erands and output. RISC instruction set is designed to operate on registers. RISC architecture supports
extensive pipelining.
The Very Long Instruction Word (VLIW) architecture implements multiple functional units (ALUs,
multipliers, etc.) in the datapath. The VLIW instruction packages one standard instruction per functional
unit of the datapath.
Parallel processing architecture implements multiple concurrent Processing Elements (PEs) and
each processing element may associate a datapath containing register and local memory. Single Instruc¬
tion Multiple Data (SIMD) and Multiple Instruction Multiple Data (MIMD) architectures are examples
for parallel processing architecture. In SIMD architecture, a single instruction is executed in parallel
with the help of the Processing Elements. The scheduling of the instruction execution and controlling of
each PE is performed through a single controller. The SIMD architecture forms the basis of re-configu¬
rable processor (We will discuss about re-configurable processors in a later chapter). On the other hand,
the processing elements of the MIMD architecture execute different instructions at a given point of time.
The MIMD architecture forms the basis of multiprocessor systems. The PEs in a multiprocessor system
communicates through mechanisms like shared memory and message passing.
Selecting the language A programming language captures a ‘Computational Model’ and maps it
into architecture. There is no hard and fast rule to specify this language should be used for capturing
this model. A model can be captured using multiple programming languages like C, C++, C#, Java, etc.
for software implementations and languages like VHDL, System C, Verilog, etc. for hardware imple¬
mentations. On the other hand, a single language can be used for capturing a variety of models. Certain
languages are good in capturing certain computational model. For example, C++ is a good candidate
for capturing an object oriented model. The only pre-requisite in selecting a programming language for
capturing a model is that the language should capture the model easily.
Partitioning System Requirements into hardware and software So far we discussed about the
models for capturing the system requirements and the architecture for implementing the system. From
an implementation perspective, it may be possible to implement the system requirements in either hard¬
ware or software (firmware). It is a tough decision making task to figure out which one to opt. Various
hardware software trade-offs are used for making a decision on the hardware-software partitioning. We
will discuss them in detail in a later section of this chapter.
Hardware Software Co-Design and Program Modelling
delay. The timer remains in the ‘READY’ state until a ‘Start Timer’ event occurs. The timer changes
its state to ‘RUNNING’ from the ‘READY’ state on receiving a ‘Start Timer’ event and remains in the
‘RUNNING’ state until the timer count expires or a ‘Stop Timer’ even occurs. The timer state changes
to ‘IDLE’ from ‘RUNNING’ on receiving a ‘Stop Timer’ or ‘Timer Expire’ event.
Example 1
Design an automatic tea/coffee vending machine based on FSM model for the following requirement.
The tea/coffee vending is initiated by user inserting a 5 rupee coin. After inserting the coin, the user can either select
‘Coffee’ or ‘Tea’ or press ‘Cancel’ to cancel the order and take back the coin.
The FSM representation for the above requirement is given in Fig. 7.5.
In its simplest representation, it contains four states namely; ‘Wait for coin’ ‘Wait for User Input’, ‘Dispense Tea’ and
‘Dispense Coffee’. The event ‘Insert Coin’ (5 rupee coin insertion), transitions the state to ‘Wait for User Input’. The
system stays in this state until a user input is received from the buttons ‘Cancel’, ‘Tea’ or ‘Coffee’ (Tea and Coffee are
the drink select button). If the event triggered in ‘Wait State’ is ‘Cancel’ button press, the coin is pushed out and the state
transitions to ‘Wait for Coin’. If the event received in the ‘Wait State’ is either ‘Tea’ button press, or ‘Coffee’ button press,
the state changes to ‘Dispense Tea’ and ‘Dispense Coffee’ respectively. Once the coffee/tea vending is over, the respective
Introduction to Embedded Systems
states transitions back to the ‘Wait for Coin’ state. A few modifications like adding a timeout for the ‘Wait State’ (Cur¬
rently the ‘Wait State’ is infinite; it can be re-designed to a timeout based ‘Wait State’. If no user input is received within
the timeout period, the coin is returned back and the state automatically transitions to ‘Wait for Coin’ on the timeout
event) and capturing another events like, ‘Water not available’, ‘Tea/Coffee Mix not available’ and changing the state to
an ‘Error State’ can be added to enhance this design. It is left to the readers as exercise.
Example 2
Design a coin operated public telephone unit based on FSM model for the following requirements.
1. The calling process is initiated by lifting the receiver (off-hook) of the telephone unit
2. After lifting the phone the user needs to insert a 1 rupee coin to make the call.
3. If the line is busy, the coin is returned on placing the receiver back on the hook (on-hook)
4. If the line is through, the user is allowed to talk till 60 seconds and at the end of 45th second, prompt for inserting
another 1 rupee coin for continuing the call is initiated
5. If the user doesn’t insert another 1 rupee coin, the call is terminated on completing the 60 seconds time slot.
6. The system is ready to accept new call request when the receiver is placed back on the hook (on-hook)
7. The system goes to the ‘Out of Order’ state when there is a line fault.
The FSM model shown in Fig. 7.6, is a simple representation and it doesn’t take care of scenarios like, user doesn’t
insert a coin within the specified time after lifting the receiver, user inserts coins other than a one rupee etc. Handling
these scenarios is left to the readers as exercise.
Most of the time state machine model translates the requirements into sequence driven program and it is difficult to
implement concurrent processing with FSM. This limitation is addressed by the Hierarchical/Concurrent Finite State
Machine model (HCFSM). The HCFSM is an extension of the FSM for supporting concurrency and hierarchy. HCFSM
extends the conventional state diagrams by the AND, OR decomposition of States together with inter level transitions
and a broadcast mechanism for communicating between concurrent processes. HCFSM uses statecharts for capturing the
states, transitions, events and actions. The Harel Stateehart, UML State diagram, etc. are examples for popular statecharts
used for the HCFSM modelling of embedded systems. In statecharts, the state is usually represented using geometric
shapes like rounded rectangle, rectangle, ellipse, circle, etc. The Harel Stateehart uses a rounded rectangle for represent¬
ing state. Arrows are used for representing the state transition and they are marked with the event associated with the state
transition. Sometimes an optional parenthesized condition is also labeled with the arrow. The condition specifies on what
Hardware Software Co-Design and Program Modelling
basis the state transition happens at the occurrence of the specified event. Lots of design tools are available for state ma¬
chine and statechart based system modelling. The IAR visualSTATE (http://www.iar.eom/websitel/l.0.l.0/371/l/index.
php) from IAR systems is a popular visual modelling tool for embedded applications.
#define NO 0 . s / TlUdC
void seat_i>elt__warnf)>' :
{ .
wait_10sec (); t ■'% « t Y *j
if (check_ignition_key()==ON)
:_seat_belt () =
>0==NO)); ' j
■ . • ' '■
7.2.5 Concurrent/Communicating
Process Model
The concurrent or communicating process model models
concurrently executing tasks/processes. So far we discussed
about the sequential execution of software programs. It
is easier to implement certain requirements in concur¬
rent processing model than the conventional sequential
execution. Sequential execution leads to a single sequen¬
tial execution of task and thereby leads to poor processor
utilisation, when the task involves I/O waiting, sleeping
for specified duration etc. If the task is split into multiple
subtasks, it is possible to tackle the CPU usage effectively,
when the subtask under execution goes to a wait or sleep
mode, by switching the task execution. However, concur¬
rent processing model requires additional overheads in
task scheduling, task synchronisation and communication.
As an example for the concurrent processing model let us
examine how we can implement the ‘Seat Belt Warning’
system in concurrent processing model. We can split the
tasks into:
1. Timer task for waiting 10 seconds (wait timer task)
2. Task for checking the ignition key status (ignition
key status monitoring task)
3. Task for checking the seat belt status (seat belt status
monitoring task)
Hardware Software Co-Design and Program Modelling
4. Task for starting and stopping the alarm (alarm control task)
5. Alarm timer task for waiting 5 seconds (alarm timer task)
We have five tasks here and we cannot execute them randomly or sequentially. We need to synchro¬
nise their execution through some mechanism. We need to start the alarm only after the expiration of
the 10 seconds wait timer and that too only if the seat belt is OFF and the ignition key is ON. Hence
the alarm control task is executed only when the wait timer is expired and if the ignition key is in the
ON state and seat belt is in the OFF state. Here we will use events to indicate these scenarios. The
wait timerexpire event is associated with1 the timer task event and it will be in the reset state initially
and it is set when the timer expires. Similarly, events ignition on and ignition jff are associated with
the task ignition key status monitoring and the events seatJbelt_on and seat_belt_off are associated with
the task seat belt status morning. The events ignition j)jf and ignition on are set and reset respectively
when the ignition key status is OFF and reset and set respectively when the ignition key status is ON, by
the ignition key status monitorihg task. Similarly the events seat_belt_off and seat_belt_on are set and
reset respectively when the seat belt status is OFF and reset and set respectively when thje seat belt status
is ON, by the seat belt status monitoring task. The events alarm Jimer jtart and alarm Jimer_expire are
associated with the alarm timer task. The alarm Jimer jtart event will be in the reset state initially and
it is set by the alarm control taskwhen the alarm is started. The alarm Jimer expire event will be in the
^eseFstateTnitialfy-and4tisj£t whe|i ,the alarm timer expires. The alarm control task waits for the signal¬
ing of the event wait Jimer expire^ and starts the alarm timer and alarm if both the events ignition on
and seat helt off are in the set state when the event wait Jimer^expire signals. If not the alarm control
task simply completes its execution and returns. In case the alarm is started, the alarm control task waits
for the signalling of any one of the events alarm timer expire or ignition off or seat belt on. Upon
signalling any one of thlese events, the alarm is stopped and the alarm control task simply completes its
execution and returns. Figure 7.8 illustrates the same.
It should be noted that the method explained here is just one way of implementing a concurrent mod¬
el for the ‘Seat Belt Warning’ system. The intention is just to make the readers familiar with the concept
of multi tasking and task communication/synchronisation. There may be other ways to model the same
requirements. It is left to the readers as exercise. The concurrent processing model is commonly used
for the modelling of‘Real Time’ systems. Various techniques like ‘Shared memory’, ‘Message Passing’,
‘Events’, etc. are used for communication and synchronising between concurrently executing processes.
We will discuss these techniques in a later chapter.
1 '
Create and initialize events
^ Fig. 7.8) (a) Tasks for ‘Seat Belt Warning System’ (b) Concurrent processing Program model for ‘Seat Belt
Warning System’ 'h' i ' *£*••■<’ fv
7,3.1.1 Things A ‘Thing’ is an abstraction of the UML model. The ‘Things’ in UML are classified
into:
Structural things: Represents mostly the static parts of a UML model. They are also known as ‘classi¬
fiers’. Class, interface, use case, use case realisation (collaboration), active class, component and node
are the structural things in UML.
Behavioural things: Represents mostly the dynamic parts of a UML model. Interaction, state machine
and activity are the behavioural things in UML.
Grouping things: Are the organisational parts of a UML model. Package and sub-system are the group¬
ing things in UML.
Annotational things: Are the explanatory parts of a UML model. Note is the Annotational thing in
UML.
The table given below gives a snapshot of various structural, behavioural, grouping and Annotational
things in UML.
Methods
. . ; ■ . .. L;
-: : . ' “ -V l S \ '<
(a) (b)
Fig. 7.10 (a) Alarm is a special type of Audio Visual Indicator (Generalisation), (b) Alarm is a part of Warning
System (Aggregation.) • -
7.3.1.3 UML Diagrams UML Diagrams give a pictorial representation of the static aspects, behav¬
ioural aspects and organisation and management of different modules (classes, packages, etc.) of the
system. UML diagrams are grouped into Static Diagrams and Behavioural Diagrams.
Static Diagrams Diagram representing the static (structural) aspects of the system. Class Dia¬
gram, Object Diagram, Component Diagram, Package Diagram, Composite Structure Diagram and
Deployment Diagram falls under this category. The table given below gives a snapshot of various static
diagrams.
‘ Component diagram ' " It is a pictorial representation of the implementation view of a system. It comprises
mjLZS.V -
r ?
'
’ .v components Yn^roiool
(physical -nu^bormrr
packaging rtf'
of oIopopp
classes utifl
and inthrfdoop)
interfaces), rolnfiAnckmo
relationships dn^
and
■ ' ^ . associations among the components. ' .
... V” „ „ *r fU
igepiagramTi^rife
Deployment diagram .r It is a pictorial representation ofthe configuration of mn time processing nodes and
LI ”, the components associated with them:
Behavioural Diagrams These are diagrams representing the dynamic (behavioural) aspects of the
system. Use Case Diagram (Fig. 7.11), Sequence Diagram (Fig. 7.12), State Diagram, Communication
Introduction to Embedded Systems
Enable or ■
« Seat Belt
^Disable Alanny
Use case
Alarm
Ignition Key
Fig. 7.11] Use Case diagram for seat belt warning system
- V) Alarm
Wait Timer —— Alarm
Timer
3 OFF
5 Trigger
I 1 Trgger
2 Timeout
6 Timeout
Fig. 7,12j Sequence diagram for one possible sequence for the seat belt warning system
Diagram, Activity Diagram, Timing Diagram and Interaction Overview Diagram are the behavioural di¬
agrams in UML model. The table given below gives a snapshot of the important behavioural diagrams.
Hardware Software Co-Design and Program Modelling
Some of the tools generate the skeletal code (stub) for the classes and application in programming lan¬
guages like C++, C#, Java, etc.
memory for implementing requirements. On the other hand, evaluate the gate count required (Normally
hardware chips are implemented using logic gates and the density of the chip is expressed in terms of the
number of gates used in the design (millions of gates ©)), if the required feature is going to implement
in hardware.
Effort required in terms of man hours, if the required feature is going to build in either software or
custom hardware implementation using VHDL or any other hardware description languages and the cost
for each are another important hardware-software trade-off in any embedded system development.
To summarise, the important hardware-software trade-offs in embedded system design are
1. Processing speed and performance
2. Frequency of change (Re-configurability)
3. Memory size and gate count
4. Reliability
5. Man hours (Effort) and cost
Summary
S Model selection! Architecture selection, Language selection, Hardware Software partitioning, etc. areysonuTdf
-- the main points in the hardware-software co-design ; . . . . : •' .
S A model captures and describes the system characteristics; The architecture specifies how a system is going-fo
implement in terms of the number and types of different components and the interconnection among them
wUcGontroller architecture, Datapath architecture. Complex Instruction Set Computing (CISC), Reo^^^Mtruc-
, ftipn(Set Computing (RISC), Very long Instruction,Word Computing (VLIW), Single Instruction Mjiluple Data
(SIMD), Multiple Instruction Multiple Data (MIV1D), etc. are the commonly used architectures in system desigtp.f
S Data Flow Graph (DFG) Model, State Machine Model, Concurrent Process Model, Sequential Programmbtlel,
Object Oriented model, etc. are the commonly used computational models in embedded system design ••
S A programming language captures a ‘Computational Model’ and maps it into architecture. A modpL^inybe,
captured using multiple programming languages like C, C++, C#, Java, etc. for software implementations ;and
languages like VHDL, System C, Verilpg, etc. for hardware implementations ' * ^
S The Hierarchical. Concurrent Finite State Machine Model (HCFSM) is an extension of the FSM for supporting
concurrency and hierarchy. HCFSM extends the conventional state diagrams by the AND. OR decomposition of
States together with inter level transitions and a broadcast mechanism for communicating between concurrent ■:
processes .MMy -
S HCFSM uses statecharts for capturing the states, transitions, events and actions. The Hard Statechart, IJML State
diagram, etc. are examples for popular statecharts for the HCFSM modelling of embedded systems
S In the sequential model, the functions or processing requirements are executed in sequence. It is same as the
conventional procedural programming
S The concurrent or communicating process model models concurrently executing tasks/procesSes
S The object oriented model is an object based model for modelling system requirements
S Unified Modelling Language (UML) is a visual modelling language for Object Oriented Design (OOD). Things,
Relationships and Diagrams are the fundamental building blocks of UML -.i-R
S UML diagrams give a pictorial representation of the static aspects, behavioural aspects and organisation and
management of different modules. Static Diagrams, Object diagram, Usecase diagram, Sequence diagram, Stat¬
echart diagram etc. are the different UML diagrams
S Hardware-Software trade-offs are the parameters used in the decision making of partitioning a system require¬
ment into either hardware or software. Processing speed, performance, frequency of changes, memory size and
gate count requirements, reliability, effort, cost, etc. are examples for hardware-software trade-offs
Hardware Software Co-Design and Program Modelling
Hardware-Software Co-design : The modem approach for the interactive ‘together’ design of hardware and
firmware for embedded systems
Controller Architecture : Architecture implementing the Finite State Machine model using a state
v- " ■ -r ■- register and two combinational circuits ■-^r-'rp
Datapath Architecture Datapath : Architecture implementing the Data Flow Graph model \ | T;jp
: A thannel between the input and output. The datapath may Contain-regC'
.• isters, counters, register files, memories and ports along with high speed '
; * arithmetic units A
Finite State Machine Datapath Architecture combining the .controller architecture with datapath architec-
1 (FSMI)) Architecture ' ' :ture : --
Complex Instruction Set Computing Architecture which uses instruction set representing complex operations
(CISC)Architecture - •' 4 ■ ’ . ' - y
Reduced Instruction Set Computing : Architecture which uses instruction set representing simple operatiohsfe?‘,;:>‘,
(RISC) Architecture
Very Long Instruction Word : Architecture implementing multiple functional units (ALUs, multipliers, :
(VLIW) Architecture etc.) in the datapath ,
Parallel Processing Architecture : Architecture implementing multiple concurrent Processing Elements '
(pes) ., '; . . ’ ../‘ it
Single Instruction Multiple Data ,: Architecture in which a single instruction is executed in parallel with the
(SIMD) Architecture help of the processing elements " _
.;MIMD Architecture : Architecture in which the processing elements execute different instruc-f
tions at a given point of time
Programming Language : An entity for capturing a ‘Computational Model’ and maps it into archi¬
tecture
Data Flow Graph (DFG) Model : A data driven model in which the program execution is determined by
data
Control DFG (CDFG) Model : Amodel containing both data operations and control operations. The CDFG
uses Data Flow Graph (DFG) as element and conditional (constructs) as
decision makers
State Machine Model : Amodel describing the system behaviour with ‘States’, ‘Events’, ‘Actions’
and ‘Transitions’
Statechart : An entity for capturing the states, transitions, events and actions
Harel Statechart : A type of statechart
Finite State Machine (FSM) Model : A state machine model with finite number of states
Hierarchical/Concurrent Finite : An extension of the FSM for supporting concurrency and hierarchy
State Machine Model (HCFSM)
Sequential Model : Model for capturing sequential processing requirements
Concurrent or Communicating : Model for capturing concurrently executing tasks/process requirements
Process Model
Object Oriented Model : Object based model for modelling system requirements
Unified Modelling Language (UML) : A visual modelling language for Object Oriented Design
UML Diagram : Diagram giving a pictorial representation of the static aspects, behavioural
aspects and organisation and management of different modules
Introduction to Embedded Systems
Static Diagram ; UML diagram representing the static (structural) aspects of the system
Behavioural Diagram UML diagram representing the dynamic (behavioural) aspects of the
system
_ • . _ • - ■ . ■ -wvV • ' - e
oftware trade-offs < : The parameters us
1
d11 Objective Questions
1. Which of the following programming model is best suited for modelling a data driven embedded system
(a) State Machine (b) Data Flow Graph .(c) Harel Statechart Model
(d) None of fhese
2. Which of the following programming model is best suited for modelling a Digital Signal Processing (DSP) embed¬
ded system
(a) Finite State Machine (b) Data Flow Graph (c) Object Oriented Model
(d) UML
3. Which of the following architecture is best suited for implementing a Digital Signal Processing (DSP) embedded
system
(a) Controller Architecture (b) CISC (c) Datapath Architecture (d) None of these
4. Which of the following is a multiprocessor architecture?
(a) SIMD (b) MIMD (c) VLIW (d) All
(e) (a) and (b) (f) (b) and (c)
5. Which of the following model is best suited for modelling a reactive embedded system?
(a) Finite State Machine (FSM) (b) DFG (c) Control DFG
(d) Object Oriented Model
6. Which of the following models is best suited for modelling a reactive real time embedded system?
(a) Finite State Machine (b) DFG (c) Control DFG
(d) Hierarchical/Concurrent Finite State Machine Model
7. Which of the following model is best suited for modelling an embedded system demanding multitasking capabili¬
ties with data sharing?
(a) Finite State Machine (b) DFG (c) Control DFG
(d) Communicating Process Model
8. Which of the following is a hardware description language?
(a) C (b) System C (c) VHDL (d) C++
(e) (b) and (c)
Hardware Software Co-Design and Program Modelling
1. What is hardware software co-design? Explain the fundamental issues in hardware software co-design
2. Explain the difference between SIMD, MIMD and VLIW architecture
3. What is Computational model? Explain its role in hardware software co-design
4. Explain the different computational models in embedded system design
5. What is the difference between Data Flow Graph (DFG) and Control Data Flow Graph (CDFG) model? Explain
their significance in embedded system design
6. What is State and State Machine? Explain the role of State Machine in embedded system design
7. What is the difference between Finite State Machine Model (FSM) and Hierarchical/Concurrent Finite State
Machine Model (HCFSM)?
8. What is ‘Statechart’? Explain its role in embedded system design
9. Explain the ‘SequentiaF Program model with an example
10. Explain the ‘Concurrent/Communicating’ program model. Explain its role in ‘Real Time’ system design
11. Explain the ‘Object-Oriented’ program model for embedded system design. Under which circumstances an Object-
Oriented model is considered as the best suited model for embedded system design?
12. Explain the role of programming languages in system design
13. What are the building blocks of UML? Explain in detail.
14. Explain the different types of UML diagrams and their significance in each stage of the system development life
cycle
15. Explain the important hardware software ‘trade-offs’ in hardware software partitioning?
Introduction to Embedded Systems
r^,= i;.- a
is .printed arul Lhe
Introduction to Embedded Systems
I hope by now you got a reasonably good knowledge on embedded systems, the different elements
of an embedded system, their application areas and domains, their characteristics and quality attributes.
Now it is time to move on to the various steps involved in the design of embedded systems. As men¬
tioned in Chapter 2 of the previous section, embedded systems are built around a central core which
can be a Commercial-off-the-Shelf Component (COTS) or an Application Specific Integrated Circuit/
Application Specific Standard Product (ASIC/ASSP) or a Programmable Logic Device (PLD) or a Gen¬
eral Purpose Processor (Microprocessor/General Purpose Microcontrollers) or Application Specific In¬
struction Set Processors (Special Purpose Microcontrollers/Digital Signal Processors). Describing all of
them in detail is outside the scope of this book and for the time being we will be dealing with the design
of embedded systems using Application Specific Instruction Set Processor and the scope of this design
is again limited to designing with Microcontroller. The target microcontroller selected for the design ap¬
proach is 8051 - Industry’s most popular 8bit microcontroller. The architecture details of the basic 8051
controller and the instructions supported by it are already explained in some of the previous chapters.
In the next sections we will explore the possibilities of how the 8051 controller can be embedded into
different applications and how the 8051 can be programmed for various embedded applications.
The embedded product design starts with product requirements specification and analysis. When
a requirement arises for an embedded product, the requirements are listed out including the hardware
features required, the functionalities to be .supported, the product aesthetics (look V feel) etc. On
finalising the requirements, the various hardware components and peripherals required to implement
the hardware features are identified and the interconnection among them are defined, the control algo¬
rithm for controlling the various hardware and peripherals are developed, product aesthetics like look
and feel, enclosure unit, etc. are defined and the control algorithm is transformed to controller specific
instructions using the firmware development tools and finally the hardware and firmware are integrated
together and the resulting product is tested for the required functionalities as per the requirement speci¬
fications. The mechanical design of thmproduct takes care of the product aesthetics and develops a suit¬
able enclosure for the product. Various standards like International Organisation for Standards (ISO)
and models like Capability Maturity Model (CMM), Process Capability Maturity Model (PCMM) etc.
are used in the entire design life cycle management. There will be well-defined documentation structure
(Templates') for the entire design life cycle. The Requirement Specification Document records the entire
requirement for the product. Formal verifications of this document will be carried out before the system
design starts and it is tenned as “Document Reviews”. Once the requirement specifications are finalised
the preliminary design and detailed designs are carried out. The preliminary design and detailed design
deals with the different modules of the total system and what all hardware/fi rmware components are
required to build these modules, how these modules are interconnected and how they are controlled.
• The chapters in this section are organised in a way to give the readers the basic lessons on “Embed¬
ded Hardware Design and Development”, “Embedded Firmware Design and Development”, “Integra¬
tion and testing of the Embedded Hardware and Firmware”, “Product Enclosure Development” and
Embedded Product Development Life Cycle Management”. We will start the section with Embedded
Hardware Design and Development.
Design and Development of Embedded Product
Embedded Hardware Design and
Development
LEARNING OBJECTIVES
/ Learn about the various elements of Embedded Hardware and their design principles ' .Xj '
/ Refresh knowledge on the basic Analog Electronic components, and circuits - Resistor, Capacitor, Diode, Inductor,
Transistor etc. and their use in embedded applications • ■ " .
/ Refresh knowledge oh the basic Digital Electronic components and circuits - Logic Gates,' Buffer ICs, Latch ICs, De- ,
coder and Encoder ICs, Multiplexer (MUX) and De-multiplexer (D-MUX), Combinational and Sequential Circuits and
their use in embedded applications \ - ; ■ ' " , f ■.
X Learn about Integrated Circuits (ICs), the .degree of integration in various types of Integrated Circuits, Analog,
Digital and Mixed Signal Integrated Circuits, the steps involved in IC Design .. •
X Learn about Electronic Design Automation tools for Embedded Hardware Design and EDA tools for Printed Circuit'
Board Design , %. ' - . " » „ . ■ >,«£ a . ' . -
X Familiarise with.the usage of the Oread EDA tool from Cadence software for Printed Circuit Board Design
X Familiarise with the different, entities for circuit diagram design (schematic design.) using the Oread CaptureCIS
tool. Familiarise with the different entities of the Capture CIS; tool‘like iDrawing Canvas', ‘Drawing Tool', 'Drawing
Details', 'Part Number creation', 'Design Rule Check (DRC)', 'Bill of Material (BoM)' creation, 'Netlistfile creation for
PCB blueprint design, etc.
X Fam iliarise with the usage of 'Layout' tool from Cadence for generating the PCB design files from the circuit diagram
description file (From Netlistfile)
X Learn the building blocks of 'PCB Layout' - Footprints (Component Packages), Routes/Traces, Layers, Via, Marking
text and graphics, etc.
X Learn about Board Outline creation, Component placement, Layer selection, PCB Track Routing, Assembly note cre¬
ation, Text and graphics addition, PCB Mounting hole creation, Design Rule Checking, Gerber file creation, etc. using
Layout tool
X Learn the important guidelines for a good PCB design
X Learn about the different mechanisms for PCB fabrication, different types ofPCBs, How a finished PCB looks like and
how it is made operational
Embedded Hardware Design and Development
As mentioned at the beginning of this book, an embedded system is a combination of special purpose
hardware and software (firmware). The hardware of embedded system is built around analog electronic
components and circuits, digital electronic components and circuits, and integrated circuits (ICs). A
printed circuit board (PCB) provides a platform for placing all the necessary hardware components for
building an embedded product, like a chess board where you can move the pawns, rookies and bishop.
If you are building a simple hardware or a test product, you can use a bread-board to interconnect the
various components. For a commercial product you cannot go for a bread-board as an interconnec¬
tion platform since they make your product bulky and the breadboard connections are highly unstable
and your product may require thousands of inter connections. Printed circuit boards (PCB) act as the
backbone of embedded hardware. This chapter is organised in such a way to refresh the reader’s knowl¬
edge on various analog and digital electronic components and circuits, familiarise them with Integrated
Circuit designing and provide them the fundamentals of printed circuit boards, its design and develop¬
ment using electronic design automation (EDA) tools.
Resistors, capacitors, diodes, inductors, operational amplifiers (OpAmps), transistors, etc. are the com¬
monly used analog electronic components in embedded hardware design. A resistor limits the current
flowing through a circuit. Interfacing of LEDs, buzzer, etc. with the port pins of microcontroller through
current limiting resistors is a typical example for the usage of resistors in embedded application. Ca¬
pacitors and inductors are used in signal filtering and resonating circuits. Reset circuit implementation,
matching circuits for RF designs, power supply decoupling, etc. are examples for the usage of capacitors
in embedded hardware circuit. Electrolytic capacitors, ceramic capacitors, tantalum capacitors, etc. are
the commonly used capacitors in embedded hardware design. Inductors are widely used for filtering the
power supply from ripples and noise signals. Inductors with inductance value in the microhenry (pH)
range are commonly used in embedded applications for filter and matching circuit implementation.
P-N Junction diode, Schottky diode, Zener diode, etc. are the commonly used diodes in embedded
hardware circuits. A schottky diode is same as a P-N junction diode except that its forward voltage
drop (voltage drop across diode when conducting) is very low (of the order of 0.15V to 0.45) when
compared to ordinary P-N junction diode (of the order of 0.7V to 1.7V). Also the current switching
time of schottky diode is very small compared to the ordinary P-N junction diode. A zener diode acts
as normal P-N junction diode when forward biased. It also permits current flow in the reverse direction,
if the voltage is greater than the junction breakdown voltage. It is normally used for voltage clamping
applications. Reverse polarity protection, voltage rectification (AC-DC converters), freewheeling of
current produced in inductive circuits, clamping of voltages to a desired level (e.g., Brown-out protec¬
tion circuit implementation using zener diode), etc. are examples for the usage of diodes in embedded
applications.
Transistors in embedded applications are used for either switching or amplification purpose. In
switching application, the transistor is in either ON or OFF state. In amplification operation, the transis¬
tor is always in the ON state (partially ON). The current is below saturation current value and the current
through the transistor is variable. The common emitter configuration of NPN transistor is widely used
in switching and driving circuits in embedded applications. Relay, buzzer and stepper motor driving
circuits are examples for common emitter configuration based driver circuit implementation using tran¬
sistor (Please refer to Chapter 2).
Digital electronics deal with digital or discrete signals. Microprocessors, microcontrollers and system
on chips (SoCs) work on digital principles. They interact with the rest of the world through digital I/O
interfaces and process digital data. Embedded systems employ various digital electronic circuits for
‘Glue logic’ implementation. ‘Glue logic’ is the custom digital electronic circuitry required to achieve
compatible interface between two different integrated circuit chips. Address decoders, latches, encod¬
ers/decoders, etc. are examples for glue logic circuits. Transistor Transistor Logic (TTL), Complemen¬
tary Metal Oxide Semiconductor (CMOS) logic etc are some of the standards describing the electrical
characteristics of digital signals in a digital system. The following sections give an overview of the
various digital I/O interface standards and the digital circuits/eomponents used in embedded system
development.
For the output pin to function properly, the output pin should be pulled, to the desired voltage for
the o/p device, through a pull-up resistor. The output signal of the IC is fed to the base of an open col¬
lector transistor. When the base drive to the transistor is ON and the collector is in open state, the o/p
pin floats. This state is also known as ‘high impedance’ state. Here the output is neither driven to logic
‘high’ nor logic Tow’. If a pull-up resistor is connected to the o/p pin, when the base drive is ON, the o/p
pin becomes at logic 0 (OV). With a pull-up resistor, if the base driver is 0, the o/p will be at logic high
(Voltage = V ). The advantage of open collector output in embedded system design is listed below.
1. It facilitates the interfacing of devices, operating at a voltage different from the IC, with the IC.
Thereby, it eliminates the need for additional interface circuits for connecting devices at different
voltage levels.
Embedded Hardware Design and Development
2. An open collector configuration supports multi-drop connection, i.e., connecting more than one
open collector output to a single line. It is a common requirement in modem embedded systems
supporting communication interfaces like I2C, 1-Wire, etc. Please refer to the various interfaces
described in Chapter 2 under the section ‘Onboard-Communication Interfaces’.
3. It is easy to build ‘Wired AND’ and ‘Wired OR’ configuration using open collector output lines.
The output of a standard logic device has two states, namely ‘Logic 0 (LOW)’ and ‘Logic 1 (HIGH),
and the output will be at any one of these states at a given point of time, whereas tri-state devices have
three states for the output, namely, ‘Logic 0 (LOW)’, ‘Logic 1 (HIGH) and ‘High Impedance (FLOAT)’.
A tri-state logic device contains a device activation line called ‘Device Enable’. When the ‘Device En¬
able’ line is activated (set at ‘Logic 1’ for an active ‘HIGH’ enable input and at ‘Logic 0’ for an active
‘LOW’ enable input), the device acts like a normal logic device and the output will be in any one of the
logic conditions, ‘Logic 0 (LOW)’ or ‘Logic 1 (HIGH)’. When the ‘Device Enable’ line is de-activated
(set at ‘Logic 0’ for’ an active ‘HIGH’ enable input and at ‘Logic 1 ’ for an active ‘LOW’ enable input),
the output of the logic device enters in a high impedance state and the device is said to be in the floating
state. The tri-stated output condition produces the effect of ‘removing’ the device from a circuit and al¬
lows more than one devices to share a common bus. With multiple ‘tri-stated’ devices share a common
bus, only one ‘device’ is allowed to drive the bus (drive the bus to either ‘Logic 0’ or ‘Logic 1’) at any
given point of time and rest of the devices should be in the ‘tri-stated’ condition.
8.2.3 Buffer
A buffer circuit is a logic circuit for amplifying the current or power. It increases the driving capability
of a logic circuit. A tn-state buffer is a buffer with Output Enable control. When the Output Enable con¬
trol is active (Low for Active low enable and High for Active high enable), the tri-state buffer functions
as a buffer. If the Output Enable is not active, the output of the buffer remains at high impedance state
(Tri-stated). Tri-state buffers are commonly used as drivers for address bus and to select the required
device among multiple.devic.es connected to a shared data bus. Tri-state buffers are available as either
unidirectional or bi-directional buffers. 74LS244/74HC244 is an example of unidirectional octal buffer.
It contains 8 individual buffers which are grouped into two. Each buffer group has its own output enable
line. Figure 8.3 illustrates the 74LS244 buffer device.
IC 74LS245 is an example of bi-directional tri-state buffer. It allows data flow in both directions, one
at a time. The data flow direction can be set by the direction control line. One buffer is allocated for the
data line associated with each direction. Figure 8.4 illustrates the 74LS245 octal bi-directional buffer.
8.2.4 Latch
A latch is used for storing binary data. It contains an input data line, clock or gating control line for trig¬
gering the latching operation and an output line. The gating signal can be either a positive edge (raising
Introduction to Embedded Systems
I 0
edge) or a negative edge (falling edge). Whenever a latch trigger happens, the data present on the input
line is latched. The latched data is available on the output line of the latch until the next trigger. D flip
flop is a typical example of a latch (refer to your Digital Electronics course material—there exist differ¬
ent types of latches namely S-R, J-K, D, T, etc.). In electronic circuits, latches are commonly used for
latching data, which is available only for a short duration. A typical example is the lower order address
information in a multiplexed address-data bus system. Latches are available as integrated circuits, IC
74LS373 being a typical example. It contains 8 individual D latches.
The 74LS373 latch IC is commonly used for latching the lower order address byte in a multiplexed
address data bus system. The address latch enable (ALE) pulse generated by the processor, when the
address bits are available on the multiplexed bus, is used as the latch trigger. Figure 8.6 illustrates the
usage of latches in address latching.
Embedded Hardware Design and Development
8.2.5 Decoder
A decoder is a logic circuit which generates all the possible combinations of the input signals. Decoders
are named with their input line numbers and the possible combinations of the input as output. Examples
are 2 to 4 decoder, 3 to 8 decoder and 4 to 16 decoder. The 3 to 8 decoder contains 3 input signal lines
and it is possible to have 8 different configurations with the 3 lines (000 to 111 in the input line cor¬
responds to 0 to 7 in the output line). Depending on the input signal, the corresponding output line is
asserted. For example, for the input state OjOl, the output line 2 is asserted. Decoders are mainly used for
address decoding and chip select signal geijeration in electronic circuits and are available as integrated
circuits. 74LS138/74AHC138 is an example for 3 to 8 decoder IC. Figure 8.7 illustrates the 74AHCT38
. decoder and the function table for it,
Introduction to Embedded Systems
ofofo
'
1 0 0 0 0 i 1 1 1 i 0 1 t i
—
1 0 1 0 0 i 1 1 1 l 1 0 i i
1 1 0 0 0 l 11. 1 1 i 1 1 0 t
1 1 1 0 0 i 1 1 1 i I 1 i 0
w wCM wcn
The decoder output is enabled only when the ‘Output Enable’ signal lines El\, E2\ and E3 are at logic
levels 0,0 and 1 respectively. If the output-enable signals are not at the required logic state, all the output
lines are forced to the inactive (High) state. The output line corresponding to the input state is asserted
‘Low’ when the ‘Output Enable’ signal lines are at the required logic state (Here E1\=E2\=0 and E3 =1).
The output line can be directly connected to the chip select pin of a device, if the chip select logic of the
device is active low.
8.2.6 Encoder
An encoder performs the reverse operation of decoder. The encoder encodes the corresponding input
state to a particular output format. The binary encoder encodes the input to the corresponding binary
format. Encoders are named with their input line numbers and the encoder output format. Examples are
4 to 2 encoder, 8 to 3 encoder and 16 to 4 encoder. The 8 to 3 encoder contains 8 input signal lines and it
is possible to generate a 3 bit binary output corresponding to the input (e.g. inputs 0 to 7 are encoded to
binary 111 to 000 in the output lines). The corresponding output line is asserted in accordance with the
input signals. For example, if the input line 1 is asserted, the output lines AO, A1 and A2 are asserted as
0,1 and 1 respectively. Encoders are mainly used for address decoding and chip select signal generation
in electronic circuits and are available as integrated circuits. 74F148/74LS148 is an example of 8 to 3
encoder IC. Figure 8.8 illustrates the 74F148/74LS148 encoder and the function table for it.
The encoder output is enabled only when the ‘Enable Input (El)’ signal line is at logic 0. A ‘High’ on
the Enable Input (El) forces all outputs to the inactive (High) state and allows new data to settle without
producing erroneous information at the outputs. The group signal (GS) is active-Low when any input is
Low: this indicates when any input is active. The Enable Output (EO) is active-Low when all inputs are
at logic ‘High’. 74LS148/74F148 is a priority encoder and it provides priority encoding of the inputs to
ensure that only the highest order data line is encoded when multiple data lines are asserted (e.g., when
both input lines 1 and 6 are asserted, only 6 is encoded and the output will be 001. It should be noted that
the encoded output is an inverted value, of the corresponding binary data, (e.g., the output lines A2, A1
and A0 will be at logic levels 000 when the input 7 is asserted). Encoding of keypress in a keyboard is I
a typical example for an application requiring encoder. The encoder converts each keypress to a binary
code.
Embedded Hardware Design and Development
Input -'.yrA
X Output |
ItL >3.- 2;pf| f$3 11! is. I$1 s$i A11 AO
1 1 1 LL 1 1 1 0 0 0
. 1 1 1 1
1 1 1 i 1 1 0 1 0 0 1 1 1 0
1 1 1 i 1 0 1 : 1 D 0 1 . 1 0 1
1 1 1 i 0 1 1 1 0 0 1 1 0 0
1 1 1 0 1 -1 1 T 0 0 1 0 1 1
1 1 0 1 1 1 1 i 0 0 1 0 1 0
1 0 1 1 1 1 1 i . 0 0 1 0 0 1
0 1 1 1 1 1 1 i 0 0 1 0 0 0
< 0
X X X 1 0 1
Channel Select x: Don’t care
The multiplexer is enabled only when the ‘Enable signal (EN)’ line is at logic 0. A ‘High’ on the EN
line forces the output to the inactive (Low) state. The input signal is switched to the output line through
the channel select control lines A2, A1 and AO. In order to select a particular input line, apply its binary
equivalent to the channel select lines AO, A1 and A2 (e.g. set A2A1A0 as 000 for selecting Input DO,
and as 001 for selecting channel Dl, etc.).
Introduction to Embedded Systems
When one output line is selected by the output selector control (S), the other output line remains in the
High impedance state.
candidate for handling input variables up to 6. Now let us try to implement a 2 input half adder combi¬
national circuit, for adding two one-bit numbers, using the K-map technique. The ‘Truth Table’ and the
corresponding K-map drawing! for the 2 input ‘half adder’ circuit is given below.
A 0
Input Output
foil: m
0 0 Tl 0 0 0
°/ A
0 1 1 0
1 0 1 0 / 1 /0 0 1
1 1 0 1
C : Carry K-Map for Output (0) K-Map for Carry (C)
The simplified logical expression for the output (T) and carry (Q of half adder is given below.
Y=AB+AB
C = AB
The logical expression AB+AB represents an XOR gate. Please refer to the ‘truth table’ of XOR gate
given under the ‘Logic Gates’ section. Using K-map it can be represented as:
0 0 0 0 1
o
0 1 l
1 0 l 1 1 0
1 1 0 K-Map for XOR Gate
Fig. 8.12) Truth Table and K-map representation for XOR Gate
Hence, the half adder circuit can be realised using an XOR A. Y=A\B+AB\
and an AND gate. The circuit implementation is shown in J8_
Fig. 8.13.
t Please refer the book ‘Modern Digital Electronics by R P Jain’ for more details on the K-map drawing and simplification methods.
Introduction to Embedded Systems
The S-R flip-flop is built using 2 NOR gates. The output of each NOR gate is fed back as input to the
other NOR gate. This ensures that if the output of one NOR gate is at logic 1, the output of the other
NOR gate will be at logic 0. The S-R flip-flop works in the following way.
1. If the Set input (S) is at logic high and Reset input (R) is at logic low, the output remains at logic
high regardless of the previous output state.
2. If the Set input (S) is at logic low and Reset input (R) is at logic high, the output remains at logic
low regardless of the previous output state.
• 3. If both the Set input (S) and Reset input (R) are at logic low, the output remains at the previous
logic state.
4. The condition Set input (5) = Reset input (R) = Logic high (1) will lead to race condition and the
state of the circuit becomes undefined or indeterminate (x).
A clock signal can be used for triggering the state change of flip-flops. The clock signal can be either
level triggered or edge triggered. For level triggered flip-flops, the output responds to any changes in
input signal, if the clock signal is active (i.e., if the clock signal is at logic 1 for ‘HIGH’ level triggered
and at logic 0 for ‘LOW’ level triggered clock signal). For edge triggered flip-flops, the output state
changes only when a clock trigger happens regardless of the changes in the input signal state. The clock
trigger signal can be either a positive edge (A 0 to 1 transition) or a negative edge (A 1 to 0 transition).
Figure 8.16 illustrates the implementation of an edge triggered S-R flip-flop.
Embedded Hardware Design and Development
From the circuit implementation, it is clear that for a J-K flip-flop, S = JQ\ and R = KQ. The J-K flip-flop
operates in the following way:
1. When J = 1 and K= 0, the output remains in the set state.
2. When J = 0 and K= 1, the output remains in the reset state.
3. When J= K = 0, the output remains at the previous logic state.
4. When J= 1 and K= 1, the output toggles its state.
A D-type (Delay) flip-flop is formed by connecting a NOT gate in between the S and R inputs 6f an S-R
flip-flop or by connecting a NOT gate between the J and K inputs of a J-K flip-flop. Figure 8.18 illus¬
trates a D-type flip-flop and its I/O states.
This flip-flop is known with the so-called name ‘Delay’ flip-flop for the following reason-the input to
the flip-flop appears at the output at the end of the clock pulse (for falling'edge triggering). \
A Toggle flip-flop or T flip-flop is formed by combining the J and K inputs of a J-K flip-flop. Figure
8.19 illustrates a T flip-flop and its I/O states.
Introduction to Embedded Systems
When the ‘F input is held at logic 1, the T flip-flop toggles the output
with each clock signal. An S-R flip-flop cannot be converted to a ‘T’
flip-flop by combining the inputs S and R. The logic state S=R=1 is
not defined in the S-R flip-flop. However, an S-R flip-flop can be con¬
figured for toggling the output with the circuit configuration shown in
Fig. 8.20.
The synchronous sequential circuit uses clocked flip-flops (S-R, J-
K, D, T etc.) as memory elements and a ‘state’ change occurs only in
response to a synchronising clock signal. The clock signal is common
to all flip-flops and the clock pulse is applied simultaneously to all flip-
flops. As an illustrative example for synchronous sequential circuit, let
us consider the design of a synchronous 3-bit binary counter.
The counting sequence for a 3-bit binary counter is given below.
From the count sequence, it is clear that the LS bit (Q0) of the binary counter toggles on each count and
the next LS bit (Qt) toggles only when the LS bit (Q0) makes a transition from 1 to 0. The Bit (Q2) of
the binary counter toggles its state only when the Qy bit and Q0 bits are at logic 1. This logic circuit can
be realised with 3 T flip-flops satisfying the following criteria:
1. All the T flip-flops are driven simultaneously by a single clock (synchronous design).
2. The output of the T flip-flop representing the Q0 bit is initially set at 0. The input line of Q0 flip-flop
is connected to logic 1 to ensure toggling of the output with input clock signal.
3. Since Q1 bit changes only when Q0 makes a transition from 1 to 0, the output of the T flip-flop
representing Q0 is fed as input to the T flip-flop representing the Qt bit.
4. The output bit Q„ changes only when Q0 = Qj = 1. Hence the input to the flip-flop representing bit
Q2 is fed by logically AND ing Q0 and Q
Embedded Hardware Design and Development
The circuit realisation of 3-bit binary counter using 3 T flip-flops is given in Fig. 8.21.
The Preset line is used for setting the output of flip-flop, whereas the Clear line is used for resetting
the output of flip-flops. The counter changes in accordance with the count pulses.
Another example for synchronous sequential circuit is ‘register’. A register can be considered as a
group of bits holding information. A D flip-flop can be used for holding a ‘bit’ information. An 8 bit
wide register can be visualised as the combination of 8 D flip-flops. The bit storing operation is con¬
trolled by the signal associated with a latch write (like a write to latch pulse). The figure given below
illustrates the implementation of a 4 bit register using D flip-flops.
R22) 4
< bit Register Implementation using D Flip-Flops
Introduction to Embedded Systems
The state of an asynchronous sequential circuit changes instantaneously with changes in input signal
state. The asynchronous sequential circuit doesnot require a synchronising clock signal for its operation.
The memory element of an asynchronous sequential circuit can be either an un-clocked flip-flop or logi¬
cal gate circuits with feedback loops for latching the state. As an illustrative example, let us explore how
we can implement the above 3-bit binary counter using an asynchronous sequential circuit. From the
count sequence of the 3-bit binary counter, it is clear that the least significant (LS) bit (Q0) of the binary
counter toggles on each count and the next LS bit (Qj) toggles only when the LS bit (Q0) makes a transi¬
tion from 1 to 0. The most significant (MS) Bit (Q ) of the binary counter toggles its state only when
the Qj bit makes a transition from 1 to 0. This logic circuit can be realised with 3 T flip-flops satisfying
the following criteria:
1. The T input of all flip-flops are connected to logic high (it is essential for the toggling condition'
of the T Flip-flop).
2. The pulse for counting is applied to the clock input of the first T flip-flop representing the LS bit
%
3. The clock to the T flip-flop representing bit Q( is supplied by the output of the T flip-flop repre¬
senting bit Qq. This ensures that the output of Qj toggles only when the output of Q0 transitions
from 1 to 0.
4. The clock to the T flip-flop representing bit Q2 is supplied by the output of the T flip-flop repre-
, senting bit This ensures that the output of Q2 toggles only when the output of transitions
from 1 to 0.
The circuit realisation of the 3-bit binary counter using 3 T flip-flops in an asynchronous sequential
circuit is given below.
The Preset line is used for setting the output of flip-flop, whereas the Clear line is used for resetting
the output of flip-flops. Here the input line (T) of all flip-flops is tied to logic 1 permanently. Hence, the
clock signal to each flip-flop can be treated as the input to the flip-flop, instead of treating it as a clock
pulse.
The table given below summarises the characteristics, pros and cons of synchronous and asynchro¬
nous sequential circuits.
In the beginning of the electronics revolution we started building electronic circuits around the early
vacuum tube technologies. As technology progressed, transistors came into the picture and we shifted
our circuit designs to transistor based technology. In'the ancient times thousands of individual transis¬
tors were required to build a digital or analog functionality. The transistors were interconnected us¬
ing conductive wires for building the required functionality. As technology gained new dimensions,
the concept of miniaturisation evolved and researchers and designers were able to build the required
functionality within a single silicon wafer, in places of thousands of interconnected transistors. An inte¬
grated circuit (IC) is a miniaturised form of an electronic circuit containing transistors and other passive
electronic components. In an IC, the circuits, required for building the functionality (say Processing
logic: CPU), are built on the surface of a thin silicon wafer. The first integrated circuit was designed
in the 1950s. Depending on the number of integrated components, the degree of integration within an
integrated circuit (IC) is known as:
Small-Scale Integration (SSI): Integrates one or two logic gate(s) per IC, e.g. LS7400
Medium-Scale Integration (MSI): Integrates up to 100 logic gates in an IC. The decade Counter 7490
is an example for MSI device
Large-Scale Integration (LSI): Integrates more than 1000 logic gates in an IC
Very Large-Scale Integration (VLSI): Integrates millions of logic gates in an IC. Pentium processor
is an example of a VLSI Device.
The IC design methodology has evolved over the years from the first generation design, SSI to de¬
signs with millions of logic gates. The gate count is a measure of the complexity of the IC. In today’s
world almost all IC designs fall under the category VLSI and it is a common practice to term IC design
as VLSI design. Depending on the type of circuits integrated in the IC, the IC design is categorised as:
Digital Design: Deals with the design of integrated circuits handling digital signals and data. The
I/O requirement for such an IC is always digital. Microprocessor/microcontroller, memory, etc. are
examples of digital design.
Introduction to Embedded Systems
Analog Design: Deals with the design of integrated circuits handling analog signals and data. Analog
design gives more emphasis to the physics aspects of semiconductor devices such as gain, power dis¬
sipation, resistance, etc. RF IC design, Op-Amp design, voltage regulator IC design, etc. are examples
for Analog IC Design.
Mixed Signal Design: In today’s world, most of the applications require the co-existence of digital
signals and analog signals for functioning. Mixed signal design involves design of ICs, which handle
both digital and analog signals as well as data. Design of an analog-to-digital converter is an example
for mixed signal IC design.
Integrated circuit design or VLSI-design involves a number of steps namely, system specification,
functional or architectural design, functional simulation, logic synthesis, physical layout (placement
and routing) and timing simulation. As in the case of any other system design, VLSI design also starts
with the system specification in which the requirements of the chip under development are listed out.
The system specification captures information on ‘what the chip does?’, the input and output to the
chip, timing constraints, power dissipation requirements, etc. (e.g. For an IC for AND gate implementa¬
tion, the intended function is logical ANDing of input signals and input per gate is 2 and output is 1).
The functional design or architectural design involves identifying the various functional modules in
the design and their interconnections. It represents an abstraction of the chip under development, com¬
puter aided design (CAD) tools are available for architectural design. The functional aspects of a chip
can be captured using CAD tools with various methods like block diagrams, truth tables, flow charts,
state diagrams, schematic diagrams and Hardware Description Language (HDL). This process of enter¬
ing the system description into the CAD tool is known as Design Entry. The truth table based design
entry uses the truth table of a logic function. This method is suitable for the realisation of logic func¬
tion implementation involving lesser number of logic , variables. The schematic capture based design
entry' uses the schematic diagram created with an EDA tool (Please refer to the section on Schematic
Capture using Capture CIS of this chapter for more details on Schematic Capture). Flow charts can
also be used for design entry because of their inherent flow structure to describe any sequential flow.
State diagrams are employed to display state machines graphically. State diagrams are used for mod¬
elling sequential circuits, characterised by a finite number of states and state-transitions. A Hardware
Description Language (HDL) based design flow involves describing the design (circuit) to be realised
in a Hardware Description Language like Very High Speed Integrated Circuit HDL (VHDL) or Ver-
ilog HDL. HDHis. similar to software application programming languages but is used for describing
hardware.
The functional simulation process simulates the functioning of the circuit captured using one of the
above-mentioned design entry techniques, using a functional simulation tool. During simulation, input
stimuli are applied to the circuit and the output is verified for the required functionality. The logic syn¬
thesis phase involving optimisation and mapping transforms the design into a gate level netlist corre¬
sponding to the logic resource available in the target chip. The chip design may be for the development
of a custom ASIC or for implementation of the required functionalities of the IC in a standard Program¬
mable Integrated Circuit like CPLD or FPGA. In case of CPLD, the logic gates are realised in terms
of the gates available in the macrocells of the CPLD, whereas for FPGAs the logic gates are realised
in terms of the Look Up Tables (LUTs). The optimisation and mapping process optimises the logic ex¬
pressions for speed, area and power and maps the design to the target technology (FPGA, CPLD, etc.)
keeping their functionality unaltered. The physical design or layout design is the process of implement¬
ing the circuit using the logic resources present in the final device (CPLD, FPGA) or creating the logic
partitioning, floor planning, placement, routing, pin assignment, etc for a custom IC (ASIC), using a
Embedded Hardware Design and Development
supported CAD tool. The Timing Simulation is performed using CAD tools for analysing the propaga¬
tion delay of the circuit, for a particular technology like FPGA, CPLD, etc. For custom ICs (ASICs), the
physical design converts the circuit description into geometric description in the form of a GDSIFfile.
The GDSII file is sent to a semiconductor fab for fabricating the IC. The IC is fabricated in silicon wafer
and finally it is packaged with the necessary pin layouts. The following section gives you an overview
of the VHDL based VLSI design.
v ' i , Representation/Remark
*, < ’ - : r-v w A “‘'<5 " -
Comment ~ (Start with two adjacent hyphens)'.^
< ., ~ { s' J
Reserved words and programmer chosen names tor entity £
Supports any length and characters A:Z, a-z, numbers 0-91
j (J. Non-case sensitive. Must'start with a character •
Bit strings Arrays of bits with base Binary (B), Octal (0) or Hexadecimal (X).
e.g. B“100” Only 1 and 0 allowed in string
0“127” numerals only 0 to 7 are allowed
X“1F9” Numerals 0 to 9 and letters A to F, and a to f are allowed in string
Numerals universaljnteger does not include points, e.g. 100 " 'V, l LA I
universal real include a point, e.g. 100.1 -r . ) T V" 'V
The special character ‘J can be used fonincrisasing the readability e.'g. 1000
represented as 1 000 , ;
Character E or e can be used for including exponent, e.g. 1E3
‘#1 can beused for describing the base of the numbering system, e.g. 2#011
2 and representing number 3. Base can be between 2 and 16
ehhhnmi
' *■ * ' ‘
i,»». *7 -..:.;V !■•■.
is^sed^o%ciarit
ised tor represe
used, for represe;
al: :A|
The basic structure of a VHDL design consists of an entity, architecture and signals. The entity decla¬
ration defines the name of the function being modelled and its interface ports to the outside world, their
direction and type. The basic syntax for an entity declaration is given below.
Entity name-of-entity is
Port (list of interface ports and their types);
Entity item declarations;
Begin
Statements; \
End entity name-of-entity
The architecture describes the internal structure and behaviour of the model. Architecture can define
a circuit structure using individually connected modules or model its behaviour using suitable state¬
ments. The basic syntax of architecture body is given below
Architecture name-af-architecture of name-of-entity is
Begin
• Statements;
End architecture name-of-architecture;
Signals in VHDL carry data. A signal connects an output and an input of two components (e.g. Gates,
flip-flops, etc.) of a circuit. A signal must be defined prior to its use. 1
Embedded Hardware Design and Development
e.g. Signal a: bit; This is an example of a signal ‘a’ which can take values of type bit.
Once a signal has been defined, values can be assigned to the signal.
e.g. a <= ‘ 1’; In this example a value ‘ 1 ’ is assigned to the signal ‘a’.
Similar to software languages like ‘C’, where functions are stored in a library which can be re-used,
in VHDL also all compiled modules are stored in library. Packages are also stored in a library. A package
may contain functions, types, components, etc. For using components from a particular library, first the
library and packages must be specified in the VHDL code as
When a VHDL module is compiled, it is saved in the work library by default. As per the VHDL stan¬
dard the work library is always visible and need not be specified in the VHDL code. If other packages
or libraries are to be used, they must be defined before the entity declaration,
e.g.
The above library definition defines that all data types, functions, etc available in the package std_
logic_l 164 in the library ieee can be used in the underlying entity. As an example for VHDL based
design, let us consider the design of a D flip-flop. The VHDL description of the D Flip-flop is given
below.
End if;
End Process; *
End DFF ARCH;
In the above example “DFF” is the name of the model and D, CLK, Q are the interface ports. The
architecture describes the internal structure and behaviour of the model. In the above example, the ar¬
chitecture “DFF-ARCH” describes the D flip-flop function.
The HDL model of the circuit is then complied and loaded in an HDL simulator. The simulator en¬
ables the designer to apply the input stimuli to the design and observe whether the output response is
as expected. If there are any functional errors, the HDL code is corrected and the design re-simulated,
till the design meets the functional specifications. In the above example on simulation, by applying a
i clock and data inputs the model functions like a D-flip-flop. The design is now ready for synthesis. A
Introduction to Embedded Systems
synthesiser tool compiles the source code to an optimised technology dependent circuit at the gate level.
The inputs to a synthesiser are the HDL code, a technology library, and constraints. The constraints are
in terms of the area and speed requirements of the circuit to be realised in the target technology. This
process is done in two phases:
Compilation: The HDL is translated to a generic netlist
Optimisation: The generic netlist is mapped to a target tech¬
nology (ASIC/FPGA) satisfying the requirements of area and
speed.
On synthesis, the VHDL model described above results in a
D flip-flop to be realised as shown in Fig. 8.24.
The next phase of implementation is physical layout (Place
and Route). In the case of an FPGA implementation, during the placement stage the various instances
in the netlist are mapped and their relative position inside the target FPGA resources is fixed. During
the routing phase, the interconnection between the various placed instances is done as per the netlist to
realise the circuit. Once the layout is complete, the final step is timing simulation. The design is now
re-simulated with the actual component and interconnect delays to verify the functionality. Figure 8.25
illustrates the various steps involved in a HDL based design.
Early embedded products were built around the old transistor and vacuum tube technologies, where the
designers built the PCB with their hands, oil paper, pencil, pen, ruler and copper plates. The process
of building a PCB was highly cumbersome in ancient times where the designers sketch the required
Embedded Hardware Design and Development
connections using pen, pencil and ruler on papers and the finished sketch was used for etching the con¬
nections on a copper plate and it took weeks and months time to finish a PCB. The more the inter con¬
nections involved in the hardware, the more difficult was the process. The accuracy and finishing of the
PCB was highly dependent on the artistic skills of the designer. Today the scenario is totally changed.
Advances in computer technology and IT brought out highly sophisticated and automated tools for PCB
design and fabrication. The process of manual sketching the PCB has given way to software packages
that gives an automatic routing and layout for your product in a few seconds. These software packages
are widely known as Electronic Design Automation (EDA) tools and various manufactures offer these
tools for different operating systems (Windows, UNIX, Linux etc). EDA tool is a set of Computer Aided
. Design/Manufacturing (CAD/CAM) software packages which helps in designing and manufacturing
the electronic hardware like integrated circuits, printed circuit board, etc. The key players of the EDA
tool industry are Cadence, Protel, Altium, Cadsoft, Redac, Merco, etc. OrCAD, Cadstar, Protel, Eagle,
etc. are the popular EDA tool packages available in the market for PCB design. Of these, I feel Cadence
OrCAD as the most flexible and user friendly tool. The reference tool used for hardware design through¬
out this book is OrCAD. An evaluation copy with limited features (for Windows OS) is available for
free download from the Cadence website www.cadence.com. Readers can use this tool for their hard¬
ware design. Remember this tool is a demo version and you cannot use this for any commercial design
and it does not provide you the full features of the licensed version.
^ xx.x Represents the version number of the tool. If the version is 10.0, xx.x becomes 10.0
1 The explanation given here is for Microsoft® Windows XP operating system
‘ The ‘Menu’ option may vary with change in version of the application. If you find it different from the one explained here, please refer
to the user’s manual of the application provided by Cadence.
(Vig. 8.26) OrCAD Capture CIS Tool
finally click the OK button. Now a schematic capture project is created at the specified location with
the specified name and the schematic tool opens default page (PAGE1) with various schematic draw¬
ing tools on the right side for creating the schematic (Fig. 8.28). Close the default page by clicking the
page close button ‘X’. Now you will be taken to the project window where you can view the following
categories.
On creating a new project, two files are created at the location for the new project with extensions
.opj and .dsn. The file names will be the same as that of the project name. The .opj file stands for OrCAD
Embedded Hardware Design and Development
Design Resources
'1-► AProjeetName.dsn
-► Library
Outputs
Referenced Projects
project file and it holds all project related settings for the project. The .dsn file represents the data source
name file holding the schematic drawing settings and data.
The .dsn tab in the project window is an expandable tab and it contains the ‘SCHEMATIC’ and
‘Design Cache’ details as shown in Fig. 8.29.
To start with the schematic design, double click on the ‘PAGE1 ’ tab under the ‘ SCHEMATIC 1’ node.
It will take you to the schematic drawing canvas along with the drawing tools on the right side of the
canvas. ‘PAGE1 ’ is created by default on creating a new project. You can change the name of it by right
clicking ‘PAGE1’ and choosing the ‘Rename option’. The schematic drawing canvas details are given
in Fig. 8.30.
(Resistor, capacitor, IC, etc), to interconnect different components (a connection between two points),
to add a text description, etc. The available schematic drawing tools and their usage are described in the
following sections.
jg|| Place Part: Component placing tool. Place various components like resistor, capacitor, different
iHt ICs, etc on the drawing canvas. Invoking this button pops up a place part dialog box as shown in
Fig. 8.31.
Place Part
Part I f‘;l| \ 5 f ’
AT76C1OE/MISC
AT76C171/MISC
AT7GC176/M ISC
AT87F51/MICROCONTROLLER
AT87F52/MICR0C0NT ROLLER
AT 87F52/LCC/MICR 0C0 NTR 0 LLE R
! AT83C1051 /MICROCONTROLLER
AT89C1051/SO/MICROCONTROLLER
AT89C2051/MICROCONTROLLER
AT89C2051 /SO/MICROCONTROLLER
AT89C51/MICROCONTROLLER
d AT S9C51 /F P/M ICR 0 CO N TRO LLE R xr Help :
Libraries: , £ * VC *Y BtagSc™ -y- ^H
GATE A * Nomal
LATCH
LIN E DR IVE R RECEIVE R
MECHANICAL / ■A Tv'':':dT:’:TYT)k Add ""
MICROCONTROLLER -Packaging t
MICROPROCESSOR ?-* : Parts per Pkg 1 '
MISC ■Wfc
MISC2
MISC3 I". • m
MISCLINEAR |vr n Tjiper-Hbmbgeneous i
MISCMEMORY
- -
The manufacturer of different ICs and other components give them a Part Number/Part Name and the
component is identified by this name/number. OrCAD incorporates a number of libraries to incorporate
the skeletal drawing of these components. The library is located at the path /OrCAD Root Directory/
tools/capture/library, where /OrCAD Root Directory is the installation directory of OrCAD tool. You
can add these libraries using the ‘AddLibrary...' button. Add the library you want by using this button.
Each library contains specific skeletal drawings. For example, the Library' ‘GATE’ contains the skeletal
diagram (Pin Diagram) of almost all available Logic Gate ICs from different manufacturers. Similarly,
the Library ‘MICROCONTROLLER’ contains the skeletal sketch (Pm Diagram) for almost all com¬
mercially available microcontrollers. If you are not sure about the component you are going to use
belongs to which library, select all libraries by pressing ‘Ctrl + A’ key in the ‘Libraries’ box (remember
Introduction to Embedded Systems
the libraries will only be seen in the Libraries section if you add the libraries explicitly to your project
using the ‘Add Library’ option). If you are certain, on which library the component you are going to
add belongs, you can select that particular library by clicking it on the libraries section. For example,
if you want to add AT89C51 microcontroller, you can go for two options either use the ‘MICROCON¬
TROLLER’ library or use the entire libraries.
To select a component, type its name under the ‘Part:’ section. The part selector supports auto com¬
plete for selecting part numbers. If you type the first few words of the part number, the .auto complete
will display the list of components starting with the typed part number. A preview of the selected com¬
ponent is also shown on the right comer of the ‘Place Part’ pop-up dialog box. The ‘Part Search... ’ func-
tion will help you to find out the library that contains a particular Part (component). Once you select the
part, click ‘OK’ button of the ‘Place Part’ pop-up dialog box. Move the cursor within the drawing canvas
and place the selected part on the desired location of the drawing canvas. Press down the mouse cursor
and then press ‘ESC’ key to end the place operation.
—i Place Wire: Component interconnection tool. It interconnects various components like resistor,
*— capacitor, different ICs etc within the drawing canvas. Choose this tool and click at the starting
point where the connection needs to be started and move the mouse till the end point of the connection.
The wire follows the mouse movement. Press ‘Esc’ when you are done with the connecting action.
Place Net Alias: Tool for interconnecting various components like resistor, capacitor, different
ICs, etc within the drawing canvas without using a direct interconnection using ‘Place Wire’ Tech¬
niques, This is used if the number of interconnections within a drawing is large and complex. Clicking
this tool pops-up a dialog box to input the Net Alias name. Input the Alias name and Pxess OK button.
Embedded Hardware Design and Development
Now your mouse pointer carries the Net Alias. Move the mouse pointer to the start point where the con¬
nection starts and place it there. Take the tool again, select the same Net Alias and place it at the other
end, where the connection needs to be terminated. If you add the same Net Alias each to different inter¬
connection points, each of them gets connected together. The schematic diagram given in Fig. 8.33
illustrates the use of ‘Place Net Alias' tool.
AT89C51
AT89C51
B | Place Bus: Places an address or data bus in the schematic. You can avoid connecting individual
t wires of the bus one-to-one between the source and destination if this is used in combination with
the ‘Place bus entry’ Tool. For 8051, the address bus A0-A7 and A8-A15 and the data bus D0-D7 can
be represented using this.
Place Bus Entry: Used in combination with the Place Bus Tool. It connects each individual lines
of the bus into the common bus.
_Qp Place Port: This tool associates a group of connections together and it is referred as port. Port
A - connection is of three types; namely bidirectional connector, incoming port connector and outgo¬
ing connector. The bidirectional port connector connects a bidirectional port, e.g. D0-D7 of Data bus
(PO) for 8051. The outgoing connector implies that the corresponding port is originating from the device
(output line) and is intended to feed as an input line to other devices. For example, the address lines of
8051, A8-A15 originates from the chip and it should be connected to the I/p line of any other device
requiring these lines. The address lines (A8, A9, A10 to A15) are grouped using the ‘Place Bus’ tool and
each address pins (A8, A9,..., A15) are connected to the address bus using the ‘Place Bus Entry’ tool.
Finally the bus is made available to the rest of the devices by connecting an outgoing Port to the bus.
The port holds a name and if the same name is used by a bus with an incoming Port, it will give the same
result as they are interconnected together directly. The same is illustrated in Fig. 8.34.
Place Junction: This tool places a junction implicitly on any part of the wiring. This tool can be
&■' used in the same way as other tools, click on the tool, move it to the desired point on the wiring
already done and press the mouse. Press ‘Esc’ Key to end the process.
PUR Place Power: Used for placing a power connection to the power line or to the power supply pin
^ of different components.
Introduction to Embedded Systems
[A8...A15]
(Fig. 3.34 Illustration of Bus, Bus Entry and Port tool usage
6ND Place Ground: Used for placing a ground connection to the ground line or to the ground pin of
different comp onents.
Iw I Place No Connect: Some components may have some pins left unconnected. The No connect
" f tool is used for representing non-connected pins.
Place off Page Connector: For modularity, the schematic can be drawn in different pages with
each page representing the associated hardware for that particular module. For example, the sche¬
matic can be drawn on different pages for power supply module, processor module, PC interface module
etc. If a connection is required from one page to another page it is achieved by using the ‘Off Page Con¬
nector’ tool. If a connection is going out from a page to other pages, it is represented using an outgoing
‘off Page Connector’ (Fig. 8.36) in that page and if a connection is coming into a page from any other
pages it is represented using an incoming ‘off Page Connector’ in that page.
* 8.6.2.1 Schematic Part Creation, Formatting and Labelling Tools Certain parts may not be
available as readymade part in the Part library of the tool. In such situations, if you know the Pin number
for the part you want to create, you can create it using the following part creation tools. Labeling tools
are used for putting text labels and others in the canvas to give more readability and understanding of
the schematic drawing.
Place Rectangle: Creates a rectangle within the drawing canvas. This rectangle can either be used
—« as an outline to a New Part to be created or as an outline to a labelling text or to isolate some part
of the schematic from other area to give visibility.
Place Line: Used for creating Pins on the Rectangular Part for part creation. When used as a for¬
matting tool it is used for drawing lines to separate different areas of the schematic diagram.
Embedded Hardware Design and Development
[A8...A15]
P0.0/AD0 t P2.0/A8 r
P0.1/ADI > P2.1/A9 [
rU.2/ADZ r2. .2/A 1U
P0.3/AD3 P2.3/A11
P0.4/AD4 P2.4/A12
P0.5/AD5 P2.5/A13
P0.6/AD6 P2.6/A14
P0.7/AD7 P2.7/A15
P1.0 P3.0/RXD
PJ J P3.1 /TXD
P1.2 P3.2/INTO
PI.3 P3 .3 /in n
P1.4 P3 .4/TO
PI.5 P3 ,5/Tl
PI.6 Pj .6/W R
P1.7 P3.7/RD
27 -
— WE §
’XTALl 4LE/PR OG - OE §
"XTAL2 PSEN
No Connect [A8...A15]
EA/VPP 3
RST 3
AT89C51o
CnI
-Ground Connection
fig. 8.35J Illustration of Junction, Power, Ground and No Connect tool usage
Place Poly line: Tool used for creating poly lines on the drawing page.
ill1 j Place Ellipse: Place an elliptical shape within the drawing canvas.
■O ■ Place Arc: Tool used for placing different forms of arcs and circle in the drawing area.
j jpfc-; Place text: Tool used for creating text labels for documenting the drawing and to add Pin names,
description, Part Number etc in the new part creation.
258 Introduction to Embedded Systems
8.6.2.2 Creating a New Schematic Page As mentioned earlier you can provide modularity to the
schematic drawing by placing the schematic drawing of different modules in different pages. If your
drawing is a simple drawing you can use a single page. If the drawing does not fit into a single page, you
can create a new page by going back to the project window by closing the current page. (Don’t forget to
save your changes © before closing the page you were working).
Refer the Fig. 8.29 (‘Project view for new project showing schematic page view’) for schematic
page view. In the schematic Page view, right click on the ‘SCHEMATIC1’ and select ‘New Page’
Title
<Title>
The ‘Title’ section is used for entering the title of the Schematic (e.g. 8051 Evaluation board). You
can also add the name of the person who carried out the drawing. ‘ Size ’ indicates the size of the drawing
page. To get the details of the ‘Size’ take the ‘Schematic Page Properties..tab from the ‘Options’ menu
when the page is in the opened state. (Options -> Schematic Page Properties). Whatever values you set
there is taken as the default value for a page.
The document number should be given according to the format specified by the QA standard you are
following, e.g. ISO standard. ‘Rev’ gives the version number of the drawing (Version number increases
with each revision). Date represents the date on which the schematic is created. ‘Sheet No.’ represents
the Sheet/Page Number in a multi sheet/page drawing. Out of these ‘Title’, ‘Document Number’ and
‘Rev Code’ is very important in reviewing the document. A typical schematic drawing details table is
illustrated in Fig. 8.38.
Title
8051 Evaluation Board
Once the schematic is finished, part numbers should be assigned to the various components present
in the schematic and error checking should be performed. If there are no errors, you can proceed to the
next step, the netlist creation. The following sections illustrate each of these steps, namely ‘Part Number
Creation’, ‘Design Rule Check’ and ‘Net List Creation’.
the relative positioning of the component within the schematic Page (x andy co-ordinates of the com¬
ponent). The topmost component is given the reference number 1 and reference number 2 to the next
component, and so on.
To generate automatic Part numbers, close all sehematic pages and come back to the project view and
highlight the dsn name or schematic name (In our example the dsn is ‘8051_projectl .dsn’ and schematic
name is ‘SCHEMATIC1’). Click on the ‘Tools’ menu from the main menu associated with the project.
Select ‘Annotate...' The following window as depicted in Fig. 8.40 pops up on the screen.
-i -
Anwtalf : ,V. -;Cf:
§!_>r , y. • ' ■ :
fE 1 1
|. yl JT'g|;|;
f: :;f 1, If v;
1 g -
f *
1 ■ •
1 • ^
BBBBloaS m f
1 Mgga • • ii
l-A-P .. :. t -. si a
hbeb mm \
* , 11 i_ < *- ?j|
ailESEi!
( Fig. 8.4o) Part Number generation settings
From the pop-up window, select the option ‘ Update entire design’ for ‘Scope', ‘Resetpart referenc¬
es to “?”' for ‘Action' and ‘ Update Instances' for ‘Mode' and then press the OK button. This prompts
for a confirmation. If you respond yes to the confirmation, every part number is re-set with “?” (‘C?’,
‘R?’, etc.). If you open the schematic page you can see this. The action performed is to reset all refer¬
ences to a default value. To assign the part numbers, go to the ‘Tools’ menu again and take ‘Annotate... ’
option. The same window pops up. Choose the option ‘Incremental reference update' for ‘Action’ item
and keep the rest same as the above settings. Pressing ‘OK’ button prompts again for user confirma¬
tion and if you confirm it, the design part number is updated automatically. It is to be noted that no two
components will have the same Part Number. Eventhough there are two or more capacitors or ICs or
resistors present in the schematic diagram with the same values (e.g. 0.1MFD (Microfarad) for capacitor
or 8.2K (kilo ohms) for resistors, etc.) different part numbers should be used for them. Part numbers are
very helpful in PCB routing, soldering of components in the finished PCB and in the creation of Bill of
Materials (BoM).
Embedded Hardware Design and Development
Select the option ‘Check entire design' from 'Scope', 'Check design rules' from 'Action'. Checkthe
following options among the'Report' category by selecting the corresponding check box.
Report
• Create DRC markers for warnings
• Check hierarchical port connections
• Check off-page connector connections
• Report identicalpart references
• Report invalid packaging
• Report hierarchical ports and off-page connectors
• Report visible unconnected power pins
• Check unconnected nets
• Report all net names ^
You can select the location for the DRC report file to be created. By default it is generated with name
'project name.drc’ (erg. 5057jprojectl. drc) and it is created at the project location directory.
The .drc file gives you the details of any errors present in the schematic along with the point (x & y
co-ordinates) in schematic diagram where the error isoccurred. Sometimes the .drc may report warn¬
ings. You can ignore some of the warnings. But you must look into the warnings and should confirm that
Introduction to Embedded Systems
the warnings will not create any issue in the total circuit. Examples of warnings that may be ignored are
“Bi-directional pin connected to input pin”, “Output pin connected to Power line”. The second example
is a very common one where the o/p pin of regulator gives the regulated supply and it usually gets con¬
nected to the power pins of other ICs in the circuit. Here, though the o/p Pin of the regulator IC is giv¬
ing the o/p power, the ‘Pin Type’ will be output and you are connecting that pin to a ‘Pin Type’ power
of other ICs. This is a possible Pin type conflict. You can either discard this warning or can avoid this
warning by changing the ‘Pin Type’ of the regulator o/p Pin to ‘power’ by right clicking the Regulator
IC component in the schematic and choosing the ‘Edit Part’ option. Double click on the pin, for which
you want to change the ‘Type’, choose the desired type.
Don’t ignore all warnings blindly. First look at the warning and understand its consequence in the
schematic. If you are sure that it will not create any impact on the schematic, simply ignore it or if you
have enough time, rectify it by. resolving the condition that created the warning.
1 Scop* - - - ; •’ pModc • -? . •:
OK' tgjggj
( • Process enWe des'gn !,* Use instances (Piefetred) s _ s
Cancel .V
11 ''''Processselection Iff ’ j ^'Useecotrrenbes 1 7 Hj
Help |
tinettemDeiiintion ^ * f ~~-z ' •
t’J{llem}M{Quanlily}\l{Refe(ence}\UValue}
Select ‘Process entire design’ from ‘Scope’ and ‘Use instances (Preferred)’ from ‘Mode’. The de¬
fault location for the report file is the Project directory and the BOM name is the project name with
extension .bom. You can change the report file location by giving the desired path name in the edit box
present in the ‘Report’ section. Press ‘OK’ to proceed. The BOM is generated automatically and it is vis¬
ible under the ‘Outputs’ section of the Project Window. The BOM can be opened for viewing by double
clicking on the .bom file. A sample BOM file is shown in Fig. 8.43.
<■) - i
If you observe the BOM closely, you can find that the BOM gives different types of information to
the different type of users. Take the first item in the BOM list. For a person who is interested in procur¬
ing the components, the first line of BOM gives the information on the quantity required for capacitor
with value 0.1MFD per board is 3. For a person who is involved in the assembling of components on
the board, the first line gives the information, the part numbers with reference values Cl, C3, C9 should
be soldered with capacitors with value 0.1MFD.
If you plan to use OrCAD ‘Layout’ as the layout tool, select the tab ‘Layout’ from the pop-up win¬
dow and select the options as given in Fig. 8.44 andproceed. The c Net list' is generated automatically
with extension .mnl and it will be shown in the ‘ Output’ section of your project window.
If Cadence ‘Allegro’ is your layout tool, select the tab ‘Other ’ instead of ‘Layout’ from the ‘Create
Netlist’ pop-up window as shown in Fig. 8.45.
Select ‘ telesis.dll' from the ‘Formatters' option and change the ‘Netlist’ file creation directory if you
want (By default the ‘Netlist’ file is generated at the project directory with same name as the project
name and extension .net) and press ‘OK’ to proceed. Av
The ‘Netlist' file is generated automatically and it is visible in the ‘Outputs’ section of the project
window. If you open the ‘Netlist’ by double clicking the .net file from the ‘Outputs’ section, a file de¬
scribing the various nets is displayed. A sample ‘Netlisf file generated in teles is' format for “Allegro”
layout tool is shown in Fig. 8.46.
On a closer look at this file, you can find that it is a representation of the packages (will be described
- in a later section of this chapter) for various components of the schematic and a ‘soft form’ representa¬
tion of the interconnections among the components present in the schematic. The .net list file contains
twoisections namely-PACKAGES section and NETS section. The packages section defines the pack¬
aged of different components in the schematic and the NETS section lists out the various connections in
Introduction to Embedded Systems
the schematic. A group of connections is given a net,name and the component parts which are getting
interconnected at that net are listed against the net naiiie. In. the net list file given above, the first net is
Embedded Hardware Design and Development
named as N1274902 and it represent the interconnection of Pin 2 & 3 of Jack JACK1 and Pin 1 of Diode
Dl. You can verify this interconnection by referring back to the schematic diagram in which Pins 2 & 3
of JACK1 is connected to Pin 1 of Diode Dl. The ‘.net’ file is fed as input to the layout tool for generat¬
ing the PCB layout.
Single In-line Package Single In-line Package is a single line package where the pins are arranged
in a single row with specific pitch spacing. The pins can be either ‘Through hole’ pins or ‘Surface
mount leads’. For ‘Through hole’ pins, the footprint represents the number of pins by drills or vias and
DIP contains chip body and through hole pins on both SEATING
PLANE
sides of the body of the chip. DIP package itself is r .065(1.65)
.161(4.09)
varying, depending on the body width of the IC, the .125(3.18^
.0.16(381)
,022(.659)
pin spacing (lead pitch), etc. The commonly used DIP .065(1.65)
.014(356)
: 110(2.79) .041(1.04)
is with 1 inch pin spacing and 0.3 inch to 0.6 inch .090(2.29) .630(16.0)
body width. The number of pins for DIP varies from P“.590(16.0)“*
3 .2 1
f " 1
El 4,30 440 4.50
r -155n - - mrm
h-0 IT' A2 0.80
4
e aes BSC
Si do View L
Li 1,00 REF
(Fig-8-52) 8 PinTSSOPPackage
Courtesy of Atmel (www.atmel.com)
0. £<7
♦
N
l N
packages in layout design. Interested readers are requested to visit the websites www.atmel.com and
www.maxim-ic.com for getting additional details on packages.
8.7.1.2 Routes/Traces ‘Routes’ are the interconnection among the pins/leads of various com¬
ponents. In schematic capture, it is represented as an electrical connection line. Whereas in layout,
the interconnection is represented as copper track interconnection among the leads/pins of different
footprints. This copper interconnection is knqwn as ‘PCB Track’. The width of the copper track can
Embedded Hardware Design and Development
8.7.1.5 Marking Text and Graphics Marking text and graphics provide information like part
number, part name, polarities of polar components (like capacitors, diodes, etc.), pin number, product
name, company name, board name, graphic outline of components, etc. These text and graphics are
printed on PCB after fabrication (Fig. 8.56). Refer to section Silk Screen for more details.
Introduction to Embedded Systems
rftn? 0.1H F Q
Graphics
t The name given here indicates the name of the demo version tool. It may be different for the commercial version of the tool. Please
refer to the OrCAD documentation for more details.
Embedded Hardware Design and Development
Select "New' from the "File' menu. A pop up window (AutoECO) describing the inputs to be fed to
the tool appears on the screen (Fig. 8.58).
I Biowse |
| AutoECO ir]
T~ When a pad's ret changes, tipup the entire track rather than ust the last segment
Layout TCH or TPL or Max file This is the reference template and you can select the _default.tch
file for this; Clicking on the ‘Browse’ button by the side of the edit box for this section guides you to the
directory containing the tch/tpl/max file. Select ‘_default.tch’ from it.
Input MNL netlist File This is the section for inputting the "Netlist' file created using OrCAD "Cap¬
ture CIS' schematic design tool. The file extension of the "Netlist' file specifically created for "Layout'
in the Capture CIS tool is ".MNL'. This "Netlist' file, containing the component information and com¬
ponent interconnection is going to be transferred to a PCB layout format. In our case the netlist file for
the design for power supply part of the 8051_Project is POWER SUPPLY.MNL.
Output File Section This is the O/p file generated in layout corresponding to the .MNL "Netlist’ file
input. By default its name appears same as netlist name and the file extension is ".MAX'
Introduction to Embedded Systems
Select the default values for all other options and press ‘Apply ECO’ button to create the new layout
file. The ‘Netlisf is processed and if it is error free, the layout tool associates each component’s package
(footprint) as per the ‘Netlisf file. The ‘Netlisf file associates a package to each component as per the
package value given for each component given in the schematic diagram (which is created using Cap¬
ture CIS). To assign a package value to a component at the time of schematic design itself, right click the
component in the schematic page and edit the package information from ‘Edit Properties... ’ section. If
you do not assign the correct package name to a component during schematic creation, a default pack¬
age is associated to the component. You have to search the layout library for the different package names
corresponding to the various packages. If the layout tool is not able to find out a package associated with
a component, at the time of layout creation, it will prompt for the package name for the component.
You should select a package for the component from the existing component library or should create
a new package if the package is not present in the library (refer to layout package creation and usage
documentation by OrCAD for more details). In our power supply example we haven’t associated any
package name for any of the component during schematic design with Capture CIS. Hence the layout
tool will prompt for the package for each component is shown in Fig. 8.59.
Select the option “Link existing footprint to component...” and link a footprint to the component
(Fig. 8.60)
The set of available footprint libraries and the. footprints available in the selected library is listed on
the footprint selection dialog box. As you move between different footprints, its preview also is shown
on the selection dialog box. Since we are using the demo version, the number of footprint libraries and
footprints within each library are limited. Select the footprint fitting to the package of your component
in terms of size. Repeat the selection for all components.
On completion of the footprint assignment and ‘Netlisf verification, Layout generates an ‘ AutoECO’
report describing the errors, if any. Accept the report if it i& error free. Now you can see the package of
various components placed on a layouting canvas (Separate Window along with the drill chart). The in¬
terconnections among various components are represented in the form of lines as shown in Fig. 8.61.
8.7.2.1 Board Outline Creation To start with the physical layout process, the first step is assign¬
ing the physical board size (PCB size in length x width) to the layout area where the components are
Embedded Hardware Design and Development
Footprint for B
eiKCOI-J .:OQA/H/TMi;.Q/Wm
BLKCOfil 00A/H/TM13 Q/W.100;
BIXCON.100/VH/TM20EM200/10
,BLKCON.100WH/TM20EM20(V20
BLKCON.10DA'H/TM2OE/V/.2C0/26
BLKCON.100WH^TM20E/W.200/40
BLKCXJN.15G/VH/TM1SQS/W.31226
DlNC/MIN SM/S/A
DIP.100/24M300/L1.250
DSUB/RP.318/TM/15
JUMPER200
OSC8\4P
PLCC44
going to be placed. For this, the datum (reference point) needs to be fixed first. Move the datum to a
convenient location using the command ‘Move Datum’ from ‘ Tool menu (Tool->Dimension->Move
Datum). Place the datum at the desired position by clicking on the desired point on the layout window
after executing the ‘Move Datum’ command. Press ‘ESC’ key to exit the command. Move the drill chart
to a convenient location by executing the command ‘Move Drill Chart’ from the 'Tool menu (Tool-^
Drill Charts Move Drill Chart). After selecting this command, click on the desired portion where you
want to place the drill chart. The drill chart will be moved to that location. Press ‘ESC’ to exit the drill
chart movement operation.
Datum acts as the reference point (0,0) for the design. All measurements are taken with respect to the
datum. The co-ordinate left to the datum is taken as -ve and right to datum is taken as +ve. Also co-ordi¬
nate on top of datum is +ve and bottom one is -ve. The co-ordinates are displayed as per the co-ordinate
system selected during the ‘Netlisf file creation. If the selected system is in inches, the co-ordinate posi¬
tions are shown in inches. If it is in metric, the co-ordinate system is represented in millimetres in the
layout tool (Fig. 8.62).
After fixing the datum, provide an outline to the layout by using the obstacle tool from the tool win¬
dow. Select the ‘Obstacle’ tool from the Tool window (Tool->Obstacle-> Select Tool) and click at the
datum to start the layout boundary. Move the cursor up till the y co-ordinate reaches the desired width
of your layout (PCB width) and then mpve the cursor towards the right till the x co-ordinate reaches the
desired length of your layout (PCB length). Now move the cursor down from this position. Once the
cursor reaches a point which is in line with the datum, a rectangle with width and length set for your
layout is formed. Press ‘ESC’ key to stop the execution of "Obstacle’ creation command. The boundary
for your layout (Blueprint of original PCB) is ready and you need to move all the components inside
this boundary and arrange them within that boundary and interconnect the various components as per
the connections coming from various components. For this, various ‘layout tools' are required. Before
Introduction to Embedded Systems
Layouting tools
Component footprints
:§|^gnfj^55ppnen|T<>3l fDRCpN^
proceeding to the next steps let’s have a quick glance at the important tools used from the tool window
for this. Figure 8.63 illustrates the different layout tools.
Component Tool: Selects a component. Mainly used for positioning the selected component within the
selected boundary of the layout.
Zooming Tools: Zooms in and out the selected portion of the Layout.
Obstacle Tool: Allows the drawing and editing of obstacles such as board outline, outline of compo¬
nents (parts) etc.
Text Tool: Inserts texts in the selected area. It is used for placing and editing Part Number, value, notes
etc on the layout area. After selecting this tool right click on the layout window and input the required
parameters.
Online BRC: Error checking option. If the online DRC option-is selected (ON) during the component
layout and inter-connection, any errors occurred (Package to package distance error, track to track dis¬
tance less than the set value, connection error, etc.) at the layout operation is indicated in real time.
Embedded Hardware Design and Development
Co-ordinate Representation
Components
41 .qj rz M
e^i I m
CD Hi- JM<H<07 O.H.P u.SH^O iSou*‘#S3‘
is ‘Board Outline’ and ‘Obstacle Layer’ is ‘Global Layer’. If not, set them to these values from the ap¬
propriate combo box. Use ‘ESC’ key to exit the properties selection command. Now set the number of
layers required for your PCB. For a through hole component based board we require at least two layers
namely; ‘TOP’ and EOF (bottom) layers. The component is placed usually at 'TOP’ layer and inter
connections (routes) are created at the ‘BOTTOMT layer. For Surface Mount Device (SMD) components
based boards, the minimum number of layers required is one, where both component placement and
interconnection are done on the same side (layer). Depending on the complexity of interconnections and
the number of components, you can use multiple layers for routing the connections. The additional lay¬
ers come in between the ‘TOP’ and ‘BOTTOM’ layers and they are referred as inner layers. Inner layers
are usually represented as ‘/AT, lIN2’ etc. The number of extra layers (Inner layers) used in the layout
(PCB) solely depends on the complexity and number of inter-connections.
8.7.2.2 Layer Selection For setting the number of layers for the board, select ‘layer tool’ from
the Tool menu (Tool->Layer->Select From Spreadsheet...). The following spreadsheet as shown in
Fig. 8.64 pops up on the design window.
The spreadsheet contains various information like ‘Layer Name’, hotkey to activate the layer (‘Layer
Hotkey’), ‘Layer NickName’, ‘Layer Type’ (whether the layer is used for routing or not), and Mirror
Layer (mirror the connections and components).
Select the different layers you want to include in your PCB. You can see different layers namely
‘TOP’, ‘BOT, ‘GND’, ‘PWR’, ‘INI’, ‘IN2’, etc. As explained earlier, ‘IN’ stands for inner layers. Our
illustrative design is a very simple board for power supply part and it does not contain any inner layers
and separate Ground and Power layers. Disable whatever layers we are not using in the design by double
clicking on the corresponding cell of ‘Layer type’ and set it to ‘Unused’. For a double sided through hole
Embedded Hardware Design and Development
v PCB with components placed at TOP layer and routing at Bottom layer, the minimum layers required
are Top Layer (‘TOP’), Bottom Layer (‘BOT’), Solder Mask Bottom (‘SMBOT’), Silk Screen Top
(SSTOP) for legend printing, Assembly Top (‘ASYTOP’) for giving component assembly notes, Drill
drawing (‘DRLDWG’) and ‘DRILL’. Keep the ‘Layer Type’ of all other layers to ‘Unused’ and close
the spreadsheet. The layers Solder mask, Silk screen, etc. are explained in a separate section of PCB
fabrication. Please refer it for more details.
8.7.2.3 Component Placement Component placement is the process of moving components into
the layout area within the board outline and arranging them properly within the area. Components can
be placed either automatically or manually. If the automatic placement option is selected, ‘Layout' au¬
tomatically arranges the components within the area covered by the board outline. Select the Automatic
component placement option from menu (Auto^PlaceABoard) to enable automatic placement. Auto
place option is normally used with boards with lesser number of components and interconnections. For
boards with large number of components and interconnections, the auto place option need not be an
optimised one, especially when the board size requirement, component placement, routing, etc. are criti¬
cal. Manual placing is the advised method in such situations. Manual placement involves moving of the
component manually to the desired location on the layout area and placing it appropriately. To activate
manual placement, select the ‘Component’ icon from the tool window or ‘Select Tool’ from the ‘ Tool
menu (Tool->Component->'Select Tool). Click on the component and move it to the desired position
within the area defined by the board outline. Move all components like this. Place the components in
away facilitating their easy interconnection. For easy interconnection, components should be oriented
Introduction to Embedded Systems
properly. Rotate component option helps in orienting a component properly. Select the component,
right click it and select ‘Rotate’ option to rotate it. While placing components, give special care to place
components which are advised to be kept closer by the design guide (e.g. Placing decoupling capacitors
very close to the power supply pin of IC), placing connectors at the edge of the board, etc. The vari¬
ous points to be taken care of in component placement are explained in a later section. The command
‘ Unplace Board’ (AutoUnplaced Board) removes all components which are placed within the area
bounded by the board outline.
The package of each component incorporates texts for the reference name of the component—
‘Reference Designator’ (Component names, Cl, C2, etc. for capacitors, Rl, R2, etc. for Resistors,
etc.) on the Assembly layer (ASYTOP) and on the Silk Screen Layer (SSTOP), ‘Component Value’
(e.g. 220uF/25V - Value of capacitor Cl in the example) on the Assembly layer (ASYTOP), the 'Foot¬
print Name' (e.g. BLKCON. 100/VH/TM1 SQ/W.l for connector Jl) on the Assembly layer (ASYTOP)
and the First pin indicator of the component (e.g. 1) on the Assembly layer (ASYTOP) and on the Silk
Screen Layer (SSTOP). Reference Designator and First pin indicator are very essential for assembling
the component on the PCB. Component Value is optional since it is available from the Bill of Material
(BOM) file. ‘Footprint Name’ is not required. Select ‘Text Tool’ from Tool window and click on the
corresponding text string and move/edit/delete or rotate the text string as per the requirement. Unwanted
text is deleted and other text strings are placed close to the corresponding footprint. Use rotate option
for orienting the text strings properly. The yellow lines represent the interconnection among various
footprint pins. The interconnection is performed in the next step. Normally the pad corresponding to pin
number 1 of a footprint is square in appearance. The component placement for 1power supply.mnT net
file considered for illustration is shown in Fig. 8.65.
8.7.2.4 PCB Track Routing Routing deals with interconnecting the various components as per the
capture schematic diagram. The routing information is passed to the layout tool from schematic capture
through the ‘Netlisf file. Routing is performed after component placement. First the components are
arranged in the desired manner. The interconnection among various components is shown in the layout
as ‘Yellow lines’. The interconnection lines of a net are referred as ‘rats’ in PCB terminology. ‘Route/
track’ represents the actual copper path present on the PCB. The track properties of the physical board
are determined by the ‘Route’ in the layout.
Similar to component placement, routing can also be performed either automatically or manually. If
automatic routing command is executed, the sweeps are executed automatically and the complete board
is routed automatically. Execute the ‘Autoroute’ command (Auto->Autoroutc->Board) from the menu
/for routing the board automatically. Auto routing is suitable for boards with lesser number of routes
(Interconnections). For complex routing, auto routing is comparatively inefficient and requires multiple
layers, more layout area and route connecting holes across different layer (PTH). Manual routing is
the best choice for routing complex boards. Though it takes lots of time (of the order weeks) to route a
complex board, manual routing provides highly optimised layout with minimal number of layers and
plated through holes. Complex boards can be autorouted if the time required to complete the board is
critical and the number of layers involved as well as number of plated through holes (PTH) in the board
is not a constraint. For a double sided, ‘Through hole’ board, normally the components are placed at
th? ‘TOP’ layer and track routing is done at the bottom (‘BOT’) layer. If the board is a multilayer board
and the number of interconnection are very high, layout contains multiple layers (Inner layers; IN 1, IN2
etc). In multilayer boards, apart from inner layers, there may be separate layers for ground ‘GND’ and
power ‘PWR’. These layers are usually spread with copper. For multilayer boards the connections are
routed through different layers and the continuation of a connection from one layer to the other layer is
achieved through a special mechanism called ‘via’. Via is a drill hole connecting a route from one layer
to another route present in a different layer. Vias are copper plated or riveted to maintain electrical con¬
nectivity between the two connecting routes.
While working with multilayered layouts, as routing progresses, the layout area becomes congested
with the different tracks routed on various layers. To avoid this you can selectively turn ON and OFF
different layers. Say, for example, if you are placing tracks on the second inner layer (IN2) and you feel
the routes which are already placed on the first inner layer (INI) is creating difficulties in placing the
routes bn the second inner layer, you can turn off the first inner layer. Any layer can be turned off while
routing by selecting the corresponding layer from the layer selection combo box and pressing the minus
keyt on the keyboard. This removes the selected layer from view. To activate the layer, press ‘-’ key
again (Toggling with ‘-’ key). Layers like Assembly Layers (AST, ASB), Silk Screen Layers (SST, SSB)
can always be switched OFF till the layout process is complete. Other layers are switched ON and OFF
conveniently according to the routing process. Layer selection enabling and disabling is depicted in
Fig. 8.66.
By default the different layers of the layout are colour coded with different colours to distinguish
them. The default colours! for different layers are given below:
Global layer = Yellow
Top = Green
Bottom (BOT) = Red
Inner Layer 1 (INI) = Aqua
Manual routing of Board Now all pre-requisite for routing is through. To start with the routing
process, select the layer on which the routing operation is carrying out, from the layers selection combo
box. Follow the steps given below:
Embedded Hardware Design and Development
1. Select the ‘Edit Segment Mode’ tool from the tools window (or execute the command
Tool-^Track-> Select Tool from the Tool menu)
2. From the View menu, choose Zoom In, click on the screen to magnify the routing area you are
working on, and then press ESC to end the zoom command
3. Select a ratsnest (A point from which the yellow line indicating an interconnection starts). The
cursor changes to a small-size cross! and the ratsnest is attached to the pointer
4. Drag the pointer to draw a track on your layout area. Right clicking the left mouse button, anchors
the track at the nearest pad on the net
5. Continue to move the cursor to draw additional segments Of the track, clicking the left mouse
button to create vertices (comers) in the track as you route. You can delete a routed segment by
placing the cursor over the segment and pressing DELETE key
6. Click the left mouse button to complete the route. The cursor changes to a regular-size cross and
the ratsnest disappears from the cursor
7. Select the completed route and choose Lock from the pop-up menu. This locks the route you cre¬
ated so that it cannot be moved if the board is later autorouted
8. Repeat the process for all rats of the layout. On completing a track drawing, the rat representing it
is replaced with the route
9. For adding a via (through hole) to connect the route (track) to a track placed on other layers, right
click the mouse on the desired location, while you are dragging the mouse to draw a track. Select
the 'Add Via’ option
10. To draw the tracks on a different layer select the layer from the layer selection combo and pro¬
ceed
11. To change the width of the track, right click on the track and select the 1Change Width’ option.
Type the desired track width and execute. The normal value for track width is 12 mil. The mini¬
mum width of track depends on the value supported by PCB fabricators and the comments from
the component manufacturer like the track width of the signal lines should be this much, etc.
Nowadays fabrication of PCBs with track width 6 mil is possible. Ensure that proper track width
is provided to tracks representing power supply lines and ground. There is a standard guideline for
track width selection for power supply lines. Follow these guidelines strictly
12. If possible reduce the track length from a component to ground and always connect the ground
tracks coming from different components in. a ‘star’ model. This eliminates ground elevation prob¬
lems in PCB
8.7.2.5 Placement & Routing Statistics Placement and routing statistics ensures that all com¬
ponents are placed in the layout and all nets are routed perfectly. It is mandatory to verify the same to
ensure that nothing is missed from the components and routes section. For verifying the placing and
routing statistics follow the steps given below.
1. Select the 'View Spreadsheet' toolbar button and choose ‘Statistics'. The Statistics spreadsheet
window is displayed
2. Scroll until you find the ‘Placed' and ‘Routed' rows, showing the placing and routing statistics.
The statistics should be 100% for both. A statistics value less than 100 for ‘Placed’ indicates some
components are missed from placing. Similarly if the statistics for ‘Routed’ is less than 100, the
routing is incomplete, meaning some tracks are yet to be routed
3. Close the spreadsheet when you have finished viewing the statistics
and labels. To create/edit/delete a text string, select the ‘Text TypeiotText - • A- jj!y-3Sr*f
>§.f f-y"A ;>vv ;• ’
Tool’ from the Tools Menu (Tool->Text->Select Tool from » String .Power Supply Pari
Tool menu). Right click on the layout window for insert¬
. ■ -»■ '- , f Custom Properties
ing a new text string and select the 'new' option (or Press < Reference fiesignator r Package Name
/Component Value • Footprint Nurne .
‘Insert' key of keyboard). The following text properties
window appears on the screen depicted in Fig. 8.67. Text location |V|
Moving a Text String For moving a text string in the layout area, follow the steps given below.
1. Select the ‘Text Tool' from the Tools menu
2. Click on the text, which is to be moved, with the left mouse button. Text gets attached to the
cursor
3. Move the mouse to position the text in the new location
4. Click the left mouse button to place the text
Editing a Text String Execute the following steps to edit an existing text in layout
1. Select the ‘ Text Tool' from the the Tools menu
2. Double click on the text which is to be edited, with the left mouse button. The ‘Text Edit’ window
pops-up
3. Perform the required changes on the Text Edit Dialog box and press OK
4. Press ‘ESC’ key to exit the text edit mode
Deleting a Text String Follow the steps given below to delete a text string.
1. Select the ‘ Text Tool' from Tools menu
2. Select the text by clicking on it with the left mouse button
3. Press the DELETE key
Embedded Hardware Design and Development
8.7.2.7 Assembly Notes Assembly notes are useful information for those who are assembling com¬
ponents on the PCB. To provide useful information, layout should contain component information like
name of the component (Cl, Rl, Dl, LI, Ul, etc.), pin number of component, polarity of the compo¬
nent, if any, point indicating the placement of an IC, etc. Some information (Like component name,
pin number, etc.) embeds with the lNetlisf file by default and they appear on the layout at the time of
placing the component. The assembly note information is kept on the Assembly layer (e.g. Assembly
Top Layer (AST)). If the same information is present on assembly and silk screen (SST or SSB) layers,
you can remove it from one of the layers. Use the ‘Text Tool’ from Tools menu (or Tool-> Text-> Select
Tool from Tools menu) to add, delete, modify or rotate Assembly note Texts.
Apart from the text information, graphic information can also be added to the Assembly layer.
Symbols of diode, capacitor, etc. showing the polarity are typical examples. Use the obstacle tool to
create symbols on the assembly layer. For performing any operation with the assembly layer, select the
assembly layer (e.g. AST) from the layer selection combo box. Now select the obstacle tool and draw
whatever symbol you want (e.g. A diode symbol is created as the combination of a triangle obstacle and
three line obstacles). You can group the obstacles together by right clicking on the layout area where
the obstacles are present and moving the mouse icon in a rectangle area which contains all the obstacles
that needs to be moved together. After all obstacles of the group are selected, hold down the right button
of mouse and move the group of obstacles to the area in layout where you want to place them. Set the
‘Obstacle Type’ of the obstacle/obstacle group to ‘Free track’ by editing the obstacle/obstacle group
properties. An obstacle/obstacle group can be linked to a component by editing the properties of the
obstacle/obstacle group (obstacles selected together). Enable Obstacle tool, right click on the obstacle/
obstacle group and select the option ‘Comp Attachment...’ from the ‘Edit Obstacle’ pop-up window.
Give the Reference name of the component to which the obstacle is to be linked (say C1, U1, etc.) on the
‘Reference designator’ edit box. Linking the assembly not obstacles to the component moves it along
with the component whenever the component is re-arranged in the layout window.
8.7.2.8 Drill Details To set the pad stack and drill dimensions of the vias used in the layout, execute
the steps given below.
1. Select the pin tool from the ‘ Tool’ menu (Tool->Pin->Select Tool)
2. Click on the via with left mouse button
• 3. Select ‘ View Spreadsheet’ from Tools menu
4. Set the pad stack size and drill diameter
8.7.2.9 PCB Mounting Hole Creation Mounting holes are required for mounting the PCB on
the enclosure. PCB is fixed to the enclosure using screws and washers. PCB should contain mounting
holes and the size of the mounting hole is determined by the mounting screws. The size of the screw is
expressed as M2, M3, etc. The ‘Capture CIS’ schematic tool does not contain a part for mounting holes
and we cannot add mounting hole information to schematic diagram. Mounting holes are directly added
to the layout. Follow the steps given below to add a mounting hole to the PCB layout.
1. Select the ‘Component Tool’ button from the toolbar
2. Right click on the layout window and select New (or Tool->Component->New... from the ‘Tool’
menu). The ‘Add Component’ dialog box appears on the screen
3. Select the ‘Footprint’ button. The ‘Select Footprint dialog’ box is displayed
4. In the Libraries group box, select the library LAYOUT. Use the Add button, if necessary, to add
this library to the list of available libraries
5. In the footprints group box, select a mounting hole (OrCAD provides three types of mounting
holes: MTHOLE1, MTHOLE2, and MTHOLE3)
Introduction to Embedded Systems
2. Execute ‘Post Process Settings' command from ‘Options' menu (Options->Post Process Set¬
tings.. .). The ‘Post Process' window appears
3. In the ‘Post Process’ window ensure that the ‘Batch Enabled’ option is set ‘ Yes' only to the layers
which are selected during the layout design process
4. Ensure that the ‘Device’ option for all entry is ‘EXTENTED GERBER'
5. Right click on the ‘Post Process' window and select ‘Run Batch' (or execute Run Post Proces¬
sor' from ‘Auto' menu after closing the ‘Post Processor' window)
6. The gerber file is generated automatically and the confirmation and path of the created file is
provided in the form of message boxes
Corresponding to each layer of the ‘Layout', a file is created with the layer name as extension (e.g.
1 power supply.top' for TOP layer). This file contains all information related to that layer. The ‘.tap' file
gives the drill detail information for CNC drilling. All these files (files corresponding to each layer and
‘.tap’ file) are sent to the PCB manufacturer for fabricating the PCB.
8.7.2.13 Finished Layout An example of a finished layout is given in Fig. 8.68. This layout is
created for our ‘power supply.mnl' netlist example.
The layout contains component footprints, tracks, mounting holes, top and bottom layer, silkscreen
top layer and assembly top layer. The tracks are routed at the bottom layer and they are shown in red
colour. The white coloured text and diode symbol is assembly note and they are drawn on the assembly
top layer. Green coloured texts are labels and they are drawn on the silkscreen top layer. Save the layout
file. The file extension for layout file is ‘.max'. The ‘.max' file can be opened with the ‘Layout Demo'
Tool at any time for viewing the routing/placing and modifying.
Introduction to Embedded Systems
pCB fabrication tool understandable format. The Gerber file created from the layout along with drill
details serve the purpose of transferring this information from layout to PCB fabrication tool. Gerber
is a universally accepted standard for transferring relative co-ordinates and track information and com¬
ponent details of a layout for PCB fabrication. Gerber files are a collection of art works. In a multilayer
pCB, each layer has its own art file representing the component co-ordinates, component info and track
info for that layer. The drill details file provides the drilling information (drill hole diameter, plating,
etc.) of different vias (PTH) present in the layout. The actual drilling is performed on the PCB using
Computer Numeric Control (CNC) drills or laser and the drilling is controlled in accordance with the
drill details file.
8.8.1.3 Multilayer PCB If the complexity of connections in the circuit to be fabricated is high,
routing of connections is performed using multiple layers. Multiple layered PCBs, contain additional
routing layers called ‘inner layers’ apart from the top and bottom layers. The number of layers required
and the connections on each layer is fixed at the routing time itself and each layer in the layout repre¬
sents a physical layer in the PCB. The number of inner layers may vary from 1 to 16 depending on the
circuit complexity. It is a common practice to allocate separate layers for ‘Ground’ and ‘Power Supply’
in multilayer PCBs. The separate ground layer and power layer reduces the effect of any accidental an¬
tenna loop formation in the circuit: All layers of the PCB are fabricated separately and finally they are
stacked and glued together. Alignment marks are provided on each layer for aligning the layers properly
for stacking them. The final PCB looks like a single PCB, though it contains PCB layers inside. The re¬
quired interconnections between the tracks of different internal layers are established through drill holes
called ‘via’. The ‘via’ may be a copper electroplated drill hole or a riveted ‘Plated Through Hole’.
Introduction to Embedded Systems
8.8.2.1 Photo Engraving (PCB Etching) Photo engraving is the oldest technology used in PCB
fabrication. It is the most popular technology and even today also it plays a vital role in PCB fabrication.
Photo engraving is based on the photographic technique (Technique similar to the one used in develop¬
ing photographs from a negative film) and it makes use of photo masks and chemical etching. To adopt
photo engraving technique, the different layers of the layout should be converted into a film very similar
to the negatives of photograph. The gerber photo plotter files are inputted to a photo plotting machine
and it produces the photo-plot (PCB film) of each layer. The process of creation of PCB film from art
files is termed as ‘Photo plotting’. The tracks and copper using areas in the layer are opaque (black) and
other portions are transparent in the films. These PCB films are used as ‘Photo mask’ for photo engrav¬
ing.
Copper coated on a substrate sheet is used as the metal for photo engraving. A photo sensitive mate¬
rial called ‘photo resist’, which is resistant to acids and other etching materials is applied to the copper
sheet. The ‘photo mask (PCB film for the corresponding layer)’ is kept on top of the photo resist mate¬
rial coated copper sheet and it is aligned properly. This sheet is then exposed to strong ultraviolet rays.
The places on the target copper clad board where the PCB film is transparent gets hardened and the
opaque regions remain as such. The photo resist is then developed by washing in a solvent that removes
the unhardened parts. Finally, the copper area to be engraved (removed) is exposed to an acid or other
etching compound which dissolves the exposed parts of the copper sheet. Ferric chloride or ammonium
per sulphate chemical bath is used for removing the unwanted copper from the substrate. Each layer of
the PCB is fabricated like this and then the required vias are drilled on each layer. Finally all layers are
stacked together exactly as present in the layout design to form the finished PCB. Vias used for inter¬
connecting various layers are copper plated or a conductive rivet is inserted into them to establish circuit
connections.
8.8.2.2 PCB Milling PCB milling is a straightforward and more precise technique for fabricating
PCBs. It uses 2 or 3 axial milling machine to mill away the unwanted copper from a copper cladded
substrate which is used for each layer. The milling is done on the basis of gerber file for each layer. Here
no PCB films are created and it is a chemical free process. PCB milling machine operates in a way simi¬
lar to a photo plotter and receives the commands from the host software that controls the position of the
milling head in the x andy axes (for 2 axial milling) and x, y and z axes (for 3 axial milling). Compared
to photo engraving, the accuracy of PCB milling is very high. PCB milling accuracy is dependent only
Embedded Hardware Design and Development
on milling system accuracy and the milling bits. With milling technique it is very easy to create sharp
track edges, pads, etc. on the PCB. Both milling process and engraving process are ‘subtractive’ process
since copper is removed from the substrate board to create electrical isolation.
8.8.2.3 PCB Printing PCB printing is the latest technology adopted in PCB fabrication. As the
name indicates PCB printing is similar to the ordinary printing. The only difference is that instead of ink
and toner, conductive ink is used and papers are replaced by substrates. Also special types of printers
are used for the same.
8.8.3 How a finished PCB looks like and how it is made operational
As mentioned earlier, all the layers of the ‘Layout file’ are transformed into corresponding physical
layer using any of the above-mentioned fabrication techniques and after fabrication of all layers, they
are stacked and glued together in the same order as they present in the ‘Layout file’. The conductive drill
holes are plated with copper, or riveted using appropriate rivets. Now the top layer and bottom layer are
the only visible layers and they contain the top layer and bottom layer tracks (if any) and component
placement copper spread pads (for Surface Mount Devices) and through hole pads (for through hole
components). If you examine this PCB you can see the tracks and component layouts but you may not
be able to identify which component layout is corresponding to a component given in the ‘schematic
diagram’, since there is no visual indication marks provided on the PCB at this stage, describing the
component name, its value (if needed) and the component polarity if any (for polar components like
capacitors). Next step is the printing of component ‘Part reference Number’ details on the top/bottom
layer where component layout is done.
8.8.3.1 Solder Mask One more step is involved in the PCB fabrication process before printing
the part deference text and layout outlines. In the etched/milled or printed PCB the copper tracks are
exposed and there is a chance for corrosion and abrasion. To avoid this, a plastic layer is deposited on
top of the PCB. This plastic layer is known as ‘Solder Mask’. Solder mask protects all the copper track
and spreading from exposure to the surrounding atmosphere. Solder mask also resists the “bead-up” of
solder (wetting by solder; where solder is a conductive lead material for fixing components to the pad
and track) and prevents the solder used for fixing components from flowing in the PCB. Solder mask
is exposed in points which are intended for soldering components. The commonly used solder mask is
green coloured and that’s why PCBs are green coloured in appearance. Solder mask is applied to top and
bottom layers of PCB depending on the presence of copper components. Solder masks are also available
in other colours like red, black, blue, etc.
8.8.3.2 Silk Screen The process of printing the part reference information for various components
on top of the solder mask layer is called ‘Silk Screen printing or Legend printing’ and the printings are
referred to ‘Silk Screen legend’. This legend provides readable information on component part numbers
and placement of components like where the first pin should come, or the polarity information like +
and - of components and also the layout boundaries of the components. This information is necessary
for placing the components while assembling the components on the PCB and also for repairing. The
printing is performed using ‘Silk screens’. It can also be done using Inkjet printers. Silk screen printing
is done on top and/or bottom layer of the PCB depending on the placement of components on these lay¬
ers. The silk screen layout file corresponding to the top layer is ‘Silk Screen Top (SST)’ and for bottom
layer it is ‘Silk Screen Bottom (SSB)’.
Introduction to Embedded Systems
8.8.3.3 Testing PCB The PCB manufacturer itself conducts a preliminary test, after fabricating the
PCB to ensure that all the nets (connections) in the gerber file supplied by the end user got interconnect¬
ed according to the gerber files: This test is known as “Bare Board Test (BBT)”. BBT is conducted at the
manufacturing setup and the PCB is delivered to the end user only after ensuring the BBT is correct.
Checking the internal connections within a multilayered board is very difficult. However the end user
can conduct tests like “Visual Inspection”, on receiving the PCB. “Visual Inspection” is performed to
ensure there are no ‘shorts’ within the visible part of the PCB. A magnifying glass is used for examining
the board. It is advised to perform a Visual Inspection test always, before soldering the components.
Nowadays tests like JTAG Boundary scanning is available for testing the interconnection among vari¬
ous ICs present in the board using the "boundary scan cells of the ICs and JTAG interface.
8.8.3.4 Component Assembly Component assembly is the process of soldering the circuit com¬
ponents like capacitors, resistors, ICs, connectors, etc. on the PCB. The components are connected to
the PCB through soldering. The soldering technique may be either manual or automated. In manual
soldering process, components are soldered on the board by soldering experts using soldering iron and
soldering lead. The Bill of Materials generated for the schematic diagram is used as the reference part
number and value selector for the components and the legend printed on the PCB helps in finding out
which component is to be placed on which part of the PCB. In automated soldering process, machines
are used for executing the soldering operation. Here also human intervention is required. Various solder¬
ing techniques like re-flow soldering, etc. are used. Techniques like X-ray are used for inspecting the
correctness of machine soldering in lead free component assemblies.
It is not recommended to assemble all components in a single stretch for the first time in a proto mod¬
el development. Split the circuit into different modules like power supply module, processor module,
etc. and assemble the components for one module at a time preferably starting with the power supply
part of the board and ensure that each module is working properly. Examine the expected output of each
module and ensure you are getting the correct output. Move on to the next modules only after getting the
correct output from the last assembled module. If you are not getting the expected output, ensure that all
components necessary for the functioning of that module is assembled properly, examine the input and
output signals of the modules using hardware debugging tools like Multimeter, CRO, Logic Analyser,
etc. Make necessary changes in the PCB circuits if required. Most of the times adding of additional
components, cutting of tracks, etc, may be required in the first board since we are using it for the first
time and we are not familiar with the component and system behaviour in it. Hence this board will be an
experimenting board-More precisely “Prototype Board” in embedded technical term. The drawbacks of
the circuits in the proto board and the circuit and component modifications, required if any, are included
in tfle next versions of the proto board and it goes into commercial production once it functions in the
expected way. It is strongly advised to fabricate lesser number of PCBs in the first run.
Run small test applications on the processor part to ensure that the processor part is working properly.
For example, if the board contains some LEDs or buzzers connected to the output ports of the processor,
you can write small firmware program to manipulate their status (like blinking LED, turning buzzer ON
and OFF, etc.). This helps in ensuring the processor part is functioning properly. You may find it very
difficult to do a modulewise assembling and testing, but remember it is a one-time process and once
you identify the problems with a board you can apply the remedial measures to all boards and go for a
mass assembly. The proverb “A stitch in time saves nine” is, really meaningful in the case of embedded
hardware design. If you spend some effort for this it will definitely help you in saving the additional
time, effort and money in debugging.
Embedded Hardware Design and Development
If you assemble all the components together in a single stretch for the first time and power it ON,
ie [
you can see your product functioning properly only if you are a very lucky person Otherwise it may
ie end up in a board blast or damage. Moreover you won’t learn anything on the hardware and the mystery
behind how it functions, if it starts functioning in the first run itself. Debugging the hardware only gives
er you sufficient knowledge on it (from a developer perspective). From a product development company
to i perspective, the product should function fully in the first run itself. The design is always well checked,
simulated and debugged before fabrication to avoid all possible problems. Still there may be chances for
‘g |
S. I
problems, since hardware functioning is unpredictable.
i- |
Solder Mask
Component Soldered on Footprint
(Green Coloured) Silk Screen Legend
n- |
to I
al |
id i
irt |
'tit 1
es 1
;r- |
he f
id- |
le, |
)ly |
eh |
he |
all 1
nd %
PCB Track
er, !
aal
rst
[Fig. 8.69] Finished PCB with components Assembled (Top view)
an
Courtesy of Keil (www.keil.com)
. of
ted
the
8.8.3.5 Conformal Coating The embedded product need not be deployed in a controlled envi- „
ronment always. Hence the PCB along with the components assembled on it should withstand all the
rly.
extreme operating conditions. The environment where the PCB is going to deploy may be a dusty one,
ior, highly humid one, etc. Presence of metal dust particles and conducting corroding particles may create
DN electrical short-circuiting or corroding of the PCB. A special coating called ‘ Conformal coating’ is ap¬
ery plied over the PCB to prevent this. Conformal coating should only be applied after assembling all the
nee components. Dilute solution of silicon rubber or epoxy is used as the conformal coating material. The
>ra assembled PCB is either dipped in the conformal solution or the conformal solution is sprayed on the
led assembled PCB. Another technique used for conformal coating is sputtering of plastic on to the PCB
inal in a vacuum chamber. The major disadvantage of using a conformal coating is, servicing of the board
and replacement of the components, in case of any damage, is extremely difficult, since the coating is
a permanent one.
4
Introduction to Embedded Systems
(~W Summary
y The hardware of embedded system contains analog electronic components like resistors, capacitors, inductors,
OpAmps, diodes, transistors, MOSFETs, etc.. Digital electronic components like logic gates, buffers, encoders
and decoders, multiplexers and de-multiplexers, latches, etc. and digital sequential and combinational circuits,
Analog, digital and mixed Integrated Circuits (ICs) and Printed Circuit Board
y Open collector is a special transistor configuration for interfacing the output signals from Integrated Circuits with
other systems which operate at the same or different voltage level as that of the IC .,
y A digital circuit used in embedded hardware design can be a Combinational or a Sequential mcmi. The output of
a combinational circuit is dependent only on the present input, whereas the output of a sequential circuit depends
on both present and past inputs -r'--
y An Integrated Circuit (IC) is a miniaturised form of an electronics circuit built on the surface of a thin silicon wa¬
fer. VLSI design deals with the design of ICs. VHDL is a hardware description language used in VLSI design >•
✓ Electronic Design Automation (EDA) tool is a set of Computer Aided Design / Manufacturing (CAD/CAM)
software packages which help in designing and manufacturing the electronic hardware like integrated circuits,'
printed circuit board, etc. , r; A ,, y'/I ( L_. ' . , ‘
y OrCAD is a popular EDA tool for P.GB design from Cadence Design Systems ('www.cadence.com). CaptureCIS
is the OrCAD too! for circuit creation (schematic capturing), Layout is the PCB layout tool and pSPICE is the
circuit simulation tool . - , . k L : .
y The schematic capturing tool.converts the component details and the interconnection among them in an elec¬
tronic circuit to a soft form representation called netlist '■ T’;?(J
y Packages are standard entities for describing a component in'RGB design. PDIP, ZIP, SO 1C, TSSOP, VQFP,
PQFP arc examples for standard packages. "... V .. ‘ / y
y Netlist file contains information about fine packages for different components of the circuit diagram, and the
interconnection among the different components represented in the form of .‘NET’s
y The multiple planes.used for interconnecting different Components in a PCB is known as ‘Layers’ and the inter¬
connection among various components is referred as ‘Routes’ in PCB terminology
y The layout tool generates a blueprint of the actual PCB in terms of the different component placement, the in¬
terconnection among them. The layout information is passed to the PCB fabricator in a universally acceptable
standard called gerber file for fabricating the PCB. Gerber file is a collection of art files corresponding to the
different layers of the PCB and drill details for the vias and holes present on the PCB
y PCB is a platform for placing electronic and electromechanical components and interconnecting them without
discrete wires. PCB contains printed conductive wires (called PCB tracks) and component layouts attached to
an insulator sheet. The insulator sheet is referred as substrate and it is usually made up of ‘Glass Epoxy—A fibre
glass reinforced epoxy material’ or ‘Pertinax—A phenol formaldehyde resin’. The normal thickness of this sub¬
strate sheet is 1.6 mm
y PCB etching (Photo engraving), PCB milling and PCB printing are the commonly used PCB fabrication tech¬
niques to develop a PCB from the gerber file information
y PCBs can be classified into Single Sided PCB, Double Sided PCB, Multilayer PCB, etc., based on the number of
layers and component placement
Keywords
\____y
Open Collector An NPN transistor configuration with emitter connected to ground and collector
left unconnected. It is an I/O interface standard in electronic circuits
Embedded Hardware Design and Development
■ Logic Gate Analog/Digital Circuit which produces the output as the output corresponding to
||f the logical operation performed on the input signals
§ Buffer Logic circuit for increasing the driving capability of a logic circuit in digital
!<% electronic circuits
j Latch Logic circuit for latching data in digital circuits
| Decoder Logic circuit for generating all the possible combinations of the input signals in
digital circuit -
f Encoder Logic circuit for encoding the input signals to a particular output format
\ Multiplexer ; Logic circuit which acts as a switch which connects ‘one input line from a set of'
input lines, to an output line at a given point of time , -.' _ •«
| De-Multiplexer Logic circuit which performs the reverse operation of multiplexer
Combinational Circuit A digital circuit which is a combination of logic"gate’s. The Output of the com¬
binational circuit at a given, point of time is dependent only on the state of the
“ Linputsat the given point-of time -V - '■
’’Sequential Circuit , . f. A digital logic circuit whose output a: any given point of time depends on both
the present and past, inputs , j, .. , " . •
; Integrated Circuit (IC): y : A miniaturised implementation of an electronics circuit containing transistors
and other passive electronic components in a silicon wafer
: VLSI Design ' : IC design
; VHDL '’’Vv* .v : «A-Hardware Description Language for VLSI design' * •. . " :
/Electronic Design Automation A set of Computer Aided Design /.-Manufacturing (CAD/CAM) software pack¬
(EDA) tool ages which helps, in designing and manufacturing the electronic hardxvare (
JftOrCAD :: ;. /;• •’ : A popular EDA tool for PCB design from Cadence Design Systems
| Package : An entity representing the physical characteristics (length, width, number of
pins, type of pins (surface mount or through hole), pin size, etc.) of a harclware
component (like iC, capacitor, etc.) irt electronic circuit
Netlist file : A soft form representation of the package of components and the interconnec¬
tion among the components in an electronic circuit
Footprint : A pictorial representation of a component when it is looked from the top (top
view of a component)
Routes : The interconnection amcmg the pins/leads of various components in a PCB
Layers : The ‘Multiple planes’ for performing the routing operation, if the number of
interconnections in a design is high in a PCB
: The conductive drill holes for providing electrical connectivity among various
• layers in a ‘Multilayer’ PCB
Gerber File : A universally acceptable standard for representing layout information for PCB
fabrication
Printed Circuit Board (PCB) : A platform for placing electronic and electromechanical components and inter¬
connecting them without discrete wires.
PCB Etching (Photo engraving) : A subtractive method for fabricating the PCB
PCB Milling : A subtractive method for fabricating the,PCB
PCB Printing : An additive method for fabricating the PCB
Single Sided PCB : PCB with a single layer for routing and connections
Double Sided PCB : PCB with track routing and component placement done on both sides of the
PCB
Introduction to Embedded Systems
Multilayer PCB ,.. -y i PCB with multiple layers for track routing
Piggy-back/plug-in/daughter : PCB designed to plug into some other PCBs
PCB
Flexible PCB : PCB with flexible substrate for copper depositing-for tracks and footprint'
Solder Mask\ <. ^ : Aplastic layer deposited on the copper tracks of the PCB to protect it from cor¬
rosion and abrasion .‘ >•
Silk Screen : The -legend for prhiting readable information on the PCB
Conformal Coating : A protective coating made up of dilute solution of silicon rubber or epoxy for-
. ..__ _ ■_. 1 . r.
protecting the PCB from i ,.ne environmental conditions
Objective Questions )
1. Which of the following analog electronic component is used for clamping of voltage in electronic circuits
(a) Schottky diode (b) Transistor (c) TRIAC (d) Zener diode
2. Which of the following analog electronic component(s) is (are) used for noise signal filtering in electronic
circuits
(a) Capacitor (b) Transistor (c) Inductor (d) All of these
(e) Only (a) and (c)
3. Which of the following is (are) true about Open Collector configuration?
(a) The emitter of the transistor is grounded (b) The emitter of the transistor is open
(c) The collector of the transistor is open • (d) The collector of the transistor is grounded
(e) (a) and (c) (f) (b) and (d)
4. The logic expression Y=AB + AB represents the logic gate
(a) AND (b) NAND (c) OR (d) XOR
(e) NOR
5. Which of the following is an example for Buffer IC?
(a) 74LS00 (b) 74LS244 (c) 74LS08 (d) 74LS373
(e) None of these
6. Which of the following digital circuits is used for selecting one input from a set of inputs and connecting it to an
output line
(a) Buffer (b) Latch (c) Multiplexer (d) De-Multiplexer
(e) None of these
7. Combinational circuits contain a memory element. State True or False
(a) True (b) False
8. Half Adder is an example of Sequential circuit. State True or False
(a) True (b) False
9. Which of the following flip-flop is known as Delay Flip-Flop
(a) S-R (b) J-K (c) D (d) T
10. For an S-R flip-flop, the previous output state is 0 and the current input state isS=R = 1, what is the current output
state?
(a) 1 (b) 0 (c) Undefined
11. Which of the following flip-flop is known as Toggle Flip-Flop
(a) S-R (b) J-K (c) D (d) T
12. For a J-K Flip-Flop, the previous output state is 1 and the current input state is J=K= 0, what is the current output
state?
(a) 1 (b) 0 (c) Undefined-
Embedded Hardware Design and Development
13. For a J-K Flip-Flop, the previous output state is 0 and the current input state is J=K=1, what is the current output
state?
(a) 1 (b) 0 (c) Undefined
14. For a T Flip-Flop, the previous output state is 1 and the current input state is 1, what will be the current output state
on applying a clock pulse?
(a) 1 (b) 0 (c) Undefined
15. The number of logic gates present in an IC is 500. The integration type of the IC is?
(a) MSI (b) SSI (c) LSI ' (d) VLSI
16. The type of integration for a microprocessor chip is
. (a) MSI (bKSSI (c) LSI (d) VLSI
17. The design of an integrated circuit with built-in ADC is an example for
(a) Analog (b) Digital (c) Mixed signal (d) None of these
18. In embedded hardware design context, schematic represents
(a) The physical arrangement of various components present in the hardware product
(b) The different components involved in a hardware product and the interconnection among them
(c) Both of these - (d) None of these
19. In embedded hardware design context. Bill of Materials (BoM) represents
(a) The type and value of each components present in the hardware
(b) The quantity of different components present in the hardware
(c) Both of these (d) None of these
20. In embedded hardware design context, ‘Netlist’ is
(a) The soft form representation of the different components of a hardware and the interconnection among them
(b) The output file generated from schematic design (c) The input file for the PCB layout design
(d) All of these (e) None of these
21. In embedded hardware design context, ‘Layout’ is
(a) A soft form representation of the PCB
(b) Software ‘Blueprint’ representing the physical placement of components in a hardware
(c) All of these (d) None of these
22. Which of the following are the building blocks of a ‘layout’?
(a) Footprints (b) Routes (c) Layers (d) Vias
(e) All of these (f) (a) and (b)
in 23. What is ‘Footprint’ in the ‘Layout’ context?
(a) The ‘Top view’ of a component
(b) The three-dimensional representation of a component .
(c) Both of these (d) None of these
24. Which of the following package(s) contain pins/pads on only one side of the component?
(a) Zigzag In-line Package (ZIP) (b) Single In-line Package (SIP)
(c) Dual In-line Package (DIP) (d) All of these (e) None of these
25. Which of the following is a Surface Mount Package?
(a) PDIP (b) TSSOP (c) SOIC (d) All of these
(e) only (b) and (c)
ut 26. Which of the following package(s) contain(s) pins/pads on the four sides of the component?
(a) VQFP (b) SOICS (c) TSSOP (d) PQFP
(e) (a) and (d)
27. The representation of interconnection among various components of a hardware in ‘Layout’ is known as
(a) Footprint (b) Route/Trace (c) Layer (d) None of these
28. In embedded hardware design context, a ‘via’ is a
(a) conductive drill hole (b) interconnection among two components (c) Ground line
(d) Power line (e) None of these
l
Introduction to Embedded Systems
29. Which is (are) the layers of ‘Layout’ used for printing ‘Assembly Notes’?
(a) AST (b) ASB (c) TOP (d) BOT
(e) (a) and (b)
30. What is gerber file in the PCB Design context?
(a) File containing the component PCB layout info, routing info, drill details, etc. in a universally accepted file
exchange protocol format
(b) File containing the component PCB layout info, routing info, drill details, etc. in a proprietary file exchange
protocol format
(c) A collection of art works in a gerber format for each layer of the PCB
(d) (a) and (c) (e) None of these
31. Which of the following is(are) true about a single sided PCB?
(a) Only a single layer is used for routing the connections between components
(b) Components are placed on only one side of the PCB
(c) All of these (d) None of these
32. Which of the following is(are) true about flexible PCB?
(a) Highly flexible compared to normal PCB (b) Uses flexible substrate for etching
(c) Commonly used for the fabrication of Antennas, membrane keyboards, etc.
(d) All of these (e) None of these
33. Which of the following technique(s) is(are) used for PCB fabrication
(a) Photo Engraving (b) PCB Milling (c) PCB printing (d) All of these
34. Which of the following is(are) subtractive process for PCB fabrication?
(a) Photo Engraving '(b) PCB printing (c) PCB Milling (d) All of these
(e) None of these (f) only (a) and (c)
35. Which of the following is(are) tme about ‘Solder Mask’?-
(a) It is a conductive layer (b) It is a non-conductive layer
(c) It protects the copper tracks from corrosion (d) It prevents the ‘wetting’ of solder
(e) (b), (c) and (d)
36. Which of the following is(are) true about ‘Conformal Coating’?
(a) It is a conductive layer (b) It is a non-conductive layer
(c) It is a coating over the PCB to protect PCB
(d) Dilute solution of silicon rubber or epoxy, or plastic is used as the conformal coating material
(e) (b), (c) and (d)
-.
Review Questions
_
1. Explain the role of the analog electronic components resistor, transistor, capacitor and diode in embedded hard¬
ware design. Draw a circuit used in embedded application using these components
2. Explain the role of logic gates in embedded hardware design. Draw a circuit using the ‘AND’ and ‘OR’ gate ICs
in embedded application
3. What is open collector? State its significance in embedded hardware development
4. Explain the role of de-coders in embedded hardware development. Draw the circuit diagram for interfacing a 3-bit
binary decoder with 8051. Explain the functioning of the circuit
5. What is a combinational circuit? Draw a combinational circuit for embedded application development
6. What is a sequential Circuit? Draw a sequential circuit.and explain its functioning
7. Explain the difference between digital combinational'and sequential circuits
8. Explain the difference between synchronous and asynchronous sequential circuits. Give an example for both
9. What is an Integrated Circuit (IC)? Explain the different types of integrations for ICs. Give an example for each
Embedded Hardware Design and Development
10. Explain the different types of IC design. Give an example for each
11. Explain the different steps involved in VHDL based IC design
12. What is Electronic Design Automation (EDA) tool. Explain the role of EDA tools in embedded system design
13. What is schematic? Explain the role of schematic in embedded hardware design
14. Explain ‘Bill of Material'" (BoM) in embedded hardware design context. What is the use of BoM in Embedded
Hardware design and development?
15. What is ‘Netlist’ in the Embedded Hardware design context? Explain its role in hardware design
16. ' Explain the terms ‘Layout’ and ‘Layout Design’ in the hardware design context
17. What are the building blocks of a ‘Layout’? Explain them in detail
18. What is the difference between ‘Package’ and ‘Footprint’? Explain the significance ofboth in embedded hardware
design
19. What is ‘Package’ in the embedded hardware design context? What is ‘Through hole’ package and ‘Surface Mount’
package? ,
20. What is the difference between ‘Single In-line Package’ (SIP) and ‘Dual In-line Package’ (DIP)?
21. What are the commonly available DIP packages?
22. What is ‘Zigzag In-line Package’ (ZIP)?
23. Explain the commonly used ‘Surface Mount Packages’ in detail
24. What is ‘layer’ in the embedded hardware design context?
25. What is ‘via’? What-is the difference between ‘via’ and ‘Through hole’!
26. What is ‘Through hole'! What are the different types of ‘Through holes’ used in PCB design?
27. What is ‘Assembly Note’! What is the role of ‘Assembly Notes’ in embedded hardware design?
28. What is ‘gerber’! What is ‘gerberfile’ in PCB design context?
29. Explain the general guidelines for an efficient PCB layout
30. Explain PCB in the Hardware Design context? Explain the different types of PCBs
31. What is ‘Flexible PCB’! What is its role in Embedded Systems?
32. Explain the different PCB fabrication techniques. State the merits and drawbacks of each
33. Explain the ‘PCB etching’ process in detail
34. What is ‘Solder Mask’! Explain its significance in PCB fabrication
35. What is ‘Silk Screen’ and ‘Silk Screen Printing’! Explain their significance in PCB fabrication
36. What is ‘Conformal Coating’ in the PCB context? Explain its significance in hardware maintenance.
Lab Assignments
f- 1. Draw the schematic diagram using CaptureCIS for encoding the keys connected to the input lines of the 8 to 3
encoder 74LS148 arid reading the encoded output using the AT89C51 microcontroller as per the requirements.
given below
f, (a) The Enable Input (El) line of the encoder is permariently grounded
IjjL, (b) The output lines AO to A2 of the encoder are interfaced to P1.0 to P1.2 of the microcontroller
■fife‘(c) Assume that a regulated supply of 5 V is fed to the board from an extdmaEac-dc adaptor. Use a power jack
i, on the board for connecting the external supply. Use filter.capacitor 0.1 MFD/12V along with the jack
d) Use a crystal frequency of 12MHz for the microcontroller
Develop the PCB layout for the above requirement using ‘Layout’ tool. The package details for the components
P|)flifri^iverfbeiqyvf"■ '• ; ' '■ y t ‘ *«*•*
^fei^licfdcoritrdlier: PDIP 40,74LS148 : PDIP16, Capacitor: Use a 2 Pin Package from the design LibTary, Crystal
If I : Use the through hole package (or 2 Pin package) for crystal from the Layout design library, Power Jack : Use
lljlfv-frhe power j ack package from the library '
Introduction to Embedded Systems
3. Draw the schematic diagram using CaptureCIS to interface the 3 to 8 decoder 74LS138 with the AT89C51 mi¬
crocontroller as per the requirements given below
(a) LEDs are connected to the 8 output lines of the decoder in such a way that anode is connected to 5V supply
and cathode to the output pin of the decoder
(b) The enable lines El\ and E2\ are grounded permanently and E3 is connected to the supply voltage VCC
through a high value current limiting resistor •,
(c) The input lines AO, A1 and A2 of the .microcontroller are connected to the Port'pins.PI .0 to PI.2 of the
microcontroller * ,
(d) Assume that a regulated supply of 5V is fed to the board from an externa! ac^dc adaptor. Use a power jack
on the board for connecting the external supply. Use.filter capacitor 0.1MFD/12V along with the jack/:. x
(e) Use a crystal frequency of 12MHz for the microcontroller , , , . ^ *../
4. Develop the PCB layout for the above requirement using ‘Layout’- tool. The package details for the components
are given below - ..... .. .... «
Microcontroller: PDIP40,74LS138 : PDIP 16, Capacitor: Use a, 2 Pin Package from the design library, Crystal
: Use the through hole package (or 2 Pin package) for crystal from the Layout,design library, Power Jack : Use
the power jack package from the library ;
5. Draw the schematic diagram using CaptureCIS for interfacing the Tri-State Buffer 74LS244 in memory mapped
configuration with AT89C51 microcontroller as per the requirements given below
(a) DIP switches are connected to.the.8 input lines of.the tri-state buffer through resistors 4.7K. The input line
is at logic high when the switch is at open state and at logic low when the switch.is at,closed state
(b) The output lines of the buffer are connected to the data bus port PO of the microcontroller
(c) The PortO pins are pulled to the supply voltagedhrough4,7K.resistor . >,
(d) The address for the, tri-state buffer is assigned as 0QF8H. Use logic gates AND, NAND and 3 to 8 decoder
IC to decode the address line and generate the address selection.pulse for the buffer
(e) The address selection pulse along with the RD\ signal of the microcontroller is used for enabling the buf¬
fer
(f) Assume that a regulated supply of 5 V is fed to the board from an external ac-dc adaptor. Use a power jack
on the board for connecting the external supply. Use filter capacitor 0.1MFD/12V along with the jack
(g) Use a crystal frequency of 12MHz for the microcontroller
6. Develop the PCB layout for the above requirement using ‘Layout’ tool. The package details for the components
are given below
Microcontroller : PDIP 40, 74LS244 : PDIP 20, Capacitor : Use a 2 Pin Package from the design library, AND
and NAND Gates : Use respective DIP package for the IC in use, 74LS138 3-8 decoder: PDIP 16, Crystal : Use
the through hole package (or 2 Pin package) for crystal from the Layout design library, Power Jack : Use the
power jack package from the library
7. Draw the schematic diagram using CaptureCIS to de-multiplex the lower order address of 8051 with the latch
74LS373 as per the requirements given below
(a) The output control line of the latch is permanently grounded
(b) The ALE signal of the microcontroller is connected to the Enable line (G) of the latch
(c) The Port 0 pins are pulled to the supply voltage through 4.7K resistor
(d) The de-multiplexed address bus is comiected to an 8-pin jumper (connector)
(e) Use a crystal frequency of 12MHz for the microcontroller /
(f) Assume that a regulated supply of 5 V is fed to the board from an external ac-dc adaptor. Use a power jack
on the board for connecting the external supply. Use filter/capacitor 0.1MFD/12V along with the jack
,8, Draw the schematic diagram using CaptureCIS to interface a common cathode 7-Segmnet LED display through
a 74LS373 latch in memory mapped configuration with AT89C51 micro'controller as per the requirements given
below
(a) The output control line of the latch is permanently grounded
Embedded Hardware Design and Development
(b) The address for the latch is assigned as FFOOH. Use logic gates AND, NAND and 3 to 8 decoder IC to
decode the address line and generate the address selection pulse for the buffer
^c) The address selection pulse along with the .write signal WR\ of the microcontroller is used for enabling the
{a « '■ ' A .. a’A-. 'v. a v.. 7 : a . a . v'- ■ " •• ' - "'A ‘ : - aa V Laa-a'
latch
(d). Use a crystal frequency ofi2MHz for the microcontroller
(c) Assume that a regulated supply of 5V is fed to the:board from an external ac-dc adaptor. Use a power jack
,_ . . • v; ...A. A ' • ■'■■■■ _■ ' ■. . - ■ - . •. ■' ■
- :LED: Use the available package from the layout design library, Crystal: Use the through hole package (or 2 pin
■> package) for crystal from the'Layout design library, power jack : Use the power jack package from the library,
■ 'lOL.Uraw the schematic diagrarn for'interfacing the; AD570 AD converter from analog device, to the AT89C51
A.: V.:, "AA 'AA - -A ^ f AAAA. ',:,r .
microcontroller as per tlie requirements given below . ■ .
■ (a) Assume that the supply voltages +5V and-15V are available externally and they are connected to the circuit
v'-s 'k'*
‘ -v
(d) the ADC uses interrupt of microcontroller for indicating the data conversion completion
.' (e) The analog input signal for conversion is supplied to the AD Converter from external source through a
; ■ jumper (connector) a ' 7 : A- 4aA. ' •„.■ - ; -A:'.,: .
.11. Draw the schematic diagram lor interfacing the DM9370 7-segment decoder driver, from Fairchild semiconduc¬
tor, for driving a common anode 7-segment LED display with the AT89C51 microcontroller as per the require-
. ments given below' . . , , , ,
. . (a) Use 330E external current limiting resistor for connecting the 7-segment LED display with the DM9370
; ic . ,';~ ~ ~ :; : ' '
(B) The DM9370 is memory mapped with the microcontroller and it is activated by a write to memory location
' ; 8000H . A>a\:, ' ' p ') 7 " A.... , \"A yT'A A
1 (c) Assume that a regulated supply of 5V is fed to the board from an external ac-dc adaptor. Use a power jack
on the board for connecting the external supply. Use filter capacitor 0.1 MFD/12 V along with the jack
(d) Use a crystal frequency of 12MHz for the microcontroller
Embedded Firmware Design and
Development
LEARNING OBJECTIVES
J . a: '
merit ■
v' Learn about the'fundamentals of embedded firmware design using Embedded V
S Learn the similarities and differences, between conventional ^' programming and T programming for Embedded .
application development •
■ri Learn the difference between native and cross-platform development
S Learn about Keywords and Identifiers, Data types, Storage Classes, Arithmetic and Logic Operations, Relational
Operations, Branching Instructions, Looping Instructions, Arrays and Pointers, Characters and Strings, Functions,
Function Pointers, Structures and Unions, Preprocessors and Macros, Constant Declarations, Volatile Variables, Delay
generation and Infinite loops, Bit manipulation operations, Coding InterrupbService Routines, Recursive and Reen-.
trant functions, and Dynamic memory allocation in Embedded C
The embedded firmware is responsible for controlling the various peripherals of the embedded hard¬
ware and generating response in accordance with the functional requirements mentioned in the require¬
ments for the particular embedded product. Finnware is considered as the master brain of the embedded
Embedded Firmware Design and Development
system. Imparting intelligence to an Embedded system is a one time process and it can happen at any
stage, it can be immediately after the fabrication of the embedded hardware or at a later stage. Once
intelligence is imparted to the embedded product, by embedding the firmware in the hardware, the
product starts functioning properly and will continue serving the assigned task till hardware breakdown
occurs or a corruption in embedded firmware occurs. In case of hardware breakdown, the damaged
component may need to be replaced by a new component and for firmware corruptions the firmware
should be re-loaded, to bring back the embedded product to the normal functioning. Coming back to the
newborn baby example, the newborn baby is very adaptive in terms of intelligence, meaning it leams
from mistakes and updates its memory each time a mistake or a deviation in expected behaviour occurs,
whereas most of the embedded systems are less adaptive or non-adaptive. For most of the embedded
products the embedded firmware is stored at a permanent memory (ROM) and they are nonalterable by
end users. Some of the embedded products used in the Control and Instrumentation domain are adap¬
tive. This adaptability is achieved by making use configurable parameters which are stored in the alter¬
able permanent memory area (like NVRAM/FLASH). The parameters get updated in accordance with
the deviations from expected behaviour and the firmware makes use of these parameters for creating the
response next time for similar variations.
Designing embedded firmware requires understanding of the particular embedded product hardware,
like various component interfacing, memory map details, I/O port details, configuration and register de¬
tails of various hardware chips used and some programming language (either target processor/controller
specific low level assembly language or a high level language like C/C++/JAVA).
Embedded firmware development process starts with the conversion of the firmware requirements
into a program model using modelling tools like UML or flow chart based representation. The UML
diagrams or flow chart gives a diagrammatic representation of the decision items to be taken and the
tasks to heqperformed (Fig. 9.1). Once the program model is created, the next step is the implementa¬
tion of the tasks and actions by capturing the model using a language which is understandable by the
target processor/controller. The following sections are designed to give an overview of the various steps
involved in the embedded firmware design and development.
top are executed after completing the first task. This is a true procedural one.-In a multiple task based
system, each task is executed in serial in this approach. The firmware execution flow for this will be
1. Configure the common parameters and perform initialisation for various hardware components
memory, registers, etc.
2. Start the first task and execute it
3. Execute the second task
4. Execute the next task
■5. :•
6. : ' •
Configuratio:
Initializat.
V r .* fca* MX , -'A if $'i\
while (1)
Task h ()
Almost all tasks in embedded applications are non-ending, and are repeated infinitely throughout the
operation. From the above ‘C’ code you can see that the tasks 1 to n are performed one after another and
when the last task (n{h task) is executed, the firmware execution is again re-directed to Task 1 and it is
repeated forever in the loop. This repetition is achieved by using an infinite loop. Here the while (1) {}
loop. This approach is also referred as ‘Super loop based Approach'.
Since the tasks are running inside an infinite loop, the only way to come out of the loop is either
a\hardware reset or an interrupt assertion. A hardware reset brings the program execution back to the
main lodp. Whereas an interrupt request suspends-the task execution temporarily and performs the cor¬
responding interrupt routine and on completion of the interrupt routine it restarts the task execution from
the point where it got interrupted.
The ‘Super loop based design' doesn’t require an operating system, since there is no need for sched¬
uling which task is to be executed and assigning priority to each task. In a super loop based design, the
priorities are fixed and the order in which the tasks to be executed are also fixed. Hence the code for
performing these tasks will be residing in the code memory without an operating system image.
This type of design is deployed in low-cost embedded products and products where response time
is not time critical. Some embedded products demands this type of approach if some tasks itself are
sequential. For example, reading/writing data to and from a card using a card reader requires a sequence
of operations like checking the presence of card, authenticating the operation, reading/writing, etc. it
Embedded Firmware Design and Development
should strictly follow a specified sequence and the combination of these series of tasks constitutes a
• single task-namely data readAvrite. There is no rise in putting the sub-tasks into independent tasks and
running them parallel. It won’t work at all. j
Atypical example of a ‘Super loop based’ product is an electronic video game toy containing keypad
and display unit. The program running inside the product may be designed in such a way that it reads-the
keys to detect whether the user has given any input and if any key press is detected the graphic display
is updated. The keyboard scanning and display updating happens at a reasonably high rate. Even if the
application misses a key press, it won’t create any critical issues; rather it will be treated as a bug in the
I firmware ©. It is not economical to embed an OS into low cost products and it is an utter waste to do so
if the response requirements are not cmcial.
. The ‘Super loop based design’ is simple and straight forward without any OS related overheads.
" The major drawback of this approach is that any failure in any part of a single task will affect the total
f system. If the program hangs up at some point while executing a task, it will remain there forever and
5 ultimately the product stops functioning. There are remedial measures for overcoming this. Use of
Hardware and software Watch Dog Timers (WDTs) helps in coming out from the loop when an unex-
j pected failure.occurs or when the processor hangs up. This, in turn, may cause additional hardware cost
and firmware overheads.
|i Another major drawback of .the ‘Super loop’ design approach is the lack of real timeliness. If the
| ■ number of tasks to be executed within an application increases, the time at which each task is repeated
§ also increases. This brings the probability of missing out some events. For example in a system with
1 Keypads, according to the ‘Super loop design’, there will be a task for monitoring the keypad connected
I I/O lines and this need not be the task running while you press the keys (That is key pressing event may
‘I not be in sync with the keypad press monitoring task within the firmware). In order to identify the key
j press, you may have to press the keys for a sufficiently long time till the keypad status monitoring task
' is executed internally by the firmware. This will really lead to the lack of real timeliness. There are cor-
i rective measures for this also. The best advised option in use interrupts for external events requiring real
time attention. Advances in processor technology brings out low cost high speed processors/controllers,
i use of such processors in super loop design greatly reduces the time required to service different tasks
and thereby are capable of providing a nearly real time attention to external events.
Throughout this book under the title ‘Embedded Firmware Design and Development’, we will be
discussing only the ‘Super loop based design'. Again the discussion is narrowed to super loop based
firmware development for 8051 controller.
The Operating System (OS) based approach contains operating systems, which can be either a General
Purpose Operating System (GPOS) or a Real Time Operating System (RTOS) to host the user written
application firmware. The General Purpose OS (GPOS) based design is very similar to a conventional
PC based application development where the device contains an operating system (Windows/Unix/
Linux, etc. for Desktop PCs) and you will be creating and running user applications on top of it. Ex¬
ample of a GPOS used in embedded product development is Microsoft® Windows XP Embedded.
Examples of Embedded products using Microsoft® Windows XP OS are Personal Digital Assistants
; (PDAs), Hand held devices/Portable devices and Point of Sale (PoS) terminals. Use of GPOS in embed¬
ded products merges the demarcation of Embedded Systems and general computing systems in terms
of OS. For Developing applications on top of the OS, the OS supported APIs are used. Similar to the
Introduction to Embedded Systems
different hardware specific drivers, OS based applications also require ‘Driver software’ for different
hardware present on the board to communicate with them.
Real Time Operating System (RTOS) based design approach is employed in embedded products
demanding Real-time response. RTOS respond in a timely and predictable manner to events. Real Time
operating system contains a Real Time kernel responsible for performing pre-emptive multitasking,
scheduler for scheduling tasks, multiple threads, etc. A Real Time Operating System (RTOS) allows
flexible scheduling of system resources like the CPU and memory and offers some way to communicate
between tasks. We will discuss the basics of RTOS based system design in a later chapter titled 'Design¬
ing with Real Time Operating Systems (RTOSf.
‘Windows CE\ ‘pSOS’, ‘Vx Works’, ‘ThreacDC, ‘ MicroC/OS-IP, ‘Embedded Linux’, ‘Symbian’ etc
are examples of RTOS employed in embedded product development. Mobile phones, PDAs (Based on
Windows CE/Windows Mobile Platforms), handheld devices, etc. are examples of ‘Embedded Prod¬
ucts’ based on RTOS. Most of the mobile phones are built around the popular RTOS ‘Symbian’.
required to perform the action specified by the opcode. It is not necessary that all opcode should have
Operands following them. Some of the Opcode implicitly contains the operand and in such situation no
operand is required. The operand may be a single operand, dual operand or more. We will analyse each
of them with the 8051 ASM instructions as an example.
HlOV A, #30
This instruction mnemonic moves decimal value 30 to the 8051 Accumulator register. Here MOV A
is the Opcode and 30 is the operand (single operand). The same instruction when written in machine
language will look like
01110100 00011110
t where the first 8 bit binary value 01110100 represents the opcode MOV A and the second 8 bit binary
value 00011110 represents the operand 30.
„ The mnemonic INC A is an example for instruction holding operand implicitly in the Opcode. The
machine language representation of the same is 00000100. This instruction increments the 8051 Accu¬
mulator register content by 1.
The mnemonic MOV A, #30 explained above is an example for single operand instruction.
UMP 16bit address is an example for dual operand instruction. The machine language for the same
is
p'O0000010 addr_bitl5 to addr_bit 8 addr_bit7 to addr_bit Op- ' ::-:v
The first binary data is the representation of the LJMP machine code. The first operand that imme¬
diately follows the opcode represents the bits 8 to 15 of the 16bit address to which the jump is required
and the second operand represents the bits 0 to 7 of the address to which the jump is targeted.
Assembly language instructions are written one per line. A machine code program thus consists of
a sequence of assembly language instructions, where each statement contains a mnemonic (Opcode +
Operand). Each line of an assembly language program is split into four fields as given below
LABEL OPCODE OPERAND COMMENTS
LABEL is an optional field. A ‘LABEL’ is an identifier used extensively in programs to reduce the reli¬
ance on programmers for remembering where data or code is located. LABEL is commonly used for
representing
• A memory location, address of a program, sub-routine, code portion, etc.
• The maximum length of a label differs between assemblers. Assemblers insist strict formats for
labelling. Labels are always suffixed by a colon and begin with a valid character. Labels can con¬
tain number from 0 to 9 and special character (underscore).
Labels are used for representing subroutine names and jump locations in Assembly language
programming. It is to be noted that ‘LABEL’ is not a mandatory field; it is optional only.
The sample code given below using 8051 Assembly language illustrates the structured assembly
language programming.
.;#!########.###########################ft##############################
, SUBROUTINE FOR GENERATING DELAY
DELAY PARAMETR PASSED THROUGH. REGISTER R1
; RETURN VALUE NONE
PA REGISTERS USED: R0, Rl
Pi#############-#-##########t#####.#####################################
Introduction to Embedded Systems
The Assembly program contains a main routine which starts at address OOOOH and it may or may not
contain subroutines. The example given above is a subroutine, where in the main program the subrou¬
tine is invoked by the Assembly instruction
LCALL DELAY
Executing this instruction transfers the program flow to the memory address referenced by the ‘LA¬
BEL’ DELAY.
It is a good practice to provide comments to your subroutines before the beginning of it by indicating
the purpose of that subroutine, what the input parameters are and how they are passed to the subroutines,
which are the return values, how they are returned to the calling function, etc. While assembling the
code a informs the assembler that the rest of the part coming in a line after the symbol is comments
and simply ignore it. Each Assembly instruction should be written in a separate line. Unlike C and other
high level languages, more than one ASM code lines are not allowed in a single line.
In the above example the LABEL DELAY represents the reference to the start of the subroutine
DELAY. You can directly replace this LABEL by putting the desired address first and then writing the
Assembly code for the routine as given below.
ORG 0100H
MOV RO, 1255 v Load Register RO with 5OH
DJNZ( Rl, 010OH :-Decrement5'Rl; and loop Ail
RET Y. % Return to calling progr-afti.
The advantage of using a label is that the required address is calculated by the assembler at the
time of assembling the program and it replaces the Label. Hence even if you add some code above the
LABEL ‘DELAY’ at a later stage, it won’t create any issues like code overlapping, whereas in the sec¬
ond method where you are implicitly telling the assembler that this subroutine should start at the speci¬
fied address (in the above example 0100H). If the code written above this subroutine itself is crossing
the 0100H mark of the program memory, it will be over written by the subroutine code and it will gener¬
ate unexpected results©. Hence for safety don’t assign any address by yourself, let us refer the required
address by using labels and let the assembler handle the responsibility for finding out the address where
the code can be placed. In the above example you can find out that the label DELAY is used for calling
the subroutine as well as looping (using jumping instruction based on decision-DJNZ). You can also use
the normal jump instruction to jump to the label by calling LJMP DELAY
The statement ORG 0100H in the above example is not an assembly language instruction; it is an
assembler directive instruction. It tells the assembler that the Instructions from here onward should be
placed at location starting from 0100H. The Assembler directive instructions are known as ‘pseudo-
ops’. They are used for
1. Detemiining the start address of the program (e.g. ORG OOOOH)
2. Determining the entry address of the program (e.g. ORG 0100H)
3. Reserving memory for data variables, arrays and structures (e.g. var EQU 70H
4. Initialising variable values (e.g. val DATA 12H)
Embedded Firmware Design and Development
The EQU directive is used for allocating memory to a variable and DATA directive is used for initial¬
ising a variable with data. No machine codes are generated for the ‘pseudo-ops’.
Till now we discussed about Assembly language and how it is used for writing programs. Now let us
have a look at how assembly programs are organised and how they are translated into machine readable
codes.
The Assembly language program written in assembly code is saved as .asm (Assembly file) file or
' ap .src (source) file. Any text editor like ‘notepad’ or ‘WordPad’ from Microsoft® or the text editor
provided by an Integrated Development (IDE) tool can be used for writing the assembly instructions.
r Similar to ‘C’ and other high level language programming, you can have multiple source files called
modules in assembly language programming. Each module is represented by an '.asm' or ‘.src' file
similar to the ‘.c' files in C programming. This approach is known as ‘Modular Programming’. Modular
'programming is employed when the program is too complex or too big. In ‘Modular Programming’, the
entire code is divided into submodules and each module is made re-usable. Modular Programs are usu¬
ally easy to code, debug and alter. Conversion of the assembly language to machine language is carried
out by a sequence of operations, as illustrated below.
9.2.1.1 Source File to Object File Translation Translation of assembly code to machine code is
performed by assembler. The assemblers for different target machines are different and it is common
that assemblers from multiple vendors are available in the market for the same target machines. Some
target processor’s/controller’s assembler may be proprietary and is supplied by a single vendor only.
I Some assemblers are freely available in the internet for downloading. Some"assemblers are commercial
’ and requires licence from the vendor. A51 Macro Assembler from Keil software is a popular assembler
/ for the 8051 family microcontroller. The various steps involved in the conversion of a program written
in assembly language to corresponding binary file/machine language is illustrated in Fig. 9.1.
Library Files
Source File 1
(.asm or .src file) Module Assembler Object File 1 ,
(Module-1) ,
H wmmmm
Source File 2 . §
(.asm or src file) — .Module Assembler Object Flip 2
(Module-2)
..V ye-C;;Vv:;
Machine Code
(Hex File)
Each source module is written in Assembly and is stored as .src file or .asm file. Each file can be
assembled separately to examine the syntax errors and incorrect assembly instructions. On successful
assembling of each .src/.asm file a corresponding object file is created with extension ‘.obf. The object
file does not contain the absolute address of where the generated code needs to be placed on the program
memory and hence it is called a re-locatable segment. It can be placed at any code memory location and
it is the responsibility of the linker/locater to assign absolute address for this module. Absolute address
allocation is done at the absolute object file creation stage. Each module can share variables and subrou¬
tines (functions) among them. Exporting a variable/fiinction from a module (making a variable/function
from a module available to all other modules) is done by declaring that variable/function as PUBLIC in
the source module.
Importing a variable or function from a module (taking a variable or function from any one of other
modules) is done by declaring that variable or function as EXTRN (EXTERN) in the module where it
is going to be accessed. The ''PUBLIC Keyword informs the assembler that the variables or functions
declared as ‘PUBLIC needs to be exported. Similarly the ‘EXTRNT Keyword tells the assembler that
the variables or functions declared as ‘EXTRNT needs to be imported from some other modules. While
assembling a module, on seeing variables/functions with keyword ‘EXTRN’, the assembler understands
that these variables or functions come from an external module and it proceeds assembling the entire
module without throwing any errors, though the assembler cannot find the definition of-the variables
and implementation of the functions. Corresponding to a variable or function declared as ‘PUBLIC’ in a
module, there can be one or more modules using these variables or functions using 1EXTRIP keyword.
For all those modules using variables or functions with ‘EXTRIP keyword, there should be one and
only one module which exports those variables or functions with ‘PUBLIC keyword. If more than one
module in a project tries to export variables or functions with the same name using ‘PUBLIC keyword,-
it will generate ‘linker’ errors.
Illustrative example for A51 Assembler-Usage of ‘PUBLIC’ for importing variables with same name
on different modules. The target application (Simulator) contains three modules namely ASAMPLE1.
A51, ASAMPLE2.A51 and ASAMPLE3.A51 (The file extension . A51 is the .asm extension specific
to A51 assembler). The modules ASAMPLE2.A51 and ASAMPLE3.A51 contain a function named
PUTCHAR. Both of these modules try to export this function by declaring the function as ‘PUBLIC’ in
the respective modules. While linking the modules, the linker identifies that two modules are exporting
the function with name PUTCHAR. This confuses the linker and it throws the error ‘MULTIPLE PUB¬
LIC DEFINITIONS’.
0
If a variable or function declared as ‘EXTRFT in one or two modules, there should be one module
defining these variables or functions and exporting them using ‘PUBLIC keyword. If no modules in a
project export the variables or functions which are declared as ‘EXTRN in other modules, it will gener¬
ate ‘linker’ warnings or errors depending on the error level/waming level settings of the linker.
Illustrative example for A51 Assembler-Usage of EXTRN without variables exported. The target
application (Simulator) contains three modules, namely, ASAMPLE1 .A51, ASAMPLE2.A51 and ASAM-
PLE3.A51 (The file extension .A51 is the .asm extension specific to A51 assembler). The modules
ASAMPLE 1.A51 imports a function named PUTCRLF which is declared as ‘EXTRN in the current
module and it expects any of the other two modules to export it using the keyword 'PUBLIC. But none
of the other modules export this function by declaring the function as ‘PUBLIC in the respective mod¬
ules. While linking the modules, the linker identifies that there is no function exporting for this function.
The linker generates a warning or error message ‘ UNRESOLVED EXTERNAL SYMBOL’ depending on
the linker ‘level’ settings.
( *** WARNING LI: UNRESOLVED EXTERNAL SYMBOL . . ■ - .
,put_crlf
' MODULE: ASAMPLE1. obj (SAMPLE)
9.2.1.2 Library File Creation and Usage Libraries are specially formatted, ordered program col¬
lections of object modules that may be used by the linker at a later time. When the linker processes a li¬
brary, only those object modules in the library that are necessary to create the program are used. Library
files are generated with extension ‘.lib’. Library file is some kind of source code hiding technique. If you
don’t want to reveal the source code behind the various functions you have written in your program and
at the same time you want them to be distributed to application developers for making use of them in
their applications, you can supply them as library files and give them the details of the public functions
available from the library (function name, function input/output, etc). For using a library file in a project,
add the library to the project.
If you are using a commercial version of the assembler/compiler suite for your development, the ven¬
dor of the utility may provide you pre-written library files for performing multiplication, floating point
arithmetic, etc. as an add-on utility or as a bonus©.
‘LIB5F from Keil Software is an example for a library creator and it is used for creating library files
for A51 Assembler/C51 Compiler for 8051 specific controller.
9.2.1.3 Linker and Locater Linker and Locater is another software utility responsible for “linking
the various object modules in a multi-module project and assigning absolute address to each module”.
Linker generates an absolute object module by extracting the object modules from the library, if any
and those obj files created by the assembler, which is generated by assembling the individual modules
of a project. It is the responsibility of the linker to link any external dependent variables or functions
declared on various modules and resolve the external dependencies among the modules. An absolute
object file or module does not contain any re-locatable code or data. All code and data reside at fixed
memory locations. The absolute object file is used for creating hex files for dumping into the code
memory of the processor/controller.
‘BL5F from Keil Software is an example_for a Linker & Locater for A51 Assembler/C51 Compiler
for 8051 specific controller.
9.2.1.4 Object to Hex File Converter This is the final stage in the conversion of Assembly lan¬
guage (mnemonics) to machine understandable language (machine code). Hex File is the representa-
Introduction to Embedded Systems
tion of the machine code and the hex file is dumped into the code memory of the processor/controller.
The hex file representation varies depending on the target processor/controller make. For Intel proces¬
sors/controllers the target hex file format will be ‘Intel HEX’ and for Motorola, the hex file should be
in ‘Motorola HEX’ format. HEX files are ASCII files that contain a hexadecimal representation of
target application. Hex file is created from the final ‘Absolute Object File’ using the Object to Hex File
Converter utility.
‘OH5T from Keil software is an example for Object to Hex File Converter utility for A51 Assembler/
C51 Compiler for 8051 specific controller.
Efficient Code Memory and Data Memory Usage (Memory Optimisation) Since the devel¬
oper is well versed with the target processor architecture and memory organisation, optimised code
can be written for performing operations. This leads to less utilisation of code memory and efficient
utilisation of data memory. Remember memory is a primary concern in any embedded product (Though
silicon is cheaper and new memory techniques make memory less costly, external memory operations
impact directly on system performance).
High Performance Optimised code not only improves the code memory usage but also improves the
total system performance. Through effective assembly coding, optimum performance can be achieved
for a target application.
Low Level Hardware Access Most of the code for low level programming like accessing external
device specific registers from the operating system kernel, device drivers, and low level interrupt rou¬
tines, etc. are making use of direct assembly coding since low level device specific operation support is
not commonly available with most of the high-level language cross compilers.
Code Reverse Engineering Reverse engineering is the process of understanding the technology be¬
hind a product by extracting the information from a finished product. Reverse engineering is performed
by ‘hawkers’ to reveal the technology behind ‘Proprietary Products’. Though most of the products
employ code memory protection, if it may be possible to break the memory protection and read the code
memory, it can easily be converted into assembly code using a dis-assembler program for the target
machine.
9.2.1.6 Drawbacks of Assembly Language Based Development Every technology has its
own pros and cons. From certain technology aspects assembly language development is the most ef¬
ficient technique. But it is having the following technical limitations also.
High Development Time Assembly language is much harder to program than high level languages.
The developer must pay attention to more details and must have thorough knowledge of the architecture,
memory organisation and register details of the target processor in use. Learning the inner details of the
processor and its assembly instructions is highly time consuming and it creates a delay impact in product
development. One probable solution for this is use a readily available developer who is well versed in
Embedded Firmware Design and Development
the target processor architecture assembly instructions. Also more lines of assembly code are required
for performing an action which can be done with a single instruction in a high-level language like ‘C\
Developer Dependency There is no common written rule for developing assembly language based
applications whereas all high level languages instruct certain set of mles for application development. In
assembly language programming, the developers will have the freedom to choose the different memory
location and registers. Also the programming approach varies from developer to developer depending
on his/her taste. For example moving data from a memory location to accumulator can be achieved
through different approaches. If the approach done by a developer is not documented properly at the
development stage, he/she may not be able to recollect why this approach is followed at a later stage or
when a new developer is instructed to analyse this code, he/she also may not be able to understand what
is done and why it is done. Hence upgrading an assembly program or modifying it on a later stage is
very difficult. Well documenting the assembly code is a solution for reducing the developer dependency
in assembly language programming. If the code is too large and complex, documenting all lines of code
may not be productive.
Non-Portable Target applications written in assembly instructions are valid only for that particular
family of processors (e.g. Application written for Intel x86 family of processors) and cannot be re-used
for another target processors/controllers (Say ARM 11 family of processors). If the target processor/con¬
troller changes, a complete re-writing of the application using the assembly instructions for the new
target processor/controller is required. This is the major drawback of assembly language programming
and it makes the assembly language applications non-portable.
“Though Assembly Language programming possesses lots of drawback, as a developer, from my
personal experience I prefer assembly language based development. Once you master the internals
of a processor/controller, you can really perform magic with the processor/controller and can extract
the maximum out of it ”
2
Machine Code
(Hex File)
[Fig. 9.2 j High level language to machine language conversion process
text editor provided by an Integrated Development (IDE) tool supporting the high level language in use
can be used for writing the program. Most of the high level languages support modular programming
approach and hence you can have multiple source files called modules written in corresponding high
level language. The source files corresponding to each module is represented by a file with correspond¬
ing language extension. Translation of high level source code to executable object code is done by a
cross-compiler. The cross-compilers for different high level languages for the same target processor are
different. It should be noted that each high level language should have a cross-compiler for convert¬
ing the high level source code into the target processor machine code. Without cross-compiler support
a high level language cannot be used for embedded firmware development. C51 Cross-compiler from
Keil software is an example for Cross-compiler. C51 is a popular cross-compiler available for ‘C’ lan¬
guage for the 8051 family of micro controller. Conversion of each module’s source code to correspond¬
ing object file is performed by the cross compiler. Rest of the steps involved in the conversion of high
level language to target processor’s machine code are same as that of the steps involved in assembly
language based development.
As an example of high level language based embedded firmware development, we will discuss how
‘Embedded C’ is used for embedded firmware development, in a later section of this chapter.
the only pre-requisites for high level language based firmware development. Rest of the things will be
taken care of by the cross-compiler used for the high level language. Thus the ramp up time required by
the developer in understanding the target hardware and target machine’s assembly instructions is waived
off by the cross compiler and it reduces the development time by significant reduction in developer ef¬
fort. High level language based development also refines the scope of embedded firmware development
from a team of specialised architects to anyone knowing the syntax of the language and willing to put
little effort on understanding the minimal hardware details. With high level language, each task can be
accomplished by lesser number of lines of code compared to the target processor/controller specific As¬
sembly language based development.
Developer Independency The syntax used by most of the high level languages are universal and
a-program written in the high level language can easily be understood by a second person knowing the
syntax of the language. Certain instructions may require little knowledge of the target hardware details
like register set, memory map etc. Apart from these, the high level language based firmware develop¬
ment makes the firmware, developer independent. High level languages always instruct certain set of
rules for writing the code and commenting the piece of code. If the developer strictly adheres to the
rules, the firmware will be 100% developer independent.
Portability Target applications written in high level languages are converted to target processor/con¬
troller understandable format (machine codes) by a cross-compiler. An application written in high level
language for a particular target processor can easily be converted to another target processor/controller
specific application, with little or less effort by simply re-compiling/little code modification followed by
re-compiling the application for the required target processor/controller, provided, the cross-compiler
has support for the processor/controller selected. This makes applications written in high level language
highly portable. Little effort may be required in the existing code to replace the target processor specific
header files with new header files, register definitions with new ones, etc. This is the major flexibility
offered by high level language based design.
9.2.2.2 Limitations of High Level Language Based Development The merits offered by high
level language based design take advantage over its limitations. Some cross-compilers available for high
level languages may not be so efficient in generating optimised target processor specific instructions.
Target images created by such compilers may be messy and non-optimised in terms of performance as
well as code size. For example, the task achieved by cross-compiler generated machine instructions
from a high level language may be achieved through a lesser number of instructions if the same task is
hand coded using target processor specific machine codes. The time required to execute a task also in¬
creases with the number of instructions. However modern cross-compilers are tending to adopt designs
incorporating optimisation techniques for both code size and performance. High level language based
code snippets may not be efficient in accessing low level hardware where hardware access timing is
critical (of the order of nano or micro seconds).
The investment required for high level language based development tools (Integrated Development
Environment incorporating cross-compiler) is high compared to Assembly Language based firmware
development tools.
ways; namely, mixing Assembly Language with High Level Language, mixing High Level Language
with Assembly and In-line Assembly programming.
9.2.3.1 Mixing Assembly with High level language (e.g. Assembly Language with ‘C’) As¬
sembly routines are mixed with ‘C’ in situations where the entire program is written in ‘C’ and the cross
compiler in use do not have a built in support for implementing certain features like Interrupt Service
Routine functions (ISR) or if the programmer wants to take advantage of the speed and optimised
code offered by machine code generated by hand written assembly rather than cross compiler gener¬
ated machine code. When accessing certain low level hardware, the timing specifications may be very
critical and a cross compiler generated binary may not be able to offer the required time specifications
accurately. Writing the hardware/peripheral access routine in processor/controller specific Assembly
language and invoking it from ‘C’ is the most advised method to handle such situations.
Mixing ‘C’ and Assembly is little complicated in the sense—the programmer must be aware of how
parameters are passed from the ‘C’ routine to Assembly and values are returned from assembly routine
to ‘C’ and how ‘Assembly routine’ is invoked from the ‘C’ code.
Passing parameter to the assembly routine and returning values from the assembly routine to the
caller ‘C’ function and the method of invoking the assembly routine from ‘C’ code is cross compiler
dependent. There is no universal written rule for this. You must get these informations from the docu¬
mentation of the cross compiler you are using. Different cross compilers implement these features in
different ways depending on the general purpose registers and the memory supported by the target
processor/controller. Let’s examine this by taking Keil C51 cross compiler for 8051 controller. The ob¬
jective of this example is to give an idea on how C51 cross compiler performs the mixing of Assembly
code with ‘C’.
1. Write a simple function in C that passes parameters and returns values the way you want your
assembly routine to. _
2. Use the SRC directive (#PRAGMA SRC at the top of the file) so that the C compiler generates an
.SRC file instead of an .ORJfile.
3. Compile the C file. Since the SRC directive is specified, the .SRC file is generated. The .SRC file
contains the assembly code generated for the C code you wrote.
4. Rename the .SRC file to A51 file.
5. Edit the .A51 file and insert the assembly code you want to execute in the body of the assembly
function shell included in the .A51 file.
As an example consider the following sample code (Extracted from Keil C51 documentation)
Ipragma SRC
unsigned char my_assembly_func (unsigned-int argument)
{
return (argument + 1); // Insert dummy lines to access all args and
// retvals
This C function on cross compilation generates the following assembly SRC file.
NAME TESTCODE
?PR?_my_assembly_func?TESTCODE SEGMENT CODE
PUBLIC _my_assembly_func
; #pragma SRC
; unsigned char my_assembly_func (
Embedded Firmware Design and Development
RSEG ?PR?_my_assembly_func?TESTC.ODE
USING 0
Assembly--funo.:
'‘'J-- Variable 'argument7040/ assigned to-Register 'R6/R7'
^"SOURCE LINE # 2 ' ' ‘
; . unsigned int argument)
respectively. If the parameters are int values, they are passed using register pairs (R7, R6), (R5, R4) and
(R3, R2). If the number of arguments is greater than three, the first three arguments are passed through
registers and rest is passed through fixed memory locations. Refer to C51 documentation for more
details. Return values are usually passed through general purpose registers. R7 is used for returning
char value and register pair (R7, R6) is used for returning int value. The ‘C’ subroutine can be in¬
voked from the assembly program using the subroutine call Assembly instruction (Again cross compiler
dependent).
E.g. LCALL _Cfunction
Where Cfunction is a function written in ‘C’. The prefix __ informs the cross compiler that the parameters
to the function are passed through registers. If the function is invoked without the _ prefix., it is under¬
stood that the parameters are passed through fixed memory locations.
9.2.3.3 Inline Assembly Inline assembly is another technique for inserting target processor/con¬
troller specific Assembly instructions at any location of a source code written in high level language
‘C\ This avoids the delay in calling an assembly routine front a ‘C’ code (If the Assembly instructions
to be inserted are put in a subroutine as mentioned in the section mixing assembly with ‘C’). Special
keywords are used to indicate that the start and end of Assembly instructions. The keywords are cross-
compiler specific. C51 uses the keywords #pragma asm and Opragma endasm to indicate a block of
code written in assembly.
E.g. ^pragma asm
■MOV A, #1311 '■ -A/! - rvv’
Important Note:
The examples used for illustration throughout the section Mixing Assembly & High Level Language
is Keil C51 cross compiler specific. The operation is cross compiler dependent and it varies from cross
compiler to cross compiler. The intention of the author is just to give an overall idea about the mixing
of Assembly code and High level language ‘C’in writing embedded programs. Readers are advised to
go through the documentation of the cross compiler they are using for understanding the procedure
adoptedfor the cross compiler in use.
Whenever the conventional ‘C’ Language and its extensions are used for programming embedded sys¬
tems, it is referred as ‘Embedded C programming. Programming in ‘Embedded C’ is quite different
from conventional Desktop application development using ‘C’ language for a particular OS platform.
Desktop computers contain working memory in the range of Megabytes (Nowadays Giga bytes) and
storage memory in the range of Giga bytes. For a desktop application developer, the resources available
are surplus in quantity and s/he can be very lavish in the usage of RAM and ROM and no restrictions are
imposed at all. This is not the case for embedded application developers. Almost all embedded systems
are limited in both storage and working memory resources. Embedded application developers should
be aware of this fact and should develop applications in the best possible way which optimises the code
memory and working memory usage as well as performance. In other words, the hands of an embedded
application developer are always tied up in the memory usage context©.
Embedded Firmware Design and Development
This section is intended only for giving readers a basic idea on how ‘C’ Language is used in embedded
firmware development.
Let us brash up whatever we learned in conventional ‘C’ programming. Remember we will only go
through the peripheral aspects and will not go in deep.
9.3.3.1 Keywords and Identifiers Keywords are the reserved names used by the ‘C’ language. All
keywords have a fixed meaning in the ‘C’ language context and they are not allowed for programmers
for naming their own variables or functions. ANSI ‘C* supports 32 keywords and they are listed below.
All ‘C’ supported keywords should be written in ‘lowercase’ letters.
Identifiers are user defined names and labels. Identifiers can contain letters of English alphabet (both
upper and lower case) and numbers. The starting character of an identifier should be a letter. The only
special character allowed in identifier is underscore (_).
9.3.3.2 Data Types Data type represents the type of data held by a variable. The various datatypes
supported, their storage space (bits) and storage capacity for ‘C’ language are tabulated below.
Embedded Firmware Design and Development
The data type size and range given above is for an ordinary ‘C’ compiler for 32 bit platform. It should
be noted that the storage size may vary for data type depending on the cross-compiler in use for embed¬
ded applications.
Since memory is a big constraint in embedded applications, select the optimum data type for a vari¬
able. For example if the variable is expected to be within the range 0 to 255, declare the same as an
‘unsigned char' or ‘unsigned short inf data type instead of declaring it as ‘inf or ‘unsigned inf. This
will definitely save considerable amount of memory.
9.3.3.3 Storage Class Keywords related to storage class provide information on the scope (vis¬
ibility or accessibility) and life time (existence) of a variable. ‘C’ supports four types of storage classes
and they are listed below.
- Apart from these four storage classes, ‘C’ literally supports storage class 'global'. An ‘auto or static’
variable declared in the public space of a file (declared before the implementation of all functions in¬
cluding main in a file) is accessible to all functions within that file. There is no explicit storage class for
‘global'. The way of declaration of a variable determines whether it is global or not.
9.3.3.4 Arithmetic Operations The list of arithmetic operators supported by ‘C’ are listed below
9.3.3.5 Logical Operations Logical operations are usually performed for decision making and
program control transfer. The list of logical operations supported by ‘C’ are listed below
! Logical NOT Performs logical Negation. Operand is complemented (logic 0 becomes 1 and vice
versa)
Introduction to Embedded Systems
9.3.3.6 Relational Operations Relational operations are normally performed for decision making
and program control transfer on the basis of comparison. Relational operations supported by ‘C’ are
listed below.
Checks equality Checks whether the'operand on.the leftside of ‘—’operator is equal to.the operand
on the right side. If yes return logic one, else return logic zero -
. != Checks Checks whether the operand on the left side of'!=’operator is not equal
: non-equality operand on the right side. If yes return logic one, else return logic zero '
9.3.3.7 Branching Instructions Branching instructions change the program execution flow condi¬
tionally or unconditionally. Conditional branching depends on certain conditions and if the conditions
are met, the program execution is diverted accordingly. Unconditional branching instructions divert
program execution unconditionally.
Commonly used conditional branching instructions are listed below
else statement!;
condition “xpressiomand. case values should be integers, vain
match found, executes the statement following the
T-T rtA VMot/'I'l IyuiVi/1! iXvartTitrvn .flia /Irtf-Vitlt /-\nil,! ■
//Conditional operator
// ?expl : exp2 JJsedfbr assigning a value depending on the (expression), (expression) is calculated
first and if it is greater than 0. evaluates exp 1 and returns it as a result of operation
(expression) ?expl: exp2 else evaluate exp2 and returns it as result. The return value is assigned to some
variable.
»H 1
inst&ctidh. wzm
goto is generally used to come out of deeply nested, loops in abnormal conditions
or errors.
9.3.3.8 Looping Instructions Looping instructions are used for executing a particular block of
code repeatedly till a condition is met or wait till an event is fired. Embedded programming often uses
the looping instructions for checking the status of certain I/'o ports, registers, etc. and also for pro¬
ducing delays. Certain devices allow write/read operations to and from some registers of the device
only when the device is ready and the device ready is normally indicated by a status register or by
setting/clearing certain bits of status registers. Hence the program should keep on reading the status
register till the device ready indication comes. The reading operation forms a loop. The looping instruc¬
tions supported by ‘C’ are listed below.
Introduction to Embedded Systems
// do while loop
The ‘body of the loop’ is executed at least once. At the end ol each execution of the
do . . \ ‘body of the loop', the While condition (expression) is evaluated and if
i loop is repeated, else loop is terminated,
body of do loop
while (expression);
//for loop
Entry controlled loop. Enters and executes the ‘body of loop’ only if the test for
for (initialisation; test for condition is true, for loop contains a loop control variable which may be initialised .
condition; update variable) within the initialisation part of the loop. Multiple variables can be initialised with
{ V operator.
body of for loop
//exiting from loop Loops can be exited in two ways. First one is normal exit where loop is exited w
the expression/test for condition becomes false/Second one isfiffc;
break; and goto statements are Used forforced exit. ;y
break exits from the innermost loop in a deeply nested loop, wher
goto label the program flow to a defined label. ,
//skipping portion of a loop Certain situation demands the skipping of a portion of a loop for some conditions.
The ‘continue’ statement used inside a loop will skip the rest of the portion following
while (expression) it and will transfer the program control to the beginning of the loop.
/'/for loop with skipping
if (condition);
continue;
while (expression);
i
Embedded Firmware Design and Development
//###########################################################
//using do while Loop
//###########################################################
do
{
} while (*status_reg!=0x01); Loop till statusj-eg = 0x01
//###########################################################
//using for loop
9.3.3.9 Arrays and Pointers Array is a collection of related elements (data types). Arrays are usu-
ally declared with data type of array, name of the array and the number of related elements to be placed
in the array. For example the following array declaration
char ,'arr [5] ;
Introduction to Embedded Systems
declares a character array with name ‘arr’ and reserves space for 5 character elements in the memory as
in Fig. 9.31.
Address of the memory location where the array elements are stored ^
f Fig. 9.dj Array representation in memory
The elements of an array are accessed by using the array index or subscript. The index of the first
element is ‘O’. For the above example the first element is accessed by arr[0], second element by arr[l],
and so on. In the above example, the array starts at mem- r~ ; v—-
ory location 0x8000 (arbitrary value taken for illustra- :;
tion) and the address of the first element is 0x8000. The &arr[°] • ^
‘ address of operator (&) returns the address of the mem- -:—
ory location where the variable is stored. Hence &arr[0]
will return 0x8000 and &arr[l] will return 0x8001, etc. arrfO]^'*'
The name of the array itself with no index (subscript)
always returns the address of the first element. If we ex- . .
j, 9.4) Array element address and content
amine the first element arr[0] of the above array, we can (K%.9:4J addt6ss “d c“'“>
relationship
see that the variable arr[0] is allocated a memory loca-
tion 0x8000 and the contents of that memory location
holds the value for arr[0] (Fig. 9.4).
Anays can be initialised by two methods. The first method is initialising the entire array at the time
of array declaration itself. Second method is selective initialisation where any member can be initialised
or altered with a value.
//Initialization of array at the time of declaration
//Selective initialization
arr[0] = 5;
arr[l], = 10;
arr. [2] = 20;
arr[3] = 3;
arr[4] = 2;
m' Hi ‘
void test,{char *p);
. ' . . • ; I 1
//Function declaration_
char arr [5] = {5, 10, 20, :3,--2); //Array data type; char
void main , (
This code snippet will print ‘1’ as the output, though the user expects 5 (size of arr) as output. The.
output is equivalent to sizeof (char), size of the data type of the array.
3. Use the syntax ‘extern array type array name [ f to access an array which is declared outside the
current file. For example ‘extern char arr[ f for accessing the array larr[ f declared in another
file
4. Arrays.are not equivalent to pointers. But the expression ‘array name’ is equivalent to a pointer, of
type specified by the array, to the first element of an array, e.g. ‘arr’ illustrated for sizeof () opera¬
tor is equivalent to a character pointer pointing to the first element of array ‘arr'.
5. Array subscripting is commutative in ‘C’ language and ‘arr[k]' is same as ‘*((arr)+(k)f where
‘arr[k]' is the content of klh element of array ‘arr' and ‘(arr)' is the starting address of the array arr
and k is the index of the array or offset address for the ‘k,h' element from the base address of array.
‘ *((arr) + (k))' is the content of array for the index k.
Pointers Pointer is a flexible at the same time most dangerous feature, capable of creating potential
damages leading to firmware crash, if not used properly. Pointer is a memory pointing based technique
for variable access and modification. Pointers aref very helpful in
1. Accessing and modifying variables
2. Increasing speed of execution
3. Accessing contents within a block of memory
4. Passing variables to functions by eliminating the use of a local copy of variables
5. Dynamic memory allocation (Will be discussed later)
To understand the pointer concept, let us have a look at the data memory organisation of a proces¬
sor. For a processor/controller with 128 bytes user programmable internal RAM (e.g. AT89C51), the
memory is organised as
Introduction to Embedded Systems
If we declare a character variable, say char input, the compiler assigns a memory location to the
variable anywhere within the internal memory 0x00 to 0x7F. The allocation is left to compiler’s choice
unless specified explicitly (Let it be 0x45 for our example). If we assign a value (say 10) to the variable
input (input=10), the memory cell representing the variable input is loaded with 10.
The contents of memory location 0x45 (representing the variable input) can be accessed and modi¬
fied by using a pointer of type same as the variable (char for the variable input in the example). It is
accomplished by the following method.
char 'input; '//■Declaring -input as character variable
char *p; //Declaring a character pointer p (*...denotes--p is-a pointer)
p = £ input //Assigns' the address of input as content to p
The compiler assigns a memory to the character pointer variable lp\ Let it be 0x00 (Arbitrary value
chosen for illustration) and the memory location holds the memory address of variable input (0x45)
Embedded Firmware Design and Development
as content. In ‘C’ the address assignment to pointer is done using the address of operator or it can
be done using explicitly by giving a specific address. The pointer feature in ‘C’ is same as the indirect
addressing technique used in 8051 Assembly instructions. The code snippet
MOV RO, #4.5H • ■
MOV A,@R0 ; RO & R1 are the-indirect addressing registers.
is an example for 8bit memory pointer usage in 8051 Assembly and the code snippet
is an example for 16bit memory pointer operation. The general form of declaring a pointer in ‘C’ is
ftdata type ^pointer; //'data -type' is - the,.standard' data type date
* A'C //int, char, float etc... supported- by 'C' language. ~;5-
The * (asterisk) symbol informs the compiler that the variable pointer is a pointer variable. Like any
other variables, pointers can also be initialised in its declaration itself.
char x, y;^ .
Pointer Arithmetic and Relational Operations ‘C’language supports the following Arithmetic
and relational operations on pointers.
1. Addition of integer with pointer, e.g. ptr+2 (It should be noted that the pointer is advanced forward
by the storage length supported by the compiler for the data type of the pointer multiplied by the
integer. For example for integer pointer where storage size of int = 4, the above addition advances
the pointer by 4 x 2 = 8)
2. Subtraction of integer from pointer, e.g. ptr-2 (Above mle is applicable)
3. Incremental operation of pointer, e.g. ++ptr and ptr++ (Depending on the type of pointer, the ++
increment context varies). For a character pointer ++ operator increments the pointer by 1 and for
an integer pointer the pointer is incremented by the storage size of the integer supported by the
compiler (e.g. pointer ++ results in pointer + 4 if the size for integer supported by compiler is 4)
4. Decrement operation of pointer, e.g. —ptr and ptr— (Context mle for decrement operation is
same as that of incremental operation)
5. Subtraction of pointers, e.g. ptrl- ptr2
6. Comparison of two pointers using relational operators, e'.g. ptrl > ptr2, ptrl < ptr2, ptrl = = ptr2,
ptrl! = ptr2 etc (Comparison of pointers of same type only will give meaningful results)
The statement if (ptr) is converted to if (ptr! =0) by the (cross) compiler. Alternatively you can
directly use the statement if (ptr! =0) in your program to check the ‘NULL’ pointer.
9. Null pointer is a ‘C’ language concept and whose internal value does not matter to us. ‘NULL'
always guarantee a ‘0’ to you but Null pointer need not be.
Pointers and Arrays—'Are they related? Arrays are not equivalent to pointers and vice versa. But
the expression array ‘name [ ]’ is equivalent to a pointer, of type specified by the array, to the first ele¬
ment of an array, e.g. for the character array ‘char arr[5J\ ‘arr[]' is equivalent to a character pointer
pointing to the first element of array ‘arr ’ (This feature is referred to as ‘equivalence of pointers and
arrays'). You can achieve the array features like accessing and modifying members of an array using a
pointer and pointer increment/decrement operators. Arrays and pointer declarations are interchangeable
when they are used as parameters to functions. Point 2 discussed under the section ‘A few important
points on Arrays’ for sizeofQ operator usage explains this feature also.
9.3.3.10 Characters and Strings Character is a one byte data type and it can hold values ranging
from 0 to 255 (unsigned character) or -128 to +127 (signed character). The term character literally refers
to the alpha numeric characters (English alphabet A to Z (both small letters and capital letters) and num¬
ber representation from ‘0’ to ‘9’) and special characters like *, ?,!, etc. An integer value ranging from
0 to 255 stored in a memory location can be viewed in different ways. For example, the hexadecimal
number 0x30 when represented as a character will give the character ‘0’ and if it is viewed as a decimal
number it will give 48. String is an array of characters. A group of characters defined within a double
quote represents a constant string.
‘FT is an example for a character, whereas “Hello" is an example for a string. String always termi¬
nates with a ‘\0’ character. The ‘\0’ character indicates the string termination. Whenever you declare a
string using a character array, allocate space for the null terminator ‘\0’;in the array length.
Embedded Firmware Design and Development
.or char name [6] = {'S', 'H', 'I', 'B', 'U', '\0'•
String operations are very important in embedded product applications. Many of the embedded prod¬
ucts contain visual indicators in the form of alpha numeric displays and are used for displaying text.
Though the conventional alpha numeric displays are giving way to graphic displays, they are deployed
widely in low cost embedded products. The various operations performed on character strings are ex¬
plained below.
: Input & Output operations Conventional ‘C’ programs running on Desktop machines make use
of the standard string inputting (,scanfQ) and string outputting (printfQ) functions from the platform
specific I/O library. Standard keyboard and monitor are used as the input and output media for desktop
application. This is not the case for embedded systems. Embedded systems are compact and they need,
not contain a standard keyboard or monitor screen for I/O functions. Instead they incorporate applica¬
tion specific keyboard and display units (Alpha numeric/graphic) as user interfaces. The standard string
input instruction supported by ‘C’ language is scanf() and a code snippet illustrating its usage is given
below.
A standard ANSI C compiler converts this code according to the platform supported I/O library files
and waits for inputting a string from the keyboard. The scanf function terminates when a white space
(blank, tab, carriage return, etc) is encountered in the input string. Implementation of the scanfQ func¬
tion is (cross) compiler dependent. For example, for 8051 microcontroller all I/O operations are sup¬
posed to be executed by the serial interface and the C51 cross compiler implements therscanfQ function
in such a way to expect and receive a character/string (according to the scanf usage context (character if
first parameter to scanfQ is “%c” and string if first parameter is “%s”)) from the serial port.
printfQ is the standard string output instruction supported by ‘C’ language. A code snippet illustrating
the usage of printf() is given below.
.char, name [ ] = "SHIBU";
; pr.intf ("%'s", name) ;
Similar to scanfQ function, the standard ANSI C compiler converts this code according to the plat¬
form supported I/O library files and displays the string in a console window displayed on the monitor.
Implementation of printfQ function is also (cross) compiler specific. For 8051 microcontroller the C51
cross compiler implements the printfQ function in such a way to send a character/string (according to
the printf usage context (character if first parameter to printfQ is “%c” and string if first parameter is
“%s”)) to the serial port.
String Concatenation Concatenation literally means ‘joining together'. String concatenation refers
to the joining of two or more strings together to form a single string. ‘C’ supports built in functions for
string operations. To make use of the built in string operation functions, include the header file ‘string.
h' to your \c’ file. ‘strcatQ’ is the function used for concatenating two strings. The syntax of ‘strcatQ’
is illustrated below.
, strcat (strl, str2.) ; //'strl\ and 'str-2' are the strings to
IpC. r. ' ; -- • //be coiieatenated.
Introduction to Embedded Systems
On executing the above instruction the null character (‘\0’) from the end of ‘strl’ is removed and
lstr2’ is appended to ‘strl'. The string ‘stiN remains unchanged. As a precautionary measure, ensure
that strl (first parameter of A treat ()’ function) is declared with enough size to hold the concatenated
string. ‘strcatQ’ can also be used for appending a constant string to a string variable.
E.g. streat(strl, "Hello!");
Note:
1., Addition of two strings, say strl; str2 is not allowed .
2. Addition of a string variable, say strl, with a constant string, say Hello” is not allowed
String Comparison Comparison of strings can be performed by using the ‘strcmpQ' function sup¬
ported by the string operation library file. Syntax of strcmpQ’ is illustrated below.
stremp (strl, str2); //strl and str2 are two character strings
If the two strings which are passed as arguments to ‘strcmpQ ’ function are equal, the return value of
‘strcmpQ’ will be zero. If the two strings are not equal and if the ASCII value of the first non-match¬
ing character in the string strl is greater than that of the corresponding character for the second string
str2, the return value will be greater than zero. If the ASCII value of first non-matching character in
the string strl is less than that of the corresponding character for the second string str2, the return value
will be less than zero. ‘strcmpQ' function is used for comparing two string variables, string variable and
constant string or two constant strings.
E.g. char strl [ ] = "Hello world";
char str.2 [ ] = "Hello World!" ;
int n; _ ' .
n= stremp(strl, str2);
Executing this code snippet assigns a value which is greater than zero to n. (since ASCII value of V
is greater than that of ‘ IT). n= strcmp(str2, strl); will assign a value which is less than zero to n. (since
ASCII value of ‘ W is less than that of ‘w’).
The function stricmpQ is another version of strcmpQ. The difference between the two is, stricmpQ
is not case sensitive. There won’t be any differentiation between upper and lowercase letters when
stricmpQ is used for comparison. If stricmpQ is used for comparing the two strings strl and str2 in the
above example, the return value of the function striemp (strl, str2) will be zero.
Note:
1. Comparison of two string variables using e= =’ operator is invalid, e.g. if (strl =- str2) is
invalid
2. Comparison of a string variable and a string constant using ‘= =’ operator is also invalid, e.g. if
(strl == “Hello ”) is invalid
Finding String length String length refers to the number of characters except the null terminator
character ‘\0’ present in a string. Stringdength can be obtained by using a counter combined with a
search operation for the ‘\0’ character. ‘C’ supplies a ready to use string function strlenQ for determining
the length of a string. Its syntax is explained below.
strlen (strl); //where strl is a character string
\
Embedded Firmware Design and Development
Executing this code snippet assigns the numeric value 12 to integer variable V.
Copying Strings strcpy() function is the ‘C’ supported string operation function for copying a string
to another string. Syntax of strcpy() function is
lllSr. . strcpy (strl, str2); //strl, str2 ar.e character strings . .. ■
This function assigns the contents of str2 to strl. The original content of strl is overwritten by the
contents of str2. str2 remains unchanged. The size of the character array which is passed as the first
argument the strcpy() function should be large enough to hold the copied string. strcpyQ function can
also be used to assign constant string to string variables.
3. Whenever a character array is declared to hold a string, allocate size for the null terminator
character ‘\0’ also in the character array
9.3.3.11 Functions Functions are the basic building blocks of modular programs. A function is a
self-contained and re-usable code snippet intended to perform a particular task. ‘Embedded C supports
two different types of functions namely, library functions and user defined functions.
Library functions are the built in functions which is either part of the standard ‘Embedded C’ library
or user created library files. ‘C’ provides extensive built in library file support and the library files are
categorised into various types like I/O library functions, string operation library functions, memory
allocation library functions etc. printfQ, scanf (), etc. are examples of I/O library functions. strcpy(),
strcmp(), etc. are examples for string operations library functions. mallocQ, callocQ etc are examples
of memory allocation library functions supported by ‘C’. All library functions supported by a particular
library is implemented and exported in the same. A corresponding header (‘.A’) file for the library file
provides information about the various functions available to user in a library file. Users should include
the header file corresponding to a particular library file for calling the functions from that library in the
Introduction to Embedded Systems
‘C’ source file. For example, if a programmer wants to use the standard I/O library function printfQ in
the source file, the header file corresponding to the I/O library file (“stdio.h” meant for standard i/o)
should be included in the ‘c’ source code using the #include preprocessor directive.
string.h” is the header file corresponding to the built in library functions for string operations and
“
“malloc.h” is the header file for memory allocation library' functions. Readers are requested to get info
on header files for other required libraries from the standard ANSI library. As mentioned earlier, the
standard ‘C’ library function implementation may be tailored-by cross-compilers, keeping the function
name and parameter list unchanged depending on Embedded ‘C’ application requirements. printfQ
function implemented by C51 cross compiler for 8051 microcontroller is a typical example. The library
functions are standardised functions and they have a unique style of naming convention and arguments.
Users should strictly follow-it while using library functions.
User defined functions are programmer created functions for various reasons like modularity, easy
understanding of code, code reusability, etc. The generic syntax for a function definition (implementa¬
tion) is illustrated below.
Return- type f ufac^lpn ;-nam,e';(argumen t^rd:st:r)c
' ' { ^
vo-ict main ^0
abc () ;
If you are writing the user defined functions after the entry' function main() and calling the same
inside main, you should specify the function prototype (function declaration) of each user defined func¬
tions before the function mainQ. Otherwise the compiler assumes that the user defined function is an ex¬
tern function returning integer value and if you define the function after main() without using a function
declaration/prototype, compiler will generate error complaining the user defined function is redefining
(Compiler already assumed the function which is used inside main() without a function prototype as
an extern function returning an integer value). The function declaration informs the compiler about the
format and existence of a function prior to its use. Implicit declaration of functions is not allowed: every
function must be explicitly declared before it is called. The general form of function declaration is
f. 'Linkage (Type. Return, type function name (arguments); • ’ ; ‘ T
The ‘Linkage Type’ specifies the linkage for the function. It can be either ‘external’ or ‘internal’. The
‘static’ keyword for the ‘Linkage Type’ specifies the linkage of the function as internal whereas the ‘ex¬
tern’ ‘Linkage Type' specifies ‘external’ linkage for the function. It is not mandatory to specify the name
of the argument along with its type in the argument list of the function declaration. The declarations can
simply specify the types of parameters in the argument list. This is called function prototyping. A func¬
tion prototype consists of the function return type, the name of the function, and the parameter list. The
usage is illustrated below.
static int add(int, int);
Let us have a look at the examples for the different scenarios explained above on user defined func¬
tions.
;'/^##########################################################;fr########
.•(//Example for Source code with function prototype for user defined-
7s//‘functions; Source file test.c’
Introduction to Embedded Systems
♦include <stdio.h>
//function prototype for user defined function test
void test (void) ;
void main ( )
Compiler Output:
Compiling...
test.c
test.c(5): warning c4013: ‘test’ undefined; assuming extern returning int
test.c(9): error C2371: ‘test’: redefinition; different basic types Error executing cl.exe.
test.exe-1 error(s), 1 waming(s)
There is another convenient method for declaring variables and functions involved in a source file.
This technique is a header (‘./?’) file and source (\c) file based approach. In this approach, correspond¬
ing to each ‘c’ source file there will be a header file with same name (not necessarily) as that of the ‘c’
source file. All functions and global/extern variables for the source file are declared in the header file
instead of declaring the same in the corresponding source file. Include the header file where the func¬
tions are declared to the source file using the “ttinclude” pre-processor directive. Functions declared in
a file can be either global (extern access) in scope or static in scope depending on the declaration of the
Embedded Firmware Design and Development
function. By default all functions are global in scope (accessible from outside the file where the func¬
tion is declared). If you want to limit the scope (accessibility) of the function within the file where it is
declared, use the keyword ‘static’ before the return type in the function declaration.
9.3.3.12 Function Pointers A function pointer is a pointer variable pointing to a function. When
an application is compiled, the functions which are part of the application are also get converted into
corresponding processor/compiler specific codes. When the application is loaded in primary memory
for execution, the code corresponding to the function is also loaded into the memory and it resides at a
memory address provided by the application loader. The function name-maps to an address where the
first instruction (machine code) of the function is present. A function pointer points to this address.
The general form of declaration of a function pointer is given below.
; , return type (xpoi nter_ name) (argument .listj ' TV ' : ‘ v ■
where, 1 return _type' represents the return type of the function, ‘pointer jiame’ represents the name of
the pointer and ‘argument list’ represents the data type of the arguments of the function. If the function
contains multiple arguments, the data types are separated using V- Typical declarations of function
pointer are given below.
//Function pointer-to. a function returning int and takes no parameter Vy
int (*fptr)(); ' ,
'//Function pointer to a function returning int and takes 1 parameter
int (*fptr) (int) ' ■
The parentheses () around the function pointer variable differentiates it as a function pointer variable.
If no parentheses are used, the declaration will look like
i.nt *fptr.(); > t/:'yv, •. -yn —
The cross compiler interprets it as a function declaration for function with name ‘fptr' whose argu¬
ment list is void and return value is a pointer to an integer. Now we have declared a function pointer, the
next step is ‘How to assign a function to a function pointer?’.
Let us assume that there is a function with signature
int functionl(void);
We can assign the address of the function 'functionl () ’ to our function pointer variable fptr' with the
following assignment statement:
fptr = Sfunctionl;
The operator gets the address of function functionl’ and it is assigned to the pointer variable
fptr’ with the assignment operator ‘=\ The address of operator is optional when the name of the
function is used. Hence the assignment operation can be re-written as:
fptr = functionl;
Once the address of the right sort of function is assigned to the function pointer, the function can be
invoked by any one of the following methods.
Introduction to Embedded Systems
(*fptr)();
fptr();
Function pointers can also be declared using typedef. Declaration and usage of a function pointer
with typedef is illustrated below.
The following sample code illustrates the declaration, definition and usage of function pointer.
'
void
1 <■'•
7’. /is
....
(*fptr) (int); _ ' ' 7” ’
. '
W-sA»,.v . V y
printf("Square of %d = %d\ri", x, x * x) ;
} '
Function pointer is a helpful feature in late binding. Based on the situational need in the application
you can invoke the required function by binding the function with the right sort of function pointer (The
function signature and function pointer signature should be matching). This reduces the^ usage of‘if’
and ‘switch - case’ statements with function names. Function pointers are extremely useful for handling
situations which demand passing of a function as argument to another function. Function pointers are
often used within functions where the function should be able to work with a number of functions whose
names are not known until the program is running. A typical example for this is callback functions,
which requires the information about the function which needs to be called. The following sample piece
of code illustrates the usage of function pointer as parameter to a function.
♦include <stdio.h>
//###################################################################
//Function prototype declaration
void square(int x);
void cube(int x) ;
void power(void (*fptr) (int), int x);
Embedded Firmware Design and Development
i void main()
| {
//Declare a function pointer
void (*fptr) (int);
//Define the function pointer to function square
* fp'tr = square;
/ //Invoke the function 'square7 through function pointer
power (fptr,2);
f, ' //Define the function pointer to .function cube .
|r‘ fptr = cube; ' V" r,, ' "yy,
b //Invoke the function 'cube' through :function pointer . A4r
power (fptr,/.); - ' " , A- -* A-A' 'hi V , .it
}
//##-##########################################.#######################
//Function for printing the square of a number
void square(int x)
{ _ -
//###################################################################
//Function for printing the third power (cube) of a number
void cube(int x)
{
printf("Cube of %d = %d\n", x, x*x*x);
}
Arra/s of Function Pointers An aray of function pointers holds pointers to functions with same
type of signature. This offers the flexibility to select a function using an index. Arrays of function point¬
ers can be defined using either direct function pointers or the ‘typedef qualifier.
The following piece of code illustrates the usage of function pointer arrays:
void main()
1
//Declare a function pointer array of size 2 and initialize
void (*fptr[2]) (int)= {NULL};
//Define the function pointer 0 to function square
fptr[0] = square;
//Invoke the function square
fptr[0] (2);
* //Define the function pointer 1 to function cube
fptr[l] = cube;
//Invoke the function cube through function pointer
fptr [1] (2) ;
}
//###################################################################
//Function for printing the square of a number
void square(int x)
|€#####«#######*#####.###.################»#*##;####«##
^/Function for’ pointing the' third power (cube) of a number x
5_ A v" ~ 7* w= Tw 1 A1 ■- T ' f ^ 5’', O-s \r j.- .. ■» -*-> - = *•* "■*»» Ai-v>:Sr* ~•' ■-
AvoicL-cube(int x)
- ■ x
printf ('v0ube of'
9.3.3.13 Structures and Unions ‘structure’is a variable holding a collection of data types (int,
float, char, long etc). The data types can be either unique or distinct. The tag ‘struct’ is used for declaring
a structure. The general form of a structure declaration is given below.
■ t' ■’ sf-fuct*1 r:t%-thhiStbhan!e@a;yffh)ft!p;:§fpliS
f '{ ■ h ... .
-
/, /tiic)J ldicirion
po t ,* ■ %. Chv. Vr * tT '",1^ T T - -• - S' , ~ x '-f " A,( - Eypffr-J* 'tx&scp- A ^
//variable 2 to declaration
ft V t; .; ...... . .//„.. .. i .V;;. .; ; ....... ||
struct jiame- is the name of the structure and the choice of the name is left to the programmer.
Let us examine the details kept in an employee record for an employee in the employee database of
an organisation as example. A typical employee record contains information like ‘Employee Name',
‘Employee Code’, and ‘Date of Birth’. This information can be represented in ‘C’ using a structure as
given below.
Instruct employee t *•
h{..; .... . .. .
char emp_iiame [20]; // Allowed maximum length for name
int emp__code; - ;x ;x xLx WiiM,
char DOB [10];// DD-MM-YYYY Format (10*'charaqterfpidg;
Hence in ‘C’, the employee record is represented by two string variables (character arrays) and an
integer variable. Since these three variables are relevant to each employee, they are grouped together
in the form of a structure. Literally structure can be viewed as a collection of related data. The above
structure declaration does not allocate memory for the variables declared inside the structure. It’s a mere
representation of the data inside a structure. To allocate memory for the structure we need to create a ’
variable of the structure. The structure variable creation is illustrated below.
.struct employee emp.1;
Keyword ‘struct’ informs the compiler that the variable ‘empU is a structure of type ‘employee’. The
name of the structure is referred as ‘structure tag’ and the variables declared inside the structure are
called ‘structure elements'. The definition of the structure variable only allocates the memory for the
different elements and will not initialise the members. The members need to be initialised explicitly.
Members of the structure variable can be initialised altogether at the time of declaration of the structure
variable itself or can be initialised (modified) independently using the V operator (member operator).
Struct employee empi- {"SKIBU'K V", 42170, "11-11-1977'"}; - ■
Introduction to Embedded Systems
This statement declares a structure variable with name ‘empl ’ of type employee and each elements
of the structure is initialised as
emp_name = "SHIBU K V"
emp _ccde = 42170
DOB= "11-11-1977"
It should be noted that the variables should be initialised in the order as per their declaration in the
structure variable. The selective method of initialisation/modification is used for initialising /modifying
each element independently.
E.g; -struct, employee ern.pl ; .' . V'
empl .emp_c.ode = 4217.0';;.;.-- "'vf' Tf r 'fy T T
All members of a structure, except character string variables can be assigned values using V Opera¬
tor and assignment (“=’) operator (character strings are character arrays and character arrays cannot be
initialised altogether using the assignment operator). Character string variables can be assigned using
string copy function (strcpy).
strcpy (enpl. emp _narr.e, "SHIBU F. V") ;- : . /
strcpy (empl . DOB, "It -1 1-1 977") ; ■■ •; : fy ■■ ■'"t
Declaration of a structure variable requires the keyword ‘structure’ as the first word and it may sound
awkward from a programmer point of view. This can be eliminated by using ‘ typedef in defining the
structure. The above ‘employee’ structure example can be re-written using typedef as
typedef struct •' ; ^ '• *■ ■ -* ... \
char enp_ name [20] ;// .Allowed maximum length fifor name = 20 :
int emp code; - v , ■ ' .
char DOB [10]; // DD-MM-YYYY Format (10 character)
} employee; " \• ' • Nx
employee empl; //No need tq add struct before employee
This approach eliminates the need for adding the keyword ‘struct’ each time a structure variable is
.‘declared.
Structure operations The following table lists the various operations supported by structures
Note: ■ • -
1. The assignment and comparison operation is valid only if both structure variables are of the sametypefyy
2. Some compilers may not support the direct assignment and comparison operation. In such situation the
1 ■ individual members of structure should be assigned or compared separately..- .• :* T ‘ '"Af-
Structure pointers Structure pointers are pointers to structures. It is easy to modify the memory held
by structure variables if pointers are used. Functions with structure variable as parameter-is a very good
example for it. The structure variable can be passed to the function by two methods; either as a copy of
the structure variable (pass by value) or as a pointer to a structure variable (pass by reference/address).
Pass by value method is slower in operation compared to pass by pointers since the execution time for
copying the structure parameter also needs to be accounted. Pass by pointers is also helpful if the struc¬
ture which is given as input parameter to a function need to be modified and returned on execution of
the calling function. Structure pointers can be declared by prefixing the structure variable name with
The following structure pointer declaration illustrates the same.
Instruct -employee. *empl; .//structure defined using the structure tag
i employee *empl,\ //structure defined with typedefi structure
For structure variables declared as pointers to a structure, the member selection of the structure is per¬
formed using the ‘ -> ’ operator.
- E.g. struct employee. *empl, emp2;
empl = &emp2; //Obtain a pointer
empl-^ emp_code = 4217-0.;
strepy (empl->DOB, "11-11-1977") ;
Structure pointers at absolute address Most of the time structures are used in Embedded C ap¬
plications for representing the memory locations or registers of chip whose memory address is fixed at
the time of hardware designing. Typical example is a Real Time Clock (RTC) which is memory mapped
at a specific address and the memory address of the registers of the RTC is also fixed. If we use a struc¬
ture to define the different registers of the RTC, the structure should be placed at an absolute address
corresponding to the memory mapped address of the first register given as the member variable of the
structure. Consider the structure describing RTC registers.
typedef struct
{
:■ //RTC Control register (8bit) memory mapped at 0x4000
unsigned char control;
..//RTC Seconds register (8bi.t) memory mapped at 0x4001
unsigned char seconds; ' - -
Introduction to Embedded Systems
liillllllllillH!lllllllllillliSl.';fR ff 1/1 t
ipl-Mli!
/ /J
■_1 L_ 1_[ i- 3--?:^ p- ^ r ^ ’
111
miuigned chft R. yh5l r - -IO ¥ ■ '-'-
'
: STC;: V -f id -- M ■ ? i ■- jy if
To read and write to these hardware registers using structure member variable manipulation, we need
to place the RTC structure variable at the absolute address 0x4000, which is the address of the RTC
hardware register, represented by the first member of structure RTC.
The implementation of structures using pointers to absolute memory location is cross-compiler de¬
pendent. Desktop applications may not give permission to explicitly place structures or pointers at
an absolute address since we are not sure about the address allocated to general purpose RAM by the
linker. Any attempt to explicitly allocate a structure at an absolute address may result in access violation
exception. More over there is no need for a general purpose application to explicitly place structure or
pointers at an absolute address, whereas embedded systems most often requires it if different hardware
registers are memory mapped to the processor/controller memory map. The C51 cross compiler for
8051 microcontroller supports a specific Embedded C keyword ‘xdata’ for external memory access and
the structure absolute memory placement can be done using the following method.
; RTC xciata *rtcgregisters = (wold xdata *); 0xd000; '
Structure Arrays In the above employee record example, three important data is associated with each
employee, namely; employee name (emp_name), employee code (emp_code) and Date of Birth (DOB).
This information is held together as a structure for associating the same with an employee. Suppose the
organisation contains 100 employees then we need 100 such structures to hold the data related to the
100 employees. This is achieved by array of structures. For the above employee structure example a
structure array with 100 structure variables can be declared as
struct employee emp [10.0]; .//structure declared using struct keyword
■ . >• or . >/ ..A-..yu yyMrSp' , g ';;g A' jbi" -
employee emp [100]; //structure declared using typedef struct
emp [0] holds the structure variable data for the first employee and emp [99] holds the structure variable
data corresponding to the 100th employee. The variables corresponding to each structure in an array can
be initialised altogether at the time of defining the structure array or can be initialised/modified using the
corresponding array subscript for the structure and the Operator as explained below.
typedef: struct..R R%-.g:gv,;: .... r,R:.A; futUfsA-T : Tu
Structure in Embedded ‘C’ programming Simple stracture variables and array of structures are
widely used in ‘embedded ‘C’ based firmware development. Structures and structure arrays are used for
holding various configuration data, exchanging information between Interrupt Service Routine (ISR)
and application etc. A typical example for the structure usage is for holding the various register configu¬
ration data for setting up a particular baudrate for serial communication.'An array of such configuration
data holding structures can be used for setting different baudrates according to user needs. It is interest¬
ing to note that more than 90% of the embedded C programmers.usex typedef to define the structures
with meaningful names.
Structure padding (Packed structure) Structure variables are always stored in the memory of the
target system with structure member variables in the same order as they are declared in the structure
definition. But it is not necessary that the variables should be placed in continuous physical memory
locations. The choice on this is left to the compiler/cro'ss-compiler. For multi byte processors (proces¬
sors with word length greater than 1 byte (8 bits)), if the structure elements are arranged in memory in
such a way that they can be accessed with lesser number of memory fetches, it definitely speeds up the
operation. The process of arranging the structure elements in memory in a way facilitating increased
execution speed is called structure padding. As an example consider the following structure:
llfc;' 'typedef struct ; 1V- ‘ • C V '• '
. { " " ' '•
Let us assume that the storage space for ‘inf is 4 bytes (32 bits) and for ‘char' it is 1 byte (8 bits) for the
target embedded system under consideration. Suppose the target processor for embedded application,
where the above structure is making use is with a 4 byte (32 bit) data bus and the memory is byte acces¬
sible. Every memory fetch operation retrieves four bytes from the memory locations 4x, 4x + 1, 4x + 2
and 4x +3, where x = 0,1,2, etc. for successive memory read operations. Hence a 4 byte (32 bit) variable
can be fully retrieved in a single memory fetch if it is stored at memory locations with starting address
4x (x = 0, 1, 2, etc.). If it is stored at any other memory location, two memory fetches are required to
retrieve the variable and hence the speed of operation is reduced by a factor of 2.
Let us analyse the various possibilities of storing the above structure within the memory.
Method-1 member variables of structure stored in consecutive data memory locations.
In this model the member variables are stored in consecutive data memory locations (Note: the
member variable storage need not start at the address mentioned here, the address given here is only
Introduction to Embedded Systems
Memory
4x + 3 4x + 2 4x+ 1 4x
Address
Byte 3 of
Data
exmpl.y
Memory.
4(x + 1) + 3 4(x+ 1) + 2 4(x +1) + 1 4(x + 1)
Address
for illustration) and if we want to access the character variable exmpl.x of structure exmpl, it can be
achieved with a single memory fetch. But accessing the integer member variable exmpl.y requires two
memory fetches.
Method-2 member variables of structure stored in data memory with padding
In this approach, a structure variable with storage size same as that of the word length (or an integer
multiple of word length) of the processor is always placed at memory locations with starting address as
multiple of the word length so that the variable can be retrieved with a single data memory fetch. The
memory locations coming in between the first variable and the second variable of the structure are filled
with extra bytes by the compiler and these bytes are called ‘padding bytes’ (Fig. 9.9). The structure pad¬
ding technique is solely dependent on the cross-compiler in use. You can turn ON or OFF the padding of
structure by using the cross-compiler supported padding settings. Structure padding is applicable only
for processors with word size more than 8bit (1 byte). It is not applicable to processors/controllers with
8bit bus architecture. —
Memory
Address 4x + 3 4x + 2 4x + 1 4x
;v . ; ■ r ; ,;.SS
Memory
4(x + 1) + 3 4(x + 1) + 2 4(x + 1) + 1 4(x+ 1)
Address
Structure and Bit Selds Bit field is a useful feature supported by structures for bit manipulation
operation and flag settings in embedded applications. Most of the processors/controllers used in embed¬
ded application development provides extensive Boolean (bit) operation support and a similar support
in the firmware environment can directly be used to explore the Boolean processing capability of the
target device.
Embedded Firmware Design and Development
For most of the application development scenarios we use integer and character to hold data items
even though the variable is expected to vary in a range far below the upper limit of the data type used
for holding the variable. A typical example is flags. Flags are used for indicating a ‘TRUE’ or ‘FALSE’
condition. Practically this can be done using a single bit and the two states of the bit (1 and 0) can be
used for representing ‘TRUE’ or ‘FALSE’ condition. Unfortunately CC’ does not support a built in data
type for holding a single bit and it forces us to use the minimum sized data type (quite often char (8bit
data type) and short int) for representing the flag. This will definitely lead to the wastage of memory.
Since memory is a big constraint in embedded applications we cannot tolerate the memory wastage. ‘C’
indirectly supports bit data types through ‘Bit fields' of structures. Structure bit fields can be directly ac¬
cessed and manipulated. Aset of bits in the structure bit field forms a ‘char’ or ‘int’ variable. The general
fomiat of declaration of bit fields within structure is illustrated below.
-: scruet: . struct_name ■ >:: ■ -.p ... .... . - - »
Eit__ya-r n_name
} ; , , ,
‘struct name' represents the name of the bit field structure. ‘data type’ is the data type which will be
formed by packing several bits. Only character (char) and integer (int/short int) data types are allowed
in bit field structures in Embedded C applications. Some compilers may not support the ‘char’ data type.
Flowever our illustrative cross-compiler C51 supports ‘char’ data type as data type. ‘bit_yar l_name’
denotes the bit variable and cbit_size’ gives the number of bits required by the variable lbit_var Ijiame ’
and so. The operator ‘:’ associates the number of bits required with the bit variable. Bit variables that are
packed to form a data type should be separated inside the structure using ‘,’ operator. A real picture of
bit fields in structures for embedded applications can be provided by the Program Status Word (PSW)
register representation of 8051 controller. The PSW register of 8051 is bit addressable and its bit level
representation is given below.
struct PSW
{ ..
char P:l, /* Bit 0 of PSW : Parity Flag */
:1, /* Bit 1 of PSW : Unused V
OV:l, /* Bit 2 of PSW : Overflow Flag */
RS0:1, /* Bit 3 of PSW : Register Bank Select 0 bit */
RSI :li /* Bit 4 of PSW : Register Bank Select 1 bit */
F0:1, /* Bit 5 of PSW: User definable Flag */
1 : AC :1, /* Bit 6 of PSW : Auxiliary Carry> Flag */:
C:l; /* Bit 7 of PSW : Carry Flag */
Introduction to Embedded Systems
/fNote that the operator is used after the last bit field to y • i- ...
indicate end of bit field*/ \ / fT..*. .... A c . •? ■! t ■ y
In the above structure declaration, each bit field variable is defined with a name and an associated
bit size representation. If some of the bits are unused in a packed fashion, the same can be skipped by
merely giving the number of bytes to be skipped without giving a name for that bit variables. In the
above example Bit 1 of PSW is skipped since it is an unused bit in the PSW register.
It should be noted that the total number of bits used by the bit field variables defined for a specific
data type should not exceed the maximum number of allocated bits for that specific data type. In the
above example the bit field data type is ‘char ’ (8 bits) and 7 bit field variables each of size 1 are declared
and one bit variable is declared as unused bit. Hence the total number of bits consumed by all the bit
variables including the non declared bit variable sums to 8, which is same as the bit size for the data
type ‘char ’. The internal representation of structure bit fields depends on the size supported by the cross-
compiler data type (char/int) and the ordering of bits in memory. Some processors/controllers store the
bits from left to right while others store from right to left (Here comes the significance of endianness).
Unions Union is a concept derived from structure and union declarations follow the same syntax
as that of structures (structure tag is replaced by union tag). Though union looks similar to structure
in declaration, it differs from structure in the memory allocation technique for the member variables.
Whenever a union variable is created, memory is allocated only to the member variable of union requir¬
ing the maximum storage size. But for structures, memory is allocated to each member variables. This
helps in efficient data memory usage. Even if a union variable contains different member variables of
different data types, existence is only for a single variable at a time. The size of a union returns the stor¬
age size of its member variable occupying the maximum storage size. The syntax for declaring union
is given below
union union_name
{• :
//variable 1 declaration
//variable 2 to declaration
//variable n declaration
or
typedef union
{
//variable 1 declaration
//variable.2 to declaration
//.
//variable n declaration
Union name;
'union jiame’ is the name of the union and programmers can use any name according to their program¬
ming style. As an illustrative example let’s declare a union variable consisting of an integer member
variable and a character member variable.
Embedded Firmware Design and Development
t*a \W- M-y-- /-■■'>■'<■.< ■■ " '5“,;; "..■:-■••/,;■;■ ■ -it s S\— ;</ ;• i ', {■-. -. ' iv .< -. ,**v =.
Assuming the storage location required for ‘ int’ as 4 bytes and for ‘char' as 1 byte, the memory allocated
to the union variable exl will be as shown below
Memory .
p exi.z-
Address 4x + 3 4x + 2 4x + 1 4x 1
Note: The start address is chosen arbitrarily for illustration, it can be any data memory. It is obvious from the figure
that by using union the same physical address can be accessed with different data type references. Hence union is
a convenient way of ‘variant access’.
In Embedded C applications, union may be used for fast accessing of individual bytes of ‘long’ or
‘int’ variables, eliminating the need for masking the other bytes of ‘long’ or ‘int’ variables which are of
no interest, for checking some conditions. Typical example is extracting the individual bytes of 16 or 32
bit counters.
A few important points on Structure and Union
1. The offsetofQ macro returns the offset of a member variable (in bytes) from the beginning of its
parent structure. The usage is offsetof (structName, memberName); where ‘structName’ is the
name of the parent structure and ‘memberName’ is the name of the member in the parent data
structure whose offset is to be determined. For using this macro use the header file ‘stddef.h ’
2. If you declare a structure just before the main () function in your source file, ensure that the struc¬
ture is terminated with the structure definition termination indicator Otherwise function main
() will be treated as a structure and the application may crash with exceptions.
3. A union variable can be initialised at the time of creation with the first member variable value
only.
9.3.3.14 Pre-processors and Macros Pre-processor in ‘C’ is compiler/cross-compiler directives
used by compiler/cross-compiler to filter the source code before compilation/cross-compilation. The
pre-processor directives are mere directions to the compilers/cross compilers on how the source file
should be compiled/cross compiled. No executable code is generated for pre-processor directives on
compilation. They are same as pseudo ops in assembly language. Pre-processors are very useful for
selecting target processor/controller dependent pieces of code for different target systems and allow a
single source code to be compiled and run on several different target boards. The syntax for pre-proces¬
sor direct’-"u is different from the «vnta.x of T/ language. Each nre-nrocessor directive starts with the
Introduction to Embedded Systems
‘#’ symbol and ends without a semicolon (;). Pre-processor directives are normally placed before the
entry point function mainQ in a source file. Pre-processor directives are grouped into three categories;
namely
1. File inclusion pre-processor directives
2. Compile control pre-processor directives
3. Macro substitution pre-processor directives
File inclusion pre processor directives The file inclusion pre processor directives include external
files containing macro definitions, function declarations, constant definitions etc to the current source
file. ‘^include’ is the pre processor directive used for file inclusion. ‘Mnclude’ pre processor instruc¬
tion reads the entire contents of the file specified by the ‘ #include’ directive and inserts it to the current
source file at a location where the ‘Mnclude’ statement is invoked. This is generally used for reading
header files for library functions and user defined header files or source files. Header files contain de¬
tails of functions and types used within the library and user created files. They must be included before
the program uses the library functions and other user defined functions. Library header file names are
always enclosed in angle brackets, < >. This instructs the pre-processor to look the standard include
directory for the header file, which needs to be inserted to the current source file.
e.g. Mnclude <stdio.h> is the directive to insert the standard library file ‘stdio.hThis file is available
in the standard include directory of the development environment. (Normally inside the folder ‘inc’).
If the file to be included is given in double quotes (“ ”), the pre-processor searches the file first in the
current directory where the current source code file is present and if not found will search the standard
include directory. Usually user defined files kept in the project folder are included using this technique.
E.g. #include “constants.h” where ‘constants’ is a user defined header file with name constant's.h, kept
in the local project folder. An include file can include another include file in it (Nesting of include files).
An include file is not allowed to include the same file itself. Source files (.c) file can also be used as
include files.
\
Compile control pre-processor directives Compile control pre-processor directives are used for
controlling the compilation process such as skipping compilation of a portion of code, adding debug
features, etc. The conditional control pre-processor directives are similar to the conditional statement if
else in ‘C\ Mfdef, Mfndef Uelse, ttendif thindef, etc are the compile control pre-processor directives.
Mfdef uses a name as argument and returns true if the argument (name) is already defined. #define is
used for defining the argument (name).
ttelse is used for two way branching when combined with Mfdef (same as if else in ‘C’).
Hendif is used for indicating the end of a block following Mfdef or Uelse
Usage of Mfdef Uelse and Uendif is given below,
tifdef
#else (optional)
The pre-processor directive Mfndef is complementary to Mfdef It is used for checking whether an
argument (e.g. macro) is not defined. Pre-processor directive #undef is used for disabling the definition
of the argument or macro if it is defined. It is complementary to Mefine. Pre-processor directives are
a powerful option in source code debugging. If you want to ensure the execution of a code block, for
Embedded Firmware Design and Development
debug purpose you can define a debug variable and define it. Within the code wherever you want to en¬
sure the execution, use the ttifdef and dendif pre-processors. The argument to #ifdef should be the debug
variable. Insert a printfQ function within the #ifdef ttendif block. If no debugging is required comment
or remove the definition of debug variable.
E.g.
define DEBUG 1
,3/,JInside code block
gfifdef DEBUG
fprintf ("Debug. Enabled") ;
EfendifT
The Uerror pre-processor directive The ikrror pre-processor generates error message in case of
an error and stops the compilation on accounting an error condition. The syntax of ikrror directive is
given below
{ ' terror error message
Macro substitution pre-processor directives Macros are a means of creating portable inline code.
‘Inline’ means wherever the macro is called in a source file it is replaced directly with the code defined
by the macro. In-line code improves the performance in terms of execution speed. Macros are similar to
S' subroutines in functioning but they differ in the way in which they are coded in the source code. Func¬
tions are normally written only once with arguments. Whenever a function needs to be executed, a call
to the function code is made with the required parameters at the point where it needs to be invoked. If a
—macro is used instead of functions, the compiler inserts the code for macro wherever it is called. From
a code size viewpoint macros are non-optimised compared to functions. Macros are generally used for
coding small functions requiring execution without latency. The ‘#define’ pre-processor directive is
used for coding macros. Macros can be either simple definitions or functions with arguments.
tidefine PI 3.1415 is an example for a simple macro definition.
Macro definition can contain arithmetic operations also. Proper care should be taken in giving right
syntax while defining macros, keeping an eye on their usage in the source code. Consider the following
example
#define A 12+25
tdefine B 45-10
Suppose the source contains a statement multiplier = A *B; the pre-processor directly replaces the mac¬
ros A and B and the statement becomes
multiplier = 12+25*45-10;
Due to the operator precedence criteria, this won’t give the expected result. Expected result can be ob¬
tained by a simple re-writing of the macro with necessary parentheses as illustrated below.
gA # define A (12+25)
#define B (45-10)
Proper care should be given to parentheses the macro arguments. As mentioned earlier macros can also
be defined with arguments. Consider the following example
Introduction to Embedded Systems
This defines a macro for calculating the area of a circle. It takes an argument and returns the area. It
should be noted that there is no space between the name of the macro (macro identifier) and the left
bracket parenthesis.
Suppose the source code contains a statement like area=CIRCLE_AREA(5); it will be replaced as
. • : area = (3.14*5*5);
Suppose the call is like this, area=CIRCLE_AREA(2+5); thejpre-processor will translate the same as
. ;■ v • •, ^ ^3Tl4ti2.+5*'2+5)' ■ ' - . i ,/ V C i .
Will it produce the expected result? Obviously no. This shortcoming in macro definition can be elimi¬
nated by using parenthesis to each occurrence of the argument. Hence the ideal solution will be;
. . Idcfine CIRCLEj\RF.A (a) (3.14 *(a)*(a}) ", l,. T 1 U .4:'
9.3.3.15 Constant Declarations in Embedded ‘C’ In embedded applications the qualifier (key¬
word) ‘const’ represents a ‘Read only’ variable. Use of the keyword ‘const’ in front of a variable declares
that the value of the variable cannot be altered by the program. This is a kind of defensive programming
in which any attempt to modify a variable declared as ‘const’ is reported as an access violation by the
cross-compiler. The different types of constant variables used in embedded ‘C’ application are explained
below.
Constant data Constant data informs that the data held by a variable is always constant and can¬
not be modified by the application. Constants used as scaling variables, ratio factors, various scientific
computing constants (e.g. Plank’s constant), etc. are represented as constant data. The general form of
declaring a constant variable kTgiven below.
const data type variable name;, ’
or
data type const variable name;
‘const’ is the keyword informing compiler/cross compiler that the variable is constant, ‘data type’ gives
the data type of the variable. It can be ‘int’, ‘char’, ‘float’, etc. ‘variable name’ is the constant variable.
E.g. const float PI = 3.1417;
float const PI = 3.1417;
Both of these statements declare PI as floating point constant and assign the value 3.1417 to it.
Constant variable can also be defined using the Mefme pre-processor directive as given below.
♦ define PI 3.1417
/*No assignment using = operator and no at end*/
The difference between both approaches is that the first one defines a constant of a particular data
type (int, char, float, etc.) whereas in the second method the constant is represented using a mere symbol
(text) and there is no implicit declaration about the data type of the constant. Both approaches are used
in declaring constants in embedded C applications. The choice is left to the programmer.
Embedded Firmware Design and Development
Pointer to constant data Pointer to constant data is a pointer which points to a data which is read
only. The pointer pointing to the data can be changed but the data is non-modifiable. Example of pointer
to constant data
const int* x; . //Integer po1nrer x to constant data
jj§; • int const* x 7/Same meaning as above definition"
Constant pointer to data Constant pointer has significant importance in Embedded C applications.
An embedded system under consideration may have various external chips like, data memory, Real
Time Clock (RTC), etc interfaced to the target processor/controller, using memory mapped technique.
The range of address assigned to each chips and their registers are dependent on the hardware design. At
the time of designing the hardware itself, address ranges are allocated to the different chips and the tar¬
get hardware is developed according to these address allocations. For example, assume we have an RTC
chip which is memory mapped at address range 0x3000 to 0x3010 and the memory mapped address
of register holding the time information is at 0x3007. This memory address is fixed and to get the time
information we have to look at the register residing at location 0x3007. But the content of the register
located at address 0x3007 is subject to change according to the change in time. We can access this data
using a constant pointer. The declaration of a constant pointer is given below.
|fe ..Constant character pointer x .go (:cbnStafit^v=itiable Mata 4 \v .
jphhblTconst x; ’-ApJiiA Tv'-il.'; IV • A- v.. ..... . '• .. iJ/S ■■ v
Constant pointer to constant data , Constant pointers pointing to constant data are widely used in
embedded programming applications. Typical uses are reading configuration data held at ROM chips
which are memory mapped at a specified address range, Read only status registers of different chips,
memory mapped at a fixed address. Syntax of declaring a constant pointer to constant data is given
below.
r/*Constant character pointer x pointing to constant data*/
•.-const char *const x;
/char const* const x; //Equivalent to above declaration
9.3.3.16 The ‘Volatile’Type Qualifier in Embedded ‘C’ The keyword ‘volatile’ prefixed with
any variable as qualifier informs the crqss-compiler that the value of the variable is subject to change at
any point of time (subject to asynchronous modification) other than the current statement of code where
the control is at present.
Examples of variables which are subject to asynchronous modifications are
1. Variables common to Interrupt Service Routines (ISR) and other functions of a file
2. Memoiy mapped hardware registers
Introduction to Embedded Systems
3. Variables shared by different threads in a multi threaded application (Not applicable to Super loop
Firmware development approach)
The volatile’ keyword informs the cross-compiler that the variable with ‘volatile’ qualifier is subject
to asynchronous change and there by the cross compiler turns off any optimisation (assumptions on the
variable) for these variables. The general form of declaring a volatile variable is given below.
-• - vo 1 ati le.. data type va ri abl e name:; or ■. j- .
data type volatileyva-plphle •name'.;,.
‘data type’ refers to the data type of the variable. It can be int, char, float, etc. ‘variable name’ is the user
defined name for the volatile variable of the specified type.
■■ ■ E.g. volatile unsigned char x; t , • ■ - . /
y ; . - unsigned char volatile x; :l" - ■■ ■ "■ ■
What is the catch in using ‘volatile’ variable? Let’s examine the following code snippet.
//Declare a memory mapped register
char* status__reg = (char*) 0x3000; , -
On cross-compiling the code snippet, the cross-compiler converts the code to a read operation from
the memory location mapped at address 0x3000 and it will assume that there is no point where the vari¬
able is going to modify (sort of over smartness) and may keep the data in a register to speed up the exe¬
cution. The actual intention of the programmer, with the above code snippet is to read a memory mapped
hardware status register and halt the execution of the rest of the code till the status register shows a ready
status. Unfortunately the program will not produce the expected result due to the oversmartness of the
cross-compiler in optimising the code for execution speed. Re-writing the code as given below serves
the intended purpose.
//Declares (volatile variable.
volatile char *stat.us reg = (char *) 0x3000;
In embedded applications all memory mapped device registers which are subject to asynchronous
modifications (e.g. status, control and general purpose registers of memory mapped external devices)
should be declared with ‘volatile’ keyword to inform the cross-compiler that the variables represent¬
ing these registers/locations are subject to asynchronous changes and do not optimise them. Another
area which needs utmost care in embedded applications is variables shared between ISR and functions
(variables which can be modified by both Interrupt Sub Routines and functions). These include structure!
variable, union variable and other ordinary variables. To avoid unexpected behaviour of the application!
always declare such variables using ‘volatile’ keyword.
The ‘constant volatile’Variable Some variables used in embedded applications can be both ‘con¬
stant’ and ‘volatile’. A ‘Read only’ status register of a memory mapped device is a typical example for
this. From a user point of view the ‘Read only’ status registers can only be read but cannot modify.
Hence it is a constant variable. From the device point the contents can be modified at any time by the
device. So it is a volatile variable. Typical declarations are given ahead.
Embedded Firmware Design and Development
Volatile pointer Volatile pointers is subject to change at any point after they are initialised. Typi¬
cal examples are pointer to arrays or buffers modifiable by Interrupt Service Routines and pointers in
dynamic memory allocation. Pointers used in dynamic memory allocation can be modified by the real-
locQ function. The general form of declaration of a volatile pointer to a non-volatile variable is given
below.
| .ata type* volatile variable name;
.g. unsigned char* volatile a;
.
.
a
' '
v
iwm..,
9.3.3.17 Delay Generation and Infinite Loops in Embedded C Almost every embedded ap¬
plication involves delay programming. Embedded applications employ delay programming for waiting
for a fixed time interval till a device is ready, for inserting delay between displays updating to give the
user sufficient time to view the contents displayed, delays involved in bit transmission and reception in
asynchronous serial transmissions like I2C, 1-Wire data transfer, delay for key de-bouncing etc. Some
delay requirements in embedded application may be critical, meaning delay accuracy should be within
a very narrow tolerance band. Typical example is delay used in bit data transmission. If the delay em¬
ployed is not accurate, the bits may lost while transmission or reception. Certain delay requirements in
embedded application may not be much critical, e.g. display updating delay.
It is easy to code delays in desktop applications under DOS or Windows operating systems. The li¬
brary function delayQ in DOS and Sleep () in Windows provides delays in milliseconds with reasonable
accuracy. Coding delay routines in embedded applications is bit difficult. The major reason is delay is
dependent on target system’s clock frequency. So we need to have a trial and error approach to code de¬
lays demanding reasonably good accuracy. Refer to the code snippet given for ‘Performance Analyser’
in Chapter 14 for getting a handle on how to code delay routine in embedded applications using IDEs.
Delay codes are generally non-portable. Delay routine requires a complete re-work if the target clock
frequency is changed. Normally ‘for loops' are used for coding delays. Infinite loops are created using
various loop control instructions like while (), do while (), for and goto labels. The super loop created
by while (1) instruction in a traditional super loop based embedded firmware design is a typical example
for infinite loop in embedded application development.
Infinite loop using while The following code snippet illustrates ‘ while’ for infinite loop implemen¬
tation.
while (1)
{
1
} while (1) ; i
Introduction to Embedded Systems
for Ur
■¥ .«.vv a.
{ :?t *1
* }
Infinite loop using goto ‘goto’ when combined with a ‘label’ can create infinite loops.
label; to be repeated .
//.
. : -
\ -V -.goto'-label; . =•. - -
Which technique is the best? According to all experienced Embedded ‘C’ programmers while()
loop is the best choice for creating infinite loops. There is no technical reason for this. The clean syntax
of while loop entitles it for the same. The syntax offor loop for infinite loop is little puzzling and it is
not capable of conveying its intended use. ‘goto’ is the favorite choice of programmers migrating from
Assembly to Embedded C ©.
break; statement is used for coming out of an infinite loop. You may think why we implement an in¬
finite loop and then quitting it? Answer - There may be instructions checking some condition inside the
infinite loop. If the condition is met the program control may have to transfer to some other location.
9.3.3.18 Bit Manipulation Operations Though Embedded ‘C’ does not support a built in Boolean
variable (Bit variable) for holding a ‘TRUE (Logic 1)’ or ‘FALSE (Logic 0)’ condition, it provides ex¬
tensive support for Bit manipulation operations. Boolean variables required in embedded application are
quite often stored as variables with least storage memory requirement (obviously char variable). Indeed
it is wastage of memory if the application contains large number of Boolean variables and each variable
is stored as a char variable. Only one bit (Least Significant bit) in a char variable is used for storing
Boolean information. Rest 7 bits are left unused. This will definitely lead to serious memory bottle
neck. Considerable amount of memory can be saved if different Boolean variables in an application are
packed into a single variable in ‘C’ which requires less memory storage bytes. A character variable can
accommodate 8 Boolean variables. If the Boolean variables are packed for saving memory, depending
upon the program requirement each variable may have to be extracted and some manipulation (setting,
clearing, inverting, etc.) needs to be performed on the bits. The following Bit manipulation operations
are employed for the same.
Bitwise AND Operator ‘&’ performs Bitwise AND operations. Please note that the Bitwise AND
operator is entirely different from the Logical AND operator The ‘&’ operator acts on indi¬
vidual bits of the operands. Bitwise AND operations are usually performed for selective clearing of bits
and testing the present state of a bit (Bitwise ANDing with ‘ 1 ’).
Bitwise OR Operator ‘| ’ performs Bitwise OR operations. Logical OR operator ‘||’ is in no way relat¬
ed to the Bitwise OR operator ‘| \ Bitwise OR operation is performed on individual bits of the operands.
Bitwise OR operation is usually performed for selectively setting of bits and testing the current state of
a bit (Bitwise ORing with ‘0’).
Bitwise Exclusive OR- XOR Bitwise XOR operator ‘A’ acts on individual operand bits and performs
an ‘Excusive OR’ operation on the bits. Bitwise XOR operation is used for toggling bits in embedded
applications.
Embedded Firmware Design and Development
Bitwise NOT Bitwise NOT operations negates (inverts) the state of a bit. The operator (tilde) is
used as the Bitwise NOT operator in C.
Setting and Clearing and Bits Setting the value of a bit to ‘1’ is achieved by a Bitwise OR opera¬
tion. For example consider a character variable (8bit variable) flag. The following instruction sets its 0th
bit always 1.
ry flag = flag 1; ' t. __
Bitwise OR operation combined with left shift operation of ‘ V is used for selectively setting any bit in
a variable. For example the following operation will set bit 6 of char variable flag.
-.//Sets 6th bit,of/flag. Bit numbering starts with 0.
The same can also be achieved by bitwise ORing the variable flag with a mask with 6th bit ‘ U and all
other bits ‘O’, i.e. mask with 01000000 in Binary representation and 0x40 in hex representation.
flag |= 0x40; //Equivalent to flag = flag | (1«6) ;
Clearing a desired bit is achieved by Bitwise ANDing the bit with ‘O’. Bitwise AND operation combined
with left shifting of ‘ 1 ’ can be used for clearing any desired bit in a variable.
/Example:
;/; • flag &= ~ (1«6);
The above instruction will clear the 6th bit of the character variable flag. The operation is illustrated
below.
Execution of (1«6) shifts ‘ 1 ’ to six positions left and the resulting output in binary will be 01000000.
Inverting this using the Bitwise NOT operation (~ (1 «6)) inverts the bits and give 10111111 as output.
When flag is Bitwise ANDed with 10111111, the 6th bit offlag is cleared (set to ‘0’) and all other bits of
flag remain unchanged.
From the above illustration it can be inferred that the same operation can also be achieved by a direct
Bitwise ANDing of the variable flag and a mask with binary representation 10111111 or hex representa¬
tion OxBF.
358 Introduction to Embedded Systems
Shifting the mask ‘ 1’ for setting or clearing a desired bit works perfectly, if the operand on which these
operations are performed is 8bit wide. If the operand is greater than 8 bits in size, care should be taken
to adjust the mask as wide as the operand. As an example let us assume flag as a 32bit operand. For
clearing the 6th bit offlag as illustrated in the previous example, the mask 1 should be re-written as ‘ 1L’
and the instruction becomes
flag &= ~ (1L«6)
Toggling Bits
Toggling a bit is performed to negate (toggle) the current state of a bit. If current state of a specified bit
is ‘ 1 ’, after toggling it becomes ‘0’ and vice versa. Toggling is also known as inverting bits. The Bitwise
XOR operator is used for toggling the state of a desired bit in an operand.
flag A--(1«6); //Toggle bit 6 of flag .
Suppose this is arranged in a 16bit integer variable date, for any calculation requiring ‘Year’, ‘Month’
or ‘Date’, each should be extracted from the single variable date. The following code snippet illustrates
the extraction of ‘Year’
tyV :'char year = (date»9) & 0x7F;
(date»9) shifts the contents 9 bits to the left and now ‘Year’ is stored in the variable date in bits 0 to
6 (including both). The contents of other bits (7 to 15) are not relevant to us and we need only th£ first
7 bits (0 to 6) for getting the ‘Year’ value. ANDing with the mask 0x7F (01111111 in Binary) retains
the contents of bits 0 to 6 and now the variable year contains the ‘Year’ value extracted from variable
'date'.
Similar to the extracting of bits, we may have to insert bits to change the value of certain data. In the
above example if the program is written in such a way that after each 1 minute, the RTC’s year register is
read and the contents of bit fields 9 to 15 in variable 'date' needs to be updated with the new value. This
can be achieved by inserting the bits corresponding to the data into the desired bit position. Following
code snippet illustrates the updating of ‘Year’ value for the above example. To set the new ‘Year’ value
to the variable 'date' all the corresponding bits in the variable for ‘Year’ should be cleared first. It is il¬
lustrated below.
:■ date = date •& ~ (0x7F<<9) ; ‘ . . :,
The mask for 7 bits is 0x7F (01111111 in Binary). Shifting the mask 9 times left aligns the mask to
that of the bits for storing ‘Year’. The ~ operator inverts the 7 bits aligning to the bits corresponding to
‘Year’. Bitwise ANDing with this negated mask clears the current bits for ‘Year’ in the variable 'date'.
Now shift the new value which needs to be put at the.‘Year’ location, 9 times for bitwise alignment for
the bits corresponding to ‘Year’. Bitwise OR the bit aligned new value with the 'date' variable whose
bits corresponding to ‘Year’ is already cleared. The following instruction performs this.
■ date' ] = (new_year «9) ; ■
If all the bits corresponding to the bit field to be modified are not set to zero prior to Bitwise ORing
with the new value, any existing bit with value T will remain as such and expected result will not be
obtained.
Testing Bits So far we discussed the Bitwise operators for changing the status of a selected bit. Bit¬
wise operators can also be used for checking the present status of a bit without modifying it for decision
making operations.
if (flag & (1«6)) //Checks whether 6th bit of flag is '1'
This instruction examines the status of the 6th bit of variable flag. The same can also be achieved by
using a constant bit mask as
if (flag & 0x40.) //Checks whether 6th bit of flag is- '1'
Introduction to Embedded Systems
9.3.3.19 Coding Interrupt Service Routines (ISR) Interrupt is an event that stops the current
execution of a process (task) in the CPU and transfers the program execution to an address in code
memory where the service routine for the event is located. The event which stops the current execu¬
tion can be an internal event or an external event or a proper combination of the external and internal
event. Any trigger signal coming from an externally interfaced device demanding immediate attentions
is an example for external event whereas the trigger indicating the overflow of an internal timer is an
example for internal event. Reception of serial data through the serial line is a combination of internal
and external events. The number of interrupts supported, their priority levels, interrupt trigger type and
the structure of interrupts are processor/controller architecture dependent and it varies from processor
to processor. Interrupts are generally classified into two: Maskable Interrupts and Non-maskable Inter¬
rupts (NMI). Maskable interrupt can be ignored by the CPU if the interrupt is internally not enabled or
if the CPU is currently engaged in processing another interrupt which is at high priority. Non-maskable
interrupts are interrupts which require urgent attention and cannot be ignored by the CPU. Reset (RST)
interrupt and TRAP interrupt of 8085 processor are examples for Non-maskable interrupts.
Interrupts are considered as boon for programmers and their main motto is “give real time behav¬
iour to applications”. In a processor/controller based simple embedded system where the application is
designed in a super loop model, each interrupt supported by the processor/controller will have a fixed
memory location assigned in the code memory for writing its corresponding service routine and this
address is referred as Interrupt Vector Address. The vector address for each interrupt will be pre-de-
fined and the number of code memory bytes allocated for each Interrupt Service Routine, starting from
the Interrupt Vector Address may also be fixed. For example the interrupt vector address for interrupt
‘INTO’ of 8051 microcontroller is ‘0003H’ and the number of code memory bytes allowed for writing
its Service routine is 8 bytes.
The function written for serving an Interrupt is known as Interrupt Service Routine (ISR). ISR for
each interrupt may be different and they are placed at the Interrupt Vector Address of corresponding
Interrupt, ISR is essentially a function that takes no parameters and returns no results. But, unlike a
regular function, the ISR can be active at any time since the triggering of interrupts need not be in sync
with the internal program execution (e.g. An external device connected to the external interrupt line can
assert external interrupt at any time regardless at what stage the program execution is currently). Hence
.special care must be taken in writing these functions keeping in mind; they are not going to be executed
in a pre-defined order. What all special care should be taken in writing an ISR? The following section
answers this query.
Imagine a situation where the application is doing some operations and some registers are modified
and an interrupt is triggered in between the operation. Indeed the program flow is diverted to the Inter¬
rupt Service Routine, if the interrupt is an enabled interrupt and the operation in progress is not a service
routine of a high priority interrupt, and the ISR is executed. After completing the ISR, the program
flow is re-directed to the point where it got interrupted and the interrupted operation is continued. What
happens if the ISR modifies some of the registers used by the program? No doubt the application will
produce unexpected results and may go for a toss. How can this situation be tackled? Such a situation
can be avoided if the ISR is coded in such a way that it takes care of the following:
1. Save the current context (Important Registers which the ISR will modify)
2. Service the Interrupt
3. Retrieve the saved context (Retrieve the original contents of registers)
4. Return to the execution point where the execution is interrupted
Normal functions will not incorporate code for saving the current context before executing the func¬
tion and retrieve the saved context before exiting the function. ISR should incorporate code for perform-
Embedded Firmware Design and Development
ing these operations. Also it is known to the programmer at what point a normal function is going to be
invoked and the programmer can take necessary precautions before calling the function, it is not the case
for an ISR. Interrupt Service Routines can be coded either in Assembly language or High level language
in which the application is written. Assembly language is the best choice if the ISR code is very small
and the program itself is written in Assembly. Assembly code generates optimised code for ISR and user
will have a good control on deciding what all registers needs to be preserved from alteration. Most of
the modem cross-compilers provide extensive support for efficient and optimised ISR code. The way in
which a function is declared as ISR and what all context is saved within the function is cross-compiler
dependent.
Keil C51 Cross-complier for 8051 microcontroller implements the Interrupt Service Routine using
the keyword interrupt and its syntax is illustrated below.
■interrupt_name is the function name and programmers can choose any name according to his/her taste.
The attribute ‘interrupt’ instructs the cross compiler that the associated function is an interrupt service
routine. The interrupt attribute takes an argument x which is an integer constant in the range 0 to 31
(supporting 32 interrupts). This number is the interrupt number and it is essential for placing the gener¬
ated hex code corresponding to the ISR in the corresponding Interrupt Vector Address (e.g. For placing
the ISR code at code memory location 0003H for Interrupt 0 - External Interrupt 0 for 8051 microcon¬
troller). using is an optional keyword for indicating which register bank is used for the general purpose
' Registers R0 to R7 (For more details on register banks and general purpose registers, refer to the hard¬
ware description of 8051). The argument y for using attribute can take values from 0 to 3 (corresponding
to the register banks 0 to 3 of 8051). The interrupt attribute affects the object code of the function in the
following way:
1. If required, the contents of registers ACC, B, DPH, DPL, and PSW are saved on the stack at func¬
tion invocation time.
2. All working registers (R0 to R7) used in the interrupt function are stored on the stack if a register
bank is not specified with the using attribute.
3. The working registers and special registers that were saved on the stack are restored before exiting
the function.
4. The function is terminated by the 8051 RETI instmction.
Typical usage is illustrated below
void external_interruptO (void ) interrupt 0 using 0
1
adc_control = *adc_control_reg //Read memory mapped ADC
; // control Register
If the cross-compiler you are using don’t have a built in support for writing ISRs, What shall you
do? Don’t be panic you can implement the ISR feature with little tricky coding. If the cross-compiler
provides support for mixing high level language-C andAsseihbly, write the ISR in Assembly and place
the ISR code at the corresponding Interrupt Vector address using the cross compiler’s support for plac¬
ing the code in an absolute address location of code memory (Using keywords like at. Refer to your
Introduction to Embedded Systems
cross compiler’s documentation for getting the exact keyword). If the ISR is too complicated, you can
place the body of the ISR processing in a normal C function and call it from a simple assembly language
wrapper. The assembly language wrapper should be installed as the ISR function at the absolute address
corresponding to the Interrupt’s Vector Address. It is responsible for executing the current context sav¬
ing and retrieving instructions. Current context saving instructions are written on top of the call to the
‘C’ function and context retrieving instructions are written just below the ‘C’ function call. It is little
puzzling to find answers to the following questions in this approach.
1. Which registers must be saved and restored since we are not sure which registers are used by the
cross compiler for implementing the ‘C’ function?
2. How the assembly instructions can be interfaced with high-level language like ‘C’?
Answers to these questions are cross compiler dependent and you need to find the answer by referring
the documentation files of the cross-compiler in use. Context saving is done by ‘Pushing’ the registers
(using PUSH instructions) to stack and retrieving the saved context is done by ‘Poping’ (using POP
instructions) the pushed registers. Push and Pop operations usually follow the Last In First Out (LIFO)
method. While writing an ISR, always keep in mind that the primary aim of an interrupt is to provide
real time behaviour to the program. Saving the current context delays the execution of the original ISR
function and it creates Interrupt latency (Refer to the section on Interrupt Latency for more details) and
thereby adds lack of real time behaviour to an application. As a programmer, if you are responsible for
saving the current context by Pushing the registers, avoid Pushing the registers which are not used by
the ISR. If you deliberately avoid the saving of registers which are going to be modified by the ISR, your
application may go for a toss. Hence Context saving is an unavoidable evil in Interrupt driven program¬
ming. If you go for saving unused registers, it will increase interrupt latency as well as stack memory
usage. So always take a judicious decision on the context saving operation. If the cross compiler offers
the context saving operation by supporting ISR functions, always rely on it. Because most modem cross
compilers are smart and capable of taking a judicious decision on context saving. In case the cross com¬
piler is not supporting ISR function and as a programmer you are the one writing ISR functions either
in Assembly or by mixing ‘C’ and Assembly, ensure that the size of ISR is not crossing the size of code
memory bytes allowed for an Interrupt’s ISR (8 bytes for 8051). If it exceeds, it will overlap with the
location for the next interrupt and may create unexpected behaviour on servicing the Interrupt whose
Vector address got overlapped.
9.3.3.20 Recursive Functions A function which calls itself repeatedly is called a Recursive Func¬
tion. Using recursion, a complex problem is split into its single simplest form. The recursive function
only knows how to solve that simplest case. Recursive-functions are useful in evaluating certain types
of mathematical function, creating and accessing dynamic data structures such as linked lists or binary
trees. As an example let us consider the factorial calculation of a number.
By mathematical definition
n factorial - 1 x 2 x.(n - 2) x (n - 1) x n; where n= 1,2, etc... and 0 factorial = 1
Using ‘C’ the function for finding the factorial of a number V is written as
int factorial (int n)
{ ' .
int count;
int factorial®!; \\ \ A .
for (count-1; count<=n;| count! 1)1
factoria.1 *-ccunL; ' ! I
Embedded Firmware Design and Development
return factorial;
|v . >
This code is based on iteration. The iteration of calculating the repeated products is performed until
the count value exceeds the value of the number whose factorial is to be calculated. We can split up the
task performed inside the function as count * (count +1) till count is one short of the number whose
factorial is to be calculated. Using recursion we can re-write the above function as ■
Lnt; factorial (ini. n) ■' •;! < .
{- * 5 ' •'
if (n==0) t( « ,V
“return 1; '§**?&*ft\ «'lu/5't.iV-5$***
;el>se •• % ;.yt
return .•(ri * f-actor i&i (nrl));
U' •; -.vU - ' '• / v. Hiu
Here the function factorial (n) calls itself, with a changed version of the parameter for each call,
inside the same for calculating the factorial of a given number. The ‘if’ statement within the recursive
function forces the function to stop recursion when a certain criterion is met. Without an ‘if’ statement a
recursive function will never stop the recursion process. Recursive functions are very effective in imple¬
menting solutions expressed in terms of a successive application of the same solution to the solution
subsets. Recursion is a powerful at the same time most dangerous feature which may lead to application
crash. The local variables of a recursive function are stored in the stack memory and new copies of each
variable are created for successive recursive calls of the function. As the recursion goes in a deep level,
stack memory may overflow and the application may bounce.
Recursion vs. Iteration: A comparison Both recursion and iteration is used for implementing cer¬
tain operations which are self repetitive in some form.
• Recursion involves a lot of call stack overhead and function calls. Hence it is slower in operation
compared to the iterative method. Since recursion implements the functionality with repeated
self function calls, more stack memory is required for storing the local variables and the function
return address
• Recursion is the best method for implementing certain operations like certain mathematical opera¬
tion, creating and accessing of dynamic data structures such as linked lists or binary trees
• A recursive solution implementation can always be replaced by iteration. The process of convert¬
ing a recursive function to iterative method is called ‘unrolling’
9.3.3.21 Re-entrant Functions Functions which can be shared safely with several processes
concurrently are called re-entrant functions. When a re-entrant function is executing, another process
can interrupt the execution and can execute the same re-entrant function. The “another process'’ re¬
ferred here can be a thread in a multithreaded application or can be an Interrupt Service Routine (ISR).
Re-entrant function is also referred as ‘pure’ function. Embedded applications extensively make use of
re-entrant functions. Interrupt Service Routines (ISR) in Super loop based firmware systems and threads
in RTOS based systems can change the program’s control flow to alter the current context at any time.
When an interrupt is asserted, the current operation is put on hold and the control is transferred to an¬
other function or task (ISR). Imagine a situation where an interrupt occurs while executing a function
x and the ISR also contain the task of executing the function x. What will happen? - Obviously the ISR
will modify the shared variables offunction x and when the control is switched back to the point where
the execution got interrupted, the function will resume its execution with the data which is modified by
the ISR. What will be the outcome of this action? - Unpredictable result causing data corruption and
potential disaster like application break-down. Why it happens so? Due to the corruption of shared data
in function which is unprotected. How this situation can be avoided? By carefully controlling the shar¬
ing of data in the function.
In embedded applications, a function/subroutine is considered re-entrant if and only if it satisfies the
following criteria.
1. The function should not hold static data over successive calls, or return a pointer to static data.
2. All shared variables within the function are used in an atomic way.
3. The function does not call any other non-reentrant functions within it.
4. The function does not make use of any hardware in a non-atomic way
Rule# 1 deals with variable usage and function return value for a reentrant function. In an operating
system based embedded system, the embedded application is managed by the operating system services
and the ‘memory manager ’ service of the OS kernel is responsible for allocating and de-allocating the
memory required by an application for its execution. The working memory required by an application is
divided into three groups namely; stack memory, heap memory and data memory (Refer to the Dynamic
Memory Allocation section of this chapter for more details). The life time of static variables is same as
that of the life time of the application to which it belongs and they are usually held in the data memory
area of the memory space allocated for the application. All static variables of a function are allocated
in the data memory area space of the application and each instance of the function shares this, whereas
local (auto) variables of a function are stored in the stack memory and each invocation of the function
creates independent copies of these variables in the stack memory. For a function to be reentrant, it
should not keep any data over successive invocations (the function should not contain any static stor¬
age). If a function needs some data to be kept over successive invocations, it shoulcl be provided through
the caller function instead of storing it in the function in the form of static, variables. If the function
returns a pointer to a static data, each invocation of the function makes use of this pointer for returning
the result. This can be tackled by using caller function provided storage for passing the data back to the
caller function. The ‘callee’ function needs to be modified accordingly to makemse of the caller function
provided storage for passing the data back to the caller.
Rule# 2 deals with ‘atomic’ operations. So what does ‘atomic’ mean in reentrancy context? Meaning
the operation cannot be interrupted. If an embedded application contains variables which are shared
between various threads of a multitasking system (Applicable to Operating System based embedded
systems) or between the application and ISR (In Non-RTOS based embedded systems), and if the
operation on the shared variable is non-atomic, there is a possibility for corruption of the variable due
Embedded Firmware Design and Development
to concurrent access of the variable by multiple threads or thread and ISR. As an example let us assume
that the variable counter is shared between multiple threads or between a thread and ISR, the instruction,
need not operate in an ‘atomic’ way on the variable counter. The compiler/cross-compiler in use con¬
verts this high level instmction into machine dependent code (machine language) and the objective of
incrementing the variable counter may be implemented using a nuimber of low level instructions by
the compiler/cross compiler. This violates the ‘atomic’ rule of operation. Imagine a situation where an
execution switch (context switch) happens when one thread is in the middle of executing the low level
instmctions corresponding to the high level instruction counter++, of the function and another thread
(which is currently in execution after the context switch) or an ISR calls the same function and executes
the instmction counter++, this will result in inconsistent result. Refer to the section ‘Racing’ under the
topic ‘ Task Synchronisation Issues' of the chapter ‘Designing with Real Time Operating Systems’ for
more details on this. Eliminating shared global variables and making them as variables local to the func¬
tion solves the problem of modification of shared variables by concurrent execution of the function.
Rule# 3 is selfexplanatory. If a re-entrant function calls another function which is non-reentrant, it
may create potential damages due to the unexpected modification of shared variables if any. What will
happen if a reentrant function calls a standard library function at run time? By default most of the run
time library is reentrant. If a standard library function is not reentrant the function will no longer be
reentrant.
Rule# 4 deals with the atomic way of accessing hardware devices. The term ‘atomic’ in hardware ac¬
cess refers to the number of steps involved in accessing a specific register of a hardware device. For the
hardware access to be atomic the number of steps involved in hardware access should be one. If access
is achieved through multiple steps, any interruption in between the steps may lead to erroneous results.
A typical example is accessing the hardware register of an I/O device mapped to the host CPU using
paged addressing technique. In order to Read/Write from/to any of the hardware registers of the device
a minimum of two steps is required. First write the page address, corresponding to the page in which
the hardware register belongs, to the page register and then read the register by giving the address of
the register within that page. Now imagine a situation where an interrupt occurs at the moment execut¬
ing the page setting instruction in progress. The ISR will be executed after finishing this instruction.
Suppose ISR also involves a Read/Write to another hardware register belonging to a different page.
Obviously it will modify the page register of the device with the required value. What will be its impact?
On finishing the ISR, the interrupted code will try to read the hardware register with the page address
which is modified by the ISR. This yields an erroneous result.
How to declare a function as Reentrant The way in which a function is declared reentrant is cross-
compiler dependent. For Keil C51 cross-compiler for 8051 controller, the keyword ‘reentrant’ added as
function attribute treats the function as reentrant. For each reentrant function, a reentrant stack area is
simulated in internal or external memory depending whether the data memory is internal or external to
the processor/controller. A typical reentrant function implementation for C51 cross-compiler for 8051
controller is given below.
.int ir.ultip.iy (char i, int b) reentrant
The simulated stack used by reentrant functions has its own stack pointer which is independent of
the processor stack and stack pointer. For C51 cross compiler the simulated stack and stack pointers are
declared and initialised in the startup code in STARTUP.A51 which can be found in the LIB subdirec¬
tory. You must modify the startup code to specify which simulated stack(s) to initialize in order to use
re-entrant functions. You can also modify the starting address for the top of the simulated stack(s) in
the startup code. When calling a function with a re-entrant stack, the cross-compiler must know that the
function has a re-entrant stack. The cross-compiler figures this out from the function prototype which
should include the reentrant keyword (just like the function definition). The cross compiler must also
know which reentrant stack to stuff the arguments for it. For passing arguments, the cross-compiler gen¬
erates code that decrements the stack pointer and then “pushes” arguments onto the stack by storing the
argument indirectly through R0/R1 or DPTR (5057 specific registers). Calling reentrant function decre¬
ments the stack pointer (for local variables) and access arguments using the stack pointer plus an offset
(which corresponds to the number of bytes of local variables). On returning, reentrant function adjusts
. the stack pointer to the value before arguments were pushed. So the caller does not need to perform any
stack adjustment after calling a reentrant function.
Reentrant vs. Recursive Functions The terms Reentrant and Recursive may sound little confusing.
You may find some .amount of similarity among them and ultimately it can lead to the thought are Re¬
entrant functions and Recursive functions the same? The answer is—it depends on the usage context of
the function. It is not necessary that all recursive functions are reentrant. But a Reentrant function can
be invoked recursively by an application.
For 8051 Microcontroller, the internal data memory is very small in size (128 bytes) and the stack
as well user data memory is allocated within it. Normally all variables local to a function and function
arguments are stored in fixed memory locations of the user data memory and each invocation of the
function will access the fixed data memory and any recursive calls to the function use the same memory
locations. And, in this case, arguments and locals would get corrupted. Hence the scope for implement¬
ing recursive functions is limited. A reentrant function can be called recursively to a recursion level
dependent on the simulated stack size for the same reentrant function.
C51 Cross-compiler does not support recursive calls if the functions are non-reentrant.
here refers 'fixed’ and it is no way related to the storage class static. As mentioned, some embedded
applications require data memory which is a combination of fixed memory (Number of variables and
variable size is known prior to cross compilation) and variable length data memory. As an example, let’s
take the scenario where an application deals with reading a stream of character data from an external en¬
vironment and the length of the stream is variable. It can vary between any numbers (say 1 to 100 bytes).
The application needs to store the data in data memory temporarily for some calculation and it can be
ignored after the calculation. This scenario can be handled in two ways in the application program. In
the first approach, allocate fixed memory with maximum size (say. 100 bytes) for storing the incoming
data bytes from the stream. In the second approach allocate memory at run .time of the application and
de-allocate (free) the memory once the data memory storage requirement is over. In the first approach
if the memory is allocated fixedly, it is locked forever, and cannot re-used by the application even if
there is no requirement for the allocated number of bytes and it will definitely create memory bottleneck
issues in embedded systems where memory is a big constraint. Hence it is not advised to go for fixed
memory allocations for applications demanding variable memory size at run time. Allocating memory
on demand and freeing the memory at run time is the most advised method for handling the stor¬
age of dynamic (changing) data and this memory allocation technique is known as ‘Dynamic Memory
Allocation
Dynamic memory allocation technique is employed in Operating System (OS) based embedded
systems. Operating system contains a ‘Memory Management Unit’ and it is responsible for handling
memory allocation related operations. The memory management unit allocates memory to hold the code
for the application and the variables associated with the application. The conceptual view of storage of
an application and the variables related to the application is represented in Fig. 9.12.
1
> 1
•
|
Dynamic Storage
> < 1
Memory K
1
|
> 1
Alterable Data
Static Storage
Constant Data >
Memory
Source Code
i
Introduction to Embedded Systems
Memory manager allocates a memory segment to each application and the memory segment holds
the source code of the application as well as data related to the application. The actual program code
(executable machine code instructions) is stored at the lower address of the memory (at the beginning
address of the memory segment and stores upward). Constant data related to the application (e.'g. const
int x = 10;) is stored at the memory area just above the executable code. The alterable data (global and
static variables; e.g. static intj = 10; int k= 2; (global declaration), etc.) are stored at the ‘Alterable Data'
memory area. The size of the executable code, the number of constant data and alterable data in an ap¬
plication is always fixed and hence they occupy only a fixed portion of the memory segment. Memory
allocated for the executable code, constant data and alterable data together constitute the 'Static storage
memory (Fixed storage memory)'. The storage memory available within the memory segment exclud¬
ing the fixed memory is the ‘Dynamic storage memory'. Dynamic storage memory area is divided into
two separate entities namely 'stack' and 'heap'. 'Stack memory' normally starts at the high memory-
area (At the top address) of the memory segment and grows down. Within a memory segment, fixed
number of storage bytes are allocated to stack memory and the stack can grow up to this maximum al¬
located size. Stack memory is usually used for holding auto variables (variables local to a function),
function parameters and function return values. Depending upon the function calls/retum and auto vari¬
able storage, the size of the stack grows and shrinks dynamically. If at any point of execution the stack
memory exceeds the allocated maximum storage size, the application may crash and this condition is
called ‘Stack overflow'. The free memory region lying in between the stack and fixed memory area is
called 'heap'. Heap is the memory pool where storage for data is done dynamically as and when the
application demands. Heap is located immediately above the fixed memory area and it grows upward.
Any request for dynamic memory allocation by the program increases the size of heap (depending on
the availability of free memory within heap) and the free memory request decrements the size of heap
(size of already occupied memory within the heap area). Heap can be viewed as a ‘bank’ in real life,
application, where customers can demand for loan. Depending on the availability of money, bank may
allot loan to the customer and customer re-pays the loan to the bank when he/she is through with his/her
need. Bank uses the money repaid by a customer to allocate a loan to another customer—some kind of
rolling mechanism. ‘C’ language establishes the dynamic memory allocation technique through a set
of ‘Memory management library routines'. The most commonly used ‘C’ library functions for dynamic
memory allocation are ‘malloc’, ‘calloc’, ‘realloc’ and free'. The following sections illustrate the use of
these memory allocation library routines for allocating and de-allocating (freeing) memory-dynamically.
mallocO mallocQ function allocates a block of memory dynamically. The malloc() function reserves
a block of memory of size specified as parameter to the function, in the heap memory and returns a
pointer of type void. This can be assigned to a pointer of any valid type. The general form of using mal¬
locO function is given below.
where ‘pointer' is a pointer of type ‘pointerjype’. ‘pointerjype’ can be 'int', 'char', 'float' etc. mal¬
locO function returns a pointer of type ‘pointerjype’ to a block of memory with size 'no. of bytes’. A
typical example is given below.
ptr= (char *) malloc (.50) ; .
This instruction allocates 50 bytes (If there is 50 bytes of memory available in the heap area) and the
address of the first byte of the allocated memory in the heap area is assigned to the pointer ptr of type
char. It should be noted that the mallocQ function allocates only the requested number of bytes and it
i
Embedded Firmware Design and Development
will not allocate memory automatically for the units of the pointer in use. For example, if the program¬
mer wants to allocate memory for 100 integers dynamically, the following code
!?". x= (int‘*) .maloc (100); ....
will not allocate memory size for 100 integers, instead it allocates memory for just 100 bytes. In order
to make it reserving memory for 100 integer variables, the code should be re-written as
' x= (int': malloc (100 '* sizeof (int)) ;
mallocQ function can also be used for allocating memory for complex data types such as structure
pointers apart from the usual data types like ‘int’, ‘char’, ‘float’ etc. mallocQ allocates the requested
bytes of memory in heap in a continuous fashion and the allocation fails if there is no sufficient memory
available in continuous fashion for the requested number of bytes by the mallocQ functions. The return
value of mallocQ will be NULL (0) if it fails. Hence the return value of mallocQ should be checked to
ensure whether the memory allocation is successful before using the pointer returned by the mallocQ
function.
- E.g. int * ptr;
- ’ if ( (ptr— (int *) malToc(50 * sizeof (int)))')
* ' printf ("Memory allocated successfully");
:■ ' / else
printf ("Memory allocation failed");
Remember mallocQ only allocates required bytes of memory and will not initialise the allocated
memory. The allocated memory contains random data.
callocO The library function callocQ allocates multiple blocks of storage bytes and initialises each
allocated byte to zero. Syntax of callocQ function, is illustrated below.
,. 'pointer- =%..,tpoinfcsr_dfype, 5*) ..caLLoc" fin|,> .si-zetof bl'p&kL* 1 . - . v* * *«
where ‘pointer’ is a pointer of type ‘pointer_type\ ‘pointer_type’ can be ‘int’, ‘char’, ‘float’ etc. V
stands for the number of blocks to be allocated and ‘size of block’ tells the size of bytes required per
block. The calloc(n, size of block) function allocates continuous memory for V number of blocks with
‘size of block’ number of bytes per block and returns a pointer of type ‘pointertype’ pointing to the first
byte of the allocated block of memory. A typical example is given below.
ptr= (char *) oalloc (5.0,1)';
Above instruction allocates 50 contiguous blocks of memory each of size one byte in the heap mem¬
ory and assign the address of the first byte of the allocated memory region to the character pointer ‘ptr'.
Since mallocQ is capable of allocating only fixed number of bytes in the heap area regardless of the
storage type, callocQ can be used for overcomming this limitation as discussed below.
ptr= (int *) calloc(50,sizeof(int));
This instruction allocates 50 blocks of memory, each block representing an integer variable and ini¬
tialises the allocated memory to zero. Similar to the mallocQ function, callocQ also returns a ‘NULL’
pointer if there is not enough space in the heap area for allocating storage for the requested number of
memory by the callocQ function. Hence it is advised to check the pointer to which the callocQ assigns
Introduction to Embedded Systems
the address of the first byte in the allocated memory area. If callocQ function fails, the return value will
be ‘NULL’ and the pointer also will be ‘NULL’. Checking the pointer for ‘NULL’ immediately after
assigning with pointer returned by callocQ function avoids possible errors and application crash.
Features provided by callocQ can be implemented using mallocQ function as
pointer = (pointer^ type *) malloc (n * size of block) ;
memset(pointer, 0, n * size of block);
For example
ptr= (int *) mall oc (10 *• sizeof (int) ); ' - w- 'I’TU?
' ‘ memset (ptr,'“0, »n * size-of block) ;
The function memset (ptr, 0, n * size of. block) sets the memory block of size (n * size of block) with
starting address pointed by ‘ptr\ to zero.
free() The ‘C’ memory management library function free'Q is used for releasing or de-allocating the
memory allocated in the heap memory by mallocQ or callocQ functions. If memory is allocated dynami¬
cally, the programmer should release it if the dynamically allocated memory is no longer required for
any operation. Releasing the dynamically allocated memory makes it ready to use for other dynamic
allocations. The syntax offreeQ function is given below.
free (ptr)•„ ..... ,
‘ptr’ is the valid pointer returned by the callocQ or mallocQ function on dynamic memory allocation.
Use of an invalid pointer with function freeQ may result in the unexpected behaviour of the application.
1. s The dynamic memory allocated using mallocQ or callocQ functions should be released (deallocated)
usingfreefj%nciioni \ "• ^
2. Any use of a pointer which refers to freed memory ifade results in abnormal behaviour of application:
3. If the parameter to free () function is not a valid pointer, the application behaviour may be
unexpected.
reallocQ reallocQ function is used for changing the size of allocated bytes in a dynamically allocated
memory block. You-may come across situations where the allocated memory is not sufficient to hold the
required data or it is surplus in terms of allocated memory bytes. Both of these situations are handled
using reallocQ function. The reallocQ function changes the size of the block of memory pointed to, by
the pointer parameter to the number of bytes specified by the-modified size parameter and it returns a
new pointer to the block. The pointer specified by the pointer parameter must have been created with
the malloc, calloc, or realloc subroutines and not been de-allocated with the free or realloc subroutines.
Function reallocQ may shift the position of the already allocated block depending on the new size, with
preserving the contents of the already allocated block and returns a pointer pointing to the first byte of
the re-allocated memory block. reallocQ returns a void pointer if there is sufficient memory available
for allocation, else return a ‘NULL’ pointer. Syntax of realloc is given below.
realloc (pointer, modified sizef-;
?5 / Embedded firmware can be developed to run with the help of an embedded operating system or without an OS
The non-OS based embedded firmware execution runs the tasks in an infinite loop and this approaches known as
‘Super loop based’execution
/ / Low level language (Assembly code) or High Level language (C.CI + etc.) or a mix of both are used in embedded j
Y firmware development :i ■ : " •' ■ - ;
Y / In Assembly language based design, the firmware is developed using the Assembly language Instructions spe¬
cific to the target processor. The Assembly code is converted to machine specific code by an assembler.
b V In High Level language based design, the firmware is developed using High Level language like ‘C/C 1+’ and it
is converted to machine specific code by a compiler or cross-compiler
1 if?'®: Mixing of Assembly and High Level language can be done in three ways namely; mixing assembly routines with
a high level language like ‘C’, mixing high level language functions like ‘C’ functions with application written
f7 in assembly code and invoking assembly instructions inline from the high level code f 11| .
| Y Embedded ‘C’ can be considered as a subset of conventional ‘C’ language. Embedded C supports almost all'
r - ‘C’ instructions and incorporates a few target processor specific functions/instructions. The standard ANSI 1C’ -
Y Y library implementation is always tailored to the ta^et^rpdesSQr/controller libraryiiles in Embedded C >Y
J;;;/ Compiler is a tool for native platform application development, whereas cross-compiler is a tool for cross plat-;*:
ClT form application development ' 4 ” “ f
Embedded ‘C’ supports all the keywords, identifiers and data types, storage classes, arithmetic and logical ppera-
|,r lions, array and branching instructions supported by standard ‘C’ ■ ■
:• V structure is a collection Of data types. Arrays of structures are helpful in holding configuration data in embedded
applications. The bitfieldfeature of structures helps in bit declaration and bit'manipulation in embedded applica¬
tions
Y Pre-processor in ‘C’ is compiler/cross-compiler directives used by compiler/cross-compiler to filter the source
code before compilation/cross-compilation. Preprocessor directives falls into one of the categories: file inclusion,
compile control and macro substitution
Y ‘Read only’ variable in embedded applications are represented with the keyword const. Pointer to constant data
is a pointer which points to a data which is read only. A constant pointer is a pointer to a fixed memory location.
Constant pointer to constant data is a pointer pointing to a fixed memory location which is read only
Y A variable or memory location in embedded application, which is subject to asynchronous modification should
be declared with the qualifier volatile to prevent the compiler optimisation on the variable
Y A constant volatile pointer represents a Read only register (memory location) of a memory mapped device hi
embedded application
Y while(l) {};do {fwhile (l)\for (;;){} are examples for infinite loop setting instructions in embedded ‘C’.
Y The ISR contains the code for saving the current context, code for performing the operation corresponding to
the interrupt, code for retrieving the saved context and code for informing the processor that the processing of
interrupt is completed
C Recursive function is a function which calls it repeatedly. Functions which can be shared safely with several
processes concurrently are called re-entrant function.
C Dynamic memory allocation is the technique for allocating memory on a need basis for tasks. mallocQ, callocQ,
reallocQ andfree() are the ‘C’ library routines for dynamic memory management
Introduction to Embedded Systems
Keywords
Super Loop Model An embedded firmware design model which executes tasks in an infinite loop
at a predefined order '% ■
Assembly Language The human readable notation of‘machine language’
Machine Language Processor understandable language made up of Is and Os ,,
Mnemonics Symbols used in Assembly language for representing the machine language
.Assembief, A program to translate Assembly language program to ob
; ■; :vt Specially formattedj; ordered program collections of obje
Linker T he* A software for linking the various object files of a project
.
■w
on top of a particular operating system running otfig
architecture I ’ , 1* \ ’
. ...
Cross-compiler The software tools used in cross-platfonn development app
the application to a target processor specific code, which is different from the
processor architecture on which'the compiler is running
Function A self-contained and re-usable code snippet intended to perform ia particular '
task
Function Pointer Pointer variable pointing to a function
structure Variable holding a collection of data types (int, float, char, long, etc.) in C lan¬
guage \ ;
structure Padding The act of arranging the structure elements in memory in a way facilitating
increased execution speed , .' Y'A
A derived form of structure, which allocates memory only to the member vari¬
able of union requiring the maximum storage size on declaring a mnion van-
able
Pre-processor A compiler/cross-compiler directives used by compiler/ cross-compiler to filter
the source code before compilation/cross-compilation in ‘C’ language
Macro The ‘C’ pre-processor for creating portable inline code
A keyword used in ‘C’ language for informing the compiler/cross-compiler that
the variable is constant. It represents a ‘Read only’ variable
Dynamic Memory Allocation The technique for allocating memory on a need basis at run time
Stack The memory area for storing local variables, function parameters and function
return values and program counter, in the memory model for an application/
task
Static Memory Area The memory area holding the program code, constant variables, static and glob¬
al variables, in the memory model for an application/task
Heap Memory The free memory lying in between stack and static memory area, which is used
for dynamic memory allocation
Embedded Firmware Design and Development
Objective Questions
1. Which of the following is a processor understandable language?
(a) Assembly language (b) Machine language (c) High level language
2. Assembly language is the human readable notation of?
(a) Machine language (b) High level language (c) None of these
3. Consider the following piece of assembly code
ORG OOOOHf r ; ^
LJMP MATNjV r v r, ' “ -A;
Here ‘ORG' is a
(a) Pseudo-op (b) Label (c) Opcode (d) Operand
4. Translation of assembly code to machine code is performed by the
(a) Assembler (b) Compiler (c) Linker (d) Locater
5. A cross-compiler converts an embedded ‘C’ program to
(a) The machine code corresponding to the processor of the PC used for application development
(b) The machine code corresponding to a processor which is different from the processor of the PC used for
application development
6. ‘ptr’ is an integer pointer holding the address of qn integer variable say x which holds the value 10. Assume the
address of the integer variable x as 0xl2fF7c. What will be the output of the below piece of code? Assume the stor¬
age size of integer is 4
.VpV.>yj'A
ptr+=2;
//Print %he address holding, hy the' pointer?
printf ("Ox%x\n", ptr);
(a) 30^ (b) 4 (c) Compile error (cannot add two pointers)
(d) 0x2(5fef4
Introduction to Embedded Systems
10. ’’ptrl ’ is a char pointer holding the address of the char variable say x which holds the value 10.. Assume the address
of char variable x as 0x12ff7c. What will be the output of the following piece of code?
++*.ptrl;. .
printf("%x\n", ptrl); : ~ ' ,
*ptrl:++;
printf("%x\nf, *ptrl); ;
(a) 0x0b (b) The contents of memory location 0xl2fF7d (c) 0xl2ff7c
(d) 0xl2ff7d
13. ‘ptrl ’ is a char pointer holding the address of the char variable say x which holds the value 10. Assume the address
of char variable x as 0xl2ff7c. What will be the output of the following piece of code?
*ptrl++; . • ; . gfep
printf ("%x\n", ptrl); , 1 . ; i
(a) 0x0b (b) The contents of memory location Ox 12fF7d (c) 0xl2ff7c
(d) 0xl2ff7d
14. Which of the following is the string termination character?
(a) ‘\n’ (b) At’ (c) AO’ (d) ‘\a’
15. What is the output of the following piece of code?
(a) Hello (b) Hello World! (c) Compile error (d) World!
17. What is the output of the following piece of code?
(c) -1
Embedded Firmware Design and Development
(a) Hello (b) Hello World! (c) Compile error (d) World!
19. What is the output of the following piece of code?
(a) Hello (b) Hello World! (c) Compile error (d) World!
20. Consider the following structure declaration
typedef struct
Assuming the size of unsigned char as 1 byte, what will be the memory allocated for the structure?
_(a) 1 byte (b) 2 bytes (c) 4 bytes (d) 0 bytes
21. Consider the following structure declaration
typedef struct
Assuming the size of unsigned char as 1 byte, what will be the output of the following piece of code when com¬
piled for Keil C51 cross compiler
Assuming the size of unsigned char as 1 byte, what will be the output of the following piece of code when com
piled for Keil C51 cross compiler
What will be the output of the following piece of code? Assume the storage size of int as 2 and unsigned char as
1 —
un.ion_ichar int^char;
void nahn(void) > / ,
28. Which of the following pre-processor directive is used for indicating the end of a block following tiifdef <
ttelsel
(a) #define (b) #undef (e) #endif (d) #ifndef
29. Which of the following preprocessor directive is used for coding macros?
(a) #ifdef (b) #define (c) #undef (d) #endif
30. What will be the output of the following piece of code?
#define A 2+8 - , «
Idefine B , 2+3 ; +, .
void main (void) V ' : :
' '' ^ .■( n ...rf ^ '• * . ' ' '.
(a) 0
31. The instruction
const unsi
represents:
(a) Pointer to constant data (b) Constant pointer to data
(c) Constant pointer to constant data (d) None of these
32. The instruction
represents:
(a) Pointer to constant data (b) Constant pointer to data
(c) Constant pointer to constant data (d) None of these
33. The instruction
represents:
(a) Pointer to constant data (b) Constant pointer to data
(c) Constant pointer to constant data (d) None of these
34. The instruction
represents:
(a) Volatile pointer to data (b) Pointer to volatile data
(c) Volatile pointer to constant data (d) None of these
35. The instruction
represents:
(a) Volatile pointer to data (b) Pointer to volatile data
(c) Pointer to constant volatile data (d) None of these
36. The constant volatile variable in Embedded application represents a
(a) Write only memory location/register (b) Read only memory location/register
(c) Read/Write memory location/register
Introduction to Embedded Systems
37. What will be the output of the following piece of code? Assume the data bus width of the controller on which the
program is executed as 8 bits.
(a) 2 Bytes (b) 100 Bytes (c) 200 Bytes (d) 4 Bytes
40. Which of the following memory management routine is used for changing the size of allocated bytes in a dynami¬
cally allocated memory block
(a) mallocQ (b) reallocQ (c) callocQ (d) freeQ
17. What is ‘inline Assembly’? How is it different from mixing assembly language with ‘C’?
18. What is ‘pointer’ in embedded C programming? Explain its role in embedded application development.
19. Explain the different arithmetic and relational operations supported by pointers
20. What is ‘NULL’ Pointer? Explain its significance in embedded C programming
21. Explain the similarities and differences between strings and character arrays
22. Explain function in the Embedded C programming context. Explain the generic syntax of function declaration and
implementation
23. What is static function? What is the difference between static and global functions?
24. Explain the similarities and differences between function prototype.and function declaration
25. What is function pointer? How is it related to function? Explain the use of function pointers
26. Explain structure in the ‘Embedded C’ programming context. Explain the significance of structure over normal
variables
27. Explain the declaration and initialisation of structure variables
28. Explain the different operations supported by structures
29. What is structure pointer? What is the advantage of using structure pointers?
30. Explain ‘structure placement at absolute memory location’ and its advantage in embedded application develop¬
ment. Will it be possible to place a structure at absolute memory location in desktop application development?
Explain
31. What is structure padding? What are the merits and demerits of structure padding?
32. What is bit field? How bit field is useful in variant data access?
33. Explain the use of offsetojl) macro in structure operations.
34. What is union? What is the difference between union and structure?
35. Explain how union is useful in variant data access.
36. Explain the use of union in ‘Embedded C’ applications
37. What is pre-processor directive? How is a pre-processor directive instruction differentiated from normal program
code?
38. What are the different types of pre-processor directives available in ‘Embedded C’? Explain them in detail
39. What is macro in ‘Embedded C’ programming?
40. What is the difference between macros and functions?
41. What are the merits and drawbacks of macros'?
42. Write a macro to multiply two numbers
43. Explain the different methods of ‘constant data' declaration in ‘Embedded C’. Explain the differences between the
methods.
44. Explain the difference between ‘pointer to constant data' and ‘constant pointer to data' in ‘Embedded C’
programming. Explain the syntax for declaring both.
45. What is constant pointer to constant datal Where is it used in embedded application development?
46. Explain the significance of ‘volatile’ type qualifier in Embedded C applications. Which all variables need to be
declared as ‘ volatile' variables in Embedded C application?
47. What is ‘pointer to constant volatile data’? What is the syntax for defining a ‘pointer to constant volatile data’?
What is its significance in embedded application development?
48. What is volatile pointer? Explain the usage of ‘ volatile pointer' in ‘Embedded C’ application
49. Explain the different techniques for delay generation in ‘Embedded C’ programming. What are the limitations of
delay programming for super loop based embedded applications?
50. Explain the different bit manipulation operations supported by ‘Embedded C’
51. What is Interrupf! Explain its properties? What is its role in embedded application development?
52. What is Interrupt Vector Address and Interrupt Service Routine (ISR)? How are they related?
53. What is the difference between Interrupt Service Routine and Normal Service Routine?
54. Explain context switching, context saving and context retrieval in relation to Interrupts and Interrupt Service
Routine (ISR)
Introduction to Embedded Systems
55. What all precautionary measures need to be implemented in an Interrupt Service Routine (ISR)?
56. What is ‘recursion’? What is the difference between recursion and iteration? Which one is better?
57. What are the merits and drawbacks of ‘recursion’!
58. What is ‘reentrant’ function? What is its significance in embedded applications development?
59. What is the difference between ‘'recursive'’ and ‘reentrant’ function?
60. Explain the different criteria that need to be strictly met by a function to consider it as ‘reentrant’ function.
61. What is the difference between Static and Dynamic memory allocation?
62. Explain the different sections of a memory segment allocated to an application by the memory manager
63. Explain the different ‘Memory management library routines’ supported by C
64. Explain the difference between the library functions mallocQ and callocQ for dynamic memory allocation
Lab Assignments )
1 Write a‘C‘ program to create a ’reentrant’ function which removes the white spaces from a string which is 'sup¬
plied as input to the function and returns the new string without white spaces. The main ‘C’ function passes a
. string to the reentrant function and accepts the string with white spaces removed
2, Write a C program to place a character variable at memory location OxOGOFF and load it with O.xFE. Compile the
application using Microsoft Visual Studio compiler and rim,it on a desktop machine with Windows Operating
« i ;. L System. Record the output and explain th^reasonbehind the output behaviour
3. Write a small embedded C program to complement bit 5 (Assume bit numbering starts at 0) of the status register
s of a device, which is memory mapped to the CPU. The status register of the device is memory mapped at location
■ 0x3000. The data bus of the controller arid the status register of the device is 8bit wide 1
. 4. Write a small embedded C program to set bit 0 and clear bit 7 of the status register of a device, which is memory
"".' mapped to the CPU. The status register of the device is memory mapped at location 0x8000. The data bus of
the controller and the status register of the device is 8bil wide. The application should illustrate the usage of bit
manipulation operations. T
5. Write a small embedded C program to test the status of bit 5 of the status register and reset it if it is 1, of a device,
which is memory mapped to the CPU. The status register of the device is memory mapped at location 0x7000.
Ihe data bus of the controller and the status register of the device is 8bit wide. The application should illustrate
the usage of bit manipulation operations.
6. Write an ‘Embedded C’program for Keil C51 cross-compiler for transmitting a string data through the serial port
of 8051 microcontroller as per the following requirements
(a) Use structure to hold the communication parameters
(b) Use a structure array for holding the configurations corresponding to various baudrates (say 2400, 4800,
9600 and 195200)
(c) Write the code for sending a string to the serial port as function. The main routine invokes this function with
the string to send. Use polling of Transmit Interrupt for ensuring the sending of a character
|V Learn the basics of an operating system and the need for an operating system
}V- Learn the internals of Real-Time Operating System and the fundamentals of RTOS based embedded firm ware design
■/ Learn the basic kernel services of an operating system ' —
|V Learn about the classification of operating systems • X
byi Learn about the different real-time kernels and the features that make a kernel Real-Time
V Learn about tasks, processes and threads in the operating system context .
W* Learn about the structure of a process, the different states of a process, process life cycle and process management
■ / Learn the concept of multithreading, thread standards and thread scheduling
/ Understand the difference between multiprocessing and multitasking,
—S Learn about the different types of multitasking (Co-operative, Preemptive and Non-preemptive)
\ / Learn about the FCFS/FIFO, LCFS/LIFO, SJF and priority based task/process scheduling ■
/ Learn about the shortest remaining time (SRT), Round Robin and priority based preemptive task/process scheduling
rS: Learn about the different Inter Process Communication (IPC) mechanisms used by tasks/process to communicate
« and co-.operate each other in a multitasking environment
S Learn the different types of shared memory techniques (Pipes, memory mapped object, etc.) for IPC
/ Learn the different types of message passing techniques (Message queue, mailbox, signals, etc.) for IPC
S Learn the RPC based Inter Process Communication
S Learn the need for task synchronisation in a multitasking environment
S Learn the different issues related to the accessing of a shared resource by multiple processes concurrently
S Learn about 'Racing', 'Starvation', 'Livelock', 'Deadlock', 'Dining Philosopher's Problem', 'Producer-Consumer/Bound¬
ed Buffer Problem', 'Readers-Writers Problem' and 'Priority Inversion'
S Learn about the ‘Priority Inheritance' and 'Priority Ceiling' based Priority avoidance mechanisms
S Learn the need for task synchronisation and the different mechanisms for task synchronisation in a multitasking
environment
S Learn about mutual exclusion and the different policies for mutual exclusion implementation
- S Learn about semaphores, different types of semaphores, mutex, critical section objects and events for task synchro¬
nisation
S Learn about device drivers, their role in an operating system based embedded system design, the structure of a
device driver, and interrupt handling inside device drivers
S Understand the different functional and nonfunctional requirements that need to be addressed in the selection of
a Real-Time Operating System
382 Introduction to Embedded Systems
In the previous chapter, we discussed about the Super loop based task execution model for firmware
execution. The super loop executes the tasks sequentially in the order in which the tasks are listed
within the loop. Here every task is repeated at regular intervals and the task execution is non-real time.
As the number of task-increases, the time intervals at which a task gets serviced also increases. If some
of the tasks involve waiting for external events or I/O device usage, the task execution time also gets
pushed off in accordance with the ‘wait’ time consumed by the task. The priority in which a task is to
be executed is fixed and is determined by the task placement within the loop, in a super loop based ex¬
ecution. This type of firmware execution is suited for embedded devices where response time for a task
is not time critical. Typical examples are electronic toys and video gaming devices. Here any response
delay is acceptable and it will not create any operational issues or potential hazards. Whereas certain
applications demand time critical response to tasks/events and any delay in the response may become
catastrophic. Flight Control systems, Air bag control and Anti Locking Brake (ABS) systems for ve¬
hicles, Nuclear monitoring devices, etc. are typical examples of applications/devices demanding time
critical task response.
How the increasing need for time critical response for tasks/events is addressed in embedded applica¬
tions? Well the answer is
1. Assign priority to tasks and execute the high priority task when the task is ready to execute.
2. Dynamically change the priorities of tasks if required on a need basis.
3. Schedule the execution of tasks based on the priorities.
4. Switch the execution of task when a task is waiting for an external event or a system resource
including I/O device operation.
The introduction of operating system based firmware execution in embedded devices can address
these needs to a greater extent.
Process Management Process management deals with managing the processes/tasks. Process man¬
agement includes setting up the memory space for the process, loading the process’s code into the mem¬
ory space, allocating system resources, scheduling and managing the execution of the process, setting
Real-Time Operating System (RTOS) based Embedded System Design
User Applications
Application
programming
' T ' ' ' ' " • ' , L '
interface (API)
Process management I •?
lme.manageraeni
tm
iJnaMying hardware
Device driver
interface
■ ~ ~ '•■••.•■■■-- ■'■*■- •
/ up and managing the Process Control Block (PCB), Inter Process Communication and synchronisation,
process termination/deletion, etc. We will look into the description of process and process management
in a later section of this chapter.
5.Primary Memory Management The term primary memory refers to the volatile memory (RAM)
_ where processes are loaded and variables and shared data associated with each process are stored. The
Memory Management Unit (MMU) of the kernel is responsible for
• Keeping track of which part of the memory area is currently used by which process
• Allocating and De-allocating memory space on a need basis (Dynamic memory allocation).
File System Management File is a collection of related information. A file could be a program
(source code or executable), text files, image files, word documents, audio/video files, etc. Each of these
files differ in the kind of information they hold and the way in which the information is stored. The
file operation is a useful service provided by the OS. The file system management service of Kernel is
responsible for
• The creation, deletion and alteration of files
• Creation, deletion and alteration of directories
• Saving of files in the secondary storage memory (e.g. Hard disk storage)
• Providing automatic allocation of file space based on the amount of free space available
• Providing a flexible naming convention for the files
The various file system management operations are OS dependent. For example, the kernel of Micro¬
soft® DOS OS supports a specific set of file system management operations and they are not the same
as the file system operations supported by UNIX Kernel.
I/O System (Device) Management Kernel is responsible for routing the I/O requests coming from
different user applications to the appropriate I/O devices of the system. In a well-structured OS, the
direct accessing of I/O devices are not allowed and the access to them are provided through a set of
Application Programming Interfaces (APIs) exposed by the kernel. The kernel maintains a list of all
the I/O devices of the system. This list may be available in advance, at the time of building the kernel.
Some kernels, dynamically updates the list of available devices as and when a new device is installed
Introduction to Embedded Systems
(e.g. Windows XP kernel keeps the list updated when a new plug V play USB device is attached to
the system). The service ‘Device Manager’ (Name may vary across different OS kernels) of the kernel
is responsible for handling all I/O device related operations. The kernel talks to the I/O device through
a set of low-level systems calls, which are implemented in a service, called device drivers. The device
drivers are specific to a device or a class of devices. The Device Manager is responsible for
• Loading and unloading of device drivers
• Exchanging information and the system specific control signals to and from the device
Secondary Storage Management The secondary storage management deals with managing the
secondary storage memory devices, if any, connected to the system. Secondary memory is used as
backup medium for programs and data since the main memory is volatile. In most of the systems, the
secondary storage is kept in disks (Hard Disk). The secondary storage management service of kernel
deals with
• Disk storage allocation
• Disk scheduling (Time interval at which the disk is activated to backup data)
• Free Disk space management
Protection Systems Most of the modem operating systems are designed in such a way to support
multiple users with different levels of access permissions (e.g. Windows XP with user permissions like
‘Administrator’, ‘Standard’, ‘Restricted’, etc.). Protection deals with implementing the security policies
to restrict the access to both user and system resources by different applications or processes or users.
In multiuser supported operating systems, one user may not be allowed to view or modify the whole/
portions of another user’s data or profile details. In addition, some application may not be granted with
permission to make use of some of the system resources. This kind of protection is provided by the
protection services running within the kernel.
Interrupt Handler Kernel provides handler mechanism for all extemal/intemal interrupts generated
by the. system.
These are some of the important services offered by the kernel of an operating system. It does not
mean that a kernel contains no more than components/services explained above. Depending on the type
of the operating system, a kernel may contain lesser number of components/services or more number
of components/services. In addition to the components/services listed above, many operating systems
offer a number of add-on system components/services to the kernel. Network communication, network
management, user-interface graphics, timer services (delays, timeouts, etc.), error handler, database
management, etc. are examples for such components/services. Kernel exposes the interface to the vari¬
ous kernel applications/services, hosted by kernel, to the user applications through a set of standard
Application Programming Interfaces (APIs). User applications can avail these API calls to access the
various kernel application/services.
10.1.1.1 Kernel Space and User Space As we discussed in the earlier section, the applications/
services are classified into two categories, namely: user applications and kernel applications. The pro¬
gram code corresponding to the kernel applications/services are kept in a contiguous area (OS de¬
pendent) of primary (working) memory and is protected from the un-authorised access by user pro¬
grams/applications. The memory space at which the kernel code is located is known as ‘Kernel Space'.
Similarly, all user applications are loaded to a specific area of primary memory and this memory area
is referred as ‘User Space'. User space is the memory area where user applications are loaded and ex¬
ecuted. The partitioning of memory into kernel and user space is purely Operating System dependent.
Real-Time Operating System (RTOS) based Embedded System Design
Some OS implements this kind of partitioning and protection whereas some OS do not segregate the
kernel and user application code storage into two separate areas. In an operating system with virtual
- memory support, the user applications are loaded into its corresponding virtual memory space with
demand paging technique; Meaning, the entire code for the user application need not be loaded to the
main (primary) memory at once; instead the user application code is split into different pages and these
pages are loaded into and out of the main memory area on a need basis. The act of loading the code into
.... .and out of the main memory is termed as ‘Swapping’. Swapping happens between the main (primary)
memory and secondary storage memory. Each process run in its own virtual memory space and are not
allowed accessing the memory space corresponding to another processes, unless explicitly requested by
the process. Each process will have certain privilege levels on accessing the memory of other processes
and based on the privilege settings, processes can request kernel-to map another process’s memory to its
Town or share through some other mechanism. Most of the operating systems keep the kernel application
code in main memory and it is not swapped out into the secondary memory.
10.1.1.2 Monolithic Kernel and Microkernel As we know, the kernel forms the heart of an op-
' erating system. Different approaches are adopted for building an Operating System kernel. Based on the
kernel design, kernels can be classified into ‘Monolithic’ and ‘Micro’.
Monolithic Kernel In monolithic kernel architecture, all kernel services run in the kernel space.
; Here all kernel modules run within the same memory space under a single kernel thread. The tight inter¬
nal integration of kernel modules in monolithic kernel architecture allows the effective utilisation of the .
low-level features of the underlying system. The major drawback of monolithic kernel is that any error
or failure in any one of the kernel modules leads to the crashing of the entire kernel application. LINUX,
SOLARIS, MS-DOS kernels are examples of monolithic kernel. The architecture representation of a
monolithic kernel is given in Fig. 10.2. —
Microkernel The microkernel design incorporates only the essential set of,Operating System ser¬
vices into the kernel. The rest of the Operating System services are implemented in programs known
as ‘Servers’ which runs in user space. This provides a’highly modular design and OS-neutral abstrac¬
tion to the kernel. Memory management, process management, timer systems and interrupt handlers
Introduction to Embedded Systems
Depending on the type of kernel and kernel services, purpose and type of computing systems where the
OS is deployed and the responsiveness to applications, Operating Systems are classified into different
types.
There is no universal definition available for the term ‘Real-Time’ when it is used in conjunction with
operating systems. What ‘Real-Time’ means inOperating System context is still a debatable topic and
there are many definitions available. In a broad sense, ‘Real-Time’ implies deterministic timing behav¬
iour. Deterministic timing behaviour in RTQS context means the OS services consumes only known and
expected amounts of time regardless the number of services. A Real-Time Operating System or RTOS
implements policies and rules concerning time-critical allocation of a system’s resources. The RTOS
Real-Time Operating System (RTOS) based Embedded System Design
decides which applications should run in which order and how much time needs to be allocated for each
application. Predictable performance is the hallmark of a well-designed RTOS. This is best achieved
by the consistent application of policies and rules. Policies guide the design of an RTOS. Rules imple¬
ment those policies and resolve policy conflicts. Windows CE, QNX, VxWorks MicroC/OS-II, etc. are
examples of Real-Time Operating Systems (RTOS).
10.2.2.1 The Real-Time Kernel The kernel of a Real-Time Operating System is referred as Real.
Time kernel. In complement to the conventional OS kernel, the Real-Time kernel is highly specialised
and it contains only the minimal set of services required for running the user applications/tasks. The
basic functions of a Real-Time kernel are listed below:
• Task/Process management
• Task/Process scheduling
• Task/Process synchronisation
• Error/Exception handling
• Memory management
• Interrupt handling
• Time management
Task/Process management Deals with setting up the memory space for the tasks, loading the
task’s code into the memory space, allocating system resources, setting up a Task Control Block (TCB)
for the task and task/process termination/deletion. A Task Control Block (TCB) is used for holding the
information corresponding to a task. TCB usually contains the following set of information.
Task ID: Task Identification Number
Task State: The current state of the task (e.g. State = ‘Ready’ for a task which is ready to execute)
Task Type: Task type. Indicates what is the type for this task. The task can be a hard real time or soft real
time or background task.
Task Priority: Task priority (e.g. Task priority = 1 for task with priority - 1)
Task Context Pointer: Context pointer. Pointer for context saving
Task Memory Pointers: Pointers to the code memory, data memory and stack memory for the task
Task System Resource Pointers: Pointers to system resources (semaphores, mutex, etc.) used by the task
Task Pointers: Pointers to other TCBs (TCBs for preceding, next and waiting tasks)
Other Parameters Other relevant task parameters
The parameters and implementation of the TCB is kernel dependent. The TCB parameters vary across
different kernels, based on the task management implementation. Task management service utilises the
TCB of a task in the following way
• Creates a TCB for a task on creating a task
• Delete/remove the TCB of a task when the task is terminated or deleted
• Reads the TCB to get the state of a task
• Update the TCB with updated parameters on need basis (e.g. on a context switch)
• Modify the TCB to change the priority of the task dynamically
Task/Process Scheduling Deals with sharing the jZlPU among various tasks/processes. A kernel
application called ‘Scheduler’ handles the task scheduling. Scheduler is nothing but an algorithm imple¬
mentation, which performs the efficient and optimal scheduling of tasks to provide a deterministic
behaviour. We will discuss the various types of scheduling in a later section of this chapter.
Introduction to Embedded Systems
Error/Exception Handling Deals with registering and handling the errors occurred/exceptions
raised during the execution of tasks. Insufficient memory, timeouts, deadlocks, deadline missing, bus
error, divide by zero, unknown instruction execution, etc. are examples of errors/exceptions. Errors/Ex¬
ceptions can happen at the kernel level services or at task level. Deadlock is an example for kernel level
exception, whereas timeout is an example for a task level exception. The OS kernel gives the informa¬
tion about the error in the form of a system call (API). GetLastError() API provided by Windows CE
RTOS is an example for such a system call. Watchdog timer is a mechanism for handling the timeouts
for tasks. Certain tasks may involve the waiting of external events from devices. These tasks will wait
infinitely when the external device is not responding and the task will generate a hang-up behaviour. In
order to avoid these types of scenarios, a proper timeout mechanism should be implemented. A watch¬
dog is normally used in such situations. The watchdog will be loaded with the maximum expected wait
time for the event and if the event is not triggered within this wait time, the same is informed to the task
and the task is timed out. If the event happens before the timeout, the watchdog is resetted.
Memory Management Compared to the General Purpose Operating Systems, the memory manage¬
ment function of an RTOS kernel is slightly different. In general, the memory allocation time increases
depending on the size of the block of memory needs to be allocated and the state of the allocated
memory block (initialised memory block consumes more allocation time than un-initialised memory
block). Since predictable timing and deterministic behaviour are the primary focus of an RTOS, RTOS
achieves this by compromising the effectiveness of memory allocation. RTOS makes use of ‘block'
based memory allocation technique, instead of the usual dynamic memory allocation techniques used
by the GPOS^RTOS kernel uses blocks of fixed size of dynamic memory and the block is allocated for
a task on a need basis. The blocks are stored in a ‘'Free Buffer Queue'. To achieve predictable timing
and avoid the timing overheads, most of the RTOS kernels allow tasks to access any of the memory
block's without any memory protection. RTOS kernels assume that the whole design is proven correct
and protection is unnecessary. Some commercial RTOS kernels allow memory protection as optional
and the kernel enters a fail-safe mode when an illegal memory access occurs.
A few RTOS kernels implement Virtual Memoryt concept for memory allocation if the system sup¬
ports secondary memory storage (like HDD and FLASH memory). In the ‘block' based memory al¬
location, a block of fixed memory is always allocated for tasks on need basis and it is taken as a unit.
Hence, there will not be any memory fragmentation issues. The memory allocation can be implemented
as constant functions and thereby it consumes fixed amount of time for memory allocation. This leaves
the deterministic behaviour of the RTOS kernel untouched. The ‘block' memory concept avoids the
garbage collection overhead also. (We will explore this technique under the MicroC/OS-II kernel in a
t Virtual Memory is an imaginary memory supported by certain operating systems. Virtual memory expands the address space avail¬
able to a task beyond the actual physical memory (RAM) supported by the system. Virtual memory is implemented with the help of a
Memory Management Unit (MMU) and ‘memory paging’. The program Memory for a task can be viewed as different pages and the
page corresponding to a piece of codethat needs to be executed is loaded into.the main physical memory (RAM). When a memory page
is no longer required, it is moved, out to, secondary storage memory and another page which contains the code snippet to be executed is
loaded into the main memory. This memory movement technique is known as. demand paging. The MMU handles the demand paging
and converts the virtual address of a location in a pageto corresponding physical address in the RAM.
Real-Time Operating System (RTOS) based Embedded System Design
latter chapter).The ‘block’ based memory allocation achieves deterministic behaviour with the trade-of
limited choice of memory chunk size and suboptimal memory usage.
Interrupt Handling Deals with the handling of various types of interrupts. Interrupts provide Real-
Time behaviour to systems. Interrupts inform the processor that an external device or an associated
task requires immediate attention of the CPU. Interrupts can be either Synchronous or Asynchronous.
Interrupts which occurs in sync with the currently executing task is known as Synchronous interrupts.
Usually the software interrupts fall under the Synchronous Interrupt category. Divide by zero, memory
segmentation error, etc. are examples of synchronous interrupts. For synchronous interrupts, the inter¬
rupt handler runs in the same context of the interrupting task. Asynchronous interrupts are interrupts,
which occurs at any point of execution of any task, and are not in sync with the currently executing task.
The interrupts generated by external devices (by asserting the interrupt line of the processor/controller
to which the interrupt line of the device is connected) connected to the processor/controller, timer over¬
flow interrupts, serial data reception/ transmission interrupts, etc. are examples for asynchronous inter¬
rupts. For asynchronous interrupts, the interrupt handler is usually written as separate task (Depends
on OS kernel implementation) and it runs in a different context. Hence, a context switch happens while
handling the asynchronous interrupts. Priority levels can be assigned to the interrupts and each inter¬
rupts can be enabled or disabled individually. Most of the RTOS kernel implements ‘Nested Interrupts’
architecture. Interrupt nesting allows the pre-emption (interruption) of an Interrupt Service Routine
(ISR), servicing an interrupt, by a high priority interrupt.
/
Time Management Accurate time management is essential for providing precise time reference for
all applications. The time reference to kernel is provided by a high-resolution Real-Time Clock (RTC)
hardware chip (hardware timer). The hardware timer is programmed to interrupt the processor/control¬
ler at a fixed rate. This timer interrupt is referred as ‘Timer tick’. The ‘Timer tick’ is taken as the timing
reference by the kernel. The ‘Timer tick’ interval may vary depending on the hardware timer. Usually
the ‘Timer tick’ varies in the microseconds range. The time parameters for tasks are expressed as the
multiples of the ‘Timer tick'.
The System time is updated based on the ‘Timer tick’. If the System time register is 32 bits wide and the
‘ Timer tick’ interval is 1 microsecond, the System time register will reset in
232 * 10-6/ (24 * 60 * 60) = 49700 Days = ~ 0.0497 Days =1.19 Hours
If the ‘Timer tick’ interval is 1 millisecond, the system time register will reset in
232 * 10-3 / (24 * 60 * 60) = 497 Days = 49.7 Days = ~ 50 Days
The ‘Timer tick’ interrupt is handled by the ‘Timer Interrupt’ handler of kernel. The ‘Timer tick’ interrupt
can be utilised for implementing the following actions.
• Save the current context (Context of the currently executing task).
• Increment the System time register by one. Generate timing error and reset the System time regis¬
ter if the timer tick count is greater than the maximum range available for System time register.
• Update the timers implemented in kernel (Increment or decrement the timer registers for each
timer depending on the count direction setting for each register. Increment registers with count di¬
rection setting = ‘count up’ and decrement registers with count direction setting = ‘count down’).
• Activate the periodic tasks, which are in the idle state.
• Invoke the scheduler and schedule the tasks again based on the scheduling algorithm.
• Delete all the terminated tasks and their associated data structures (TCBs)
• Load the context for the first task in the ready queue. Due to the re-scheduling, the ready task might
be changed to a new one from the task, which was preempted by the ‘Timer Interrupt’ task. \
i
Introduction to Embedded Systems
Apart from these basic fruitions, some RTOS provide other functionalities also (Examples are file
management and network functions). Some RTOS kernel provides options for selecting the required
kernel functions at the time of building a kernel. The user can pick the required functions from the set
of available functions and compile the same to generate the kernel binary. Windows CE is a typical ex¬
ample for such an RTOS. While building the target, the user can select the required components for the
kernel. j
10.2.2.2 Hard Real-Time Real-Time Operating Systems that strictly adhere to the timing con¬
straints for a task is referred as ‘Hard Real-Time’ systems. A Hard Real-Time system must meet the
deadlines for a task without any slippage. Missing any deadline may produce catastrophic results for
Hard Real-Time Systems], including permanent data lose and irrecoverable damages to the systemAisers.
Hard Real-Time systems emphasise the principle ‘A late answer is a wrong answer’. A system can have
several such tasks and the key to their correct operation lies in scheduling them so that they meet their
time constraints. Air bag control systems and Anti-loci^ Brake Systems (ABS) of vehicles are typical
examples for Hard Real-Time Systems. The Air bag control system should be into action and deploy the
air bags when the vehicle meets a severe accident. Ideally speaking, the time for triggering the air bag
deployment task, when an accident is sensed by the Air bag control system, should be zero and the air
bags should be deployed exactly within the time frame, which is predefined for the air bag deployment
task. Any delay in the deployment of the air bags makes the life of the passengers under threat. When
the air bag deployment task is triggered, the currently executing task must be pre-empted, the air bag
deployment task should be brought into execution, and the necessary I/O systems should be made read¬
ily available for the air bag deployment task. To meet the strict deadline, the time between the air bag
deployment event triggering and start of the air bag deployment task execution should be minimum, ide¬
ally zero. As a mle of thumb, Hard Real-Time Systems does not implement the virtual memory model
for handling the memory. This eliminates the delay in swapping in and out the code corresponding to the
task to and from the primary memory. In general, the presence of Human in the loop (HITL) for tasks
introduces unexpected delays in the task execution. Most of the Hard Real-Time Systems are automatic
and does not contain a ‘human in the loop’.
10.2.2.3 Soft Real-Time Real-Time Operating System that does not guarantee meeting deadlines,
but offer the best effort to meet the deadline are referred as ‘Soft Real-Time’ systems. Missing deadlines
for tasks are acceptable for a Soft Real-time system if the frequency of deadline missing is within the
compliance limit of the Quality of Service (QoS). A Soft Real-Time system emphasises the principle ‘A
late answer is an acceptable answer, but it could have done bit faster'. Soft Real-Time systems most of¬
ten have a ‘human in the loop (HITLf. Automatic Teller Machine (ATM) is a typical example for Soft-
Real-Time System. If the ATM takes a few seconds more than the ideal operation time, nothing fatal
happens. An audio-video playback system is another example for Soft Real-Time system. No potential
damage arises if a sample comes late by fraction of a second, for playback.
for the program. Task is also known as ‘ Job’ in the operating system context. A program or part of it in
execution is also called a ‘Process'. The terms ‘Task', ‘Job' and ‘Process' refer to the same entity in the
operating system context and most often they are used interchangeably.
10.3.1 Process
A ‘Process' is a program, or part of it, in execution. Process is also known as an instance of a program in
execution. Multiple instances of the same program can execute simultaneously. A process requires vari¬
ous system resources like CPU for executing the process; memory for storing the code corresponding to
the process and associated variables, I/O devices for information exchange, etc. A process is sequential
in execution.
10.3.1.1 The Structure of a Process The concept of ‘Process' leads to concurrent execution
(pseudo parallelism) of tasks and thereby the efficient utilisation of the CPU-and other system resources.
Concurrent execution is achieved through the sharing of CPU among the processes. A process mimics
a processor in properties and holds a set of registers, process status, a Program Counter (PC) to point to
the next executable instruction of the process, a stack for holding the local variables associated with the
process and the code corresponding to the process. This can be visualised as shown in Fig. 10.4.
A process which inherits all the properties of the CPU can be considered as a virtual processor, await¬
ing its turn to have its properties switched into the physical processor. When the process gets its turn, its
registers arid the program counter register becomes mapped to the physical registers of the CPU. From
a memory perspective, the memory occuoied by the process is segregated into three regions, namely,
Stack memory, Data memory and Code memory (Fig. 10.5).
The ‘Stack’ memory holds all temporary data such as variables local to the process. Data memory
holds all global data for the process. The code memory contains the program code (instructions) cor¬
responding to the process. On loading a process into the main memory, a specific area of memory is
allocated for the process. The stack memory usually starts (OS Kernel implementation dependent) at
Introduction to Embedded Systems
the highest memory address from the memory area allocated for
the process. Say for example, the memory map of the memory area
allocated for the process is 2048 to 2100, the stack memory starts at
address 2100 and grows downwards to accommodate the variables
local to the process.
ID. 3.1.2 Process States and State Transition The creation
of a process to its termination is not a single step operation. The
process traverses through a series of states during its transition from
the newly created state to the terminated state. The cycle through
which a process changes its state from ‘newly created’ to ‘execu¬
tion completed' is known as ‘Process Life Cycle'. The various states
through which a process traverses through during a Process Life
Cycle indicates the current status of the process with respect to time
and also provides information on what it is allowed to do next. Fig¬
ure 10.6 represents the various states associated with a process.
The state at which a process is being created is referred as
‘Created State’. The Operating System recognises a process in the
‘‘Created State' but no resources are allocated to
the process. The state, where a process is incept¬
ed into the memory and awaiting the processor
time for execution, is known as ‘Ready State'.
At this stage, the process is placed in the ‘Ready
Incepted into memory
list' queue maintained by the OS. The state where
in the source code instructions corresponding to
the process is being executed is called ‘Running
State'. Running state is the state at which the
process execution happens. ‘Blocked State/Wait
State' refers to a state where a running process
Scheduled for
is temporarily suspended from execution and
does not have immediate access to resources. The
blocked state might be invoked by various condi¬
tions like: the process enters a wait state for an
event to occur (e.g. Waiting for user inputs such
as keyboard input) or waiting for getting access
to a shared resource (will be discussed at a later Running
resentation. The states associated with a task may be known with a different name or there may be more
or less number of states than the one explained here under different OS kernel. For example, under
VxWorks’ kernel, the tasks may be in either one or a specific combination of the states READY, PEND,
DELAY and SUSPEND. The PEND state represents a state where the task/process is blocked on wait¬
ing for I/O or system resource. The DELAY state represents a state in which the task/process is sleeping
and the SUSPEND state represents a state where a task/process is temporarily suspended from execu¬
tion and not available for execution. Under MicroC/OS-II kernel, the tasks may be in one of the states,
DORMANT, READY, RUNNING, WAITING or INTERRUPTED. The DORMANT state represents
the ‘Created’ state and WAITING state represents the state in which a process waits for shared resource
or I/O access. We will discuss about the states and state transition for tasks under VxWorks and uC/OS-
II kernel in a later chapter.
10.3..1.3 Process Management Process management deals with the creation of a process, setting
up the memory space for the process, loading the process’s code into the memory space, allocating sys¬
tem resources, setting up a Process Control Block (PCB) for the process and process termination/dele¬
tion. For more details on Process Management, refer to the section ‘Task/Process management’ given
under the topic ‘The Real-Time Kernel’ of this chapter.
>
10.3.2 Threads ■ Stack memory for Thread 1
PO
wit
Task/Process dar
PO
Code memory
| VC
Data memory
*
ere
Stack ere
the
Registers Registers Registers av
Thread 2 TbrCaj 3
Thi
Thread 1
■void main (void.) int ChildThread 1 int. ChildThread.2 (ne
(void) (void)' - ' ' 3
'//Or e at a ~ych i 1 d
thread 1 //Do something
CreateThread (NULL,
;i000, (LPTHREAD^STA
. RTJROUTINE ).^ _
■ChildThread 1, NULL ,.-r bio
//Create child
ne i
ttfo?ead*2m
S
Wr
5 ti
s
• /
;
r&'str'x
10.3.2.2 Thread Standards Thread standards deal with the different standards available for thread
creation and management. These standards are utilised by the operating systems for thread creation and |1
thread management. It is a set of thread class libraries. The commonly available thread class libraries
are explained below.
Real-Time Operating System (RTOS) based Embedded System Design
, -n, \
POSIX Threads: POSIX stands for Portable Operating System Interface. The POSIX.4 standard deals
with the Real-Time extensions and POSIX.4a standard deals with thread extensions. The POSIX stan¬
dard library for thread creation and management is ' Pthreads'. ‘P threads' library defines the set of
POSIX thread creation and management functions in ‘C’ language.
The primitive
;■ int pthread_create(pthreadyt.*new_thread_ID, const pthread_attr_t *attribute,
void * (*stnr: function)(void *), void *arguments);
W&..: ...
creates a new thread for running the function start_ function. Here pthreadj is the handle to the newly
created thread and pthread_attrj is the data type for holding the thread attributes, 'start Junction’ is
the function the thread is going to execute and arguments is the arguments for 'start Junction’ (It is
a void * in the above example). On successful creation of a Pthread, p threadjreateQ associates the
Thread Control Block (TCB) corresponding to the newly created thread to the variable of type pthreadj
(newjhreadJD in our example).
The primitive
hint pthread_j.oin (pthread_t new_thread,void * *thread_status);
blocks the current thread and waits until the completion of the ^thread pointed by it (In this example
newjhread) 1
All the POSIX ‘thread calls’ returns an integer. A return value of zero indicates the success of the call.
It is always good to check the return value of each call.
Write a multithreaded application to print “Hello I’m in main thread” from the main thread and “Hello I’m in new thread”
5 times each, using the pthread_createQ andpthreadJoinQ POSIX primitives.
J int i, j;
(.•' for ( j= 0; j .< 5; j++ )
11, 1
;v: printf("Hello I'm in new threadin'' ).;
u //Wait1for some' time. Do nothing
fj/ //The fallowing line o'f. code can be replaced with
ft //OS supported delay function like sleep (), delay !) etc... '
■ ; ._ ■
$f' for ( i= 0; i < 10000; i+f ) ;,
1; 1 ; ..
W- return NULL; ‘
I • -/f - '
Introduction to Embedded Systems
//***********■*******************************************************
//Start of main thread “ ' ' I
■ int; main (■ void ) « • %t* V- , . pi„ v | ..
{ - . ■- .. ‘
int i, j;
pthread L tcb;
//Create the new thread for'executing ncw_thread function
If (pthrjepd_create ( Stclg, NULL, new_thread, NULL )T ,
Win32 Threads Win32 threads are the threads supported by various flavours of Windows Operating
Systems. The Win32 Application Programming Interface (Win32 API) libraries provide the standard set
of Win32 thread creation and management functions. Win32 threads are created with the API
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttribut.es, DWORD dwStack-
^ Size, LPTHREAD__START__ROUTINE IpStartAddress, LPVOID IpParameter, DWORD dwCre-
i. ationFlags, LPDWORD lpThreadld
The parameter IpThreadAttributes defines the security attributes for .the thread and dwStackSize de¬
fines the stack size for the thread. These" two parameters are not supported by the Windows CE Real-
Time Operating Systems and it should be kept as NULL and 0 respectively in a CreateThread API Call.
The other parameters are
IpStartAddress: Pointer to the function which is to be executed by the thread.
IpParameter: Parameter specifying an application-defined value that is passed to the thread routine.
dwCreationFlags: Defines the state of the thread when it is created. Usually it is kept as 0 or CRE-
ATE_SUSPENDED implying the thread is created and kept at the suspended state.
lpThreadld: Pointer to a DWORD that receives the identifier for the thread.
On successful creation of the thread, CreateThread returns the handle to the thread and the thread identi¬
fier.
The A¥lGetCurrentThread(void)rttumst\\ehand\eofthQC\mQntthrea.dd.ndGetCurrentThreadId(void)
returns its ID. GetThreadPriority (HANDLE hTbread) API returns an integer value representing the cur¬
rent priority of the thread whose handle is passed as hThread. Threads are always created with normal
priority (THREAD_PRIORITY_NORMAL. Refer MSDN documentation for the different thread pri¬
orities and their meaning). SetThreadPriority (HANDLE hThread, int nPriority) API is used for setting
the priority of a thread. The first parameter to this function represents the thread handle and the second
one the thread priority.
For Win32 threads, the normal thread termination happens when an exception occurs in the thread,
or when the thread’s execution is completed or when the primary thread or the process to which the
thread is associated is terminated. A thread can exit itself by calling the ExitThread (DWORD dwEx-
itCode) API. The parameter dwExitCode sets the exit code for thread termination. Calling ExitThread
API frees all the resources utilised by the thread. The exit code of a thread can be checked by other
threads by calling the GetExitCodeThread (HANDLE hThread, LPDWORD IpExitCode). Terminate-
Thread (HANDLE hThread, DWORD dwExitCode) API is used for terminating a thread from another
thread. The handle hThread indicates which thread is to be terminated and dwExitCode sets the exit code
for the thread. This API will not execute the thread termination and clean up code and may not free the
resources occupied by the thread. TerminateThread is a potentially dangerous call and it should not be
used in normal conditions as a mechanism for terminating a thread. Use this call only as a final choice.
SuspendThread(HANDLE hThread) API can be used for suspending a thread from execution provided
the handle hThread possesses THREAD SUSPEND RESUME access right. If the SuspendThread API
call succeeds, the thread stops executing and increments its internal suspend count. The thread becomes
suspended if its suspend count is greater than zero. The SuspendThread function is primarily designed
for use by debuggers. One must be cautious in using this API for the reason it may cause deadlock condi¬
tion if the thread is suspended at a stage where it acquired a mutex or shared resource and another thread
tries to access the same. The ResumeThread(HANDLE hThread) API is used for resuming a suspended
thread. The ResumeThread API checks the suspend count of the specified thread. A suspend count of
Introduction to Embedded Systems
zero indicates that the specified thread is not currently in the suspended mode. If the count is not zero,
the count is decremented by one and if the resulting count value is zero, the thread is resumed. The
API Sleep (DWORD dwMilliseconds) can be used for suspending a thread for the duration specified in
milliseconds by the Sleep function. The Sleep call is initiated by the thread.
...
Example
"
2 j
Write a multithreaded application using Win32 APIs to set up a counter in the main thread and secondary thread to count
from 0 to 10 and print the counts from both the threads. Put a delay of 500 ms in between the successive printing in both
the threads.
void ChildThread(void) -
' {
, L char- :i/:.,'A.; S/x....... .t »r,-y AAAA>..
for (i=0;i<=10;++i) . - - 1-
A ... .A" AyAA ":A;. ' •; Dv .A. Ai,
1 f \ .?* ^ \ > ■f1 \ t ' i ' & S * * «"'■* A|$ >. V ♦ -if ^
printf ("Executing Child Thread:Counter- = *'%d\n"f i)“; ' _ * >
Sieep (500) ? - f?
//***^*********************^*****.****.**^*************************
//Primary thread; 5 ; ’1 i f Ai-4 - • ~ -
//********** * * * ******* ******* ******************* '*.*.*"* if**.*-* *:*.* *,* **
• .. . • * •' .■ . A* ■ A" • , ■ »■ A--' a ;
int main (-int argc, char* argv[‘. ) : -
scheduling policies implemented by the windows op¬ F.Vr i :ui i l«* t tiii.li ihre.-id: Counter “ u
<:>■ :i» l L.j. > \A J h:i.y.'i}. Counter ru f‘
erating system for thread scheduling. You may get the Hr* i.?» T'hi.v- rv’i *. Counter S! •V.
,-T
[!!>••• .!»’/ Qri.'i*! TbrtW’.rb Counter
same output or a different output each time yoti run the Exe-ul: in<; Ma.in 1 }«*'<■•. :>a: Counter-. - •T
application. Ex (5 >:.«(: i nq Slain Tin-ea.d' Counter- -• «
Exceut:! mi Chi. Ifl Coun C-xv =- si
Exectil ;ng Jin in ihvi :■*'}. t Counter' :::
Java Threads Java threads are the threads Exct. ul inn Child Thread. Counter V
ExeOTtL i.Hff Ha in Thread: Counter “ i
supported by Java programming Language. Chi ld f hrurni r Coun tf. s: i
Frci':? n,») y fcoy to t.».,nl:inue„
The java thread class ‘ Thread’ is defined in the
package ‘java.lang’. This package needs to be
imported for using the thread creation functions ,-N
supported by the Java thread class. There are (Wr10-9.) ap^hcltion1 GWin32 Multithreaded
two ways of creating threads in Java: Either by
extending the base ‘Thread’ class or by implementing an interface. Extending the thread class allows
inheriting the methods and variables of the parent class (Thread class) only whereas interface allows a
way to achieve the requirements for a set of classes. The following piece of code illustrates the imple¬
mentation of Java threads with extending the thread base class ‘ Thread’.
-import java.lang,*;. . , v ‘n - \ ;
public class MyThread extends Thread
life' •.*!?*"
m public void'.run () .
m { r "-KK'lffyx V; -
N,w,‘ * System.out.println ("Hello from MyThread!");
•+ 1
public static void main (String args[])
The above piece of code creates a new class MyThread by extending the base class Thread. It also
overrides the run() method inherited from the base class with its own run() method. The run() method
of MyThread implements all the task for the MyThread thread. The method startQ moves the thread to
a pool of threads waiting for their turn to be picked up for execution by the scheduler. The thread is said
to be in the "Ready’ state at this stage. The scheduler picks the threads for execution from the pool based
on the thread priorities.
jbE.g. MyThread.start () ;
The output of the above piece of code when executed on Windows XP platform is given in
Fig. 10.10.
Introduction to Embedded Systems
[D:\JayaHoine\jrel .0
Invoking the static method yieldQ voluntarily, give up the execution of the thread and the thread is
moved to the pool of threads waiting to get their turn for execution, i.e. the thread enters the ‘Ready’
state.'
E.g. MyThread.yield(); -
The static method sleepQ forces the thread to sleep for the duration mentioned by the sleep call, i.e.
the thread enters the ‘Suspend’ mode. Once the sleep period is expired, the thread is moved to the pool
of threads waiting to get their turn for execution, i.e. the thread enters the ‘Ready’ state. The method
sleep() only guarantees that the thread will sleep for the minimum period mentioned by the argument to
the call. It will not guarantee anything on the resume of the thread after the sleep period. It is dependent
on the scheduler.
• E.r*g. .MyThread.sleep (100)Sleep for 100 milliseconds.
Calling a thread Object’s waitQ method causes the thread object to wait. The thread will remain in the
‘-Wait’ state until another thread invokes the notify() or notifyAll() method of the thread object which is
waiting. The thread enters the ‘Blocked’ state when waiting for input from I/O devices or waiting for
object lock in case of accessing shared resources. The thread is moved to the ‘Ready’ state on receiving
the I/O input or on acquiring the object lock. The thread enters the ‘Finished/Dead’ state on completion
of the task assigned to it or when the stopQ method is explicitly invoked. The thread may also enter this
state if it is terminated by an unrecoverable error condition.
For more information on Java threads, visit Sun Micro System’s tutorial on Threads, available at
http ://j ava. sun, com/ tutori al/applet/overview/threads. html
Summary So far we discussed about the various thread classes available for creation and manage¬
ment of threads in a multithreaded system in a General Purpose Operating System’s perspective. From
an RTOS perspective, POSIX threads and Win32 threads are the most commonly used thread class
libraries for thread creation and management. Many non-standard, proprietary thread classes are also
used by some proprietary RTOS. Portable threads (Pth), a very portable POSIX/ANSI-C based library
from GNU, may be the “next generation” threads library. Pth provides non-preemptive priority based
scheduling for multiple threads inside event driven applications. Visit http://www,gnu,org/software/pth/
for more details on GNU Portable threads.
10.3.2.3 Thread Pre-emption Thread pre-emption is the act of pre-empting the currently running
thread (stopping the currently running thread temporarily). Thread pre-emption ability is solely depen¬
dent on the Operating System. Thread pre-emption is performed for sharing the CPU time among all
the threads. The execution switching among threads are known as ‘ Thread context switching'. Thread
context switching is dependent on the Operating system’s scheduler and the type of the thread. When
we say ‘Thread’ , it falls into any one of the following types.
Real-Time Operating System (RTOS) based Embedded System Design
User Level Thread User level threads do not have kemel/Operating System support and they exist
solely in the rqnning process. Even if a process contains multiple user level threads, the OS treats it as
single thread and will not switch the execution among the different threads of it. It is the responsibility
of the process to schedule each thread as and when required. In summary, user level threads of a process
are non-preemptive at thread level from OS perspective.
Kernel/System Level Thread Kernel level threads are individual units of execution, which the OS
treats as separate threads. The OS interrupts the execution of the currently running kernel thread and
switches the execution to another kernel thread based on the scheduling policies implemented by the
OS. In summary kernel level threads are pre-emptive.
For user level threads, the execution switching (thread context switching) happens only when the
currently executing user level thread is voluntarily blocked. Hence, no OS intervention and system calls
are involved in the context switching of user level threads. This makes context switching of user level
threads very fast. On the other hand, kernel level threads involve lots of kernel overhead and involve
system calls for context switching. However, kernel threads maintain a clear layer of abstraction and
allow threads to use system calls independently. There are many ways for binding user level threads
with system/kemel level threads. The following section gives an overview of various thread binding
models.
Many-to-One Model Here many user level threads are mapped to a single kernel thread. In this
model, the kernel treats all user level threads as single thread and the execution switching among the
user level threads happens when a currently executing user level thread voluntarily blocks itself or relin¬
quishes the CPU. Solaris Green threads and GNU Portable Threads are examples for this. The ‘PThread’
example given under the POSIX thread library section is an illustrative example for application with
Many-to-One thread model.
One-to-One Model In One-to-One model, each user level thread is bonded to a kernel/system level
thread. Windows XP/NT/2000 and Linux threads are examples for One-to-One thread models. The
modified1 PThread' example given under the 'ThreadPre-emption' section is an illustrative example for
application with One-to-One thread model.
Many-to-Many Model In this model many user level threads are allowed to be mapped to many
kernel threads. Windows NT/2000 with ThreadFibre package is an example for this.
10.3.2.4 Thread v/s Process I hope, by now you got a reasonably good knowledge of process and
threads. Now let us summarise the properties of process and threads.
402
The terms multiprocessing and multitasking are a little confusing and sounds alike. In the operating sys¬
tem context multiprocessing describes the ability to execute multiple processes simultaneously. Systems
which are capable of performing multiprocessing, are.known as multiprocessor systems. Multiprocessor
systems possess multiple CPUs and can execute multiple processes simultaneously.
The ability of the operating system to have multiple programs in memory, which are ready for execu¬
tion, is referred as multiprogramming. In a uniprocessor system, it is not possible to execute multiple
processes simultaneously. However, it is possible for a uniprocessor system to achieve some degree of
pseudo parallelism in the execution of multiple processes by switching the execution among different
processes. The ability of an operating system to hold multiple processes in memory and switch the
processor (CPU) from executing one process to another process is known as multitasking. Multitasking
creates the illusion of multiple tasks executing in parallel. Multitasking involves the switching of CPU
from executing one task to another. In an earlier section ‘The Structure of a Process' of this chapter, we
learned that a Process is identical to the physical processor in the sense it has own register set which mir¬
rors the CPU registers, stack and Program Counter (PC). Hence, a ‘process' is considered as a ‘ Virtual
processor', awaiting its turn to have its properties switched into the physical processor. In a multitasking
environment, when task/process switching happens, the virtual processor (task/process) gets its proper¬
ties converted into that of the physical processor. The switching of the virtual processor to physical pro¬
cessor is controlled by the scheduler of the OS kernel. Whenever a CPU switching happens, the current
context of execution should be saved to retrieve it at a later point of time when the CPU executes the
process, which is interrupted currently due to execution switching. The context saving and retrieval is
essential for resuming a process exactly from the point where it was interrupted due to CPU switching.
The act of switching CPU among the processes or changing the current execution context is known as
‘ Context switching'. The act of saving the current context which contains the context details (Register
details, memory details, system resource usage details, execution details, etc.) for the currently running
process at the time of CPU switching is known as ‘ Context saving'. The process of retrieving the saved
context details for a process, which is going to be executed due to CPU switching, is known as ‘Con¬
text retrieval'. Multitasking involves ‘Context switching’ (Fig. 10.11), ‘Context saving' and ‘Context
retrieval'.
Toss juggling - The skilful object manipulation game is a classic real world example for the multitask¬
ing illusion. The juggler uses a number of objects (balls, rings, etc.) and throws them up and catches
them. At any point of time, he throws only one ball and catches only one per hand. However, the speed at
which he is switching the balls for throwing and catching creates the illusion, he is throwing and catch¬
ing multiple balls or using more than two hands © simultaneously, to the spectators.
10.4.1 Types of Multitasking
As we discussed earlier, multitasking involves-the switching of execution among multiple tasks. De¬
pending on how the switching act is implemented, multitasking can be classified into different types.
The following section describes the various types of multitasking existing in the Operating System’s
context.
10.4.1.1 Co-operative Multitasking Co-operative multitasking is the most primitive form of
multitasking in which a task/process gets, a chance to execute only when the currently executing task/
process voluntarily relinquishes the CPU. In this method, any task/process can hold the CPU as much
time as it wants. Since this type of implementation involves the mercy of the tasks each other for getting
the CPU time for execution, it is known as co-operative multitasking. If the currently executing task is
non-cooperative, the other tasks may have to wait for a long time to get the CPU.
10.4.1.2 *Preemptive Multitasking Preemptive multitasking ensures that every task/process gets a
chance to execute. When and how much time a process gets is dependent on the implementation of the
preemptive scheduling. As the name indicates, in preemptive multitasking, the currently running task/
process is preempted to give a chance to other tasks/process to execute. The preemption of task may be
based on time slots or task/process priority.
10.4.1.3 Non -preemp tive Multitasking In non-preemptivemultitasking, the process/task, which is
currently given the CPU time, is allowed to execute until it terminates (enters the ‘ Completed’ state) or enters
the ‘Blocked/Wait’ state, waiting for an I/O or system resource. The co-operative and non-preemptive multi¬
tasking differs in their behaviour when they are in the ‘Blocked/Wait’ state. In co-operative multitasking, the
currently executing process/task need not relinquish the CPU when it enters the ‘Blocked/Waif state,
Introduction to Embedded Systems
waiting for an I/O, or a shared resource access or an event to occur whereas in non-preemptive multi¬
tasking the currently executing task relinquishes the CPU when it waits for an I/O or system resource
or an event to occur.
ctsm •
The Operating System maintains various queues'! in connection with the CPU scheduling, and a pro¬
cess passes through these queues during the course of its admittance to execution completion.
t Queue is a special kind of arrangement of a collection of objects. In the operating system context queue is considered as a buffer.
Real-Time Operating System (RTOS) based Embedded System Design
Device Queue
Based on the scheduling algorithm used, the scheduling can be classified into the following
categories.
order in which they enter the ‘Ready ’ queue. The first entered process is serviced first. It is same as any
real world application where queue systems are used; e.g. Ticketing reservation system where people
need to stand in a queue and the first person standing in the queue is serviced first. FCFS scheduling is
also known as First In First Out (FIFO) where the process which is put first into the ‘Ready’ queue is
serviced first.
Three-processes with process IDs PI, P2, P3 with estimated completion time 10, 5,7 milliseconds respectively inters the
ready queue together in the order PI, P2, P3. Calculate the waiting time and Turn Around Time (TAT) for each process
and the average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes).
The sequence of execution of the processes by the CPU is represented as
<--10--5—-7-►
Assuming the CPU is readily available at the time of arrival of PI, PI starts executing without any waiting in the 1Ready’
queue. Hence the waiting time for PI is zero. The waiting time for all processes are given as
Waiting Time for PI = 0 ms (PI starts executing first).
Waiting Time for P2 = 10 ms (P2 starts executing after completing PI)
Waiting Time for P3 = 15 ms (P3 starts executing after completing P1 and P2)
Average waiting time = (Waiting time for all processes) / No. of Processes
— = (Waiting time for (P1+P2+P3)) / 3
= (0+10+15)/3 = 25/3
= 8.33 milliseconds
Turn Around Time (TAT) for P1 = 10 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P2 = 15 ms (-Do-)
Turn Around Time (TAT) for P3 = 22 ms (-Do-)
Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P1+P2+P3)) / 3
= (10+15+22)/3 =47/3
= 15.66 milliseconds
Average Turn Around Time (TAT) is the sum of average waiting time and average execution time.
Average Execution Time = (Execution time for all processes)/No. of processes
= (Execution time for (Pl+P2+P3))/3
= (10+5+7)/3 = 22/3
= 7.33
Average Turn Around Time = Average waiting time + Average execution time
= 8.33 + 7.33
= 15.66 milliseconds
Real-Time Operating System (RTOS) based Embedded System Design
^Example 2
Calculate the waiting time and Turn Around Time (TAT) for each process and the Average waiting time and Turn Around
Time (Assuming there is no I/O waiting for the processes) for the above example if the process enters the ‘Ready’ queue
together in the order P2, PI, P3.
The sequence of execution of the processes by the CPU is represented as
05 15 22
◄-5--10--7-►
Assuming the CPU is readily available at the time of arrival of P2, P2 starts executing without any waiting in the ‘Ready'
queue. Hence the waiting time for P2 is zero. The waiting time for all processes is given as
Waiting Time for P2 = 0 ms (P2 starts executing first)
Waiting Time for PI = 5 ms (PI starts executing after completing P2)
Waiting Time for P3 = 15 ms (P3 starts executing after completing P2 and PI)
. Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P2+P1+P3)) / 3
= (0+5+15)/3 = 20/3
= 6.66 milliseconds
- Turn Around Time (TAT) for P2 = 5 ms (Time spent in Ready'Queue + Execution Time)
frfajjujgfjjjtibtf atie6
.'.y, '-
Example 1
Three processes with process IDs PI, P2, P3 with estimated completion time 10,5,7 milliseconds respectively enters the
: ready queue together in the order PJ, P2, P3 (Assume only P1 is present in the ‘Ready' queue when the scheduler picks
Introduction to Embedded Systems
it up and P2, P3 entered ‘Ready’ queue after that). Now a new process P4 with estimated completion time 6 ms enters
the ‘Ready’ queue after 5 ms of scheduling P1. Calculate the waiting time and Turn Around Time (TAT) for each process
and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes). Assume all the
processes contain only CPU operation and no I/O! operations are involved.
Initially there is only PI available in the Ready queue and the scheduling sequence will be PI, P3, P2. P4 enters the
queue during the execution of PI and becomes the last process entered the ‘Ready’ queue. Now the order of execution
changes to PI, P4, P3, and P2 as given below.
10.5.1.3 Shortest Job First (SJF) Scheduling Shortest Job First (SJF) scheduling algorithm ‘sorts
the ‘Ready’ queue’ each time a process relinquishes the CPU (either the process terminates or enters
the ‘ Waif state waiting for I/O or system resource) to pick the process with shortest (least) estimated
completion/run time. In SJF, the process with the shortest estimated run time is scheduled first, followed
by the next shortest process, and so on.
Three processes with process IDs PI, P2, P3 with estimated completion time 10, 5,7 milliseconds respectively enters the
ready queue together. Calculate the waiting time and Turn Around Time (TAT) for each process and the Average waiting
time and Turn Around Time (Assuming there is no I/O waiting for the processes) in SJF algorithm.
The scheduler sorts the ‘Ready’ queue based on the shortest estimated completion time and schedules the process with
the least estimated completion time first and the next least one as second, and so on. The order in which the processes are
scheduled for execution is represented as
Real-Time Operating System (RTOS) based Embedded System Design
r-
0 5 12 22
◄ — 5—-7--10-►
The estimated execution time of P2 is the least (5 ms) followed by P3 (7 ms) and PI (10 ms).
The waiting time for all processes are given as
Waiting Time for P2 = 0 ms (P2 starts executing first)
Waiting Time for P3 = 5 ms (P3 starts executing after completing P2)
Waiting Time for PI = 12 ms (PI starts executing after completing P2 and P3)
Average waiting time = (Waiting time for all processes) / No. of Processes
= (Waiting time for (P2+P3+P1)) / 3
= (0+5+12)/3 = 17/3
= 5.66 milliseconds
Turn Around Time (TAT) for P2 = 5 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P3 = 12 ms (-Do-)
Turn Around Time (TAT) for P1 = 22 ms (-Do-)
Average Turn Around Time = (Turn Around Time for all processes) / No. of Processes
= (Turn Around Time for (P2+P3+P1)) / 3
= (5+12+22)/3 = 39/3
= 13 milliseconds
Average Turn Around Time (TAT) is the sum of average waiting time and average execution time.
The average Execution time = (Execution time for all processes)/No. of processes
= (Execution time for (P1+P2+P3 ))/3
= (10+5+7)/3 =2273 = 7.33
Average Turn Around Time = Average Waiting time + Average Execution time
= 5.66 + 7.33
= 13 milliseconds
From this example, it is clear that the average waiting time and turn around time is much improved with the SJF schedul¬
ing for the same processes when compared to the FCFS algorithm.
r—— ---—
Example 2
v' : ' — —■ *
Calculate the waiting time and Turn Around Time (TAT) for each process and the Average waiting time and Turn Around
Time for the above example if a new process P4 with estimated completion time 2 ms enters the ‘Ready’ queue after 2 ms
of execution of P2. Assume all the processes contain only CPU operation and no I/O operations are involved.
At the beginning, there are only three processes (PI, P2 and P3) available in the ‘Ready’ queue and the SJF scheduler
picks up the process with the least execution completion time (In this example P2 with execution completion time 5 ms)
for scheduling. The execution sequence diagram for this is same as that of Example 1.
Now process P4 with estimated execution completion time 2 ms enters the ‘Ready’ queue after 2 ms of start of execu¬
tion of P2. Since the SJF algorithm is non-preemptive and process P2 does not contain any I/O operations, P2 continues
its execution. After 5 ms of scheduling, P2 terminates and now the scheduler again sorts the ‘Ready’ queue for process
with least execution completion time. Since the execution completion time for P4 (2 ms) is less than that of P3 (7 ms),
which was supposed to be run after the completion of P2 as per the ‘Ready’ queue available at the beginning of execu¬
tion scheduling, P4 is picked up for executing. Due to the arrival of the process P4 with execution time 2 ms, the ‘Ready’
queue is re-sorted in the order P2, P4, P3, PI. At the beginning it was P2, P3, PI. The execution sequence now changes
as per the following diagram
Introduction to Embedded Systems
n j H ; X’AA+uT :
• Pi A
0 5 7 14 24
< _ 5 ___7 ——--10 —-►
10.5.1.4 Priority Based Scheduling The Turn Around Time (TAT) and waiting time for processes
in non-preemptive scheduling varies with the type of scheduling algorithm. Priority based non-preemptive
scheduling algorithm ensures that a process with high priority is serviced at the earliest compared to other
low priority processes in the ‘Ready’ queue. The priority of a task/process can be indicated through various
mechanisms. The Shortest Job First (SJF) algorithm can be viewed as a priority based scheduling where each
task is prioritised in the order of the time required to complete the task. The lower the time required for com¬
pleting a process the higher+s its priority in SJF algorithm. Another way of priority assigning is associating a
priority to the task/process it fhe time of creation of the task/process. The priority is a number ranging from 0
to the maximum priority supported by the OS. The maximum level of priority is OS dependent. For Example,
Windows CE supports 256 levels of priority (0 to 255 priority numbers). While creating the process/task, the
priority can be assigned to it. The priority number associated with a task/process is the direct indication of its
priority. The priority variation from high to low is represented by numbers from 0 to the maximum priority or
by numbers from maximum priority to 0. For Windows CE operating system a priority numbef 0 indicates the
highest priority and 255 indicates the lowest priority. This convention need not be universal and it depends on
Real-Time Operating System (RTOS) based Embedded System Design
the kernel level implementation of the priority structure. The non-preemptive priority based scheduler sorts
the ‘Ready’ queue based on priority and picks the process with the highest level of priority for execution.
Example 1
Three processes with process IDs PI, P2, P3 with estimated completion time 10, 5, 7 milliseconds and priorities 0, 3,
2 {i0—highest priority, 3—lowest priority) respectively enters the ready queue together. Calculate the waiting time and
Turn Around Time (TAT) for each process and the Average waiting time and Turn Around Time (Assuming there is no
I/O waiting for the processes) in priority based scheduling algorithm.
The scheduler sorts the ‘Ready' queue based on the priority and schedules the process with the highest priority (PI
with priority number 0) first and the next high priority process (P3 with priority number 2) as second, and so on. The order
in which the processes are scheduled for execution is represented as
Example 2
Calculate the waiting time and Turn Around Time (TAT) for each process and the Average waiting time and Turn Around
Time for the above example if a new process P4 with estimated completion time 6 ms and priority 1 enters the ‘Ready’
queue after 5 ms of execution of PI. Assume all the p vosses contain only CPU operation and no I/O operations are
involved.
At the beginning, there are only three processes (PI, P2 and P3) available in the ‘Ready' queue and the scheduler
picks up the process with the highest priority (In this example PI with priority 0) for scheduling. The execution sequence
diagram for this is same as that of Example 1. Now process P4 with estimated execution completion time 6 ms and
priority 1 enters the ‘Ready' queue after 5 ms of execution of PI. Since the scheduling algorithm is non-preemptive and
process PI does not contain any I/O operations, PI continues its execution. After 10 ms of scheduling, Rluterminates and
now the scheduler again sorts thq‘Ready' queue for process, with highest priority. Since the priority fhnP4 (priority 1)
is higher than that of P3 (priority 2), which was supposed to be run after the completion of PI as per the-‘Ready' queue
available at the beginning of execution scheduling, P4 is picked up for executing. Due to the arrival of the process P4 with
Introduction to Embedded Systems
priority 1, the ‘Ready’ queue is resorted in the order PI, P4, P3, P2. At the beginning it was PI, P3, P2. The execution
sequence now changes as per the following diagram
0 10 16 23 28
‘Preemption’. Preemptive scheduling can be implemented in different approaches. The two important
approaches adopted in preemptive scheduling are time-based preemption and priority-based preemption.
The various types of preemptive scheduling adapted in task/process scheduling are explained below, i
Example 1
Three processes with process IDs PI, P2, P3 with estimated completion time 10, 5, 7 milliseconds respectively enters
the ready queue together. A new process P4 with estimated completion time 2 ms enters the ‘Ready’ queue after-2 ms.
Assume all the processes contain only CPU operation and no I/O operations are involved.
At the beginning, there are only three processes (PI, P2 and P3) available in the 1 Ready’ queue and the SRT scheduler
picks up the process with the shortest remaining time for execution completion (In this example, P2 with remaining time —
5 ms) for scheduling. The execution sequence diagram for this is same as that of example 1 under non-preemptive SJF
scheduling.
Now process P4 with estimated execution completion time 2 ms enters the ’Ready’ queue after 2 ms of start of execu¬
tion of P2. Since the SRT algorithm is preemptive, the remaining time for completion of process P2 is checked with the
remaining time for completion of process P4. The remaining time for completion of P2 is 3 ms which is greater than that
of the remaining time for completion of the newly entered process P4 (2 ms). Hence P2 is preempted and P4 is scheduled
for execution. P4 continues its execution to finish since there is no new process entered in the ‘Ready’ queue during its
execution. After 2 ms of scheduling P4 terminates and now the scheduler again sorts the ’Ready’ queue based on the re¬
maining time for completion of the processes present in the ‘Ready’ queue. Since the remaining time for P2 (3 ms), which
is preempted by P4 is less than that of the remaining time for other processes in the ‘Ready’ queue, P2 is scheduled for
execution. Due to the arrival of the process P4 with execution time 2 ms, the ’Ready’ queue is re-sorted in the order P2,
P4, P2, P3, PI. At the beginning it was P2, P3, PI. The execution sequence now changes as per the following diagram
0 2 4 7 14 24
■42X2X-3-K-7--10-►
Waiting time for P2 = 0 ms + (4 - 2) ms = 2 ms (P2 starts executing first and is interrupted by P4 and has to wait till the
completion of P4 to get the next CPU slot)
Introduction to Embedded Systems
Waiting time for P4 = 0 ms (P4 starts executing by preempting P2 since the execution time for completion of P4 (2 ms) is
less than that of the Remaining time for execution completion of P2 (Here it is 3 ms))
Waiting time for P3 = 7 ms (P3 starts executing after completing P4 and P2)
Waiting time for PI = 14 ms (PI starts executing after completing P4, P2 and P3)
Average waiting time = (Waiting time for all the processes) / No. of Processes
= (Waiting time for (P4+P2+P3 +P1)) / 4
= (0 + 2 + 7 + 14)/4 = 23/4
, = 5.75 millisecond^'
Turn Around Time (TAT) for P2 = 7 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P4 = 2 ms (Time spent in Ready Queue + Execution Time = (Execution Start Time
-Arrival Time) + Estimated Execution Time = (2 - 2) + 2)
Turn Around Time (TAT) for P3 = 14 ms (Time spent in Ready Queue + Execution Time)
Turn Around Time (TAT) for P1 = 24 ms (Time spent in Ready Queue + Execution Time)
Average Turn Around Time = (Turn Around Time for all the processes) / No. of Processes
= (Turn Around Time for (P2+P4+P3+P1)) / 4
= (7+2+14+24)/4 = 47/4
= 11.75 milliseconds
Now let’s compare the Average Waiting time and Average Turn Around Time with that of the Average waiting time and
Average Turn Around Time for non-preemptive SJF scheduling (Refer to Example 2 given under the section Non-pre-
emptive SJF scheduling) 7
Average Waiting Time in non-preemptive SJF scheduling = 6 ms
Average Waiting Time in preemptive SJF scheduling = 5.75 ms
Average Turn Around Time in non-preemptive SJF scheduling = 12 ms
Average Turn Around Time in preemptive SJF scheduling = 11.75 ms
This reveals that the Average waiting Time and Turn Around Time (TAT) improves significantly with preemptive SJF
scheduling.
10.5.2.2 Round Robin (RR) Scheduling The term Round Robin is very popular among the sports
andgames activities. You might have heard about ‘Round Robin’ league or ‘Knock out’ league associ¬
ated with any football or cjricket tournament. In the ‘Round Robin’ league each team in a group gets an
equal chance to'play against the rest of the teams in the same group whereas in the ‘Knock out’ league
the losing team in a match moves out of the tournament ©.
In the process scheduling context also, ‘Round Robin’ brings the same message “Equal chance to
all”. In Round Robin scheduling, each process in the ‘Ready’ queue is executed for a pre-defined time
slot. The execution starts with picking up the first process in the ‘Ready’ queue (see Fig. 10.13). It is
executed for a pre-defined time and when the pre-defined time elapses or the process completes (before
the pre-defined time slice), the next process in the ‘Ready’ queue is selected for execution. This is re¬
peated for all the processes in the ‘Ready’ queue. Once each process in the ‘Ready’ queue is executed for
the pre-defined time period, the scheduler comes back and picks the first process in the ‘Ready’ queue
again for execution. The sequence is repeated. This reveals that the Round Robin scheduling is similar
to the FCFS scheduling and the only difference is that a time slice based preemption is added to switch
the execution between the processes in the ‘Ready’ queue. The ‘Ready’ queue can be considered as a
circular queue in which the scheduler picks up the first process for execution and moves to the next till
the end of the queue and then comes back to the beginning of the queue to pick up the first process.
The time slice is provided by the timer tick feature of the time management unit of the OS kernel (Re¬
fer the Time management section under the subtopic ‘ The Real-Time kernel for more details on Timer
tick). Time slice is kernel dependent and it varies in the order of a few microseconds to milliseconds.
Certain OS kernels may allow the time slice as user configurable. Round Robin scheduling ensures that
Real-Time Operating System (RTOS) based Embedded System Design
every process gets a fixed amount of GPU time for execution. When the process gets its fixed time for
execution is determined by the FCFS policy (That is, a process entering the Ready queue first gets its
fixed execution time first and so on...). If a process terminates before the elapse of the time slice, the
process releases the CPU voluntarily and the next process in the queue is scheduled for execution by
the scheduler. The implementation of RR scheduling is kernel dependent. The following code snippet
illustrates the RR scheduling implementation for RTX51 Tiny OS, an 8bit OS for 8051 microcontroller
Introduction to Embedded Systems
RTX51 defines the tasks as simple C functions with void return type and void argument list. Thq attri¬
bute jask_ is used for declaring a function as task. The general form of declaring a task is
- ; . a void func (void) ;_tas.k_ tasked «** ,» * ,
where func is the name of the task and taskjd is the ID of the task. RTX51 supports up to 16 tasks and
so taskjd varies from 0 to 15. All tasks should be implemented as endless loops.
The two tasks in this program are counter loops. RTX51 Tiny starts executing task 0 which is the
function named jobO. This function creates another task called job 1. After jobO executes for its time
slice, RTX51 Tiny switches to jobl. After job 1 executes for its time slice, RTX51 Tiny switches back to
jobO. This process is repeated forever.
Now let’s check how the RTX51 Tiny RR Scheduling can be implemented in an embedded device (A
smart card reader) which addresses the following requirements.-
• Check the presence of a card
• Process the data received from the card
• Update the Display
• Check the serial port for command/data
• Process the data received from serial port
These four requirements can be considered as four tasks. Implement them as four RTX51 tasks as ex¬
plained below.
'void check_card_ task (void)' _task__ 1 f'X/jAA VvXC Ay. AA-A 3Ai.A-A.'AAci ■iAyT
I . :S~ :-:-k ■ ; . ■v-.'.'W,:V,; ■■,...■■ . .., . ’ 1
.■■ .■ . • ■ • . . ' - .’ .... ■ ..'-'V ■
The os_createJask (task_ID) RTX51 Tiny kernel call puts the task with task ID taskJD in the
‘Ready’ state. All the ready tasks begin their execution at the next available opportunity. RTX51 Tiny
does not have a main () function to begin the code execution; instead it starts with executing task 0. Task
0 is used for creating other tasks. Once all the tasks are created, task 0 is stopped and removed from the
task list with the osjdeletejask kernel call. The RR scheduler selects each task based on the time slice
and continues the execution. If we observe the tasks we can see that there is no point in executing the
task process_cardjask (Task 2) without detecting a card and executing the task process_serial_data_
task (Task 4) without receiving some data in the serial port. In summary task 2 needs to be executed only
when task 1 reports the presence of a card and task 4 needs to be executed only when task 3 reports the
arrival of data at serial port. So these tasks (tasks 2 and 4) need to be put in the ‘Ready’ state only on
satisfying these conditions. Till then these tasks can be put in the ‘Wait’ state so that the RR scheduler
will not pick them for scheduling and the RR scheduling is effectively utilised among the other tasks.
This can be achieved by implementing the wait and notify mechanism in the related tasks. Task 2 can
be coded in a way that it waits for the card present event and task 1 signals the event ‘card detected’. In
a similar fashion Task 4 can be coded in such a way that it waits for the serial data received event and
task 3 signals the reception of serial data on receiving serial data from serial port. The following code
snippet explains the same.
void check_card_task (void) ,._:;ask_ 1 -I -t ■ • .■ r-..
t. ' , /
//Function for checking the signaling of card -/present event
os_waitl (K_SIG); \.
//Process card data /
Introduction to Embedded Systems
ft&ggH
Wj* K 7V
IIP •'-/■/pi
4J. ;-> *-*a£J
.A-wP/IiP-C
void pro'cess_s6rial
The os_send_signal (Task ID) kernel call sends a signal to task Task ID. If the specified task is already
waiting for a signal, this function call readies the task for execution but does not start it. The osjwaitl
(event) kernel call halts the current task and waits for an event to occur. The event argument specifies the
event to wait for and may have only the value K_SIG which waits for a signal. RTX51 uses the Timer
0 of 8051 for time slice generation. The time slice can be configured by the user by changing the time
slice related parameters in the RTX51 Tiny OS configuration file CONF_TNY.A51 file which is located
in the \KEIL\C51\RTXTINY2\ folder. Configuration options in CONFJNY.A51 allow users to:
• Specify the Timer Tick Interrupt Register Bank.
• Specify the Timer Tick Interval (in 8051 machine cycles).
• Specify user code to execute in the Timer Tick Interrupt.
• Specify the Round-Robin Timeout.
• Enable or disable Round-Robin Task Switching.
• Specify that your application includes long duration interrupts.
• Specify whether or not code banking is used.
• Define the top of the RTX51 Tiny stack.
• Specify the minimum stack space required.
• Specify code to execute in the event of a stack error.
• Define idle task operations.
The RTX51 kernel provides a set of task management functions for managing the tasks. At any point
of time each RTX51 task is exactly in any one of the following state.
Real-Time Operating System (RTOS) based Embedded System Design
Refer the documentation available with RTX51 Tiny OS for more information on the various RTX51
task management kernel functions and their usage.
RR scheduling with interrupts is a good choice for the design of comparatively less complex Real-
Time Embedded Systems. In this approach, the tasks which require less Real-Time attention can be sched¬
uled with Round Robin scheduling and the tasks which require Real-Time attention can be scheduled
through Interrupt Service Routines. RTX51 Tiny supports Interrupts with RR scheduling. For RTX51
the time slice for RR scheduling is provided by the Timer interrupt and if the interrupt is of high prior¬
ity than that of the timer interrupt and if its service time (ISR) is longer than the timer tick interval, the
RTX51 timer interrupt may be interrupted by the ISR and it may be reentered by a subsequent RX51
Tiny timer interrupt. Hence proper care must be taken to limit the ISR time within the timer tick interval
or to protect the timer tick interrupt code from reentrancy. Otherwise unexpected results may occur.
The limitations of RR with interrupt generic approach are the limited number of interrupts supported by
embedded processors and the interrupt latency happening due to the context switching overhead.
RR can also be used as technique for resolving the priority in scheduling among the tasks with same
level of priority. We will discuss about how RR scheduling can be used for resolving the priority among
equal tasks under the VxWorks kernel in a later chapter.
Three processes with process IDs PI, P2, P3 with estimated completion time 6,4,2 milliseconds respectively, enters the
ready queue together in the order PI, P2, P3. Calculate the waiting time and Turn Around Time (TAT) for each process
and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes) in RR algo¬
rithm with Time
/
slice = 2 ms.
The scheduler sorts the ‘Ready’ queue based on the FCFS policy and picks up the first process PI from the ‘Ready’
queue and executes it for the time slice 2 ms. When the time slice is expired, PI is preempted and P2 is scheduled for
execution. The Time slice expires after 2ms of execution of P2. Now P2 is preempted and P3 is picked up for execution.
P3 completes its execution within the time slice and the scheduler 'picks P1 again for execution for the next time slice.
This procedure is repeated till all the processes are serviced. The order in which the processes are scheduled for execution
> /
is represented as
Introduction to Embedded Systems
10.5.2.3 Priority Based Scheduling Priority based preemptive scheduling algorithm is same as
that of the non-preemptive priority based scheduling except for the switching of execution between
tasks. In preemptive scheduling, any high priority process entering the ‘Ready’ queue is immediately
scheduled for execution whereas in the non-preemptive scheduling any high priority process entering
the ‘Ready’ queue is scheduled only after the currently executing process completes its execution or
only when it voluntarily relinquishes the CPU. The priority ofV task/process in preemptive scheduling
is indicated in the same way as that of the mechanism adopted for non-preemptive multitasking. Refer
the non-preemptive priority based scheduling discussed, in an earlier section of this chapter for more
details.
Real-Time Operating System (RTOS) based Embedded System Design
Example 1
Three processes with process IDs PI, P2, P3 with estimated completion time 10, 5, 7 milliseconds and priorities 1, 3, 2
(0—highest priority, 3—lowest priority) respectively enters the ready queue together. A new process P4 with estimated
completion time 6 ms and priority 0 enters the ‘Ready’ queue after 5 ms of start of execution of PI. Assume all the pro¬
cesses contain only CPU operation and no I/O operations are involved.
At the beginning, there are only three processes (PI, P2 and P3) available in the ‘Ready’ queue and the scheduler picks
up the process with the highest priority (In this example P1. with priority 1) for scheduling.
Now process P4 with estimated execution completion time 6 ms and priority 0 enters the ‘Ready’ queue after 5 ms of
start of execution of P1. Since the scheduling algorithm is preemptive, P1 is preempted by P4 and P4 runs to completion.
After 6 ms of scheduling, P4 terminates and now the scheduler again sorts the ‘Ready’ queue for process with highest
priority. Since the priority for PI (priority 1), which is preempted by P4 is higher than that of P3 (priority 2) and P2
((priority 3), PI is again picked up for execution by the scheduler. Due to the arrival of the process P4 with priority 0, the
‘Ready’ queue is resorted in the order PI, P4, PI, P3, P2. At the beginning it was PI, P3, P2. The execution sequence now
changes as per the following diagram
m [111
|HH9@| QQ++UA' +1 rA'A
• t ■+ y q
1
-Lb- i-i-T-S F • "-Rfiii
0 ' 5 11 16 23 28
◄-5-—-6--►-* -5-———7- -5-►
Priority based preemptive scheduling gives Real-Time attention to high priority tasks. Thus priority
based preemptive scheduling is adopted in systems which demands ‘Real-Time’ behaviour. Most of
the RTOSs make use of the preemptive priority based scheduling algorithm for process scheduling.
Preemptive priority based scheduling also possesses the same drawback of non-preemptive priority
based scheduling-1 Starvation'. This can be eliminated by the 1 Aging' technique. Refer the section Non-
preemptive priority based scheduling for more details on ‘Starvation’ and '’Aging'.
return;
}
WaitForSingleOb j.ect (hThread, INFINITE) ;
return.;--
■
..///Task .execution time is 10 . units
' .of execution ,..
,» ,' ,. V ’;■>,< 'V'" \ V, >. <•,»/;!
/./Sleep for 5 units of execution ” / //.' //‘A/ // ' '/ '''//“■• ■‘
Sleep (5) ; //Parameter given is not in mi 1 '..iseconds *1 ' yl A
- //Repeat .‘task - • - ' " A' * ' it _>■ ■ -•
■ ' ;v' t'/'f:ffV/• .f ’ n" '
k/'XT' t
t Rj y’rkfc * * * * * * ** * * *
k k kkk * * * * * *. * Tt ** * ■* * * 1 * * A
k k * k k k k k k k kkk kk k k k kkkkkkkkkk
SetThreadPriority(hThread,THREAD_PRIORITY_ABOVE_NORMAL);
ResumeThread(hThread);
WaitForSingleObject(hThread, INFINITE);
return;
The first piece of code represents a process (Process 1) with priority normal and it performs a task
which requires 7.5 units of execution time. After performing this task, the process sleeps for 17.5 units
of execution time and this is repeated forever. The second piece of code represents a process (Process
Introduction to Embedded Systems
2) with priority above normal and it performs a task which requires 10 units of execution time. After
performing this task, the process sleeps for 5 units of execution time and this is repeated forever. Process
2 is of higher priority compared to process 1, since its priority is above ‘Normal’.
Now let us examine what happens if these processes are executed on a Real-Time kernel with pre¬
emptive priority based scheduling policy. Imagine Process 1 and Process 2 are ready for execution. Both
of them enters the ‘Ready’ queue and the scheduler picks up Process 2 for execution since it is of higher
priority (Assuming there is no other process running/ready for execution, when both the processes are
‘Ready’ for execution) compared to Process 1. Process 2 starts executing and runs until it executes the
Sleep instruction (i.e. after 10 Units of execution time). When the Sleep instruction is executed, Process 2
enters the wait state. Since Process 1 is waiting for its turn in the ‘Ready’ queue, the scheduler picks up
it for execution, resulting in a context switch. The Process Control Block (PCB) of Process 2 is updated
with the values of the Program Counter (PC), stack pointer, etc. at the time of context switch. The esti¬
mated task execution time for Process 1 is 7.5 units of execution time and the sleeping time for Process
2 is 5 units of execution. After 5 units of execution time, Process 2 enters the ‘Ready’ state and moves
to the ‘Ready’ queue. Since it is of higher priority compared to the running process, the running process
(Process 1) is pre-empted and Process 2 is scheduled for execution. Process 1 is moved to the ‘Ready’
queue, resulting in context switching. The Process Control Block of Process 1 is updated with the cur¬
rent values of the Program Counter (PC), Stack pointer, etc. when the context switch is happened. The
Program Counter (PC), Stack pointer, etc. for Process 2 is loaded with the values stored in the Process
Control Block (PCB) of Process 2 and Process 2 continues its execution form where it was stopped ear¬
lier. Process 2 executes the Sleep instmction after 10 units of execution time and enters the wait state.
At this point Process 1 is waiting in the ‘Ready’ queue and it requires 2.5 units of execution time for
completing the task associated with it (The total time for completing the task is 7.5 units of time, out
of this it has already completed 5 units of execution when Process 2 was in the wait state). The sched¬
uler schedules Process 1 for execution. The Program Counter (PC), Stack pointer, etc. for Process 1 is
loaded with the values stored in the Process Control Block (PCB) of Process 1 and Process 1 continues
its execution form where it was stopped earlier. After 2.5 units of execution time, Process 1 executes the
Sleep instmction and enters the wait state. Process 2 is already in the wait state and the scheduler finds
no other process for scheduling. In order to keep the CPU always busy, the scheduler runs a dummy
process (task) called ‘IDLE PROCESS (TASK)’. The ‘IDLE PROCESS (TASKV executes some dummy
task and keeps the CPU engaged. The execution diagram depicted in Fig. 10.24 explains the sequence
of operations.
The implementation of the ‘IDLE PROCESS (TASK)’ is dependent on the kernel and a typical imple¬
mentation for a desktop OS may look like. It is simply an endless loop.
void Idle_Process (void)
{
//Simply wait.
//Do nothing...
While (1);' . f
1
The Real-Time kernels deployed in embedded systems, where operating power is a big constraint
(like systems which are battery powered); the ''IDLE TASK’ is used for putting the CPU into IDLE mode
for saving the power. A typical example is the RTX51 Tjny Real-Time kernel, where the ‘IDLE TASK'
sets the 8051 CPU to IDLE mode, a power saving mode. In the ‘IDLE5 mode, the program execution is
halted and all peripherals and the interrupt system continues its operation. Once the CPU is put into the
‘IDLE’ mode, it domes out of this mode when an Interrupt occurs or when the RTX51 Tiny Timer Tick
Interrupt (The timer interrupt used for task scheduling in Round robin scheduling) occurs. It should be
noted that the ‘IDLE PROCESS (TASK)’ execution is not pre-emptive priority scheduling specific, it is
applicable to all types of scheduling policies which demand 100% CPU utilisation/CPU power saving.
Back to the desktop OS environment, let’s analyse the process, threads and scheduling in the Win¬
dows desktop environment. Windows provide a utility called task manager for monitoring the different
process running on the system and the resources use'd by each process. A snapshot of the process details
returned by the task manager for Windows XP kernel is shown in Fig. 10.15. It should be noted that
this snapshot is purely machine dependent and it varies with the number of processes running on the
machine.
‘Image Name’ represents the name of the process. ‘PID* represents the Process Identification Number
(Process ID). As mentioned in the Threads and Process’ section, when a process is created an ID is
associated to it. CPU usage gives the % of CPU utilised by the process during an interval. ‘ CPU Time’
gives the total CPU time used by a process after its commencement. ‘Mem Usage’ represents the total
main memory" in kilobytes, used by the process. ‘ VMSize’ represents the total virtual memory (paged
memory), in kilobytes, used by a process. ‘Paged Pool’ represents the paged memory, in kilobytes, cur¬
rently used by the system. ‘NP Pool’ is the non-paged pool or system memory used by a process. The
non-paged memory is not swapped to the secondary storage disk. ‘Base PrV ^presents the priority of
Introduction to Embedded Systems
['Fig. 10.15] Windows XP task manager for monitoring process and resource usage
the process. As mentioned in an earlier section, a process may contain multiple threads. The ‘ Threads'
section gives the number of threads present in a process. ‘Handles’ reflects the number of object handles
owned by the process. This value is the reflection of the object handles present in the process’s object
table. ‘User Objects' reflects the number of objects active in the user mode for a process. Use ‘Ctrl'
+ ‘Alt' + 'Del' key for accessing the task manager and select the ‘ View’ -> ‘Select Columns’ option to
select the different monitoring parameters for a process.
Competing Processes: The competing processes do not share anything among themselves but they
share the system resources. The competing processes compete for the system resources such as file,
display device, etc.
Co-operating processes exchanges information and communicate through the following methods.
Co-operation through Sharing: The co-operating process exchange data through some shared
resources.
Co-operation through Communication: No data is shared between the processes. But they commu¬
nicate for synchronisation.
The mechanism through which processes/tasks communicate each other is known as Inter Pro¬
cess/Task Communication (IPC). Inter Process Communication is essential for process co-ordination.
The various types of Inter Process Communication (IPC) mechanisms adopted by process are kernel
(Operating System) dependent. Some of the important IPC mechanisms adopted by various kernels are
explained below.
10.7.1.1 Pipes ‘Pipe’ is a section of the shared memory used by processes for communicating. Pipes
follow the client-servert architecture. A process which creates a pipe is known as a pipe server and a
process which connects to a pipe is known as pipe client. A pipe can be considered as a conduit for
information flow and has two conceptual ends. It can be unidirectional, allowing information flow in
one direction or bidirectional allowing bi-directional information flow. A unidirectional pipe allows the
process connecting at one end of the pipe to write to the pipe and the process connected at the other end
of the pipe to read the data, whereas a bi-directional pipe allows both reading and writing at one end.
The unidirectional pipe can be visualised as
- j
tClient Server is a software architecture containing a client application and a server application. The application which sends request is
known as client and the application which receives the request process it and sends a response back to the client is known as server. A
server is canable of receiving request from multinkxlien!&. .
Introduction to Embedded Systems
Process 1 Process 2
hPIFileMap = hP2FileMap =
CreateFileMapping CreateFileMapping
(HANDLE)-1, (HANDLE) -1,
NULL, NULL,
PAGEREADWRITE, PAGE_READWRITE,
0,0x8000, 0,0x8000,
"memorymapobject"); "memoryniapobj ect");
usage count for the named object and it is incremented each time when a process creates/opens a memory
mapped object with existing name. This will prevent the destruction of a shared memory object by one
process while it is being accessed by another process. Hence passing the name of the memory mapped
object is strongly recommended for memory mapped object based inter process communication. The
MapViewOfFile (HANDLE hFileMappingObject DWORD dwDesiredAccess, DWORD dwFileOffs-
etHigh, DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap) system call maps aview of the
memory mapped object to the address space of the calling process. The parameter hFileMappingObject
specifies the handle to an existing memory mapped object. The dwDesiredAccess parameter represents
the read write access for the mapped view area. A value of FILE MAP WRITE makes the view access
read-write, provided the memory mapped object hFileMappingObject is created with read-write ac¬
cess, whereas the value FILE_MAPJREAD gives read only access to the shared memory, provided the
memory mapped object hFileMappingObject is created with read-write/read only access. The parameter
dwFileOffsetHigh specifies the higher order 32 bits and dwFileOffsetLow specifies the lower order 32
bits of the memory offset where mapping is to begin from the memory mapped object. A value of ‘0’
for both of these maps the view from the beginning memory area of the memory object. dwNumberOf¬
BytesToMap specifies the number of bytes of the memory object to map. If dwNumberOfBytesToMap
is zero, the entire memory area owned by the memory mapped object is mapped. On successful execu¬
tion, MapViewOfFile call returns the starting address of the mapped view. If the function fails it returns
NULL. A mapped view of the memory mapped object is unmapped by the API call UnmapViewOfFile
(LPCVOID IpBaseAddress). The IpBaseAddress parameter specifies a pointer to the base address of
the mapped view of a memory object that is to be unmapped. This value must be identical to the value
returned by a previous call to the MapViewOfFile function. Calling UnmapViewOfFile cleans up the
committed physical storage in a process’s virtual address space. In other words, it frees the virtual ad¬
dress space of the mapping object. Under Windows NT/XP OS, a process can open an existing memory
mapped object by calling the API OpenFileMapping(DWORD dwDesiredAccess, BOOL blnheritHan-
dle, LPCTSTR IpName). The parameter dwDesiredAccess specifies the read write access permissions foi
Introduction to Embedded Systems
the memory mapped object. A value of FILE JAAP_ALL_ACCESSprovides read-write access, whereas
the value FILE JAAP_READ allocates only read access and FILEJAAPJWR1TE allocates write only
access. The parameter blnheritHandle specifies the handle inheritance. If this parameter is TRUE, the
calling process inherits the handle of the existing object, otherwise not. The parameter IpName specifies
the name of the existing memory mapped object which needs to be opened. Windows CE 5.0 does not
support handle inheritance and hence the API call OpenFileMapping is not supported.
The following sample code illustrates the creation and accessing of memory mapped objects across
multiple processes. The first piece of code illustrates the creation of a memory mapped object with name
“memorymappedobject” and prints the address of the memory location where the memory is mapped
within the virtual address space of Process 1.
#include <stdio.h> J ' ■ ' I'JJ"
#include <windo.ws . h> - i" j ; ■ - .*
1/ ******************** * .* ******** *.* * * * * ■* .* ****** ** * *-**:* * * * * ***** ***** *-*
//Process 1: Creates the memory mapped object and maps it to-
//Process l's Virtual. Addresp space A " ", - . .
/ / *************** *•■*■*.* ************ ** *'* ******* ** * ***** * ********* *******
void main () { ‘ ' .. ...
//Define . the handle / to -Memory mapped Object -'*PV
HANDLE hFiieMap; .. ... ■. * y . ' ’.. y
.//Define the handle to. the view of-Memory mapped Object
LPTSTR hMapViev;; : .-r y, y.:.
■ ' print! (" / / ********* ********************* * * * ********* * ** *\n");
printf f"" ; ; Process l\n") ? . ... 4 .. /
p r i n t E ("//************* ** * * *■* * ** * * *V*.* * * * * * * * * * ***** *1* * * \nn);
//Create an 8 KB memory mapped object
hFiieMap = Create-FileMappirig ( (HANDLE) -1,
NULL, .// default security attributes'*
BAGE_READWRITE, // Read-Write Access
0, //Higher order 32 bits of the memory mapping object
0x2000, //Lower order 32 bits of the memory mapping object
TEXT("memorymappedobject")); // Memory mapped object name
if (NULL == hFiieMap)
printf ("Memory mapped Object Creation Failed: Error Code: %d\n", GetLastError
0 );
//Memory mapped Object Creation failed. Return
return;
1
{
printf ("Mapping of Memory mapped view Failed: Error Code:
%d\n", GetLastError ());
//Memory mapped view Creation failed. Return
return;
}
else , ... . , ... . .. _ - - .: . •: ■
r ■ * / "r:h.., .
The piece of code given below corresponds to Process 2. It illustrates the accessing of an existing
memory mapped object (memory mapped object with name “memorymappedobject/lcreated by Process
1) and prints the address of the memory location where the memory is mapped within the virtual address
space of Process 2. To demonstrate the application, the program corresponding to Process 1 should be
executed first and the program corresponding to Process 2 should be executed following Process 1.
finclude <stdio.h>
#include <windows.h>
yy****************************************** *■*-* **********************
//Process 2: Opens the memory mapped object created by Process 1
//Maps the object to Process 2's virtual address space.
y y ********************************** *********************************
void main() {
//Define the handle for the Memory mapped Object
HANDLE hChildFileMap;
//Define the handle for the view of Memory mapped Object
LPTSTR hChildMapView;
printf (V/**********************************************\n");
printf (" Process 2\n");
printf(V/**********************************************\n">;
//Create an 8 KB memory mapped object
hChildFileMap = Creat^FileMapping( INVALID_HANDLE_VALUE,
NUL-L, // default security attributes
PAGE_READWRITE, // Read-Write Access
0, //Higher order 32 bits of the memory mapping object
Introduction to Embedded Systems
else . ' t ‘ -■ /
hChildMapView-(LPTSTR)MapViewOfFile(hChildFileMap,
FIiK_MA?_ WRITE, //Pead-Wr: t.e access- '/• - ■
0, //Map the entire '-view - -v
if (NULL == hChildMapyiew) • ‘ r.
{ , : .J , , '.' ^ " it‘ ' _ , .
printf ("Mapping of Memory mapped view Failed: Error Code: %d\n", GetLastError;,'
()); ’ ■ '■ ; “ % •’ ■ • ■ ■ /V' ■■
else
{
//Successfully created the memory mapped view.
//Print the start address of the mapped view
printf ("The memory mapped view starts at memory location 0x%08x\n", hChild¬
MapView) ;
}
The output of the above programs when run in the sequence, Process 1 is executed first and Process
2 is executed while Process 1 waits for the user input from the keyboard, is given in Fig. 10.19.
This reveals that using memory mapped objects with same name across multiple processes running
on the same system maps the object to the same virtual address space of the processes.
Reading and writing to a memory mapped area is same as any read write operation using pointers.
The pointer returned by the API call MapViewOfFile can be used for this. The exercise of Read and
Write operation is left to the readers. Proper care should be taken to avoid any conflicts that may arise
due to the simultaneous read/write access of the shared memory area by multiple processes. This can be
handled by applying various synchronisation techniques like events, mutex, semaphore, etc.
For using a memory mapped object across multiple threads of a process, it is not required for all
the threads of the process to create/open the memory mapped object and map it to the thread’s virtual
address space. Since the thread’s address space is part of the process’s virtual address space, which
contains the thread, only one thread, preferably the parent thread (main thread) is required to create the
memory mapped object and map it to the process’s virtual address space. The thread which creates the
memory mapped object can share the pointer to the mapped memory area as global pointer and other
threads of the process can use this pointer for reading and writing to the mapped memory area. If one
thread of a process tries to create a memory mapped object with the same name as that of an existing
mapping object, which is created by another thread of the same process, a new view of the mapping
object is created at a different virtual address of the process. This is same as one process trying to create
two views of the same memory mapped object©.
Communication. The major difference between shared memory and message passing technique is that,
through shared memory lots of data can be shared whereas only limited amount of info/data is passed
through message passing. Also message passing is relatively fast and free from the synchronisation
overheads compared to shared memory. Based on the message passing operation between the processes,
message passing is classified into
10.7.2.1 Message Queue Usually the process which wants to talk to another process posts the
message to a First-In-First-Out (FIFO) queue called ‘Message queue’, which stores the messages tem¬
porarily in a system defined memory object, to pass it to the desired process (Fig. 10.20). Messages are
sent and received through send (Name of the process to which the message is to besent,-mess age) and
receive (Name of the process from which the message is to be received, message) methods. The mes¬
sages are exchanged through a message queue. The implementation of the message queue, send and re¬
ceive methods are OS kernel dependent. The Windows XP OS kernel maintains a single system message
queue and one process/thread (Process and threads are used interchangeably here, since thread is the
basic unit of process in windows) specific’message queue. A thread which wants to communicate with
another thread posts the message to the system message queue. The kernel picks up the message from
the system message queue one at a time and examines the message for finding the destination thread and
then posts the message to the message queue of the corresponding thread. For posting a message to a
thread’s message queue, the kernel fills a message structure MSG and copies it to the message'queue of
the thread. The message structure MSG contains the handle of the process/thread for which the message
is intended, the message parameters, the time at which the message is posted, etc. A thread can simply
post a message to another thread and can continue its operation or it may wait for a response from the
thread to which the message is posted. The messaging mechanism is classified into synchronous and
asynchronous based on the behaviour of the message posting thread. In asynchronous messaging, the
message posting thread just posts the message to the queue and it will not wait for an acceptance (return)
from the thread to which the message is posted, whereas in synchronous messaging, the thread which
posts a message enters waiting state and waits for the message result from the thread to which the mes¬
sage is posted. The thread which invoked the send message becomes blocked and the scheduler will not
pick it up for scheduling. The PostMessage (HWND hWnd, UINTMsg, WPARAMwParam, LPARAM
(Fig. 10.20 Concept of message queue based indirect messaging for IPC
Real-Time Operating System (RTOS) based Embedded System Design
dwMaxMessages Specifies the maximum number of messages to queue at any point of time. Set this value to zero
-rypU'-y -* ■ to specify ho limit on the number of messages to queue at any point of time.
bReadAccess Specifies the Read Write access to the message queue. Set to TRUE to request read access to
the queue. Set to FALSE to request write access to the queue.
On successful execution, the Create MsgQueue API call returns a ‘Read Only’ or ‘Write Only’ handle
to the specified queue based on the bReadAccess member of the MSGQUEUEOPTIONS structure IpOp-
tions. If the queue with specified name already exists, a new handle, which points to the existing queue,
is created and a following call to GetLastError returns ERROR_ALREADY_EXISTS. If the function
fails it returns NULL. A single call to the CreateMsgQueue creates the queue for either ‘read’ or ‘write’
access. The CreateMsgQueue API should be called twice with the bReadAccess member of the MS¬
GQUEUEOPTIONS structure IpOptions set to TRUE and FALSE respectively in successive calls for
obtaining ‘Read only’ and ‘Write only’ handles to the specified message queue. The handle returning
by CreateMsgQueue API call is an event handle and, if it is a ‘Read Only’ access handle, it is signalled
by the message queue if a new message is placed in the queue. The signal is reset on reading the mes¬
sage by ReadMsgQueue API call. A ‘Write Only’ access handle to the message queue is signalled when
the queue is no longer full, i.e. when there is room for accommodating new messages. Processes can
monitor the handles with the help of the wait functions, viz. WaitForSingleObject or WaitForMulti-
pleObjects, supported by Windows CE. The OpenMsgQueue(HANDLE hSrcProc, HANDLE hMsgQ,
LPMSGQUEUEOPTlONS IpOptions) API call opens an existing named or unnamed message queue.
The parameter hSrcProc specifies the process handle of the process that owns the message queue and
hMsgQ specifies the handle of the existing message queue (Handle to the message queue returned by
the CreateMsgQueue function). As in the case of CreateMsgQueue, the IpOptions parameter points to a
MSGQUEUEOPTIONS structure that sets the properties of the message queue. On successful execution
the OpenMsgQueue API call returns a handle to the message queue and NULL if it fails. Normally the
OpenMsgQueue API is used for opening an unnamed message queue. The WriteMsgQueue(HANDLE
hMsgQ, LPVOID IpBuffer, DWORD cbDataSize, DWORD dwTimeout, DWORD dwFlags) API call is
used for writing a single message to the message queue pointed by the handle hMsgQ. IpBuffer points to
a buffer that contains the message to be written to the message queue. The parameter cbDataSize speci¬
fies the number of bytes stored in the buffer pointed by IpBuffer, which forms a message. The parameter
dwTimeout specifies the timeout interval in milliseconds for the message writing operation. A value of
zero specifies the write operation to return immediately without blocking if the write operation cannot
succeed. If the parameter is set to INFINITE, the write operation will block until it succeeds or the mes¬
sage queue signals the ‘write only’ handle indicating the availability of space for posting a message.
The dwFlags parameter sets the priority of the message. If it is set to MSGQUEUEJMSGALERT, the
message is posted to the queue as high priority or alert message. The Alert message is always placed in
the front of the message queue. This function returns TRUE if it succeeds and FALSE otherwise.
The ReadMsgQueue(HANDLE hMsgQ, LPVOID IpBuffer, DWORD cbBufferSize, LPDWORD
IpNumberOfBytesRead, DWORD dwTimeout, DWORD*pdwFlags) API reads a single message from
the message queue. The parameter hMsgQ specifies a handle to the message queue from which the
message needs to be read. IpBuffer points to a buffer for storing the message read from the queue. The
parameter cbBufferSize specifies the size of the buffer pointed by IpBuffer, in bytes. IpNumberOfBytes¬
Read specifies the number of bytes stored in the buffer. This is same as the number of bytes present in
Real-Time Operating System (RTOS) based Embedded System Design
the message which is read from the message queue. dwTimeout specifies the timeout interval in mil¬
liseconds for the message reading operation. The timeout values and their meaning are same as that of
the write message timeout parameter. The dwFlags parameter indicates the priority of the message. If
the message read from the message queue is a high priority message (alert message), dwFlags is set
to MSGQUEUE_MSGALERT. The function returns TRUE if it succeeds and FALSE otherwise. The
GetMsgQueuelnfo (HANDLE hMsgQ, LPMSGQUEUEINFO Iplnfo) API call returns the information
about a message queue specified by the handle hMsgQ. The message information is returned in a MS-
GQUEUEINFO structure pointed by Iplnfo. The details of the MSG.QUEUEINFO structure is explained
below.
f typo del MSG.QUEUEINFO {
I!',, DWORD dwSize;
k, DWORD, .dwFlags;
|p DWORD. dwMaxMessages;
fp; DWORD. cbMaxMessage;
y DWORD dwCurrent. Me s sage s;
V DWORD dwMaxQueueMessages;
| WORD wNumReaders;
I"'’ WORD wNumWriters;
i:}
< ..
MSGQUEUEINFO, *PMSGQUEUEINFO, FAR* LPMSGQUEUEINFO;
i Member Description
DwSize ' Specifies the size of the buffer passed in. J|.
dwF'ags Describes the behaviour of the message queue.Tt retrieves WHMSGOUEUEOPTIOKS.
dwFlags passed when the message queue is created with CreateMsgQueue API call.
dwMaxMessages Specifies the maximum number of messages to queue .at any point of time. This reflects
the MSGQUEUEOPTIONS. dwMaxMessages. value passed when the message queue is
created with CreateMsgQueue API call.
(cbMaxMessage -Ty;: . Specifies the maximum number of bytes in each message. This reflects the
'Sr,-*' MAv'.TAyA A T
;' MSGQUEUEOPTIONS.cbMaxMessage value passed when the message queue is created
\\'W CreateMsgQueue API call. r 'iwgp- f-y- * w
dwCurrentMessages Specifies the number of messages currently existing in the specified message queue.
dwMaxQueueMessages Specifies maximum number of messages that have ever been in the queue at one time.
wNumReaders Specifies the number of readers (processes which opened the message queue for reading)
subscribed to the message queue for reading.
wNumWriters Specifies the number of writers (processes which opened the message queue for writing)
subscribed to the message queue for writing.
The GetMsgQueuelnfo API call returns TRUE if it succeeds and FALSE otherwise. The
CloseMsgQueue(HANDLE hMsgQ) API call closes a message queue specified by the handle hMsgQ.
If a process holds a ‘read only’ and ‘write only’ handle to the message queue, both should be closed for
closing the message queue.
‘Message queue’ is the primary inter-task communication mechanism under VxWorks kernel. Mes¬
sage queues support two-way communication of messages of variable length. The two-way messaging
Introduction to Embedded Systems
between tasks can be implemented using one message queue for incoming messages and another one
for outgoing messages. Messaging mechanism can be used for task-to task and task to Interrupt Service
Routine (ISR) communication. We will discuss about the VxWorks’ message queue implementation in
a separate chapter.
10.7.2.2 Mailbox Mailbox is an alternate form of ‘Message queues’ and it is used in certain Real-
Time Operating Systems for IPC. Mailbox technique for IPC in RTOS is usually used for one way mes¬
saging. The task/thread which wants to send a message to other tasks/threads creates a mailbox for post¬
ing the messages. The threads which are interested in receiving the messages posted to the mailbox by
the mailbox creator thread can subscribe to the mailbox. The thread which creates the mailbox is known
as ‘mailbox server’ and the threads which subscribe to the mailbox are known as ‘mailbox clients’.
The mailbox server posts messages to the mailbox and notifies it to the clients which are subscribed to
the mailbox. The clients read the message from the mailbox on receiving the notification. The mailbox
creation, subscription, message reading and writing are achieved through OS kernel provided API calls.
Mailbox and message queues are same in functionality. The only difference is in the number of mes¬
sages supported by them. Both of them are used for passing data in the form of message(s) from a task
to another task(s). Mailbox is used for exchanging a single message between two tasks or between an In¬
terrupt Service Routine (ISR) and a task. Mailbox associates a pointer pointing to the mailbox and a wait
list to hold the tasks waiting for a message to
appear in the mailbox. The implementation
of mailbox is OS kernel dependent. The Mi-
croC/OS-II implements mailbox as a mecha¬
nism for inter-task communication. We will
discuss about the mailbox based IPC imple¬
r-t-
mentation under MicroC/OS-II in a latter
3O
chapter. Figure 10.21 given below illustrates
the mailbox based IPC technique.
communication. Whenever a specified signal occurs it is handled in a signal handler associated with the
signal. We will discuss about the signal based IPC mechanism for VxWorks’ kernel in a later chapter.
On security front, RPC employs authentication mechanisms to protect the systems against vulner¬
abilities. The client applications (processes) should authenticate themselves with the server for getting
access. Authentication mechanisms like IDs, public key cryptography (like DES, 3DES), etc. are used
by the client for authentication. Without authentication, any client can access the remote procedure. This
may lead to potential security risks.
Sockets are used for RPC communication. Socket is a logical endpoint in a two-way communication
link between two applications running on a network. A port number is associated with a socket so that
the network layer of the communication channel can deliver the data to the designated application.
Sockets are of different types, namely, Internet sockets (INET), UNIX sockets, etc. The INET socket
works on internet communication protocol TCP/IP, UDP, etc. are the communication protocols used by
INET sockets. INET sockets are classified into:
1. Stream sockets
2. Datagram sockets
Stream sockets are connection oriented and they use TCP to establish a reliable connection. On the
other hand, Datagram sockets rely on UDP for establishing a connection. The UDP connection is unre¬
liable when compared to TCP. The client-server communication model uses a socket at the client side
and a socket at the server side. A port number is assigned to both of these sockets. The client and server
should be aware of the port number associated with the socket. In order to start the communication, the
client needs to send a connection request to the server at the specified port number. The client should
be aware of the name of the server along with its port number. The server always listens to the specified
port number on the network. Upon receiving a connection request from the client, based on the success
of authentication, the server grants the connection request and a communication channel is established
between the client and server. The client uses the host name and port number of server for sending re¬
quests and server uses the client’s name and port number for sending responses.
If the client and server applications (both processes) are running on the same CPU, both can use the
same host name and port number for communication. The physical communication link between the
client and server uses network interfaces like Ethernet or Wi-Fi for data communication. The underlying
implementation of socket is OS kernel dependent. Different types of OSs provide different socket inter¬
faces. The following sample code illustrates the usage of socket for creating a client application under
Windows OS. Winsock (Windows Socket 2) is the library implementing socket functions for Win32.
#include <stdio.h> . ! •
♦include "winsock2.h" 1
//Specify the server address
#define SERVER "172.168.0.1"
//Spegify the server port
♦define PORT 5000
int buflength = 100;
char *sendbuf = "Hi from Client";
char buffer [buflength] ;
void main() {
/ J ************************ ~k-k ******-k ***********-k* ± ic ******* *
else
{
printf("Winsock Initialisation failed.An");
return; .... ■
} ■
//******** ********** ***************************************
// Create a SOCKET for connecting to server
’ SOCKET My Socket; .'( ' 1'’ *"/ C.
MySocket = socket(AF_INET, 50CK_STREAMr; IPPROTOJTCP) ; ../.
d^if“|(Hpb.cket == INVALID_SOCKET) ~ ' • * ' "/"■%
■ WSACleanup () ; ' .
.return; • . .f 7 ■ - - '-~
//*********************************************************
// Send command to server
if (send(MySocket, sendbuf, (int)strlen(sendbuf), 0 )
== SOCKET_ERROR) {
printf ("Sending data to server failed..An") ;
closesocket(MySocket);
WSACleanup();
return;
else
I
printf ("Successfully sent command to server...\n") ;
Introduction to Embedded Systems
’ closesocket
ocket (MySocket) ; . . ' ... • .
anup () ;
WSACleanupO
.V' ' ^ ;v ' c - ' .
return.pSfeftsi s » i -• 0 & i . . s.t
IS. • k m ms
. .*>/.'« ■
» v v> s * , - , „ , , 1 - . ^
t
1 J.
•.
fj '*>'» >• f *■<»¥ * 's <■ ,
‘ * ,, V1'' *« iU * _ 1- ^ 5 ^
-1 *, y
A>{V/ I
s ‘
* »/•“ * A-r-'»i>
»
vA J- 'W&v< /<. i ' .• j'-i-i’ ' ‘‘f ^ * A ” N i / is ( >Vv V> ** / xx,x_
f0£;
The above application tries to connect to a server machine with IP address 172.168.0.1 and port num¬
ber 5000. Change the values of SERVER and PORT to connect to a machine with different IP address
and port number. If the connection is success, it sends the data “Hi from Client” to the server and waits
for a response from the server and finally terminates the connection.
Under Windows, the socket function library Winsock should be initiated before using the socket
related functions. The function WSAStartup performs this initiation. The socketQ function call creates
a socket. The socket type, connection type and protocols for communication are the parameters for
socket creation. Here the socket type is INET (AFJNET) and connection type is stream socket (SOCK_
STREAM). The protocol selected for communication is TCP/IP (IP PROTO_TCP). After creating the
socket it is connected to a server, For connecting to server, the server address and port number should be
indicated in the connection request. The sockaddrjn structure specifies the socket type, IP address and
port of the server to be connected to. The connect () function connects the socket with a specified server.
If the server grants the connection request, the connect () function returns success. The sendQ function
is used for sending data to a server. It takes the socket name and data to be sent as parameters. Data from
server is received using the function call recvQ. It takes the socket name and details of buffer to hold the
received data as parameters. The TCP/IP network stack expects network byte order (Big Endian: Higher
order byte of the data is stored in lower memory address location) for data. The function htonsQ con¬
verts the byte order of an unsigned short integer to the network order. The closesocketQ function closes
the socket connection. On the server side, the server creates a socket using the function socket() and
binds the socket with a port using the bind() function. It listens to the port bonded to the socket for any
incoming connection request. The function listenQ performs this. Upon receiving a connection request,
the server accepts it. The function accept() performs the accepting operation. Now the connectivity is
established. Server can receive and transmit data using the function calls recvQ and sendQ respectively.
The implementation of the server application is left to the readers as an exercise.
each other with different 1PC mechanisms including shared memory and variables. Imagine a situation
where two processes try to access display hardware connected to the system or two processes try to ac¬
cess a shared memory area where one process tries to write to a memory location when the other process
is trying to read from this. What could be the result in these scenarios? Obviously unexpected results.
How these issues can be addressed? The solution is, make each process aware of the access of a shared
resource either directly or indirectly. The act of making processes aware of the access of shared resources
by each process to avoid conflicts is known as ‘ Task/Process Synchronisation'. Various synchronisation
issues may arise in a multitasking environment if processes are not synchronised properly. The following
sections describe the major task communication synchronisation issues observed in multitasking and the
commonly adopted synchronisation techniques to overcome these issues.
// Process A ''
^jypid,rProc.ess^AiHypid]{ <■ i pft-
.int i; ■/ iftftJ,/ . , vft. y. ft'"/ ' fW ,,.
for (i =0; i<5; i++) "ft ' ft” ft •’ ”■ /ftftft
pqStV’T'f'-1'"ftv ;iV ’ ' *4' ‘ ’ft;,...ft- !ft„. \ftft 1 ' •'*• * r. ■ .‘p ft'
I ^ ’
// 'Process B
void ProcessB(void) {
, Tnt j;
for (j =5; j<10; j++)
{ *
if (Buffer[j] > 0)
counter++;
"ft
1 s
//Main Thread.
. int main (.) {
DWORD id; .
From a programmer perspective the value of counter will be 10 at the end of execution of processes
A & B. But ‘it need not be always’ in a real world execution of this piece of code under a multitasking
kernel. The results depend on the process scheduling policies adopted by the OS kernel. Now let’s dig
into the piece of code illustrated above. The program statement counter++; looks like a single statement
from a high level programming language (‘C’ language) perspective. The low level implementation of
this statement is dependent on the underlying processor instruction set and the (cross) compiler in use.
The low level implementation of the high level program statement counter++; under Windows XP op¬
erating system running on an Intel Centrino Duo processor is given below, The code snippet is compiled
with Microsoft Visual Studio 6.0 compiler.
.. ., , •* i
mov eax,dword ptr [ebp-4];Load counter m Accumulator . ,
add eax,lIncrement Accumulator by 1
mov dword ptr [ebp-4],eax /Store count
At the processor instruction level, the value of the variable counter is loaded to the Accumulator
register (EAX register). The memory variable counter is represented using a pointer. The base pointer
register (EBP register) is used for pointing to the memory variable counter. After loading the contents
of the variable counter to the Accumulator, the Accumulator content is incremented by one using the
add instruction. Finally the content of Accumulator is loaded to the memory location which repre¬
sents the variable counter. Both the processes Process A and Process B contain the program statement
counter++; Translating this into the machine instruction.
fcilKsiiA
nov dword ptr [ebp-4],eax mov dword ptr [ebp-41,eax
Imagine a situation where a process switching (context switching) happens from Process A to Process
B when Process A is executing the counter++; statement. Process A accomplishes the counter++; state¬
ment through three different low level instmctions. Now imagine that the process switching happened at
the point where Process A executed the low level instruction, ‘mov eax,dword ptr [ebp-4]’ and is about
to execute the next instruction ‘add eax,l’. The scenario is illustrated in Fig. 10.23.
Process B increments the shared variable ‘counter’ in the middle of the operation where Process A
tries to increment it. When Process A gets the CPU time for execution, it starts from the point where
it got interrupted (If Process B is also using the same registers eax and ebp for executing counter++;
instruction, the original content of these registers will be saved as part of the context saving and it will
be retrieved back as part of context retrieval, when process A gets the CPU for execution. Hence the
content of eax and ebp remains intact irrespective of context switching). Though the variable counter is
incremented by Process B, Process A is unaware of it and it increments the variable with the old value.
This leads to the loss of one increment for the variable counter. This problem occurs due to non-atomic!
operation on variables. This issue wouldn’t have been occurred if the underlying actions corresponding
to the program statement counter++; is finished in a single CPU execution cycle. The best way to avoid
this situation is make the access and modification of shared variables mutually exclusive; meaning when
one process accesses a shared variable, prevent the
other processes from accessing it. We will discuss
this technique in more detail under the topic ‘ Task
Synchronisation techniques' in a later section of
this chapter.
To summarise, Racing or Race condition is the
situation in which multiple processes compete
(race) each other to access and manipulate shared
data concurrently. In a Race condition the final val¬
ue of the shared data depends on the process which
acted on the data finally.
10.8.1.2 Deadlock A race condition produces
incorrect results whereas a deadlock condition cre¬
ates a situation where none of the processes are able
to make any progress in their execution, resulting
in a set of deadlocked processes. A situation very
similar to our traffic jam issues in a junction as il¬
lustrated in Fig. 10.24.
/
Deadlock Handling A smart OS may foresee the deadlock condition and will act proactively to
avoid such a situation. Now if a deadlock occurred, how the OS responds to it? The reaction to deadlock
condition by OS is nonuniform. The OS may adopt any of the following techniques to detect and prevent
deadlock conditions.
Ignore Deadlocks: Always assume that the system design is deadlock free. This is acceptable for the
reason the cost of removing a deadlock is large compared to the chance of happening a deadlock. UNIX
is an example for an OS following this principle. A life critical system cannot pretend that it is deadlock
free for any reason.
Detect and Recover: This approach suggests the detection of a deadlock situation and recovery from
it. This is similar to the deadlock condition that may arise at a traffic junction. When the vehicles from
different directions compete to cross the junction, deadlock (traffic jam) condition is resulted. Once a
Real-Time Operating System (RTOS) based Embedded System Design
deadlock (traffic jam) is happened at the junction, the only solution is to back up the vehicles from one
direction and allow the vehicles from opposite direction to cross the junction. If the traffic is too high,
lots of vehicles may have to be backed up to resolve the traffic jam. This technique is also known as
‘back up cars’ technique (Fig. 10.26).
Operating systems keep a resource graph in their memory. The resource graph is updated on each
resource request and release. A deadlock condition can be detected by analysing the resource graph by
graph analyser algorithms. Once a deadlock condition is detected, the system can terminate a process or
preempt the resource to break the deadlocking cycle.
Avoid Deadlocks: Deadlock is avoided by the careful resource allocation techniques by the Operating
System. It is similar to the traffic light mechanism at junctions to avoid the traffic jams.
Prevent Deadlocks: Prevent the deadlock condition by negating one of the four conditions favouring
the deadlock situation.
• Ensure that a process does not hold any other resources when it requests a resource. This can be
achieved by implementing the following set of rules/guidelines in allocating resources to pro¬
cesses.
1. A process must request all its required resource and the resources should be allocated before
the process begins its execution.
2. Grant resource allocation requests from processes only if the process does not hold a resource
currently.
Introduction to Embedded Systems
• Ensure that resource preemption (resource releasing) is possible at operating system level. This
can be achieved by implementing the following set of rules/guidelines in resources allocation and
releasing.
1. Release all the resources currently held by a process if a request made by the process for a new
resource is not able to fulfil immediately.
2. Add the resources which are preempted (released) to a resource list describing the resources
which the process requires to complete its execution.
3. Reschedule the process for execution only when the process gets its old resources and the new
resource which is requested by the process.
Imposing these criterions may introduce negative impacts like low resource utilisation and starvation
of processes.
Livelock The Livelock condition is similar to the deadlock condition except that a process in livelock
condition changes its state with time. While in deadlock a process enters in wait state for a resource and
continues in that state forever without making any progress in the execution, in a livelock condition a
process always does something but is unable to make any progress in the execution completion. The
livelock condition is better explained with the real world example, two people attempting to cross each
other in a narrow corridor. Both the persons move towards each side of the corridor to allow the opposite
person to cross. Since the corridor is narrow, none of them are able to cross each other. Here both of the
persons perform some action but still they are unable to achieve their target, cross each other. We will
make the livelock, the scenario more clear in a later section-The Dining Philosophers ’Problem, of this
chapter.
Starvation In the multitasking context, starvation is the condition in which a process does not get
the resources required to continue its execution for a long time. As time progresses the process starves
on resource. Starvation may arise due to various conditions like byproduct of preventive measures of
deadlock, scheduling policies favouring high priority tasks and tasks with shortest execution time, etc.
1Q.8.1.3 The Dining Philosophers’ Problem The ‘Dining philosophers ’problem’ is an interest¬
ing example for synchronisation issues in resource utilisation. The terms ‘dining’, ‘philosophers’, etc.
may sound awkward in the operating system context, but it is the best way to explain technical things
abstractly using non-technical terms. Now coming to the problem definition:
Five philosophers (It can be V. The number 5 is taken for illustration) are sitting around a round
table, involved in eating and brainstorming (Fig. 10.37). At any point of time each philosopher will be
in any one of the three states: eating, hungry or brainstorming. (While eating the philosopher is not in¬
volved in brainstorming and while brainstorming the philosopher is not involved in eating). For eating,
each philosopher requires 2 forks. There are only 5 forks available on the dining table (V for V number
of philosophers) and they are arranged in a fashion one fork in between two philosophers. The philoso¬
pher can only use the forks on his/her immediate left and right that too in the order pickup the left fork
first and then the right fork. Analyse the situation and explain the possible outcomes of this scenario.
Let’s analyse the various scenarios that may occur in this situation.
Scenario 1: All the philosophers involve in brainstorming together and try to eat together. Each philoso¬
pher picks up the left fork and is unable to proceed since two forks are required for eating the spaghetti
present in the plate. Philosopher 1 thinks that Philosopher 2 sitting to the right of him/her will put the
fork down and waits for it. Philosopher 2 thinks that Philosopher 3 sitting to the right of him/her will
Real-Time Operating System (RTOS) based Embedded System Design
put the fork down and waits for it, and so on. This forms a circular chairmf un-granted requests. If the
philosophers continue in this state waiting for the fork from the philosopher sitting to the right of each,
they will not make any progress in eating and this will result in starvation of the philosophers and
deadlock.
Scenario 2: All the philosophers start brainstorming together. One of the philosophers is hungry and he/
she picks up the left fork. When the philosopher is about to pick up the right fork, the philosopher sitting,
to his right also become hungry and tries to grab the left fork which is the right fork of his neighbouring
philosopher who is trying to lift it, resulting in a ‘Race condition’..
Scenario 3: All the philosophers involve in brainstorming together and try to eat together. Each philoso¬
pher picks up the left fork and is unable to proceed, since two forks are required for eating the spaghetti
present in the plate. Each of them anticipates that the adjacently sitting philosopher will put his/her fork
down and waits for a fixed duration arid after this puts the fork down. Each of them again tries to lift the
fork after a fixed duration of time. Since all philosophers are trying to lift the fork at the same time, none
of them will be able to grab two forks. This condition leads to livelock and starvation of philosophers,
where each philosopher tries to do something, but they are unable to make any progress in achieving
the target.
Figure 10.28 illustrates these scenarios.
Solution: We need to find out alternative solutions to avoid tht deadlock, livelock, racing and starva¬
tion condition that may arise due to the concurrent access of forks by philosophers. This situation can be
handled in many ways by allocating the forks in different allocation techniques including Round Robin
allocation, FIFO allocation, etc. But The requirement is that the solution should be optimal, avoiding
\
The ‘Real Problems’ in the‘Dining Philosophers problem' (a) Starvation and Deadlock (b) Racing
(c) Livelock and Starvation
deadlock and starvation of the philosophers and allowing maximum number of philosophers to eat at a
time. One solution that we could think of is:
• Imposing rules in accessing the forks by philosophers, like: The philosophers should put down the
fork he/she already have in hand (left fork) after waiting for a fixed duration for the second fork
(right fork) and should wait for a fixed time before making the next attempt.
This solution works fine to some extent, but, if all the philosophers try to lift the forks at the same
time, a livelock situation is resulted. , . '
Another solution which gives maximum concurrency that can be thought of is each philosopher ac¬
quires a semaphore (mutex) before picking up any fork. When a philosopher feels hungry he/she checks
whether the philosopher sitting to the left and right of him is already using the fork, by checking the state
Real-Time Operating System (RTOS) based Embedded System Design
of the associated semaphore. If the forks are in use by the neighbouring philosophers, the philosopher
waits till the forks are available. A philosopher when finished eating puts the forks down and informs
the philosophers sitting to his/her left and right, who are hungry (waiting for the forks), by signalling
the semaphores associated with the forks. We will discuss about semaphores and mutexes at a latter sec¬
tion of this chapter. In the operating system context, the dining philosophers represent the processes and
forks represent the resources. The dining philosophers’ problem is an analogy of processes competing
for shared resources and the different problems like racing, deadlock, starvation and Iivelock arising
from the competition.
10.8.1.4 Producer-Consumer/Bounded Buffer Problem Producer-Consumer problem is a
common data sharing problem where two processes concurrently access a shared buffer with fixed size.
A thread/process which produces data is called ‘Producer thread/process’ and a thread/process which
consumes the data produced by a producer thread/process is known as ‘Consumer thread/process\
Imagine a situation where the producer thread keeps on producing data and puts it into the buffer and the
consumer thread keeps on consuming the data from the buffer and there is no synchronisation between
the two. There may be chances where in which the producer produces data at a faster rate than the rate at
which it is consumed by the consumer. This will lead to ‘buffer overrun’ where the producer tries to put
data to a full buffer. If the consumer consumes data at a faster rate than the rate at which it is produced
by the producer, it will lead to the situation '‘buffer under-run’ in which the consumer tries to read from
an empty buffer. Both of these conditions will lead to inaccurate data and data loss. The following code
snippet illustrates the producer-consumer problem
\ iinclude’ <windows.h> .
1 1! inc Lucie Cstdio. h> ' ' V . , "* ’ W-
: fi define N 20 //Define buffer size as 20 ' . , •./ - ... , J" , '
Ifirit buffet [N]; /‘/'Shared-buffer for producer '& consumer . b >- ..■* ' y:
\~j /*******************************************************************
if’/
p//Consumer thread
pdoid consumer_thread(void). {
fc' int y=0,value; 'v
I while(true) {
for (y=0;y<N;y++)
i \
Introduction to Embedded Systems
{
value=buffer[y];
printf("Consumed : Buffer [%d] = %4d\n", y, value);
Sleep(20);
}
■}
}
//*******************************************************************
//Main Thread
int main. ()
f '
DWORD thread_id;
/'/Create Producer thread
GreateThread(NULL, 0, "
(LPTHRKAD START ROUTINE) producer_t.hread,
' NULL,0,&thread_id);
//-Create Consumer thread
CreateThread (NULL,.0,
(LPTHREAD_START_ROUTJ.NE ) co.nsumer_thread,
,»• NULL,0,&thread_id);
//Wait for some time and exit
Sleep(500);
return 0;
}
Here the ‘producer thread’ produces random numbers and puts it in a buffer of size 20. If the ‘producer
thread’ fills the buffer fully it re-starts the filling of the buffer from the bottom. The ‘consumer thread’
consumes the data produced by the ‘producer thread’. For consuming the data, the ‘consumer thread’
reads the buffer which is shared with the ‘producer thread’. Once the ‘consumer thread’ consumes all the
data, it starts consuming the data from the bottom of the buffer. These two threads run independently and
are scheduled for execution based on the scheduling policies adopted by the OS. The different situations
that may arise based on the scheduling of the ‘producer thread’ and ‘consumer thread’ is listed below.
1. ‘Producer thread’ is scheduled more frequently than the ‘consumer thread’: There are chances for
overwriting the data in the buffer by the ‘producer thread’. This leads to inaccurate data.
2. Consumer thread’ is scheduled more frequently than the ‘producer thread’: There are chances for
reading the old data in the buffer again by the ‘consumer thread’. This will also lead to inaccurate
data.
The output of the above program when executed on a Windows XP machine is shown in Fig. 10.29.
The output shows that the consumer thread runs faster than the producer thread and most often leads
to buffer under-run and thereby inaccurate data.
Note
It should be noted that the scheduling of the threads ‘producerJhredd' and1 consumer Jhread' is OS
kernel scheduling policy dependent and you may not get the same output all the time when you run
this piece of code in Windows XP.
The producer-consumer problem can be rectified in various methods. One simple solution is the
‘sleep and wake-up'. The ‘sleep and wake-up' can be implemented in various process synchronisation
techniques like semaphores, mutex, monitors, etc. We will discuss it in a latter section of this chapter.
Real-Time Operating System (RTOS) based Embedded System Design
(; .j Running
Process A
(High Priority)
Process B
(Med Priority)
, Process C
(Low Priority)
through a mutual exclusion mechanism like Binary Semaphore S. Imagine a situation where Process C
is ready and is picked up for execution by the scheduler and ‘Process C’ tries to access the shared vari¬
able ‘X’. ‘Process C’ acquires the ‘Semaphore S’ to indicate the other processes that it is accessing the
shared variable ‘X’. Immediately after ‘Process C’ acquires the ‘Semaphore S’, ‘Process B’ enters the
‘Ready’ state. Since ‘Process B’ is of higher priority compared to ‘Process C’, ‘Process C’ is preempted
and ‘Process B’ starts executing. Now imagine ‘Process A’ enters the ‘Ready’ state at this stage. Since
‘Process A’ is of higher priority than ‘Process B’, ‘Process B’ is preempted and ‘Process A’ is scheduled
for execution. ‘Process A’ involves accessing of shared variable ‘X’ which is currently being accessed
by ‘Process C’. Since ‘Process C’ acquired the semaphore for signalling the access of the shared variable
‘X’, ‘Process A’ will not be able to access it. Thus ‘Process A’ is put into blocked state (This condition
is called Pending on resource). Now ‘Process B’ gets the CPU and it continues its execution until it
relinquishes the CPU voluntarily or enters a wait state or preempted by another high priority task. The
highest priority process ‘Process A’ has to wait till ‘Process C’ gets a chance to execute and release the
semaphore. This produces unwanted delay in the execution of the high priority task which is supposed
to be executed immediately when it was ‘Ready’.
, Priority inversion may be sporadic in nature but can lead to potential damages as a result of missing
critical deadlines. Literally speaking, priority inversion ‘inverts’ the priority of a high priorityytask with
that of a low priority task. Proper workaround mechanism should be adopted for handling the priority
inversion problem. The commonly adopted priority inversion workarounds are:
Priority Inheritance: A low-priority task that is currently accessing (by holding the lock) a shared
resource requested by a high-priority task temporarily ‘ inherits’ the priority of that high-priority task,
from the moment the high-priority task raises the request. Boosting the priority of the low priority task
to that of the priority of the task which requested the shared resource holding by the low priority task
eliminates the preemption of the low priority task by other tasks whose priority are below that of the
task requested the shared resource and thereby reduces the delay in waiting to get the resource requested
by the high priority task. The priority of the low priority task which is temporarily boosted to high is
Real-Time Operating System (RTOS) based Embedded System Design
brought to the original value when it releases the shared resource. Implementation of Priority inheri¬
tance workaround in the priority inversion problem discussed for Process A, Process B and Process C
example will change the execution sequence as shown in Fig. 10.31.
Process A
(High Priority)
Process B
(Med Priority)
Process C
(Low Priority)
Priority inheritance is only a work around and it will not eliminate the delay in waiting the high prior¬
ity task to get the resource from the low priority task. The only thing is that it helps the low priority task
to continue its execution and release the shared resource as soon as possible. The moment, at which the
low priority task releases the shared resource, the high priority task kicks the low priority task out and
grabs the CPU - A true form of selfishness©. Priority inheritance handles priority inversion at the cost
of run-time overhead at scheduler. It imposes the overhead of checking the priorities of all tasks which
tries to access shared resources and adjust the priorities dynamically.
Priority Ceiling: In ‘Priority Ceiling’, a priority is associated with each shared resource. The priority
associated to each resource is the priority of the highest priority task which uses this shared resource.
This priority level is called ‘ceiling priority’. Whenever a task accesses a shared resource, the scheduler
elevates the priority of the task to that of the ceiling priority of the resource. If the task which accesses
the shared resource is a low priority task, its priority is temporarily boosted to the priority of the high¬
est priority task to which the resource is also shared. This eliminates the pre-emption of the task by
other medium priority tasks leading to priority inversion. The priority of the task is brought back to the
original level once the task completes the accessing of the shared resource. ‘Priority Ceiling’ brings
the added advantage of sharing resources without the need for synchronisation techniques like locks.
Since the priority of the task accessing a shared resource is boosted to the highest priority of the task
among which the resource is shared, the concurrent access of shared resource is automatically handled.
. Another advantage of ‘Priority Ceiling’ technique is that all the overheads are at compile time instead of
Introduction to Embedded Systems
run-time. Implementation of ‘priority ceiling’ workaround in the priority inversion problem discussed
for Process A, Process B and Process C example will change the execution sequence as shown in
Fig. 10.32.
Process A
(High Priority)
Process B
(Med Priority)
Process C
(Low Priority)
Jhe biggest drawback of ‘Priority Ceiling’ is that it may produce hidden priority inversion. With ‘Pri¬
ority Ceiling’ technique, the priority of a task is always elevated no matter another task wants the shared
resources. This unnecessary priority elevation always boosts the priority of a low priority task to that of
the highest priority tasks among which the resource is shared and other tasks with priorities higher than
that of the low priority task is not allowed to preempt the low priority task when it is accessing a shared
resource. This always gives the low priority task the luxury of running at high priority when accessing
shared resources©.
should not read from the buffer if it is empty. Hence proper synchronisation should be provided to
implement this sequence of operations.
3. Communicating between processes.
The code memory area which holds the program instructions (piece of code) for accessing a shared
resource (like shared memory, shared variables, etc.) is known as ‘critical section’. In order to synchro¬
nise the access to shared resources, the access to the critical section should be exclusive. The exclusive
access to critical section of code is provided through mutual exclusion mechanism. Let us have a look
at how mutual exclusion is important in concurrent access. Consider two processes Process A and Pro¬
cess B running on a multitasking system. Process A is currently running and it enters its critical section.
Before Process A completes its operation in the critical section, the scheduler preempts Process A and
schedules Process B for execution (Process B is of higher priority compared to Process A). Process B
also contains the access to the critical section which is already in use by Process A. If Process B contin¬
ues its execution and enters the critical section which is already in use by Process A, a racing condition
will be resulted. A mutual exclusion policy enforces mutually exclusive access of critical sections.
Mutual exclusions can be enforced in different ways. Mutual exclusion blocks a process. Based on
the behaviour of the blocked process, mutual exclusion methods can be classified into two categories. In
the following section we will discuss them in detail.
10.8.2.1 Mutual Exclusion through Busy Waiting/Spin Lock ‘Busy waiting’ is the simplest
method for enforcing mutual exclusion. The following code snippet illustrates how ‘Busy waiting’
enforces mutual exclusion.
//Inside parent thread/main thread corresponding to a process’
. bool bFlag; //Global declaration of lock Variable.
'bFlag= FALSE; //Initialise the lock to indicate'it is available. . , ;
//...:.
//Inside the child threads/threads ,of a process '
while (bFlag == TRUE); //Check the lock for availability
' bFlag=TRUE; //Lock"is available. Acquire the lock
• //Rest of the: source,code dealing with shared resource access
The ‘Busy waiting’ technique uses a lock variable for implementing mutual exclusion. Each process/
thread checks this lock variable before entering the critical section. The lock is set to by a process/
thread if the process/thread is already in its critical section; otherwise the lock is set to l0\ The major
challenge in implementing the lock variable based synchronisation is the non-availability of a single
atomic instruction! which combines the reading, comparing and setting of the lock variable. Most of¬
ten the three different operations related to the locks, viz. the operation of Reading the lock variable,
checking its present value and setting it are achieved with multiple low level instructions. The low/level
implementation of these operations are dependent on the underlying processor instruction set and the
(cross) compiler in use. The low level implementation of the ‘Busy waiting’ code snippet, which we
discussed earlier, under Windows XP operating system running on an Intel Centrino Duo processor is
given below. The code snippet is compiled with Microsoft Visual Studio 6.0 compiler.
I
■">-D:\Examples\counter.cpp.----
;vl: . #include <stdio.h> . V. !
i-2-i finclude <windows.h> ” •* i
i ■ /
run-time. Implementation of ‘priority ceiling’ workaround in the priority inversion problem discussed
for Process A, Process B and Process C example will change the execution sequence as shown in
Fig. 10.32.
A
| ■ | Waiting
hggtjij Running
oo
3 3 bO
O C
Yi &i
^ to <D
• r-t
O O s o> d 3
'OhC £ -pd rp. € “
•s s s »
3 ■"* « i
O
cr ■o «
o c
E rid Cx, to
w a ~
Ph
u^S ° s
w -a
« ‘C '
m 3
c/i O
C/5 Vh
> oo a> cl,
o
8 I
Ph
O
Ih
P,
Process A
(High Priority)
Process B
(Med Priority)
Process C
(Low Priority)
/'he biggest drawback of ‘Priority Ceiling’ is that it may produce hidden priority inversion. With ‘Pri¬
ority Ceiling’ technique, the priority of a task is always elevated no matter another task wants the shared
resources. This unnecessary priority elevation always boosts the priority of a low priority task to that of
the highest priority tasks among which the resource is shared and other tasks with priorities higher than
that of the low priority task is not allowed to preempt the low priority task when it is accessing a shared
resource. This always gives the low priority task the luxury of running at high priority when accessing
shared resources©.
should not read from the buffer if it is empty. Hence proper synchronisation should be provided to
implement this sequence of operations.
3. Communicating between processes.
The code memory area which holds the program instmctions (piece of code) for accessing a shared
resource (like shared memory, shared variables, etc.) is known as 1critical section'. In order to synchro¬
nise the access to shared resources, the access to the critical section should be exclusive. The exclusive
access to critical section of code is provided through mutual exclusion mechanism. Let us have a look
at how mutual exclusion is important in concurrent access. Consider two processes Process A and Pro¬
cess B running on a multitasking system. Process A is currently running and it enters its critical section.
Before Process A completes its operation in the critical section, the scheduler preempts Process A and
schedules Process B for execution (Process B is of higher priority compared to Process A). Process B
also contains the access to the critical section which is already in use by Process A. If Process B contin¬
ues its execution and enters the critical section which is already in use by Process A, a racing condition
will be resulted. A mutual exclusion policy enforces mutually exclusive access of critical sections.
Mutual exclusions can be enforced in different ways. Mutual exclusion blocks a process. Based on
the behaviour of the blocked process, mutual exclusion methods can be classified into two categories. In
the following section we will discuss them in detail.
10.8.2.1 Mutual Exclusion through Busy Waiting/Spin Lock ‘Busy waiting1 is the simplest
method for enforcing mutual exclusion. The following code snippet illustrates how ‘Busy waiting’
enforces mutual exclusion.
//Inside parent 'thread/main thread corresponding to a process
bool bFlag; //Global declaration of lock-Variable./ ' ;T I
/ - ^ .
'bFlag= FALSE; //Initialise the lock to indicate it is available,
ft S ^ ;
mmm
/Inside '.the;; chi Id threads/threads 70 f'
while(bFlag == TRUE); //Check the lock for availability is
- -bF,lag=TRUE;. //Lock is available.Acquire the lock W - -y t '
//Rest. of-the source code dealing with shaped resource access ' ■
The ‘Busy waiting’ technique uses a lock variable for implementing mutual exclusion. Each process/
thread checks this lock variable before entering the critical section. The lock is set to 7’ by a process/
thread if the process/thread is already in its critical section; otherwise the lock is set to ‘ O’. The major
challenge in implementing the lock variable based synchronisation is the non-availability &f a single
atomic instruction! which combines the reading, comparing and setting of the lock variable. Most of¬
ten the three different operations related to the locks, viz. the operation of Reading the lock variable,
checking its present value and setting it are achieved with multiple low level instmctions. The low/level
implementation of these operations are dependent on the underlying processor instruction set and the
(cross) compiler in use. The low level implementation of the ‘Busy waiting’ code snippet, which we
discussed earlier, under Windows XP operating system running on an Intel Centrino Duo processor is
given below. The code snippet is compiled with Microsoft Visual Studio 6.0 compiler.
I
3:
4: int main()
5: {
/'/Code .memory Opcode Operand
00401010' push ebp-
004010.11 . mov ebp,esp
00401013 sub esp,44h
00401016 , push , .u-: ebx ; .
004010.17 push esi s
00401018 , „ push ; edi id N
00401019 ■ . lea . edi,[ebp-44h]
00401 QIC mov< ecx,llh, -
00401021 1 . ’ .pov.i - J - eax,OCCCCCCCCh
00401026 I'’ T'l/ieprfdtbs dword ptr .[edi]
The assembly language instructions reveals that the two high level instructions (while(bFlag==false);
and bFlag=true;), corresponding to the operation of reading the lock variable, checking its present
value and setting it is implemented in the processor level using six low level instructions. Imagine a
situation where ‘Process 1’ read the lock variable and tested it and found that the lock is available and
it is about to set the lock for acquiring the critical section (Fig. 10.33). But just before ‘Process 1’ sets
the lock variable, ‘Process 2’ preempts ‘Process 1’ and starts executing. ‘Process 2’ contains a critical
section code and it tests the lock variable for its availability. Since ‘Process 1 ’ was unable to set the lock
variable, its state is still ‘O’ and ‘Process 2’ sets it and acquires the critical section. Now the scheduler
preempts ‘Process 2’ and schedules ‘Process 1’before ‘Process 2’ leaves the critical section. Remember,
‘Process 1 ’ was preempted at a point just before setting the lock variable (‘Process 1’ has already tested
the lock variable just before it is preempted and found that the lock is available). Now ‘Process 1’ sets
the lock variable and enters the critical section©. It violates the mutual exclusion policy and may pro¬
duce unpredicted results.
The above issue can be effectively tackled by combining the actions of reading the lock variable,
testing its state and setting the lock into a single step. This can be achieved with the combined hardware
and software support. Most of the processors support a single instruction ‘ Test and Set Lock (TSL)’ for
testing and setting the lock variable. The ‘Test and Set Lock (TSL)' instruction call copies the value of
the lock variable and sets it to a nonzero value. It should be noted that the implementation and usage of
‘Test and Set Lock (TSL)’ instruction is processor architecture dependent. The Intel 486 and the above
family of processors support the ‘ Test and Set Lock (TSL)’ instruction with a special instruction CMPX¬
CHG—Compare and Exchange. The usage of CMPXCHG instmction is given below.
This instruction compares the Accumulator (EAX register) with ‘dest’.. If the Accumulator and ‘dest’
contents are equal, ‘dest’ is loaded with ‘src’. If not, the Accumulator is loaded with ‘dest’. Executing
this instruction changes the six status bits of the Program Control and Status register EFLAGS. The
destination (‘dest’) can be a register or a memory location. The source (‘src’) is always a register. From
a programmer’s perspective the operation of CMPXCHG instruction can be viewed as:
. i|fif (accumulator == destination)
fei: ■ :• 1 *. -
• ZF -1; //Set the Zero Flag of EFLAGS Register
, ■ ■ destination ^source;’” ' v ’ ;
}
else
{
ZF = 0; //Reset the Zq-rg;.F-lag of EFLAGS Register
accumulator '= destination;'*
} 1
Introduction to Embedded Systems
The process/thread checks the lock variable to see whether its state is ‘O’ and sets its state to ‘ 1 ’ if its
state is ‘O’, for acquiring the lock. To implement this at the 486 processor level, load the accumulator
with ‘0’ and a general purpose register with 11 ’ and compare the memory location holding the lock vari¬
able with accumulator using CMPXCHG instruction. This instmction makes the accessing, testing and
modification of the lock variable a single atomic instmction. How the CMPXCHG instmction support
provided by the Intel® family of processors (486 and above) is made available to processes/threads is
OS kernel implementation dependent. Let us see how this feature is implemented by Windows Operat¬
ing systems. Windows CE/Windows XP kernels support the compare and exchange hardware feature
provided by Intel® family of processors, through the API call InterlockedCompareExchange (LPLONG
Destination, LONG Exchange, LONG Comperand). The variable Destination is the long pointer to the
destination variable. The Destination variable should be of type llong\ The variable Exchange rep¬
resents the exchange value. The value of Destination variable is replaced with the value of Exchange
variable. The variable Comperand specifies the value which needs to be compared with the value of
Destination variable. The function returns the initial value of the variable 'Destination'. The following
code snippet illustrates the usage of this API call for thread/process synchronisation.
//Inside parent thread/J'main -thread .corresponding. to a'process
long bFlag; //Global ..declaration of lock Variable.
bFlag=0; '././Initialise the„lock. t,o indicate it .is available. :
. //..:...1;....r...4....
//Inside the child threads/ -threads of a process
//.Check the lock fpr availability & aqguire the lock if available
while - (InterlockedCompareExchange (ScbFlag, 1,,, 0) — 1) ;
♦include "stdafx.h"
♦include <intrin.h>
♦include <windows.h>
long bFlag; //Global declaration of lock Variable.
//Declare InterlockedCompareExchange as intrinsic function
♦pragma intrinsic(_InterlockedCompareExchange)
void child_thread(void)
{
//Inside the child thread of a process
//Check the lock for availability & acquire the lock if available.
//The lock can be set by any other threads
while (^InterlockedCompareExchange (&bFlag, 1, 0) == 1);
//Rest of the source code dealing with shared resource access
//...
return;
Real-Time Operating System (RTOS) based Embedded System Design
i .:v,.// • ,.'s. - a ■/
Note: Visual Studio 2005 or a later version of the compiler, which supports interlocked intrinsic
functions, is required for compiling this application. The assembly code generated for the intrinsic
interlocked function while (JnterlockedCompareExchange (&bFlag, 1, 0) == 1); when compiledus-
ing Visual Studio 2005 compiler, on Windows XP platform with Service Pack 2 running on an Intel®
Centrino® Duo processor is given below. It clearly depicts the usage of the cmpxchg instruction
The Intel 486 and above family 6f processors provide hardware level support for atomic execution of
increment and decrement operations also. The XADD low level instruction implements atomic execu¬
tion of increment and decrement operations. Windows CE/XP kernel makes these features available to
the users through a set of Interlocked function API calls. The API call Interlockedlncrement (LPLONG
Introduction to Embedded Systems
IpAddend) increments the value of the variable pointed by IpAddend and the API InterlockedDecrement
(LPLONG IpAddend) decrements the value of the variable pointed by IpAddend.
The lock based mutual exclusion implementation always checks the state of a lock and waits till the
lock is available. This keeps the processes/threads always busy and forces the processes/threads to wait
for the availability of the lock for proceeding further. Hence this synchronisation mechanism is popu¬
larly known as ‘Busy waiting’. The ‘Busy waiting’ technique can also be visualised as a lock around
which the process/thread spins, checking for its availability. Spin locks are useful in handling scenarios
where the processes/threads are likely to be blocked for a shorter period of time on waiting the lock, as
they avoid OS overheads on context saving and process re-scheduling. Another drawback of Spin lock
based synchronisation is that if the lock is being held for a long time by a process and if it is preempted
by the OS, the other threads waiting for this lock may have to spin a longer time for getting it. The ‘Busy
waiting’ mechanism keeps the process/threads always active, performing a task which is not useful and
leads to the wastage of processor time and high power consumption.
The interlocked operations are the most efficient synchronisation primitives when compared to the
classic lock based synchronisation mechanism. Interlocked function based synchronisation technique
brings the following value adds.
• The interlocked operation is free from waiting. Unlike the mutex, semaphore and critical section
synchronisation objects which may require waiting on the object, if they are not available at the
time of request, the interlocked function simply performs the operation and returns immediately.
This avoids the blocking of the thread which calls the interlocked function.
• The interlocked function call is directly converted to a processor specific instruction and there is
no user mode to kernel mode transition as in the case of mutex, semaphore and critical section
objects. This avoids the user mode to kernel mode transition delay and thereby increases the over¬
all performance.
The types of interlocked operations supported by an OS are underlying processor hardware depen¬
dent and so they are limited in functionality. Normally the bit manipulation (Boolean) operations are not
supported by interlocked functions. Also the interlocked operations are limited to integer or pointer vari¬
ables only. This limits the possibility of extending the interlocked functions to variables of other types.
Under windows operating systems, each process has its own virtual address space and so the interlocked
functions can only be used for synchronising the access to a variable that is shared by multiple threads of
a process (Multiple threads of a process share the same address, space) (Intra Process Synchronisation).
The interlocked functions can be extended for synchronising the access of the variables shared across
multiple processes if the variable is kept in shared memory.
10.8.2.2 Mutual Exclusion through Sleep &Wakeup The ‘Busy waiting’ mutual exclusion en¬
forcement mechanism used by processes makes, the CPU always busy by checking the lock to see
whether they can proceed. This results in the wastage of CPU time and leads to high power consump¬
tion. This is not affordable in embedded systems powered on battery, since it affects the battery backup
time of the device. An alternative to ‘busy waiting’ is the ‘Sleep & Wakeup' mechanism. When a process
is not allowed to access the critical section, which is currently being locked by another process, the
process undergoes ‘Sleep' and enters the ‘blocked" state. The process which is blocked on waiting for
access to the critical section is awakened by the process which currently owns the critical section. The
process which owns the critical section sends a wakeup message to the process, which is sleeping as a
result of waiting for the access to the critical section, when the process leaves the critical section. The
‘Sleep & Wakeup’ policy for mutual exclusion can be implemented in different ways. Implementation of
Real-Time Operating System (RTOS) based Embedded System Design
this policy is OS kernel dependent. The following section describes the important techniques for ‘Sleep
& Wakeup' policy implementation for mutual exclusion by Windows XP/CE OS kernels.
Semaphore Semaphore is a sleep and wakeup based mutual exclusion implementation for shared
resource access. Semaphore is a system resource and the process which wants to access the shared
resource can first acquire this system object to indicate the other processes which wants the shared
resource that the shared resource is currently acquired by it. The resources which are shared among a
process can be either for exclusive use by a process or for using by a number of processes at a time. The
display device of an embedded system is a typical example for the shared resource which needs exclu¬
sive access by a process. The Hard disk (secondary storage) of a system is a typical example for sharing
the resource among a limited number of multiple processes. Various processes can access the different
sectors of the hard-disk concurrently. Based on the implementation of the sharing limitation of the
shared resource, semaphores are classified into two; namely ‘Binary Semaphore’ and ‘Counting Sema¬
phore’. The binary semaphore provides exclusive access to shared resource by allocating the resource to
a single process at a time and not allowing the other processes to access it when it is being owned by a
process. The implementation of binary semaphore is OS kernel dependent. Under certain OS kernel it is
referred as mutex. Unlike a binary semaphore, the ‘ Counting Semaphore’ limits the access of resources
by a fixed number of processes/threads. ‘Counting Semaphore’ maintains a count between zero and a
value. It limits the usage of the resource to the maximum value of the count supported by it. The state of
the counting semaphore object is set to ‘signalled’ when the count of the object is greater than zero. The
count associated with a ‘Semaphore object’ is decremented by one when a process/thread acquires it and
the count is incremented by one when a process/thread releases the ‘Semaphore object'. The state of the
‘Semaphore object’ is set to non-signalled when the semaphore is acquired by the maximum number of
processes/threads that the semaphore can support (i.e. when the count associated with the ‘Semaphore
object’ becomes zero). A real world example for the counting semaphore concept is the dormitory sys¬
tem for accommodation (Fig. 10.34). A dormitory contains a fixed number of beds (say 5) and at any
point of time it can be shared by the maximum number of users supported by the dormitory. If a person
wants to avail the dormitory facility, he/she can contact the dormitory caretaker for checking the avail¬
ability. If beds are available in the dorm the caretaker will hand over the keys to the user. If beds are not
available currently, the user can register his/her name to get notifications when a slot is available. Those
who are availing the dormitory shares the dorm facilities like TV, telephone, toilet, etc. When a dorm
user vacates, he/she gives the keys back to the caretaker. The caretaker informs the users, who booked
in advance, about the dorm availability.
The creation and usage of ‘counting semaphore object’ is OS kernel dependent. Let us have a look at
how we can implement semaphore based synchronisation for the ‘Racing’ problem we discussed in the
beginning, under the Windows kernel. The following code snippet explains the same.
finclude <scdio.h> :. mAf
*finclude <windows.h>
■^define..MAX_SEMAPHORE_COUNT 1 //Make the semaphore object for //exclusive
use
#define thread count ‘2 //fio.of Child Threads 'Xl-
if (!ReleaseSemaphore(
hSemaphore, // handle to semaphore
. ai
Real-Time Operatingjlystem (RTOS) based Embedded System Design
1
.} '■ .dpi... i-g pF.iV f V | - /
,/
//Wait for the signalling .of .Sd.maphbre...object
;/ • ■ WaitForSlngleObject (hSemaphore, INFINITE) ; •
*v'V!
WAr-.
2 ‘ • /./Semaphore is acquired ,
i counter++; ' ' . •• , ’ -
h printf("Process B : Counter = %d\n", counter);
‘- //Release Semaphore
if (iReleaseSe.maphore (
hSemaphore, // handle to semaphore
1/ // increase count by one
NULL) ) // not interested in previous count
{
//Semaphore Release failed. Print Error code &
//return.
printf("Release Semaphore Failed Error Code: %d\n",
GetLastError());
return;
return;
//****************■*:**********************************•*:***************
// Main Thread
void main() (
DWORD thread_id;
,■int i;
if(NULL==child threads[i])
}
// Wait for the termination of child threads
WaitForMultipleObjects(thread_count, child_threads, TRUE,
INFINITE)-;
//Close handles of child threads
for( i=0; i < thread_count; i++ )
CloseHandle(child_threads[i]);
//Close Semaphore object handle
CloseHandle(hSemaphore);
return;
}
Please refer io the Online Learning Centre for details on the various Win32 APIs used, in the program
for counting semaphore creation, acquiring, signalling, and releasing. The VxWorks and MicroC/OS-II
Real-Time Operating System (RTOS) based Embedded System Design
Real-Time kernels also implements the Counting semaphore based task synchronisation/shared resource
access. We will discuss them in detail in a later chapter.
Counting Semaphores are similar to Binary Semaphores in operation. The only difference between
Counting Semaphore and Binary Semaphore is that Binary Semaphore can only be used for exclu¬
sive access, whereas Counting Semaphores can be used for both exclusive access (by restricting the
maximum count value associated with the semaphore object to one (1) at the time of creation of the
semaphore object) and limited access (by restricting the maximum count value associated with the
semaphore object to the limited number at the time of creation of the semaphore object).
the room to get notifications when a room is available. When a person gets a room he/she is granted the
exclusive access to the room facilities like TV, telephone, toilet, etc. When a user vacates the room, he/
she gives the keys back to the receptionist. The receptionist informs the users, who booked in advance,
about the room’s availability.
Let’s see how we can implement mutual exclusion with mutex object in the ‘Racing’ problem ex¬
ample given under the section ‘Racing’, under Windows kernel.
’#include <stdioth>.
K#include <windows.. h:>, . -'-r/uW
#define thread_count 2 , , , • //No.of Child Threads
; //************+********************** ^e*.*.* **.**.*******.************* **
//counter is an integer variable and Buffer is a byte array shared
i / /between' two ' ’ “» U
; //threads Process_A and Proce;ss_B / " ■ '' j -T; '‘ .“ '
char Buffer[10] = {1,2,3,4)5,6,7,8,9,10}; ' -
short int counter = 0;
/'/Define the-.handle to Mutex Object
HANDLE hMutex; . ,P/J/fiV .... 5 fij .
'i&k>icZ'k*k kkkk kkk. kk'k *,*-*.;* *** * k kkk kkkk k. kkkk jck kk.^'k k kkk:k k *** * * kkk kk kk kk k W....t> 5,|
// Child Thread 1
void Process_A (void) { • / • ' vy ycT _ . ■_ - 'fifi/T
j/int i; | fi„./.s., .# r. .. ... ' , • . ’ • •' ’ j|f§!
fe../f dr’”fi(i =0r'-ikSL'iTTf-ttvt. ./T! v ' ././,/;///;:
}
}
return;
}
yy*******************************************************************
// Child Thread 2
void Process B(void) {
Real-Time Operating System (RTOS) based Embedded System Design
i
Introduction to Embedded Systems
child threads[i])
WaitForMultipleObjects(thread_count,:child_threads^ TRUE
INFINITE) ; . ;v . , ..... -:i. ,:■ ;.Ar.A;.W':T'. ■ ■
//'Close child thread handles ) •/ ;
. ■ for-( i=0; i < thread count; • i++ ) : o
;.CloseHandle (child_threads [i ] ) ; ■;
//Close Mutex object handle • ■
Cl.oseHan.dle (hMutex) ;
return; , •> . r ■
Please refer to the Online Learning Centre for details on the various Win32 APIs used in the program
for mutex creation, acquiring, signalling, and releasing.
The mutual exclusion semaphore is a special implementation of the binary semaphore by certain
real-time operating systems like VxWorks and MicroC/OS-II to prevent priority inversion problems in
shared resource access. The mutual exclusion semaphore has an option to set the priority of a task own¬
ing it to the highest priority of the task which is being pended while attempting to acquire the semaphore
which is already in use by a low priority task. This ensures that the low priority task which is currently
holding the semaphore, when a high priority task is waiting for it, is not pre-empted by a medium prior¬
ity task. This is the mechanism supported by the mutual exclusion semaphore to prevent priority inver¬
sion.
VxWorks kernel also supports binary semaphores for synchronising shared resource access. We will
discuss about it in detail in a later chapter.
Critical Section Objects In Windows CE, the ‘Critical Section objecf is same as the ‘mutex object’
except that ‘Critical Section objecf can only be used by the threads of a single process (Intra process).
The piece of code which needs to be made as ‘Critical Section’ is placed at the ‘ Critical Section' area
by the process. The memory area which is to be used as the ‘Critical Section’ is allocated by the pro¬
cess. The process creates a ‘Critical Section’ area by creating a variable of type CRITICAL ^SECTION.
The Critical Section’ must be initialised before the threads of a process can use it for getting exclusive
access. The InitializeCriticalSectionjLPCRITICAL SECTION IpCriticalSection) API initialises the
critical section pointed by the pointer IpCriticalSection to the critical section. Once the critical section
Real-Time Operating System (RTOS) based Embedded System Design
is initialized, all threads in the process can use it. Threads can use the API call EnterCriticalSection
(LPCRITICAL SECTION IpCriticalSection) for getting the exclusive ownership of the critical section
pointed by the pointer IpCriticalSection. Calling the EnterCriticalSectionQ API blocks the execution of
the caller thread if the critical section is already in use by other threads and the thread waits for the criti¬
cal section object. Threads which are blocked by the EnterCriticalSectionQ call, waiting on a critical
section are added to a wait queue and are woken when the critical section is available to the requested
thread. The API call TryEnterCriticalSection(LPCRITICAL_SECTION IpCriticalSection) attempts to
enter the critical section pointed by the pointer IpCriticalSection without blocking the caller thread. If
the critical section is not in use by any other thread, the calling thread gets the ownership of the criti¬
cal section. If the critical section is already in use by another thread, the TryEnterCriticalSectionQ call
indicates it to the caller thread by a specific return value and the thread resumes its execution. A thread
can release the exclusive ownership of a critical section by calling the API LeaveCriticalSection(LPCR¬
ITICAL _ SECTION IpCriticalSection). The threads of a process can use the API DeleteCriticalSection
(LPCRITICAL SECTION IpCriticalSection) to release all resources used by a critical section object
which was created by the process with the CRJIICAL_SECTION variable.
Now let’s have a look at the ‘Racing’ problem we discussed under the section ‘Racing’. The racing
condition can be eliminated by using a critical section object for synchronisation. The following code
snippet illustrates the same.
int j;
for (j =5; j<10; j++)
{ V
if (Buffer[j j > 0)
l. ’ .. • •
■ //Use critical section object-!or'"synch'rcnisation
EnterCriticalSection(&CS) ;
countcr+i-; ... r ,,, ^ .. , / 1-
LeaveCriticalSection(&CS); , . ■ •
} ; ' ' ' .
printf("Process B : Counter = %d\n",counter);
int main/) {
Here the shared resource is the shared variable ‘counter’. The concurrent access to this variable by
the threads ‘Process_A’ and ‘ Process JB’ may create race condition and may produce incorrect results.
The critical section object ‘ CS’ holds the piece of code corresponding to the access of the shared vari¬
able ‘counter by each threads. This ensures that the memory area containing the low level instructions
corresponding to the high level instruction ‘counter++’ is accessed exclusively by threads ‘Process_A’
and ‘Process_B’ and avoids a race condition. The output of the abpve piece of code when executed on
Windows XP machine is given in Fig. 10.36.
The final value of ’counter’ is obtained as 10, which is the expected result for this piece of code.
If you observe this output window you can see that the text is not outputted to the o/p window in the
expected manner. The print/ () library routine used in this sample code is re-entrant and it can be pre¬
empted while in execution. That is why the outputting of text happened in a non expected way.
It should be noted that the scheduling of the threads ‘ Process_A' and ‘ Process _B’ is OS kernel sched¬
uling policy dependent and you may not get the same output all the time when you run this piece of:
code under Windows XP.
Real-Time Operating System (RTOS) based Embedded System Design
sProcesi Counter = 9
IProcesj Counter = 10
[ Fig. 10.36) Output of the Win32 application resolving racing condition through critical section object
The critical section object makes the piece of code residing inside it non-reentrant. Now let’s try the
above piece of code by putting the printf () library routine in the critical section object.
Mint i;
pi for (i =0; i<5;-i++)
p "
p if (Buffer[i] > 0)
// Child Thread 2
void Process 3(void) {
if (Buffer[j] > 0)
0m
//************
// Main Thread
int .main () {
DWORD., id
The output of the above piece of code when executed on a Windows XP machine is given below.
‘ig. 10.37j Output of the Win32 application resolving racing condition through critical section object
Real-Time Operating System (RTOS) based Embedded System Design
It should be noted that the scheduling of the threads ‘Process_A ’ and ‘Process_B’ is OS kernel sched¬
uling policy dependent and you may not get the same output all the time when you nin tbis piftcp. of
code in Windows XP. The output of the above program when executed at three different instances of
time is given shown in Fig. 10.38. ’ ‘
Process : Counter
aMtS
Process : Counter
Process : Counter
Process : Counter Mm
Process : Counter :
Process Counter
■ '
Process ; Counter
Process :■ Counter
Process : Counter m
Process : Counter
RspiiiliiPfifiini
"C;\l^dgr^;Piies\Mici‘bsbft Visual
[Process ft .: C ounter
Process ft : C ounter
Process ft Counter
Process A Counter
Process A Counter
Process B ^Counter
Process B Counter
Process B Counter
Process B lijCounter = 9
Process B :.li Counter = 10 la
(Fig. 10.38] Illustrati on of scheduler behaviour under Windows XP kernel
Events Event i> i i mhiomsation technique which uses the notification mechanism for
synchronisation M • > . cut!on wc may come across situations which demand the processes
to wait for a par ’ uic - ainm lor ns operations. A typical example of this is the producer consum¬
er threads, when fiu < .-Mm- . tin cad should wait for the consumer thread to produce the data and
producer thread 'nmnu >- m ' - me consumer thread to consume the data before producing fresh data.
If this sequence i- ms i. , u w ill end up m producer-consumer problem. Notification mechanism
is used for handi'.w m c, A \ \cm objects are used for implementing notification mechanisms.
Introduction to Embedded Systems
ill
A thread/process can wait for an event and another thread/process can set this event for processing by
the waiting thread/process. The creation and handling of event objects for notification is OS kernel
dependent. Please refer to the Online Learning Centre for information on the usage of ‘Events’ under
Windows Kernel for process/thread synchronisation.
The MicroC/OS-II kernel also uses ‘events’ for task synchronisation. We will discuss it in a later
chapter.
■Mfpftl
Device driver is a piece of software that acts as
ff';
■
a bridge between the operating system and the
hardware. In an operating system based prod¬
uct architecture, the user applications talk to the
Operating System kernel for all necessary infor¬ ■' - ' ■ ■ ’ ■ '-A ."
mation exchange including communication with STf- r
j
: yi*--* — . z
^ * : '— ui
c
t-
| '>- *
the hardware peripherals. The architecture of the tAi.. * li. .k'fy'i u ..Jii*. 4;-yfil
J Likll
the user application. All the device related access DiWkL1 .Oliver:*,
time of booting the device and are always kept in the RAM. Drivers which need to be installed for ac¬
cessing a device are known as ‘Installable drivers’. These drivers are loaded by the OS on a need basis.
Whenever the device is connected, the OS loads the corresponding driver to memory. When the device
is removed, the driver is unloaded from memory. The Operating system maintains a record of the drivers
corresponding to each hardware.
The implementation of driver is OS dependent. There is no universal implementation for a driver.
How the driver communicates with the kernel is dependent on the OS structure and implementation.,
Different Operating Systems follow different implementations.
It is very essential to know the hardware interfacing details like the memory address assigned to the
device, the Interrupt used, etc. of on-board peripherals for writing a driver for that peripheral. It varies
on the hardware design of the product. Some Real-Time operating systems like ‘Windows CE’ support
a layered architecture for the driver which separates out the low level implementation from the OS
specific interface. The low level implementation part is generally known as Platform Dependent Device
(PDD) layer. The OS specific interface part is known as Model Device Driver (MDD) or Logical Device
Driver (LDD). For a standard driver, for a specific operating system, the MDD/LDD always remains the
same and only the PDD part needs to be modified according to the target hardware for a particular class
of devices.
Most of the time, the hardware developer provides the implementation for all on board devices for
a specific OS along with the platform. The drivers are normally shipped in the form of Board Support
Package. The Board Support Package contains low level driver implementations for the onboard pe¬
ripherals and OEM Adaptation Layer (OAL) for accessing the various chip level functionalities and a
bootloader for loading the operating system. The OAL facilitates communication between the Operating
System (OS) and the target device and includes code to handle interrupts, timers, power management,
bus abstraction, generic I/O control codes (IOCTLs), etc. The driver files are usually in the form of a
dll file. Drivers can run on either user space or kernel space. Drivers which run in user space are known
as user mode drivers and the drivers which run in kernel space are known as kernel mode drivers. User
mode drivers are safer than kernel mode drivers. If an error or exception occurs in a user mode driver,
it won’t affect the services of the kernel. On the other hand, if an exception occurs in the kernel mode
driver, it may lead to the kernel crash. The way how a device driver is written and how the interrupts are
handled in it are operating system and target hardware specific. However regardless of the OS types, a
device driver implements the following: .
1. Device (Hardware) Initialisation and Interrupt configuration
2. Interrupt handling and processing
3. Client interfacing (Interfacing with user applications)
The Device (Hardware) initialisation part of the driver deals with configuring the different registers
of the device (target hardware). For example configuring the I/O port line of the processor as Input or
output line and setting its associated registers for building a General Purpose 10 (GPIO) driver. The
interrupt configuration part deals with configuring the interrupts that needs to be associated with the
hardware. In the case of the GPIO driver, if the intention is to generate an interrupt when the Input line
is asserted, we need to configure the interrupt associated with the I/O port by modifying its associated
registers. The basic Interrupt configuration involves the following.
1. Set the interrupt type (Edge Triggered (Rising/Failing) or Level Triggered (Low or High)), enable
the interrupts and set the interrupt priorities.
2. Bind the Interrupt with an Interrupt Request (IR.Q). The processor identifies an interrupt through
IRQ. These IRQs are generated by the Interrupt Controller. In order to identify an interrupt the
interrupt needs to be bonded to an IRQ.
Introduction to Embedded Systems
3. Register an Interrupt Service Routine (ISR) with an Interrupt Request (IRQ). ISR is the handler for
an Interrupt. In order to service an interrupt, an ISR should be associated with an IRQ. Registering
an ISR with an IRQ takes care of it.
With these the interrupt configuration is complete. If an interrupt occurs, depending on its priority, it
is serviced and the corresponding ISR is invoked. The processing part of an interrupt is handled in an
ISR. The whole interrupt processing can be done by the ISR itself or by invoking an Interrupt Service
Thread (1ST). The 1ST performs interrupt processing on behalf of the ISR. To make the ISR compact
and short, it is always advised to use an 1ST for interrupt processing. The intention of an interrupt is to
send or receive command or data to and from the hardware device and make the received data avail¬
able to user programs for application specific processing. Since interrupt processing happens at kernel
level, user applications may not have direct access.to the drivers to pass and receive data. Hence it is
the responsibility of the Interrupt processing routine or thread to inform the user applications that an
interrupt is occurred and data is available ,for further processing. The client interfacing part of the device
driver takes care of this. The client interfacing implementation makes use of the Inter Process commu¬
nication mechanisms supported by the embedded OS for communicating and synchronising with’user
applications and drivers. For example, to inform a user application that an interrupt is occurred and the
data received from the device is placed in a shared buffer, the client interfacing code can signal (or set)
an event. The user application creates the event, registers it and waits for the driver to signal it. The
driver can share the received data through shared memory techniques. IOCTLs, shared buffers, etc. can
be used for data sharing. The story line is incomplete without performing an interrupt done (Interrupt
processing completed) functionality in the driver. Whenever an interrupt is asserted, while vectoring to
its corresponding ISR, all interrupts of equal and low priorities are disabled. They are re-enable only on
executing the interrupt done function (Same as the Return from Interrupt RETI instruction execution
for 8051) by the driver. The interrupt done function can be invoked at the end of corresponding ISR or
1ST.
We will discuss more about device driver development in a dedicated book coming under this book-
series.
Memory Requirements The OS requires ROM memory for holding the OS files and it is normally
stored in a non-volatile memory like FLASH. OS also requires working memory RAM for loading the
OS services. Since embedded systems are memory constrained, it is essential to evaluate the minimal
ROM and RAM requirements for the OS under consideration.
Real-time Capabilities It is not mandatory that the operating system for all embedded systems need
to be Real-time and all embedded Operating systems-are ‘Real-time'm behaviour. The task/process
Real-Time Operating System (RTOS) based Embedded System Design
scheduling policies plays an important role in the ‘Real-time’ behaviour of an OS. Analyse the real-time
capabilities of the OS under consideration and the standards met by the operating system for real-time
capabilities.
Kernel and Interrupt Latency The kernel of the OS may disable interrupts while executing certain
services and it may lead to interrupt latency. For an embedded system whose response requirements are
high, this latency should be minimal.
Inter Process Communication and Task Synchronisation The implementation of Inter Process
Communication and Synchronisation is OS kernel dependent. Certain kernels may provide a bunch of
options whereas others provide very limited options. Certain kernels implement policies for avoiding
priority inversion issues in resource sharing.
Modularisation Support Most of the operating systems provide a bunch of features. At times it may
not be necessary "for an embedded product for its functioning. It is very useful if the OS supports modu¬
larisation where in which the developer can choose the essential modules and re-compile the OS image
for functioning. Windows CE is an example for a highly modular operating system.
Support for Networking and Communication The OS kernel may provide stack implementation
and driver support for a bunch of communication interfaces and networking. Ensure that the OS under
consideration provides support for all the interfaces required by the embedded product.
Development Language Support Certain operating systems include the run time libraries required
for running applications written in languages like Java and C#. A Java Virtual Machine (JVM) custom¬
ised for the Operating System is essential for running java applications. Similarly the .NET Compact
Framework (.NETCF) is required for running Microsoft® .NET applications on top of the Operating
-System. The OS may include these components as built-in component, if riot, check the availability of
the same from a third party vendor for the OS under consideration.
Cost The total cost for developing or buying the OS and maintaining it in terms of commercial prod¬
uct and custom build needs to be evaluated before taking a decision on the selection of OS.
Development and Debugging Tools Availability The availability of development and debugging
tools is a critical decision making factor in the selection of an OS for embedded design. Certain Operat¬
ing Systems may be superior in performance, but the availability of tools for supporting the develop-!
ment may be limited. Explore the different tools available for the OS under consideration.
Ease of Use How easy it is to use a commercial RTOS is another important feature that needs to be
considered in the RTOS selection.
Introduction to Embedded Systems
After Sales For a commercial embedded RTOS, after sales in the form of e-mail, on-call services,
etc. for bug fixes, critical patch updates and support for production issues, etc. should be analysed thor¬
oughly.
®5 Summary
S The Operating System is responsible for making the system convenient to use, organise and manage system;,.
' . resources efficiently and properly. • - '>&$%■■■
S Process/Task management, Primary memory management, File system management, I/O system (Device) man¬
agement, Secondary Storage Management, protection implementation, Time management/interrupt handling,
...etc. are the important services handled by the OS kernel. • • ...
■' v The core of the operating system is known, as kernel. Depending on the implementation of the different kernel
. services, the kernel is classified as Monolithic and Micro. User Space is the memory area in which user applica¬
tions are confined to run, whereas kemil space is the memory area reserved for kernel applications.^
X Operating systems with a generalised kernel are known as General Purpose Operating Systems (GPOS), whereas
operating systems with a specialised kernel with deterministic timing behaviour are known as Real-Time Operat-
. ingSystems.(RTOS). i
S In the operating system context a task/process is a program, or part of it, in execution. The process holds a set
of registers, process status, a Program Counter (PC) to point to the next executable instruction of the process, a
stack for holding the local variables associated with the process and the code, corresponding to the proceSshJH^I
•T The different states through which a process traverses through during its journey from the.newly created state to
finished state is knotvn as Process Life Cycle - • .
C Process management deals with the creation of a process, setting up the memory space for the process, loading
the process’s code into the memory space, allocating system resources, setting up a Process Control Block (PCB)
for the process and processfermination/deletion. f
S A thread is the primitive that can execute code. It is a single sequential flow of control within a process. A process
may contain multiple threads. The act of concurrent execution of multiple threads under an operating system is
known as multithreading. ' 'A- -'•*
S Thread standards are the different standards available for thread creation and management. POSIX, Win32, Java,
etc. are the commonly used thread creation and management libraries.
S -The ability of a system to execute multiple processes simultaneously is known as multiprocessing, whereas
the ability of an operating system to hold multiple processes in memory and switch the processor (CPU) from
executing one process to another process is known as multitasking. Multitasking involves Context Switching,
Context Saving and Context Retrieval.
'T Co-operative multitasking, Preemptive multitasking and Non-preemptive multitasking are the three important
types of multitasking which exist in the Operating system context.
C CPU utilisation, Throughput, Turn Around Time (TAT), Waiting Time and Response Time are the important
criterions that need to be considered for the selection of a scheduling algorithm for task scheduling.
U Job queue, Ready queue and Device queue are the important queues maintained by an operating system in as¬
sociation with CPU scheduling.
C First Come First Served (FCFS)/First in First Out (FIFO), Last Come First Served (LCFS)/Last in First Out
(LIFO), Shortest Job First (SJF), priority based scheduling, etc. are examples for Non-preemptive scheduling,
whereas Preemptive SJF Scheduling/Shortest Remaining Time (SRT), Round Robin (RR) scheduling and prior¬
ity based scheduling are examples for preemptive scheduling.
C -Processes in a multitasking system falls into either Co-operating or Competing. The co-operating processes share
data for communicating among the processes through Inter Process Communication (IPC), whereas, competing
Real-Time Operating System (RTOS) based Embedded System Design
processes do not share anything among themselves but they share the system resources like display devices,
keyboard, etc.
V Shared memory, message passing and Remote Procedure Calls (RPC) are the important IPC mechanisms through
which the co-operating processes communicate in an operating system environment. The implementation of the
IPC mechanism is OS kernel dependent.
S Racing, deadlock, livelbck, starvation, producer-consumer problem, Readers-Writers problem and priority inver¬
sion are some of the problems involved in shared resource access in task communication through sharing.
y The ‘Dining Philosophers’ ‘Problem’ is a real-life representation of the deadlock, starvation, livelock and ‘Rac¬
ing’ issues in shared resource access in operating system context.
y Priority inversion is the condition in which a medium priority task gets the CPU for execution, when a high
priority task needs to wait for a low priority task to release a resource which is shared between the high priority,
task and the low priority task.
y Priority inheritance and Priority ceiling are the two mechanisms for avoiding Priority Inversion in a multitask¬
ing environment.
y The act of preventing the access of a shared resource by a task/process when it is currently being held by another
task/process is known as mutual exclusion. Mutual exclusion can be implemented through either -busy waiting
{spin lock) or sleep and wakeup technique.
y Test-and Set, Flags, etc. are examples of Busy waiting based mutual exclusion implementation, whereas Sema¬
phores, mutex, Critical Section Objects and events are examples for Sleep and Wakeup based mutual exclusion.
y Binary semaphore implements exclusive shared resource access, whereas counting semaphore limits the concur¬
rent access to a shared resource, and mutual exclusion semaphore prevents priority inversion in shared resource
access.
y Device driver is a piece of software that acts as a bridge between the operating system and the hardware. Device
drivers are responsible for,initiating and managing the communication with the hardware peripherals.
y Various functional and non-functional requirements need to be evaluated before the selection of an RTOS for an
embedded design.
'I®' Keywords
c_ . __
Operating System : A piece of software designed to manage and allocate system resources and execute other
pieces of the software
Kernel : The core of the operating system which is responsible for managing the system resources
and the communication among the hardware and other system services
Kernel space : The primary memory area where the kernel applications are confined to run
User space : The primary memory area where the user applications are confined to run
Monolithic kernel A kernel with all kernel seivices run in the kernel space under a single kernel thread
Microkernel : A kernel which incorporates only the essential services within the kernel space and the rest
is installed as loadable modules called servers
Real-Time Operat- : Operating system with a specialised kernel with a deterministic timing behaviour
ing System (RTOS)
Scheduler : OS kernel service which deals with the scheduling of processes/tasks
Hard Real-Time : Real-time operating systems that strictly adhere to the timing constraints for a task
Soft Real-Time Real-time operating systems that does not guarantee meeting deadlines, but, offer the best
effort to meet the deadline
Task/Job/Process : In the' operating system context a task/process is a prqgram, or part of it, in execution
Introduction to Embedded Systems
Process Life Cycle : The different states through which a process traverses through during its journey from the
newly created state to completed state
Thread : The primitive that can execute code. It is a single sequential flow of control within a pro¬
cess
Multiprocessing : Systems which contain multiple CPUs and are capable of executing multiple processes si¬
systems multaneously
Multitasking : The ability of an operating system to hold multiple processes in memory and switch the
processor (CPU) from executing one process to another process
Context switching : .The act of switching CPU among, the processes and changing the current execution context
Co-operative multi- : .Multitasking model in which a task/process gets a chance when the currently executing task ;
tasking relinquishes the CPU. voluntarily.. . 'f ", ' ' r
Preemptive multi¬ Multitasking model in which a currently running task/process is preempted to execute an- f
tasking other task/process ' l'
Non-preemptive ; : Multitasking model in which .a task gets a chance to execute when the currently executing;!
multitasking task relinquishes the GPU or when it enters a wait state ' .; #p
First Come First : Scheduling policy which sorts the Ready Queue with FCFS model and schedules the first
Served (FCFSj/First arrived process from the Ready queue for execution
in First Out (FIFO)
Last Come First : Scheduling policy Which sorts the Ready Queue with LCFS model and schedules the last
Served (LCFS)ZLast arrived process from the Ready queue for execution • **
in .First Out (LIFO)
Shortest Job First Scheduling policy which sorts the Ready queue with the order of the shortest execution time
(SJF) for process and schedules the process with least estimated execution completion time from
the Ready queue for execution
Priority based Scheduling policy which sorts the Ready queue based on priority and schedules the process
Scheduling with highest priority from the Ready queue for execution
Shortest Remaining : Preemptive scheduling policy which sorts the Ready queue with the order of the shortest re¬
Time (SRT) maining time for execution completion for process and schedules the process with the least
remaining time for estimated execution completion from the Ready queue for execution
Round Robin : Preemptive scheduling policy in which the currently running process is preempted based
on time slice
Co-operating pro- : Processes which share data for communicating among them
cesses
Inter Process/Task : Mechanism for communicating between co-operating processes of a system
Communication
(IPC)
Shared memory : A memory sharing mechanism used for inter process communication
Message passing : IPC mechanism based on exchanging of messages between processes through a message
queue or mailbox
Message queue : A queue for holding messages for exchanging between processes of a multitasking system
Mailbox : A special implementation of message queue under certain OS kernel, which supports only
a single message
Signal : A form of asynchronous message notification
Remote Procedure : The IPC mechanism used by a process to invoke a procedure of another process running on
Call or RPC the same CPU or on a different CPU which is interconnected in a network
Racing : The situation in which multiple processes compete (race) each other tp access and manipu¬
late shared data concurrently
Real-Time Operating System (RTOS) based Embedded System Design
Deadlock : A situation where none of the processes are able to make any progress in their execution
frnriirlitf riti riti WtvrWii /nrn/'poc :fc went ifiW'-THr vt [rXcrxiirr>a Kp 1 Ain / anAtWot- *%*-/%
•An^progress in
_ Starvation - : The condition in which a process does not ;j :PU or system resources required (■■
Dining Philosophers’: A real-life representation of the-deadlock, su , livelock and racing issues in shared
Problem resource access in operating system context
Producer-Consumer : A common data sharing problem where two
problem with fixed size
?, r, A ' \ 17 V
Readers-Writers 1
: A
A t ' l
data --UI
sharing problem ‘'l.1* MSS j U
characteriSed-by 'l
mt|l
problem ' data cphcurjenlly^^^|
concurrently g§ .
pprihrity inversion : The'conditionin whiQh,a medium priority |£ts
It r.y . highest priority task v. hich rises tire shared resource) and the priority of the task is temporar¬
ily '< ;'v ily boosted to the priority of the shared resource when the resource is being held by thefe|
\ for avoiding priority inversion , A -
.'"jirTT | /"D . p«m • I h fa lAntw/af'ntmdhlimtAdn l-M /yfln/A n nn ehUnn T fl a*-/Artn Inx r-ryiihl rtnihranOTuyl
i -Mutual Exclusion : The act of preventing the access of a'shared resource by a task/process when it is being held
Semaphore : A system resource for implementing mutual exclusion in shared resource access or for re-
Mutex * : The binary semaphore implementation for exclusive resource access under certain OS ker-
S nc! — • • ,
Device driver : A piece of software that acts as a bridge between the operating system and the hardware
®S Objective Questions
V - J
12. Different threads, which are part of a process, share the same address space. State ‘True’ or ‘False’
(a) True (b) False
13. Multiple threads of the same process share_?
(a) Data memory (b) Code memory
(c) Stack memory (d) All of these
(e) only (a) and (b)
14. Which of the following is true about multithreading?
(a) Better memory utilisation
(b) Better CPU utilisation
(c) Reduced complexity in inter-thread communication
(d) Faster process execution (e) All of these
15. Which of the following is a thread creation and management library?
(a) POSIX • ' (b) Win32
(c) Java Thread Library (d) All of these
16. Which of the following is the POSIX standard library for thread creation and management
(a) Pthreads (b) Threads (c) Jthreads (d) None of these
17. What happens when a thread object’s waitQ method is invoked in Java?
(a) Causes the thread object to wait
(b) The thread will remain in the ‘Wait’ state until another thread invokes the notifyO or notifyAllQ method of the
thread object which is waiting
(c) Both of these
(d) None of these
18. Which of the following is true about user level threads?
(a) Even if a process contains multiple user level threads, the OS treats it as a single thread
(b) The user level threads are executed non-preemptively in relation to each other
(c) User level threads follow co-operative execution model
(d) All of these —
19. Which of the following are the valid thread binding models for user level to kernel level thread binding?
(a) One to Many (b) Many to One (c) One to One (d) Many to Many
(e) All of these (f) only (b), (c) and (d)
20. If a thread expires, the stack memory allocated to it is reclaimed by the process to which the thread belongs. State
‘True’ or ‘False’
(a) True (b) False
7. Multitasking involves
(a) Context switching (b) Context saving (c) Context retrieval (d) All of these
(e) None of these
8. What are the different types of multitasking present in operating systems?
(a) Co-operative (b) Preemptive (c) Non-preemptive (d) All of these
9. In Co-operative multitasking, a process/task gets the CPU time when
(a) The currently executing task terminates its execution
(b) The currently executing task enters ‘Wait’ state
(c) The currently executing task relinquishes the CPU before terminating
(d) Never get a chance to execute (e) Either (a) or (c)
10. In Preemptive multitasking
(a) Each process gets an equal chance for execution
(b) The execution of a process is preempted based on the scheduling policy
(c) Both of these (d) None of these
11. In Non-preemptive multitasking, a process/task gets the CPU time when
(a) The currently executing task terminates its execution
(b) The currently executing task enters ‘Wait’ state
(c) The currently executing task relinquishes the CPU before terminating
(d) All of these
(e) None of these
12. MSDOS Operating System supports
(a) Single user process with single thread
(b) Single user process with multiple threads
(c) Multiple user process with single thread per process
(d) Multiple user process with multiple threads per process
Task Scheduling
1. Who determines which task/process is to be executed at a given point of time?
(a) Process manager (b) Context manager (c) Scheduler (d) None of these
2. Task scheduling is an essential part of multitasking.
(a) True (b) False
3. The process scheduling decision may take place when a process switches its state from
(a) ‘Running’to‘Ready’ (b) ‘Running’to‘Blocked’
(c) ‘Blocked’to‘Ready’ (d) ‘Running’to‘Completed’
(e) All of these
(f) Any one among (a) to (d) depending on the type of multitasking supported by OS
4. A process switched its state from ‘Running’ to ‘Ready’ due to scheduling act. What is the type of multitasking
supported by the OS?
(a) Co-operative (b) Preemptive (c) Non-preemptive (d) None of these
5. A process switched its state from ‘Running’ to ‘Wait’ due to scheduling act. What is the type of multitasking sup¬
ported by the OS?
(a) Co-operative (b) Preemptive (c) Non-preemptive (d) (b) or (c)
6. Which one of the following criteria plays an important role in the selection of a scheduling algorithm?
(a) CPU utilisation (b) Throughput (c) Turnaround time (d) Waiting time
(e) Response time (f) All of these
7. For a good scheduling algorithm, the CPU utilisation is
(a) High (b) Medium (c) Non-defined
Introduction to Embedded Systems
3. Under Windows Operating system, the input and output buffer memory for a named pipe is allocated in
(a) Non-paged system memory (b) Paged system memory
(c) Virtual memory (d) None of the above
4. Which among the following techniques is used for sharing data between processes?
(a) Semaphores (b) Shared memory (c) Messages d) (b) and (c)
5. Which among the following is a shared memory technique for IPC?
(a) Pipes (b) Memory mapped Object
(c) Message blocks (d) Events (e) (a) and (b)
6. Which of the following is best advised for sharing a memory mapped object between processes under windows
kernel?
(a) Passing the handle of the shared memory object (b) Passing the name of the memory mapped object
(c) None of these
7. Why is message passing relatively fast compared to shared memory based IPC?
(a) Message passing is relatively free from synchronisation overheads
(b) Message passing does not involve any OS intervention
(c) All of these
(d) None of these
8. In asynchronous messaging, the message posting thread just posts the message to the queue and will not wait for
an acceptance (return) from the thread to which the message is posted. State ‘True’ or ‘False’
(a) True " (b) False
9. Which of the following is a blocking message passing call in Windows? \
(a) PostMessage (b) PostThreadMessage (c) SendMessage (d) All of these
(e) None of these
10. Under Windows operating system, the message is passed through_for Inter Process Communication
(IPC) between processes?
(a) Message structure (b) Memory mapped object
(c) Semaphore (d) All of these _
11. Which of the following is true about ‘ Signals’ for Inter Process Communication?
(a) Signals are used for'asynchronous notifications (b) Signals are not queued
(c) Signals do not carry any data (d) All of these
12. Which of the following is true about Racing or Race conditiorp.
(a) It is the condition in which multiple processes compete (race) each other to access and manipulate shared data
concurrently
(b) In a race condition the final value of the shared data depends on the process which acted on the data finally
(c) Racing will not occur if the shared data access is atomic
(d) All of these
13. Which of the following is tme about deadlock?
(a) Deadlock is the condition in which a process is waiting for a resource held by another process which is wait¬
ing for a resource held by the first process
(b) Is the situation in which none of the competing process will be able to access the resources held by other
processes since they are locked by the respective processes
(c) Is a result of chain of circular wait
(d) All of these
14. What are the conditions favouring deadlock in multitasking?
(a) Mutual Exclusion (b) Hold and Wait .(c) No kernel resource preemption at kernel level
(d) Chain of circular waits (e) All of these
15. Livelock describes the situation where
(a) A process waits on a resource is not blocked on it and it makes frequent attempts to acquire the resource. But.
unable to acquire it since it is held by other process
Real-Time Operating System (RTOS) based Embedded System Design
(b) A process waiting in the ‘Ready’ queue is unable to get the CPU time for execution
(c) Both of these
(d) None of these
16. Priority inversion is
(a) The Condition in which a high priority task needs to wait for a low priority task to release a resource which is
shared between the high priority task and the low priority task
(b) The act of increasing the priority of a process dynamically •
(c) The act of decreasing the priority of a process dynamically
(d) All of these
17. Which of the following is true about Priority' inheritance?
(a) A low priority task which currently holds a shared resource requested by a high priority task temporarily
inherits the priority of the high priority task
(b) The priority of the low priority task which is temporarily boosted to high is brought to the original value when
it releases the shared resource
(c) All of these
(d) None of these
18. Which of the following is true about Priority Ceiling based Priority inversion handling?
(a) A priority is associated with each shared resource
(b) The priority associated to each resource is the priority of the highest priority task which uses this shared
resource
(c) Whenever a task accesses a shared resource, the scheduler elevates the priority of the task to that of the ceiling
priority of the resource
(d) The priority of the task is brought back to the original level once the task completes the accessing of the
shared resource
(e) All of these
19. Process/Task synchronisation is essential for?
(a) Avoiding conflicts in resource access in multitasking environment
(b) Ensuring proper sequence of operation across processes.
(c) Communicating between processes
(d) All of these
(e) None of these
20. Which of the following is true about Critical Section!
(a) It is the code memory area which holds the program instructions (piece of code) for accessing a shared
resource
(b) The access to the critical section should be exclusive
(c) All of these
(d) None of these
21. Which of the following is true about mutual exclusion?
(a) Mutual exclusion enforces mutually exclusive access of resources by processes
(b) Mutual exclusion may lead to deadlock
(c) Both of these
(d) None of these
22. Which of the following is an example of mutual exclusion enforcing policy?.
(a) Busy Waiting (Spin lock) (b) Sleep & Wake up
(c)„ Both of these (d) None of these
23. Which of the following is true about lock based synchronisation mechanism?
(a) It is CPU intensive
(b) Locks are useful in handling situations where the processes js likely to be blocked for a shorter period of time
on waiting the lock ■
Introduction to Embedded Systems
(c) If the lock is being held for a long time by a process and if it is preempted by the OS, the other threads waiting
for this lock may have to spin a longer time for getting
(d) All of these
(e) None of these
24. Which of the following synchronisation techniques follow the ‘Sleep & Wake-up' mechanism for mutual exclu¬
sion?
(a) Mutex (b) Semaphore (c) Critical Section (d) Spin lock
(e) (a), (b) and (c)
25. Which of the following is true about mutex objects for IPC synchronisation under Windows OS?
(a) Only one process/thread can own the ‘mutex object’ at a time
(b) The state of a mutex object is set to non-signalled when it is not owned by any process/thread, and set to
signalled when it is owned by any process/thread
(c) The state of a mutex object is set to signalled when it is not owned by any process/thread, and set to non-sig¬
nalled when it is owned by any process/thread
(d) Both (a) & (b) (e) Both (a) & (c)
26. Which of the following is (are) the wait functions provided by windows for synchronisation purpose?
(a) WaitForSingleObject (b) WaitForMultipleObjects
(c) Sleep (d) Both (a) and (b)
27. Which of the following is true about Critical Section object?
(a) It can only be used by the threads of a single process (Intra process)
(b) The ‘Critical Section’ must be initialised before the threads of a process can use it
(c) Accessing Critical Section blocks the execution of the caller thread if the critical section is already in use by
other threads
(d) Threads which are blocked by the Critical Section access call, waiting on a critical section, are added to a wait
queue and are woken when the Critical Section is available to the requested thread
(e) All of these _
28. Which of the following is a non-blocking Critical Section accessing call under windows?
(a) EnterCriticalSection (b) TryEnterCriticalSection
(c) Both of these (d) None of these
29. The Critical Section object makes the piece of code residing inside it_?
(a) Non-reentrant (b) Re-entrant (c) Thread safe (d) Both (a) and (c)
30. Which of the following synchronisation techniques is exclusively used for synchronising the access of shared
• resources by the threads of a process (Intra Process Synchronisation) under Windows kernel?
(a) Mutex object (b) Critical Section object (c) Interlocked functions
(d) Both (c) and (d)
C~ ;-
Review Questions
L.
3. Explain the difference between the memory management of general purpose kernel and real-time kernel.
4. What is virtual memory? What are the advantages and disadvantages, of virtual memory?
5. Explain how ‘accurate time management’ is achieved in real-time kernel
6. What is the difference between ‘Hard’ and ‘Soft’ real-time systems? Give an example for ‘Hard’ and ‘Soft’ Real-
Time kernels
Task Scheduling
1. What is task scheduling in the operating system context?
2. Explain the various factors to be considered for the selection of a scheduling criteria
3. Explain the different queues associated with process scheduling
4. Explain the different types of non-preemptive scheduling algorithms. State the merits and de-merits of each
5. Explain the different types of preemptive scheduling algorithms. State the merits and de-merits of each
6. Explain Round Robin (RR) process scheduling with interrupts
7. Explain staiyation in the process scheduling context. Explain how starvation can be effectively tackled?
./ 8. What is IDLEPROCESS? What is the significance of IDLEPROCESS in the process scheduling context?
9. Three processes with process IDs PI, P2, P3 with estimated completion time 5, 10, 7 milliseconds respectively
enters the ready queue together in the order PI, P2, P3. Process P4 with estimated execution completion time 2
milliseconds enters the ready queue after 5 milliseconds. Calculate the waiting time and Turn Around Time (TAT)
! for each process and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes) in the FIFO scheduling
10. Three processes with process IDs PI, P2, P3 with estimated completion time 12, 10, 2 milliseconds respectively
enters the ready queue together in the order P2, P3, PI. Process P4 with estimated execution completion time
Introduction to Embedded Systems
4 milliseconds enters the Ready queue after 8 milliseconds. Calculate the waiting time and Turn Around Time
(TAT) for each process and the average waiting time and Turn Around Time (Assuming there is no I/O waiting for
the processes) in the FIFO scheduling
11. Three processes with process IDs PI, P2, P3 with estimated completion time 8, 4, 7 milliseconds respectively
_ enters the ready queue together in the order P3, PI, P2. PI contains an I/O waiting time of 2 milliseconds when it
completes 4 milliseconds of its execution. P2 and P3 do not contain any I/O waiting. Calculate the waiting time
and Turn Around Time (TAT) for each process and the average waiting time and Turn Around Time in the LIFO
scheduling. All the estimated execution completion time is excluding I/O wait time
12. Three processes with process IDs PI, P2, P3 with estimated completion time .12, 10, 2 milliseconds respectively
enters the ready queue together in the order P2, P3, PI. Process P4 with estimated execution completion time 4
milliseconds enters the Ready queue after 8 milliseconds. Calculate the waiting time and Turn Around Time (TAT)
for each process and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes) in the LIFO scheduling
13. Three processes with process IDs PI, P2, P3 with estimated completion time 6, 8, 2 milliseconds respectively
enters the ready queue together. Process P4 with estimated execution completion time 4 milliseconds enters the
Ready queue after 1 millisecond. Calculate the waiting time and Turn Around Time (TAT) for each process and
the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes) in the non-
preemptive SJF scheduling
14. Three processes with process IDs PI, P2, P3 with estimated completion time 4,6, 5 milliseconds and priorities 1,
0,3 (0—highest priority, 3 lowest priority) respectively enters the ready queue together. Calculate the waiting time
and Turn Around Time (TAT) for each process and the average waiting time and Turn Around Time (Assuming
there is no I/O waiting for the processes) in non-preemptive priority based scheduling algorithm
15. Three processes with process IDs PI, P2, P3 with estimated completion time 4, 6, 5 milliseconds and priorities
1, 0, 3 (0—highest priority, 3 lowest priority) respectively enters the ready queue together. Process P4 with es ¬
timated execution completion time 6 milliseconds and priority 2 enters the ‘Ready’ queue after 5 milliseconds.
Calculate the waiting time and Turn Around Time (TAT) for each process and the average waiting time and Turn
Around Time (Assuming there is no I/O waiting for the processes) in non-preemptive priority based scheduling
algorithm
16. Three processes with process IDs PI, P2, P3 with estimated completion time 8,4, 7 milliseconds respectively en¬
ters the ready queue together. PI contains an I/O waiting time of 2 milliseconds when it completes 4 milliseconds
of its execution. P2 and P3 do not contain any I/O waiting. Calculate the waiting time and Turn Around Time (TAT)
for each process and the average waiting time and Turn Around Time in the SRT scheduling. All the estimated
execution completion time is excluding I/O waiting time
17. Three processes with process IDs PI, P2, P3 with estimated completion time 12, 10, 6 milliseconds respectively
enters the ready queue together. Process P4 with estimated execution completion time 2 milliseconds enters the
Ready queue after 3 milliseconds. Calculate the waiting time and Turn Around Time (TAT) for each process and
the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the processes) in the SRT
scheduling
18. Three processes with process IDs PI, P2, P3 with estimated completion time 10,14,20 milliseconds respectively,
enters the ready queue together in the order P3, P2, PL Calculate the waiting time and Turn Around Time (TAT)
for each process and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes) in RR algorithm with Time slice = 2 ms
19. Three processes with process IDs P-1, P2, P3 with estimated completion time 12, 10, 12 milliseconds respectively
enters the ready queue together in the order P2, P3, PL Process P4 with estimated execution completion time 4
milliseconds enters the Ready queue after 8 milliseconds. Calculate the waiting time and Turn Around Time (TAT)
for each'process and the Average waiting time and Turn Around Time (Assuming there is no I/O waiting for the
processes) in RR algorithm with Time slice = 4 ms
20. Three processes with process IDs PI, P2, P3 with estimated completion time 4,6, 5 milliseconds and priorities 1,
0, 3 (0—highest priority, 3 lowest priority) .respectively enters the ready queue together. Calculate the waiting time
Real-Time Operating System (RTOS) based Embedded System Design
(
and Turn Around Time (TAT) for each process and the average waiting time and Turn Around Time (Assuming
there is no I/O waiting for the processes) in preemptive priority based scheduling algorithm
21. Three processes with process IDs PI, P2, P3 with estimated completion time 6, 2, 4 milliseconds respectively,
enters the ready queue together in the order PI, P3, P2. Process P4 with estimated execution time 4 milliseconds
entered the ‘Ready’ queue 3 milliseconds later the start of execution of PI. Calculate the waiting time and Turn
Around Time (TAT) for each process and the Average waiting time and Turn Around Time (Assuming there is no
I/O waiting for the processes) in RR algorithm with Time slice = 2 ms
Lab Assignments
(d) The main thread waits for the execution completion of the child thread and quits when the child thread
. completes its execution.
(e) The child thread writes the data “Hi from child thread” to the anonymous pipe and sets the event object
“synchronise” and sleeps'for 500 milliseconds and then quits. ■
Compile and execute the application using Visual Studio under Windows XP/NT OS. g
6. Write a Win3 2 console application (Process!) illustrating the creation of a memory mapped object of size 512KB,
• with name “mysharedobject”. Create an event object with name "synchronise” with,state non-signalled. Read
the memory mapped object when the event is signalled and display the contents on the console window. Create
a second console application (Process 2) for opening the memory mapped object with name “mysharedobject”
and event object with name “synchronise”. Write the message “Message from Process 2” to the memory mapped
object and set the event object “synchronise”. Use appropriate-error handling mechanisms wherever possible.
Compile both the applications using Visual Studio and execute them in the order Process 1 followed by Process
2 under Windows XP/NT OS. - - ' ■ W
7. Write a multithreaded Win32 console application where:
J ’' - (a) The main thread creates a child thread with default stack size and name ‘Child_Thread\
(b) The main thread sends user defined messages and the message ‘ WM_QUIT’ randomly to the child thread.
(c) The child thread processes the message posted by the main thread and quits when it receives the ‘WM_
QUIT’message. .. ■ . . ,<r - • •<•:,- , . •.> . ^ r:.,.
(d) The main thread checks the termination of the child thread and quits when the child thread completes its
execution. . . ... .. - :
(e) The main thread continues sending random messages to the child thread till the WMyQUIT message is sent
to child thread. . ,, > . , ., • - .»
(f) The messaging mechanism between the main thread and child thread is synchronous.
- Compile the application using Visual Studio and execute it under Windows XP/NT OS.
8. Write a Win32 console application illustrating the usage of anonymous pipes for data sharing between a parent
and child processes using handle inheritance mechanism. Compile and execute the application using Visual Stu¬
dio under Windows XP/NT OS.
9. Write a Win32 console application for creating an anonymous pipe with 512 bytes of size and pass the ‘Read
handle’ of the pipe to a second process (another Win32 console application) using a memory mapped object. The
first process writes a message “Hi from Pipe Server”. The second process reads the data written by the pipe server
to the pipe and displays it on the console window. Use event object for indicating the availability of data on the
pipe and mutex objects for synchronising the access to the pipe.
10. Write a multithreaded Win32 Process addressing:
(a) The main thread of the process creates an unnamed memory mapped object with size IK and shares the
handle of the memory mapped object with other threads of the process
(b) The main thread writes the message “Hi from main thread” and informs the availability of data to the child
thread by signalling an event object
(c) The main thread waits for the execution completion of the child thread after writing the message to the
memory mapped area and quits when the child thread completes its execution
(d) The child thread reads the data from the memory mapped area and prints it on the console window whefr the
event object is signalled by the main thread
(e) The read write access to the memory mapped area is synchronised using a mutex object
11. Write a multithreaded application using Java thread library satisfying:
(a) The first thread prints “Hello I’m going to the wait queue” and enters wait state by invoking the wait
method.
(b) The second thread sleeps for 500 milliseconds and then prints “Hello I’m going to invoke first thread” and
invokes the first thread. , (
(c) The first thread prints “Hello I’m invoked by the second thread” when invoked by the second thread.
lip.
LEARNING OBJECTIVES
In Chapter 10 we discussed about the fundamentals of RTOS, the different kernel services of an
RTOS, the different techniques used for implementing multitasking, the different mechanisms used for
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
communicating between multiple concurrently running tasks and the mechanisms to synchronise the
execution of tasks and accessing of shared resources. The implementation of the multitasking strat¬
egy, inter-task communication and synchronisation is OS kernel dependent. It varies across kernels.
Let’s have a look at these implementations for the VxWorks and MicroC/OS-II Real Time Operating
Systems.
• - - ■- ■ • - r- : . ■ -■ ■ , :• ■ : . ..
VxWorks is a popular hard real-time, multitasking operating system from Wind River Systems (http://
www.windriver.com/1. It is fully compatible with the POSIX Standard (1003.1b) for real-time exten¬
sion. It supports a variety of target processors/controllers including Intel x-86, ARM, MIPS, Power PC
(PPC) and 68K. Please refer to the Wind Driver System’s VxWorks documentation for the complete list
of processors supported.
The kernel of VxWorks is popularly known as wind. The latest release of VxWorks introduces the
concept of Symmetric multiprocessing (SMP) and thereby facilitates multicore processor based Real
Time System design. The presence of VxWorks RTOS platform spans across aerospace and defence ap¬
plications to robotics and industrial applications, networking and consumer electronics, and car naviga¬
tion and telematics systems.
VxWorks follows the task based execution model. Under VxWorks kernel, it is possible to ran even
a ‘subroutine’ as separate task with own context and stack. The ‘wind* kernel uses the priority based
scheduling policy for tasks. The inter-task communication among the tasks is implemented using mes¬
sage queues, sockets, pipes and signals, and task synchronisation is achieved through semaphores.
a debug request for debugging a task in execution or if an exception happens during the execution of a
task, it is moved to the state ‘SUSPEND’.
When a task is created with the task creation kernel system call tasklnitQ, the task is created with the
state ‘SUSPEND’. In order to run the newly created task, it should be brought to the ‘READY’ state. It
is achieved by activating the task with the system call taskActivateQ. The system call taskSpawnQ can
be used for creating a task with ‘READY’ state. The taskSpawn() call follows the syntax
where task name is the name of the task and it is unique. The parameter task:jpriority defines the prior¬
ity for the task. It can vary from 0 (Highest priority) to 255. The taskjjptions parameter specifies the
options for the task. For example, if the task requires any floating point operation, it should be set as
VX_FP_TASK. Set the option as VX_UNBREAKABLE for disabling breakpoints in the task. Refer to
VxWorks’ documentation for more details on the task options. The parameter routinejiame specifies
the entry point for the task. It can be function main or a subroutine, ‘argl ’ to ‘arglO’ specifies the argu¬
ments required for executing the task. It is possible to have multiple tasks with the same routinejiame
and different argument list. Each task will have its own stack and context. On creating the task, task¬
Spawn returns a task ID which is a 4 byte handle to the task’s data structure. The following table gives
a snapshot of various task creation and management routines supported by VxWorks.
.r
..'■Al.
Create
'
and initialise• a task.
- s.'
. C
■. . . • v.
: .,5 < * , a •. - ' -'i . •.
^ .>:•
taskNamef)
V? 4 W ,,, ^ St'*
Return
< W
the task name a-soc
W.-W \SjkL t,
i ited vifh a task 11) SU* A. as ™ * ,wA
’ ' ' ’
t Please refer to the VxWorks library reference manual for the parameter details of each function call
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
taskRestart( ) Restart a task. It recreates a task with original task creation parameters
taskDelay() .**■: Sleep the task (Sleep period is given in timer tick units). The state of the task changes to.
, DELAY" The task ;s moved to the end of the ready queue for tasks of the same priority.
nanosleepf) Sleep-the task'(Sleep period is given in nanoseconds). The state of the task changes to
« . DELAY’ - •
exitQ] •" Terminate the calling task and free the memory occupied its stack and TCB.
*>r'*'JFri(, ■<. a \s.-sy", TSjiL m'.-.iX ','i C (,'d'//1'^v”'- -v/-IvTT'''If 71? ~»V> c - JWA' ' L/At ;• "a'/ v.,f" ''vrr-X” j
taskl )elele( ) Terminate the task specified and free the memory occupied by its stack and TCB Tv'. - iV t/u// v:
taskSafe() Protects a task from deletion by other tasks. It is essential for ensuring that the task is not •
deleted by other tasks, when it is holding a shared resource and executing m the critical
section. . • ~ -'
tgskUnsafe() ‘; Used by a task to inform other tasks that it is available for deletion. A task uses
call taskSafeC W' §8ffip
The task creation and management system routines are part of the library taskLib. The following
piece of code illustrates the creation of task.
//Function Prototype -
void-print task(void);
'//Main task ,
void main_task(void)
{ ‘ "
int tasked;
//Spawn Task A
if((taskld = taskSpawn("taskl",90,VX_NO_STACK_FILL,2000,
(FUNCPTR) print_task,0,0,0,0,0,0,0,0,0,0)) == ERROR)
printf ("Creation of print_task failed\n");
exit () ; ”
1
f/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
//Task print_task
int ID;
//Get the task ID
ID = taskIdSelf( );
//Print the task ID
Introduction to Embedded Systems
The main task main task creates a new task with name taskl, priority 90, stack size 2000 and option
NO_STACK_FILL and terminates. The new task taskl retrieves its task ID, prints it and finally deletes
the task.
VxWorks supports two different types of .scheduling policies namely; Round Robin (Time slice
based) and Priority based pre-emptive. The Round Robin scheduling policy is adopted for priority reso¬
lution among equal priority tasks. Imagine a situation where more than one tasks with equal priority are
in the ‘READY’ state and the currently executing task is also with the same priority, if the scheduling
policy adopted is pre-emptive, these tasks may not be able to pre-empt the currently running tasks since
they are of equal priority. The other tasks will get a chance to execute only when the running task volun¬
tarily relinquishes the CPU or it is pended on a resource request. This issue can be addressed if Round
Robin policy is adopted for resolving the priority among equal priority tasks. Round Robin scheduling
can be enabled with the function call kernelTimeSliceQ. This function takes the time slice in the form of
t Please refer to the VxWorks library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
timer tick. Each task is executed for the amount of time specified, if there are multiple tasks with equal
priority exist. The task relinquishes the CPU to the other task with equal priority when this time interval
is completed. Setting a time interval value of 0 for the time slice interval disables Round Robin schedul¬
ing for equal priority tasks.
VxWorks kernel has a well-defined exception handling module which takes care of the handling of
exceptions that arises during the execution of a task. In case of an exception, the task caused the excep¬
tion is suspended and the state of the task at the. point of exception is saved-.-AH other tasks and kernel
continue their operation without any interruption.
It is not necessary that a message queue always have space to accommodate a message when a task
tries to write a message to it. The message queue may be full and may not have room for accommodat¬
ing a message. In such case the task can either wait until a space available in the queue or can return
immediately. Similarly when a task tries to read a message from the message queue, there may be situa¬
tions in which the message queue is empty. The task needs to wait until a message is available or it can
return immediately. The choice on whether the task needs to wait or return immediately is determined
by the timeout parameters used in conjunction with message sending and reception. A time out value
NO JVAIT(0), allows the task to return immediately and the value WAIT_FOREVER (-1) forces the task
to wait until a space is available for posting a message in the case of message sending and a message is
available for reading in the case of message reception.
A message can be posted as either normal message or high priority (urgent) message. The msgQSendQ
function provides options to set a message as either normal or high priority. A normal message is placed
at the bottom of the message queue and a high priority message is always placed at the top of the mes¬
sage queue. A value MSGJPR1_N0RMAL for the message priority option declares it as normal message
whereas the value MSG_PRI_URGENT sets a message as urgent.
The following piece of code illustrates the implementation of Wind message queue based communi¬
cation in a multitasking application.
[ Example I
Here a main task creates two tasks, namely, ‘task_a' and ‘‘taskjb’ with priority 100, stack size 10000 and option NO_
STACK_FILL. It also creates a message queue with number of messages supported as 50 and the maximum length in
bytes for a message as 20. The message queue is created with an option for queuing the pended tasks (tasks waiting for
a message to arrive in the message queue) as FIFO. ‘task_a’ sends the message “Hi from Task A" with message priority
‘urgent’ and with option for waiting if there is no space available in the message queue for posting the message. ltask_b'
receives the message from the message queue and waits for the availability of a message if the message queue is empty.
f Please refer to the VxWorks library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
♦include "stdio.h"
//Header for Wind Message queue related functions
#include "msgQLib.h"
MSG_Q_ID MsgQueuelD;
void task_A(void)
(
//Send message to message queue .
if((msgQSend(MsgQueuelD, MESSAGE, MAX_MESSAGE_LENGTH, WAITJFOREVER,
MSG_PRI_URGENT) ) == ERROR) ..
printf ("Message ^endi'-ng £ailed.:.\ri"') •; ' ' :
yy********************************************************************
//Main task for creating the message queue and creating tasks A & B
void main_task(void)
{
int taskld;
//Spawn Task A
if((taskld = taskSpawn("task_a",100,VX_NO_STACK_FILL,10000,
(FUNCPTR)task_A,0,0,0,0,0,0,0,0,0,0)) == ERROR)
printf("Creation of Task A failed\n");
//Spawn Task B
if ((taskld = taskSpawn("task_b",100,VX_NO_STACK_FILL,10000,
(FUNCPTR)task_B,0,0,0,0,0,0,0,0,0,0)) == ERROR)
printf ("Creation of Task B failed\n") ;
h ' -
The POSIX standard supports named message queues with options for setting message level priori¬
ties. The table given below summarises the list of POSIX Message queue function calls supported by
VxWorks.
mqjiotifyO Notifies a task, when a message for the task arrives in an empty message queue. The mqjiotifyO
call specifies a signal to be sent to the task when a message is placed in an empty queue. A task
can cancel the notification registration at any point during the waiting by passing a NULL in
place of the signal value to the mq_notify() function call.
mq_setattr( ) Sets the attributes for the message queue, The attributes are: ,
1. Blocking flag 0_N0NBL0CK. - . * ' •.
> 2. Allowed maximum number ofmessages in the queue. ; |||g
3. Allowed maximum size df a message.' - 1 V’
4. The number of messages currently present in the queue.
A taskuan change Only the blocking flag. ■ ;
mq_getattr() Retrieves the attributes for the message queue.
t Please refer to the VxWorks library reference manual for the parametei details of each function call
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
Pipes are another form of IPC mechanism supported by VxWorks. Pipes use the message queue
based technique for communication. The only difference is that pipe acts as a virtual I/O device and it
can be accessed with I/O access calls in the same way as accessing an I/O device. The syntax for pipe
creation function is given below.
pipeDevCreate ("/pipe/name", max_msgs, max_length);
where the first parameter is the name of the pipe, the parameter maxjnsgs represents the maximum
number of messages and the allowed maximum size of a message. Normal Input/ Output routines like
open, read, write and I/O controls are used for accessing pipes. Interrupt Service Routines are only
allowed to write to a pipe and not reading from the pipe.
Sockets are the mechanism utilised by VxWorks to communicate between processes running on different
targets which are networked and for executing a procedure (function) which is part of another process
running on the same CPU or on a different CPU within the network (Remote Procedure Call (RPC)).
Sockets works on Internet communication protocols. VxWorks supports TCP/IP and UDP based proto¬
cols for socket communication.
‘Signals’ is a form of software event notification based Inter Process Communication (IPC) mecha¬
nism supported by VxWorks. The ‘signal’ mechanism of VxWorks supports 31 different signals. A
signal is associated with an event and the occurrence of the event asserts the signal. Any task or Inter¬
rupt Service Routine (ISR) can generate a signal which is intended for a task. Upon receiving the signal
notification, the task is suspended and it executes the ‘signal handler’ associated with the signal on its
next scheduling. As a rule of thumb the signal handler should not contain any routines which contain
code (e.g. Accessing a shared resource, critical section, etc.) that may cause the blocking of the task and
thereby leading to a deadlock situation. ‘Signal’ based IPC mechanism is more appropriate for exception
and error handling than general purpose communication mechanism. The definition for a typical signal
handler is given below.
cvoid sigHandler (int slgNum)
where sigHandler is the name of the signal handler function and sigNnm is the signal number.
The definition for a signal handler for passing additional arguments will look like
void sigHandler (int sigNum, int code, struct sigcontext *pSigContext)
where sigHandler is the name of the signal handler function, sigNam is the signal number, code is the
additional argument and pSigContext is the context of task before signal.
The Signal handler function should be attached to the signal using the function sigactionf) (For
POSIX compliant signal usage) or sigvec() for Wind signal usage.
VxWorks support two kinds of ‘signal’ interfaces, namely;
1. UNIX BSD Style Signals
2. POSIX Compatible Signals
The table given below summarises the list of basic ‘Signal’ function calls supported by VxWorks.
siginit( ) Signal library initialisation routine. It must be executed during the OS initialisation before
enabling interrupts: r' . '
signal() Specifies the handle associated with a signal. Supported by both Wind signal interface and
POSIX interface/
t Please refer to the VxWorks library reference manual for the parameter details of each function call.
Introduction to Embedded Systems
void task_A(void)
{
//.
//.
//Disable task preemption
taskLock (>; - -V: . ,:-p
Interrupt locking based mutual exclusion implementation disables interrupts. It is appropriate for
exclusive access of resource shared between tasks and Interrupt Service Routine (ISR). The major draw¬
back of this method is that the system will not respond to interrupts when the task is executing the criti¬
cal section code (Accessing shared resource). It is not acceptable for a real time system.
An overview of the implementation of interrupt locking based mutual exclusion is given below.
♦include "vxWorks.h"
Jl j ******** * *•* * * * ***■*********-***'********************************■*** *•
void
//Disable Interrupts MH
■int-vlock = intLock () ;
•'■ A •?' T v t Av,' i && i'*' “ A £«, ‘
//Shared data.-accessJcodel
II..
//Enable .Interrupts /A//};/:/
intUnlock(lock) ;
It should be noted that the implementation of the intLock routine is processor architecture dependent.
The intLock routine returns an architecture-dependent lock-out key representing the interrupt level prior
to the call. For example, for ARM processors, interrupts (IRQs) are disabled by setting the T bit in the
CP SR.
Semaphores provide both exclusive access to shared resource and synchronisation among tasks. Vx¬
Works has the implementation for Wind specific and POSIX compliant versions of semaphores. The
Wind specific Semaphores supported by VxWorks are:
1. Binary semaphore
2. Mutual exelusion semaphore
3. Counting semaphore
The binary semaphore provides exclusive access to resource by locking it exclusively. At any given
point of time tj^ state of the binary semaphore is either ‘available’ or ‘unavailable’. Counting sema¬
phore is used for sharing resources among a fixed number of tasks. It associates a count with it and the
count is decremented each time when a resource acquires it. When the count reaches ‘O’ the semaphore
will not be available and a task requesting the semaphore is pended. The count associated with a count¬
ing semaphore is specified at the time of creating the counting semaphore. Counting semaphores are
usually used for protecting multiple copies of resources. On releasing the semaphore by a task, the count
associated with it is incremented by one. The inherent problem with binary semaphore is that they are
not deletion safety, meaning whenever a task holding the semaphore is deleted, the semaphore is not
released. Also the ‘binary’ semaphore doesn’t support a mechanism for priority inheritance for avoiding
priority inversion. These limitations are addressed in a special implementation of ‘binary’ semaphore
called ‘mutual exclusion’ semaphore.
The semaphores are distinguished with the way they created. The access and control routines for all
semaphores remain the same. The following table gives a snapshot of the different semaphore related
function calls.
Introduction to Embedded Systems
semBCreateQ. Create and initialise a binary semaphore. Successful creation retums a semaphore ID whichf
can be used as the semaphore handle for all semaphore related actions. When a task tries to
access a semaphore, if the semaphore is not available the task is put in a wait queue. The queue
is ordered either in FIFO model or priority model. It can be specified at the time of creation
of the semaphore. A value'hf SHiM Q FIFO sets FIFO model whereas SEM Q PRIORITY
vsets the priority model. The semaphore creation also specifies its status at the time of creation
(Available or unavailable). .... . , < - v: ■ ■t
;i Create.and initialise amutual-exclusion semaphore.iSuccessfiili^reation returns a= semaphore
1 ID which can be used as the semaphore handle for all semaphore related actic
g tries 40 access a; semaphore, ;if the
.l.The queue is ordered either in I
of creation of the semaphore.'
PRIORITY sets the priority me
of creation (Available or unav
>it:, ASR; SA A;
semBCCreate/) t Create and initialise a counting semaphore. Successful creation returns a semaphore ID which
can be used as the semaphore handle for all semaphore related actions. When a task tries to
access a semaphore, if the semaphore is not available the task is put in a wait queue. The queue
is ordered either in FIFO model or priority model. It can be specified at the time of creation
of the semaphore. A value of SEM_Q FIFO sets FIFO model whereas SF.M Q PRIORITY
sets the priority model. The semaphore creation also specifies its status at the time of creation
(Available or unavailable). . ' £ , , V' '
" seniDeIete() Terminate and free a semaphore. , ;r; y a ,* . A Wri .Iri ,
semTake( ) Acquires a semaphore. ' , .a y;A.-a . , v. . aa
The mutual exclusion semaphore has an option to set its priority to the highest priority of the task
which is being pended while attempting to acquire a semaphore which is already in use by a low priority
task. This ensures that the low priority task which is currently holding the semaphore, when a high prior¬
ity task is waiting for it, is not pre-empted by a medium priority task. This is the mechanism supported
by the mutual exclusion semaphore to prevent priority inversion. To make use of this feqfure, the mutual
exclusion semaphore should be created with the option SEM_INVERSION_SAFE and the queue holding
the pended tasks waiting for the semaphore should be sorted on priority basis. These two options should
be specified while creating the semaphore. The syntax for this is given below.
Id = semMCreate (SEM_Q_PRIORITY | SEM__INVERSION_SAFE) ;
where Id is the 4 byte ID returned by the system on creating the semaphore. It is used as the handle for
accessing the semaphore. The option SEMjQ PRIORITY sets the ordering of the queue, holding the
pended tasks which are waiting for the semaphore, as priority based. The option SEMJNVERSION_
SAFE enables priority inheritance.
The mutual exclusion semaphore protects a task from deletion when it is currently in the critical sec¬
tion. If the task is not protected from deletion when it is holding a semaphore and is executing in the
critical section, the semaphore will not be released properly and it may not be available for access to
f Please refer to the VxWorks library reference manual for the parameter details of e^ch function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
other resources. This may lead to starvation and deadlock. In order to make a semaphore ‘task deletion
safe’, it should be created with the option SEM_DELETE_SAFE. The queue holding the pended tasks
waiting for the semaphore can be sorted on either priority basis or first come first served (FIFO) basis.
The task safe option should be specified while creating the semaphore. The syntax for this is given be¬
low.
Id - semHCreat'.e (SEM_Q PRIORITY ! SF.M -D5lLETF._ SAFE)r. ./( W "Tf •' '
of the task which is currently preempted by the ISR is used by ISR. In this case the stack size for
each task should be large enough to accommodate ISR stack requirement also.
3. Supports connecting of ‘C’ function with system hardware interrupts which are not used by Vx-
Works kernel. The connecting of ‘C function’ with the interrupt vector is achieved through the
function call intConnect(). It is responsible for saving the processor registers, setting up the stack
entry point, pushing the argument, to be passed to the ISR function, on the stack and invoking the
‘C function’ as ISR. The intConnectQ function takes the arguments; byte offset of the interrupt
vector to connect to, the address of the ‘C function’ to be connected with the interrupt vector and
an argument to the ‘C’ function. The ‘C function’ becomes the ISRxfor the interrupt. On comple¬
tion of the ISR, the registers are restored to the original state. j
4. To avoid starvation and deadlock situations, as a rule of thumb, ISRs are not allowed invoking
routines (involving the access of shared resource protected through semaphores) that may block
the ISR.
5. ISRs are not allowed to acquire (take) a semaphore. But are allowed to release (give) a semaphore
(except mutual exclusion semaphore).
6. ISRs are not allowed to invoke memory allocation and de-allocation functions like mallocQ, cal-
loc(), free() etc. The memory allocation functions internally make use of semaphores for synchro¬
nisation and they may block the ISR.
7. ISRs are not allowed to call any creation or deletion routines. The creation and deletion calls may
use semaphores internally and it may block the ISR.
8. ISRs must not perform I/O operations through VxWorks drivers. The restriction is put for the rea¬
son, most of the device drivers require a task context and it may block the ISR. However, ISRs are
allowed write operations for pipes, which is a virtual I/O device.
9. ISR must not call routines/instructions involving floating point operations. The reason is that the
floating point registers are not saved and re-stored by the ISR connecting code. It is the responsi¬
bility of the user to save and re-store floating point registers explicitly, if the ISR involves floating
point calculation.
10. ISRs can make use of the logging service supplied by logger component to send text messages to
the system console.
11. If an exception happens during the execution of an ISR, wind stores the exception details and re¬
sets the system. During the boot-up time, the boot ROM checks for captured exception messages
and displays it on the display console.
12. Interrupts communicate with tasks using shared memory, message queues, pipes, semaphore (can
give a semaphore but can’t take one), pipes and signals.
13. The intLock()function performs a system wide lockout of all interrupts to the highest priority level
supported by the kernel. If the intention is to lockout only interrupts up to a certain level and the
system wants to preserve the occurrence of high priority intemrpts, it can be achieved through the
system call intLockLevelSet(), which sets the lock-out priority level to a desired one than locking
out all interrupts including high priority. With an intLockLevelSet(), the intLock()function locks
only interrupts with priority level specified by the lock level in intLockLevelSetQ. All other en¬
abled, high priority interrupts are acknowledged.
14. For interrupts whose priority is higher than that of the lock level set by the function intLockLevel-
Set() and Non-maskable interrupts, the system call intVecSetQ should be used for connecting a C
function as ISR for the interrupt and also the ISR should not use any kernel facilities that depend
on interrupt locks for their operation.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
The following table gives a snapshot of the Interrupt handling related function calls under
VxWorks.
returned
intCountO Returns the current interrupt;
lintlevelSe i H
intVecBaseSet(FUNCPTR *.baseAddr) Sets the CPU’s vector base register to the specified value. Initially it. will be :
0 for almost all processors (Exceptions are their). The subsequent calls to
and intVecGet() uses this base address value
roBwnfffliii
t Please icier to the VxWorks library reference manual for the parameter details of each function call.
Introduction to Embedded Systems
watchdog timer, the watchdog expires and it raises a watchdog timeout event. The necessary actions to
handle the situation can be written in the watchdog timeout handler. Under VxWorks, watchdog timeout
is handled as part of the system clock ISR. User can associate s ‘C’ function with the Watchdog timer.
The ‘C’ function is executed when the watchdog expires. The time is specified in terms of system timer
tick. The timer is created using the function call wdCreateQ. The timer is started with the function call
wdStartQ and it takes the number of ticks as timer value, ‘C’ function to be executed in case of timeout
and the parameter to the ‘C’ function as parameters. The watchdog timer can be stopped and cancelled
at any time prior to its expiration by calling the function wdCancelQ.
The following table gives a snapshot of the watchdog timer related function calls.
>t:-Lt■’■'ivlriij. ^
fcisffc t ptiitcn n ( ■ .truer
1 •' ; :-n ■ Iv"::LLTr o-.ij lie-Uncute a Wa'chdot; titest ;
11.2 MicroC/OS-II V
MicroC/OS-II (pC/OS-II) is a simple, easy to use real-time kernel written in ‘C’ language for embedded
application development. MicroC OS 2 is the acronym for Micro Controller Operating System Version
2. The [xC/OS-II features:
• Multitasking (Supports multi-threading)
• Priority based pre-emptive task scheduling
MicroC/OS-II is a commercial real-time kernel from Micrium Inc (www.micrium.com). The
MicroC/OS-II kernel is available for different family of processors/controllers and supports proces¬
sors/controllers ranging from 8bit to 64bit. ARM family of processors, Intel 8085/x86/8051, Freescale
f Please refer to the VxWorks library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
68K series and Altera Nios II are examples for some of the processors/controllers supported by MicroC/
OS-II. Please check the Micrium Inc website for complete details about the different family of proces¬
sors/controllers for which the MicroC/OS-II kernel port is available. MicroC OS follows the task based
execution model. Each task is implemented as infinite loop.
| ■ while (1)
It creates a task with name MicroCTask. The task doesn’t return anything. The argument to the task
is passed through a pointer to void. Depending on the argument required the pointer can pass structure
variables, other data types or function pointers to the task. The task MicroCTask is in the ‘Dormant’ state
now.
You need to pass this task to the MicroC kernel to make it ready for execution. It is essential for as¬
signing resources to the task MicroCTask. The MicroC kernel call OSTaskCreateQ or OSTaskCreateExQ
is used for this. These function calls take the address of the task, the priority assigned to it and the stack
size requirement for the task as parameters. OSTaskCreateQ is the older version of the task allocation
call and OSTaskCreateExQ is the enhanced version of it. Calling these functions allocate the memory
for the task and put the task in the ‘Ready’ state. The usage of these functions is illustrated below.
Introduction to Embedded Systems
f Please refer to the MicroC library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
OSTaskDeKj Deletes a specified task. The task is specified by its priority. Calling this routine places the
j £. , .task-in ‘Dormant’ state. The task can be-recreated later, if needed, by calling OSTaskCCreate
: “ .:6¥ OSTaskCreateExt. It is the responsibility of the calling function to ensure that the task
‘' . 'doesn’t hold resources like semaphores and mailboxes. A task can delete itself by passing
OS_PRIO_SELF as the argument to this function call.
• OSTaskDelReqQ Same as OSTaskDelQ funotion.11 sends a task delete request to the-MicroC kernel. This
call makes the deletion safe and. protects the task from deletion when it is holding si
resources like semaphore and mailboxes.
: OSTaskNameGetQ Returns the name associated with a task. The task is identi using its prronty.
: is identified usi
i task in a Task C
Interrupt Service Routines (ISRs) are not allowed to create a task. The stack statistics of a task can
be examined by calling the function OSTaskStkCheckQ. In order to get the stack statistics for a task, the
task should be created with the option OS TASKJJPT_STK_CHK. The stack details of the stack for a
task in terms of no. of bytes used and no. of bytes free in the stack are returned in a structure variable
of type OS_STK_DATA. The execution time of this call is determined by the size of the stack and hence
the call is non-deterministic. It is advised not to call this function from ISR due to its non-deterministic
nature. The following code snippet illustrates the usage of this function.
II ******************************************* ***** ****** *************
‘//Define variable of type OS_STK_DATA y ;-
OS_STK_DATA stack__mera; ' r ■7 •••• v ■"
•//Define 32 bit integer for holding stack size - ■
. INT32U stack_.size.; « ... •
//Define variable for holding return value
INT8U err;
The function calls OSTaskSuspendQ and OSTaskReswne() are used in combination to suspend and
resume a task. Whenever an OSTaskSuspendQ call is made by a task, the task is put in the ‘Waiting’
state and the scheduler picks up the highest priority task from the ‘Ready’ list for execution. Suspend¬
ing of IDLE task is not allowed. The following code snippet illustrates the usage of the task suspend
function.
//**************************************************************»****
//Task implementation
void task(void *args)
{
while (1)
A task can delay itself by calling either OSTimeDlyQ or OSTimeDlyHMSMQ function. Upon calling
these functions, the task is put into the ‘Waiting’ state. The task becomes ready for execution when the
delay time expires or when another task resumes the delayed task by calling a task resume function.
Task delaying is the mechanism by which a task can voluntarily relinquish the CPU to other tasks when
it is waiting for any events to occur. A task can only delay itself and cannot impose delaying of other
tasks. The activation of the delayed task to ‘Ready’ state from ‘Waiting’ state, when the wait period ex¬
pires is done by the function OSTimeTickQ. The function OSTuneTichQ is the Interrupt Service Routine
(ISR) for the system timer clock and the kernel executes it. Users don’t need to bother about it. A task
which is currently in the waiting state introduced by the self delaying can be resumed by another task.
The function call OSTimeDlyResumeQ resumes a specified task which is currently in the ‘waiting’ state
induced by self delaying. A task which is self delayed by invoking OSTimeDlyHMSMQ function cannot
be resumed by another tasks if the combined delay set by the hour, minute, seconds and milliseconds
parameter exceeds 65535 timer ticks.
The task self delaying and resuming it from another tasks technique is a good approach for building
applications where one task requires waiting for I/O operations. The task involving Input operation (e.g.
Waiting for a key press) can self delay it and key press event can be handled in an Interrupt Service
Routine (ISR) and the ISR can resume the delayed task upon receiving the key press event. The follow¬
ing code snippet illustrates the usage of task delay and task resuming functions.
//**************************************■* ****************************
//Task implementation
void taskA,(void *args)
{
while (1)
{
//Execute the task specific details
//.....
//.....
//Delay' the task for 5 Timer ticks. >
OSTimeDly(5);
//Rest of the task
//..'..
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
The function pair OSTaskSuspendQ and OSTaskResumeQ can be used for suspending a task uncon¬
ditionally and resuming the suspended task. If a task which is being suspended is delayed by task delay
call, the task is resumed only after the task is resumed from suspension and both the time expires.
Tasks can be deleted by calling OSTaskDelQ. The task which is being deleted is kept in the ‘Dormant’
state and it can be recreated, if needed by calling task creation functions like OSTaskCreateQ or OSTa-
skCreateExQ. Proper care should be applied in the usage of OSTaskDelQ function. Imagine a situation
where Task 1 is holding a shared resource, a semaphore and Task 2 attempts to delete Task 1 by execut¬
ing OSTaskDelQ. Task 1 will be moved to the ‘Dormant’ state and this will lead to the locking of the
semaphore used by Task 1 and this semaphore will not be available to other tasks - leading to starvation
of tasks. The function OSTaskDelRepQ makes task deletion safe by ensuring that the task is not deleted
when it is holding a shared resource.
OS_EXIT_CRITICAL
OSTaskidleHook
It simply increments a counter to keep the CPU always active. Depending on the power saving re¬
quirements, the IDLE task can be re-written to include the CPU specific power down modes.
The MicroC/OS-II kernel runs a statistical task for computing the percentage of CPU usage. The
statistical task is created with priority OS_LOWEST_PRIO -1 and it is executed in every second. The
function call OSStatinitQ sets up the statistical task related activities. This function must be called be¬
fore OSStartQ. The OSStartQ function starts the multitasking. It is invoked only after invoking OSInitQ.
It starts the execution of the highest priority task from the ‘Ready’ list. It never returns to the calling
function. There should be at least one task to execute. The following code snippet illustrates the OS
initialisation and start operations.
# in elude <inclu:
//Main task
void ..main (void.)
OS-II kerne] sendee and is responsible for handling the context switch operation. It saves the CPU reg¬
isters and Program Status Word (PSW) to the stack of the preempted task (context saving) and loads the
CPU registers and PSW from the stack of the highest priority task (context retrieval) which is going to
execute.
The task rescheduling will not take place if the scheduler is in the locked state. The scheduler can
be locked by invoking the system call OSSchedLockQ. Proper care should be applied in the usage of
OSSchedLockQ. Once the scheduler is locked with this call, the application should not call any other
function calls, which suspends the execution of the current task, like OSTaskSuspendQ ,OSTaskResume (),
OSTaskDelQ, OSTimeDlyQ, OSTimeDlyHMSM(). Since the scheduler is in the locked state it will not
be able to schedule another task for execution and the current task is already in the suspended stage, the
system hangs-up.
The scheduling can be resumed by calling OSSchedUnlock(). The MicroC/OS-II kernel supports
nested locking of scheduling up to 255 levels deep. Scheduling is enabled only when the same number
of scheduler unlocking calls as that of scheduler locking calls is executed. Interrupts are serviced even
when the scheduler is in the locked state. The scheduler locking and unlocking mechanism is used as a
technique for exclusive access to shared resources (An implementation of mutual exclusion). The fol¬
lowing piece of code illustrates the usage of scheduler locking and unlocking functions.
/'/***-*****'********** + **+W*************il-****** + *****.V*r ***************
'//Task implementation ' '' '
void taskAfvoid *arcrst
taskA(void *args) - • ' '
while (1)
s{ ( * y-* , -^V ' f 1
//Execute the task specific detaiS| II "’A .AS
The priorities associated with a task can be changed dynamically. The function call OSTaskChange-
Prio() changes the priority associated with a task. The function takes current priority and new priority
as arguments;.
Mailbox and message queues are same in functionality. The only difference is in the number of mes¬
sages supported by them. Both of them are used for passing data in the form of message(s) from a task
to another task(s) or from an I SR to task(s).
Mailbox is used forexchanging a single message between two tasks or between an Interrupt Service
Routine (ISR) and a task. Mailbox associates a pointer pointing to the mailbox and a wait list to hold
the tasks waiting for a message to appear in the mailbox. The following table gives a snapshot of the
important mailbox related routines supported by MicroC/OS-Il.
A mailbox is created with the function call OSMboxCreate(void *msg), where the parameter msg is
a pointer holding the initial message. If this is a non-null value, the mailbox holds the message pointed
by msg. If msg is a null pointer, the mailbox is created without any messages present initially. This func¬
tion allocates an Event Control Block (ECB) to the mailbox and returns a pointer of type OSJsVENT
to the allocated ECB. The Event Control Block (ECB) is a data structure holding information like,
the event associated, list of tasks waiting for this event to occur, etc. A task can post a message to the
mailbox if the task knows the ECB pointer for the mailbox. Along with the ECB pointer of the mail¬
box, the task passes a pointer to the message which is to be posted to the mailbox, as argument to the
OSMboxPost(OS_EVENT *pevent, void *msg) function. If the mailbox is not empty when a task is
f Please refer to the MicroC library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
trying to post a message, the task returns immediately. On the arrival of a message in the mailbox, it is
delivered to the highest priority task waiting for the event and the message is deleted from the mailbox.
If the priority of the highest priority task waiting for the message is higher than that of the message
posting task, the message posting task is preempted and the highest priority task is executed by the
scheduler. A task can broadcast a message to all tasks waiting for a message by posting the message
with the function call OSMboxPostOptfOS EVENT *pevent, void *msg, INT8U opt). The lopf param¬
eter should be set as OS_POST_OPT_BROADCAST for broadcasting a message. If this parameter is set
as OS_POST_OPTJAONE, this function becomes equivalent to OSMboxPost. The functions OSMbox-
PendQ, and OSMboxAcceptQ can be used for retrieving a message from the mailbox. OSMboxPendQ
provides option to set the function call as either a blocking call till a message is available in the mail¬
box or till a wait period specified in the parameter list. OSMboxAccept() is a non-blocking call. This
function will not block a calling task when there is no message available in the mailbox. This can be
used by ISRs to check the availability of messages in a mailbox. A task can delete a mailbox by calling
OSMboxDel(OS_EVENT *pevent, INT8U opt, INT8U *err). The parameter ‘opf specifies whether a
mailbox needs to be deleted immediately or only when the pending operations on this mailbox are over.
The value OS_DEL_NO_PEND for ‘opt’ sets the first choice and a value OSJDEL_ALWAYSdeletes the
mailbox immediately. Proper care should be applied in the usage of this function. There are possibilities
for tasks to access a deleted mailbox. This may result in unpredicted behaviour. Also tasks which are
using the function OSMboxAcceptQ for checking the availability of a message may never know about
the non-availability of a mailbox. Hence use this function judiciously and as a precautionary measure
delete all tasks which uses the specified mailbox before it is being deleted. The following piece of code
illustrates the usage of mailbox functions:
/ /***** *** *************** *^7r**:r******x***%** *** *** *ir**-»r **** ****x + * ****
#incluae ^Includes . h> « ' A 4A ,
//Define Task IDs for First, Task, Task 1 and Task 2 \ - - v
#define TASK_FIRST_ip T .... ; .
♦define TASK _i_ ID 2 - • - . U' ! A *V : ’’ ' .
’ #define TASK 2_ ID 3 ' . V' \
l {
?//Initialise KicroC OS
I’ OSIn-it ( ) ;
f- \
’//Create the first task.
OSi’a skCrca teExt (lar,r. Fr s t, - •
//Create Task2
OSTaskCreateExt(Task2,
(void *)0,
STask2Stk [TASK_STK__SIZE - 1],
TASK_2_PRIO,
TASK_2_ID,
&Task2Stk[0],
TASK_STK_SIZE,
(void *) 0,
OS TASK OPT STK CHK);
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
while (1)
{
/'/Implementation\>f Taskl
void Taskl (void *3^3)
while (1)
The sample application creates a main task TaskFirst which creates a mailbox and two tasks namely
Task! and Task2. Task! posts the message ‘0’ to the mailbox and Task2 reads the mailbox to retrieve the
message present in it. The startup tasks, viz. the initialisation of MicroC kernel (OSInitQ), main task
creation and starting of multitasking (OSStartQ) is done by the main function.
Message queue is same as that of mailbox in operation; the only difference is—mailbox is designed
for handling only a single message whereas message queue is designed for the handling of multiple mes¬
sages, Message queue is used for exchanging a message data between two or more tasks or between an
Interrupt Service Routine (1SR) and tasks. Message queue associates a pointer pointing to the Message
queue and a wait list to hold the tasks waiting for a message to appear in the Message queue. The following
table gives a snapshot of the important message queue related routines supported by MicroC/OS-II.
OSQDelQ Deletes a message queue. Option for specifying whether the message queue needs to be deleted
immediately or only when the pending operations on this message queue are completed.
f Please refer to the Micio< lihrnn leteicve manual for the each Umni-'u
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
A message queue is created with the function call OSQCreate(void **start, INT8Usize), where start
is the base address of the message storing area and size is the size of the message storage area in bytes.
This function allocates an Event Control Block (ECB) to the message queue and returns a pointer of
type OS_EVENT to the allocated ECB. A task can post a message to the message queue if the task knows
the ECB pointer for the message queue. Along with the ECB pointer of the mailbox, the task passes a
pointer to the message which is to be posted to the message queue, as argument to the OSQPost(OS_
EVENT *pevent, void *msg) function. If there is no room for a message in the message queue when a
task is trying to post a message, the task returns immediately. On the arrival of a message in the message
queue, it is delivered to the highest priority task waiting for the event and the message is deleted from
the message queue. If the priority of the highest priority task waiting for the message is higher than
that of the message posting task, the message posting task is preempted and the highest priority task is
executed by the scheduler. A task can broadcast a message to all tasks waiting for a message by posting
the message with the function call OSQPostOpt(OS_EVENT *pevent, void *m.sg, INT8Uopt). The lopf
parameter should be set as OS POST OPT BROADCAST for broadcasting a message. If this parameter
is set as OS POST_OPT_NONE, this function becomes equivalent to OSQPost. This function can also
serve the purpose of the function OSQPostFrontQ, which posts the message to the front of the message
queue. For this, set the ‘opt’ parameter as OS_POST_OPT_FRONT. The functions OSQPendQ, and
OQSAccept() can be used for retrieving a message from the message queue. QSQPendQ provides op¬
tion to set the function call as either a blocking call till a message is available in the message queue or
till a wait period specified in the parameter list. OSQAcceptQ is a non-blocking call. This function will
not block a calling'task when there is no message available in the message queue. This can be used by
ISRs to check the availability of messages in a message.queue. A task can delete a message queque by
calling OSQDel(OS_EVENT *pevent, INT8U opt, INT8U *err). The parameter ‘opf specifies whether
a message queue needs to be deleted immediately or only when the pending operations on this message
queue are over. The value OS_DEL_NO_PEND for 'opt’ sets the first choice and a value OS_DEL_AL-
WAYS deletes the message queue immediately. Proper care should be applied in the usage of this func¬
tion. There are possibilities for tasks to access a deleted message queue. This may result in unpredicted
behaviour. Also tasks which are using the function OSQAcceptQ for checking the availability of a mes¬
sage may never know about the non-availability of a message queue. Hence use this function judiciously
and as a precautionary measure delete all task which uses the specified message queue before it is being
] deleted.
Message queue and mailbox operates on the same principle. However message queue supports mul-
| tiple messages and also provides option to post a message as urgent message, by posting it in front of the
j message queue. Mailbox holds only one message at a time and there is no provision to post a message
to the mailbox until the existing message is read by a task. Message queue supports emptying of the
message queue by flushing it.
2. Disabling interrupts
3. Semaphores for mutual exclusion
4. Events (Flags) for synchronisation
In a multitasking environment, problems arise only when a task is preempted by another task, when
the task was accessing a shared resource and the task which preempted it also tries to access the re¬
source. This may lead to inconsistent results. The easiest option to solve this problem is prevent task
preemption by disabling the task scheduling when a task is accessing a shared resource. The task sched¬
uling is re-enabled only when the task completes the accessing of the shared resource. Interrupts are still
identified and serviced even if the scheduler is in the locked state. This technique is suited for control¬
ling the access to resources shared between tasks and it will not serve the purpose of exclusive access of
resources shared between a task and Interrupt Service Routine (ISR). The function calls OSSchedLockQ
and OSSchedUnlock() are used for locking and unlocking the scheduler respectively.
We have seen that the disabling of task scheduling will not serve the purpose of exclusive access
to resources shared between a task and ISR. The interrupts should be disabled to ensure error-free op¬
eration when a task is executing in the critical section of code (The piece of code which corresponds
to the shared resource access). The interrupts are re-enabled when the task complete the execution of
the critical section code. Disabling and enabling of interrupts can be done by the MicroC/OS-II sup¬
plied macros; 0S_ENTER_CR1TICAL() and OS_EXIT_CRITICAL() respectively. The implementation
of these macros is processor specific. These macros need to be ported as per the target processor specific
implementation of Interrupt enabling and disabling. These macros are defined in the MicroC/OS-II
header file ‘os_cpu.h\ The usage of these ‘macros’ are illustrated below.
J*J~k'k k H kkk k' k it. it k it kk it k k k k k k k k-k-k k k k k k it k it^kk.k k k’kkk k kkk kit. k kkk k k k kk k kk k k k k
Counter++;
//Leave Critical section.
//Enable Interrupts
OS_EXIT_CRITICAL()!
//Rest of the task
//.
//...
}
Here the variable ‘ Counter’ is shared between a task and ISR. The accessing of it is made exclusive in
the task by disabling the interrupt just before accessing the variable and re-enabling the interrupts when
the operation on the variable is over.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
Disabling of interrupts may produce adverse effects like impact on the real time response of the sys¬
tem. It is advised not to disable interrupts for critical section access operations which are lengthy.
Semaphore based mutual execution is the most efficient and easy technique for implementing ex¬
clusive resource access. Semaphores control the access to shared resources as well as synchronise the
execution of tasks. The MicroC/OS-II supports two different types of semaphores namely counting
semaphore and mutual exclusion semaphore (mutex).
The counting semaphore is used for restricting the access to shared resources and as a signalling
mechanism for task synchronisation. It associates a count for keeping track of the number of tasks using
the semaphore at a given point of time. It also holds a wait list queue for holding the tasks which are
waiting for the semaphore. The following table gives a snapshot of the important counting semaphore
related routines supported by MicroC/OS-II. .
mmmI Signals the availability of a semaphore. A task can use this call to info
the semaphore is available; The Count associated with the semap
Upon executing this call the highest priority-task waiting (If t
■ H1 mm
semaphore is 0) for this semaphore is granted access to the semaph
waiting for the semaphore is greater than that of the priority i * ' =.-u "*• 1
?• mx. & of the semaphore, it is preempted. -' ' .me ' -e
OSSemPendQ Used by a task for acquiring a semaphore. If the semaphore is available (The count associated
with the semaphore is greater than 0) at the time of calling this.function, access is granted to
the task and the count associated with the semaphore is decremented by one. If the semaphore
is not available (The count associated with the semaphore is zero) at the time of calling this-
function, the calling task is blocked and it is put in the semaphore wait list queue-associated
with the semaphore. A task can wait until the semaphore is available or till a timeout occurs.
The timeout value for waiting can be specified in the ‘timeout’ argument of this function call.
The ‘timeout’ value is specified in terms of clock ticks. It can take values ranging from 0 to
65535; A value of 0 indicates wait until the semaphore is available. -
OSSeniAcceptf) Used for acquiring a semaphore. Similar to OSSemPendQ in fund toning but differ in the waylt
operates. It allows a task to check whether a semaphore is available. Unlike OSSemPendQ, the
calling task is not blocked (‘Pended’) if the semaphore is not available. .«/ s ;
OSSemQueryO Retrieves the information about the semaphore. The information is retrieved in a data structure
of type OS_SEM_DATA. It is used for getting information like, the semaphore count, whether
a semaphore is available, is there any tasks waiting for the semaphore and if yes how many
tasks etc.
OSSemSetQ - It is used for changing the current count value associated with the semaphore. Thecount.
|T T ■ T. T,.® . associated with the semaphore is reset .to. the value specifiedin the ‘count’ parameter to this
function. If the semaphore count is 0 at the time of executing this call, the count is changed
- .■ *"• ""'T' ’ only if there are no tasks waiting for this semaphore. This function call is primarily used for
signalling mechanism for task synchronisation and not for protecting shared res6iuclsr. ':lg;^
t Please refer to the Microt fihraiy leleience manual for the parameter details of each function call
Introduction to Embedded Systems
OSSemDeit) Deletes a semaphore. Option for specifying whether the semaphore needs to be deleted
. immediately or only when the pending operations on this semaphore are over. Proper care
. should be applied in the usage of this function. There are possibilities for tasks to .access a
t-4 deleted semaphore. This may result in unpredicted behaviour.
The mutual exclusion semaphore or mutex is a binary semaphore. It differs from the ordinary sema¬
phore in the sense it implements provisions for handling priority inversion problems in task scheduling.
The mutual exclusion semaphore (mutex) temporarily raises the priority of a low priority task holding the
mutex to a priority above the highest priority of the task which tries to acquire the mutex when it is be¬
ing in use by a low priority task. The mutex object associates a flag to indicate whether the mutex object
is available or currently in use and a priority to assign to the task owning the mutex, when another high
priority task tries to acquire it. It also holds a waitlist queue for holding the tasks waiting for the mutex.
Normally the mutex object elevates the priority of a task holding it to a priority value same as that
of the priority of the highest priority task trying to acquire it. However MicroC/OS-II doesn’t support
the existence of two tasks of the same' priority. Hence a small work around is implemented to overcome
this issue. Under MicroC/OS-II, the priority of the task holding the mutex is elevated to a value which
is greater than the highest priority of the task which is trying to acquire it, when it is being held by a
low priority task. The priority reserved to the mutex is known as Priority Inheritance Priority (PIP).
PIP should be an unused priority by tasks. The following table gives a snapshot of the important mutual
exclusion semaphore (mutex) related routines supported by MicroC/OS-II.
H Try
ait list queue associated with the mutex. A task can wait until the niutexliM
timeout occurs. The timeout value for waiting can be specified in the ‘tin
this function call. The ‘timeout! value is specified in terms of clock ticks. It
• ’ ranging from 0 to 65535. A value of 0 indicates' waif until the mutex is avail
OSMutexAcceptQ Used for acquiring a mutex. Similar to OSMutexPendQ in functioning but differ in the way
it operates. It allows a task to check whether a mutex is available. The function returns 1 if
the mutex is available, else returns 0. Unlike OSMutexPend(), the calling task is not blocked
(‘Pended’) if the mutex is not available.
OSMutexPastQ Signals the availability .of .a .mutex,. A task can use |his call to inform'the waiting task that the
•' mutex is available.,The following actions take place on the execution of this call.
1. The flaglssociated with the mutex is set as4, i.e. (Mutex value is set as O’xFT),
2. If the priority of the task holding the mutex was raised to the PIP, it is brought back to the
, <! . original priority of the task. .
3. If more than one task is waiting for, th&mutex. the mutex is. given to the highestfettMigmfe.»
and the flag associated with the mutexis set as 0 (Mutex value is set as,0x00) indicating
mutex is not available.
4. If the priority of the task which acquired the mutex is greater than that of the task which
released the mutex, the task which released the mutex is pended. PTTHlsJIk
5. A rescheduling of tasks is performed.
t Please refer to the MicroC library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
OSMutexQueryO Retrieves the information about the mutex. The information is retrieved in a data structure of
type OS_MUTEX_DATA. It is used for getting information like, whether a mutex is available
/■tv' -• - or not. What is the PIP for the mutex, is there any tasks waiting for the mutex and if yes how
many tasks, etc
-Deletes a mutex
or only when th
j‘Unthe tisagfe-of
J J k k k k_ k'ik rk k$k *;★"* jk k kk kk k kk kr,k k k k kk k k k.k,k k'gk .k'kyif'k kkk kk kk kk kk' kikkk:k;$kkkkk.k kkk k.k:.< ‘ '
//Set the default stack size for all tasks ... . >
.#define -TASK STK SIZE 1024 , ,
II Start multitasking
OSStart{);
//Create Ta'sk2 ‘ ,
OSTaskCreateExt(Task2,
(void *)0,
&Task2Stk[TASK_STK_SIZE - 1],
TASK_2_PRIO,
TASK_2_ID,
&Task2Stk[0] ,
TASK_STK_SIZE,
(void *) 0,
OS_TASK_OPT_STK_CHK) ;
while (1)
{
//Do Nothing. Simply sleep for one timer tick
OSTimeDly(OS_TICKS_PER_SEC) ;
' )
//**************************************************,»****************
//Implementation of Taskl
void Taskl(void'*args) i
{
The sample application creates a main task TaskFirst with priority 10. which creates a mutex MyMu¬
tex with PIP 9 and two tasks namely Task! (with priority 11) and Task2 (with priority 12). Taskl and
Task2 contain critical section access. The mutex MyMutex is used for acquiring exclusive access to'the
critical section. Each task releases the mutex after they exit the critical section of code.
It is possible to associate a name to the Event Control Block (ECB) which is associated with a sema-p
phore, mailbox, message queue or a mutex object. The function call OSEventNameSet(QS EVENT *pe-\
vent, char *pname, INT8U *err) associates the name holding by the character string pointed by pnamejr
to the entity pointed by the ECB pointer pevent. Similarly the function OSEventNameSGet (08 EVE N'T
*pevent, char *pname, INT8U *err) retrieves the name associated with the object pointed by the EGP'
pointer pevent into a character string pointed by pname. These functions are mainly used for associating
a name with a resource for debugging purpose.
Introduction to Embedded Systems
Event flags are another mechanism supported by MicroC/OS-II for task synchronisation. You can
create a group of event flags and can assign specific meaning to each bit present in the event flags group.
Tasks or Tasks and ISRs can modify or access these event flag bits to synchronise their execution. The
following table gives a snapshot of the important event flag related routines supported by MicroC/OS-II
for task synchronisation.
SLR. For example for setting the O'1, 3rd and 5th bit, set the ‘flags’ p
00101001) and ‘opt’ parameter as OS_FLAG__SET. If the priority ofthe
or the specified bit pattern is greater than that of the priority of the t
tvailability of the specified bit pattern, it is preempted.
OSFlag. Used by a task to wait for the arrival of a specific combination of flag bitkimthfyeyent
flag group. The ‘flags’ parameter of the function call specifies which all event flag l)ite
to be checked and the parameter ‘opt’ specifies the type'of event bit checking. .It pan
be any one of the following type. Y Jjf >
1. OS_FLAG_WAlT_CLR_ALL : Check all specified bits in the flags is clear (0)
2. OS_FLAGJVAIT_CLR_ANY: Check any specified bit in the flags is clear(dj JP
3. OS_FLAG_WAIT_SET_ALL : Check all specified bits in the flags is set '(l|y §
4. OS_FLAG_WAIT_SET_ANY: Check any specified bit in the flags is set (1)
The flag OS_FLAG_CONSUME can be combined with the above flag to clear the bits
of the flag group when the specified condition occurs. For example a task can use
OS FLAG WAIT JET ANYA OS FLAG CONSUME to wait for the setting of any
specified flag bit and the flag bits are automatically reset (cleared) when the condition!
occurs. If the priority of the task waiting for the specified bit pattern is greater than
that of the priority of the task signalled the availability of the specified bit pattern, it
is preempted. If the event flag bits satisfy the conditions specified by the task, the task
is readied. If the event flag bits don’t satisfy the conditions specified by the task, at
the time of calling this function, the calling task is blocked and it is put in the event
flag wait list queue associated with the event flags group. A task can wait until the
specified flag pattern arrives or till a timeout occurs. The timeout value for waiting
can be specified in the ‘timeout’ argument of this function call. The ‘timeout’ value is
specified in terms of clock ticks. It can take values ranging from 0 to 65535. A value
of 0 indicates wait until the specified event flag condition occurs.
OSFlagAcceptQ .Used for checking the occurrence of a specified flag bit pattern in the e vent flag-group.
. Similar to OSFlagPendQ in functioning but differ in the way it operates. It'allows "a
h task to check whether the specified bit pattern is available..Unlike OSFlagPendQ, the
calling task is not blocked (‘Pended’) if the specified flag event is not occufrefir T -T
f Please refer to the MicroC library reference manual for the parameter details of each function call
An Introduction to Embedded System Design with VxWorks and MicroC/OS-IIRTOS
MV
sum
OSFlagPendGetFlagsRdyQ Returns the flag value that made the task ready from blocked state.
OSFlagQneryO Retrieves the current value of the event flags in the event flags group. The function
returns the state of the flag bits in the event group.
OSFlagNameSet() Associates a name with the specified event flag group. The name is passed as a
character pointer. This function is mainly used for associating a name with a resource
for debugging purpose.
OSFlagNameGet() Retrieves the name associated with the specified event flag group: The name is retrieved
in a character pointer, which is passed as argument to this function This function is
mainly used for associating a name with a resource for debugging purpose.
OSFlagDelQ Deletes an event flags group. Option for specifying whether the event-flags group
needs to be deleted immediately or only when the pending operations on this event
flags group are over. Proper care should be applied in the usage of this function. There
are possibilities for tasks to access a deleted event flags group. This may result in
unpredicted behaviour.
#include <includes.h>
tdefine TASK_1_PRI0 12
fdefine TASK_2_PRI0 11
(void *)0,
STaskFirstStk[TASK_STK_SIZE - 1],
\ TASK_FIRST_PRIO,
\ TASK_FIRST_ID,
&TaskStartStk[0] ,
TASK_STK_SIZE,
(void *) 0,
.. - • < OS_TASK_OPT_STK_CHK) ;
,// Start,, multitasking „ - . _.. ’
OSStart () ; '' , , : V--*
//’Create Task2
OSTaskCreateExt(Task2,
(void *)0,
&Task2Stk[TASK_STK_SIZE - 1],
TASK_2_PRIO,
TASK_2_ID,
&Task2Stk[0] ,
TASK_STK_SIZE,
(void *) 0,
OS_TASK_OPT_STK_CHK) ;
while (1)
{
//Do Nothing. Simply sleep for one timer tick
OSTimeDly(OS_TICKS_PER_SEC);
}
}
I
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
‘/ ^■*:* * '*' *''*' *jk & */* * * * * ★ *'“* * * ■* ^c;*‘*‘* ★j^***/* •*•*■•***
//Implementation of Task2:;// /., - '‘ytTSs I///.., ''/| \ " . ,/
void Task2(vcid *args) : } "~c ip./' , ’ -
The sample application creates a main task TciskFirst with priority 10, which creates an event flag
group StatusFlags and two tasks namely Taskl (with priority 12) and Task2 (with priority 11). Task2
Introduction to Embedded Systems
requires the signalling of two flags to continue its execution. At some point of execution Task2 waits for
the occurring of any one of the specified event. Taskl sets the flags at some point of its execution. Since
the priority of Task2 is higher than that of Taskl, Task2 preempts Taskl when the signalling of event flag
occurs. Task2 also prints the event flag bits value which made Task2 ‘Ready’ for execution.
The flag based task execution synchronisation is the best choice for synchronising the execution of
cooperating tasks which requires the occurring of a particular sequence of operation for completion. A
typical example is a ‘Smart Card Reader’ interfaced with a PC. The card reading operation can be con¬
trolled from an application running on the PC. Obviously there are two tasks running in the Smart Card
Reader Device. Task 1, ‘The Communication task’, checks the serial port or USB port to receive the
commands from PC. The second task ‘The card Reading task’ waits until a command is received from
the PC, to continue its operation. The ‘Communication task’ can inform the ‘Card reading task’ that a
command is received, by setting a flag in the event flags group.
,f Please refer to the MicroC library reference manual for the parameter details of each function call.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-Il RTOS
Block 1
Block!;
Block!
Block 2
IBll Block
OSMemNameSet(OS_MEM *pmem, Associates a name with the specified memory partition. The name is
char *pname, INT8U*eir) passed as a character pointer pname. This function is mainly used for
associating a name with a resource for debugging purpose, siJ-
' OSMemNameGSet(OS_MEM *pmem, Retrieves the name associated with the specified memor^^ifilfifibn.
- char *pname, INT8U *m) The name is retrieved in a character pointer pname, which is passed as
argument to this function. This function is mainly used for .associating
a name with a resource for debugging purpose. *> ?* * 1
Introduction to Embedded Systems
W/Implementation of Interrupt
'void interrupt xyzlnterrup:. (int vector) ■ • . ■
{ } ; ... y .< '
The keyword interrupt identifies a function as Interrupt Service Routine (ISR). The parameter vector
specifies the interrupt vector address corresponding to the ISR. It should be noted that an ISR neither
takes and nor returns any variables. All CPU registers are saved (PUSHed) to the stack of the task which
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
is interrupted by the ISR. However certain processor architecture like Motorola 68030 requires a sepa¬
rate stack for ISR.
Summary
_
•A VxWorks is a POSIX compliant popular hard real time, multitasking operating system from Wind River Systems.'^
The kernel of VxWorks is popularly known as wind A
S Under VxWorks kernel, the tasks may be in one of the primary states ‘READY’, ‘PEND’, ‘DELAY’, ‘SUSPEND’
or a specific combination of it at any given point of time A... . T • ; '
V VxWorks supports priority levels from 0 to 255, with 0 being the highest priority, for tasks. VxWorks’ scheduler
implements the pre-emptive priority based scheduling of tasks with Round Robin scheduling for priority
resolution among equal priority tasks .
V The kernel functionalities of ‘VxWorks’ is also implemented as tasks and they are known as system tasks. The '
root task tUsrRoot is the first task executed by the scheduler. The function usrRootO is the entry point to this
task - " ' f ‘ • . A,-A ? ASlAlPl
V VxWorks implements Inter Task communication through shared memory, message queues, pipes, sockets,
Remote Procedure Call (RPC) and signals ’ '
S' VxWorks’ kernel follows a single linear address space for tasks. The data can be shared in the form of global
variable, linear buffer, linked list and ring buffer . • '• ! A '
S VxWorks implements mutual exclusion policies in three different ways namely; Task Locking (Disabling task
preemption), Disabling Interrupts, Locking resource with semaphores
S VxWorks supports three types of semaphores namely; Binary semaphore, counting semaphore and mutual
exclusion semaphore
S Under VxWorks kernel, Interrupt Service Routines (ISRs) run at a special context outside any task’s context. A
separate stack is maintained for use by all ISRs
S The watchdog timer interface provided by VxWorks kernel can be utilised for monitoring task execution. The
watchdog timeout is handled as part of the system clock ISR. User can associate a ‘C’ function with the watchdog
timeout handler
S Wind River Workbench is the development environment from Wind River Systems for VxWorks development
S MicroC/OS-II is a portable, ROMable, scalable, preemptive, real-time, multitasking kernel from Micrium Inc
S MicroC/OS-II follows the task based execution model and each task is implemented as infinite loop. Under
MicroC/OS-II kernel, the tasks may be in one of the states Ready, Running, Waiting, Interrupted at a given point
of time
S MicroC/OS-II supports tasks up to 64. Out of this 8 are reserved for the MicroC/OS-II kernel. Each task will have
a unique task priority assigned. No two tasks can have the same priority
Introduction to Embedded Systems
S The OS kernel initialisation function OSInitQ is the first task executed by MicroC kernel. It initialises the different
OS kernel data structure and creates the Idle task OSIdleQ. The Idle task is the lowest priority task under the".
MicroC/OS-II kernel and it is executed to keep the CPU always busy when there are no user tasks to execute, yy
V MicroC/OS-II executes a statistical task with priority OSjLOWESTJPRIO - 1 in every second to capture the .
percentage of CPU usage. , *.
C The MicroC/OS-II supports preemptive priority based scheduling. Each task is assigned a unique priority ranging
from 0 to 63 with 0 being the highest priority level. The four highest priorities (0 to 3) and the four lowest
priorities (63 to OS_LO\VEST PRIO 3) are reserved for the kernel - j4
V The MicroC/OS-II kernel starts executing the first available highest priority task and a task re-scheduling happens !
whenever a task enters the ‘Waiting’ state due to the invocation of system calls, and whenever an Interrupt
occurs, Ihe tasks, are rescheduled upon return from the Interrupt Service Routine,
V The' MicrbC/OS-II kernel supports Inter Task Communication through mailbox and message queues. *'*•$«*
V The MicroC/OS-II kernel implements mutual exclusion through disabling task scheduling, disabling interrupts,
using semaphores and events • ^ |l||p
V MicroC/OS-II supports two different types of semaphores, namely, counting semaphore and mutual exclusion/-,
semaphore (mutex) ■ -• ^ v. -7 ' ^ **
V Under MicroC/OS-II, the ‘ClockTick’ acts as the time source for providing timing reference for time delays and
timeouts.‘Clock Tick’generates periodic interrupts - " : ' ,5
V Under MicroC/OS-II, each interrupt is assigned an Interrupt Request number (IRQ). The actions corresponding ■
to an interrupt is implemented in a function ~ called Interrupt Service Routine (ISR). The IRQ links to the
corresponding ISR
S The Micro-IDE is an example for 3rd party development tool for MicroC/OS-II
VxWorks : Apopular hard real-time, multitasking operating system from Wind River Systems ‘ UfCJ
wind : The kernel of VxWorks
Task Scheduling : The act of allocating the CPU to different tasks
Kernel : A set of services which forms the heart of an operating system
Message Queue : An inter-task communication mechanism used in VxWorks. It comprises queue for sending
and receiving messages
Pipe : A message queue based IPC mechanism under VxWorks
Socket : The mechanism utilised by VxWorks to communicate between processes running on different
targets which are networked and for executing a procedure (function) which is part of another
process running on the same CPU or on a different CPU within the network
Signals : A form of software event notification based Inter Process Mechanism supported by VxWorks
Mutual Exclusion : A binary semaphore implementation under VxWorks kernel, which implements mechanisms
Semaphore to prevent priority inversion
Watchdog Timer : A special timer implementation for monitoring the task execution time and triggering
precautionary actions in case the execution exceeds the allowed time
Tornado® II : The VxWorks development environment from Wind River Systems
MicroC/OS-II : A portable, ROMable, scalable, preemptive, real-time, multitasking kernel from Micrium Inc
Mailbox : An IPC mechanism supported by MicroC/OS-II kernel, for exchanging a single message
between two tasks or between an ISR and a task
An Introduction to Embedded System Design with VxWorks and MicroC/OS-Il RTOS
Objective Questions
1. Explain ‘task’ in ‘VxWorks’, context? Explain the different possible states of a task under VxWorks kernel.
2. Explain the state transition under VxWorks kernel with a state transition diagram. Give an example for the scenarios
. for each state transitions.
An Introduction to Embedded System Design with VxWorks and MicroC/OS-II RTOS
Lab Assignments
1. Write a VxWorks multitasking application to create two tasks as,;per the following requirements
(a) The stack size for both the tasks are 2000
(b) Priority for both the tasks are 100
(c) Task 1 prints the message “Hello from Task 1 ” continuously with a delay of 500 timer ticks between successive
printing > •'
(d) Task-2 prints the message “Hello from Task 2” continuously with adelay of 500 timer ticks between successive
’ j <- 'printing fr. c- ■ r—;*- , £•• y ~r v. '
2. Write a VxWorks multitasking application to retrieve and change the POSIX priority assigned to the tasks as per
the foliowing requirements^ ; ' ; . * r/W- v*»v.‘ .
(a) Create two tasks with names “Task!” and “Task2” respectively. The stack size for both the tasks are 2000
-.••• (b) Priority for both the tasks are initially set at 100 " 4 .t f;
(c) Task 1 is spawned first and then Task2 -r"' " ’ '• “ * V'T ‘
(d) Taskl retrieves its priority and prints it on the console and then changes it to current priority-1,‘ if the chrregt
priority is an even number, else changes to pridrity+1 “
(e) Task2 retrieves its priority and prints it on the console and then changes it to current priority-1, if the current
priority is an even nuihber,’ else changes to priority+1 • : - '
(fj Use Round Robin scheduling with time slice 20 timer tick for priority' resolution
3. An embedded application involves reading data from an Analog to Digital Converter. The ADC indicates the
availability of data by asserting the interrupt line which is connected to the processor of the system. The interrupt
vector for the external intenupt line to which the ADC is interfaced is 0x0003. The ISR for the external interrupt
reads the data and stores it in a Shared buffer of size 512 bytes. The received data is processed by a task. The task
reads the data and prints it on the console. The communication between the ISR and task is synchronised using a
binary semaphore. The task waits for the availability of the binary semaphore and the ISR signals the availability
of the binary semaphore when an interrupt occurs and the data is read from the device! Write an application fo
implement this requirement under VxWorks kernel
4. Re-write the message queue example given under the Inter Task Communication section of VxWorks for posting
the message as a high priority message
5. Create a POSIX based message queue under VxWorks for communicating between two tasks as per the requirements
given below
(a) Use a named message queue with name “MyQueue” .
(b) Create two tasks (Task! and Task2) with stack size 4000 and priorities 99 and 100 respectively
(c) Taskl creates the specified message queue as Read Write and reads the message present, if any, from the
message queue and prints it on the console
(d) Task2 opens the message queue anji posts the message “Hi From Task2”
(e) Handle all possible error scenarios appropriately
6. Implement the above requirements with ‘notification’ based POSIX message queue (Hint: Use signal for handling
the notification)
7. Write a VxWorks application as per the requirements given below
(a) Three tasks with names Taskl, Task2 and Task3 with priorities 100, 99 and 98 respectively are created with
stack size 2000
(b) Taskl^ checks the availability of a binary semaphore ‘binarySem’ and if it is available, it prints the message
“Hi from Taskl” 50 times and then releases the binary semaphore and completes its execution
(c) Task2 prints the message “Hi from Task2” 50 times and completes its execution
(d) Task3 checks the availability of a binary semaphore ‘binarySem’ and if it is available, it prints the message
“Hi from Task2” 50 times and then releases the binary semaphore and completes its execution
-mm
An Introduction to Embedded System Design with VxWorks and MicroC/OS-Il RTOS
(e) The tasks are spawned in the order Taskl, Task2 and Task3
(f) Handle all possible error scenarios appropriately
■ I.I r ~
message "jqj from Taskl” (Assume that !*® fssBSm jn §§| has a MicroC librarv
1
9. Write a complete MicroC program for synchronising the access of an integer variable ‘Var’ shared between two
tasks using‘disabling task scheduling technique’as per the following requirements
t: (a) Create two tasks with names Taskl and Task2 with stack.size 1024 and priorities 9 apdTO respectively
■ , ..(b) Taskl increments the shared variable and prints its value with the message “From Taskl : Var- ” (Assume
: (hat the. compiler in use has a MicroC library implementation for the printfQ function tailored for the target
processor port) and sleeps for 20 Timer Ticks : . .
(cj Task2 decrements the shared variable and prints its value with the message “From Task2 : Var = ” (Assume
that .the compiler in use has a MicroC library implementation for: the print i'O function tailored for the target
processor port) and sleep - for 20 Timer Ticks . • -;(is s? ?
(d) The tasks are created in the orderTaskl, Task) 1 . • . : : ■ -I ' •
TO! An embedded system involves , the reception of serial data through a serial port and displaying the current time
. in HH:MM:SS format on the first line of a 2 line 16 character display and indicating serial data reception on the
- second line of the LCD. Design the system based on MicroC/OS-11 as per the requirements given below v
• (a) Taskl is created for polling the serial receive interrupt flag of the processor (Assume that MicroC
' . y . j implementation provides a variable called RI for indicating the status. It is set on the reception of a data bvtc).
Taskl is created with stack size 512 bytes and priority 9
• '■ (b) Upon receiving serial data, Taskl displays the message “Data Received - ” and appends the, received data
byte to the display - •• Jdx ^ y V TV/ i,-. ,y„ , . !
(c) Task2 is created for reading the time from a MicroC/OS-II 'time'.structure,'^igii^asBjaned-to(be^ipdated
every second by the system .timer interrupt, and displaying the time on the first line of the LCD in HH;MM:
SS format. The Display should be centralised to the first line with packing with blank characters on both
sides . ' ; (“ HILMMiSS ”)
(d) Task2 is created with stack size 512 bytes and priority 10 .
(e) It is assumed that the function for initialising the LCD, LCDInitO is available. You can use a dummy function
call LCDInit() to indicate the initialisation of the LCD. Similarly a dummy function LCDWrite(x, y, char) is
also available for displaying character on the LCD at position x,y. x = 0 for line 1 and 1 for line 2. y varies
from 0 to 15
(f) Task3 displays the message “HAVE A GREAT DAY” on the second line of the LCD if there is no data
reception activity in progress.: Task3 is created with stack size 512 bytes and priority 11
(g) Each task should contain self-delaying for a period of 5 timer ticks to give another task a chance to execute
(h) When the system starts up, the current time is displayed on the first line and the message “HAVE A GREAT
DAY” is displayed on the second line of the LCD
(i) The learning objective of this application is the usage of semaphore under MicroC/OS-II kernel for enforcing
mutual exclusion in shared resource access
I
Integration and Testing of Embedded
Hardware and Firmware
LEARNING OBJECTIVES
^ f \ ^ ^ ^ ^ y . Tv■" ^ » ( t"1 ? $•'*.**’* ^ ^f Xyt'iPv $ fr~-% ' * /* tT - ' s a ^ < •'*
•/ Learn about the different techniques for embedding firmw are into hardware ' : - ; •, ;
•K - Learn about the steps involved in the Out of system Programming
S Learn about the In System Programming (ISP) jo; firmware embedding
S. Learn about the In Application Programming (IAP)) for dieting an area of the program storage memory for updating
•=■ configuration data, tables, etc. x ' ': \^ ’ < ‘ *.
v Learn the technique used for embedding Gs image and applications into the program storage memory of an embed¬
ded device ' . - . .
^ Know the various things to be taken care.in the 'board bring up'' .
Integration testing of the embedded hardware and firmware is the immediate step following the embed¬
ded hardware and firmware development. Embedded hardware and firmware are developed in various
steps as described in the earlier chapters. The final embedded hardware constitute of a PCB with all
necessary components affixed to it as per the original schematic diagram. Embedded firmware repre¬
sents the control algorithm and configuration data necessary to implement the product requirements on
the product. Embedded firmware will be in a target processor/controller understandable format called
machine language (sequence of Is and Os-Binary). The target embedded hardware without embedding
the firmware is a dumb device and cannot function properly. If you power up the hardware without
embedding the firmware, the device may behave in an unpredicted manner. As described in the earlier
chapters, both embedded hardware and firmware should be independently tested (Unit Tested) to ensure
their proper functioning. Functioning of individual hardware sections can be done by writing small utili¬
ties which checks the operation of the specified part. As far as the embedded firmware is concerned, its
targeted functionalities can easily be checked by the simulator environment provided by the embedded
firmware development tool’s IDE. By simulating the firmware, the memory contents, register details,
status of various flags and registers can easily be monitored and it gives an approximate picture of
“What happens inside the processor/controller and what are the states of various peripherals” when the
firmware is running on the target hardware. The IDE gives necessary support for simulating the various
inputs required from the external world, like inputting data on ports, generating an interrupt condition,
Integration and Testing of Embedded Hardware and Firmware
etc. This really helps in debugging the functioning of the firmware without dumping the firmware in a
real target board.
12.1
«v.k-'v v-,' .
INTEGRATION OF HARDWARE AND FIRMWARE
? j- , ' ■* *"*' f fit"*# *> * ~ t o v A- , Wt
Integration of hardware and firmware deals with the embedding of firmware into the target hardware
board. It is the process of ‘Embedding Intelligence' to the product. The embedded processors/control¬
lers used in the target board may or may not have built in code memory. For non-operating system based
embedded products, if the processor/controller contains internal memory and the total size of the firm¬
ware is fitting into the code memory area, the code memory is downloaded into the target controller/pro-'
cessor. If the processor/controller does not support built in code memory or the size of the firmware is
exceeding the memory size supported by the target processor/controller, an external dedicated EPROM/
FLASH memory chip is used for holding the firmware. This chip is interfaced to the processor/control¬
ler. (The type of firmware storage, either processor storage or external storage is decided at the time of
hardware design by taking the firmware complexity into consideration). A variety of techniques are used
for embedding the firmware into the target board. The commonly used firmware embedding techniques
for a non-OS based embedded system are explained below. The non-OS based embedded systems store
the firmware either in the onchip processor/controller memory or offchip memory (FLASH/NVRAM,
etc.).
RS-232/USB cable
resolves this issue to certain extent. A gang programmer is similar to an ordinary programmer except
that it contains multiple ZIF sockets (4 to 8) and capable of programming multiple devices at a time.
But it is bit expensive compared to an ordinary programmer. Another big drawback of this programming
technique is that once the product is deployed in the market in a production environment, it is very dif¬
ficult to upgrade the firmware.
The out-of-system programming technique is used for firmware integration for low end embedded
products which runs without an operating system. Out-of-circuit programming is commonly used for
development of low volume products and Proof of Concept (PoC) product Development.
12.1.2.1 In System Programming with SPI Protocol Devices with SPI In System Programming
support contains a built-in SPI interface and the on-chip EEPROM or FLASH memory is programmed
through this interface. The primary I/O lines involved in SPI - In System Programming are listed be¬
low.
MO SI - Master Out Slave In
MISO - Master In Slave Out
SCK-System Clock
RST - Reset of Target Device
GND - Ground of Target Device
PC acts as the master and target device acts as the slave in ISP. The program data is sent to the MOSI
pin of target device and the device acknowledgement is originated from the MISO pin of the device.
SCK pin acts as the clock for data transfer. A utility program can be developed on the PC side to generate
the above signal lines. Since the target device works under a supply voltage less than 5V (TTL/CMOS),
it is better to connect these lines of the target device with the parallel port of the PC. Since Parallel port
operations are also at 5V logic, no need for any other intermediate hardware for signal conversion.
The pins of parallel port to which the ISP pins of device needs to be connected are dependent on the
program, which is used for generating these signals, or you can fix these lines first and then write the
program according to the pin inter-connection assignments. Standard SPI-ISP utilities are feely avail¬
able on the internet and there is no need for going for writing own program. What you need to do is just
connect the pins as mentioned by the program requirement. As mentioned earlier, for ISP operations, the
Introduction to Embedded Systems
target device needs to be powered up in a pre-defined sequence. The power up sequence for In System
Programming for Atmel’s AT89S series microcontroller family is listed below.
1. Apply supply voltage between VCC and GND pins of target chip.
2. Set RST pin to “HIGH” state.
3. If a crystal is not connected across pins XTAL1 and XTAL2, apply a 3 MHz to 24 MHz clock to
XTAL1 pin and wait for at least 10 milliseconds.
4. Enable serial programming by sending the Programming Enable serial instruction to pin MOSI/
PI.5. The frequency of the shift clock supplied at pin SCK/P1.7 needs to be less than the CPU
clock at XTAL1 divided by 40.
5. The Code or Data array is programmed one byte at a time by supplying the address and data to¬
gether with the appropriate Write instruction. The selected memory location is first erased before
the new data is written. The write cycle is self-timed and typically takes less than 2.5 ms at 5 V.
6. Any memory location can be verified by using the Read instruction, which returns the content at
the selected address at serial output MISO/P1.6.
7. After successfully programming the device, set RST pin low or turn off the chip power supply and
turn it ON to commence the normal operation.
and performing a call to the common entry point. Like any other subroutine call, after completion of
the function, control will return to the end-user’s code. The Boot ROM is shadowed with the user code
memory in its address range. This shadowing is controlled by a status bit. When this status bit is set,
accesses to the internal code memory in this address range will be from the Boot ROM. When cleared,
accesses will be from the user’s code memory. Hence the user should set the status bit prior to calling the
common entry point for IAP operations (The LAP technique described here is for PHILIPS’ 89C51RX
series microcontroller. Though the underlying principle in IAP is the same, the shadowing technique
used for switching access between Boot ROM and code memory may be different for other family of.
devices).
Now the firmware is embedded into the target board using one of the programming techniques described
above. ‘What Next?’ The answer is power up the board. You may be expecting the device functioning
exactly in a way as you designed. But in real scenario it need not be and if the board functions well in
the first attempt itself you are very lucky. Sometimes the first power up may end up in a messy explosion
leaving the smell of burned components behind. It may happen due to various reasons, like Proper care
was not taken in applying the power and power applied in reverse polarity (+ve of supply connected to
Introduction to Embedded Systems
-ve of the target board and vice versa), components were not placed in the correct polarity order (E.g.
a capacitor on the target board is connected to the board with +ve terminal to -ve of the board and vice
versa), etc... etc ...I would like to share a very interesting incident which happened in my development
career during the power up of a new board. Though the board was well checked and extreme care was
taken in applying the power, when I powered the board after embedding the firmware, 1 heard an explo¬
sion followed by the burning of a capacitor on the target board and I really struggled hard to stop the
fire from spreading by a strong puff of air—It is not a joke, It’s a true incident. The reason was very
simple, the power applied to the board was °V and the filter capacitor placed at the regulator IC input
side was with a rating of 6V (220MFD/6A Since the capacitor voltage rating was below the input
supply, the dielectric of capacitor got burned. Be' cautious: Before you power up the board make sure
everything is intact. We will discuss about the various tools used for troubleshooting the target hardware
in a later chapter.
Summary
l _
S Integration of hardware add firmware deals with the embedding of firmware into the target hardware board.
A For non-operating system based embedded products, if the processor/controller contains internal memory and
the total size of the firmware is fitting into the code memory area, the code memory is downloaded into the target
controller/processor. If the processor/controller does not support built-in code memory or the size of the firmware
is exceeding the memory size supported by the target processor/contfoller, the firmware is held in an external
dedicated EPROM/FLASH memory chip' • “ v r,y~ J ” ;!-' v y .
S Out-of-circuit programming and In System Programming (ISP) are the two different methods for embedding
firmware into a non Operating System based product A - \ .
v' In the out-of-circuit programming, the device is removed from the target board and is programmed using a
" ‘Device Programmer’, whereas in the In System Programming technique, the firmware is embedded into the
controller memoty/prograip.memory chip without removing the chip from the target board
A JTACr, SPI, etc. are the commonly used serial protocols for transferring the embedded firmware into the target
device
A In Application Programming (IAP) is a technique used by the firmware running on the target device for modifying
a selected portion of the code memory. It is not a technique for first time embedding of user written firmware. It
modifies the program code memory under the control of the embedded application
A IAP is normally used for updating calibration data, look-up tables, etc. which are stored in code memory
A Factory programmed chip embeds firmware into the chip at the time of chip fabrication
A In System Programming (ISP) is used for embedding the OS impge and application program into the non-volatile
storage memory of the embedded product. The bootloader program running in the target device implements the
necessary routine for embedding the OS image into the non-volatile memory and booting the system
W1 Keywords
JTAG An Interface for hardware troubleshooting like boundary, scan testing
In System Programming (ISP) Firmware embedding technique in which the firmware ikembedded into the
program memory without removing the chip from: the target board
Serial Peripheral Interface (SPI) Serial interface for connecting devices
Integration and Testing of Embedded Hardware and Firmware
Review Questions
1. Explain the different techniques for embedding the firmware into the target board for a non-OS based embedded
system
2. Explain the major drawbacks of out-of-circuit programming
3. Explain the firmware embedding process for OS based embedded products
4. What is the difference between In System Programming (ISP) and In Application Programming (IAP)?
■V
- '
ff:M ' -:
LEARNING OBJECTIVES
S Learn about the different entities of the embedded system development environment w-A
S Learn about the Integrated Development Environments (IDEs) for embedded firmware development and debugging
V Learn the usage of p'/ision 3 IDE from Kell software (www.keil.com) tor embedded firmware development, simulation
and debugging for 8051 family of microcontrollers
/ Familiarise with the different IDEs for firmware development for different family of processors/controllers and
Embedded Operating Systems ■ -
/ learn about the different types of files (List File, Preprocessor output file, Object File, Map File, Hex File, etc.) gen¬
erated during the cross compilation of a source file written in high level language like Embedded C and during the
cross assembling of a source file written in Assembly language f ■>
S learn about disassembler and decompiler, and their role in embedded, firmware development
S Learn.about Simulators, In Circuit Emulators (ICE), and Debuggers and their role in embedded firmware debugging
S Learn about the different tools and techniques used for embedded hardware debugging
S Learn the use of magnifying glass, multimeter, CRO, logic analyser and function generator in embedded hardware
debugging
S Learn about the boundary scanning technique for testing the interconnection among the various chips in a complex
hardware board
This chapter is designed to give you an insight into the embedded-system development environment.
The various tools used and the various steps followed in embedded system development are explained
here. It is a summary of the various design and development phases we discussed so far and an intro¬
duction to the various design/development/debug tools employed in embedded system development. A
typical embedded system development environment is illustrated in Fig. 13.1.
As illustrated in the figure, the development environment consists of a Development Computer (PC)
or Host, which acts as the heart of the development environment, Integrated Development Environ¬
ment (IDE) Tool for embedded firmware development and debugging, Electronic Design Automation
(EDA) Tool for Embedded Hardware design, An emulator hardware for debugging the target board,
Signal sources (like Function generator) for simulating the inputs to the target board, Target hardware
The Embedded System Development Environment
PCB fabrication
files
debugging tools (Digital CRO, Multimeter, Logic Analyser, etc.) and the target hardware. The Inte¬
grated Development Environment (IDE) and Electronic Design Automation (EDA) tools are selected
based on the target hardware development requirement and they are supplied as Installable files in CDs
by vendors. These tools need to be installed on the host PC used for development activities. These tools
can be either freeware or licensed copy or evaluation versions. Licensed versions of the tools are fully
featured and folly functional whereas trial versions fall into two categories, tools with limited features,
and foil featured copies with limited period of usage.
cross platform development and compiler for same platform development)’, ‘Linker’ and a ‘Debugger’.
Some IDEs may provide interface to target board emulators, Target processor’s/controller’s Flash mem¬
ory programmer, etc. and incorporate other software development utilities like ‘Version Control Tool’,
‘Help File for the Development Language’, etc. IDEs can be either command line based or GUI based.
Command line based IDEs may include little or less GUI support. The old version of TURBO C IDE for
developing applications in C/C++ for x86 processor on Windows platform is an example for a generic
IDE with command line interface. GUI based IDEs provide a Visual Development Environment with
mouse click support for each action. Such IDEs are generally known as Visual IDEs. Visual IDEs are
very helpful in firmware development. A typical example for a Visual IDE is Microsoft® Visual Studio
for developing Visual C++ and Visual Basic programs. Other examples are NetBeans and Eclipse.
IDEs used in embedded firmware development are slightly different from the generic IDEs used
for high level language based development for desktop applications. In Embedded Applications, the
IDE is either supplied by the target processor/controller manufacturer or by third party vendors or as
Open Source. MPLAB is an IDE tool supplied by microchip for developing embedded firmware using
their PIC family of microcontrollers. Keil pVision3 (spelt as micro vision three) from Keil software is
an example for a third party IDE, which is used for developing embedded firmware for 8051 family
microcontrollers. CodeWarrior by Metrowerks is an example of IDE for ARM family of processors.
It should be noted that in embedded firmware development applications each IDE is designed for a
specific family of controllers/processors and it may not be possible to develop firmware for all family
of controllers/processors using a single IDE (as of now there is no known IDE with support for all fam¬
ily of processors/controllers). However there is a rapid move happening towards the open source IDE,
Eclipse for embedded development. Most of the proccessor/control manufacturers and third party IDE
providers are trying to build the IDE around the popular Eclipse open source IDE. This may lead to a
single IDE based on Eclipse for embedded system development in the near future. Since this book is
primarily focusing on 8051 based embedded firmware development, the IDE chosen for demonstration
is Keil (iVision3. A demo version of the tool for Microsoft Windows OS based development is avail¬
able for free download from the Keil Software website. Please instal the same on your machine before
proceeding to the next sections.
K? pVision3
Output Window
Analog Devices
0-^ AnchoiChips
Cancel
Vendor: - /ttmet
Device: AT89C51 1“ Use Extended linker (LX51) instead of B!
Cancel
Once the target processor is selected, the IDE automatically adds the required startup code for the
firmware and it prompts you whether the standard startup code needs to be added to the project (Fig.
13.5). Press ‘Yes’ to proceed. The startup code contains the required default initialisation like stack
pointer setting and initialisation, memory clearing, etc. On cross-compiling, the code generated for
the startup file is placed on top of the code generated for the function mainQ. Hence the reset vector
jjyisipn3
(OOOOH) always starts with the execution of startup code before the main code. For more details on
the contents and code of startup file please go through the pVision help files which is listed under the
‘Books'1 section of the project workspace window.
A ‘Target’ group with the name ‘ Target! ’ is automatically generated mider the ‘Files’ section of the
project Window. ‘TargetP contains a ‘Source Group’ with the name ‘Source GroupP and the startup
file (STARTUP.A51) is kept under this group (Fig. 13.6). All these groups are generated automatically.
If you want you can rename these groups by clicking the respective group names.
Ef sample - jjVislor; 3
B'-jSJ Target 1
(EJ'-gg 'S&ce|nwip^;;
i.[2 STARTUP.A51
You can see that similar to the Visual Studio IDE’s ‘Project Window’ for VC++ development, Keil
IDE’s ‘Project Window’ also contains multiple tabs. They are the ‘Files’ tab, which gives the file details
for the current project, ‘Regs’ tab, giving the Register details while debugging the source code, ‘Books’
tab showing all the available help and documentation files supplied by the IDE, ‘Functions’ tab lists out
the functions present in a ‘C’ source file and finally a ‘Templates’ tab which generate automatic code
framework (function body) for if, if else, switch case etc and code documentation template (Header).
These steps create a project workspace. To start with the firmware development we need to create a
source file and then add that source file to the ‘Source Group’ of the ‘Target’. Click on the ‘File’ tab on
the menu tool of the IDE and select the option ‘New’. A blank text editor will be visible on the IDE to
the right of the ‘Project Window’. Start writing the code on the text editor as per your design (Refer to
Introduction to Embedded Systems
the Keil help file for using Keil supported specific Embedded C instructions for 8051 family). You can
write the program in ANSI C and 8051 specific codes (like Port Access, bit manipulation instruction
etc) using Keil specific Embedded C codes. For using the Keil specific Embedded code, you need to add
the specific header file to the text editor using the Mnclude compiler directive. For example, #include
<reg51.h> is the header file including all the target processor specific declarations for 8051 family pro¬
cessors for Keil C51 Compiler. Standard ‘C’ programs (Desktop applications) calls the library routines
for accessing the I/O and they are defined in the stdio.h file, whereas these library files cannot be used as
such for embedded application development since the I/O medium is not a graphic console as in the C
language based development on DOS Operating system and they are re-defined for the target processor
I/Os for the cross compilers by the cross compiler developer. If you open the stdio.h file by ANSI C and
Keil for its IDE, you can find that the implementation of I/O related functions (e.g.printf()) are entirely
different. •
The difference in the implementation is explained with typical stdio.h function-printfQ (e.g.
printf(“Hello World\n ”)). With ANSI C & DOS the function outputs the string Hello World to the DOS
console whereas with the C51 cross-compiler, the same function outputs the string Hello World to the
serial port of the device with the default settings. Coming back to the firmware development, let’s follow
the universal unwritten law of first ‘C’ program- The “Hello World' program. Write a simple ‘C’ code to
print the string Hello world.
i iB.ni jHai^SiWPal
The code is written in the text editor which appears within the IDE on selecting the ‘New’ tab from
the ‘File’ Menu. Write the code in C language syntax (Fig. 13.7). Add the necessary header files. You
can make use of the standard template files available under the ‘ Templates’ tab of the ‘Project Window’
for adding functions, loops, conditional instmctions, etc. for writing the code. Once you are done with
the code, save it with extension .c in a desired folder (Preferably in the current project directory). Let the
name of the file be ‘sample.c’. At the moment you save the program with extension .c, all the keywords
(like #include, int, void, etc.) appear in a different colour and the title bar of the IDE displays the name
of the current .c file along with its path. By now we have created a ‘c’ source file. Next step is adding the
created source file to the project. For this, right click on the ‘Source Group’ from the ‘Project Window’'
and select the option ‘AddFiles to Group ‘Source Group”. Choose the file ‘sample.c’ from the file se¬
lection Dialog Box and press 1Add’ button and exit the file selection dialog by pressing ‘Close’ button.
Now the file is added to the target project (Fig. 13.8). You can see the file in the ‘Project Window’ under
‘Files’ tab beneath the ‘Source Group’.
If you are following the modular programming technique, you may have different source files for
performing an intended operation. Add all those files to the project as described above. It should be
noted that function mainQ is the only entry point and only one ‘.c’ file among the files added to the
project is allowed to contain the function mainQ. If more than one file contains a function with the name
mainQ, compilation will end up in error. The next step is configuring the target. To configure the target,
Introduction to Embedded Systems
go to ‘Project’ tab on the Menu and select ‘Options for Target’. The configuration window as shown in
Fig. 13.9 is displayed.
Device Target | Output J listing j | $151 Locate j 8L51 Misc ] Debug j Utilities ]
Defaults
Target Configuration
The target configuration window is a tabbed dialog box. The device is already configured at the time
of creating a new project by selecting the target device (e.g. Atmel AT89C51). If you want to check it,
select the ‘Device’ tab and verify the same. Select ‘Target’ tab and configure the following. Give the
clock frequency for which the system is designed, e.g. 6MHz, 11.0592MHz, 12MHz, 24MHz, etc. This
has nothing to do with the firmware creation but it is veiy essential while debugging the firmware to note
the execution time since execution time is dependent on the clock frequency. If the system is designed
to make use of the processor resident code memory, select the option Use On-chip ROM (For AT89C51
On-chip ROM is 4K only; 0x0000 to OxOFFF). If external code memory is used, enter the start address
and size of the code memory at the Off-chip Code memory column (e.g. Eprom Start: 0x0000 and Size
OxOFFF). The working memory (data memory or RAM) can also be either internal or external to the
processor. If it is external, enter the memory map starting address of the external memory along with
the size of external memory in the ‘Off-chip Xdata memory section (e.g. Ram Start: 0x8000 and Size Ox
1000). Select the memory model for the target. Memory model refers to the data memory. Keil supports
three types of data memory model; internal data memory (Small), external data memory in paged mode
(Compact) and external data memory in non-paged mode (Large). Now select the Code memory size.
The Embedded System Development Environment
Code memory model is also classified into three; namely, Small (code less than 2K bytes), Compact (2K
bytes functions and 64K bytes code memory) and Large (Plain 64K bytes memory). Choose the type
depending on your target application and target hardware design. If your design is for an RTOS based
system, select the supported RTOS by the IDE. Keil supports two 8 bit RTOS namely, RTX51 Tiny and
RTX51 Full. Choose none for a non RTOS based design.
Move to the next Tab, ‘Output\ The output tab holds the settings for output file generation from the
source code (Fig. 13.10). The source file can either be converted into an executable machine code or a
library file.
'•KJX
(♦ Create Executable:
; F Debug Infoimatii
Mer Make --
r Run User-Program#!:
Cancel Defaults
You can select one of the output settings (viz. executable binary file (hex) or library file (lib)). For
executable file, tick the ‘Create Hex File’ option and select the target processor specific hex file format.
Depending on the target processor architecture the hex file format may vary, e.g. Intel Hex file and
Motorola hex file. For 8051, only one choice is available and it is Intel hex File HEX-80. The list files
section coming under the tab ‘Listing’ tells what all listing files should be created during cross-compila¬
tion process (Fig. 13.11).
‘C57’ tab settings are used for cross compiler directives and settings like/Pre-processor symbols,
code optimisation settings, type of optimisation (viz. code optimised for execution speed and code
optimised for size), include file’s path settings, etc. The lA5V tab settings are used for assembler direc-
Introduction to Embedded Systems
tives and settings like conditional assembly control symbols, include file’s path settings etc. Another
important option is ‘‘Debug'. The ‘Debug' tab is used for configuring the firmware debugging. ‘Debug'
supports both simulation type firmware debugging and debugging the application while it is running on
the target hardware (Fig. 13.12).
You can either select the Simulator based firmware debugging or a target firmware level debugging
from the ‘Debug' option. If the Simulator is selected, the firmware need not be downloaded into the
target machine. The IDE provides an application firmware debugging environment by simulating the
target hardware in a software environment. It is most suitable for offline analysis and rapid code devel¬
opments. If target level debugging is selected, the binary file created by the cross-compilation process
needs to be downloaded into the target hardware and the debugging is done by single stepping the
firmware, A physical link should be established between the target hardware and the PC on which the
IDE is running for target level debugging. Target level hardware debugging is achieved using the Keil
supported monitor programs or through an emulator interface. Select the same from the Drop-down list.
Normally the link between target hardware and IDE is established through a Serial interface. Use the
settings tab to configure the Serial interface. Select the ‘Comm Port' to which the target device is con¬
nected and the baudrate for communication (Fig. 13.13).
If the Debug mode is configured to use the Target level debugging using any one of the monitor
program or the emulator interface supported by the Keil IDE, the created binary file is downloaded into
The Embedded System Development Environment
Device j Target j Output | Listing j C51 j A51 j BL51 Locate j BL51 Wise W>ug j Ulities |
'(• Use Simulator Settings F Use: | Keil ISD51 In-System Debugger .t| Settings •
r Lrnit Speed to Real-Time
-|Keil ISD51 in-System Debugger
P Load AppLckion at Startup. P Run to mainQ P Load MON3SO: Dallas Contiguous Mode l r&M
,1 3,31 :C~2
Infineon XC800 ULlNK Driver
AD! Monitor Driver
Infineon DAS Client for XC800
J isCj
f Restore-Debug Session Settings -Restore uwog —;—
1
1 - /-K;Toolbox P Breakpoints • P Toolbox A _
cleaned j Help : J
-Cache Options-
Restore Debug Sessi| F Cache DATA (SFR) F Cache XDATA
17 Watchpoints 8
-Stop Program Execution with —
17 Memory Display!
V Serial Interrupt
I i
Dialog DLL: Paral
OK Cancel
the target board using the configured serial connection and the firmware execution occurs in real time.
The firmware is single stepped (Executing instruction-by-instruction) within the target processor and
the monitor program running on the target device reflects the various register and memory contents in
the IDE using the serial interface.
The ‘ Utilities' tab is used for configuring the flash memory programming of the target processor/con¬
trollers from the Keil IDE (Fig. 13.14). You can use either Keil IDE supported programming drivers or
a third party tool for programming the target system’s FLASH memory. For making use of Keil IDE
provided flash memory programming drivers, select the option ' Use Target Driver for Flash Program¬
ming’ and choose a driver from the drop-down list. To use third party programming tools, select the
option ‘ Use External Tool for Flash Programming' and specify the third party tool to be used by giving
the path in the ‘Command' column and specify the arguments (if any) in the ‘Arguments' tab to invoke
the third party application.
Arguments':
With this we are done with the writing of our first simple Embedded C program and configuring the
target controller for running it. The next step is the conversion of the firmware written in Embedded C
to machine language corresponding to the target processor/controller. This step is called cross-compila¬
tion. Go to ‘Project' tab in the menu and select ‘Rebuild all target files'. This cross-compiles all the files
within the target group (for modular programs there may be multiple source files) and link the object
codes created by each file to generate the final binary. The output of cross-compilation for the “Hello
World" application is given in Fig. 13.15.
The Embedded System Development Environment
You can see the cross-compilation step & linking in the o/p window along with cross-compilation er¬
ror history. Now perform a 'Build Target’ operation. This links all the object files created (in a multi-file
system where each source files are cross-compiled separately) (Fig. 13.16).
X
Build target 'Target 1' i
5?
compiling sample.c... * _
linking. . . Hfj
■AL:
In a multi source file project (source group containing multiple .c files) each file can be separately
cross-compiled by selecting the ‘ Translate current file' option. This is known as Selective Compila¬
tion. Remember this generates the object file for the current file only and it needs to be combined with
object files generated for other files, by using ‘Build Target’ option for creating the final executable
(Fig. 13.17). Selective compilation is very helpful in a multifile project where a modified file only needs
to be re-compiled and it saves the time in re-compiling all the files present in the target group.
compiling sample.c...
sample.c - 0 Error(s), 0 Warning(s).
Introduction to Embedded Systems
See the difference between the three; selective compilation cross-compiles a selected source file and
creates a re-locatable object file corresponding to it, whereas ‘Build Target’ performs the linking of
different re-locatable object files and generates an absolute object file and finally creates a binary file.
‘Rebuild all target file’ creates re-locatable object files by cross-compiling all source files and links all
object files to generate the absolute object file and finally generates the binary file. If there is any error
in the compilation, it is displayed on the output window along with the line number of code where the
error is occurred. So it is easy to trace the code to find out the error part in the code. The error is listed
out in the output window along with line number and error description. On clicking the error description
at the output window, the line of code generated the error is highlighted with a bold arrow. One example
of error code is given below. In the “Hello World” example, the include file is commented and the code
while compiling will generate the error indicating print/function is not defined. Have a look at the same
(Fig. 13.18).
t:5C:l
13.1.1.1 Debugging with Keil fjVision3 IDE Debugging firmware is the process of monitoring
the program flow, various registers and memory contents while the firmware is executed. You can de¬
bug the firmware in two methods. The first method is by using breakpoints and simulator (a software
tool which simulates the functionalities of the target processor). The second method is hardware level
debugging.
The Embedded System Development Environment
For simulator-based debugging, select the option ‘ Use Simulator’ from the ‘Debug’ tab of the
‘ Options for Target’ as illustrated in a previous section on debug. Insert a ‘ Breakpoint’ in the code line
of the source program where debugging needs to be started. Breakpoints can be inserted by right click¬
ing on the desired source code line and by selecting the ‘Insert/Remove Breakpoint' option. It can also
be inserted by using the ‘Debug’ tab present in the menu of the IDE (Not the debug tab of Options for
Target Pop-up dialog). It toggles the breakpoint (if the breakpoint is already inserted, it is removed and
if not a breakpoint is added). The breakpoint breaks the firmware execution at the selected point and
further execution requires user interaction. After a break, the code can be executed by single stepping or
by a complete run again. For the above ‘Hello World’ example, we are going to debug the firmware at
the source code lineprintf and a breakpoint is inserted as shown in Fig. 13.19.
mm
mm
ifpip
□•••<§§ Source Group i
|.[|| STARTUP.A51
EE~ © sample.c
Q2l H}
If compiling sample.c...
fy linking . . .
| Program Size: da.ta = 30.1 xdata=0 code = 1078
1 creating hex file from "sample"...
5 "sample" - 0 Error(s), 0 Warning(s).
The ‘breakpoint’ is distinguishable with a special visible mark. All debug related actions are grouped
within the ‘Debug’ tab of the IDE menu. Each debug instruction has a hot key (e.g. Ctrl+F5 for start and
stop of debugging) associated with it. Identify the hotkey and use it or use mouse to activate the debug
related action each time from the debug menu. To start debugging, select the option ‘Start/Stop Debug
Session (Ctrl+F5)’ from the ‘Debug’ menu. If you set the option ‘Go till mainQ’ on the ‘Debug’ tab of
Introduction to Embedded Systems
‘Options for Targetthe application runs till the code memory where the main() function starts. If this
option is not selected, the execution breaks at the Reset vector (0x0000) itself. Both of these options
are shown below. Note that while debugging, the project window tab automatically selects the ‘Regs'
tab and shows the various register contents in this window when the program is at the break stage. You
can switch the project window in between the ‘Files' section and "Regs' section to find out the changes
happening to various registers on executing each line of code (Fig. 13.20).
| Project Workspace
3: i nt main(void)
C:0x0C2A 7BFF MOV R3,#0xFF
C:0x0C2C 7A0C MOV .R2,#0x0C
C:0x0C2E 7911 MOV Rl,#0xll
C: 0x0C30 020862 LJMP PRINTF(C:086
C: 0x0C33 00 NOP'
C:0x0C34 00 NOP
C: 0x0C35 00 NOP
"U ->7
jg4 Disassembly
If you observe these two figures you can see that code memory execution starts at location 0x0000
and the firmware corresponding to the function main is located at 0x0C2A and it is not the first execut¬
able code. Before executing main () some other code placed at location OxOClE (jump from the reset
vector to location OxOClE) is executed. The code at this memory location is the code memory generated
for the startup file. Startup file code is always executed before entering the function main. It should be
noted that the code memory location mentioned here for function main is not always fixed. It varies
depending on the changes made to the startup file. From this breakpoint you can go to the breakpoint
you set in the code memory either by single stepping (‘Step' command (FII)) or by a run to the next
breakpoint (‘Go' command (F5)).
The Embedded System Development Environment
By default, the text editor window in debug mode contains two tabs, namely Source code view tab
and Disassembly tab. The Source code and corresponding Disassembly view is shown in Fig. 13.21.
While debugging the firmware you can switch the view between Assembly code and original source
code lines by selecting the corresponding tab. This switches the view between the original source code
and the corresponding Assembly code for it. The Disassembly view disappears temporarily if you click
the ‘Disassembly Window’ option under the ‘ View’ tab of the IDE menu. To enable Disassembly View
click again on the ‘Disassembly Window’ option under the ‘ View’ tab of the IDE menu (Fig. 13.22).
During debugging the content of various CPU and general purpose registers are displayed under the
‘Regs’ tab of the project Window. Apart from this you can inspect the data memory and code memory
by invoking the ‘Memoiy Window’ under the ‘ View’ tab of the IDE menu (Fig. 13.23).
For viewing the Data memory, use the Prefix ‘D:’ before the address and type it at the Address edit
box and press enter (For example, for viewing the data memory starting from 0x00, type D:0x00 at the
Address box and press enter). You can edit the content of the desired data memory by right clicking
on the current contents pointed by the address or just by a double click on the current content. Code
memory can also be inspected and modified in a similar way to the Data memory. The only difference
is instead of D: use ‘C:’ (D stands for Data and C stands for Code) (Fig. 13.24).
Similar to other Desktop application development IDEs, this also provides option for viewing local
variables and call stack details. Invoke ‘ Watch & Call Stack Window’ from the 'View’ tab of the IDE
menu (Fig. 13.25).
Introduction to Embedded Systems
Address: D:0x00
D: 0x00 IT
Decimal
D: 0x07 0
•I Unsigned
D: OxOE ■
D: 0x15 Signed
n • Qt, i n n
MM
Asa!
1 Float
Double
The local variables can be viewed in the ‘Locals’ section of ‘ Watch and call Stack Window’ whereas
users can add other variables to the ‘Watch’ window to get their yalues. ‘Call Stack’ gives the ‘Callee-
Caller’ details for subroutine calls. Invoking ‘Symbol Window’ urider the ‘View’ tab of IDE menu while
debugging, displays the list of ‘Publics’, ‘Locals’ and ‘Lines’ used by the application, in the disassembly
The Embedded System Development Environment
window (Assembly code). Selecting ‘Publics' shows the different variables and functions present in
the Assembly code along with their ‘Address’, ‘Name’ and ‘Type’. Address can either be Data memory
address or Code memory address. ‘Type’ indicates whether it is a variable or a function. The different
variables supported are ‘unsigned char (uchar)’, ‘signed char (chary, bit, unsigned integer (uint), signed
int (inf), etc. Figure 13.26 shows the Symbols Window displaying the various symbols used in Assem¬
bly for our sample application.
Symbols
Same
D:0x08 ?_PRINTF5177BYTE
D:0x08 ?_PRINTF?BYTE
D:0x08 ?_SPRINTF5177BYTE
D:0x08 ? SPRINTF7BYTE
D: OxE 0
OxDO.G
D:0xE0 uchar
D:0xF0 uchar
C:0x03C7 C7CCASE function
C:0x0378 C7CLD0PTR function
C:0x035F C7CLDPTR function
C:0x03A5 C7CSTPTR function
rmmR7 ropi nnnATA ft iriohnn
Activating the "Code Coverage’ option under the "View' tab of the IDE menu when the execution
is at halted stage, gives the details of instructions in each function/module and how many of them are
executed till execution break. Figure 13.27 illustrates the same.
13.1.1.2 Simulating Peripherals and Interrupts with Keil pVision3 IDE Embedded systems
are designed to interact with real world and the actions performed by them may depend on the inputs
from various sensors connected to the processor/controller of the embedded system. By a mere software
simulation of the firmware, we can only inspect the memory, register contents, etc. of the processor and
cannot infer anything on the real-time performance of the system since the inputs provided by the sen¬
sors are real-time and dynamic. To a certain extent, we can simulate these inputs using the simulator
support provided by the IDE. Keil provides extensive support for simulating various peripherals like;
ports, memory mapped I/O, etc. and Interrupts (External, Timer and Serial interrupts). The main limita¬
tion of simulation is that we can simulate it with only known values (in a real application the simulated
value may not be the real input) and also it is difficult to predict the real-time behaviour of the system
Introduction to Embedded Systems
based on these simulations. In our “Hello World’ application, if we debug the application by placing a
breakpoint at the ‘printf function, in the source code window you can observe that the ‘print/ function
is not getting completed on single stepping and it gives you the feeling that the firmware is hang up
somewhere. If you are single stepping (using Fll) the firmware from the beginning, in the disassembly
window you can see that for the code corresponding to printf, the application is looping inside another
function called ‘putchar’, which checks a bit TI (Transmit Interrupt) and loops till it is asserted high
(Fig. 13.28). As we mentioned earlier, the printf function is outputting the data to the Serial port. As
long as the Transmit Interrupt is not asserted, the firmware control is looped there and it generates an
application hang-up behaviour. If you are not able to view the point where the firmware loops in the
Disassembly window, invoke ‘Stop Running’ from ‘Debug’ tab of IDE menu. The firmware execution
will be stopped and in the disassembly window you can see the exact point where the application was
looping.
The Embedded System Development Environment
We can simulate the Transmit Interrupt ‘ TV using the simulator support provided by Keil. If you have
already stopped the execution, continue the code execution by invoking ‘Go’ option from ‘Debug1 tab
or by pressing ‘F5’ key. Now select ‘Interrupt' option from the ‘Peripherals’ tab of the IDE menu. The
interrupt simulation window will pop-up. Select the ‘Serial Xmif interrupt and enable the Global Inter¬
rupt Enable (EA) as well as Transmit Interrupt (77) (Fig. 13.29).-
1 P3.2/lnt0 0003H 0 0 0 0 if
I Timer 0 000BH 0 0 0 1
1 P3.3/lnt1 0013H 0 0 0 0
1 I'mer I 001BH 0 0 0
| Serial Rev. 0023H 0 • 0 0
s SerialXmit. 0023H if k o 011
. Selected Interrupt——
•v v.*pt-S/P: iXn -V -
You can simulate any other interrupt listed in the interrupt system in a similar fashion. On enabling
TI, firmware execution comes out of the infinite loop and dumps the string “Hello World” to the serial
port. The output of ‘Hello World’ application is shown in Fig. 13.30. You can capture whatever data go¬
ing through the serial port while simulation. For this invoke ‘Serial Window #1’ from the ‘View’ tab of
the IDE menu.
**»•***?*
pt
5 I printf ( "Hello WorId\n"
Si >
£ Disassembly -mix
3: int main(void)
C:0x0C2A 7BFF MOV R3„#0xFF
Serial#!
To simulate the Ports and their status, select '1/O-Ports' from the ‘Peripherals' tab of IDE menu
(Fig. 13.31).
The first value (E.g. PO: OxFF) simulates the contents of Port 0 Special Function Register. In order to
make the port pins as input pins the port pin’s corresponding SFR bit should be .set as 1. With the SFR
bit set to 1, the state of the corresponding port pin can be changed to either logic 1 or logic 0. To output
logic 0 to a port pin, clear the corresponding SFR bit and for outputting logic 1, set the corresponding
port bit in the SFR bit. In the above example, if Port 0 is viewed as an output port, the port will be out-
putting all Is. If it is treated as an input port, the value inputted will be OxOF (whichlslhe state of the
port pins). Serial Port and Timers can also be simulated by selecting the respective commands from the
‘Peripherals’ tab of the IDE menu. It is left for the readers for experimenting. The 'Reset CPlf option
available under the 4Peripherals' tab is used for resetting the firmware execution. Resetting the CPU
while debugging brings the program execution to the reset vector (0x0000).
| Ji Projsd WoHcspats: C ~ - |[
HgTOxOROQ 020C1E . LJlvt? ' C:0C1E
B~iHprget 1 pC: 0x0003 00 NOP
S-^§3 Source Group 1 EC: 0x0004 00 NOP
[1] STARTUP.A51 KG: 0x0005 00 NOP
A- [Tj sample.c Be:0x0006 00 NOP
||C:0x0007 00 NOP
IpC:0x0008 00 NOP
SC:0x0009 00 NOP
jc-.OxOOOA 00 NOP
||C:OxOOOB 00 NOP
jfc:OxOOOC 00 NOP
/
81 ■
dfci 1
.. — J
1 n to. *{} i ^ |=| sample.c Disassembly ^ Serial #1
~ t ' “
Generating precise delays in software for super loop based embedded applications are often quite
difficult and there are no standard functions available for generating precise delays. If the ‘C’ program
is running on DOS platform, there is a standard function ‘delay()’ available which generates delay with
multiples of millisecond. There are no such standard functions available for generating delays in non-op¬
erating system based embedded programming. The only way of generating delays is writing a loop and
set its parameters according to the clock frequency used. Often we need to do a trial and error method to
finetune the parameters depending on the crystal used. The ‘Performance analyser’ support by the Keil
IDE helps in calculating the time consumed by a function. To activate this, select1 Performance Anaty^ —
ser...’ from the ‘Debug’ tab of the IDE menu while debugging is on, before entering the function whose
execution time needs to be calculated. A Performance analyser set up for selected function is shown in
Fig. .13.33,
The various functions available on the current module is listed on the right side of the ‘Setup Per¬
formance analyser window under the ‘Function symbolssection. Double clicking on the function of
interest displays the same on the ‘Define Performance Analyser Range:’ Click the ‘Define’ button to add
it to the analyser. Invoke the ‘Performance analyser window’ from the ‘ View’ tab of the IDE menu. A
new tab with name ‘Performance...’ will be added to the text editor window. Now execute the function
whose execution time is to be analysed by single step (Fll) or by giving a step Over (F10) command.
Select the tab ‘Performance...’ and click on the function name for which the analysis is perfomied.
The time taken for executing the function is displayed at the ‘total time’ display window. This gives a
rough estimate on the execution time of the function (Fig. 13.34). Note that in addition to this time, the
time taken for calling this function also needs to be taken into account. Moreover, all these calculations
are based on the target clock frequency set on the target options. We will discuss the same for a new
function for generating milliseconds delay added to our program ‘Hello World’ application. The main
Introduction to Embedded Systems
function calls the delay routine delay_ms() before executing the print/function. To verify that this func¬
tion is taking 1 millisecond time to finish execution with parameter 1, add the function to the perfor¬
mance-analyser as mentioned above. Invoke the performance analyser and execute the function. Switch
view to ‘Performance... ’ tab and observe the execution time. The complete source code for including
delay is given below.
' -..lA-ncliide Kstdi-o. h-> • . ‘ • , ... * • •
int main(void) ‘ ’ ■ 7,
I ^ . :
//Calling 1 milli second delay
delay ms (1); ’ •
printf("Hello World\n");
1 . '
/./###################################################################
//'Function for generating milli seconds delay
//Assumes clock frequency=24MHz
i
void delayjms (int n)
M-- . .
...int j,. k;
for (j-C; j<r.; j++) • -
:;j;. "v’ ■ • - ■ « . J
for (k=C; k<247; k++);
It should be noted that generating highly precise delay is very difficult and there may be a tolerance
of +/- fraction of millisecond^ in the delay. Also the delay is purely dependent on the system clock
The Embedded System Development Environment
frequency. In a real world scenario, the stability of the clock is expressed in terms of +/- some parts per
million (ppm) of the fundamental frequency. Any change in the target clock frequency needs the re-tun¬
ing of the code to bring it back to the desired tolerance level. Precise time delay can be generated using
the hardware timer unit of the microcontroller.
13.1.1.3 Target Level Firmware Debugging with Keil fjVision3 IDE Simulation based de¬
bugging technique lacks real-time behaviour and various input conditions needs to be simulated using
the IDE support for debugging the firmware. Target level hardware debugging involves debugging the
firmware which is embedded in the target board. This technique is also known as In Circuit -Debug¬
ging/ Emulation (ICD/E). For performing target level debugging with Keil, the target board should be
connected to the development machine through a serial interface. The IDE debug settings should be
modified to activate target level debugging. Select the ‘Debug’ option from ‘ Options for Target’ and
change ‘Use Simulator’ to ‘'Used selected debug support from a list of available target debug supports
(Fig. 13.35).
Select the target supported debug option from the drivers available on the drop-down list and config¬
ure the serial link properties using the ‘Settings’ tab. Keil supports two types of target level debugging,
namely ‘Monitor program based debugging’ and ‘Emulator based debugging’. For Monitor program
based debugging, the target processor should have Keil IDE supported monitor program installed and
the target hardware should be configured to run the monitor program. Debug instructions from the IDE
communicates with the monitor program running on the target processor and it returns the debug infor¬
mation to the IDE using the configured serial link. For Emulator based debugging, Keil IDE acts as an
Introduction to Embedded Systems
interface between a Reil supported third party Emulator hardware and the target board. The Serial link
should be connected to the emulator hardware and other end of the emulator hardware should be inter¬
faced to the target hardware using any ICE supported interfaces like JTAG, BDM or pin-to-pin socket
(For JTAG/BDM interface, target processor/controller should have this support at processor/controller
side. If not, pin-to-pin socket is used)/
13.1.1.4 Writing ISR in Embedded C using the pVision3 IDE The C51 cross-compiler from
Keil supports the implementation of Interrupt Service Routines (ISRs) in Embedded C. The general
form of writing an Interrupt Service Routine in C for C51 cross-compiler is illustrated below.
: :Void 'ISR'_Name \ (void) interrupt'yJNTR_.NQ- 'using .REGyBANK
IIIIS
-TV/Body of ISR
I 1 * I
The attribute interrupt informs the cross compiler that the function with given name (Here ISR
Name) is an Interrupt Service Routine. The attribute INTR_NO indicates the interrupt number for an
interrupt. It is essential for placing the ISR generated assembly code at the Interrupt Vector for the cor¬
responding interrupt. Keil supports 32 ISRs for interrupt numbers 0 to 31. The interrupt number 0 cor¬
responds to External Interrupt 0,1 corresponds to Timer 0 Interrupt, 2 correspond^ to External Interrupt
1, 3 corresponds to Timer 1 Interrupt, 4 corresponds to Serial Interrupt and so on. The keyword using
specifies the Register bank mentioned in the attribute REG_BANK for the registers R0 to R7 inside the
ISR. It is an optional attribute and if it is not mentioned in the ISR implementation, the current register
bank in use by the controller is used by the ISR for the registers R0 to R7. The value of REG_BANK can
vary from 0 to 3, representing register banks 0 to 3. With the attribute interrupt, the C51 cross compiler
automatically generates the interrupt vector and entry and exit code for the specified interrupt routine.
The contents of the Accumulator, B register, DPH, DPL, and PSW are Pushed to the stack as and when
required, at the time of entering the ISR and are retrieved at the time of leaving the ISR. If the register
bank is not specified in the ISR implementation, all the working registers which are modified inside the
ISR is pushed to the stack and poped while returning from ISR. It should be noted that the ISR neither
takes any parameter nor return any. The following piece of code illustrates the implementation of the
Serial Interrupt ISR for 8051 using C51 cross compiler.
^include <reg51.h> //Keil C51 Header file
//ISR for handling Serial Interrupts. Interrupt number = 4
//Interrupt Vector at 0023H. Use Register Bank 1
13.1.1.5 Assembly Based Firmware Development with pVision3 IDE Apart from Embedded
C based development, Keil IDE also supports Assembly language based firmware development. The
steps involved in creating a project for writing assembly instructions are same as that of Embedded C
based development. The only difference is that there is no need to include the standard startup file at
the time of creating a project. Select the option ‘No’ to the query ‘Copy standard 8051 startup Code to
Project Folder and add File to Project?' while creating a new project (Refer to Sectiojp. 13.1.1 for details
on creating a new project). Since we are not using the IDE supplied startup code, thd^ctions performed
by startup code (mainly initialisation of stack pointer), should be written by the programmer before the
start of the main program in the assembly file. Create a new file and start writing AsseniSly instructions
in the file. Save the file with extension \src’ and add the file to the ‘Source Group’ of the target as il¬
lustrated in embedded C based development (Section 13.1.1). A program written in Assembly Language
corresponding to the “Hello World” program written in Embedded C is illustrated below.
ORG ‘ Q0C0K~
,JMP MAIN . ;'j ■ - '
ORG , 0C03H ; External Interrupt 0 .Handler
„ 11 - Reti ' ■ ’
QRG V . .§•
000BH
" ; T.imerO Interrupt Handler
*• ? 3 * \ . - RET-l - ’ " ' : i
ORG u ■GC13H ; External 'Interrupt 1 -Handler-'
RETI
■ORG. - 001BK ; Timerl Incorrupt Handler . -
''RETI (..
ORG 0023H ; Serial Interrupt Handler
, RETI
ORG 0100H
MAIN: MOV SP, #50H ; Initialise stack pointer
; #########I####################################### ### #######.#########
; 8051 Serial Routine Parameter settings
MOV SCON, #'50h ; Configure Serial Communication
f Register.
MOV TMOD, #21h ; Baud Generation Settings
MOV TH1, #0FDH ; Re-Load Value for TIMER 1 in Auto
; Re-Load
MOV TLl, #0FDH ; 9600 baud
MOV A, PCON
ANL A, #7FH
MOV PCON, A
SETB TR1 ; Start Timerl
;###########.#########################################################
OUT_TEXT: CALL ,TEXT_OUT
JMP OUT_TEXT
;####################################################################
; Text -outputting Routine
; Same as printf 0 in Embedded C
Introduction to Embedded Systems
HELLO WORLD
'Hello World\
END
Compile this source code using the same compile options illustrated for Embedded C based develop¬
ment. Here also you can have multiple source (.sre) files. Add all source files to the ‘Source Group’ and
compile the program. The Assembly program is written to store the string “Hello World” in the code
memory and it is retrieved from the code memory for sending to the serial port. The string can also be
storedrin the data memory and retrieve it from there for sending to the serial port. In that case the storage
memory for the string “Hello World’ (12 bytes with string termination character ‘V) in the code memory
will be released and the same will occupy in the data memory. Figure 13.36 shows the assembling of
source code written in assembly language.
If you observe the output window while compiling, you can see that the source code is ‘assembled
instead of ‘compiling’. Why this strange thing happens? - The answer is conversion of program writ¬
ten in assembly language to object code is carried out by the utility ‘Assembler’. Assembler is same as
compiler in functioning but the input is different. You can debug this application usinp the simulator in
a similar way as that of debugging the ‘C’ source code. On debugging the assembly code you can view
the output on the ‘Serial Window’ and it is exactly the same as the output produced by the “Hello World’
Application written in Embedded C.
Now let’s have a comparison between the application written in Embedded C and Assembly Lan¬
guage for outputting the string “Hello World” to the Serial port. Read carefully.
Ql. How many lines of code you wrote for the program “Hello World”?
Embedded C: Only a single line of Code excluding the framework of main
Assembly: More than 25 lines
Q2. How many registers of the target processor you are familiar with for writing the “Hello
World” program?
Embedded C: None
Assembly: Almost all
The Embedded System Development Environment
[=1 Rle Edit View 'Y&6]ect Debugy Flash -'Peripherals Tools SVCSVWridow: 'Hdp
« ^s'JTarget 1
Project Workspace v x (j" »;0i
•'."ni ;f;i ■<* a-i.*~Ta
A’. XJL&.
;•••» _ ;— _ _ -..! | Ul > ?rr? rrrr f<rrf?T
0§ Target 1 1. 1 402
v 02 ; 5 car 3 of Ms in Program
szsrz
C»fet ;iSi **#-£#*$****#;
:'\04';yORG OOOOH
LJMP MAIN
•jg-OS-:: ORG 0003H
Cfa,> f iCx terns. interrupt: G Handler
-• RETI
'■ J
4P3 ORG 0003.4
iiiD ; ^
§ lamplejasm I
E sample_asm - pVisfon3
Rle Edit ;:Vjew ^Project Debug . Rash Peripherals :£oote -..SVCS ..-'Window..;:Hep
!F Target 1
j £3 Source Group I
Bj sample_asm.sr
13.3?) Debugging the Source code written in Assembly Language for outputting the text “Hello World" to
serial port
Introduction to Embedded Systems
Q3. How many Assembly Instructions of the target processor you are familiar with for writing
the “Hello World” program?
Embedded C: None
Assembly: Almost all
Q4. What is the code size for the “Hello World” program written in?
Embedded C: 1078 bytes
Assembly: 321 bytes
Q5. What is the data memory size for the “Hello World” Program written in?
Embedded C: 30 bytes + 1 bit
Assembly: 8 bytes Minimum and 21 Bytes Maximum (If the “Hello World” string is stored in Data
memory)
1 Summary
1. Compared to Assembly programming, Embedded C requires lesser number of lines of code to
implement a task
2. Embedded C programmers require less or little knowledge on the internals of the target processor
whereas Assembly programmers require thorough knowledge of the internals of the processor
3. Embedded C programmers need not be aware of the instruction set of the target processor whereas
Assembly language programmers should be well versed in the same.
4. The Code memory usage by programs written in Assembly is optimal compared to the one written
in Embedded C
5. The Data memory usage by programs written in Assembly is optimal compared to the one written
in Embedded C
13.1.1.6 Flash Memory Programming with Keil gVision3 IDE Once the firmware starts func¬
tioning properly, after the modifications following debug and simulation, the next step is embedding the
| firmware into the target processor/controller. As mentioned in a previous chapter, firmware can be em-
j bedded using various techniques like Out of System Programming, In System Programming (ISP), etc.
j Keil IDE provides an In System Programming (ISP) support which is selected at the time of configuring
! the ‘Options for Target’. It can be either Keil provided flash programming driver or any third party tool
■ which can be invoked from the IDE. The flash programming makes use of the serial connection estab¬
lished between the Host PC and the target board. The target processor should contain a bootloader or a
monitor program which can understand the flash memory programming related commands sent from the
IDE. Cross-compile all the source files within the module, link them and generate the binary code using
‘Rebuild all target files' option. Download the generated binary file to the target processor’s/controller’s
memory by invoking ‘Flash Download’ option from the ‘Project’ tab of the IDE menu. The target board
should be powered and interfaced with the host PC where the IDE is running and the connection should
be configured properly.
13.1.1.7 Summary of usage of Keil (jVision3 IDE So far we discussed about IDE and how IDE
is helpful in Embedded application development and debugging. Our discussion was focusing only on
8051 family 8bit microcontroller firmware application development using Keil pVision3 IDE. Though
the discussion was specific to Kefi IDE, I believe it was capable of givingyou the basic fundas of IDEs
and how the IDE is used in embedded system development. The hot khy usage, GUI details and func¬
tions offered by different IDEs are totally different. Illustrating all IDEs is out' of scope of this book and
also there is nb common IDE supporting multiple family devices that can b^ used for illustrating the
same. Users are requested to go through the respective manuals of the IDE thpy are Rising for firmware
development. Also, IDEs undergo rapid changes by adding new features, functions, etc. and whatever
The Embedded System Development Environment
we discussed here need not be the same after three months or six months or one year, in terms of fea¬
tures, UI, hot keys, menu options, etc. When I started the work of this book, the IDE offered from Keil
was Keil pVision2 and at the time of finishing this work, the latest IDE available from Keil is Keil pVi-
sion3. It incorporated lots of new features and changes compared to the old IDE. Users are requested
to visit the web site wwwrkeil.com to get the latest updates on the new versions of the IDE. You can
directly download the trial version of the IDE from the above site.
'aisonhnce ■ . ■ . , ,. . . ; +y
p://www raisonance.eom/products/info/RlDE.ph.p ~~ \;u-
■,
8051' - ^ TAR Embedded Workbench
ror so5i
IAR Systems A
http://www.iar.eom/websitel/l .0.1 0/244/1/mdex.php
; "" ' y
’r
’ "
Infineon Family DAvE Infineon Technologies. For the 8.16 and 32 bit Infineon
processors/controllers. Vv
http://www.infineon.com''
Introduction to Embedded Systems
And.... The list continues. There are thousands of IDEs available in the market as either commercial or
non-commercial and as either Open source tools or proprietary tools. Listing all of them is out of the
scope of this book. The intention is to just make the readers familiar with some of the popular IDEs for
some commonly used processors/controllers and RTOSs for embedded development.
Command Line Represents the entire command line that was used for invoking the compiler.
C51 COMPILER 98.02, COMPILATION OF MODULE .SAMPLE / A;/.
■ A- ' = A A*AA' - ^ .V'-'' A \ -3' ' 'A " -A"- ' , - ■ ■
OBJECT MODULE PLACED IN sample.OBJ .
COMPILER INVOKED BY: C: \Keil\C51\BIN\C5X.S^^^®p^^^^&^^P^^lj^lwEND
'f n ~ , >V - ~ A. A "f -w ' a ^ ' '“**•*'' Y'-S' ^ aW
LCpDEl-LI ST INCLUDE SYMBOLS • ' '
Source Code The source code listing outputs the line number as well as the source code on that line.
Special cross compiler directives can be used to include or exclude the conditional codes (code in #if
blocks) in the source code listings. Apart from the source code lines, the list file will include the com¬
ments in the source file and depending on the list file generation settings the entire contents of all include
files may also be included. Special cross compiler directives can be used to include the entire contents
of the include file in the list file.
line level source
1 //Sample.c for printing Hello World!
2 //Written by xyz
3 #include <stdio.h>
1 =i /*-
2 =1 STDIO.H
3 =1
4 =1 Prototypes for standard I/O functions.
5 =1 Copyright © 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
6 =1 All rights reserved.
7 =i -*/
8 =1
9 =1 #ifiidef_STDIO_H_
_1
10 —i #define_STDIO
11 =1
12 =1 #ifndef EOF
13 =1 #define EOF -1
14 -l #endif
15 =1
16 -l #ifndefNULL
T
iL Introduction to Embedded Systems
Assembly Listing Assembly listing contains the assembly code generated by the cross compiler for
the ‘C’ source code. Assembly code generated can be excluded from the list file by using special com¬
piler directives.
Module Information The module information provides the size of initialised and un-initialised
memory areas defined by the source file
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 9
CONSTANT SIZE - 14
XDATA SIZE - —- - '
PDATASIZE
DATA SIZE
IDATASIZE = —- —- 1
BIT SIZE
END OF MODULE INFORMATION.
Warnings and Errors Warnings and Errors section of list file records the errors encountered or any
statement that may create issues in application (warnings), during cross compilation. The warning lev¬
els can be configured before cross compilation. You can ignore certain warnings, e.g. a local variable is
declared within a function and it is not used anywhere in the program. Certain warnings require prompt
attention.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ‘ERROR.(S)
List file is a very useful tool for application debugging in case of any cross compilation issues.
Page Header A header on each page of the linker listing (MAP) file which indicates the linker ver¬
sion number, date, time, and page number.
e.g. BL51 LINKER/LOCATER V3.62 02/29/2004 09:59:51 PAGE 1
Command Line Represents the entire command line that was used for invoking the linker,
e.g. BL51 BANKED LINKER/LOCATER V6.00, INVOKED BY: , L-
CPU Details Details about the target CPU and rhemory model (internal data memory, external data
memory, paged data memory, etc.) come under this category.
Input Modules This section includes the names of all object modules, and library files and modules
that are included in the linking process. This section can be checked for ensuring all the required mod¬
ules are lined in the linking process
e.g.
mi „ , V * .a- t / t
STP. • ’ ■ . ' /
Memory Map Memory map lists the starting address, length, relocation type and name of each
segment in the program.
e-g.
TYPE BASE LENGTH RELOCATION SEGMENT NAME
Symbol Table It contains the value, type and name for all symbols from the different input
modules
Introduction to Embedded Systems
e.g.
SYMBOL TABLE OF MODULE: sample (?C_STARTUP)
MODULE ?C STARTUP
C:0C1FH SEGMENT ?C C51 STARTUP
I:0022H SEGMENT 7STACK
C:0000H PUBLIC ?C STARTUP
D:00E0H SYMBOL ACC
D:OOFOH SYMBOL B
D:0083H SYMBOL DPH
D:0082H SYMBOL DPL
Inter Module Cross Reference The cross reference listing includes the section name, memory type
and the name of the modules in which it is defined and all modules in which it is accessed.
e-g-
NAME. ....USAGE MODULE NAMES
Warnings and Errors Errors and warnings generated while linking a program are written to this
section. It is very useful in debugging link errors.'
e.g. LINK/LOCATE RUN COMPLETE. 0 WARNING (S) , 0 ERROR (S)
NB: The file extension for MAP files generated by different linkers!heaters need not be the same.
It varies across linker!locater in use. For example the map file generated for BL51 Linker/locater is
with extension .M51
Intel HEX file are corresponding to a HEX Record. Each record is made up of hexadecimal numbers that
represent machine-language code and/or constant data. Individual records are terminated with a carriage
return and a linefeed. Intel HEX file is used for transferring the program and data to a ROM or EPROM
which is used as code memory storage.
13.2.5.1 Intel HEX File Format As mentioned, Intel HEX file is composed of a number of HEX
records. Each record is made up of five fields arranged in the following format:
:llaaaattdd...cc
Each group of letters corresponds to a different field, and each letter represents a single hexadecimal
digit. Each field is composed of at least two hexadecimal digits (which make up a byte) as described
below:
An extract from the Intel hex file generated for “Hello World” application example is given below.
03000000020C1FDO
OCOC1F00787FE4F6D8FD758121020C2BD3
0E0C110048656C6C6F20576F726C64210A008E
090C2B0O7BFF7A0C7911020862CA
1(3080000E517240BF8E605172278083007027 80B65
10081000E475F001120BB4020B5C2000EB7F2ED2CA
10082000008018EF540F2490D43440D4FF30040BD0
10083000EF24BFB41A0050032461FEE518600215CD
1008400018051BE51B7002051A30070D7808E475C2
100BFA00B8130CC2983098FDA899C298B811F6306B
070C0A0099FDC299F5992242
0000000IFF
Let’s analyse the first record
1 1 a a a a t t • d d d d d d c c
0 3 0 0 0 0 0 0 0 2 0 C 1 F D 0
Introduction to Embedded Systems
: field indicates the start of a new record. 03 (//) gives the number of data bytes in the record. For this
record,1//’ is 03 and the number of data bytes in the corresponding record is 03. The start address (aaaa)
of data in the record is 0000H. The record type byte (tt) for this record is 00 and it indicates that this
record is a data record. The data for the above record is 02, 0C and IF. They are supposed to place at
three consecutive memory locations in the EEPROM with starting address 0000H. The arrangement is
given below.
If you are familiar with 8051 Machine code you can easily identify that 02 is the machine code for
the instruction LJMP and the next two bytes represent the 16bit address of the location to which the
jump is intended. Obviously the instmction is LJMP 0C1F. The last two digits (cc) of the record holds
the checksum of the values present in the record. The checksum is calculated by adding all the bytes in
the record and then taking modulo 256 of the result. The resultant is 2’s complemented and represented
as checksum in the record field. Above example it is OxDO. Intel hex files end with an end of file record
indicating the end of records in the hex file. Let’s examine the end of record structure.
End of record also starts with the start of record symbol Since End of record does not contain any
data bytes, field 7/’ will be 00. The field ‘aaaa’ is not significant since the number of data bytes are
zero. Field 7/’ will hold the value 01 to indicate that this record is an End of record. Field ‘cc’ holds the
checksum of all the bytes present in the record and it is calculated as 2’s complement of Modulo 256 of
(0 + 0 + 0 + l) = 0xFF
13.2.5.2 Motorola HEX File Format Similar to the Intel HEX file, Motorola HEX file is also an
ASCII text file where the HEX data is represented in ASCII format in lines. The lines in Motorola HEX
file represent a HEX Record. Each record is made up of hexadecimal numbers that represent machine-
language code and/or constant data. The general form of Motorola Hex record is given below.
Field Description
SOR Stands for Start of record. The ASCII Character. ‘S’ is used as the Start of Record. Every
record begins with the character‘S’
Stands for Record type. The character11’ represents the type o
There are different meanings for the record depending on the value of
0: Header. Indicates tt
1: Data Record with 1
2: Data record with 2-
9: End of File Record
The Embedded System Development Environment
I -«ngth (Iff. Stii-cl? fir i! * “• ~r. t i router i tine re\>_m. riLl' jgtMtypE'# idri~ --H
iC«iha. u^lwts. u* aianticr^i! ca'a'C'hb bwte Hit. nep^^r^ <u*w oitd
13.3 DISASSEMBLER/DECOMPILER _
Disassembler is a utility program which converts machine codes into target processor specific Assembly
codes/instractions. The process of converting machine codes into Assembly code is known as ‘Disas¬
sembling’. In operation, disassembling is complementary to assembling/cross-assembling. Decompiler
is the utility program for translating machine codes into corresponding high level language instructions.
Decompiler performs the reverse operation of compiler/cross-compiler. The disassemblers/decompilers
for different family of processors/controllers are different. Disassemblers/Decompilers are deployed in
reverse engineering. Reverse engineering is the process of revealing the technology behind the working
of a product. Reverse engineering in Embedded Product development is employed to find out the secret
behind the working of popular proprietary products. Disassemblers/decompilers help the reverse-engi¬
neering process by translating the embedded firmware into Assembly/high level language instructions.
Introduction to Embedded Systems
Disassemblers/Decompilers are powerful tools for analysing the presence of malicious codes (virus
information) in an executable image. Disassemblers/Decompilers are available as either freeware tools
readily available for free download from internet or as commercial tools. It is not possible for a disas¬
sembler/decompiler to generate an exact replica of the original assembly code/high level source code
in terms of the symbolic constants and comments used. However disassemblers/decompilers generate
a source code which is somewhat matching to the original source code from which the binary code is
generated.
13.4.1 Simulators
In a previous section of this chapter, describing the Integrated Development Environment, we discussed
about simulators for embedded firmware debugging. Simulators simulate the target hardware and the
firmware execution can be inspected using simulators. The features of simulator based debugging are
listed below.
1. Purely software based
2. Doesn’t require a real target system
3. Very primitive (Lack of featured I/O support. Everything is a simulated one)
4. Lack of Real-time behaviour
Simulate I/O Peripherals Simulator provides the option to simulate various I/O peripherals. Us¬
ing simulator’s I/O support you can edit the values for I/O registers and can be used as the input/output
value in the firmware execution. Hence it eliminates the need for connecting I/O devices for debugging
the firmware.
The Embedded System Development Environment
Simulates Abnormal Conditions With simulator’s simulation support you can input any desired
value for any parameter during debugging the firmware and can observe the control flow of firmware. It
really helps the developer in simulating abnormal operational environment for firmware and helps the
firmware developer to study the behaviour of the firmware under abnormal input conditions.
13.4.1.2 Limitations of Simulator based Debugging Though simulation based firmware de¬
bugging technique is very helpful in embedded applications, they possess certain limitations and we
cannot fully rely upon the simulator-based firmware debugging. Some of the limitations of simulator-
based debugging are explained below.
Deviation from Real Behaviour Simulation-based firmware debugging is always carried out in a
development environment where the developer may not be able to debug the firmware under all possible
combinations of input. Under certain operating conditions we may get some particular result and it need
not be the same when the-firmware runs in a production environment.
Lack of real timeliness The major limitation of simulator based debugging is that it is not real-time
in behaviour. The debugging is developer driven and it is no way capable of creating a real time behav¬
iour. Moreover in a real application the I/O condition may be varying or unpredictable. Simulation goes
for simulating those conditions for known values.
the developer had to seek the help of an expert to figure out the exact problem creator. As technology
has achieved a new dimension from the early days of embedded system development, various types of
debugging techniques are available today. The following section describes the improvements over firm¬
ware debugging starting from the most primitive type of debugging to the most sophisticated On Chip
Debugging (OCD).
13.4.2.1 Incremental EEPROM Burning Technique This is the most primitive type of firmware
debugging technique where the code is separated into different functional code units. Instead of burning
the entire code into the EEPROM chip at once, the code is burned in incremental order, where the code
corresponding to all functionalities are separately coded, cross-compiled and burned into the chip one
by one. The code will incorporate some indication support like lighting up an “LED (every embedded
product contains at least one LED). If not, you should include provision for at least one LED in the
target board at the hardware design time such that it can be used for debugging purpose)” or activate a
“BUZZER (In a system with BUZZER support)” if the code is functioning in the expected way. If the
first functionality is found working perfectly on the target board with the corresponding code burned
into the EEPRO'M, go for burning the code corresponding to the next functionality and check whether
it is working. Repeat this process till all functionalities are covered. Please ensure that before entering
into one level up, the previous level has delivered a correct result. If the code corresponding to any
functionality is found not giving the expected result, fix it by modifying the code and then only go for
adding the next functionality for burning into the EEPROM. After you found all functionalities working
properly, combine the entire source for all functionalities together, re-compile and bum the code for the
total system functioning.
Obviously it is a time-consuming process. But remember it is a onetime process and once you test
the firmware in an incremental model you can go for mass production. In incremental firmware burning
technique we are not doing any debugging but observing the status_of firmware execution as a debug
method. The very common mistake committed by firmware developers in developing non-operating
system-based embedded application is burning the entire code altogether and fed up with debugging
the code. Please don’t adopt this approach. Even though you need to spend some additional time on
incremental burning approach, you will never lose in the process and will never mess up with debug¬
ging the code. You will be able to figure out at least ‘on which point of firmware execution the issue
is arising’-“A stitch in time saves nine”. Incremental firmware burning technique is widely adopted
in small, simple system developments and in product development where time is not a big constraint
(e.g. R&D projects). It is also very useful in product development environments where no other debug
tools are available.
target board’s serial port (COM) to the development PC’s COM Port using an RS232 Cable. Power up
the target board. Depending on the execution flow of firmware and the inline debug codes inserted in the
firmware, you can view the debug information on the 'HyperTerminal’. Typical usage of inline debug
codes and the debug info retrieved on the HyperTerminal is illustrated below.
//First Inline Debug Code >
printf ("Starting Configuration...\n");
Configurations.\ if;, Stt'.' si:
, yzC&S ■’■■■ Vi- if tit
* v*• y V| ,V
If the firmware is error free and the execution occurs properly, you will get all the debug messages on
the HyperTerminal. Based on this debug info you can check the firmware for errors (Fig. 13.38).
13.4.2.3 Monitor Program Based Firmware Debugging Monitor program based firmware de¬
bugging is the first adopted invasive method for firmware debugging (Fig. 13.39). In this approach a
monitor program which acts as a supervisor is developed. The monitor program controls the download¬
ing of user code into the code memory, inspects and modifies register/memory locations; allows single
stepping of source code, etc. The monitor program implements jfie debug functions as per a pre-defined
command set from the debug application interface. The monitor program always listens to the serial port
of the target device and according to the command received from the serial interface it performs com¬
mand specific actions like firmware downloading, memoiy inspection/modification, firmware single
stepping and sends the debug information (various register and memory contents) back to the main de¬
bug program running on the development PC, etc. The first step in any monitor program development is
determining a set of commands for performing various operations like firmware downloading, memory/
register inspection/modification, single stepping, etc. Once the commands for each operation is fixed,
write the code for.performing the actions corresponding to these commands. As mentioned earlier, the
commands may be received through any of the external interface of the target processor (e.g. RS-232C
serial interface/parallel interface/USB, etc.). The monitor program should query this interface to get
commands or should handle the command reception if the data reception is implemented through inter¬
rupts. On receiving a command, examine it and perfonn the action corresponding to it. The entire code
stuff handling the command reception and corresponding action implementation is known as the “moni¬
tor program”. The most common type of interface used between target board and debug application is
RS-232C Serial interface. After the successful completion of the ‘monitor program’ development, it is
compiled and burned into the FLASH memory or ROM of the target board. The code memory contain¬
ing the monitor program is known as the ‘Monitor ROM.
Target CPU
Target board
Von-Neumann RAM
The monitor program usually resides at the reset vector (code memory 0000H) of the target proces¬
sor. The monitor program is commonly employed in development boards and the development board
supplier provides the monitor program, in the form of a ROM chip. The actual code memory is down¬
loaded into a RAM chip which is interfaced to the processor in the Von-Neumann architecture model.
The Von-Neumann architecture model is achieved by ANDing the PSEN\ and RD\ signals of the target
processor (In case of 8051) and connecting the output of AND Gate to the Output Enable . (RD\) pin
of RAM chip. WR\ signal of the target processor is interfaced to The WR\ signal of the Von Neumann
RAM. Monitor ROM size varies in the range of a few kilo bytes (Usually 4K). An address decoder
circuit maps the address range allocated to the monitor ROM and activates the Chip Select (CSV) of
the ROM if the address is within the range specified for the Monitor ROM. A user program is normally
loaded at locations0x4000 or 0x8000. The address decoder circuit ensures the enabling of the RAM'
chip (CSV) when the address range is outside that allocated to the ROM monitor. Though there are two
memory chips (Monitor ROM Chip and Von-Neumann RAM), the total memory, map available for both
of them will be 64K for a processor/controller with 16bit address space and the memory decoder ,units
take care of avoiding conflicts in accessing both. While developing user program for monitor &OM-
based systems, special care should be taken to offset the user code and handling the interrupt vectors.
The target development IDE will help in resolving this. During firmware execution and single stepping,
the user code may have to be altered and hence the firmware is always downloaded into a Von-Neumann
RAM in monitor ROM-based debugging systems. Monitor ROM-based debugging is suitable only for
development work and it is not a good choice for mass produced systems. The major drawbacks of
monitor based debugging system are
1. The entire memory map is converted into a Von-Neumann model and it is shared between the
monitor ROM, monitor program data memory, monitor program trace buffer, user written firm¬
ware and external user memory. For 8051, the original Harvard architecture supports 64K code
memory and 64K external data memory (Total 128K memory map). Going for a monitor based
debugging shrinks the total available memory to 64K Von-Neumann memory and it needs to ac¬
commodate all kinds of memory requirement (Monitor Code, monitor data, trace buffer memory,
User code and External User data memory).
2. The communication link between the debug application running on Development PC and monitor
program residing in the target system is achieved through a serial link and usually the controller’s
On-chip UART is used for establishing this link. Hence one serial port of the target processor be¬
comes dedicated for the monitor application and it cannot be used for any other device interfacing.
Wastage of a serial port! It is a serious issue in controllers or processors with single UART.
13.4.2.4 In Circuit Emulator (ICE) Based Firmware Debugging The terms ‘Simulator’ and
‘Emulator’ are little bit confusing and sounds similar. Though their basic functionality is the same
- “Debug the target firmware”, the way in which they achieve this functionality is totally different. As
mentioned before, ‘Simulator’ is a software application that precisely duplicates (mimics) the target CPU
and simulates the various features and instructions supported by the target CPU, whereas an ‘Emulator’
is a self-contained hardware device which emulates the target CPU. The emulator hardware contains
necessary emulation logic and it is hooked to the debugging application running on the development PC
on one end and connects to the target board through some interface on the other end. In summary, the
simulator ‘simulates’ the target board CPU and the emulator ‘emulates’ the target board CPU-
There is a scope change that has happened to the definition of an emulator. In olden days emulators
were defined as special hardware devices used for emulating the functionality of a processor/controller
Introduction to Embedded Systems
and performing various debug operations like halt firmware execution, set breakpoints, get or set inter¬
nal RAM/CPU register, etc. Nowadays pure software applications which perform the functioning of a
hardware emulator is also called as ‘Emulators’ (though they are ‘Simulators’ in operation). The emula¬
tor application for emulating the operation of a PDA phone for application development is an example
of a ‘Software Emulator’. A hardware emulator is controlled by a debugger application running on the
development PC. The debugger application may be part of the Integrated Development Environment
(IDE) or a third party supplied tool. Most of the IDEs incorporate debugger support for some of the
emulators commonly available in the market. The emulators for different families of processors/con¬
trollers are different. Figure 13.40 illustrates the different subsystems and interfaces of an ‘Emulator’
device.
(Fig. 13.40) In
The Emulator POD forms the heart of any emulator system and it contains the following functional
units.
Emulation Device Emulation device is a replica of the target CPU which receives various signals
from the target board through a device adaptor connected to the target board and performs the execution
of firmware under the control of debug commands from the debug application. The emulation device
can be either a standard chip same as the target processor (e.g. AT89C51) or a Programmable Logic
Device (PLD) configured to function as the target CPU. If a standard chip is used as the emulation de¬
vice, the emulation will provide real-time execution behaviour. At the same time the emulator becomes
dedicated to that particular device and cannot be re-used for the derivatives of the same chip. PLD-based
emulators can easily be re-configured to use with derivatives of the target CPU under consideration. By
simply loading the configuration file of the derivative processor/controller, the PLD gets re-configured
and it functions as the derivative device. A major drawback of PLD-based emulator is the accuracy of
replication of target CPU functionalities. PLD-based emulator logic is easy to implement for simple
target CPUs but for complex target CPUs it is quite difficult.
Emulation Memory It is the Random Access Memory (RAM) incorporated in the Emulator device.
It acts as a replacement to the target board’s EEPROM where the code is supposed to be downloaded
after each firmware modification. Hence the original EEPROM memory is emulated by the RAM of
emulator. This is known as ‘ ROM Emulation’. ROM emulation eliminates the hassles of ROM burning
and it offers the benefit of infinite number of reprogrammings (Most of the EEPROM chips available
The Embedded System Development Environment
in the market supports only 100 to 1000 re-program cycles). Emulation memory also acts as a trace
buffer in debugging. Trace buffer is a memory pool holding the instructions executed/registers modi¬
fied/related data by the processor while debugging. The trace buffer size is emulator dependent and the
trace buffer holds the recent trace information when the buffer overflows. The common features of trace
buffer memory and trace buffer data viewing are listed below:
• Trace buffer records each bus cycle in frames
• Trace data can be viewed in the debugger application as Assembly/Source code
• Trace buffering canbedone on the basis of a Trace trigger (Event)
• Trace buffer can also- record signals from target board other than CPU signals (Emulator depen¬
dent)
• Trace data is a-very useful information in firmware debugging
Emulator Control Logic Emulator control logic is the logic circuits used for implementing complex
hardware breakpoints, trace buffer trigger detection, trace buffer control, etc. Emulator control logic
circuits are also used for implementing logic analyser functions in advanced emulator devices. The
‘Emulator POD5 is connected to the target board through a ‘Device adaptor’ and signal cable.
Device Adaptors Device adaptors act as an interface between the target board and emulator POD.
Device adaptors are normally pin-to-pin compatible sockets which can be inserted/plugged into the
target board for routing the various signals from the pins assigned for the target processor. The device
adaptor is usually connected to the emulator POD using ribbon cables. The adaptor type varies depend¬
ing on the target processor’s chip package. DIP, PLCC, etc. are some commonly used adaptors.
The above-mentioned emulators are almost dedicated ones, meaning they are built for emulating a
specific target processor and have little or less support for emulating the derivatives of the target proces¬
sor for which the emulator is built. This type/of emulators usually combines the entire emulation control
logic and emulation device (if present) in a single board. They are known as ‘Debug Board Modules
(DBMs)\ An alternative method of emulator design supports emulation of a variety of target proces¬
sors. Here the emulator hardware is partitioned into two, namely, 'Base Terminal’ and ‘Probe Card’.
The Base terminal contains all the emulator hardware and emulation control logic except the emulation
chip (Target board CPU’s replica). The base terminal is connected to the Development PC for establish¬
ing communication with the debug application. The emulation chip (Same chip as the target CPU) is
mounted on a separate PCB and it is connected to the base terminal through a ribbon cable. The ‘Probe
Card’ board contains the device adaptor sockets to plug the board into the target development board.
The board containing the emulation chip is known as the ‘Probe Card'. For emulating different target
CPUs the ‘Probe Card’ will be different and the base tenninal remains the same. The manufacturer of
the emulator supplies ‘Probe Card’ for different CPUs. Though these emulators are capable of emulat¬
ing different CPUs, the cost for ‘Probe Cards' is very high. Communication link between the emulator
base unit/ Emulator POD and debug application is established through a Serial/Parallel/USB interface.
Debug commands and debug information are sent to and from the emulator using this interface.
(BDM), OnCE, etc. Some vendors add ‘on chip software debug support’ through JTAG (Joint Test Ac¬
tion Group) port. Processors/controllers with OCD support incorporate a dedicated debug module to the
existing architecture. Usually the on-chip debugger provides the means to set simple breakpoints, query
the internal state of the chip and single step through code. OCD module implements dedicated registers
for controlling debugging. An On Chip Debugger can be enabled by setting the OCD enable bit (The bit
name and register holding the bit varies across vendors). Debug related registers are used for debugger
control (Enable/disable single stepping, Freeze execution, etc.) and breakpoint address setting. BDM
and JTAG are the two commonly used interfaces to communicate between the Debug application run¬
ning on Development PC and OCD module of target'CPU. Some interface logic in the form of hardware
will be implemented between the CPU OCD interface and the host PC to capture the debug information
from the target CPU and sending it to the debugger application running on the host PC. The interface
between the hardware and PC may be Serial/Parallel/USB. The following section will give you a brief
introduction about Background Debug Mode (BDM) and JTAG interface used in On Chip Debugging.
Background Debug Mode (BDM) interface is a proprietary On Chip Debug solution from Motorola.
BDM defines the communication interface between the chip resident debug core and host PC where the
BDM compatible remote debugger is running. BDM makes use of 10 or 26 pin connector to connect
to the target board. Serial data in (DSI), Serial data out (DSO) and Serial clock (DSCLK) are the three
major signal lines used in BDM. DSI sends debug commands serially to the target processor from the
remote debugger application and DSO sends the debug response to the debugger from the processor.
Synchronisation of serial transmission is done by the serial clock DSCLK generated by the debugger
application. Debugging is controlled by BDM specific debug commands. The debug commands are usih
ally 17-bit wide. 16 bits are used for representing the command and 1 bit for status/control.
Chips with JTAG debug interface contain a built-in JTAG port for communicating with the remote
debugger application. JTAG is the acronym for Joint.Test Action Group. JTAG is the alternate nappe for
IEEE 1149.1 standard. Like BDM, JTAG is also a serial interfacerThe signal lines of JTAG protocol are
explained below.
Test Data In (TDI): It is used for sending debug commands serially from remote debugger to the target
processor.
Test Data Out (TDO): Transmit debug response to the remote debugger from target CPU.
Test Clock (TCK): Synchronises the serial data transfer.
Test Mode Select (TMS): Sets the mode of testing.
Test Reset (TRST): It is an optional signal line used for resetting the target CPU.
The serial data transfer rate for JTAG debugging is chip dependent. It is usually within the range of
10 to 1000 MHz.
of the target board (address/data lines, port pins, etc.), checking the inter, connection among various
components, circuit continuity checking, etc. The various hardware debugging tools used in Embedded
Product Development are explained below.
\
13.5.2 Multimeter
I believe the name of the instrument itself is sufficient to give an outline of its usage. A multimeter is
used for measuring various electrical quantities like voltage (Both AC and DC), current (DC as well as
AC), resistance, capacitance, continuity checking, transistor checking, cathode and anode identification
of diode, etc. Any multimeter will work over a specific range for each measurement. A multimeter is the
most valuable tool in the toolkit of an embedded hardware developer. It is the primary debugging tool
for physical contact based hardware debugging and almost all developers start debugging the hardware
with it. In embedded hardware debugging it is mainly used for checking the circuit continuity between
different points on the board, measuring the supply voltage, checking the signal value, polarity, etc.
Both analog and digital versions of a multimeter are available. The digital version is preferred over
analog the one for various reasons like readability, accuracy, etc. Fluke, Rishab, Philips, etc. are the
manufacturers of commonly available high quality digital multimeters.
(e.g. High to low transition of a port pin of the target processor). Most of the modem digital CROs con¬
tain more than one channel and it is easy to capture and analyse various signals from the target board
using multiple channels simultaneously. Various measurements like phase, amplitude, etc. is also pos¬
sible with CROs. Tektronix, Agilent, Philips, etc. are the manufacturers of high precision good quality
digital CROs.
connected to the clock line and Test mode select line of the Test Access Port of the PCB respectively.
This forms a boundary scan path. Figure 13.41 illustrates the same.
As mentioned earlier, each pin of the device associates a boundary scan cell with it. The boundary
scan cell is'a multipurpose memory cell. The boundary scan cell associated with the input pins of an IC
is known as ‘input cells’ and the boundary scan cells associated with the output pins of an IC is known as
‘output cells’. The boundary scan cells can be used for capturing the input pin signal state and passing it
to the internal circuitry, capturing the signals from the internal circuitry and passing it to the output pin,
and shifting the data received from the Test Data In pin of the TAP. The boundary scan cells associated
with the pins are interconnected and they form a chain from the TDI pin of the device to its TDO pin.
The boundary scan cells can be operated in Normal, Capture, Update and Shift modes. In the Normal
mode, the input of the boundary scan cell appears directly at its output. In the Capture mode, the bound¬
ary scan cell associated with each input pin of the chip captures the signal from the respective pins to the
cell and the boundary scan cell associated with each output pin of the chip captures the signal from the
internal circuitry. In the Update mode, the boundary scan cell associated with each input pin of the chip
passes the already captured data to the internal circuitry and the boundary scan cell associated with each
output pin of the chip passes the already captured data to the respective output pin. In the shift mode,
data is shifted from TDI pin to TDO pin of the device through the boundary scan cells. ICs support¬
ing boundary scan contain additional boundary scan related registers for facilitating the boundary scan
operation. Instruction Register, Bypass Register, Identification Register, etc. are examples of boundary
scan related registers. The Instruction Register is used for holding and processing the instruction re:
ceived over the TAP. The bypass register is used for bypassing the boundary scan path of the device and
directly interconnecting the TDI pin of the device to its TDO. It disconnects a device from the bound¬
ary scan path. Different instructions are used for testing the interconnections and the functioning of the
Introduction to Embedded Systems
chip. Extest, Bypass, Sample and Preload, Intest, etc. are examples for instructions for different types
of boundary scan tests, whereas the instruction Runbist is used for performing a self test On the internal
functioning of the chip. The Runbist instruction produces a pass/fail result.
Boundary Scan Description Language (BSDL) is used for implementing boundary scan tests us¬
ing JTAG. BSDL is a subset of VHDL and it describes the JTAG implementation in a device. BSDL
provides information on how boundary scan is implemented in an integrated chip. The BSDL file (File
which describes the boundary scan implementation for a device in . bsd format) for a JTAG compli¬
ant device is supplied the device manufacturers or it can be downloaded from internet repository. The
BSDL file is used as the input to a Boundary Scan Tool for generating boundary scan test cases for a
PCB. Automated tools are available for boundary scan test implementation from multiple vendors. The
ScanExpress™ Boundary Scan (JTAG) product from Corelis Inc. (www.corelis.comJ is a popular tool
for boundary scan test implementation.
/ Integrated Development Environment (IDE) is an integrated environment for developing and debugging the
target processor specific embedded firmware. IDE is a software package which bundles a ‘Text Editor (Source
Code Editor)’, ‘Cross-compiler,(for cross platform development and compiler'forsame platform development)’,
‘Linker’and a‘Debugger’- ; . - -v- x. • |
F Keil pVisionJ is a licensed IDE tool from Keil Software iwww.keil.com). an ARM company, for 8051 family
‘ microcontroller based embedded firmware development •--v -. ....
F List File (.1st), Pre-processor Output file, Map File (File extension linker dependent), Object File (.obj), Hex File
(.hex), etc. are the files generated during the cross-compilation process of a source file
F Hex file is the binary executable file created from the source code. The absolute object file created by the litiker/
locater is Converted into processor understandable binary code. Object to Hexfile1 converter is the'utili^pragmtg>:
. for converting hit ^object file to a hex file ' ! r % >* tTA
F Intel HEX and Motorola HEX are the two commonly used Ilex file formats in embedded applications - f ||
F Disassembler is a utility program which converts machine codes into target processor specific Assembly codes/
instructions. Disassembling is the process of converting machine codes into Assembly code
F Decompiler is the utility program for translating machine codes into corresponding high level language
instructions ;A
F Simulator is a software application that precisely duplicates (mimics) the target CPU and simulates the, various
features and instructions supported by the target CPU r ■
F Emulator is a self-contained hardware device which emulates the target CPU. Emulator hardware contains
necessary emulation logic and it is hooked to the debugging application running on the development PC on ofte
end and connects to the target board through some interface on the other end
F Incremental EEPROM Burning technique. Inline breakpoint based Firmware Debugging, Monitor Program
based Firmware Debugging, In Circuit Emulator (ICE) based Firmware Debugging and On Chip Firmware
Debugging (OCD) are the techniques used for debugging embedded firmware on the target hardware /< d •
F Background Debug Mode (BDM) Interface and JTAG are the two commonly used interfaces for On Chip
Firmware Debugging (OCD) ... be
F Magnifying glass, Multimeter, Cathode Ray Oscilloscope (CRO), Logic Analyser and Function generator'arglhet
commonly used hardware debugging tools . - -i,
F 'Boundary scan is a technique for testing.the interconnection among the various chips, which.supportboufid^;':
scanning, in a complex board containing too many interconnections and multiple planes for routing
F Boundary Scan Description Language (BSDL) is a language similar to VHDL, which describes the boundary
The Embedded System Development Environment
scan implementation of a device and is used for implementing boundary scan tests using JTAG. BSDL file is ,
a file containing the boundary scan description for a device in boundary scan description language, which is
supplied as input to a Boundary scan tool for generating the boundary scan test cases
fW Keywords
Integrated Development A, software' package which bundles a ‘Text Editor (Source Code Editb?)’|§
Environment (IDE) ‘Cross-compiler (for cross platform development and compiler for same
platform development)’, ‘Linker’ and a ‘Debugger’
Kell p Vision! A licensed IDE tool from Keil Software (www.keil.com ). an ARM company,
fpr.8051 family microcontroller based embedded firmware development/^.-
Listing file (.LST File) File generated during cross compilation of rr fource code and it contains
information about the cross compilation process, like cross compiler details,
formatted source text (‘C’ code), assembly code generated from the sopree.
file, symbol tables, errors and warnings detected during the cross-compilation
process, etc. v .* .,.
Object File (.OBJ File) A specially formatted file with data records for symbolic information,'object
code, debugging information, library references, etc. generated during the
cross-compilation of a source file .a ■„ fy ' {S|§|H€
Map file File generated during cross-compilation process and it contains information
about the link/locate process. " ' ■-* / V
Hex file The binary executable file created from the source code SS*§|
Intel HEX HEX file representation format ,
Motorola HEX HEX file representation format ! )„"■
Disassembler Utility program which converts machine codes into target processor specific
Assembly codes/instructions.
Decompiler Utility program for translating machine codes into corresponding high level
language instructions
Simulator Software application that precisely duplicates (mimics) the target CPU and
simulates the various features and instructions supported by the target CPU
Monitor Program Program which acts as a supervisor and controls the downloading of user
code into the code memory, inspects and modifies register/memory locations,
allows single stepping of source code, etc.
In Circuit Emulator (ICE) : A hardware device for emulating the target CPU for debug purpose
Debug Board Module (DBM) : ICE device which contains the emulation control logic and emulation chip in
a single hardware unit and is designed for a particular family of device
Background Debug Mode (BDM) : A proprietary serial interface from Motorola for On Chip Debugging
JTAG : A serial interface for target board diagnostics and debugging
Boundary Scan : A target hardware debug method for checking the interconnections among
the various chips of a complex board
Boundary Scan Description : A language similar to VHDL, which describes the boundary scan
Language (BSDL) implementation of a JTAG supported device
Introduction to Embedded Systems
Objective Questions ]
1 Which of the following intermediate file, generated during cross-compilation of an Embedded C file holds the
assembly code generated corresponding to the c source code.
(a) List File (b) Preprocessor output file (c) Object file (d) Map file
2 Which of the following detail(s) is(are) kept in an object file generated during the process of cross-compiling an
Embedded C file.
(a) Variable and function names (b) Variable and function reference
(c) Reserved memory for global variables (d) All of these
(e) None of these
3 Which of the following intermediate file, generated during the cross-compilation of an Embedded C files holds the
information about the link/locate process for the multiple object modules of the project?
(a) List file (b) Preprocessor output file (c) Object file (d) Map file
4 Which of the following file generated during the cross-compilation process of an Embedded C project holds the
machine code corresponding to the target processor?
(a) List file (b) Preprocessor output file (c) Object file (d) Map file
5 Examine the following Intel HEX Record
:03000000020C1FD0
This record is ?
(a) a Data Record (b) an End of File Record (c) a Segment Address Record
(d) an Extended Linear Address record
6 Examine the following Intel HEX record
:03000000020C1FDO
What is the number of data bytes in this record?
(a) 0 (b) 3 (c) 2 (d) 20
7 Examine the following Intel HEX record
: 03000000020C1FDO
What is the start address of the data bytes in this record?
(a) 0x0000 (b) 0x3000 (c) OxlFDO (d) 0x20Cl
8 Examine the following Intel HEX Record
:03000000020C1FDO
Which all are the data bytes present in this record?
(a) 03,00,00 (b) 02, 0C, IF (c) 0C, IF, DO (d) 00,00,20
9 The program that converts machine codes into target processor specific Assembly code is known as
(a) Disassembler (b) Assembler (c) Cross-compiler (d) Decompiler
10 Which of the following is true about a simulator used in embedded software debugging?
(a) It is a software tool (b) It requires target hardware for simulation
(c) It doesn’t require target hardware for simulation (d) (a) and (b) (e) (a) and (c)
11 Which of the following is an example for on chip firmware debugging?
(a) OnCE (b) BDM (c) All of these
Review Questions
3 What are the different files generated during the cross-compilation of an Embedded C file? Explain them in
detail.
4 Explain the various details held by a List file generated during the process of cross-compiling an Embedded C
project.
5 Explain the various details held by a Map file generated during the process of cross-compiling an Embedded C
project.
6 Explain the various details stored in an Object file generated during the cross-compilation of an Embedded C file.
7 Explain the difference between Intel Hex and Motorola Hex file format.
8 Explain the format of Hex records in an Intel Hex File.
9 Explain the format of Hex records in a Motorola Hex File.
10 What is the difference between an assembler and a disassembler? State their use in Embedded Application
development.
11 What is a decompiler?
12 What is the difference between a simulator and an emulator?
13 Explain the advantages and limitations of simulator based debugging.
14 What are the different techniques available for embedded firmware debugging? Explain them in detail.
15 What is a Monitor program? Explain its role in embedded firmware debugging?
16 What is ROM emulation! Explain In Circuit Emulator (ICE) based debugging in detail.
17 Explain On Chip Debugging (OCD).
18 Explain the different tools used for hardware debugging.
19 Explain the Boundary Scan based hardware debugging in detail.
Lab Assignments
if any, display unit if any, PCB mounting holes, placement of connectors, etc.) from the engineering
design team. Apart from the product aesthetics, the engineering design team should be well aware of the
budget limitations for the product and they should be capable of suggesting a design fitting into the al¬
located budget. Some technical aspects also need to be considered while selecting materials for product
enclosures. For example, certain products involving RF signals cannot use metal enclosures since they
absorb the RF signals and reduce the field strength, hence in such cases some cheap alternatives should
be put forward. The enclosure design should also take into account various protections like mechanical
shock, water resistance, etc.
14.1 • - ,
Frankly speaking, product enclosure design is an art. As a sculpture shows the artistic ability of its
creator, the product’s enclosure reflects the design capabilities of its designer. With the latest design
technologies, the custom drawing using pen and pencil on paper for a design (front, top and end view
of the design) has given way to Computer Aided Design (CAD). A variety of CAD tools are available
for Product Enclosure Design. ‘Solidworks’, ‘AutoCAD’, ‘ProEngineer’, ‘Catia Interface’, etc. are ex¬
amples of CAD tools. By combining the artistic ability of the designer with the design flexibility offered
by the automated tools, good product enclosures maintaining very good aesthetics can be developed.
This is the best choice for enclosure development for prototype products and low volume product re¬
quirements. The capital investment required is very small for handmade enclosures. Commonly used
materials for handmade enclosures are plastics like Poly Vinyl Chloride (PVC) and Acrylic. The PVC/
Acrylic sheet can be cut into desired shape and combined using a suitable adhesive. It is a low cost tech¬
nology but limited to the fabrication of low volume products. The manpower involved is comparatively
high and development time is also high.
tools is used as input for this. Rapid .prototyping is also known as generative manufacturing or solid
freeform fabrication or layered manufacturing. Rapid prototyping is best suggested for Proof of Concept
(PoC) models. The various techniques adopted in rapid prototyping are listed below.
14.2.3.1 Room Temperature Vulcanised (RTV) Tooling Room Temperature Vulcanised (RTV)
tooling technique is used for polyurethane housings. First an RTV rubber tools is created by pouring
liquid silicone rubber around a master pattern. The resulting model is taken out of the master pattern,
finished well and then used for casting polyurethane housings. These housings are painted according to
the user needs. Text and graphics can be printed on top of the painted housing. The key advantage of
RTV is the ability to produce high volume prototypes at a nominal cost.
14.2.3.2 Computer Numeric Control (CNC) Tooling In Computer Numeric Control (CNC)
tooling, CNC machines are used for milling the block of plastic according to the input from a design file
generated by the 3D modelling tool to develop desired housing.
Introduction to Embedded Systems
14.2.3.3 Injection Moulding Injection moulding system consists of a moulding machine and
mould plates (dummy shape of the product to be developed). Plastic/resin is fed into the injection barrel
of the machine through the hopper and in the barrel it is heated to the appropriate melting temperature.
Molted resin is injected to the space between the mould plates through a nozzle to create moulds. Mould
is cooled constantly to a temperature that allows the resin to solidify. Mould plates are held together by
hydraulic or mechanical force.
14.3 SUMMARY___
Competitive products where features as well as aesthetics are important should always require attractive
enclosures. Mobile handsets, children toys, etc. are examples of it. The more you make the enclosure
attractive, the more is the demand. Custom enclosure development is required for such products. Proof
of Concept (PoC) design products and products which are not aiming a commercial market can go for
either custom enclosure or Commercial off-the-shelf enclosure. Moreover the enclosure should consider
any special needs by the product like protection standards, shielding requirements, etc.
Summary ]
•S Product enclosure is essential for protecting the hardware from dust, water, corrosion, etc. to provide ruggedness
and to provide a good aesthetics (look and feel) to the product
V product enclosure design should take care of the aesthetics', protebtion requirements, shielding requirements, etc.
V Product enclosure design deals with the design of the enclosure for the embedded product (Hardware with
firmware integrated). Various computer aided design (CAD) tools like ‘Solidworks’, 'AutoCAD’, ‘ProEngineer’,
‘Catia Interface’, etc. can be used for product enclosure design
Product Enclosure Design and Development
•S Product enclosure, can be developed manually or using Computer Assisted Manufacturing (CAM) tools bas ed on
the design file generated from the Computer Aided Design -
V Handmade enclosure is a typical example for manually developed product enclosure. Tt is the best ehqiceSfpf
prototype and low volume production It uses plasties like ABS or PVC and sheet metalfor enclosure design; 5
V Rapid prototyping is a 3-Dimcntional Computer Assisted Manufacturing (3D CAM) technique used for prototype
development as well as mass production " , ; ,.
S 'Stereolithography (SLA). Scleetive Laser Sintering (SLS), Fused Deposition Modelling (FDM), etc. are examples
. for various types of rapid prototyping “' •- ; &
V Stereolithography is a process in which liquid plastic is Solidified in precise patterns by a UV laser beam, resulting
in a solid epoxy realisation of a 3D design ofithe enclosure ":v'\ ,
V Selective Laser Sintering (SLS) is similar to SLA, except that the 3D prototypes are created from the drawing by
fusing or sintering powdered thermoplastic materials layer by ldyer
V FuSed Deposition Modelling: (EDM) is a solid-based rapid,prototyping method, which,uses melted thermoplastic ;
polymer in the form of paste for building the layers ‘
S The tooling and moulding techniques are mainly used for developing plastic and metal housings for high volume-
production. Here a tool or mould is developed first and the enclosure is developed from this mould. Room
Temperature Vulcanised (RTV), Computer Numeric Control (CNC), injection moulding, etc. are examples for
. tooling techniques ? - • * ; ■ ■ _ ; ' ’ ';i£
V Room Temperature Vulcanised (RTV) tooling technique is used for polyurethane housings. First an RTV rubber
tool is created by pouring liquid silicone rubber around a master pattern. The resulting model is taken out of the
master pattem/finishedwelfandthen usedforcasting polyurethaneLousings - P.
V In Computer Numeric Control (CNC) tooling, CNC machines are used for milling the block of plastic according
to the input from a design file generated by the 3D modeling tool to develop desired housing ;
V Injection moulding system consists of a moulding machine and mould plates (dummy shape of the product to be
developed). Plastic/resin is fed into the injection barrel of the machine through the hopper and in the barrel it
is heated to the appropriate melting temperature. Molted resin is injected to the space between the mould plates
through a nozzle to create moulds
V Sheet metal is the foil (thin) form of metal sheets, which can be cut or bent into different shapes
V Commercial off-the-shelf enclosure is the readily available enclosures in the market. It can be either plastic or
metal
Keywords
( “ Objective Questions
1 Which of the following is not a Rapid Prototyping technology for product enclosure development?
(a) Selective Laser Sintering (SLS) (b) Fused Deposition Modelling (FDM)
(c) Room Temperature Vulcanisation (RTV) (d) Stereolithography (SLA)
2 Which of the following is not a Tooling and Moulding technology for product enclosure development?
(a) Room Temperature Vulcanisation (RTV) (b) Sheet metal
(c) Injection moulding (d) CNC tooling
Review Questions
1 Explain the various factors that need to be addressed while selecting an enclosure for an embedded product.
2 Explain the different product enclosure development techniques in detail
3 What are the merits and limitations of Handmade Enclosures?
4 What is Rapid Prototyping? What are the different techniques available for Rapid Prototyping? Explain the merits
and limitations of each.
5 Explain the Tooling and Moulding techniques used for product enclosure development
6 Explain the Sheet Metal based product enclosure development
w, wOTKTTa
hV:-1 *b .5?.- ;^SWl’;1:''^ TOM
fgg*# ;«? Vif2[;
■PT",
LEARNING OBJECTIVES
duct development
ict development
/' / - ;-rr * 3 s ^ /w - ^ /* " / / ^ ^,-
■^Jtectrrtthe different phaseZfNeed/fonceptuabsatio ypment, Testing, Deployment, Support,
Upgrades and Disposal) of the product developmer ies-happening in each stage of the life
Sifpgi
./ i_i_* j:cc„._i._un:_i_L~;_,
t jJ^SV «i 51 ■’/'> 1 s «« ■< ■'ir 4 v
■'—<T-"‘ HtyMjftp
/ ZtlTZpXZ!,. adeontoges ond,motions of UmWlncemml c, Fountain Hotel in embedded
• product development life cycle management ffpjr
development life cycle management ■ : v" ”
S Learn about the application, advantages and limitations of Spiral Model in embedded product development life
cycle management ,
Just imagine about your favourite chicken dish that your mom served during a holiday lunch. Have
you ever thought of the effort behind preparing the delicious chicken dish? Frankly speaking No, Isn’t
it? Okay let’s go back and analyse how the yummy chicken in the chicken stall became the delicious
chicken dish served on the dining table. One fine morning Mom thinks “WohhH It’s Sunday and why
can’t we have a special lunch with our favourite chicken dish.” Mom tells this to Papa and he agrees on
it and together they decide how much quantity of chicken is required for the four-member family (may
be based on past experience) and lists out the various ingredients required for preparing the dish. Now
the list is ready and papa checks his purse to ensure whether the item list is at the reach of the money in
his purse. If not, both of them sit together and make some reductions in the list. Papa goes to the market
Introduction to Embedded Systems
to buy the items in the list. Finds out a chicken stall where quality chicken is selling at a nominal rate.
Procures all the items and returns back home with all the items in the list and hands over the packet to
Mom. Mom starts cleaning the chicken pieces and chopping the ingredient/vegetables and then puts
them in a vessel and starts boiling them. Mom may go on to preparing other side dishes or rice and
comes back to the chicken dish preparation at regular intervals to add the necessary ingredients (like
chicken masala, chilly powder, coriander powder, tamarind, coconut oil, salt, etc.) in time and checks
whether all ingredients are in correct proportion, if not adjust them to the required proportion. Papa
gives an overall supervision to the preparation and informs mom if she forgot to add any ingredients
and also helps her in verifying the proportion of the ingredients. Finally the fragrance of spicy boiled
chicken comes out of the vessel and mom realises that chicken dish is ready. She takes it out from the
stove and adds the final taste building ingredients. Mom tastes a small piece from the dish and ensures
that it meets the regular quality. Now the dish is ready to serve. Mom takes the responsibility of serving
the same to you and what you need to do is taste it and tell her how tasty it is. If you closely observe
these steps with an embedded product.developer’s mind, you can find out that this simple chicken dish
preparation contains various complex activities like overall management, development, testing and re¬
leasing. Papa is the person who did the entire management activity starting from item procurement to
giving overall supervision. Mom is responsible for developing the dish, testing the dish to certain extent
and serving the dish (release management) and finally you are the one who did the actual field test. All
embedded products will have a design and development life cycle-similar to our chicken dish prepara¬
tion example, with management, design, development, test and release activities.
Embedded Product Development Life Cycle (Let us call it as EDLC, though it is not a standard and
universal term) is an ‘Analysis-Design-Implementation’ based standard problem solving approach for
Embedded Product Development. In any product development application, the first and foremost step is
to figure out what product needs to be developed (analysis), next you need to figure out a good approach
for building it (design) and last but not least you need to develop it (implementation).
of the overall investment expenditure. For this, the product should be acceptable by the end user and it
should meet the requirements of end user in terms of quality, reliability and functionality. So it is very
essential to ensure that the product is meeting all these criteria, throughout the design, development,
implementation and support phases. Embedded Product Development Life Cycle (EDLC) helps out in
ensuring all these requirements. EDLC has three primary objectives, namely
1. Ensure that high quality products are delivered to end user.
2. Risk minimisation and defect prevention in product development through project management.
3. Maximise the productivity.
The communication aspect of the project management deals with co-ordination and interaction among
resources and client from which the request for the product development aroused. Project management
adds an extra cost on the budget but it is essential for ensuring the development process is going in the
right direction and the schedules of the development activity are meeting. Without management, the
development work may go beyond the stipulated time frame (schedule slipping) and may end up in a
product which is not meeting the requirements from the client side, as a result re-works should be done
to rectify the possible deviations occurred and it will again put extra cost on the development. Project
management makes the proverb “A stitch in time saves nine” meaningful in an embedded product devel¬
opment. Apart from resource allocation, project management also covers activities like task allocation,
scheduling, monitoring and project tracking. Computer Assisted Software Engineering (CASE) Tools
and Gantt charts help the manager in achieving this. Microsoft® Project Tool is a typical example of
CASE tool for project management.
if the product under development requires a 10 base T Ethernet connectivity, you can either implement
the same in your product by using the TCP/IP chip and related components or can use a readily available
TCP/IP full functional plug-in module. The second approach will save effort and time. EDLC should
take all these aspects into consideration to provide maximum productivity,
The life cycle of a product development is commonly referred to as models. Model defines the various
phases involved in the life cycle. The number of phases involved in a model may vary according to the
complexity of the product under consideration. A typical simple product contains five minimal phases
namely: ‘Requirement Analysis’, ‘Design’, ‘Development and Test’, ‘Deployment’ and ‘Maintenance’.
The classic Embedded Product Life Cycle Model contains the phases: ‘Need’, ‘Conceptualisation’,
‘Analysis’, ‘ Design’, ‘Development andTesting’, ‘Deployment’, ‘Support’, ‘Upgrades’and ‘Retirement/
Disposal’ (Fig. 15.1). In a real product development environment, the phases given in this model may
vary and can have submodels or the models contain only certain important phases of the classic model.
As mentioned earlier, the number of phases involved in the EDLC model depends on the complexity of
the product. The following section describes each phases of the classic EDLC model in detail'.
Upgrades
Design
Development and
Support Testing
Deployment
15.4.1 Need
Any embedded product evolves as an output of a ‘Need'. The need may come from an individual or
Introduction to Embedded Systems
15.4.2 Conceptualisation
Conceptualisation is the ‘Product Concept Development Phase’ and it begins
immediately after a Concept Proposal is formally approved. Conceptualisation
phase defines the scope of the concept, performs cost benefit analysis and feasi¬
bility study and prepares project management and risk management plans. Con-
The Embedded Product Development Life Cycle (EDLC)
ceptualisation can be viewed as a phase shaping the “Need” of an end-user or convincing the end user,
whether it is a feasible product and how this product can be realised (Fig. 15.2).
The ‘Conceptualisation’ phase involves two types of activities, namely; ‘Planning Activity’ and
‘Analysis and Study Activity’. The Analysis and Study Activities are performed to understand the op¬
portunity of the product in the market (for a commercial product). The following are tire important
‘Analysis and Study activities’ performed during ‘Conceptualisation Phase’.
15.4.2.1 Feasibility Study Feasibility study examines the need for the product carefully and sug¬
gests one or more possible solutions to build the need as a product along with alternatives. Feasibility
study analyses the Technical (Technical Challenges, limitations, etc.) as well as financial feasibility
(Financial requirements, funding, etc.) of the product under consideration.
15.4.2.2 Cost Benefit Analysis (CBA) The Cost Benefit Analysis (CBA) is a means of identify¬
ing, revealing and assessing the total development cost and the profit expected from the product. It is
somewhat similar to the loss-gain analysis in business term except that CBA is an assumption oriented
approach based on some rule of thumb or based on the analysis of data available for similar products
developed in the past. In summary, CBA estimates and totals up the equivalent money value of the ben¬
efits and costs of the product under consideration and give an idea about the worthwhile of the product.
Some common underlying principles of Cost Benefit Analysis are illustrated below.
Common Unit of Measurement In order to make the analysis sensible and meaningful, all aspects
of the product, either plus points or minus points should be expressed in terms of a common unit. Since
Introduction to Embedded Systems
the ultimate aim of a product is “Marginal Profit” and the marginal profit is expressed in terms of money
in most cases, ‘money’ is taken as the common unit of measurement. Hence all benefits and costs of the
product should be expressed in terms of money. Money in turn may be represented by a common cur¬
rency. It can be Indian Rupee (INR) or US Dollar (USD), etc.
Market choice based benefit measurement Ensure that the product cost is justifying the money
(value for money), the end user is spending on the product to purchase, for a commercial product.
Targeted end users For a commercial product it is very essential to understand the targeted end-
users for the product and their tastes to give them the best in the industry.
15.4.2.3 Product Scope Product scope deals with what is in scope (what all functionalities or tasks
are considered) for the product and what is not in scope (what all functionalities or tasks are not consid¬
ered) for the product. Product scope should be documented properly for future reference.
15.4.2.4 Planning Activities The planning activity covers various plans required for the product
development, like the plan and schedule for executing the next phases following conceptualisation, Re¬
source Planning - How many resources should work on the project, Risk Management Plans - The
technical and other kinds of risks involved in the work and what are the mitigation plans, etc. At the end
of the conceptualisation phase, the reports on ‘Analysis and Study Activities’ and ‘Planning Activities’
are submitted to the client/project sponsor for review and approval, along with any one of the following
recommendation.
1. The product is feasible; proceed to the next phase of the product life cycle.
2. The product is not feasible; scrap the project
The executive committee, to which the various reports are submitted, will review the same and will
communicate the review comments to the officials submitted the conceptualisation reports and will give
the green signal to proceed, if they are satisfied with the analysis and estimates.
15.4.3 Analysis
Requirements Analysis Phase starts immediately after the documents
submitted during the ‘Conceptualisation’ phase is approved by the
client/sponsor of the project. Documentation related to user require¬
ments from the Conceptualisation phase is used as the basis for further
user need analysis and the development of detailed user requirements.
Requirement analysis is performed to develop a detailed functional mod¬
el of the product under consideration. During the Requirements Analysis
Phase, the product is defined in detail with respect to the inputs, process¬
es, outputs, and interfaces at a functional level. Requirement Analysis
phase gives emphasis on determining ‘what functions must be performed
by the product’ rather than how to perform those functions. The various
activities performed during ‘Requirement Analysis’ phase is illustrated
in Fig. 15.3.
15.4.3.1 Analysis and Documentation The analysis and documentation activity consolidates the
business needs of the product under development and analyses the purpose of the product. It address¬
es various functional aspects (product features and functions) and quality attributes (non-functional
requirements) of the product, defines the functional and data requirements and connects the function-
The Embedded Product Development Life Cycle (EDLC)
al requirements with the data requirements. In summary, the analysis activities address all business
functionalities of the product and develop a logical model describing the fundamental processes and the
data required to support the functionalities. The various requirements that need to be addressed during
the requirement analysis phase for a product under consideration are listed below.
1. Functional capabilities like performance, operational characteristics (Refer to Chapter 3 for
details on operational characteristics), etc.
2. Operational and non-operational quality attributes (Refer to Chapter 3 for Quality attributes of
embedded products)
3. Product external interface requirements
4. Data requirements
5. User manuals
6. Operational requirements
7. Maintenance requirements
8. General assumptions
15.4.3.2 Interface Definition and Documentation The embedded product under consideration
may be a standalone product or it can be a part of a large distributed system. If it is a part of another
system there should be some interface between the product and the other parts of the distributed system.
Even in the case of standalone products there will be some standard interfaces like serial, parallel etc as
a provision to connect to some standard interfaces. The interface definition and documentation activity
should clearly analyse on the physical interface (type of interface) as well as data exchange through
these interfaces and should document it. It is essential for the proper information flow throughout the
life cycle.
15.4.3.3 Defining Test Plan and Procedures Identifies what kind of tests are to be performed
and what all should be covered in testing the product. Define the test procedures; test setup and test
Introduction to Embedded Systems
environment. Create a master test plan to cover all functional as well as other aspects of the product
and document the scope, methodology, sequence and management of all kinds of tests. It is essential for
ensuring the total quality of the product. The various types of testing performed in a product develop¬
ment are listed below.
1. Unit testing—Testing each unit or module of the product independently for required functionality
and quality aspects
2. Integration testing—Integrating each modules and testing the integrated unit for required func¬
tionality
3. System testing—Testing the functional aspects/product requirements of the product after integra¬
tion. System testing refers to a set of, different tests and a few among them are listed below.
• Usability testing—Tests the usability of the product
• Load testing—Tests the behaviour of the product under different loading conditions.
• Security testing—Testing the security aspects of the product
• Scalability testing—Testing the scalability aspect of the product
• Sanity testing—Superficial testing performed to ensure that the product is functioning prop¬
erly
• Smoke testing—Non exhaustive test to ensure that the crucial requirements for the product
are functioning properly. This test is not giving importance to the finer details of different
functionalities
• Performance testing—Testing the performance aspects of the product after integration
• Endurance testing—Durability test of the product
4. User acceptance testing—Testing the product to ensure it is meeting all requirements of the end
user.
At the end, all requirements should be put in a template suggested by the standard (e.g. ISO-9001)
Process Model (e.g. CMM Level 5) followed for the Quality Assurance. This document is referred
as ‘Requirements Specification Document’. It is sent out for review by the client and the client, after
reviewing the requirements specification document, communicates back with the review comments.
According to the review comments more requirement analysis may have to be performed and re-work
required is performed on the initial ‘Requirement Specification Document’.
15.4.4 Design
Product ‘Design phase’ deals with the entire design of the product taking
the requirements into consideration and it focuses on ‘how’ the required
functionalities can be delivered to the product. The design phase identi¬
fies the application environment and creates an overall architecture for
the product. Product design starts with ‘Preliminary Design/High Level
Design’. Preliminary design establishes the top-level architecture for the
product, lists out the various functional blocks required for the product,
and defines the inputs and outputs for each functional block. The func¬
tional block will look like a ‘black box’ at this design point, with only in¬
puts and outputs of the block being defined. On completion, the ‘Prelimi¬
nary Design Document (PDD) is sent for review to the end-user/client
The Embedded Product Development Life Cycle (EDLC)
who come up with a need for the product. Tf the end-user agrees on the preliminary design, the product
design team can take the work to the next level - ‘Detailed Design Detailed design generates a detailed
architecture, identifies and lists out the various components for each functional block, the inter connec¬
tion among various functional blocks, the control algorithm requirements, etc. The ‘Detailed Design’
also needs to be reviewed and get approved by the end user/customer. If the end user wants modifica¬
tions on the design, it can be informed to the design team through review comments.
An embedded product is a real mix of hardware, embedded firmware and enclosure. Hence both
Preliminary Design and Detailed Design should take the design of these into consideration. For tradi¬
tional embedded system design approach, the functional requirements are classified into either hard¬
ware or firmware at an early stage of the design and the hardware team works on the design of the
required hardware, whereas the software team works on the design of the embedded firmware. Today
the scenario is totally changed and a hardware software co-design approach is used. In this approach,
the functional requirements are not broken down into hardware and software, instead they are treated
as system requirements and the partitioning of system requirements into either hardware or software
is performed at a later stage of the design based on hardware-software trade-offs for implementing the
required functionalities. The hardware and software requirements are modelled using different model¬
ling techniques. Please refer to Chapter 7 for more details on hardware-software co-design and program
models used in hardware-software co-design. The other activities performed during the design phase
are ‘Design of Operations and maintenance manual’ and ‘Design of Training materials’ (Fig. 15.4).
The operations manual is necessary for educating the end user on ‘how to operate the product’ and the
maintenance manual is essential for providing information on how to handle the product in situations
like non-functioning, failure, etc.
Introduction to Embedded Systems
i
I -Inputs
O - Outputs
C - Constraints
A - Assumptions
—— *
C A
C A
I Inputs
O Outputs
C Constraints
A Assumptions
Cl, C2 Component
IC Inter-connection
M Module
ded hardware development, embedded firmware development and product enclosure development. Em¬
bedded hardware development refers to the development of the component placement platform - PCB
using CAD tools and its fabrication using CAM Tools. Embedded firmware development is performed
using the embedded firmware development tools. The mechanical enclosure design is performed with
CAD Tools like Solid Works, AutoCAD, etc. “Look and Feel” of a commercial product plays an impor¬
tant role on its market demand. So the entire product design and development should concentrate on the
product aesthetics (Size, Weight, Appearance, etc.) and special requirements like protection shielding,
etc.
Component procurement also carried out during this phase. As mentioned in one of the earlier chap¬
ters, some of the components may have “lead time - The time required for the component to deliver
after placing the purchase order for the component” and the component procurement should be planned
well in advance to avoid the possible delay in the product development. The other important activities
carried out during this phase are preparation of test case procedures, preparation of test files and de¬
velopment of detailed user, deployment, operations and maintenance manual. The embedded firmware
development may be divided into various modules and the firmware (code) review for each module as
well as the schematic diagram and layout file review for hardware is carried out during the development
phase and the changes required should be incorporated in the development from time to time.
The testing phase can be divided into independent testing of firmware and hardware (Unit Testing),
testing of the product after integrating the firmware and hardware (Integration Testing), testing of the
whole system on a functionality and non-functionality basis (System Testing) and testing of the product
against all acceptance criteria mentioned by the client/end user for each functionality (User Acceptance
Testing). The Unit testing deals with testing the embedded hardware and its associated modules, the
different modules of the firmware for their functionality independently. A test plan is prepared for the
unit testing (Unit Test plan) and test cases (Unit Test cases) are identified for testing the functionality of
the product as per the design. Unit test is normally performed by the hardware developer or firmware
developer as part of the development phase. Depending on the product complexity, rest of the tests
can be considered as the activities performed in the ‘Testing phase’ of the product. Once the hardware
and firmware are unit tested, they are integrated using various firmware integration techniques and the
integrated product is tested against the required functionalities. An integration test plan is prepared for
this and the test cases for integration testing (Integration test cases) is developed. The Integration testing
ensures that the functionality which is tested working properly in an independent mode remains work¬
ing properly in an integrated product (Hardware + Firmware). The integration test results are logged in
and the firmware/hardware is reworked against any flaws detected during the Integration testing. The
purpose of integration testing is to detect any inconsistencies between the firmware modules units that
are integrated together or between any of the firmware modules and the embedded hardware. Integration
testing of various modules, hardware and firmware is done at the end of development phase depending
on the readiness of hardware and firmware.
The system testing follows the Integration testing and it is a set of various tests for functional and
non-functional requirements verification. System testing is a kind of black box testing, which doesn’t
require any knowledge on the inner design logic or code for the product. System testing evaluates the
product’s compliance with the requirements. User acceptance test or simply Acceptance testing is the
product evaluation performed by the customer as a condition of the product purchase. During user Ac¬
ceptance testing, the product is tested against the acceptance value set by customer/end user for each
requirement (e.g. The loading time for the application running on the PDA device is less than 1 mil¬
lisecond ©). The deliverables from the ‘Design and Testing’ phase are firmware source code, firmware
binaries, finished hardware, various test plans (Hardware and Firmware Unit Test plans, Integration Test
plan, System Test plan and Acceptance Test plan), test cases (Hardware and Firmware Unit Test cases,
Introduction to Embedded Systems
Integration Test cases, System Test cases and Acceptance Test cases) and test reports (Hardware and
Firmware Unit Test Report, Integration Test Report, System Test Report and Acceptance Test Report).
15.4.6 Deployment
Deployment is the process of launching the first fully functional model of the
product in the market (for a commercial embedded product) or handing over
the fully functional initial model to an end user/client. It is also known as First
Customer Shipping (FCS). During this phase, the product modifications as per
the various integration tests are implemented and the product is made opera¬
tional in a production environment. The ‘Deployment Phase’ is initiated after
the system is tested and accepted (User Acceptance Test) by the end user. The
important tasks performed during the Deployment Phase are listed below.
15.4.6.1 Notification of Product Deployment Whenever the product is ready to launch in the
market, the launching ceremony details should be communicated to the stake holders (all those who are
related to the product in a direct or indirect way) and to the public if it is a commercial product. The
notifications.can be sent out through e-mail, media, etc. mentioning the following in a few words.
1. Deployment Schedule (Date Time and Venue)
2. Brief description about the product
3. Targeted end users
4. Extra features supported with respect to an existing product (for upgrades of existing model and
new products having other competitive products in the market)
5. Product support information including the support person name, contact number, contact mail ID,
etc.
15.4.6.2 Execution of Training Plan Proper training should be given to the end user to get them
acquainted with the new product. Before launching the product, conduct proper training as per the train¬
ing plan developed during the earlier phases. Proper training will help in reducing the possible damages
to the product as well as the operating person, including personal injuries and product mal-functioning
due to inappropriate usage. User manual will help in understanding the product, its usage and accessing
its functionalities to certain extend.
15.4.6.3 Product Installation Instal the product as per the installation document to ensure that it
is fully functional. As a typical example take the case of a newly purchased mobile handset. The user
manual accompanying it will tell you clearly how to instal the battery, how to charge the battery, how
many hours the battery needs to be charged, how the handset can be switched on/off, etc.
15.4.6.4 Product post-implementation Review Once the product is launched in the market,
conduct a post-implementation review to determine the success of the product. The ultimate aim behind
post-implementation review is to document the problems faced during installation and the solutions
adopted to overcome them which will act as a reference document for future product development. It
also helps in understanding the customer needs and the expectations of the customer on the next version
of the product.
15.4.1 Support
The support phase deals with the operations and maintenance of the product in a production environ¬
ment. During this phase all aspects of operations and maintenance of the product are covered and the
The Embedded Product Development Life Cycle (EDLC)
product is scrutinised to ensure that it meets the requirements put forward by the
end user/client. ‘Bugs (product mal-fimctioning or unexpected behaviour or any
operational error)’ in the products may be observed and reported during the op¬
erations phase. Support should be provided to the end user/client to fix the bugs
in the product. The support phase ensures that the product meets the user needs
and it continues functioning in the production environment. The various activities
involved in the ‘Support’ phase are listed below.
15.4.7.1 Set up .a Dedicated Support Wing The availability of certain embedded products in
terms of product functioning in production environment is crucial and they may require 24x7 support in
case of product failure or malfunctioning. For example the patient monitoring system used in hospitals
is a very critical product in terms of functioning and any malfunctioning of the product requires immedi¬
ate technical attention/support from the supplier. Set up a dedicated support wing (customer care unit)
and ensure high quality service is delivered to the end user. ‘After service’ plays significant role in prod¬
uct movement in a commercial market. If the manufacturer fails to provide timely and quality service to
the end user, it will naturally create the talk "the product is good but the after service is very poor' among
the end users and people will refrain from buying such products and will opt for competitive products
which provides more or less same functionalities and high quality service. The support wing should be
set up in such a way that they are easily reachable through e-mail, phone, fax, etc.
15.4.7.2 Identify Bugs and Areas of Improvement None of the products are bug-free. Even if
you take utmost care in design, development and implementation of the product, there can be bugs in it
and the bugs reveal their real identity when the conditions are favouring them, similar to the harmless
microbes living in human body getting turned into harmful microbes when the body resistance is weak.
You may miss out certain operating conditions while design or development phase and if the product
faces such an operating condition, the product behaviour may become unexpected and it is addressed
with the familiar nick name lBug’ by a software/product engineer. Since the embedded product market
is highly competitive, it is very essential to stay connected with the end users and know their needs for
the survival of the product. Give the end user a chance to cxpressfyheir views on the product and sug¬
gestions, if any, in terms of modifications required or feature enhancements (areas of improvement),
through user feedbacks. Conduct product specific surveys and collect as much data as possible from the
end user.
15.4.8 Upgrades
The upgrade phase of product development deals with the development of up- •
grades (new versions) for the product which is already present in the market.
Product upgrade results as an output of major bug fixes or feature enhancement
requirements from the end user. During the upgrade phase the system is subject
<
to design modification to fix the major bugs reported or to incorporate the new
feature addition requirements aroused during the support phase. For embed¬
ded products, the upgrades may be for the product resident firmware or for the
4 ft
hardware embedding the firmware. Some bugs may be easily fixed by modifying the firmware and it
is known as firmware up-gradation. Some feature enhancements can also be performed easily by mere
firmware modification. The product resident firmware will have a version number which starts with
version say 1.0 and after each firmware modification or bug fix, the firmware version is changed accord¬
ingly (e.g. version 1.1). Version numbering is essential for backward traceability. Releasing of upgrades
Introduction to Embedded Systems
is managed through release management. (Embedded Product Engineers might have used the term
“Today I have a release” at least once in their life). Certain feature enhancements and bug fixes require
hardware modification and they are generally.termed as hardware upgrades. Firmware also needs to be
modified according to the hardware enhancements. The upgraded product appears in the market with the
same name as that of the initial product with a different version number or with a different name.
15.4.9 Retirement/Disposal
We are living in a dynamic world where everything is subject to rapid changes.
The technology you feel as the most advanced and best today may not be the
same tomorrow. Due to increased user needs and revolutionary technological
changes, a product cannot sustain in the market for a Jong time. The disposal7
retirement of a product is a gradual process. When the product manufacture
realises that there is another powerful technology or component available, in the
market which is most suitable for the production of the current product, they
will announce the current product as obsolete and the newer version/upgrade of the same is going to be
released soon. The retirement/disposition phase is the final phase in a product development life cycle
where the product is declared as obsolete and discontinued from the market. There is no meaning and
monetary benefit in continuing the production of a device or equipment which is obsolete in terms of
technology and aesthetics. The disposition of a product is essential due to the following reasons.
1. Rapid technology advancement
2. Increased user needs
Some products may get a very long ‘Life Time’ in the market, beginning from the launch phase to
the retirement phase and during this time the product will get reasonably good amount of maturity and
stability and it may be able to create sensational waves in the market (e.g. Nokia 3310 Mobile Handset;
Launched in September 2000 and discontinued in early 2005, more than 4 years of continued presence
in the market with 126 million pieces sold). Some products get only small ‘Life Time’ in the market and
some of them even fails to register their appearance in the market.
By now we covered all the phases of embedded product development life cycle. We can correlate the
various phases of the product development we discussed so far to the various activities involved in our
day-today life. Whenever you are a child you definitely have the ambition to become an earning person
like your mom/dad (There may be exceptions too ©). The need for a job arises here. To get a good job
you must have a good academic record and hence you should complete the schooling with good marks
and should perform very well in interviews. Finally you are managed to get a good job and your profes¬
sional career begins here. You may get promotions to next senior level depending on your performance.
At last that day comes—Your retirement day from your professional life.
phase of EDLC is executed in sequence. The linear model establishes a formal analysis and design
methodology with highly structured development phases. In linear model, the various phases of EDLC
are executed in sequence and the flow is unidirectional with output of one phase serving as the input to
the next phase. In the linear model all activities involved in each phase are well documented, giving an
insight into what should be done in the next phase and how it can be done. The feedback of each phase
is available locally and only after they are executed. The linear model implements extensive review
mechanisms to ensure the process flow is going in the right direction and validates the effort during a
phase. One significant feature of linear model is that even if you identify bugs in the current design, the
corrective actions are not implemented immediately and the development process proceeds with the
current design. The fixes for the bugs are postponed till the support phase, which accompanies the de¬
ployment phase. The major advantage of ‘Linear Model’ is that the product development is rich in terms
of documentation, easy project management and good control over cost and schedule. The major draw¬
back of this approach is that it assumes all the analysis can be done and everything will be in right place
without doing any design or implementation. Also the risk analysis is performed only once throughout
the development and risks involved in any changes are not accommodated in subsequent phases, the
working product is available only at the end of the development phase and bug fixes and corrections are
performed only at the maintenance/support phase of the life cycle. ‘Linear Model’ (Fig. 15.7) is best
suited for product developments, where the requirements are well defined and within the scope, and no
change requests are expected till the completion of the life cycle.
Conceptualisation
Deployment Deployment
Support
Upgrades
Retirement
1
If you closely observe this model you can see that each cycle is interconnected in a similar fashion
of a fountain, where water first moves up and then comes down, again moves up and comes down. The
major advantage of iterative/fountain model is that it provides very good development cycle feedback at
each function/feature implementation and hence the data can be used as a reference for similar product
development in future. Since each cycle can act as a maintenance phase for previous cycle, changes in
feature and functionalities can be easily incorporated during the development and hence more respon¬
sive to changing user needs. The iterative model provides a working product model with at least mini¬
mum features at the first cycle itself. Risk is spread across each individual cycle and can be minimised
easily. Project management as well as testing is much simpler compared to the .linear model. Another
major advantage is that the product development can be stopped at any stage with a bare minimum
working product. Though iterative model is a good solution for product development, it possess lots of
drawbacks like extensive review requirement at each cycle, impact on operations due to new releases,
training requirement for each new deployment at the end of each development cycle, structured and well
documented interface definition across modules to accommodate changes. The iterative/incremental
model is deployed in product developments where the risk is very high when the development is carried
out by linear model. By choosing an iterative model, the risk is spread across multiple cycles. Since
each cycle produces a working model, this model is best suited for product developments where the
continued funding for each cycle is not assured.
After the initial requirement analysis, the design for the first prototype is made, the development
process is started. On finishing the prototype, it is sent to the customer for evaluation. The customer
evaluates the product for the set of requirements and gives his/her feedback to the developer in terms of
shortcomings and improvements needed. The developer refines the product according to the customer’s
exact expectation and repeats the proto development process. After a finite number of iterations, the
final product is delivered to the customer and launches in the market/operational environment. In this
approach, the product undergoes significant evolution as a result of periodic shuttling of product infor¬
mation between the customer and developer. The prototyping model follows the approach - ‘Require¬
ments definition, proto-type development, proto-type evaluation and requirements refining’. Since the
requirements undergo refinement after each proto model, it is easy to incorporate new requirements
and technology changes at any stage and thereby the product development process can start with a bare
minimum set of requirements. The evolutionary model relies heavily on user feedback after each imple¬
mentation and hence finetuning of final requirements is possible. Another major advantage of proto-typ¬
ing model is that the risk is spread across each proto development cycle and it is well under control. The
major drawbacks of proto-typing model are
1. Deviations from expected cost and schedule due to requirements refinement
2. Increased project management
3. Minimal documentation on each prototype may create problems in backward prototype
traceability
4. Increased Configuration Management activities
Prototyping model is the most popular product development model adopted in embedded product
industry. This approach can be considered as the best approach for products, whose requirements are
not fully available and are subject to change. This model is not recommended for projects involving the
upgradation of an existing product. There can be slight variations in the base prototyping model depend¬
ing on project management.
Risk analysis^/ \
(^Reqmts. plan
Product design /
\ ^Development
\. plan
& T plan Validation and
verfication
iif Summary
i — ' — —>
✓ The Development phase transforms the design into a realisable product. The detailed specifications generated
during the design phase are translated into hardware and firmware during the development phase
V The Testing phase deals with the execution of various tests like Integration testing. System testing. User
acceptance testing, etc.
/ The Deployment phase deals with the launching of the product. Product deployment notification, Training plan
execution, product installation, Product post-implementation review, etc. are the activities performed during
Deployment phase
The Support phase deals with the operations and maintenance of the product in a production environment
The Upgrade phase of product development deals .with the development of upgrades (new versions) .for the
iU product which is already present in the market. Product upgrade results as an output of major bug fixes or feature
enhancement requirements from the end user . -. *’ V
k / Retirement/Disposal phase of a product deals with the gradual disposal of a product from the market
t / Linear or Waterfall, Iterative or incremental or fountain,Prototyping or evolutionary and Spiral models are the
commonly used EDLC models in embedded product development
v-' / The Linear or Waterfall model executes all phases of the EDLC in sequence, one after another. It is the best
suited method for product development where the requirements are fixed
. / Iterative or Fountain model follows the sequence—Do some analysis, follow some design, then some
implementation. Evaluate it and based on the short comings, cycle back through and conduct more analysis, opt
for new design and implementation and repeat the cycle till the requirements are met completely. ;
1 / Prototyping model is a variation to the Iterative model, in which a more refined prototype is produced at the end
of each iteration. It is the best suited model for developing embedded products whose requirements are not fully
available at the time of starting the project, and are subject to change
- s Spiral model is the EDLC model combining-linear and prototyping model to give the best possible risk
minimisation iaproduct development . ’fL
pw Keywords
pj
pi*j'H.^^j'5'.'fii3^lij| mmmmmwwwamam
wMSS^BSSSSSS^m^^t
»?W • •»< •*.' : .->•■- t »v*TLr; ; ..a •
I . *• ° 'tyC;)' >•ti$ :aV;Ii »•? ««» &• < ffu? • •>■: f -
;■?'■;.-■■•• s*-iSVlfJt> 'T «%£. V
1 • "^*5»r*a%sag^ifc'
J!g>?
■Ml
Ipfpt:T v £ f#KS? \-'*t,i1«<(**«i.ii*1i fiij■" ~J0&KT, T?*■ T*
Objective Questions
6. An embedded product contains LCD interfacing and a developer is assigned to work on this module. The developer
coded the module and tested its functioning using a simulator. The test performed by the developer falls under?
(a) Integration Testing (b) Unit Testing
(c) System Testing (d) Acceptance Testing
7. For an Embedded product, the requirements are well defined and are within the scope and no change requests
are expected till the completion of the life cycle. Which is the best-suited life cycle model for developing this
product? i
(a) Linear (b) Iterative (c) Prototyping (d) Spiral
8. Which of the following is(are) not a feature of prototyping model
(a) Requirements refinement (b) Documentation intensive
(c) Intensive Project Management (d) Controlled risk
9., An embedded product under consideration is very complex in nature and there is a possibility for change in
requirements of the product. Also the risk associated with the development of this product is very high. Which is
the best-suited life cycle method to handle this product development?
(a) Linear (b) Iterative (c) Prototyping (d) Spiral
Review Questions
EPILOGUE
: The embedded industry has evolved a lot from the first mass produced embedded system, Autonetics D-17, to the
the sensational gadget in the embedded device market at the time.
non,
:k industry has witnessed the improvements over processor design from the Wt processors of the 1970s to today's
system on chip and multicore processors. The.embedded operating system vendors, standards bodies, alliances, :and
open ■source communities are alsonlayinga vital role in the growth ’of Embedded domain. ' ■ '
. I hope this book-was able to give you the fundamentals of embedded system, the steps involved in their design and
development. I know this book itself is not complete in all aspects. It is quite difficult to present all the aspects of
embedded design in a single book with limited page count. Moreover, the embedded industry is the one growing at a
tremendous pace and whatever we feet superior m terms of performance and features may not be the same tomorrow.
Wait for the rest of the books planned under this senes to getan m-depth knowledge on the vanous aspects of embed¬
ded1 system design *" 1 , • j,r
/ So what next? Get your hands dirty with the design and development of Embedded Systems©. ff
'"''■Wishingyou good luck and best wishes in yoiirPew journey..;. " ' :
From the first mass produced embedded system, Autonetics D-17, to the recently launched Apple
iPhone' , the embedded industry has evolved a lot, in terms of miniaturisation, performance, features,
development cost and time to market. This book will not be complete without throwing some light into
the current trends and bottlenecks in the embedded development.
■("Apple iPhone was the sensational gadget in the embedded device market at the time of writing this book (Year 2008-09).
Introduction to Embedded Systems
system requirement, as and when they appear in the market. Even today the 8bit processors/controllers
are widely used in low-end applications. The only difference between the olden days 8bit processors/
controllers and today’s 8bit processors/controllers is that today’s 8bit processors/controllers are more
power and feature packed. In the olden days we used V number of ICs for building a device, but today,
with the high degree of integration and IP Core re-use techniques, processors/controllers are integrating
multiple IC functionalities into a single chip (System on Chip). In the olden days we required a proces¬
sor/controller, Brown out circuit, reset circuit, watch dog timer ICs, and ADC/DAC ICs separately for
building a simple Data Acquisition System, today a single microcontroller with all these components
integrated is available in the market and it leads to the concept of miniaturisation and cost saving. Em¬
bedded industry has also witnessed the architectural changes for processors. In the beginning of the
processor revolution, the speed at which an 8085 microprocessor executing a piece of code was awe¬
some to us. Because it was beyond our imaginations. As we experienced the performance enhancements
offered by general computing processors like x86, P-I, P-II, P-III, P-IV, Celeron, Centrino and Core
2 Duo, today we are unsatisfied with the performance offered by even the most powerful processor,
because we have witnessed the rapid growth in the processor technology and our expectations are sky
high today. Reduced Instruction Set Computing (RISC) and execution pipelining contributed a lot to
the improvement on processor performance. From the procedural execution, the processor architecture
moved to the single stage instruction pipelining and today processor families like ARM are supporting
8-stage pipelining (The latest ARM family ARM11 at the time of writing this book) with instruction and
data cache.
The operating clock frequency was a bottleneck in processor designs. The earlier versions of proces¬
sors/controllers were designed to operate at a few MHz. Advances in the semiconductor technology has
elevated the bar on the operating frequency limitations. Nowadays processors/controllers with operat¬
ing frequency in the range of Giga hertz (GHz) are available. Indeed, increase in operating frequency
increases the total power consumption. Another trend in the embedded processor market is the ‘Device
oriented’ design of processors. As mentioned earlier, in the beginning of the embedded revolution, we
started building our embedded products around the available processors/controllers. As embedded in¬
dustry started gaining momentum, the need for device specific processor design is flagged and processor
manufactures started thinking in that direction. Today, when we think about developing a product, say
Set Top Box or Handheld Device, we have a bunch of processors to select for the design. Intel is offering
a variety of device specific processors. Intel PXA family of StrongARM processor (Now owned by Mar¬
vell Technology Group) is specifically designed for PDA applications and Intel has an x86 version for
Set Top Box devices. The following section gives an overview of the key processor architecture/trends
in embedded development.
is a typical example for SoC targeted for multimedia applications. It integrates a powerful ARM 11 Core
and other system functions like USB OTG interface, Multimedia and human interface (Graphics Ac¬
celerator, MPEG 4, Keypad Interface), Standard Interfaces (Timers, Watch Dog Timer, general purpose
I/O), Image Processing Unit (Camera Interface, Display/TV control, Image Inversion and rotation, etc.)
etc. on a single silicon wafer. On a first look SoC and Application Specific Integrated Circuit (ASIC)
resembles the same. But they are not. SoCs are built by integrating the proven reusable IPs of differ¬
ent sub-systems, whereas ASICs are IPs developed for a specific application. By integrating multiple
functions into a single chip, SoCs greatly save the board space in embedded hardware development and
thereby leads to the concept of miniaturisation. SoCs are also cost effective.
fflie statistics shown here is based oft the data available till Dec 2008. Since processor technbiogy is undergoing rapid changes, this data
may not be relevant in future.
Introduction to Embedded Systems
from Atmel Corporation is a typical example for RSoC. It integrates an 8bit AVR processor and a dy¬
namically reconfigurable Field Programmable Gate Array (FPGA). The system is reconfigured from a
library of pre-compiled IP cores stored in a FLASH memory on a need basis. RSoCS bring the concept
of silicon sharing and thereby leads to less silicon usage and low power consumption.
iJ
16.2 EMBEDDED OS TRENDS
The Embedded OS industry is also undergoing revolutionary changes to take advantage of the potential
offered by the trends in processor technologies. Today we have lot of options to select from a bunch
of commercial and open source embedded OSs for building embedded devices. Most of the embed¬
ded OSs are trying to bring the virtualisation concept to the embedded device industry by adopting the
microkernel architecture in place of the monolithic architecture. In microkernel architecture, the kernel
contains very limited and essential features/services and rest of the features or services are installed as
service and they run at the user space. Another noticeable trend adopting by OS suppliers is the ‘Device
oriented’ design similar to the processor trends. In the olden days for building a device, we have to se¬
lect an OS matching the device requirements and then customise it. Today OS suppliers are providing
off-the-shelf OS customised for the device. Microsoft Embedded OS product line is a typical example
of this, Microsoft has a range of OSs targeted for a group of device families like point of sale terminal,
media player, set top box, etc. The Windows Mobile OS from Microsoft is specifically designed for
mobile handsets. Open source embedded OS (Mostly centered around Embedded Linux) are gaining
attention in the embedded market and the open source community is also offering off the shelf OS cus¬
tomised for specific group/family of devices. The Ubuntu MID edition which is specifically designed for
Mobile Internet Device (MID) is a typical example Tor this. Since the processor technology is shifting
the paradigm of computing from single core to multicore processors, the OS suppliers are also forced
'to change their strategies for supporting multicore processors. The latest version of the VxWorks (Vx-
,Works 6.6SMP^) RTOS is designed to support the latest market-leading multicore processors.
language in hardware access and the bunch of cross-compilers and IDEs available for different plat¬
forms. We lived in an era where imagining a language beyond ‘C’ and Assembly (ALP) for embedded
firmware was impossible. Now the scenario is totally changed. We have object-oriented languages like
C++ for embedded firmware development. Efficient cross platform development tools and compilers
from providers like Microsoft® played a significant role in this transformation. Advances in the com¬
piler implementations and processor support brings java as one of the emerging language for system
software development. Though java lacks lot of features essential for system software development, a
move towards improving the missing factors is in progress. Still it is in the infancy stage. We will dis¬
cuss the java based development under a separate thread. When it comes to embedded software develop ¬
ment, a bunch of languages, including ‘C’, ‘C++’, Microsoft C#, ASP.NET, VB, Java,,etc. are available.
The following sections illustrate the role of Java and Microsoft .NET framework supported languages
for embedded development.
Ja\a Applications
_ _ “
Embedded OS (RTOS/GPOSJ
/ / ■” ■ f',<? 'TaC'H'Ax- -A'C V'S'A'A- A-.
:*J' 1 C . \ . ", * .■ ~ 1
Embedded Hardware
As mentioned earlier, JVM interprets the bytecode and thereby greatly affects the speed of execution.
Another technique called Just In Time (JIT) compiler speeds up the Java program execution by cach¬
ing all the previously interpreted bytecode. This avoids the delay in interpreting a bytecode which was
already interpreted earlier.
Introduction to Embedded Systems
16.4.3 Android
Android is an open source software platform and operating system for mobile devices. It was developed
by Google Corporation (www.google.com) and retained by the Open Handset Alliance (OHA). The
Android operating system is based on the Linux kernel. Google has developed a set of Java libraries
and developers can make use of these libraries for application development in java. For more details on
android, please visit the website http://www.android.com/
16.4.4 Openmoko
Openmoko is a project for building open hardware and firmware (OS) standards for a family of open
source mobile phones. The target operating system under consideration by openmoko is Embedded
Linux and it is known as Openmoko Linux. It also supplies the hardware details (schematics and CAD
drawings) of the phone as reference design for developers. Developers can customise the software stack
and hardware to suit their product needs. For more details on Openmoko, please visit the website http://
wiki.openmoko.org/wiki/Main_Page
PIC® is a popular 8/16/32 bit RISC microcontroller family from Microchip Technology (www.microchip.corn). The 8bit
PIC family comprises the products PIC 1 OF, PIC12F, PIC16F and PIC18F. They differ in the amount of program memory
supported, performance, instruction length and pin count. Based on the architecture, the 8bit PIC family is grouped into
three namely;
Baseline Products based on the original PIC architecture. They support 12bit instmction set with very limited fea¬
tures. They are available in 6 to 40 pin packages. The 6 pin 10F series, some 12F series (8 pin 12F629) and some 16F
series (The 20-pin 16F690 and 40-pin 16F887) falls under this category.
Mid-Range This is an extension to the baseline architecture with added features like support for interrupts, on-chip
peripherals (Like AZU converter, Serial Interfaces, LCD interface, etc.), increased memory, etc. The instruction set for
this architecture is Mbit wide. They are available in 8 to 64 pin packages with operating voltage in the range 1.8V to
5.5V. Some products of the 12F (The 8 pin 12F629) and the 16F (20-pin 16F690 and 40-pin 16F887) series comes under
this category.
High Performance The PIC 18F J and K series comes under this category. The memory density for these devices
is very high (Up to 128KB program memory and 4KB data memory). They provide built-in support for advanced periph¬
erals and communication interfaces like USB, CAN, etc. The instmction set for this architecture is 16bit wide. They are
capable of delivering a speed of 16MIPS.
As mentioned earlier we have a bunch of devices to select for our design, from the PIC 8bit family. Some of them have
limited set of I/O capabilities, on-chip Peripherals, data memory (SRAM) and program memory (FLASH) targeting low
end applications. The 12F508 controller is a typical example for this. It contains 512 x 12 bits, of program memory, 25
bytes of RAM, 6 I/O lines and comes in an 8-pin package. On the other hand some of the PIC family devices are feature
rich with a bunch of on-chip peripherals (Like ADC, UART, I2C, SPI, etc.), higher data and program storage memory,
targeting high end applications. The 16F877 controller is a typical example for this. It contains 8192 x 14 bits of FLASH
program memory, 368 bytes of RAM, 256 bytes of EEPROM, 33 I/O lines, On-chip peripherals like 10-bit A/D converter,
3-Timer units, USART, Interrupt Controller, etc. It comes in a 40-pin package.
Here, the 16F877 device is selected as the candidate for illustrating the generic PIC architecture. Figure ATI illus¬
trates the architectural details of PIC 16F877 device.
The 16F877 PIC family devices contain three types of memory namely; FLASH memory for storing program code,
data memory for storing data and holding registers and EEPROM for holding non-volatile data. PIC follows the Harvard
architecture and thereby contains two separate buses for program memory and data memory fetching. The data bus is
8bit wide and program memory bus is Mbit wide. The Program Counter is Mbit wide and is capable of addressing 8K
Introduction to Embedded Systems
RAO to RA5 RBO to RB7 RCO to RC7 RDO to RD7 REO to RE2
program memory locations. The On-chip FLASH memory is partitioned into pages of size 2K bytes each. The program
counter is formed by two register, an 8b it register PCL and a 5bit register PCH. PCL is a read/writable register whereas
PCH is not directly accessible. PCH can be written indirectly through the register PCLATH. A write to the PCLATH
register copies the least significant 5 bits to PCH. The W register holds the result of ah operation performed in the ALU.
The data memory contains the general purpose and special function registers and it is partitioned into multiple banks.
Each bank contains 128 bytes. The special function registers are located at the lower memory address area of each bank.
The working RAM is implemented as general purpose registers. The data memory bank is selected by setting the memory
bank selector bits RPI and RPO in the STATUS register. The RPO, RP1 combinations for selecting different jlata memory
banks is given in the following table.
Overview of PIC and AVR Family of Microcontrollers and ARM Processors
General purpose registers are the memory locations available to users for data storage. They can be accessed directly
or indirectly through the File Select Register (FSR). Special Function Registers are used by the CPU and peripherals for
configuration, status indication, data association, etc. They are located in the lower memory area of a memory bank. The
size of the SFR memory varies across device families and the typical size for 16F877 is 96 bytes. The status of arithmetic
operations and reset along with the data memory bank selector bits RPO and RP1 are held in the STATUS register. The
bit details of the status register is explained below.
wm.
The stack of 16F877 is an 8 level deep 13bit wide hardware stack. The stack pointer register which holds the current
address of the stack is not readable and writeable. The stack space for PIC is an independent implementation and it is not
part of either data memory or program memory. The program counter PC is PUSHed to the stack on execution of a CALL
instruction or when an Interrupt is occurred. The Program Counter is POPed on the execution of a Return instruction
(RETURN or RETLW or RETFIE). The stack operates as a circular queue. When all the 8 locations are PUSHed, a 9th
PUSH operation stores the pushed data at the start of the stack.
The indirect addressing of data memory is accomplished by the registers INDF and File Select Register (FSR). INDF
is a virtual register. Any instructions using the INDF register internally accesses the FSR register. In indirect addressing
the memory bank is indicated by the IRP bit of the STATUS register and the 7th bit (In 0 to 7 numbering) of FSR. The
memory location within the memory bank is pointed by the bits 0 to 6 of FSR register.
16F877 contains five bi-directional ports namely; PORTA, PORTB, PORTC, PORTD and PORTE. Ports B, C and D
are 8bit wide, whereas Port A is 6bit wide and Port E is 3bit wide. Each Port associates a data direction register called
TRISx (x= A for PORTA, B for PORTB, C for PORTC, D for PORTD and E for PORTE). It sets the Input or Output
mode for the port. If the TRISx.n bit (where ‘n’ is the port pin number, 0 to 5 for Port A, 0 to 7 for Port B, C and D, and 0
to 2 for Port E) is set, the corresponding port pin configures as Input Port. PORTx (x = A or B or C or D or E) is the data
register associated with each port. Some of the ports have alternate functions associated with them. The alternate func¬
tions are enabled by setting the corresponding bit in the register controlling the alternate functions for a Port. It should
be noted that TRISx is the register deciding I/O direction even when the port is operating in the alternate function mode.
The number of ports available in a device is device family specific.
The PIC 16F877 supports 14 interrupt sources. The interrupt control register INTCON holds the global and individual
interrupt enable bits and bits for recording the status of all interrupts. 16F877 supports only one Intemipt vector for
/
Introduction to Embedded Systems
servicing all the interrupts. The interrupt vector is located at code memory location 0004H. The trigger type for external
interrupts can be configured as either rising edge or falling edge, in the OPTION_REG by setting or clearing the INTEDG
flag respectively. RETFIE is the instruction for returning from an interrupt service routine.
A watchdog timer is implemented to keep track of the proper execution of instructions. The watchdog timer is imple¬
mented as a free running RC oscillator. Watchdog timer doesn’t require an external clock for its operation. During normal
operation, a watchdog timeout resets the controller, whereas if the device is in the SLEEP mode, the watchdog timeout
wakes up the CPU and put the controller in the normal operation mode. The watchdog timer can be enabled or disabled
by setting or clearing the WDTE bit of the CONFIGURATION WORD register.
The 16F877 device undergoes reset if any one of the following conditions is met:
Power on Reset (POR) The power on reset signal is generated internally when the voltage supply to the device
reaches in the range 1,2V to 1 ,7 V when it is raised from OV. In order to ensure a proper voltage level and stabilised clock,
for proper operation of the device, some delays are introduced upon the detection of a POR. The Power up Timer (P WRT)
is started on detecting a POR. PWRT runs for a period of 72 ms allowing the supply voltage to stabilise. When PWRT
expires, the Oscillator Start-up Timer (OST) is started when PWRTs time delay expires. OST provides a delay of 1024
oscillator periods. This ensures that the crystal resonator for the oscillator circuit is properly stabilised before the code
execution begins.
Brown-out Reset (BOR) BOR ensures that the program execution flow is not corrupted when the supply voltage
falls below the threshold value for proper operation of the device. Whenever the supply voltage falls below this thresh¬
old, the BOR holds the device under reset till the voltage raises above the threshold value. The Brown-out Reset can be
enabled by setting the bit BODEN. Brown-out reset follows the same sequence of operation for PWRT and OST as in the
case of the Power on Reset.
Watchdog Reset Watchdog reset occurs when the watchdog timer overflows (if the watchdog timer is in the
enabled state). Watchdog reset can happen either in the normal operation mode or when the controller is in the SLEEP
mode. During normal operation, a watchdog timeout resets the controller, whereas if the device is in the SLEEP mode, the
watchdog timeout wakes up the CPU and put the controller in the normal operation mode. The Watchdog reset in the nor¬
mal operation mode follows the same sequence of operation for PWRT and OST as in the case of the Power on Reset.
External Reset The controller can be brought into the reset state at any time during program execution by applying
a reset pulse at the RESET pin (MCLR\). Similar to watchdog timer reset, external reset can happen either in the normal
operation mode or when the controller is in the SLEEP mode. During normal operation, the external reset assertion resets
the controller, whereas if the device is in the SLEEP mode, the external reset wakes up the CPU and put the controller in
the normal operation mode. The external reset in the normal operation mode follows the same sequence of operation for
PWRT and OST as in the case of the Power on Reset.
PIC microcontrollers support power saving, by executing the SLEEP instruction. During SLEEP mode, the watchdog
timer can be kept either in the enabled or disabled. If the watchdog timer is in the enabled state, the controller resumes its
operation when the watchdog timer expires. During SLEEP mode, the I/O pins maintain their status at the time of entering
the SLEEP state. The device is wakeup from the SLEEP mode by one of the following events.
1. An external reset signal at pin MCLR\
2. Watchdog timer expiration
3. Occurrence of external Interrupts, port change interrupt or peripheral interrupt
16F877 contains three timers/counters namely Timer 0, 1 and 2. Timer 0 can act as an 8bit timer/counter with internal
or external clock. Timer 0 interrupt is generated when the timer count rolls over to 00H from FFH. Timer 0 mode can be
selected by the TOCS bit of the OPTIONREG register. In the timer mode, Timer 0 increments on every instruction cycle
(If the timer pre-scaler is not set). Timer 1 is a 16bit timer/counter (TMR1) consisting of two 8bit registers (TMR1H and
TMR1L). Timer 1 can function as either a timer or counter. In counter mode, timer 1 counts the external pulses occurring
at the corresponding I/O pin. Timer 2 is an 8bit timer which is specially designed for generating Pulse Width Modulation
(PWM) signals. Timer 2 generates the PWM time base for Capture Compare PWM module (CCP) 1 and 2. Timer 2 as¬
sociates a prescaler and a postscaler.
The Capture Compare PWM Modules (CCP) are associated with PWM signal generation and each CCP module con¬
tains a 16bit register which acts as either 16bit capture register or 16bit compare register or 16bit PWM master/slave duty
cycle register.
Overview of PIC and AVR Family of Microcontrollers and ARM Processors
The Parallel Slave Port (PSP) acts as a slave port which can be directly interfaced to the 8bit data bus of an external
microprocessor/controller. Port D operates as the Parallel Slave Port. In PSP mode, the external microprocessor can read
or write the PORTD latch.
The Universal Synchronous Asynchronous Receiver Transmitter (USART) acts as the serial communication interface
(SCI) for the device. It supports full duplex asynchronous serial data transfer and half duplex synchronous serial data
transfer. In synchronous half duplex commrmication, the USART can act as either master or slave. The baudrate for serial
data communication is configurable.
Master Synchronous Serial Port (MSSP) or Synchronous Serial Port (SSP) is another serial interface supported by the
16F877 device. MSSP can operate as either Serial Peripheral Interface (SPI) or Inter Integrated Circuit (I2C) interface.
The SPI mode requires minimum 3 wires and I2C requires minimum 2 wires for communication.
The 16F877 family device contains an on-chip Analog to Digital Converter ADC for analog signal conversion. The
resolution of the ADC is lObit. The ADC is controlled by the ADC register ADCONO and the register ADCCON1 con¬
figures the port pin used for supplying Analog input signal.
PIC 24 and dsPIC series are the 16bit PIC microcontrollers and PIC32 is the 32bit PIC microcontroller from micro¬
chip. MPLAB from Microchip Technologies is the Integrated Development Environment for the PIC family of microcon¬
trollers.
tinyAVR® Smallest version of 8bit AVR microcontroller. Limited pin counts. It contains up to 8K bytes of internal
FLASH program memory and 512 bytes of SRAM and EEPROM. The tinyAVR® microcontrollers are commonly used
for general purpose applications. _
megaAVR® High performance AVR microcontroller with hardware multiplier. It contains up to 256K bytes of in¬
ternal FLASH program memory and 8K bytes of SRAM and 4K bytes of EEPROM.
'TNI
XMEGA High performance AVR microcontrollers with advanced peripherals like DMA and event systems.
AVR microcontrollers are also available as application oriented controllers specifically designed for certain applica¬
tions like Automotive (Automotive AVR), CAN networking (CAN AVR), LCD driver (LCD AVR), Motor control, light¬
ing applications (Lighting AVR), ZigBee, Remote Access Control, USB connectivity, etc. The AVR microcontroller may
contain built-in peripherals like ADC, UART, SPI bus interface, etc. Figure A 1.3 illustrates the architecture of the AVR
ATmega8 Microcontroller.
The AVR 8 CPU contains an 8bit ALU, 32, 8bit general purpose registers, Stack pointer, program counter, instruction
register, instruction decoder, and status and control register.
AVR follows the ‘Harvard’ architecture and supports separate memories and buses for program and data. AVR imple¬
ments single level ‘pipelining’ with pre-fetching of instruction while an instruction is being executed.
All the 32 registers are directly connected to the ALU and it allows the accessing of two independent registers in one
single instruction execution. The register access time is one clock cycle. Out of the 32 registers, 6 can be used as three
16bit indirect address registers. These 16bit registers are known as ‘X’, ‘Y’ and ‘Z’ registers. The address map of the
general purpose registers are shown below in Fig. A 1.2.
The higher 6 registers can be grouped into three 16bit registers as shown in the register map. Registers R26 and R27
forms the X register with lower order byte of X register in R26. Similarly registers R28 and R29 forms the Y register
with lower order byte of Y register in R28. Register Z is formed by registers R30 and R31 with the lower order byte of Z
register in R30. These registers are used as indirect addressing registers in data memory access and lookup table access
registers for FLASH memory.
The Stack pointer register is a 16bit register holding the current address of the stack. For AVR architecture, the stack
memory grows down (From a highest memory address to lower address). The stack is implemented in the SRAM mem¬
ory area and the stack pointer points to the current location of the stack in the SRAM. The reset value or stack pointer is
Introduction to Embedded Systems
da ‘ ; .
JtvU
OOOOH. It should be initialised to a value higher than 0060H for proper functioning. Whenever a data byte is ‘PUSHed’
to the stack, the stack pointer register is decremented by one. Whenever a data byte is ‘POPed’ from the stack, the stack
pointer register is incremented by one. This is contradictory with the 8051 stack operation where the stack is located at
the lowest address and it grows up. The stack pointer register is implemented as a combination of two eight bit registers
namely Stack Pointer High (SPH) and Stack Pointer Low (SPL). Both the registers are required only if the size of the
SRAM is greater than 256 bytes. For AVR processors with SRAM less than 256 bytes only the SPL register is physically
implemented see Fig. A1.3.
The status register SREG holds the status of the most recently executed arithmetic instruction in the ALU. The bit
details of the status register is explained below.
B7
:u,\
f .t "■ H. s
The table given below explains the meaning and use of each bit.
- ~ < ... ..
earn generated by the arithmetic or
Sign Flag ,
'f w '> '’ '' '> '< '' " ” T " 'r '' T ▼▼▼▼▼▼▼▼ '< 'r 'r ,r ,r 'r V ''
/ ( H^iPort B drivers Port C drivers Purt D drivers
? W^F-.T-■' •!
.MjfillJ*
ttit. ■
Port A digital interface j ' j Port B digital interface | j. Port C digital interface, . . | fort D,digital interface
Instruction decoder
mmwm is mm
/
iBSl1,
V; ;V>A - j t
A.
T I
r
Progran
■: ior.'c ■
AYR CPU
v:,v,3k:±/
TW1
Debug interface [
JT AG/1-Wire
(Fig.A1.3)
AVR supports a number of interrupts including AVR CPU specific and additional On-chip hardware specific (These
are implemented only if the microcontroller integrates the hardware in it. Examples are SPI interrupt). The table given
below summarises the various interrupts supported by ATmegaS and their interrupt vectors.
address ; / .
0000H RESET Microcontroller Reset occurred
, 23- v Cv:- 000111 AINTO ^:0&r|i3>As;Extemal Interrupt 0 , .. ' ./ \: -.
, 0002H INTI External Interrupt 1 ' " ,
4-1-—-——Pee3H- - .u TJMER2COMP -T ■> 'Timer/Counter2compare match -
Introduction to Embedded Systems
lllfe
- 0006H TIMER 1 COMPA Tinter/iGoujiter 1 compare match A
” f t . 01
If the BOOTRST fuse of the AVR microcontroller is programmed, the reset vector is shifted to the bootloader start
address, The interrupt vector address can be relocated to the start of the bootloader FLASH memory by setting the inter¬
rupt vector select (IVSEL) bit of the Interrupt Control register GICR. The reset address and the Interrupt vector address
for the various combinations of interrupt vector select (IVSEL) and BOOTRST and the corresponding reset address and
interrupt vector location are listed in the table given below.
The bit details of the general interrupt control register GICR is explained below.
B7 B6 B5 B4 • -B3 *v B2 B1 B0
INTI INTO RSD RSD RSD RSD IVSEL IVCE
As mentioned earlier, the bit IVSEL controls the relocation of Interrupt vector addresses to the bootloader area of
FLASH memory. The Interrupt Vector Change Enable enables the relocating of interrupt vectors. In order to enable the
interrupt relocation, the following sequence of operation should be performed on IVCE & IVSEL.
1. Set IVCE-
2. Within 4 machine cycles of setting JVCE, write the desired value to IVSEL (I for interrupt vector relocation en¬
abling and 0 for disabling interrupt vector relocation) along with a value of 0 for IVCE flag.
Interrupts are automatically disabled during the setting up of IVCE and for 4 machine cycles following the instruc¬
tion setting up JVCE. If IVSEL is not modified within this 4 machine cycles, interrupts are automatically re-enabled. The
global interrupt enabler bit in the status register SREG remains unchanged.
The reset of AVR can happen in four ways. They are:
1. Power ON Reset: The microcontroller is reset when the supply voltage is below the Power ON Reset threshold
value
2. External Reset: The reset pin of the microcontroller is held low for a specified period.
3. Watchdog Reset: The watchdog Timer is expired. It happens due to flaws in program execution.
4. Brown Out Reset: The supply voltage to the microcontroller falls below a specified threshold. This is essential for
preventing data corruptions and unexpected program execution.
Overview of PIC andAVR Family of Microcontrollers and ARM Processors
During reset, all I/O registers are set to their initial values; all ports are reset to their initial states. When the reset
sources go inactive, a delay counter is started to wait for a specified period set by the CKSEL fuses of the device. It
ensures the proper stabilisation of the power source before the start of program execution. The program execution starts
at either the rest vector (OOOOH) or the Reset address of Boot FLASH depending on the configuration of the BOOTRST
fuse. The MCU Control and Status Register MCUCSR keep track of the source of reset. The bit details of this register are
shown below.
H7
v ‘
B5 'kX<4i
WDRF is the flag corresponding to a watchdog initiated reset and BORF flag indicates a reset triggered by Brown Out
detection (The operating voltage goes beyond a threshold). If the reset is initiated by the external RESET line, the EXTRF
flag is set. A power On reset sets the PORE flag.
Watchdog timer is a mechanism to bring the controller out of a system hang-up condition. Unexpected program flow
may result in unexpected behaviour and thereby leading to an illusion of total system hang-up. This can be prevented by
using a watchdog timer to monitor the time taken to execute the code segment, which is prone to create system hang-up
behaviour.
The AVR watchdog timer unit contains a built in watchdog timer oscillator which runs at a separate on-chip oscillator
at a frequency of 1MHz. The watchdog reset interval can be adjusted by changing the pre-scaler values of the watchdog
timer. The watchdog timeout event triggers a reset when the watchdog timer exceeds the time period set. The Watchdog
Timer Control Register (WDTCR) controls the operation of the watchdog timer. The bit details of the watchdog timer
register are given below.
:
B7 . ■/
.»-■ -v.<. •
B6 B5 B4
i«3^ B: MB
it'.hivCsSsffc
BO
IfS
The bits, WDPO, WDPl and WDP2 set the oscillator cycles in which the watchdog timer expires (sets the watchdog
timer pre-scaler). It varies from 16K oscillator cycles (WDPO, WDP1, WDP2 = 000) to 2048K oscillator cycles (WDPO,
WDPl, WDP2 =111). The bit WDE controls the enabling and disabling the watchdog timer. Setting it to 1 starts the
watchdog timer. The bit, Watchdog Change Enable, controls the disabling of the Watchdog timer. In order to disable a
watchdog timer this bit should be set before clearing the watchdog enable bit WDE. Writing a 0 to the Watchdog Enable
bit takes effect only when the WDCE bit is at logic 1. Before starting the watchdog timer, it should be resetted using the
instruction WDR.
Apart from the general purpose registers, AVR holds various status, control and configuration registers for config¬
uring, controlling and providing feedback on various operations like Port setting, initialisation, read write, watch dog
timer management, interrupt management, serial data communication, etc. These registers are known as special function
registers. These registers fall under the I/O space and they are accessed by the I/O access instructionsIN and OUT. I/O
Registers within the address range OxOO-OxlF are directly bit accessible using the SBI and CBI instructions. In these reg¬
isters, the value of single bits can be checked by using the SBIS and SBIC instructions. If the I/O registers are addressed
as data space using the LD and SD instructions, add 0x20 to these addresses.
The external interrupts INTO and INTI are configurable for either level triggering or edge triggering. This can be
configured by setting or clearing the Interrupt Sense Control bits of the MCU Control Register (MCUCR). The bit details
of this register are shown below.
B7 B6 B5 . v B4 >, r m, . . r .
Bits ISC11, ISC10 configures the external interrupt INTI and bits ISC01, ISC00 configures the external interrupt
INTO. The various settings for the bits ISC1 and ISC0 and the corresponding interrupt triggering conditions are listed
below.
-b ■ i-i;; l V: ; /;/ A ty ,■ _ -. c i r --“K L-*. ; in: ■ :.t: -;n :: c *3 le:st k- f;-.7.NT/; xtfi iMy JigT1 jf" -
The AVR mega architecture supports multiple bidirectional ports (Up to 4 Ports: Port A, Port B, Port C and Port D)
with configurable pull-up option. The ports can be configured as either input port or output port. Each port contains a
group of port pins (up to 8 pins). Port pins are internally pulled to logic ‘High’ using pull-up resistors. All I/O pins have
protection diodes to both +ve supply voltage and ground. Three I/O mapped registers are associated with each port. They
are:
Data. Direction Register (DDRx) The data direction register associated with Port x (where x can be A or B or
C or D). If the bit corresponding to a port pin is set as 1, the port pin is configured as output pin. If the bit is set as 0, the
corresponding pin of the port is configured as input pin.
Data Register (PORTx) The data register associated with Port x (where x can be A or B or C or D). It is used for
holding the data to output to the port when it is configured as an Output port. When the Port is configured as I/p port, the
Data register is used for turning ON and OFF the pull-up resistors associated with each pin of the port.
Port Input Pin (PINx) This register holds the Pin status.
Three register bits namely DDx« (bits in (DDRx Register), PORTxm and PINxw (where x - A or B or C or D and n =
0 to 7) are associated with each port pin. The DDxn bit selects the direction of the port. A value of 1 sets the port pin as
Output port and a value of 0 sets the port pin as input port. If PORTxn is set at logic 1 when the DDxn for the correspond¬
ing port pin is configured as input pin, the Pull-up associated with the port pin is activated. The pull up associated with
the pin can be turned Off by writing a logic 0 to the corresponding PORTxn register bit or by setting the Pin as Output
pin. If port pin PORTxa is written with logic 1 when the DDxn pin of it is configured as Output pin, the port pin is driven
‘High’. The port pin is driven ‘Low’ in the output mode by writing a 0 to the PORTxn register bit.
Apart from bi-directional I/O operation, some of the port pins support alternate functions. The alternate functions for a
port pin can be enabled by overriding the port pin I/O functionality with alternate functions. These overriding is enabled
by setting the override bit corresponding to the alternate function in the corresponding special function register holding
the override bit. The override value can be mentioned in the corresponding bit. Hence for each alternate function, two bits
are required; 1 for enabling the override and the other one for setting the override value.
The mega8 AVR supports two 8bit Timers/Counters and a 16bit Timer/Counter. Timer/counter 0 can operate in any
one of the modes; Single Channel Counter, Frequency Generator, External Event Counter and 10-bit Clock pre-scaler:
The clock to the timer unit is supplied via the internal clock or via a clock prescaler or via an external clock source on
the port pin TO. The Timer/Counter is incremented at each clock tick. The counting happens in the upward direction and
the Timer/Counter register overflows to 00H on next clock pulse when the count value is maximum, i.e. OFFH. Whenever
a Timer/Counter overflow occurs, it is indicated through the flag corresponding to the timer.
- Each 8bit timer V (x = 0 or 2) associates the following registers with it for operation
1. A Timer/Counter Control Register (TCCRx) for selecting the clock source
2. A Timer/Counter Register (TCNTx) for holding the count
3. A Timer/Counter Interrupt mask register (TIMSK). It contains the bit TOIEx for individually enabling Or disabling
the Timer/Counter x interrupt.
4. Timer/Counter Interrupt Flag Register (TIFR). It contains the bit TOVx for indicating the overflow of Timer/Coun¬
ter x. It is cleared by hardware when the interrupt vectors to its ISR.
The 16bit timer (Timer/Counter 1) serves as Pulse Width Modulation (PWM) generator/Frequency generator and
external event counter.
AVR has a built-in USART for full duplex synchronous or asynchronous serial data communication. It supports Serial
Frames with 5, 6, 7, 8, or 9 data bits and 1 or 2 Stop Bits, with hardware based odd or even parity generation and parity
check. Port D bit 0 and bit 1 when configured in the alternate function mode serves as the Receive Pin and Transmit Pin
respectively for serial data communication. The USART Transmitter has two flags namely; USART Data Register Empty
Overview of PIC and AVR Family of Microcontrollers and ARM Processors
(UDRE) and Transmit Complete (TXC) for indicating its state. The flag UDRE is set when the transmit buffer is empty.
The flag TXC is set upon transmission of data. Both of these flags can be used for generating interrupts. Similarly the
USART receiver has one flag named Receive Complete (RXC) for indicating the availability of data in the receive buf¬
fer.
AVR microcontroller contains a special program called bootloader located in the FLASH memory. It has the capabil¬
ity to load program into the FLASH memory area of the controller. It facilitates firmware upgrades. The bootloader is
self modifiable, and it can also erase itself from the code if it is not required anymore. The bootloader memory size is
configurable with fuses and it has two independently configurable boot lock bits for locking the bootloader for security
reasons. The bootloader program can be configured to execute either on .power on reset (If the BOOTRST fuse of the
AVR microcontroller is programmed, the reset vector is shifted to the bootloader start address) or on receiving a specific
command from a host PC through the USART or SPI interface.
The AVR architecture incorporates multiple levels of power saving. The different levels correspond to a power sav¬
ing mode and the device should put in the corresponding mode to take advantage of the power savings. The power sav¬
ing mode is activated by executing the SLEEP instruction. The power saving mode is set in the MCU Control Register
(MCUCR) before executing the SLEEP instruction. The details of the MCUCR bits associated with power saving is
illustrated below.
g5 l,- .2 BO
SM2 SMI SMO ISC 1.1, A ISC 10 ISC01 ISC00
The Sleep Enable (SE) bit enables the CPU to enter sleep state on executing a SLEEP instruction. Bits SMO, SMI and
SM2 sets the power saving' mode. The different power saving modes supported by AVR are listed below.
Idle Mode This mode is set by configuring the bits SM2:SM1:SM0 as 0:0:0 with SE= 1. The clock to CPU is frozen.
CPU stops execution. Other units like SPI, USART, Analog Comparator, ADC, Two-wire Serial Interface, Timer/Coun¬
ters, Watchdog, and the interrupt system continue operational. The Idle mode is terminated by an externally or internally
triggered interrupt like external interrupts, Timer Overflow and USART Transmit Complete interrupts and Analog Com¬
parator interrupt, etc. _
ADC Noise Reduction Mode This mode is set by configuring the bits SM2:SML.SM0 as 0:0:1 with SE = 1.
The clock to I/O system, CPU is frozen. CPU stops execution. Other units like ADC, external interrupts, two-wire Serial
Interface address watch, Timer/Counter2 and the Watchdog timer (if enabled) continue operational. This mode is termi¬
nated by an ADC Conversion Complete interrupt, or an External reset, or a Watchdog Reset, or a Brown-out Reset, or a
Two-wire Serial Interface address match interrupt, or a Timer/Counter2 interrupt, or an SPM/EEPROM ready interrupt,
or an external interrupt (INTO or INT1)
Power down Mode This mode is set by configuring the bits SM2:SM1 :SM0 as 0:1:0 with SE = 1. The external
oscillator is halted and stops all internally generated clocks. All modules using the generated clock are frozen. External
interrupts, Two-wire Serial Interface address watch, and the watchdog continue operational. The power down mode is
terminated by an External Reset, or a Watchdog Reset, or a Brown-out Reset, or a Two-wire Serial Interface address
match interrupt, or an external level interrupt*
Power save Mode This mode is set by configuring the bits SM2:SM1:SM0 as 0:1:1 with SE = 1. This mode is
same as Power down mode except that Timer/Counter 2 continue runs in this mode if it is clocked asynchronously. Along
with the power down mode termination conditions, a Timer Overflow or an Output Compare event from Timer/Counter2
also triggers the termination of this mode.
Standby Mode This mode is set by configuring the bits SM2:SM1 :SM0 as 1:1:0 with SE = 1 and when the external
clock is used. The device wakes up from the standby mode in 6 clock cycles.
The size of on-chip FLASH memory, SRAM and EEPROM varies across different members of the AVR family. AVR
8 family supports FLASH memory up to 256KB, EEPROM from 64 bytes to 4KB and SRAM up to 8KB. Also the pres¬
ence of on-chip peripherals like ADC, Two Wire Interface (TWI), SPI, etc. varies across the family members.
AVR normally does not support the execution of program from external program memory. The code is always ex¬
ecuted form the internal FLASH memory.
Introduction to Embedded Systems
AVR provides built in On-Chip debug support in the form of either JTAG interface or Debug WIRE (1-Wire) interface.
All AVR mega family devices with 16KB or more FLASH memory supports built in JTAG interface for On-chip debug¬
ging and In System Programming. All new AVR mega family devices with FLASH memory less than 16KB supports the
1-Wire debug interface.
Please refer to the website http://www.atmel.com/dvn/resources/prod_documents/doc0856.pdf for details on the In¬
struction set for 8bit AVR.
AVR® 32 is a 32bit microcontroller family from Atmel featuring Single Instruction Multiple Data (SIMD) and DSP
instructions. It also supports audio and video processing features.
SgHMr* *
INTRODUCTION TO ARM® FAMILY OF
Advanced RISC Machines (or ARM) is a powerful 32bit RISC processor from ARM Holdings (;www.arm.comL ARM
holdings is a joint venture between Acom Computers (http://www.acomcomputers.co.uk/). Apple Computers (www.
apple.conf) and VLSI Technology ('www.vlsi.com www.nxp.com) and is founded in 1990. ARM Holdings is the Intel¬
lectual Property (IP) supplier of ARM processor core and it doesn’t manufacture ARM processor chips. ARM designs
the ARM processor core and licences its ARM IP to their networked partners. The partners can utilise the ARM IP for
building System On Chip devices. Freescale semiconductors, Texas Instruments, NXP (Philips) Semiconductors, etc. are
some of the channel partners of ARM, which fabricates ARM processors. Please visit the ARM website http://www.arm.
com/community/all_partners.php for the complete list of ARM community partners.
The ARM architecture has evolved a lot from its first version ARM1 to the latest ARM 11 Processor core. ARM1,
ARM2, ARM3, ARM 4&5, ARM6, ARM7, ARM8, StrongARM, ARM9, ARM10 and ARM 11 are the product families
from ARM since its introduction to the market. Some ARM processor extensions like Thumb, El Segundo, Jazelle, etc.
are also introduced by ARM during its journey from ARM 1 to ARM11.
ARM is a RISC processor and it supports multiple levels of pipelines for instruction execution. ARM contains thirty-
seven 32bit registers. Out of the 37 registers 30 are general purpose registers and 16 general purpose registers (Registers
rO to r 15) are available in the user mode of operation. Register rl 5 is the program counter (pc). Register rl 3 functions as
the stack pointer register (sp). Register r 14 is used as link register (lr) for the branch and link instructions. The Current
Program Status Register (CPSR) holds execution status of the processor, processor operation mode, interrupt enable bit
status, etc. The details of the bits associated with the CPSR register is illustrated below.
User Mode It is the main execution mode for user applications. This mode is also known as ‘Unprivileged Mode’.
It enables the protection and isolation of operating system from user applications.
Fast Interrupt Processing (FIQ) Mode The processor enters this mode when a high priority interrupt is
raised.
Normal Interrupt Processing (IRQ) Mode The processor enters this mode when a normal priority inter¬
rupt (Interrupts other than high priority) is raised.
Overview of PIC and AVR Family of Microcontrollers and ARM Processors
Supervisor/Software Interrupt Mode The processor enters this mode on reset and when a software inter¬
rupt instruction is executed.
Abort Mode Enters this mode when a memory access violation occurs
Undefined Instruction Mode Enters this mode when the processor tries to execute an undefined instruction.
System Mode This mode is used for running operating system tasks. It uses the same register as the User mode.
All the above modes, except User mode, are privileged modes. The privileged mode uses several other registers than
the registers available in the user mode. Figure A1.4 illustrates the register organisation for the various modes.
rO
SHE
1 W-
. r3
L :■1 - * - r4
r9*
j
mm r 12
rl3 (spi m
rl4.flr) mm Hi HI m i1 iHlvpi SS12
(
rl 5 pc) rl4flr) -rl4 fir) mw. rlJ 'll) rl4(lrl *151pc)
iCPSR SPSR
ARM Instruction Set The original ARM instruction set. Here all instructions are 32bit wide and are word aligned.
Since all instructions are word.aligned, one single fetch reads four 8bit memory locations. Hence there is no significance
for the lower 2 bits (bO and bl which generates 4) of the program counter. Only bits 2 to 31 of PC are significant for ARM
instructions.
Thumb Instruction Set Thumb is the 16bit subset for the 32bit ARM instructions. These instructions can be
considered as a 16bit compressed form of the original 32bit ARM instructions. These instructions can be executed either
by decompressing the instruction to the original 32bit ARM instruction or by using a dedicated 16bit Instruction decoding
unit. Here all instmctions are half word aligned; meaning one single fetch reads two 8bit memory locations. Hence there
is no significance for the lower bit (bO which generates 2) of the program counter. Only bits 1 to 31 of PC are significant
for Thumb instmctions. Thumb instmctions provide higher code density than the 32bit original ARM instmctions. The
concept of thumb instruction was introduced in the ARM V4 (Version 4.0) architecture.
Jazelle Instruction Set Jazelle is the hardware implementation of the Java Virtual Machine (JVM) for executing
•“java byte codes. The 140 Java instmctions are implemented directly in the Jazelle hardware unit, rest 94 Java instmctions
Introduction to Embedded Systems
are implemented by emulating them with multiple ARM instructions. All instructions related to the Jazelle are 8bit wide.
Here the processor reads 4 instructions at a time by a word access.
All ARM instructions are conditional, meaning all instructions associate a conditional code with them. The condition
code specifies the condition to be checked (Flags to be tested) for executing the instruction. A typical instruction looks
like Opcode <condition code> Operands, e.g. ADDAL r0,rl,r2 always adds rl and r2 and stores the result in register
rO, whereas the instruction ADDEQ r0,rl,r2 adds rl and r2 only if the zero flag (Z) is set. The table given below gives a
snapshot of the different condition codes supported by ARM.
Use the condition code AL for executing the instruction regardless of any conditions. The ARM instruction set can be
broadly classified into:
Data Processing Instructions The data processing instructions include the arithmetical and logical operation
instructions, comparison and data movement instructions. The following table summarises the data processing operations
supported by ARM.
Program Control (Branching) Instructions Diverts the program flow. It can be either a conditional execu¬
tion or unconditional branching. BX, B, BL, etc. are examples for branching instructions.
Multiplication Instructions Multiply (MUL), Multiply Accumulate (MLA), Multiply Long (MLL) and Multi¬
ply Long Accumulate (MLAL) instructions are the multiply instructions supported by ARM Instruction set.
Overview of PIC and AVR Family of Microcontrollers and ARM Processors
Barrel shift Operations ARM ISA doesn’t implement shift instructions by default. However shift operations are
implemented as part of other instructions, with the help of a barrel shifter. Bit shifting operations like Logical Left Shift
(LSL), Arithmetic Right Shift (ASR), Logical Right Shift (LSR), Rotate Right (ROR), and Rotate Right Extended (RRE)
are examples for this.
Branching Instructions For changing the program execution flow. It can be either conditional branching or
unconditional branching.
Co-Processor Specific Instructions ARM does not execute certain instructions and lets a co-processor to
execute these instructions. CDP, LDC, STC, MRC, MCR, etc. are examples for this.
Under ARM architecture, Interrupts, Reset, memory access error, etc. are considered as exceptions and ARM has a
well defined exception mechanism to handle them. The different exceptions defined by ARM are listed below.
For all exceptions except the “Reset", the various operations performed are listed below.
1. Copy the contents of the CPSR register to the SPSR register of the new mode.
2. Set the following states in the CPSR register
(a) Change the state to ARM
(b) Change the mode to exception mode, e.g. FIQ mode for Fast Interrupts.
(c) Disable interrupts if needed
3. Save the address of the instruction following the instruction generated the exception to the register ‘rl4’ LR (reg¬
ister) of the mode corresponding to the exception.
4. Set Program counter with the vector address corresponding to the exception.
Figure A1.5 given below explains the vector address for all exceptions.
' Reserved- 1
DataAboit'-
Pre-fetch Abort
_Software interrupt
Undefined instruction
" '- Reset
Upon the execution completion of the exception code, the following actions are performed.
1. Restore CPSR register from the SPSR register of the current mode
2. Restore Program counter from register ‘rl4’, (LR) of the current mode.
The processor bus architecture followed by ARM is known as Advanced Microcontroller Bus Architecture (AMBA).
The AMBA architecture standardises the on-chip connection of different IP cores and enables IP reusability. The AMBA
specifications define three different buses for on-chip device communication. They are:
Advanced High Performance Bus (AHB) AHB acts as the backbone for connecting high-performance, high
clock frequency system modules like memory controllers, DMA bus master, etc.
Advanced System Bus (ASB) ASB is used for connecting high performance system modules. ASB is an alter¬
native system bus for AHB, for system modules which don’t require performance up to the mark offered by AHB.
Advanced Peripheral Bus (APB) APB is a simple, low performance, low power bus used for connecting low
speed peripherals.
ARM supplies the high performance processor core and the specification for the different buses for connecting with
the ARM core. The ARM partners can build System on Chip (SoC) devices by integrating various peripherals to the ARM
core using the AMBA specified bus. A typical SoC architecture using the ARM core and AMBA bus standard is given in
Fig.A1.6.
fFigTAl.e)
;§le
^M'ghdix-
V* $«}S ■- s-';: *
1 .DIGITAL CLOCK % '4r% joA ?sb' to T SS *v
Design and Implement a Digital Clock as per the requirements given below.
. 1. Use a 16 character 2-line display for displaying the current Time. Display the time in DAY HH:MM:SS format
on the first line. Display the message ‘Have A Nice Day! ’ on the second line. DAY represents the day of the Week
(SUN, MON, TUE, WED, THU, FRI and SAT). HH represents the hour in 2 digit format. Depending on the format
settings it may vary from 00 to 12 (12 Hour Format) or from 00 to 23 (24 Hour format). MM represents the minute
in 2 digit format. It varies from 00 to 59. SS represents the seconds in 2 digit format, it varies from 00 to 59. The
alignment of display character should be centre justified (Meaning if the characters to display are 12, pack it with
2 blank space each at right and left and then display).
2. Interface a Hitachi HD44780 compatible LCD with 8051 microcontroller as per-the following interface details
Data Bus: Port P2
Register Select Control.line (RS): Port Pin P1.4
Read/Write Control Signal (RW): Port Pin PI.5
LCD Enable (E):P1.6
1 3. The Backlight of the LCD should be always ON
4. Use AT89C51/52 or AT89S8252 microcontroller. Use a crystal Oscillator with frequency 12.00 MHz.
* This will provide accurate timing of 1 microsecond/machine cycle.
5. A 2 x 2 matrix key board (4 keys) is interfaced to Port PI of the microcontroller. The key details are MENU key
connected to Row 0, and Column 0 of the matrix; ESC key connected to Row 0, and Column 1 of the matrix;
UP key connected to Row 1, and Column 0 of the matrix; DOWN key connected to Row 1, and Column 1 of the
matrix. The Rows 0, 1 and columns 0, 1 of matrix keyboard are interfaced to Port pins P1.0, Pl.l, PI.2 and PI.3
respectively.
6. The hour (HH) and minutes (MM) should be adjustable through a Menu Screen. The Menu screen is invoked by
pressing the MENU key of the matrix key.
7. The selection of an item in the menu is achieved by pressing the MENU key (Performs the Enter Key function).
Exit from the menu is achieved through pressing the ESC key. Pressing the ESC key takes the user to the previous
menu.
8. The menu contains 4 submenu items, namely ‘Adjust Hour’, ‘Adjust Minute’, ‘Adjust Day’ and ‘Adjust Format’.
The ‘Adjust Hour’ adjusts the-hour, ‘Adjust Minute’ adjusts the minute, ‘Adjust Day’ adjusts the day. of the week
(SUN to SAT) and ‘Adjust Format’ adjusts the hour display format (12 hour/24 hour format). ‘Adjust Hour’ is the
default submenu item and on invoking the menu this prompt is displayed on the screen. The user can select other
menus by pressing the UP or DOWN key. Once the submenu is selected, the User can enter that submenu by press-
Introduction to Embedded Systems
ing the MENU key. The Submenu lists the current Hour or the Current Minute or the Current Day or the Current
Format according to the submenu selected. User can adjust the values for each by pressing the UP or DOWN key.
Once a value is selected, it can be applied by pressing the MENU key. Pressing the MENU key applies the new
value and takes the user to the previous menu. Pressing ESC from any point takes the user to the previous menu.
The menu arrangement is illustrated in A2.1.
Solution:
The HD44780 standard for LCD defines a unique set of control signals, data bus and command set for Alpha numeric
Liquid Crystal Display (LCD) units. This makes the interfacing and controlling of Alpha numeric LCD unit built using
the Display Controller Hitachi 44780, independent of the LCD manufacturer. As per the HD44780 standard, the LCD
contains 3 controlling signal and a 4 bit/8bit data bus. The 4 bit/8 bit mode of operation is configurable. The Display
modules come in different varieties like single line 8 character, single line 16 character, 2 line 8 character, 2 line 16 char¬
acter, etc. The Pin details for an HD44780 compatible, 2 line 16-character LCD module (ODM16112) from ORIOLE
Electronics (www.orioleindia.com) is given in Fig. A2.2.
The LCD module contains interface pins for power supply, backlight control, LCD control and data lines. They are
explained below.
Pin
.'■P in Nn#
No# :• Pin N’timp
-PinName' np'irrintinn
Description _
: , 3 , :;; L v:v yVLcd yrL The Power supply for Liquid.c^stal units. Usually it is connected through a variable resistyr and
? LTff .* ..'.*••• -’•*> , v. ; if should be adjusted for adjusting the brightness of display characters on the LCD .
. i 15* yCC Backlight+ve supply Pow'er supply for backlight LED L '
is. Data;;a<wu«s
line (
: . : • .1 ;T- T;W,. WTW .c--.7.G-r " ±x\" N‘ ..." W •*" ™T-'- - “• ■ ;"W
D1 " Data line 1 ; y.y yy/yyW fry
■ Data line 3
D4 Wy'.y-Data line 4
Data line 5
Dataline6 L WyT/W-v :y y
Data line 7
Control Interface ' „y j :
RS = 0 Selects Command Register
RS = 1 Selects Data Register
RW. ' RW = 0 Enables Data/Command Write
RW =1 Enables Data'Command Register Read
6 EN Enable Signal. Signal for latching the data present on the data bus to the
LCD unit. This signal is active falling edge triggered (1 to 0 transition
is required for asserting this signal)
The HD44780 display controller contains an Instruction register and a data register for holding the commands and the
data respectively to the controller. The commands and data to the controller is sent through the same data bus interface.
The RS control signal interface is asserted accordingly and the RW signal is asserted low to inform the controller that
the incoming data is command data or display data. For reading from the controller, the RW signal is asserted high and
the RS signal is asserted low or high according to the requirement (RS = 0 for reading the command register. RS = 1 for
reading from the data register).
The command register holds the different commands sent by the host controller (8051) CPU to control the various
options of the LCD. We will discuss the various control commands at a later section. For displaying a character on the
LCD, the ASCII value of the character (For example 30H for displaying 0) is written to the data port of LCD. The LCD
module contains two types of memory; namely; Display Data RAM (DDR) and Character Generator RAM. Display Data
RAM is the memory which holds the ASCII value of the characters written to the LCD. The display data RAM size for the
ODM16112 module is 80 characters. Each Crystal cell in the LCD module is mapped to the DDRAM location. The first
40 DDRAM address locations (00H to 27H) are mapped for the crystal cells corresponding to the first line. The next 40
DDRAM memory locations are mapped to the crystal cells for the second line and the address range for these DDRAM
is from 40H to 67H.
The Character Generator RAM (CGRAM) is used for generating and storing user generated character patterns. Read¬
ers are requested to go through the LCD module'manual for generating and storing user generated character pattern.
The various parameters for configuring and controlling the LCD is sent as commands to the Instruction register of the
LCD. These parameters include, the interface length setting (4 or 8 bit interface), Display Data Address Selection, LCD
cursor movement, Clear the LCD, display character scrolling, etc. For more details on the supported command set for the
LCD module, refer to the LCD manual. The common commands supported for executing the commands for an HD44780
standard LCD module is listed below.
For all the commands listed above, except the Busy flag check, the RS signal is 0 (RS = 0 Command Register Selec¬
tion) and RW signal is 0 (RW = 0 Write to LCD).
For the Busy Flag check instruction, the RS signal is 0 and RW signal is 1 (Read Operation). The busy flag gives an
indication of whether the system is busy in processing a command. The busy flag (BF) should be checked before writing
a command to the LCD.
For proper operation of the LCD, it should be initialised properly as per the initialisation sequence listed in the LCD
manual. The LCD undergoes an internal power on initialisation following the application of VCC to the LCD module.
During this period the LCD will not accept any external commands. The Initialisation sequence for a standard HD44780
display is illustrated in Fig. A2.3.
Please refer to the datasheet of the LCD module for exact details on the initialisation routine.
Now let us have a look at the matrix keyboard. You can either buy a ready to use 2 x 2 matrix keyboard or can build
own 2x2 matrix keyboard using 4 push button switches. The arrangement of push buttons and the interfacing of the
matrix keyboard is shown in Fig. A2.4.
In a matrix keyboard, the keys are arranged in matrix fashion (i.e. they are connected in a row and column style). For
detecting a keypress, the keyboard uses the scanning technique, where each row of the matrix is pulled low and the col-
VCC
[Fig.A2.4| Interfacing of 2 • 2 matrix keyboard (Fig. RZ.3 Flow chart illustrating initialisation of
with 8051 Port PI HD44I80 Compatible LCD
umns are read. After reading the status of each columns corresponding to a row, the row is pulled high and the next row
is pulled low one by one and the status of the columns are read. This process is repeated until the scanning for all rows are
completed. When a row is pulled low and if a key connected to the row is pressed, reading the column to which the key
is connected will give logic 0. Since keys are mechanical devices, there is a possibility for debounce issues, which may
give multiple key press effect for a single keypress. To prevent this, a proper key debouncing technique should be applied.
Hardware keydebouncer circuits and software keydebounce techniques are the keydebouncing techniques available. The
software keydebouncing technique doesn’t require any additional hardware and is easy to implement. In the software
debouncing technique, on detecting a keypress, the key is read again after a debounce delay. If the keypress is a genuine
one, the state of the key will remain as ‘pressed’ on the seconfi read also. Pull-up resistors are connected to the column
lines to limit the current that flows to the row line on a keypress.
The necessary hardware interfacing and circuit diagram for implementing the digital clock is given in Fig; A2.5.
The next step is the design of the firmware for implementing the digital clock. Timer 0 is used for generating precise
time generation. Timer 0 when configured in the 16bit timer mode can generate precise timing of 65536 (FFFFH +1)
machine cycles. For a clock frequency of 12MHz, the time for 1 machine cycle is 1 microsecond. Timer 0 in 16bit Timer
Design Case Studies
mode, with a count loaded 00H, generates Timer Interrupt after 65536 machine cycles (i.e. after 65536 microseconds = ~
65 milliseconds). For generating a timing of 1 second, timer 0 can be loaded with a count for generating 50 milliseconds.
A Timer multiplier counter can be used to generate 1 second timing. The.Timer multiplier counter is incremented by one
each time the timer 0 rolls over from FFFFH to OOOOH. The seconds register is incremented each time when the Timer
multiplier counter becomes. 20. The minutes register is incremented each time when the ‘seconds’ register become 60.
The hour register is incremented by one each time when the minutes register become 60. The hour is adjusted according
to the hour format, i.e. hour is adjusted in the range 1 to 12 for 12 Hour format and 0 to 23 for 24 Hour format. For 24 hour
format, the day register is incremented by one when the hour register rolls over from 23 to 0. For 12 hour time format the
day register is incremented by one when the hour register rolls over from 12 to 1 and the time format changes from PM
to AM.
Two sets of registers or memory locations for hour, minute, seconds, day, time format and AM/PM selection is used.
The first set of registers is the real registers for representing the time. These registers are modified inside the Timer 0
interrupt in accordance with the seconds, minute, hour, day and AM/PM change. The register details are given below.
R7: Seconds Register
R6: Minute Register
R5: Hour Register
R4: Day Register
Bit 0: Format Register (only 1 bit is required since format falls into either of (12/24)
Bit 1: AM/PM Register (only 1 bit is required since the time falls into either AM/PM
(The memory location 20H is the physical storage location for bits with address 00H and 01H)
The second set of registers is used within the menu to adjust these values as and when the user changes it through
menu. Here data memory locations are used for holding the variables. The details of the memory variables are given
below.
30H: Seconds Register
31H: Minute Register
32H: Hour Register
33H: Day Register
Bit 2: Format Register (only 1 bit is required since format falls into either of (12/24)
Bit 3: AM/PM Register (only 1 bit is required since the time falls into either AM/PM
(The memory location 20H is the physical storage location for bits with address 02H and 03H)
The LCD needs to be updated only when there is a change in time or when the user exits from'the menu. This is indi¬
cated by setting a flag. The flag name is time_changed and it is a bit variable with bit address 04IL The flow chart given
in Fig. A2.6 below models the firmware requirements for building the Digital Clock.
The detection of keypress and the triggering of corresponding action are performed by the ‘Key Handler’ function
implementation. The key handler scans the rows and columns of the keyboard and identifies which key is pressed. On
detecting a keypress the actions corresponding to that key is invoked by the ‘Key Handler’ function. The keybouncing is
implemented using a 20 milliseconds delay. If a key is found closed during a scan, the same key is read after 20 millisec¬
onds to confirm it is a deliberate key closure. Hence a key should be held in pressed condition for at least 20 milliseconds
to identify it as valid key closure input. Otherwise the key closure is considered as an accidental one and it is ignored.
The flow chart given in Fig. A2.7 illustrates the implementation of the ‘Key Handler’.
The ‘Menu Key Handler’ function implements the handling of the ‘MENU key in different contexts. In the normal
scenario, the ‘MENU key is used ftjr invoking the ‘Main Menu’. Within the ‘Menu’, the ‘MENU key functions as an
‘ENTER’ key. The flow chart given in Fig. A2.8 illustrates the implementation of ‘MENU key handling.
The ‘Up Key Handler’ function implements the handling of the ‘UP’ key in different contexts. Inside the ‘MENU,
this key is used for navigating between the menu items and setting the values for menu items. This key doesn’t have a
function when the user is not inside the ‘MENU, meaning, pressing this key will not produce any impact when the user
hasn’t invoked the ‘MENU. The flow chart given in Fig. A2:9 illustrates the implementation of ‘UP’ key handling.
The ‘Down Key Handler’ function implements the handling of the ‘DOWN key in different contexts. Inside the
‘MENU, this key is used for navigating between the menu items and setting the values for menu items. This key doesn’t
have a function when the user is not inside the ‘MENU, meaning, pressing this key will not produce any impact when
the user hasn’t invoked the ‘MENU. The flow/chart given in Fig. A2.10 illustrates the implementation of ‘DOWN key
handling.
Introduction to Embedded Systems
Main routine
Timer 0 interrupt handler
start
YES
‘Key Handler’
Check for key press and handle it accordingly
NO
NO |
Hour register =
Hour register-12
Complement AM/PM bit
— YES "
>r
Increment menu -
counter
>r
>f
Return
Display‘Adjust Hour’ 1
on first line temp hour
on second line Display ‘Adjust Format’ on first line & (he
format corresponding to temporary ‘Format’
bit on second line. The format details V/s
temporary ‘Format’ bit mapping is given
Return
below.
0=12
1=24
Return
— YES “
> r
Decrement menu
counter
> f
> f
Return
Return
3 temporary ‘Format’ bit mapping is given
below.
0 = 12
1=24
£
Return ^
The ‘ESC Key Handler’ function implements the handling of the ‘ESC’ key in different contexts. Inside the ‘MEN If,
this key is used for traversing to the previous menu and finally to come out of the ‘ MENU. This key doesn’t have a func¬
tion when the user is outside the ‘MENU, meaning, pressing this key will not produce any effect when the user hasn’t
invoked the ‘MENU. The flow chart given in Fig. A2.ll illustrates the implementation of ‘ESC key handling.
Now comes the final part, implementation of the firmware requirements, which are modelled above, in Assembly
Language. The firmware implementation is given below.
1
Introduction to Embedded Systems
;################################.####################################
,-Subroutine for retrieving the DAY string corresponding to the day
;index passed. The Day index is passed through Accumulator
;Input Variable: Through Accumulator Register
/Output Variable: The start .address-of the corresponding string is-
;loaded in DPTR register.
;#######################################!############################
GET DAY STRING: CJNE A, #0H, IS_MONDAY
MOV. DPTR, I TEXT SUIN'
RET ' , - , ^ ' ,
IS, .MONDAY: CJNE A, #1H, ISJTUESDAY
MOV DPTR,#TEXT_MON ,
Ret. ViWR-RRf ,<V.. •, I
IS "TUESDAY: CJNE-A, #2H, IS_WEDNE S DAY
.MOV DPTR, f TEXT_TUE ,
RET', ' ; '
IS WEDNESDAY: CJNE A, #3H, IS_THURSDAY ■
"MOV DPTR,iTEXT WED ?'■ '■
RET V-tR ; - ./ "tV V ■
IS THURSDAY: CJNE A, 14H, IS _FRTDAY
MOV D?TR, #TEXT THU . ,
ret y- ,v " ’; rlb:" V
IS FRIDAY: CJNE A,'* #5K, 1S_ SATURDAY
MOV DPTR, #TEXT_FRI
RET , , : « -
IS SATURDAY: CJNE a, #6H, invalid day
MOV DPTR, i TEXT SAT ' Vi
/####################################################################
/Subroutine for Displaying AM/PM on the last 4 cells of the second-
/line of LCD. The bit corresponding to the AM/PM string to be-
/displayed on LCD is passed through TEMP_AM_PM bit. This bit is-
/complemented first. This helps in calling this subroutine for-
/handling both UP/DOWN key in Submenu 2 (for AM_PM Adjust)
/Input Variable: TEMP_AM_PM bit
/Output Variable: None
/####################################################################
Design Case Studies
; III########ft##################################;#####:#i|t###jl‘####### '
; ESC KEY HANDLER ' _ • .'■ ; ->■» "'V r .. C
/Handies ‘the ESC Key press in Main menu, Submenu 1 and ; Submenu :2z:..; C ,■
; Inputs: SUBMENU2. COUNTER, SU3MF.NU1__C0UNTER, MAIK_ .MENU_COUNTER ■
;Output: None ' E C- s; ■ .. ... y,5tj“ o-
CAlX, MAIN_SM1_CMNHLR
I RET
; ESC_CHECK_MAINMENU: MOV A,MAIN_MENU_COUNTER
CJNE A,#00H, ESC_INSIDE_MAINMENU
s ;ESC Press Outside Mainmenu
; Do Nothing
/■ RET'
|>;ESC_INSrDE_MA'INMENU:;ESC from-Main Menu
!" . /Reset all menu related counters
. ..." CLR A
*' MOV MAIN MENU "COUNTER, A
-. •. ' MOV SUBMENUl COUNTER, A
if. ..‘V; .'“S • ''MOV SU3MFA!:;2 COUNTER, A
pA. •’ ' i ' 5§t Time changed Flag.
. .- IfThis is mandatory for Refreshing LCD Display
f - SETB TIME_CHANGED < - ‘ I. •
■ u ret , , ■■■*-> '■ .
I-V;Common Handler for ESC key in Submenu 1 and UP/DOKN Key in Main Menu
Handles ESC Key in Submenu 1 and UP/DOWN Key in. Main Menu *.
‘O;.Before, Calling this routine, Accumulator. Register should be ■ loaded-'
)/} with " the value .of ‘Submenu 1 counter or main'menu counter ,r
• Input: Accumulator Register .- . ■.
K;,*Output: None ' V
Ua;-# # # # # ####### # #,# ######## #.# ############# #:# ###### ## ####### # # # # # # # # # # # # #
ji"MAIN_SMl_CMNHLR: CJNE A, #01H, SM1_ADJUSTEMINUTE
, ;ESC from-Submenu 1
CALL DISPLAY_HOUR_BL
RET
, SM1_ADJUST_MINUTE:CJNE A,#02H, SMI ADJUST DAY
CASE DISPLAY_MINUTE_BL
RET
SM1_ADJUST_DAY: CJNE A,#03H, SM1_ADJUST_F0RMAT
CALL DISPLAY_DAY_BL
RET
SM1_ADJUST_F0RMAT: CJNE A,#04H, SM1_1NVALID
CALL DISPLAY_FORMAT_BL
SM1_INVALID: RET
; #########■################ #-# #########################################
;UP KEY HANDLER
;Handles the UP Key press in Main menu, Submenu 1 and Submenu 2
;Inputs: SUBMENU2_C0UNTER, SUBMENUl^COUNTER, MAIN_MENU_COUNTER
/"Output: None
;###########!#######f#########f######################################
UP_KEY_HANDLER: MOV A, SUBMENU2_COUNTER
/Check whether inside Submenu 2
CJNE A,#OOH, UP_IN_SUBMENU2
/Not inside Submenu 2
/Check whether inside Submenu 1
JMP 0P_CHECK_SUBMEND1
/Inside Submenu 2
/Complement AM_PM
UP_IN_SUBMENU2: /Display the AM/PM on second Line
CALL DISPLAY__AMPM
RET
UP_CHECK_SUBMENU1:MOV A,SUBMENUl COUNTER
Design Case Studies
, t %i /- - '. /Check 'the time format. For- 12. hour format, reset-
/Temp_Hour to 1 if it exceeds 12 ■’
' ; YYiYYr Y , JB FORMAT,; UP_HR_F0RMAT_24 ■ ‘ 1 Y/,,. . .
YY. YiV,, CJNE A, # 13, UP_IIOUR_5Ml_RTRN r lY' -
.*•-' ■' .. mcv a, ii ' " ^ • =Y
UF_HCUR_SMi _RTAN: MCV TEMP HOUR, A , ’.Y£ t <■££■■ 4’~:-
r ' ; -/ - ; /Display the -temp hour on second line -c
' ; CALI, DISPLAY HR MT .. ' Y'Y'YYYk '.; :
' RET
CJNE A,#0,DN_H0UR_SM1_RTRN
MOV A, #12
DN_HOUR_SMl_RTRN:MOV TEMP_HOUR, A
;Display the temp hour on second line
CALL DIS PLAY_HR_MT
RET
DN__HR_FORMAT_24:CJNE A, #0FFH,DN_HOUR_SMl_RTRN
;For 24 hour format reset the Temp_Hour> to ’23-
;if Temp_hour <0
MOV A, #23 I
MOV TEMP_HOUR, A
/Display, the temp hour on second line '
CALL DIS PLAY_HR_MT . '
■ RET
DN_ADJUST MINUTE: CJNE A,#Q2H, DN_ADJUST_DAY
■ /Inside Adjust Minute Menu
/Decrement Temp_Minute by 1
MOV A, TEMP_MINUTE
DEC A
CJNE A,#0FFH,DN_MNT_SM1_RTRN
/Reset the Temp_Minute to 59-
/if Temp_Minute < 0
MOV A,#59
DN_MNT_SM1_RTRN:MOV TEMP_MINUTE, A
/Display the temp minute on second line
CALL DISPLAY_HR_MT - '
RET
DN_ADJUST_DAY: CJNE A,#03H, DN_ADJUST_FORMAT
/Inside Adjust Day Menu -
_ / Decrement Temp_Day by 1
MOV A, TEMP_DAY
DEC A
CJNE A,#0FFH,DN_DAY_SM1_RTRN
MOV A, #6
DN_DAY_SM1_RTRN:CALL GET_DAY_STRING
MOV TEMP_DAY,A
/Display the DAY string on second line
;DPTR Register holds the start’ address of the string
MOV A,#LINE_2_5DIGIT
CALL SEND_COMMAND
CALL DISPLAY__STRING
RET *
DN_ADJUST_FORMAT:CJNE A,#04H, DN_SM1_INVALID
/Toggle Temporary Format bit
CPL TEMP^FORMAT
/Display the current temp format on second line
CALL DISPLAY_FORMAT
DN_SM1_INVALID: RET
/Check whether inside mainmenu
DN_CHECK_MAINMENU:MOV A,MAIN_MENU_COUNTER
■ CJNE A,#OOH, DN_IN_MAINMENU
/DOWN Key pressed outside menu
s
/Do nothing
RET
/DOWN Key pressed inside main menu
Introduction to Embedded Systems
/Display 'Adjust Hour' on First Line and blank the Second line
./Input:'/;None V o' ■, . : *r. "vV’.- -4
/Output: None' . _■ - :. ' .
' f#f##'####f#f#####'##########l####################>#'f###t#.###:*########'#
DI'SPL AY_HO,UR_BL.: MOV A, #LINE_1
- ... -CALL SEND COMMAND ' ‘ f* ' '
' MOJ/ DPTR, #TEXT_HOUR ' >r -} •/; ., .>
CALL DISPLAY_STRING
MOV A,.#LINE_2 ;■
CALL SEND_COMMAND ' _ ■■ . C
MOV DPTR,#TEXT_LCD_BLANK . -
'CALL- DISP.LAY_STRING * C-n’i - • ■
RET
##########################*########## ### # ## # # |# # #|«# iff I # ## i
/Display 'Adjust Minute' on First Line and .blank the -/SeTcorrd lirie
/Input: None \\ ' *v "• 5 ' '
/Output: None , ‘O' , *■
/#####.####fr####################################fft###.#.####f##########
DISPLAY_MINUTE_BL:MOV A, #LINE_l" " f]-
CALL send COMMAND
MOV DPTR,#TEXT_MINUTE
CALL DISPLAY_STRING
MOV A,#LINE_2
CALL SEND_COMMAND
MOV DPTR,#TEXT_LCD_BLANK
CALL DISPLAY_STRING
RET
/#######!############################################################
/Display 'Adjust Day' on First Line and blank the Second line
/Input: None
/Output: None .
/########## # # ########################################################
DISPLAY_DAY__BL :MOV A,#LINE_1
CALL SEND_COMMAND
MOV DPTR,#TEXT_DAY
CALL DISPLAY_STRING
MOV A,#LINE_2
CALL SEND_COMMAND
MOV DPTR,#TEXT_LCD_BLANK
CALL DISPLAY_STRING
RET
/####################################################################
/Display 'Adjust Format' on First Line and blank the Second line
/Input: None
/Output: None
Design Case Studies
';####################################################################
D'lS.PLAY_FORMAT_BL:MOV A, #LINE_1
CALL SEND_COMMAN.D
MOV DPTR,JTEXT_FORMAT
, CALL DISPLAY_STRING
.MOV A, #LINE_2
CALL SEND_COMMAND
MOV DPTR, #T.EXT_LCD_BLANK
CALL DIS.PLAY^STRING
r RET.
';:.*##:#|l#;######.##f###################f######|###################ff#####
; LCD initialisation Routine.. . . - f ■
; “Subroutine fori initialising .LCD.-'-' .V ‘ ■ - Iv.'
i
; Written for ORIOLE CDM1611 Module;
; Refer zo the LCD Datasheet for timing parameters and commands
; Fine June this . ‘subroutine as per the LCD Module .if ’ . .. .
; - input ; None p •■■'..
; Out put: None 7 . \ •
;# # ######### # # # # # ## # # # # # ######## ## ################ ## ########## ## # # # # #
INITIALISEjLCD: MOV A,#FUNCTION_SET
CALL WRITE_COMMAND
MOV A,#FUNGTION_SET
CALL WRITEJSOMMAND
MOV A, #FON.CTION_SET .
CALL WRITE_COMMAND
MOV A, #FUNCTION__SE.T
CALL SEND_COMMAN D
MOV A,#ENTRY^MODE
CALL SEND_COMMAND
MOV A,#CLEAR_LCD
CALL SEND COMMAND
MOV A,#LCD_ON
CALL SEND_COMMAND
RET
;#################################################-###################
(•Subroutine for writing to the command register of LCD with 'BUSY'-
;checking. Command to be written is passed through Accumulator
;Input: Accumulator
;Output: None
;#####!##############################################################
SEND_COMMAND: SETB P2.7 ; Configure Pi . P2.7 as input Pin
CLR RS ;RS=0, Select Command Register of LCD
SETB RW ;RD=1, Read Operation
READJ3USY: SETB EN ; Toggle LCD Enable to generate a High to
CLR EN ;Low transition. This latches the data
JB P2.7,READ_BUSY ;Loop till Busy Flag is Reset
MOV P2, A ;Load Display port with the command
i; CLR'RS ;RS=0, Select Command Register of LCD
i
CLR RW ;RD=0, Write Operation
SETB EN ;Toggle LCD Enable to generate a High to
Introduction to Embedded Systems
JB P2.7,CHECK_BUSY
MOV P2-, A ;Load LCD Data bus with Data
SETB RS ;RS=1, Select Data Register of LCD
CLR, RW ;RD=0, TWrite Operation -
SETB EN /Toggle LCD Enable to generate a High :to
CLR EN /Low transition. This latches .the data
RET
/•# f # it# || ##|!i# # ## # # # iff ###,.## # #■ ####.#,### #.#,# ########.# # #?*###### ##;######
/ Subroutine for Displaying a decimal number .in. the range-. .
/ 0-to 99? tO corresponding ASCII. Displays the ASCII value of tr.e- !
/number on second line of LCD at position starting from 14''?’ -fr' ''
/ (0 to 15 are the positions on 16 character LCD)
/The:; decimal--.number ,is/passed through Accumulator Register
/Input: Accumulator Register A
;/0yt.phK:''Udner j -”'y'* • *• c'/r i > C-.T
DB '[MON]#' ’
TEXT_TUE’:\ 1 w At Tf 1 . /\ \
DB '[TUE]#' ' ■ ' '
■#v.^ fU/.'y'-A V* • ’,’'4 * ' ’■*
TEXT_WED
DB \ * *7 % [WEpj'lt * . ; ,
db ' ; '[SAT]#' [ / : / - _
TEXT_12HR: - ., 7' - * * - *, . ,
DB ' [ 12 ' fetOUR] J * ; 1 ' :
TEXT_2 4HR: ' * 1 ;' - * ; , •
DB f 7 ' [24 HOUR]#' ; ;
TEXT_AM:.- '
,Db „*■ Ov .-r '[AM]#'
;TEXTUPM: , , -,v' <; i ill a 7■' * * ‘ :{
*PB r '[PM]#'
END ... , - ■ ■vj'EtJD of Assembly Program
The timer interrupt generates ‘seconds’ timing with a tolerance of 1 to 6 microseconds/Second. This code may not be
optimised for code size or performance. Fine tuning of the code, instruction usage, redundant and dead code elimination,
etc. are left to the readers as an assignment.
The Oscillator and Reset circuitry generates the necessary clock and reset signals for the proper operation of the
host processor and smartcard read/write IC. The watchdog timer unit provides the necessary reset to the system in case
of system misbehaviour. Sometimes the watchdog timer hardware comes as part of the host processor; if not a separate
watchdog timer IC is used. The watchdog interrupt is connected to one of the interrupt lines of the processor. Most of the
microcontrollers contain built in data memory and program memory. If the on-chip program and data memory are not
sufficient for the application, external data memory and program memory chips can be used in the design. The block dia¬
gram depicted in Fig. A2.12 illustrates the various components required for building the handheld, contactless smartcard
reader.
The firmware requirements for building the handheld smartcard reader can be divided into the following tasks.
1. Startup task
2. Battery monitoring and Charge controlling task
3. Card read/write operation task
4. Communication Task
5. Keyboard scanning task
6. LCD update task
7. Watchdog timer expire event
The communication task and keyboard scanning task can be implemented either as a polling task or an interrupt driven
task. The watchdog timer expiration event is captured as an interrupt.
The startup task implements the necessary startup operations like setting up the interrupts, configuring the ports of
the host processor, initialising the LCD, initialising the battery monitor and charge control IC, initialising the smart card
reader IC, etc.
Design Case Studies
The battery monitoring and charge controlling task reads the various registers (Voltage, capacity registers etc) of the
battery monitor IC, checks the presence of a charger and initiates a charge command, terminates the charging when the
battery is full, produces warning signal when the battery voltage is below a threshold value and switch off the unit if the
battery voltage falls below the critical threshold value. This task is assigned highest priority to avoid data corruption in
case of a power down.
The card read/write operation task is responsible for implementing the communication sequence for data read/write
operation with the card. The smartcard read/write operation follows a specific sequence of operation. The sequence varies
with the standards (Like ISO/IEC 14443 A/B, ISO/IEC 15693, etc.) supported by the read write IC. A typical read write
operation follows the sequence:
1. Reader initiates a ‘Request’ command for checking the presence of cards in the vicinity of the reader. If a card is
present in the field it responds to the reader with Answer To Request (ATR).
2. Upon receiving the ATR, the reader sends a command to capture the serial number of all cards present in the field.
Most of the reader ICs implement special algorithms to decode and capture the serial number of all cards present
in the field.
3. Reader selects the serial number of a card from the list of serial numbers received and sends a command to select
the specified card. The card whose serial number matches with the one sent by the reader responds to the reader and
all other cards in the field enters sleep state. Now the communication channel is established exclusively between
the reader and a card.
4. The Reader authenticates the card with an encrypted key. If the key matches with the key stored in the card, the
authentication succeeds. Different security mechanisms are used by different family of cards and readers for
implementing authentication.
5. The reader sends a read/write command to the card along with the details of the memory area which the reader
wants to read/write and the data to write in case of a write operation. The memory of the card is segmented into
different sectors and each sector may be further divided into blocks. Each block holds a fixed number of data bytes.
Each memory segment contains a key for authentication. The memory organisation of the card is manufacturer
specific.
The keyboard scanning task scans the keyboard and identifies a key press and performs the operation corresponding
to the key press. This can be implemented as a task or an Interrupt Service Routine if the keyboard hardware supports
interrupt generation on a key press.
The LCD update task updates the LCD with new data. This task can be invoked by other tasks like keyboard scanning
task, battery monitoring task, card read/write task and communication task for displaying the various information.
The communication task handles reader communication with the host PC. The communication interface can be either
USB or RS-232. This task can also be implemented as Interrupt Service Routine. Most of the processors support Serial
Interrupt for handling Serial data transmission using UART and RS-232. If USB is used as the interface, the interrupt
line of the USB chip can be connected to the Interrupt line of the processor and communication can be controlled in the
ISR.
The watchdog timer expiration ISR handles the actions corresponding to a watchdog timer event.
The firmware for controlling the handheld reader can be implemented in either a ‘Super loop’ model where the tasks
are executed in sequence in a super loop with interrupts running in the background or using an RTOS scheduler like uC/
OS-II or RTX-51 or VxWorks for scheduling the tasks.
taring purpose. The AMR data transmission interface can be built as an integral part of the utility meter or can be built as
an add-on module for existing utility readers. Nowadays, Application Specific Standard Product (ASSP) ICs integrating
both utility metering capability and AMR interface is available for building a utility meter with integrated AMR interface.
The AMR enabled utility meter can also be built using a low-cost microcontroller with an AMR interface. Our discussion
is focused only on AMR interfaces for energy metering for single phase power supply.
The implementation of a microcontroller-based AMR enabled energy metering device is shown in Fig. A2.13. It con¬
tains an energy metering unit and a wireless/wired automated meter reading interface. The energy metering unit records
the electricity consumption. A single chip energy meter IC forms the heart of the energy metering unit. It contains a high
performance microcontroller (typically a 16bit RISC microcontroller), integrated ADCs and LCD controller, etc. The
instantaneous power consumption in an electric circuit is given as P = VI; where V is the instantaneous voltage and I is
the instantaneous current. The .electronic energy meter records the current consumption by integrating the instantaneous
power. The instantaneous current is measured using a current sensor circuit. The current sensor circuit essentially con¬
tains a current sensing resistor and an instrumentation amplifier for signal conditioning. The signal conditioned current
value is fed to the energy metering IC. The ADC present in the energy metering IC digitises the instantaneous current
value. The voltage sensor circuit senses the instantaneous voltage and it is fed to the energy metering IC for digitisation.
The microcontroller present in the energy metering IC computes the instantaneous power, the average power consump¬
tion and stores it in the EEPROM memory. It also displays the parameters like instantaneous current, voltage, electricity
consumed in KWh units on an LCD through a built-in LCD controller interface. The MSP430 chip from Texas Instru¬
ments is a typical example of a single chip energy metering IC.
The Automated Meter Reading (AMR) interface to the energy metering unit can be provided through an RF interface
or through Power Line Communication (PLC) module. The RF based wireless AMR interface is best suited for short
range automated reading applications like handheld device based recording of energy consumption. The meter reading
official carries a handheld fox recording the energy meter reading through a Wireless AMR interface. The specific energy
meter whose reading is to be recorded can be selected by sending the unique meter identification number (Meter ID) of
Design Case Studies
the meter and then initiating an automated meter reading operation. The RF interface consists of an RF Transceiver (RF
transmitter cum receiver), and impedance matching circuit and an antenna for RF data transfer. The impedance matching
circuit consists of capacitor, resistors and inductors and it tunes the circuit for the required frequency. The RF transceiver
unit is responsible for RF Data modulation and de-modulation. The antenna is a metallic coil for transmitting and receiv¬
ing RF modulated data. The interface between the energy metering unit and the RF module is implemented through serial
interfaces like SPI, I2C, etc. The firmware running in the microcontroller of the single chip energy meter IC controls the
communication with the RF module.
The Power Line Carrier Communication (PLCC) based AMR interface makes use of a power line modem for inter¬
facing the power lines with the energy metering unit for data communication. The PLC modem provides bi-directional
half-duplex data communication over the mains. The PLC modem can be interfaced to the microcontroller of the energy
metering unit through serial interfaces like UART, SPI, etc. The firmware running in the microcontroller controls the
communication with the PLC modem. Texas Instruments’ F28x controller platform provides a cost-effective means to
implement PLC technology.
GPRS based data communication over GSM network is another option for implementing AMR interface for long
range communication. A GPRS modem is required for establishing the communication with the GSM network. The
GPRS modem can be interfaced to the microcontroller of the energy metering unit for data communication. ZigBee, Wi¬
Fi, etc. are other wireless interface options for AMR interface.
The firmware requirements for building the AMR enabled energy meter can be divided into the following tasks:
1. Startup task \
2. Energy consumtion recording task
3. Automatic meter data transfer task
4. LCD update task
The ‘Startup task’ deals with the initialisation of various port pins, interrupt configuration, stack pointer setting, etc.
for the microcontroller, initialisation of the RF interface for the communication parameter, and initialisation of the LCD.
The energy consumption task records the energy consumption. The energy metering SoC may contain dedicated hard¬
ware units for calculating the energy consumption based on the instantaneous voltage and current and the meter reading
will be available in some dedicated registers of the SoC. The energy consumprion recording task can read this register
periodically and update the EEPROM storage memory (Which may be part the SoC or an external chip interfaced to the
SoC). The automatic meter data transfer task can be implemented as a periodic task or a command driven operation in
which the meter reading is sent upon receiving a command from the host system (the system which requests energy con¬
sumption for billing and usage purpose like the central electricity office or handheld terminal carried by the meter reading
official). For the command driven communication model, a set of command interfaces are defined for communication be¬
tween the AMR enabled meter and the host system. Each AMR enabled meter holds a unique Meter ID and the firmware
can be implemented in such a way that the AMR module responds only when the Meter ID sent by the host along with
the command matches the Meter ID stored in the EEPROM of the Meter. The LCD update task updates the LCD when
a change in display parameter (like instantaneous current and voltage, energy consumption, etc.) occurs. These tasks can
be implemented in a super loop with interrupts or under an RTOS like uC/OS-II. The firmware requirements in the super
loop based implementation is modelled here with the flow chart as shown in Fig. A2.14.
4. DIGITAL CAMERA
Digital camera is a device for capturing and storing images in the form digital data (series of 1 s and Os) in place of the conven¬
tional paper/film based image storage. It is a typical example of an embedded computing system for data collection/storage
application. It contains a lens and image sensors forcapturingthe image, LCD for displayingthe cap turedimage, user interface
buttons to control the operation of the device and Communication interface to connect it with aPC to transfer captured images.
Figure A2.15 given below illustrates the various subsystems of a digital camera.
In today’s digital world people are accustomed to digital devices. Indeed they use digital camera for capturing their
favourite moments in life. But how the camera converts their favourite moments into a digital picture is still a surprising
thing to most of them. Let us have a closer look at the internals of a digital camera to explore its functioning. Figure A2.16
illustrates the various components of a digital camera. /
Introduction to Embedded Systems
- Lense
Like a normal film camera, it uses a lens to focus the scene which is going to be captured as an image. The only differ¬
ence is in ‘how the scene is captured’. In a normal film camera, the image is captured in a photo film and the photo film is
later developed to recreate the image, whereas in a digital camera, the image is captured electronically. Charge Coupled
Device (CCD) or CMOS image sensors are used for capturing the image. When exposed to light these sensors produce
electrons which in turn generates analog voltage signals. These analog signals are post-processed (filtered, amplified and
digitised) by an Analog Front End (AFE) system. Analog Front End ICs are available from various manufactures like
Texas Instruments. The digitised data is supplied to a Digital Media processor, which implements various compression
algorithms like JPEG, TIFF, etc. for compressing the raw image data. The digital media processor may contain a DMA
unit to transfer the compressed, digitised image directly to the storage memory.
The various system functions like shutter control and zooming control is performed by the ‘System Control’ unit of
the camera which is implemented using a 16/32 microprocessor/microcontroller. Stepper motors are used for shutter and
zoom control. The system control unit is also responsible for handling the I/O (Buttons) and graphical user interface
(LCD control).
The digital camera is powered by a re-chargeable battery and the monitoring and charging control is carried out by a
battery charge control and monitoring IC, which is under the control of the ‘System Control Unit’.
Provisions for interfacing various storage memory devices like SD/CF/MMC cards are implemented in the digital
camera. The camera device can be connected to a host PC through the communication interface (Like USB, IEEE 1394,
etc.) supported by the device, for image transfer.
Nowadays, a single-chip called System On Chip (SoC), incorporating both image processing unit and 16/32 processor
in a single IC are available and they simplify the design. The resolution of a digital camera is expressed in terms of mega
pixels. You may be familiar with the terms 3.2 mega pixels, 5.1 megapixels, 7.2 megapixels, etc. It represents the pixels
per inch of the. image sensing device (CCD/CMOS). As the number of pixels increase, the image quality also increases.
The various system control tasks and image capturing and processing tasks for the digital camera are implemented
using an embedded operating system.
Bibliography
Semaphore; 463
Set-
Index
EMBEDDED
SYSTEMS
Meant for students and practicing engineers, this ,
book provides a comprehensive introduction to the l||«
design and development of embedded hardware and ^
firmware, their integration, and the management of
embedded system development process.
Salient features
> Follows a design-oriented approach
> Detailed coverage of RTOS internals, multitasking,
task management, task scheduling, task
communication and synchronisation
> In-depth elucidation of the internals of
MicroC/ OS-II and VxWorks RTOS kernels
> Practical implementation using real-life examples of
Washing Machine, Automotive, Stepper Motor, and
Mobile Phones
> Deals in embedded C, delving into basics and
unraveling advanced level concepts
> Pedagogy
♦ 433 Review questions
♦ 428 Objective-type questions
♦ 25 Examples
♦ 80 Lab assignments
URL: http://w\vw.mhhe.com/shibu/esle
ISBN-13: TO-0-07-0m5M-*t 1
ISBN-10*- D-OT-OmSfl'I-X
Me
Graw Higher
Hill Education
Education! TH?SD0?DllmSflT4l