Embedded Systems Unit 3
Embedded Systems Unit 3
Embedded Systems
Unit-3
Embedded Firmware Design
Presented by:
Mr. M Madhu Manikya Kumar
Assistant Professor
Topics to be Discussed
• Embedded Firmware
• Embedded Firmware design approaches
• Embedded Firmware development languages
• ISR concept
• Interrupt sources
• Interrupt servicing mechanism
• Multiple interrupts
• DMA
• Device driver programming
• Concepts of C versus Embedded C and Compiler versus Cross-compiler
Embedded Firmware
• The embedded firmware is responsible for controlling the various peripherals of
the embedded hardware and generating response in accordance with the functional
requirements of the product.
• The embedded firmware is the master brain of the embedded system.
• The embedded firmware imparts intelligence to an Embedded system.
• It is a onetime process and it can happen at any stage.
• The product starts functioning properly once the intelligence imparted to the
product by embedding the firmware in the hardware.
• The product will continue serving the assigned task till hardware breakdown
occurs or a corruption in embedded firmware.
• In case of hardware breakdown , the damaged component may need to be replaced
and for firmware corruptions the firmware should be re-loaded, to bring back the
embedded product to the normal functioning.
• The embedded firmware is usually stored in a permanent memory (ROM) and it is
non alterable by end users.
• 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 details of various hardware chips used and
some programming language (either low level Assembly Language or High level
language like C/C++ or a combination of the two)
• The embedded firmware development process starts with the conversion of the
firmware requirements into a program model using various modeling tools.
• The firmware design approaches for embedded product is purely dependent on the
complexity of the functions to be performed and speed of operation required.
Embedded Firmware design approaches
• There exist two basic approaches for the design and implementation of embedded
firmware, namely;
• The Super loop based firmware development approach is Suitable for applications
that are not time critical and where the response time is not so important
(Embedded systems where missing deadlines are acceptable).
• It is very similar to a conventional procedural programming where the code is
executed task by task
• The tasks are executed in a never ending loop
• The task listed on top on the program code is executed first and the tasks just
below the top are executed after completing the first task
• A typical super loop implementation will look like:
• Doesn’t require an Operating System for task scheduling and monitoring and
free from OS related overheads.
Inline Assembly
Assembly Language
• The assemblers for different family of target machines are different. A51 Macro
Assembler from Keil software is a popular assembler for the 8051 family micro
controller
• Each source file can be assembled separately to examine the syntax errors and
incorrect assembly instructions
• Assembling of each source file generates a corresponding object file. The object
file does not contain the absolute address of where the generated code needs to be
placed (a re-locatable code) on the program memory
• The software program called linker/locater is responsible for assigning absolute
address to object files during the linking process
• The Absolute object file created from the object files corresponding to different
source code modules contain information about the address where each instruction
needs to be placed in code memory
• A software utility called ‘Object to Hex file converter’ translates the absolute
object file to corresponding hex file (binary file)
Advantages:
1.Efficient Code Memory & Data Memory Usage (Memory Optimization):
The developer is well aware of the target processor architecture and
memory organization, so optimized code can be written for performing
operations.
This leads to less utilization of code memory and efficient utilization
of data memory.
2.High Performance:
Optimized 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 target processor.
3.Low level Hardware Access:
Most of the code for low level programming like accessing external
device specific registers from OS kernel ,device drivers, and low level
interrupt routines, etc are making use of direct assembly coding.
2.Developer dependency:
There is no common written rule for developing assembly language based
applications.
3.Non portable:
Target applications written in assembly instructions are valid only for that
particular family of processors and cannot be re-used for another target
processors/controllers.
If the target processor/controller changes, a complete re-writing of the
application using assembly language for new target processor/controller is
required.
High Level Language
• The embedded firmware is written in any high level language like C, C++
• A software utility called ‘cross-compiler’ converts the high level language to
target processor specific machine code
• The cross-compilation of each module generates a corresponding object file. The
object file does not contain the absolute address of where the generated code needs
to be placed (a re-locatable code) on the program memory
• The software program called linker/locater is responsible for assigning absolute
address to object files during the linking process
• The Absolute object file created from the object files corresponding to different
source code modules contain information about the address where each instruction
needs to be placed in code memory
• A software utility called ‘Object to Hex file converter’ translates the absolute
object file to corresponding hex file (binary file)
High Level Language – Source File to Hex File
Translation
Advantages:
• Reduced Development time: Developer requires less or little knowledge on
internal hardware details and architecture of the target processor/Controller.
• Developer independency: The syntax used by most of the high level languages
are universal and a program written high level can easily understand by a second
person knowing the syntax of the language
• The programmer must be aware of how to pass parameters from the ‘C’ routine to
assembly and values 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 purpose.
• We can get this information from documentation of the cross compiler.
• Different cross compilers implement these features in different ways depending on
GPRs and memory supported by target processor/controller
Mixing High level language like‘C’ with
Assembly Language
• The source code is already available in assembly language and routine written in a
high level language needs to be included to the existing code.
• The entire source code is planned in Assembly code for various reasons like
optimized code, optimal performance, efficient code memory utilization and
proven expertise in handling the assembly.
• The functions written in ‘C’ use parameter passing to the function and returns
values to the calling functions.
• The programmer must be aware of how parameters are passed to the function and
how values returned from the function and how function is invoked from the
assembly language environment.
• Passing parameter to the function and returning values from the function using
CPU registers , stack memory and fixed memory.
• ISR tells the processor or controller what to do when the interrupt occurs. The
interrupts can be either hardware interrupts or software interrupts
• An interrupt is a signal (an “interrupt request”) generated by some event external
to the CPU
• In response to the interrupt, the routine or program, which is running presently
interrupts and an interrupt service routine (ISR) executes.
• Signal interrupt [The signal differs from the function in the sense that execution of
signal handler (ISR) can be masked and till mask is reset, the handler will not
execute on interrupt. Function on the other hand always executes on the call after a
call-instruction.]
• The state of continuous monitoring is known as polling. The microcontroller
keeps checking the status of other devices; and while doing so, it does no other
operation and consumes all its processing time for monitoring. This problem can
be addressed by using interrupts.
• In the interrupt method, the controller responds only when an interruption occurs.
Thus, the controller is not required to regularly monitor the status (flags, signals
etc.) of interfaced and inbuilt devices.
Interrupts v/s Polling
• The polling method is like a salesperson. The salesman goes from door to door
while requesting to buy a product or service. Similarly, the controller keeps
monitoring the flags or signals one by one for all devices and provides service to
whichever component that needs its service.
Interrupt Service Routine:
• Interrupt handling code often called an ISR (“Interrupt Service Routine”)
• When ISR is finished, execution returns to code running prior to interrupt.
• Interrupt Service Routines (ISR) are the portions of the program code that handle
the interrupt requests.
• When an Interrupt is triggered (either a hardware or software interrupt), the
processor breaks away from the current task, moves the instruction pointer to the
ISR, and then continues operation
• When the ISR has completed, the processor returns execution to the previous
location.
• Many embedded systems are called interrupt driven systems, because most of the
processing occurs in ISRs, and the embedded system spends most of its time in a
low-power mode
Latency
• The interrupt latency is the interval of time measured from the instant an interrupt
is asserted until the corresponding ISR begins to execute. The worst-case latency
for any given interrupt is a sum of many things, from longest to shortest:
• The longest period global interrupt recognition is inhibited
• The time it would take to execute all higher priority interrupts if they occurred
simultaneously
• The time it takes the specific ISR to service all of its interrupt requests (if multiple
are possible)
• The time it takes to finish the program instructions in progress and save the
current program state and begin the ISR
• We can see how higher-priority interrupts can have much lower latencies. In
simple cases, latency can be calculated from instruction times, but many modern
systems with 32-bit CPUs, caches, and multiple interrupt sources, are far too
complex for exact latency calculations. Interrupt latency must be considered at
design time, whenever responsiveness matters.
Interrupt Vector Table
• Constant table in ROM.
• Special addresses with respect to CPU.
• Each interrupt has specific address in interrupt vector table .
• This specific address should be programmed to have the address of ISR of this
interrupt.
• At interrupt processing PC will contain this address or it will be an instruction to
jump to this address
Direct Memory Access (DMA)
• Direct memory access (DMA) is a mode of data transfer between the memory
and I/O devices.
• We have two other methods of data transfer, programmed I/O and Interrupt
driven I/O.
• In programmed I/O, the processor keeps on scanning whether any device is
ready for data transfer. If an I/O device is ready, the processor fully dedicates itself
in transferring the data between I/O and memory. It transfers data at a high rate,
but it can’t get involved in any other activity during data transfer. This is the
major drawback of programmed I/O.
• In Interrupt driven I/O, whenever the device is ready for data transfer, then it
raises an interrupt to processor. Processor completes executing its ongoing
instruction and saves its current state. It then switches to data transfer which
causes a delay. Here, the processor doesn’t keep scanning for peripherals ready for
data transfer. But, it is fully involved in the data transfer process. So, it is also not
an effective way of data transfer.
Without DMA vs With DMA
When the CPU is using programmed The CPU initiates the transfer, does other
input/output, it is typically fully operations while the transfer is in
occupied for the entire duration of the progress, and receives an interrupt from
read or write operation, and is thus the DMA controller when the operation
unavailable to perform other work. is done.
• The two methods of data transfer are not useful for transferring a large block of
data. But, the DMA controller completes this task at a faster rate and is also
effective for transfer of large data block.
• The DMA controller transfers the data in three modes
• Burst Mode
• Cycle Stealing Mode
• Transparent Mode
• Burst Mode: Here, once the DMA controller gains the charge of the system bus,
then it releases the system bus only after completion of data transfer. Till then the
CPU has to wait for the system buses
• Cycle Stealing Mode: In this mode, the DMA controller forces the CPU to stop
its operation and relinquish the control over the bus for a short term to DMA
controller. After the transfer of every byte, the DMA
controller releases the bus and then again requests for the system bus. In this way,
the DMA controller steals the clock cycle for transferring every byte.
• Transparent Mode: Here, the DMA controller takes the charge of system bus
only if the processor does not require the system bus.
DMA Data Tranfer: Block Diagram
DMA Data Tranfer
• Two control signals are used to request and acknowledge a DMA transfer .
• The HOLD signal is a bus request signal which asks the microprocessor to release
control of the buses after the current bus cycle.
• The HLDA signal is a bus grant signal which indicates that the microprocessor has
indeed released control of its buses by placing the buses at their high-impedance
states.
• DREQ (DMA request): Used to request a DMA transfer for a particular
DMAchannel.
• DACK (DMA channel acknowledge): Acknowledges a channel DMArequest from
a device.
DMA Data Transfer
• Data transfer technique directly between memory and I/O deviceSteps:
• A programmer uses generic commands for device driver for using a device. The
operating system provides these generic commands.
• Each command relates to an ISR. The device driver command uses an SWI to call
the related ISR device-function routine)
• Generic functions used for the commands to the device are device
• create ( )
• open ( )
• connect ( )
• bind ( )
• read ( )
• write ( )
• ioctl ( ) [for IO control]
• delete ( )
• close ( )
Concept of C vs Embedded C
C Language:
• C is a general-purpose programming language, which is widely used to design any
type of desktop-based applications.
• The main features of C language include low-level access to memory, a simple set
of keywords, and clean style, these features make C language suitable for system
programming like OS or compiler development.
Embedded C:
• Embedded C is an extension of C language and it is used to develop micro-
controller based applications.
For C language, the standard compilers can be used to For Embedded C, a specific compilers that are able to generate
compile and execute the program. particular hardware/micro-controller based output is used.
Popular Compiler to execute a C language program Popular Compiler to execute a Embedded C language program
are:GCC (GNU Compiler collection),Borland turbo C, are: Keil compiler, BiPOM ELECTRONIC, Green Hill
Intel C++ software
C Embedded C
It is very easy to read and modify the C language. It is not easy to read and modify the Embedded C language.
Bug fixing are very easy in a C language program. Bug fixing is complicated in a Embedded C language
program.
C language has a free-format of program coding. Formatting depends upon the type of microprocessor that is
used.
Input can be given to the program while it is running. Only the pre-defined input can be given to the running
program.
• For example, a compiler that runs on Windows platform also generates a code that
runs on Linux platform is a cross compiler.