DT131B Embedded Systems Programming
Lecture 23: Embedded Software
Adapted from various sources,
Barnett, Cox and O’Cull (2007), Embedded C Programming and the Atmel AVR. (Chapter 3-4)
Dawit Mengistu (dawit.mengistu@hkr.se)
Review: Embedded Systems Arichtecture
Display
Information Processing System
A/D-converters sample-and-hold
D/A converters
Actuators
Sensors Environment Temp, pressure, etc
2
Review: Embedded System Architecture
Micro controller
I/O Unit
Peripherals
Memory
Program
Data
CPU
Comm I/O 123
…
Digital I/O 123
…
Comm Unit (BT, WiFi, LAN…)
Sensors 123
…
ADC 12 …
Human Interface
Display
Timers
Actuators 123
…
Remote
Embedded Software Development
• Firmware:Anembeddedcomputerprogramthat contains instruction codes that are used to operate and/or control devices.
• Notquitehardandnotquitesoft.
• Oftenstoredinpermanentorsemi-permanent memory chips that cannot be removed from the device, and needs special commands and procedures to be changed or overwritten.
Firmware Development
Involves the following:
1. Requirement Identification/specification
2. Knowledge of hardware (micro controllers and their interfaces)
3. Knowledge of programming language
4. Knowledge of development environment
Development Cycle
Initial product development
1∆ Requirement study 2∆ Design
3∆ Program Development 4∆ Testing
5∆ Deployment/marketing
On-field feedback
∆ Requirement changes, identification of bugs ∆ Program Modifications
∆ Testing
∆ Updates/upgrades (online)
Embedded Systems Functional Requirements
• Complex functionality, implement multiple algorithms, user interfaces
• Real-time operation: must finish operations by deadlines,
– deadlines may be hard, firm or soft
• Acceptable Performance
7
Embedded Systems Non- Functional Requirements
• Limited processing power and memory
• Low energy consumption (more important if device is sought to be battery powered)
• Reliability
• Low manufacturing cost
• Smallsize,lowweight,
• Ability to work in harsh environment
8
Design Requirements
• Embedded system developers should consider resource constraints including the following requirements:
– CPU,
– time,
– memory,
– operating system,
– multi-tasking concurrency, – time-to-market,
– Reliability, etc.
• Software engineering methodology is needed for the software design and development.
9
System Design Challenges
• How much hardware do we need: How big is the CPU? Memory?
• How do we meet our performance deadlines: Faster hardware or smarter software?
• How to minimize power: Turn off unnecessary logic? Reduce memory accesses?
• Validation:
– Is the specification correct?
– Does the implementation meet the spec (functional/non- functional)?
– How do we test on real data?
10
Performance Optimization
• Microcontrollers usually have limited speed (few MHZs) and memory (few KB)
• It is thus necessary to optimize firmware and other program codes.
• Optimization requirements may be conflicting and we should prioritize as is necessary.
11
Optimization Goals
• Software optimization is the process of manipulating software code to achieve two main goals:
– Faster execution time.
– Small code size.
– Low power consumption
• In general, there is a trade off between faster execution type and smaller code size and energy efficiency.
Memory Optimization
• Usefulhints:
– Use unsigned int if the data is surely known to take on
positive values only.
– Whenever possible, avoid passing parameters by value, use pointers instead.
– Use boolean types or consider padding of bits from several data units if the information to be stored is just one bit.
– Use float instead of double whenever possible. • …
13
Speed Optimization
• UseDVS(DynamicVoltageScaling)whenpossible. Some microcontrollers have the option of programatically reducing the clock frequency, thereby reducing unnecessary waste of energy.
• Considerseveralfactors:
– Layout of multi dimensional arrays
– Use of for(..) loops
– Rolling out loops
– Use of look up tables instead of compute-intensive operations (trignometric, PRN generators, …)
14
Required Knowledge
• To implement efficient software, the programmer must be familiar with:
– Processor architecture.
– Programming language (C, assembly or linear
assembly).
– The code generation tools (compiler, assembler and linker).
Work at All Levels to Optimize Software
• Algorithmiclevel
(using the most efficient algorithm + data structures)
• High-levelsourcecodetransformations
• Compileroptimizations
• Operatingsystemsupport
(e.g. for minimizing power consumption)
Optimization at Algorithmic level
Choosing best decoding/filtering etc. algorithm+data structures
Example: MPEG-2 data structures: Inverse Discrete Cosine Transform (IDCT) most power/cycle hungry hot spot.
Transformations:
• Replacing “double“ by “float“ (result is of acceptable quality)
Energy consumption reduced to 34%, cycles reduced to 35 %
• Standard IDCT -> “Fast“ IDCT [significant loss of precision].
Energy consumption reduced to 4.86%, cycles reduced to 5.10%
C source file
Parser Optimiser
Code generator
Code Optimization Example
.c .if .opt .asm
Optimising Compiler
Optimizing C Compiler Options
• Example: the ‘C6x optimizing C compiler uses the ANSI C source code and can perform optimisation currently up-to about 80%.
• However, to achieve this level of optimisation, knowledge of different levels of optimisation is essential. Optimisation is performed at different stages and levels.
Common Optimizations
• Revise coding to minimize executions of software-implemented instructions
• More efficient conditionals
• Combine loops and unroll short loops
• Pointer addressing for multiple-indexed arrays
• In-line short functions instead of passing arguments
• Global literals instead of constant parameters
• Tailor macros for specific purpose
• Assembly language for high use routines to
– Eliminate extra compiler inserted instructions – Minimize stack push/pull operations
Optimization Examples: Reducing Software Implemented Instructions
• Using reciprocals to eliminate divides
– use if((Y+Z) < -0.5) ;
– instead of if(1.0/(Y+Z) < -2.0 ) ;
• Using multiplies to replace divides – use X = A/(B*C*D) ; – instead of X = A/B/C/D ;
• Using squares to replace square roots
– use if(Z*Z > R2) ;
– instead of if(Z > SQRT(R2)) ; where Z > 0
• Using pointer for double index arrays to eliminate integer multiply for addresses
– use
– instead of
ptr = &A[0][0] ;
for(L=0 ; L