Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Embedded Systems in C++

Similar presentations


Presentation on theme: "Programming Embedded Systems in C++"— Presentation transcript:

1 Programming Embedded Systems in C++
Day 1: C++ for Embedded Programming  19 May 2014 Colin Walls, Mentor Graphics

2 [ # ] This is what a question slide will look like. Please answer in the chat. Also, questions to me start with ?

3 Agenda Monday: C++ for Embedded Programming Tuesday: C to C++ Migration Strategy Wednesday: C++ and a Real Time Operating System Thursday: Case study #1 Friday: Case Study #2

4 Why is C++ Not More Widely Used?
Conservatism “If it ain’t broke, don’t fix it” Early attempts failed bad tools inappropriate programming practice Fears code bloat performance dynamic memory

5 [ 1 ] Who is already using C++ for embedded applications?

6 The Genealogy of C++ BCPL and B K & R C other OO Cs C++ ANSI C Java

7 The Genealogy of C++ CPL BCPL and B Algol Simula 67 Fortran IV K & R C
other OO Cs C++ ANSI C Java

8 Limitations of C C is extremely powerful and flexible and can therefore be dangerous It has low level capabilities (which are useful for embedded) There are many pitfalls for the unwary Programmers need to be very methodical and disciplined Programmers need to understand how the program behaves at low and high levels Large projects are thus hard to maintain Programmers need an expert knowledge of the application

9 [ 2 ] What other languages make sense for embedded system programming?

10 How C++ May Help Encapsulates and hides areas of high expertise from non-experts into “objects” “An expert is a man who has made all the mistakes that can be made, in a very narrow field.” - Nils Bohr Objects can be used intuitively by non-experts to implement conceptual designs at a high-level Case study …

11 C++ Language Overview End of line comment notation
<code> // <comment> Allows nesting Eases “commenting out” Dynamic memory allocation operators new and delete mostly equivalent to malloc() and free() Mandatory function prototypes

12 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

13 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

14 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

15 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

16 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

17 C++ Language Overview Function parameter default values
void fun(int w, int x, int y=1, int z=3) { ... } fun(1, 2, 3, 4); fun(1, 2, 3); fun(1, 2); fun(1); // error fun(1, 2,, 4) // error

18 C++ Language Overview Reference parameters Pointers
void swap(int& a, int& b) { int temp = a; a = b; b = temp; } ... swap(x, y); Pointers void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; } ... swap(&x, &y);

19 C++ Language Overview Inline functions small functions faster
use inline keyword or include code in class definition only advice to the compiler

20 C++ Language Overview Function overloading
multiple functions with same name different parameter number/types max(int a, int b, int c); max(int a, int b); max(float x, float y); max(1, 2); max(3.4, 5.6); max(1.1, 2.2, 3.3); // error

21 C++ Language Overview Typesafe linkage
function names “mangled” to encode parameter types max(int, int) becomes [say] max_int_int() max(int, int, int) becomes max_int_int_int() linker becomes “C++ aware”

22 Object Oriented Features
Concept of class is key similar to a C structure contains data and code code/data may be hidden (private) class is a new data type operators may be defined automatic constructor/destructor inheritance

23 [ 3 ] When you are coding, what should be your #1 priority?

24 Templates template <class X> void swap(X& x, X& y) { X temp; temp = x; x = y; y = temp; } int i, j; float a, b; ... swap(a, b); swap(i, j);

25 Templates What is the “cost”? In theory, no overhead
code is generated when required compilers process one module at a time instantiations are local to a module duplicate code results solution is “smart” linker also deals with out-of-line inline functions

26 Efficiency - Templates
FILE1.CC max(int, int); max(char, char); FILE2.CC max(int, int); max(char, char); Local Copy max(int, int); Local Copy max(int, int); Local Copy max(char, char); Local Copy max(char, char);

27 Efficiency - Templates
60K unique 50K common 330K total linked image size 100K duplicate 60K unique 50K common Normal Link 60K unique 50K common

28 Efficiency - Templates
60K unique 50K common 330K total linked image size 100K duplicate Optimised Link 60K unique 50K common Link 230K total linked image size 60K unique 50K common

29 Exception Handling Efficient exit from structured code
Use to deal with error conditions Extra code generated must be able to turn EHS off

30 Exception Handling catch try catch throw catch catch throw catch catch

31 Exception Handling try { // application code } catch (int n)
// exception handling code throw 99;

32 Exception Handling Simple error handling
have no catch statement – use terminate() have generic handler: catch (...) { ... }

33 Class Layout for Inheritance
class BaseClass { ... }; class DerivedA : virtual public BaseClass { ... }; class DerivedB : virtual public BaseClass { ... }; class DerivedC : virtual public BaseClass { ... }; class DerivedD : virtual public BaseClass { ... }; class Mderived : DerivedA, DerivedB, DerivedC, DerivedD { ... }; MDerived DerivedA DerivedB DerivedC DerivedD Duplicate copies of BaseClass subobject

34 Class Layout for Inheritance
class BaseClass { ... }; class DerivedA : virtual public BaseClass { ... }; class DerivedB : virtual public BaseClass { ... }; class DerivedC : virtual public BaseClass { ... }; class DerivedD : virtual public BaseClass { ... }; class Mderived : DerivedA, DerivedB, DerivedC, DerivedD { ... }; MDerived DerivedB DerivedC DerivedD BaseClass DerivedA Only a single copy of BaseClass subobject

35 Language Extensions packed and unpacked interrupt asm volatile

36 Approaches Don’t program like a PC Embedded C++
every PC is the same memory infinite and free processor power excess speed of operation the requirement every embedded system is different top-down, heavily OO approach leads to overheads showing up too late Embedded C++ Encapsulation of expertise Beyond C++

37 Agenda Monday: C++ for Embedded Programming Tuesday: C to C++ Migration Strategy Wednesday: C++ and a Real Time Operating System Thursday: Case study #1 Friday: Case Study #2

38 Question review ...

39 [ Review - 2 ] What other languages make sense for embedded system programming?

40 [ Review - 3 ] When you are coding, what should be your #1 priority?

41 Colin Walls


Download ppt "Programming Embedded Systems in C++"

Similar presentations


Ads by Google