Module 4 - Embedded Firmware Design

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

UNIT - Embedded firmware Design

Introduction - 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.

The embedded firmware can be developed in various methods like


 Write the program in high level languages like Embedded C/C++ using an Integrated
Development Environment (The IDE will contain an editor, compiler, linker, debugger,
simulator etc. IDEs are different for different family of processors/controllers.
 Write the program in Assembly Language using the Instructions Supported by your
application’s target processor/controller

Embedded Firmware Design & Development:

 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 firm firmware


ware 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 is imparted to the system by embedding the
firmware in the hardware.

 The product
duct 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
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 hhardware,
ardware, 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 co
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
compl of
the functions to be performed and speed of operation required.

There exist two basic approaches for the design and


implementation of embedded firmware, namely;

 The Super loop based approach

 The Embedded Operating System based approach

The decision on which approach needs to be adopted for


firmware development is purely dependent on the complexity
and system requirements

1. The Super loop:

The Super loop based firmware development approach is


suitable for applications that are not time critical and where
wh 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 codeis 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:

1. Configure the common parameters and perform


initialization for variouss hardware components
memory, registers etc.
2. Start the first task and execute it
3. Execute the second task
4. Execute
the next
task5. :
6. :
7. Execute the last defined task
8. Jump back to the first task and follow the same flow.
The ‘C’ pseudo code for the super loop is

given belowvoid main ()


{
Confi
gurati
ons
();
Initia
lizati
ons
();
while (1)
{
Task 1 ();
Task 2 ();
:
:
Task n ();
}
}
Advantages of super loop based approach

1. Doesn’t require an Operating System for task scheduling and monitoring andfree
from OS related overheads
2. Simple and straight forward design

3. Reduced memory footprint

Disadvantages of super loop based approach


1. Non Real time in execution behavior (As the number of tasks increases the
frequency at which a task gets CPU time for execution also increases)

2. Any issues in any task execution may affect the functioning of the product (This
can be effectively tackled by using Watch Dog Timers for task execution
monitoring).

Enhancements:
 Combine Super loop based technique with interrupts

 Execute the tasks (like keyboard handling) which require Real


time attentionas Interrupt Service routines.

Embedded OS based Approach:


The embedded device contains an Embedded Operating System
which canbe one of the following:

 A Real Time Operating System (RTOS).

 A Customized General Purpose Operating System (GPOS).

 The Embedded OS is responsible for scheduling the


execution of user tasks and the allocation of system
resources among multiple tasks. It Involves lot of OS
related overheads apart from managing and executing
user defined tasks.

 Microsoft® Windows XP Embedded is an example of


GPOS for embedded devices.
 Point of Sale (PoS) terminals, Gaming Stations, Tablet
PCs etc are examples of embedded devices running on
embedded GPOSs

 ‘Windows
Windows CE’, ‘Windows Mobile’,‘QNX’, ‘VxWorks
VxWorks’,
‘ThreadX
ThreadX’, ‘MicroC/OS-II’, ‘Embedded Linux’, ‘Symbian
Symbian’
etc are examples of RTOS employed in Embedded
Product development

 Mobile Phones, PDAs, Flight Control Systems etc are


examples of embedded devices that runs on RTOS.
RTOS

Embedded firmware Development Languages


 Assembly Language

 High level language.

o Subset of C (Embedded C)
o Subset of C++ (Embedded C++)
o Any other high level language with supported Cross-compiler
Cross
 Mix of Assembly & High level Language
o Mixing High Level Language (Like C) with Assembly Code
o Mixing Assembly code with High Level Language (Like C)
o Inline Assembly.

Assembly language based firmware development


 Assembly Language’ is the human readable notation of ‘machine
language’. ‘Machine language’ is a processor und
understandable
erstandable language.
Machine language is a binary representation and it consists of 1s and 0s.
Assembly language and machine languages are processor/controller
dependent.

 An Assembly language program written for one processor/controller family


will not work with others. Assembly language programming is the process of
writing processor specific machine code in mnemonic form, converting the
mnemonics into actual processor instructions (machine language) and
associated data using an assembler

 The general format of an assembly language instruction is an Opcode


followed by Operands. The Opcode tells the processor/controller what to do
and the Operands provide the data and information required to perform the action
specified by the opcode.

The operand may be a single operand, dual operand or more.. For example,

MOV A, #30

The above 8051 instruction 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 this.

01110100 00011110

The first 8 bit binary value 01110100 represents the opcode MOV A
and the second 8 bit binary value 00011110 represents the operand 30.

Assembly language instructions are written one per line. A machine code program
consists of a sequence of assembly language instructions, where each statement contains
a mnemonic (Opcode + Operand)
Operand). Each line of an assembly language program is split
into four fields as:
LABEL OPCODE OPERAND COMMENTS

LABEL is an optional
tional field. A ‘LABEL’ is an identifier used extensively in programs to
reduce the reliance 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 . Labels are always suffixed by a colon and begin with a valid
character. Labels can contain number from 0 to 9 and special character _ (underscore).
;###############################################################
; SUBROUTINE FOR GENERATING DELAY
; DELAY PARAMETR PASSED THROUGH REGISTER R1
; RETURN VALUE NONE,REGISTERS USED: R0, R1
;###############################################################
##### DELAY: MOV R0, #255 ;Load Register R0 with 255

DJNZ R1, DELAY; Decrement R1 and loop till R1= 0

RET ; Return to calling program


The symbol ; represents the start of a comment. Assembler ignores the text in a line
after the ; symbol while assembling the program
DELAY is a label for representing the start address of the memory location where the
piece of code is located in code memory
The above piece of code can be executed by giving the label DELAY as part of the
instruction. E.g. LCALL DELAY; LMP DELAY
Steps followed in conversion of assembly language program to machine
language program
Source File to Hex File Translation:
The Assembly language program written in assembly code is saved as
.asm (Assembly file) file or a .src (source) file or a format supported by the assembler
Similar to ‘C’ and other high level language programming, it is possible
poss to have multiple
source files called modules in assembly language programming. Each module is
represented by a ‘.asm’ or ‘.src
.src’ file or the assembler supported file format similar to the
‘.c’ files in C programming
The software utility called ‘Assembler’ performs the translation of assembly code to
machine code
Note: 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.

Figure 5: Assembly Language to machine language conversion process

 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.

4. Code Reverse Engineering:

 It is the process of understanding the technology behind a


product by extracting the information from the finished
product.
 It can easily be converted into assembly code using a dis-
assembler program for the target machine.
Drawbacks:

1. High Development time:


 The developer takes lot of time to study about architecture
,memory organization, addressing modes and instruction
set of target processor/controller.
 More lines of assembly code is required for performing a
simple action.
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 targetprocessor/controller is required.

High Level language based firmware design


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.

High Level Language – Source File to Hex File Translation

 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)

Library Files

Source File 1
Module
(.c /.c++ etc) Object File 1
Cross-compiler
(Module-1)

Source File 2
Module
(.c /.c++ etc) Object File 2
Cross-compiler
(Module-2)

Object to Hex File Linker/


Absolute Object File
Converter Locator

Figure: High level language to machine language conversion process

Advantages of High level language firmware design

 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

 Portability: An Application written in high level


language for particular target processor /controller can be
easily be converted to another target processor/controller
specific application with little or less effort.
Drawbacks of High level language firmware design
• The cross compilers may not be efficient in generating
the optimized target processor specific instructions.

• Target images created by such compilers may be


messy and non-
non optimized in terms of performance as
well as code size.

• The investment required


required for high level language based
development tools (IDE) is high compared to
Assembly Language based firmware development
tools.

Mixing of Assembly Language with High Level Language


Embedded firmware development may require the mixing of
Assembly Language with high level language or vice versa.

High Level language and low level language can be mixed in


three different ways

 Mixing Assembly Language with High level language like ‘C’

 Mixing High level language like ‘C’ with Assembly Language

 In line Ass
Assembly

The passing of parameters and return values between the high


level and low level language is cross-compiler specific

1. Mixing Assembly Language with High level language like ‘C’


(Assembly Language with ‘C’):

Mixing assembly language with high level language becomes necessary


because of the following reasons,

 Assembly routines are mixed with ‘C’ in situations where the entire
program is written in ‘C’ and the cross compiler has built in support for
implementing certain features like ISR( Interrupt service subroutine).

 If the programmer er wants to take advantage of the speed and optimized


code offered by the machine code generated by hand written assembly
rather than cross compiler generated machine code.

 For accessing certain low level hardware , the timing specifications may
be very critical and cross compiler generated machine code 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 in the above situations.

Mixing ‘C’ and assembly is little complicated. Because 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.
2. Mixing High level language like ‘C’ with
Assembly Language(‘C’ with Assembly Language)

Mixing of code written in a HLL(High level language) like C and assembly


language is useful in the following scenarios.

1. The source code is already available in assembly and a routine written in


HLL like C needs to be included to the existing code.

2. The entire source code is planned in Assembly code for various reasons
like optimized code, optimal performance, efficient code memory
utilization but some portions of the code may be tedious and difficult to
code in assembly. For example 16-bit multiplication/division program in
8051 assembly code has around 35 lines of code. Built in library functions
written in C must be included

3. To include and a routine written in a high level language needs to be


included to the existing code.

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. A typical example for c51 cross compiler is given below.(note:
a cross compiler compiles HLL program to processor specific machine code.
C51 is the cross compiler used for 8051 microcontroller).

lcall _Cfunct

The above instruction is invoking a function written in C Cfunct. The


prefix _ informs the compiler that the parameters to the function are passed
through registers. If the prefix _ is absent, then the compiler understands that
the parameters are passed through fixed memory locations.

Note - Passing parameter to the function and returning values from the
function using CPU registers , stack memory and fixed memory. Its
implementation is cross compiler dependent and varies across compilers.

3. In line Assembly:
 Inline assembly is another technique for inserting the target
processor/controller specific assembly instructions at any location
of source code written in high level language ‘C’

 Inline Assembly avoids the delay in calling an assembly routine


from a ‘C’ code.

 Special keywords are used to indicate the start and end of Assembly
instructions

E.g #pragma asm

Mov A,#13H

#pragma ensasm
Keil C51 uses the keywords #pragma asm and #pragma endasm to
indicatea block of code written in assembly.

You might also like