Professional Documents
Culture Documents
Abstract
Overview
Embedded systems
Whats special/different
Resource management
memory
Access to hardware
predictability
Absolute addresses
Bits unsigned
Coding standards
Stroustrup/Programming
Embedded systems
Predictability is key
Correctness is even more important than usual
Over a long time and over a large range of conditions, it simply doesnt
Stroustrup/Programming
Embedded systems
Embedded systems
Stroustrup/Programming
Predictability
E.g., you can simply measure the time for an add operation or a virtual
function call and thatll be the cost of every such add operation and every
virtual function call (pipelining, caching, implicit concurrency makes this
somewhat trickier on some modern processors)
Free store allocation (new)
Exception throw
Ideals/aims
Stroustrup/Programming
Time
Space
Communication channels
Files
ROM (Read-Only Memory)
Flash memory
You must take the time to learn about the way your language
features are implemented for a particular platform
Hardware
Operating system
Libraries
Stroustrup/Programming
10
Stroustrup/Programming
11
old object
New object
Allocation delays
Fragmentation
12
Solution: pre-allocate
Global objects
Stacks
Stack:
No fragmentation
Constant time operations
No fragmentation
Constant time operations
Top of stack
Pool:
Stroustrup/Programming
13
Stroustrup/Programming
14
Pool example
// Note: element type known at compile time
// allocation times are completely predictable (and short)
// the user has to pre-calculate the maximum number of elements needed
template<class T, int N>class Pool {
public:
Pool();
// make pool of N Ts construct pools only during startup
T* get();
// get a T from the pool; return 0 if no free Ts
void free(T*);
// return a T given out by get() to the pool
private:
// keep track of T[N] array (e.g., a list of free objects)
};
Pool<Small_buffer,10> sb_pool;
Pool<Status_indicator,200> indicator_pool;
Stroustrup/Programming
15
Stack example
// Note: allocation times completely predictable (and short)
// the user has to pre-calculate the maximum number of elements needed
template<int N>class Stack {
public:
Stack();
// make an N byte stack construct stacks only during startup
void* get(int N); // allocate n bytes from the stack; return 0 if no free space
void free(void* p);
// return the last block returned by get() to the stack
private:
// keep track of an array of N bytes (e.g. a top of stack pointer)
};
Stack<50*1024> my_free_store; // 50K worth of storage to be used as a stack
void* pv1 = my_free_store.get(1024);
int* pi = static_cast<int*>(pv1); // you have to convert memory to objects
void* pv2 = my_free_store.get(50);
Pump_driver* pdriver = static_cast<Pump_driver*>(pv2);
Stroustrup/Programming
16
Templates
Stroustrup/Programming
17
Failing how?
Why?
Power surges/failure
The connector vibrated out of its socket
Falling debris
Falling computer
X-rays
E.g., only when the temperature exceeds 100 F. and the cabinet door is closed
Errors that occur away from the lab are the worst
E.g., on Mars
Stroustrup/Programming
18
Replicate
Self-check
Monitor (sub)systems
19
Absolute addresses
Stroustrup/Programming
20
21
Bit manipulation
&
|
^
<<
>>
~
and
inclusive or
exclusive or
left shift
right shift
ones complement
a:
1 0 1 0 1 0 1 0 0xaa
b:
0 0 0 0 1 1 1 1 0x0f
a&b: 0 0 0 0 1 0 1 0 0x0a
a|b:
1 0 1 0 1 1 1 1 0xaf
a^b:
1 0 1 0 0 1 0 1 0xa5
a<<1:
0 1 0 1 0 1 0 0 0x54
b>>2:
0 0 0 0 0 0 1 1 0x03
~b:
1 1 1 1 0 0 0 0 0xf0
Stroustrup/Programming
22
Bit manipulation
Bitwise operations
Sign bit
8 bits == 1 byte
& (and)
val
| (or)
^ (exclusive or xor)
0 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1
<< (left shift)
>> (right shift)
0xff: 1 1 1 1 1 1 1 1
~ (one's complement)
Basically, what the hardware provides
right:
0 1 0 0 1 1 0 1
true
For example
false
Stroustrup/Programming
23
Bit manipulation
Or |
1 1 1 1 1 1 1 1
val
1 0 1 0 1 0 1 0
And &
Set a bit
0xff:
For example:
enum Flags { bit4=1<<4, bit3=1<<3, bit2=1<<2, bit1=1<<1, bit0=1 };
unsigned char x = bit3 | bit1; // x becomes 8+2
x |= bit2;
// x becomes 8+4+2
if (x&bit3) {
// is bit3 set? (yes, it is)
//
}
unsigned char y = x &(bit4|bit2);
// y becomes 4
Flags z = Flags(bit2|bit0); // the cast is necessary because the compiler
// doesnt know that 5 is in the Flags range
Stroustrup/Programming
24
Bit manipulation
Exclusive or (xor) ^
1 0 1 0 1 0 1 0 0xaa
b:
0 0 0 0 1 1 1 1 0x0f
a^b:
1 0 1 0 0 1 0 1 0xa5
Stroustrup/Programming
25
Unsigned integers
signed
26
Complexity
Inherent complexity
Incidental complexity
Undereducated programmers
Stroustrup/Programming
27
Coding standards
Often they fail and create more complexity than they manage
Stroustrup/Programming
28
Coding standards
C++ coding standards that restrict programming to something like the C subset do
harm
They are not uncommon
And examples
Stroustrup/Programming
29
Coding standards
Common aims
Reliability
Portability
Maintainability
Testability
Reusability
Extensibility
Readability
Stroustrup/Programming
30
No function shall have more than 200 lines (30 would be even better)
// violation!
// violation!
Stroustrup/Programming
31
// ok
// violation: parenthesize (a<b) and (c<=d)
32
Dont look too hard at the code (unless you happen to need a
good simple encryption algorithm for an application); its
simply to give you the flavor of some bit manipulation code
It takes one word (4 bytes at a time)
33
TEA
void encipher(
const unsigned long *const v,
unsigned long *const w,
const unsigned long * const k)
{
unsigned long y = v[0];
unsigned long z = v[1];
unsigned long sum = 0;
unsigned long delta = 0x9E3779B9;
unsigned long n = 32;
while(n-->0) {
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
sum += delta;
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
}
w[0]=y;
w[1]=z;
}
Stroustrup/Programming
34
TEA
void decipher(
const unsigned long *const v,
unsigned long *const w,
const unsigned long * const k)
{
unsigned long y = v[0];
unsigned long z = v[1];
unsigned long sum = 0xC6EF3720;
unsigned long delta = 0x9E3779B9;
unsigned long n = 32;
// sum = delta<<5; in general, sum = delta * n
while(n-->0) {
z -= (y << 4 ^ y >> 5) + y ^ sum + k[sum>>11 & 3];
sum -= delta;
y -= (z << 4 ^ z >> 5) + z ^ sum + k[sum&3];
}
w[0]=y;
w[1]=z;
}
Stroustrup/Programming
35