Unit - 3
Unit - 3
EMBEDDED FIRMWARE
These system components are necessary for the proper functioning of the embedded systems
processors/controllers and firmware execution.
Reset Circuit:
The Reset circuit is essential to ensure 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 (Normally from vector address 0x0000 for conventional processors/controllers.
The reset vector can be relocated to an address for processors/controllers supporting
bootloader.
The reset signal can be either active high (The processor undergoes reset when the reset
pin of the processor is at logic high) or active low (The processor undergoes reset when
the reset pin of the processor is at logic low).
Since the processor operation is synchronised to a clock signal, the reset signal should
be wide enough to give time for the clock oscillator to stabilise before the internal reset
state starts.
The reset pulse width can be adjusted by changing the resistance value R and
capacitance value C
The reset signal to a processor can be applied at power ON through
R1
R2
Q
Reset Pulse
DZ A c ve Low
R3
G ND
The Zener diode (DZ) and transistor Q form the heart of the circuit.
The transistor conducts always when the supply voltage (VCC) is greater than that of
Oscillator Unit
A microprocessor/microcontroller is a digital device made up of digital combinational
and sequential circuits.
The instruction execution of a microprocessor/controller occurs in sync with a clock
signal.
The oscillator unit of the embedded system is responsible for generating the precise
clock for the processor.
• Clock frequency: The execution speed is directly proportional to clock frequency. But
we should not blindly increase the clock frequency to increase execution speed. There
is an upper threshold of clock frequency for logical devices on the chip. Beyond that,
the system becomes unstable and non-functional.
• Power consumption: Power consumption of system is directly proportional to clock
frequency. If we increase the clock frequency, power consumption will also increase.
• Accuracy: The accuracy of program execution depends on the accuracy of clock
signal. The accuracy of quartz oscillator or ceramic resonator is expressed in +/- ppm
(parts per million).
• RTC holds information like current time (In hour, minutes and seconds) in 12 hour /24
hour format, date, month, year, day of the week etc and supplies timing reference to the
system.
• RTC is intended to function even in the absence of power.
• RTCs are available in the form of Integrated Circuits from different semiconductor
manufacturers like Maxim/Dallas, ST Microelectronics etc
• The RTC chip contains a microchip for holding the time and date related information
and backup battery cell for functioning in the absence of power, in a single IC package.
• The RTC chip is interfaced to the processor or controller of the embedded system. For
Operating System based embedded devices, a timing reference is essential for
synchronizing 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
• It helps in rescuing the system if a fault develops and program gets stuck.
• On restart, the system functions normally. The watchdog timer reset is a required feature
in control applications.
Microoprocessor /
Controller
Watchdog
Free Running
Reset Pin
Counter
Watchdog Reset
Embedded firmware refers to the control program and or the configuration settings
that an embedded system developer dumps into code(program) memory of the embedded
system.
Write the program in high level languages like Embedded C/C++ using Integrated
Development Environment. (The IDE will contain an editor, compiler, linker,
debugger, simulator etc. Different families of processors/controllers have different
IDE’s)
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.
The embedded firmware development process starts with the conversion of the
firmware requirements into a program model using various modeling tools and
implementation of the tasks and action using a language.
The firmware design approaches for embedded product are purely dependent on the
complexity 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 Conventional procedural based Firmware Design (or) Super loop-based approach
The decision on which approach needs to be adopted for firmware development is purely
dependent on the complexity and system requirements
The Super loop-based firmware development approach is adapted for applications that are not
time critical and where the response time is not so important (Embedded systems where
missing deadlines are acceptable).
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
5) ….
7) Jump back to the first task and follow the same flow.
Visualisa on of the opera onal sequence list above in terms of a ‘c’ program code as
Void main(){
configura on()
while(1)
task1();
task2();
…..
taskn();
Almost all task in embedded applications are non-ending and are repeated infinitely
throughout the operation.
By analyzing C code we can see that the task 1 to n are performed one after another
Example of Super Loop Based Design is Electronic video game toy containing keypad and
display unit.
The program running inside the product must be designed in such a way that it reads
the key to detect whether 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 reasonable high rate
Even if the application misses the key press, it won‘t create any critical issue, rather
it will treated as a bug in the firmware.
The general format of an assembly language instruc on is opcode followed by the Operand.
Opcode tells what to do and Operand gives the informa on to do the task.
• Here the first 8 bit represents opcode MOV A and next 8 bit represent the operand 30.
Each line of assembly language program split into four fields as given below
Similar to other high level programming, you can have multiple source files called modules in
assembly language programming. Each module is represented by .asm or .src file. This
approach is known as modular programming.
Each source module is written in assembly and is stored in .src or .asm file. Each file
can be assembled separately to examine the syntax errors and incorrect assembly instructions.
On assembling of each .src/.asm file a corresponding object file is created with extension .obj.
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 relocatable segment. It can be placed
at any code memory location and it is responsibility of the linker/loader to assign absolute
address for this module. Absolute address allocation is done at absolute object file creation
stage. Each module can share variables and subroutine among them.
PUBLIC keyword informs the assembler that the variable / function need to be
exported.
EXTRN inform that the variable/function needs to be imported from some other
modules.
While assembling a module , on seeing variable/func on with keyword EXTRN , assembler
understand that these variables or func on come from an external module and it proceeds
assembling the en re module without throwing any errors, though the assembler cannot find
the defini on of variables and implementa on of that func on.
Libraries are specially formatted, ordered program collection of object modules that may be
used by the linker at a later time.
When a linker processes a library, only those object modules in the library that are
necessary to create the program are used. Library files are generated with the extension
‘.lib’.
Library file is some kind of source code hiding technique. An application can make use
of the various function available in the library without revealing the source code.
Example LIB51 from keil software.
Linker and locator is another software utility responsible for “linking the various object
modules in a multi module project and assigning absolute address to each module”.
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 modules 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.
This is the final stage in the conversion of Assembly language to machine understandable
Language.
Hex file is the representation of the machine code and the hex file is dumped into the
code memory of the processor.
Hex file representation varies depending on the target processor make.
o For INTEL processor the target hex file format will be ‘Intel HEX’ and for
Motorola, 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.
Example: ‘OH51’ from keil so ware.
Assembly language based development is the most common technique adopted from the
beginning of the embedded technology development.
Thorough understanding of the processor architecture, memory organization, register set and
mnemonics is very essential for Assembly Language based Development.
o Since the developer is well versed with the target processor architecture and
memory organization, optimized code can be written for performing operations.
This lead to the less utilization of code memory and efficient utilization of data
memory. Memory is the primary concern in any embedded product.
High Performance
o Optimized code not only improves the code memory usage but also improve the
total system performance. Though effective assembly coding optimum
performance can be achieved for target applications.
o 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 routine etc. are making use of direct assembly coding since low level
device specific operation support is not commonly avail with most of the high
level language compilers
o Assembly language programs are much harder to program than high level
languages.
o Developer must have thorough knowledge of architecture, memory
organization and register details of target processor in use.
o Learning the inner details of the processor and its assembly instructions are
high time consuming and it create delay impact in product development.
– Solution: Use a readily available developer who is well versed in target
processor architecture assembly instructions.
o 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
Non- Portable
o Target applications written in assembly instructions are valid only for that
particular family of processors. Example - Application written for Intel X86
family of processors. It cannot be reused for other target processors. If the target
processor changes, a complete rewriting of the application using assembly
instructions for the new target processor is required.
Any High level language with a supported cross compilers for the target processor can be used
for embedded firmware development.
Cross Compilers are used for converting the application development in high level
language into target processor specific assembly code.
Most commonly used language is C. C is well defined easy to use high level language
with extensive cross platform development tool support.
o With High level language, each task can be accomplished by lesser number of
lines of code compared to the target processor specific assembly language
based development
Developer Independency:
o The syntax used by most of the high level languages are universal and a program
written in high level language can be easily be understood by a second person
knowing the syntax of the language.
o High level language always instruct certain set of rules for writing code and
Portability:
Some cross compilers avail for the high level languages may not be so efficient
ingenerating optimized target processor specific instructions
Target images created by such compilers may be messy and no optimized in terms of
performance as well as code size.
High level language and assembly languages are usually mixed in three ways
Mixing Assembly Language with High level Language (Assembly Language with ‘C’)
Assembly routines are mixed with C in situations where entire program is written in C and the
cross compiler in use do not have built in support for implementing certain features like
Interrupt Service Routine or if the programmer want to take the advantage of speed and
optimized code offered by machine code generated by handwritten assembly rather than cross
compiler generated machine code.
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 the assembly routine is invoked from the C code. These are
cross compiler dependent. There is no universal rule for it. You must get the information from
the documentation of cross compiler you are using different cross compilers implement these
features in different ways depending upon the general purpose registers and the memory
supported by the target processor Example
1. Write a simple function in C that passes parameters and return values the way you want your
assembly routine to
2. Use the SRC directive (#pragma SRC) so that compiler generate an SRC file instead of .OBJ
file
3. Compile the C code. Since the SRC directive is specified the .SRC file is generated. The
.SRC file contain the assembly code generated for the C code you wrote
5. Edit .A51 file and insert the assembly code you want to execute in the body of the assembly
function shell included in the .A51 file.
#pragma src
Unsigned char my_assembly_func(unsigned int argument){
return (argument+1);
}
The special compiler directive SRC generates the Assembly code corresponding to the ‘C’
function and each line of the source code is converted to the corresponding Assembly
instruction. You can easily identify the Assembly code generated for each line of the source
code since it is implicitly mentioned in the generated .SRC file. By inspecting this code
segments you can find out which register are used for holding the variables ofthe ‘C’ function
and you can modify the source code by adding the assembly routine you want.
• To include built in library functions written in C language provided by the cross compiler
Most often the functions written in C use parameter passing to the function and returns values
to the calling function. By mixing C with Assembly
• How parameters are passed to the function
Parameters are passed to the function and values are returned from the function using CPU
registers, stack memory and fixed memory. Its implementation is cross compiler dependent and
it varies across cross compilers.
Example Keil C51 cross compiler.C51 allows passing of maximum of three arguments through
general purpose registers R2 to R7. If three arguments are char variables, they are passed to the
functions using registers R7,R6, andR5. If the parameters are int variables they are passing
using register pairs(R7,R6),(R5,R4) and (R4,R3). If the number of arguments is greater than
three, the first three arguments are passed through registers and the rest is passed through fixed
memory locations. Return values are usually passed through fixed memory locations. R7 is
used for returning char value and register pair (R7,R6) used for returning int values.
Inline Assembly
MOV A,#13H
#pragma endasm