3rd Module Microcontroller and Embedded System
3rd Module Microcontroller and Embedded System
» 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 system environment or variables are detected by the sensors
connected to the input port of the embedded system.
MCE, CSE, 1
» 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.
Smart watches use Ambient Light Sensor to detect the surrounding light
intensity and uses an electrical/ electronic actuator circuit to adjust the
screen brightness.
Sensors Actuators
1. Sensor is an input device 1. Actuator is an output device
1. Convert a physical parameter to an 1. Convert an electrical signal to a physical
electrical output output
1. A device that detects events or changes in 1. A component of a machine that is
the environment and send the information responsible for moving and controlling
to another electronic device mechanisms
1. Sensor help to monitor the changes in the 1. Actuator helps to control the environment
environment MCE, CSE or physical changes 2
» The I/O Subsystem:
» The I/O subsystem of the embedded system facilitates the interaction of the
embedded system with the external world. As mentioned earlier the interaction
happens through the sensors and actuators connected to the input and output
ports respectively of the embedded system.
» Light Emitting Diode (LED): 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.
MCE, CSE 3
» Light Emitting Diode is a 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. A resister is used in series between the power
supply and the LED to limit the current through the LED.
» Out of the 8 LED segments, 7 are used for displaying alpha-numeric characters
and 1 is used for representing ‘decimal point'.
MCE, CSE 5
» The 7-segment LED displays are available in two different configurations,
namely; Common Anode and Common Cathode.
MCE, CSE 6
» Optocoupler: Optocoupler is a solid state device to isolate two parts of a
circuit. Optocoupler combines an LED and a photo-transistor in a single
housing (package).
» circuit isolation,
MCE, CSE 8
» Stepper Motor: A stepper motor is an electro-mechanical device which
generates discrete displacement (motion) in response, to de electrical signals.
» The paper feed mechanism of a printer/ fax makes use of stepper motors for
its functioning.
MCE, CSE 9
» Based on the coil winding arrangements, a two-phase stepper motor is
classified into two. They are:
» For reversing the motor rotation the current flow through the windings is
reversed dynamically.
» The different stepping modes supported by stepper motor are explained below:
MCE, CSE 11
» The following Figure shows the stator winding details of Stepper motor:
MCE, CSE 12
» ULN2803 is an octal peripheral driver array available from Texas Instruments
and ST microelectronics for driving a 5V stepper motor.
MCE, CSE 13
» Push Button Switch: Push button switch 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 in the open state and it makes
a circuit contact when it is pushed or pressed.
» In the 'Push to Break' configuration, the switch is 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.
» Depending on the way in which the push button interfaced to the controller, it
MCE, CSE 14
» Push button is used for generating a momentary pulse.
» In embedded application push button is generally used as reset and start switch
and pulse generator. The Push button is normally connected to the port pin of
the host processor/ controller.
» Depending on the way in which the push button interfaced to the controller, it
can generate either a 'HIGH' pulse or a 'LOW' pulse.
» The following Figure Illustrates how the push button can be used for
generating 'LOW' and 'HIGH' pulses.
MCE, CSE 15
» Keyboard: Keyboard is an input device 'HIGH' Pulse generator for user
interfacing.
» If the number of keys required is very limited, push button switches can be
used and they can be directly interfaced to the port pins for reading.
However, there may be situations demanding a large number of keys for user
input (e.g. PDA device with alpha-numeric keypad for user data entry).
In such situations it may not be possible to interface each keys to a port pin
due to the limitation in the number of general purpose port pins available
for the processor/ controller in use and moreover it is wastage of port pins.
MCE, CSE 16
» For example, for interfacing 16 keys, in the direct interfacing technique, 16
port pins are required, whereas in the matrix keyboard only 8 lines are
required.
» The 16 keys are arranged in a 4 column x 4 Row matrix. The following Figure
illustrates the connection o keys in a matrix keyboard.
MCE, CSE 17
» In a matrix keyboard, the keys are arranged in matrix fashion. For detecting a
key press, the keyboard uses the scanning technique, where each row of the
matrix is pulled low and the columns 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 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, proper key de-bouncing technique should be applied.
MCE, CSE 18
EMBEDDED FIRMWARE
» Embedded firmware refers to the control algorithm (Program instructions)
and or the configuration settings that an embedded system developer dumps
into the code (Program) memory of the embedded system. It is an un-
avoidable part of an embedded system.
» There are various methods available for developing the embedded firmware.
They are listed below:
1. Write the program in high level languages like Embedded C/ C++ using an
Integrated Development Environment (IDE)
» The IDE will contain a editor, compiler, linker, debugger, simulator, etc.
MCE, CSE 19
» IDEs are different for different family of processors/ controllers.
» For example, Keil microvision3 IDE is used for all family member of 8051
microcontroller, since it contains the generic 8051 compiler C51.
» The instruction set for each family of processor/ controller is different and the
program written in either of the methods given above should be converted into
a processor understandable machine code before loading it into the program
memory.
» The process of converting the program written in either a high level language
or processor/ controller specific Assembly code to machine readable binary
code is called 'HEX File Creation'.
MCE, CSE 20
» The methods used for 'HEX File Creation' is different depending on the
programming techniques used.
» Also third party tools are available, which may be of free of cost, for this
conversion.
MCE, CSE 21
» For a beginner in the embedded software field, it is strongly recommended to
use the high level language based development technique. The reasons for this
being:
» Writing codes in a high level language is easy, the code written in high level
language is highly portable which means you can use the same code to run
on different processor/ controller with little or less modification. The only
thing you need to do is re-compile the program with the required processor's
IDE, after replacing the include files for that particular processor.
» Also the programs written in high level languages are not developer
dependent. Any skilled programmer can trace out the functionalities of the
program by just having a look at the program. It will be much easier if the
source code contains necessary comments and documentation lines. It is
very easy to debug and the overall system development time will be
reduced to a greater extent. MCE, CSE 22
» 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.
MCE, CSE 23
» The super loop based task execution model for firmware executes the tasks
sequentially in 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.
» But, certain applications demand time critical response to tasks/ events and
delay in the response may be catastrophic.
» Examples: Flight control systems, Air bag control, Anti-lock Brake Systems
(ABS) for vehicles, Nuclear monitoring devices, etc.
» In embedded systems, the time critical response for tasks/ events may be
addressed by –
» Assigning priority to tasks and execute the high priority task.
MCE, CSE 25
» The following Figure gives an insight into the basic components of an
operating system and their interfaces with rest of the world.
User Applications
Application Programming
Interface (API)
Memory Management
Kernel Services
Process Management
Time Management
MCE, CSE 26
» The Kernel:
» The kernel is the core of the operating system. It is responsible for managing
the system resources and the communication among the hardware and other
system services.
» Kernel acts as the abstraction layer between system resources and user
applications.
» For a general purpose OS, the kernel contains different services like memory
management, process management, time management, file system
management, I/O system management.
MCE, CSE 26
» Process Management: deals with managing the process/ tasks.
MCE, CSE 27
» Primary Memory Management: refers to a volatile memory (RAM), where
processes are loaded and variables and shared data are stored.
MCE, CSE 28
» 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.
MCE, CSE 29
» 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, direct access to I/O devices is not allowed; access to
them is establish through Application Programming Interface (API).
» The kernel maintains list of all the I/O devices of the system.
» „Device Manager‟ of the kernel is responsible for handling all I/O related
operations.
MCE, CSE 30
» 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, as main
memory is volatile.
» Disk scheduling
MCE, CSE 31
» Protection Systems: Modern operating systems are designed in such way to
support multiple users with different levels of access permissions.
» The protection deals with implementing the security policies to restrict the
access of system resources and particular user by different application or
processes and different user.
MCE, CSE 32
» The important services offered by the kernel of an OS:
» Kernel Space and User Space: The program code corresponding to the
kernel applications/ services are kept in a contiguous area of primary
(working) memory and is protected from the un-authorized access by user
programs/ applications.
The memory space at which the kernel code is located is known as „Kernel
Space‟. All user applications are loaded to a specific area of primary
memory and this memory area is referred as „User Space‟.
The partitioning of memory into kernel and user space is purely Operating
System dependent.
Most of the operating systems keep the kernel application code in main
memory and it is not swapped out into the secondary memory.
MCE, CSE 33
» Monolithic Kernel and Microkernel: Kernel forms the heart of OS. 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. All kernel modules run
within the same memory space under a
single kernel thread.
MCE, CSE 35
TYPES OF OPERATING SYSTEMS
» Depending on the type of kernel and kernel services, purpose and type of
computing system, Operating Systems are classified into different types.
» The operating systems, which are deployed in general computing systems, are
referred as GPOS. The GPOSs are often quite non-deterministic in behavior.
MCE, CSE 36
» Real Time Operating System (RTOS):
» RTOS decides which applications should run in which order and how much
time needs to be allocated for each application.
MCE, CSE 37
» The Real-Time kernel: The kernel of a Real-Time OS is referred as Real-
Time kernel.
» The Real-Time kernel is highly specialized and it contains only the minimal
set of services required for running user applications/ tasks.
» Memory management
» Interrupt handling
» Time management.
MCE, CSE 38
» 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 and setting up a Task Control Block (TCB) for the task and
task/process termination/deletion.
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.
MCE, CSE 39
Task Priority: Task priority (E.g. Task priority = 1 for task with
priority = 1)
Task Pointers: Pointers to other TCBs (TCBs for preceding, next and
waiting tasks)
MCE, CSE 41
» Error/ Exception Handling: Deals with registering and handling the errors
occurred/ exceptions rose during the execution of tasks.
Errors/ Exceptions can happen at the kernel level services or at task level.
MCE, CSE 42
» Errors/ Exceptions can happen at the kernel level services or at task level.
Timeouts and retry are two two techniques used together. The
tasks retries an event/ message certain number of times; if no
response is received after exhausting the limit, the feature might be
aborted.
The OS kernel gives the information about the error in the form of a
system call (API).
MCE, CSE 43
» Memory Management: The memory management function of an RTOS
kernel is slightly different compared to the General Purpose Operating
Systems.
MCE, CSE 44
Most of the RTOS kernels allow tasks to access any of the memory blocks
without any memory protection to achieve predictable timing and avoid
the timing overheads.
MCE, CSE 45
» Interrupt Handling: Deals with the handling of various interrupts. Interrupts
inform the processor that an external device or an associated task requires
immediate attention of the CPU.
Interrupts which occurs at any point of execution of any task, and are
not in sync with the currently executing task are Asynchronous
MCE, CSE 46
Interrupts which occurs at any point of execution of any task, and are
not in sync with the currently executing task are Asynchronous
interrupts.
» Priority levels can be assigned to the interrupts and each interrupts can be
enabled or disabled individually. Most of the RTOS kernel implements
„Nested Interrupts‟ architecture.
MCE, CSE 47
» 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 „Timer tick‟ interval may vary depending on the hardware timer.
The time parameters for tasks are expressed as the multiples of the „Timer
tick‟.
MCE, CSE 48
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;
If the „Timer tick‟ interval is 1 millisecond, the System time register will
reset in
MCE, CSE 49
» The „Timer tick‟ interrupt can be utilized for implementing the following
actions:
Increment the System time register by one. Generate timing error and
reset the System time register if the timer tick count is greater than the
maximum range available for System time register.
MCE, CSE 50
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 pre-empted by the „Timer Interrupt‟ task.
MCE, CSE 51
» Hard Real-Time: A Real Time Operating Systems which strictly adheres to
the timing constraints 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 system/users.
For example, Air bag control systems and Anti-lock Brake Systems
(ABS) of vehicles are typical examples of Hard Real Time Systems.
MCE, CSE 52
» Soft Real-Time: Real Time Operating Systems that does not guarantee
meeting deadlines, but, offer the best effort to meet the deadline are referred
as soft real-time systems.
MCE, CSE 53
INTEGRATION OF
HARDWARE AND FIRMWARE
» 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/ controllers 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 contain 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.
MCE, CSE 54
» 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/
controller.
» 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 on-chip processor/ controller memory
or off-chip memory (FLASHI/ NVRAM, etc.).
MCE, CSE 55
» Out-of-Circuit Programming:
MCE, CSE 56
» The commands to control the programmer are sent from the utility program to
the programmer through the interface (see the following Figure).
5. Insert the device to be programmed into the open socket as per the insert
diagram shown on the programmer
MCE, CSE 58
10. Wait till the completion of programming operation (Till busy LED of
programmer is off)
11. Ensure that programming is success by checking the status LED on the
programmer (Usually 'Green' for success and 'Red' for error condition) or by
noticing the feedback from the utility program
12. Unlock the ZIF socket and d take the device out of programmer.
» With ISP, programming is done 'within the system', meaning the firmware is
embedded into the target device without removing it from the target board. It
is the most flexible and easy way of firmware embedding. The only pre-
requisite is that the target device must have an ISP support. Apart from the
target board, PC, ISP cable and ISP utility, no other additional hardware is
required for ISP.
MCE, CSE 60
» In System Programming with SPI Protocol: Devices with SPI (Serial
Peripheral Interface) ISP (In System Programming) support contains a built-
in SPI interface and the on-chip EEPROM or FLASH memory. The primary
I/O lines involved in SPI-In System Programming are listed below:
» MOSI – Master Out Slave In
» 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.
MCE, CSE 61
» Standard SPI-ISP utilities are feely available on the internet and, there is no
need for going for writing own program. For ISP operations, the target device
needs to be powered up in a pre-defined sequence.
1. Apply supply voltage between VCC and GND pins of target chip
MCE, CSE 62
5. The Code or Data array is programmed one byte at a time by supplying the
address and data together 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 5V
6. Any memory location can be verified by using the Read instruction, which
returns the content at the selected address at serial output MISO/ Pl.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.
MCE, CSE 63
» The key player behind ISP is a factory programmed memory (ROM) called
'Boot ROM‟. The Boot ROM normally resides at the top end of code memory
space and it varies in the order of a few Kilo Bytes (For a controller with 64K
code memory space and lK Boot ROM, the Boot ROM resides at memory
location FC00H to FFFFH).
» It contains a set of Low-level Instruction APIs and these APIs allow the
processor/ controller to perform the FLASH memory programming, erasing
and Reading operations.
» The contents of the Boot ROM are provided by the chip manufacturer and the
same is masked into every device.
MCE, CSE 64
» In Application Programming (IAP):
» Updating calibration data, look-up tables, etc., which are stored in code
memory, are typical examples of IAP.
MCE, CSE 65
» Use of Factory Programmed Chip:
MCE, CSE 66
» Firmware Loading for Operating System Based Devices:
» The 'Boot 1oader' for such embedded systems comes as pre-loaded or it can
be loaded to the memory using the various interface supported like JTAG.
» Boot loader implements menu options for selecting the source for OS image
to load (Typical menu item examples are Load from FLASH ROM, Load
from Network, Load through UART, etc).
MCE, CSE 67
» Once a communication link is established between the host and target
machine, the OS image can be directly downloaded to the FLASH memory of
the target device.
MCE, CSE 68