Download presentation
Presentation is loading. Please wait.
1
Programming Embedded Systems in C++
Day 3: C++ & a Real-Time Operating System 21 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
[ 1 ] Are you using an RTOS? If so, which one?
5
Today’s Topics Hiding the RTOS API Thread local data Paired operations
Using classes to encapsulate the API Thread local data Paired operations Dynamic memory management
6
Today’s Topics Hiding the RTOS API Thread local data Paired operations
Using classes to encapsulate the API Thread local data Paired operations Dynamic memory management
7
C++ and an RTOS Benefits: Wide range of possibilities
application programmers do not need detailed knowledge of RTOS code portability Wide range of possibilities
8
Task/Object Creation Encapsulate API using classes Task example:
have base class for generic task constructor/destructor – task creation empty virtual main() application task defined by deriving class instantiate to create tasks note separation of definition from creation
9
Task/Object Creation class task { unsigned taskID; public: virtual void main(); task() taskID = task_create(main); } ~task() task_delete(taskID); ... };
10
Task/Object Creation Same approach to other RTOS objects: Semaphores
Mailboxes Queues Event flags Memory pools Timers ...
11
Today’s Topics Hiding the RTOS API Thread local data Paired operations
Using classes to encapsulate the API Thread local data Paired operations Dynamic memory management
12
Thread Local Data C++, like C, has no concept of thread
Using a class adds this facility Benefit is ability to have variables (class members) which are local to a thread new scope concept Can share by declaring static Maybe complex variables – e.g. RTOS objects Same idea could be applied to other objects
13
Thread Local Data class mytask : task { unsigned taskID; public: int tld[100]; static int state; void main(); ... }; mytask alpha, beta; alpha.tld and beta.tld are distinct [thread local] alpha.state and beta.state are the same variable
14
Today’s Topics Hiding the RTOS API Thread local data Paired operations
Using classes to encapsulate the API Thread local data Paired operations Dynamic memory management
15
[ 2 ] Is dynamic memory an issue/problem for a real time system?
16
Paired Operations Common requirement for a pair of complementary operations e.g. disable/enable interrupts Problem can be ensuring that both are executed Object oriented “trick” solves the problem Constructor performs first, destructor performs second
17
Paired Operations <normal code> { object declaration (constructor disables interrupts) <critical code> (destructor re-enables interrupts) }
18
Paired Operations class Device_Locked { private: static Semaphore device; // NB static public: Device_Locked() { device.acquire(); }; ~ Device_Locked() { device.release(); }; };
19
Today’s Topics Hiding the RTOS API Thread local data Paired operations
Using classes to encapsulate the API Thread local data Paired operations Dynamic memory management
20
Dynamic Memory Management
Dynamic behavior is troublesome in real time embedded systems Issues: non-determinism key to real time is predictability resource exhaustion also fragmentation
21
Memory Fragmentation #define K (1024) ... char *p1, *p2;
Heap [10K] Heap [10K] #define K (1024) ... char *p1, *p2; p1 = malloc(3*K); p2 = malloc(4*K); free(p1); p1 = malloc(4*K); //fails! [3K free] p2 4K [3K free]
22
Dynamic Memory Define series of partition pools
Geometric series of sizes e.g. 16, 32, 64, 128, 256 bytes Write malloc() to select best fit can be deterministic
23
Dynamic Memory malloc(16); 16 byte pool malloc(9); 32 byte pool
24
Dynamic Memory In C++ new maps onto malloc()
new is also used when an object is created Can create a pool for each class with blocks of exactly the right size Overload new to use this pool Or create a base class that does the work and derive all new classes from that
25
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
26
[ 3 ] What aspect of embedded systems programming is most challenging?
27
Question review ...
28
[ Review - 2 ] Is dynamic memory an issue/problem for a real time system?
29
Colin Walls
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.