Professional Documents
Culture Documents
SW Design PDF
SW Design PDF
Software Design
for
Safety Critical
Microcontroller Applications
by Eberhard De Wille
Page 1
Page 2
Motivation:
Some Famous Software Bugs
Page 3
Also the pilot claims that the aircraft failed to respond to its throttle. `At that point I gave the
order to disconnect the (automatic) throttle and I'm sure that this movement put a mess in the
computer. I push forward the throttles . . . and I had no answer.
Copyright 2010 Eberhard De Wille
Page 4
Page 5
The USS Vincennes was engaged in a heavy ground battle with Iraqi gun boats.
Some days before the USS Stark was sunk by Iraqi gun boats.
The day before F14 fighters were stationed on the civil Iran airport. Their purpose
was not known. The Iran Air Flight 655 started from this airport.
In the stress situation of the battle the civil mode 3 transponder signals were
mistaken for mode 2 hostile military signals. (acoustic detection at this time!)
Warning calls and requests for identification were ignored by the A320.
Two missiles were fired which downed the A320 and killed 289 people
Copyright 2010 Eberhard De Wille
Page 6
Page 7
Calculated
seconds
Seconds
Error in seconds
Deviation (meter)
3600
3599.9966
0.0034
28800
28799.9725
0.0274
55
20
72000
71999.9313
0.0687
137
48
172800
172799.8352
0.1648
330
72
259200
259199.7528
0.2472
494
100
360000
359999.6667
0.3433
687
Page 8
REMEMBER ALWAYS
You can not test quality into software
you have to design it into software!
Page 9
Page 10
Page 11
Global
Variables
D_analog_input
Page 12
/*
/*
/*
/*
x_sw_raw = _getbit(P2,14);
if (x_sw_raw != l_x_1 || cm_tcu == INIT) { /* transition or init call */
l_tm_tr = sy_tmr_1ms;
/* store transition time */
l_x_1 = x_sw_raw;
/* store input level */
l_st_chk = 1;
/* set deb. time check flag */
if ((cm_tcu) == INIT) x_sw = 0;
/* set de-bounced level to 0 */
}
if (l_st_chk && sy_tmr_1ms - l_tm_tr >= L_C_TM_DEB) {
/* de-bounce time is reached after
transition or init call */
x_sw = x_sw_raw;
/* input level is valid */
l_st_chk = 0;
/* clear deb. time check flag */
}
}
Page 13
/*
/*
/*
/*
/*
Page 14
Page 15
Page 16
/*
/*
/*
/*
/*
Page 17
Page 18
Page 19
Page 20
Page 21
Page 22
Page 23
Page 24
Page 25
Frame Work
Operating System
OS Abstraction Layer
Functional
Library
Function
Block 1
Function
Block 2
Function
Block 3
C
Library
Physical Layer
HW Abstraction Layer
Microcontroller Hardware
Copyright 2010 Eberhard De Wille
Page 26
Function
Block 2
Function
Block 2
C
Library
Physical Layer
- Robust design without cross influences
- Portability therefore no dependencies to HW or OS
- Maintainability therefore clear structures and clear interfaces
- High quality and stability of the application
Copyright 2010 Eberhard De Wille
Page 27
U_SHORT us_GetOnOffSwitchRaw(void)
{
U_SHORT
x_sw_raw;
HW Abstraction Layer
x_sw_raw = _getbit(P2,14);
return(x_sw_raw);
}
Page 28
OS Abstraction Layer
Adaptation
To
Operating
System
Adaptation
To
Frame
Work
Page 29
Physical Layer
Page 30
Frame Work
Page 31
Functional
Library
Page 32
C
Library
Page 33
Function Block 2
Absolute
Limitation
Division
C-Library
Absolute
Shifting
Division
Limitation
RefLine
Function Block 3
1.
Generic functions
written only once
2.
Shifting
Division
RefLine
Page 34
C-Library
Absolute
Shifting
Division
Limitation
RefLine
Abs
Shift
Limit
Specific
Algorithms
4.
SW changes do not
imply retesting the
library functions
used
Function Block 2
Abs
Limit
Div
Specific
Algorithms
Function Block 3
TEST
3.
Shift
Div
RefLine
Specific
Algorithms
Page 35
uC C164
C-Library
Absolute
Shifting
Division
Limitation
RefLine
Absolute
Shifting
Division
Limitation
RefLine
5.
Library accommodates
different versions of
same function
depending on uC used
6.
Algorithms can be
generally coded and
does not need to
change because of
changing the uC
uC TMS470
Absolute
Shifting
Division
Limitation
RefLine
uC ST7
Absolute
Shifting
Division
Limitation
RefLine
Copyright 2010 Eberhard De Wille
Page 36
Page 37
Control
Registers
ADC Unit
uC IR Level
uC IR Level
Control
Registers
Timer /
CAPCOM Unit
uC IR Level
Control
Registers
ASC Unit
Control
Registers
SSC Unit
Page 38
Page 39
Control
Registers
ADC Unit
Control
Registers
Timer /
CAPCOM Unit
Control
Registers
SSC Unit
Control
Registers
ASC Unit
Page 40
Page 41
Page 42
Page 43
4ms task
1 ms
Page 44
Page 45
Page 46
Page 47
Page 48
Page 49
Page 50
Module Design
Page 51
Module Design
Object Orientation
Page 52
Page 53
Page 54
Boundaries (interfaces) have to be small and narrow. If they are not, the
definition of the object (s) may not be correct.
Page 55
Sensor Object
Sensor Filtered Data
Sensor Filtering
Object
Sensor Diagnostics
Object
Page 56
Page 57
Module Design
Control Flow and Data Flow
Page 58
Algorithm
Main Function
Get X-Sensor
Raw Data
X-raw
value
X-Sensor
Filtering
X-filtered
value
Algorithm
Term1
X-Sensor
abs calculation
X-filtered
abs value
Algorithm
Term 2
Black arrows are
control flow
Red arrows are
data flow
Fire flags
Algorithm
Weighting
Function
Term data
Page 59
Page 60
Calculate Filtered
Sensor Value
Get Raw
Sensor Value
Term 1
Airbag fire
decision
Term 2
Term
Evaluation
Calculate Filtered
Sensor Value
Term 3
Get Raw
Sensor Value
Term 4
Calculate Filtered
Sensor Value
Calculate Filtered
Sensor Value
Get Raw
Sensor Value
Get Raw
Sensor Value
Page 61
Calculate Filtered
Sensor Value
Get Raw
Sensor Value
Get Filtered
Sensor Value
Term 1
Airbag fire
decision
Term
Evaluation
Term 2
Get Filtered
Sensor Value
Term 3
Term 4
Get Filtered
Sensor Value
Get Filtered
Sensor Value
Page 62
Fire
flags
Fire
decision
Fire flags
Algorithm
Main Function
Fire
descision
term 1
data
Term 1 data
Algorithm
Term1
Algorithm
Weighting
Function
term 2
data
Algorithm
Term 2
Term 2 data
Sensor filtered
value
X-filtered
abs value
X-Sensor Filtering
X-filtered
value
X-Sensor
abs calculation
X-Sensor
Raw Data
X-Sensor
Object
Page 63
Page 64
The use of the technical interface of the C programming language, with its
pass and return parameters is a prerequisite to achieve synchronization of the
control and data flow.
Consequently the use of global variables or unmotivated use of module wide
static variables has to be avoided.
The adherence to an object oriented design will support the idea of a
synchronized control and data flow.
The module design has to be done carefully. Interfaces have to be narrow
(e.g. basic data types instead of arrays and structures) and small (as less
interfaces (e.g. get-functions) as possible.
Copyright 2010 Eberhard De Wille
Page 65
Module Design
Include Structure
Page 66
Page 67
T_VOID
unsigned char
signed char
unsigned short int
short
unsigned long int
long int
void
T_UCHAR;
T_SCHAR;
T_USHORT;
T_SSHORT;
T_ULONG;
T_SLONG;
//
//
//
//
//
//
//
void
8 bit
8 bit
16 bit
16 bit
32 bit
32 bit
unsigned
signed
unsigned
signed
unsigned
signed
Definition of own basic data types allows adaptation to other CPU platforms
Definition of own basic data types allows own simple types based on basic types
Note that PC-Lint and the Lint-MISRA checker NEED redefinitions of the
basic datatypes. If this is not done Lint only come up with a warning concerning
this fact and is not able to find other errors.
Copyright 2010 Eberhard De Wille
Page 69
1
0
#define NULL
#define SET
#define RESET
#define TEST
|=
&=~
&
Page 70
AF_ub_MAX_REAR_THR
AF_ub_FILTER_16_8
AF_ub_HIGHBYTE
4
(T_UBYTE) 8
(T_UBYTE) 8
/*------------------------------------------*/
/*
T Y P E - D e c l a r a t i o n s
*/
/*------------------------------------------*/
typedef struct S_INTAREA
{
T_UBYTE ub_Term1;
T_UBYTE ub_Term2;
T_UBYTE ub_ff;
T_UBYTE ub_stop;
} S_INTAREA;
typedef struct
{
T_UBYTE p1;
T_UBYTE p2;
T_UBYTE p3;
T_UBYTE p4;
} S_FUZZYSET;
S_FUZZYSET
Page 71
Page 72
Module Design
Encapsulation and General Structure
Page 73
Page 74
Page 75
Page 76
Page 77
Page 78
Page 79
Page 80
Module Design
Data
Page 81
ModuleA.h
#ifdef PUBLIC
// No global variables to other software
#endif //PUBLIC
#ifdef PROTECTED
// No global variables to other software
#endif //PROTECTED
#define PRIVATE
static T_UWORD Variable1;
static T_UBYTE Variable2;
#endif //PRIVATE
}
void function2 (T_UWORD value)
{
static T_UWORD Variable4;
T_UWORD Variable5;
}
Page 82
ModuleA.h
#ifdef PUBLIC
// No global variables to other software
#endif //PUBLIC
#ifdef PROTECTED
// No global variables to other software
#endif //PROTECTED
ModuleA.c
#define HOME
#include ModuleA.h"
#undef HOME
#define PRIVATE
static T_UWORD Variable1;
static T_UBYTE Variable2;
#endif //PRIVATE
Page 83
ModuleA.h
#define HOME
#include ModuleA.h"
#undef HOME
#ifdef PUBLIC
// No global variables to other software
#endif //PUBLIC
#ifdef PROTECTED
// No global variables to other software
#endif //PROTECTED
#define PRIVATE
#define INIT 0
#define RUN 1
static T_UWORD Variable1;
static T_UBYTE Variable2;
#endif //PRIVATE
Page 84
ModuleB.c
#define HOME
#include ModuleA.h"
#undef HOME
T_VOID function2 (T_VOID)
{
T_UWORD Variable5;
//some code
Variable123 = Get_Variable1();
}
T_VOID function2 (T_VOID)
{
T_UWORD Variable5;
//some code
Set_Variable1(Variable5);
}
#define HOME
#include ModuleB.h"
#undef HOME
static T_UWORD Variable1;
T_VOID Init_ModuleA (T_VOID)
{
Variable1 = 0;
}
T_VOID function2 (T_VOID)
{
T_UWORD Variable5;
//some code
Variable1 = 12345; // some result
}
T_UWORD Get_Variable1 (T_VOID)
{ return(Variable1); }
T_UWORD Set_Variable1 (T_UWORD value)
{ Variable1 = value; }
Page 85
Pseudo
Assembler Code
Using Pass
Parameters
Register -- Register
Pseudo
Assembler Code
Using Global / Static
Register
Register
Register
ARrsw_ay
-COMP
---
ARrsw_ay
-8191
-8191
Register
Register
Register
Register
ARrsw_ay
-COMP
---
ARrsw_ay
8191
8191
Register
Pass parameters need more stack and use an extra function call BUT
Pass parameters save up to 40% runtime and RAM
Note: Modern compiler may be able to do own optimizations (but this may
depend on the selected optimization options at compile time)
Copyright 2010 Eberhard De Wille
Page 86
Module Design
Interfaces
Page 87
w_err_1 = w_ss;
Page 88
Page 89
Page 90
Page 91
Module Design
Documentation
Page 92
System
Requirements
Search of
Req. ID
Reference of
Req. ID, Perl Check
Search of
Req. ID
System Test
Spec
System Test
Report
Funct. Names
Reference of
Req. ID
SW
Requirements
Search of
Req. ID
SW Integration &
Validation Test Spec
SW Integration &
Validation Test Report
Module Names
Module Names
Search of
Req. ID
Reference of
Req. ID, Perl Check
Reference of
Req. ID, Perl Check
Search of
Req. ID
SW Architecture
& Design
SW Module
Test Spec
Module and
C-Function Names
Module and
C-Function Names
Source Code
SW Module
Test Report
Module and
C-Function Names
Module and
C-Function Names
SGML
Example
Page 93
Page 94
Design Pattern
a few Thoughts
Page 95
Page 96
Page 97