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 4: C++ for Embedded - Case Study No. 1    22 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 ] How big is your embedded software development team? Is it growing?

5 Encapsulation of Expertise
Software teams getting larger Team members each have own expertise Use objects to contain that knowledge can be used by others without understanding distribution and re-use simplified 2 case studies non-volatile RAM write-only port

6 Using NVRAM Retains data after the power has been removed
Often used for configuration parameters Integrity checking required Facility for initialization Need transparent access

7 Case Study: NVRAM Issues: Access time may be limited
software must control timing Memory may have limited lifetime software must even out the “wear” May have block access software must manage buffering

8 Software Requirements
Declaration of NVRAM variables [objects] Means to set the value Means to retrieve the value Must be done in a natural fashion

9 Code using NVRAM void main(void) { nvramchar x(2, 0x80000, 16);
nvramchar y(3, 0x80000, 16); char z=99; x = 3; y = z; z = x + y; }

10 nvramchar class characteristics
Constructor with 3 parameters: NVRAM base address size offset Overloaded = operator to set the object’s value Overloaded char operator to retrieve object’s value

11 Assumptions in Case Study
No special timing needs could easily be added later Last 7 bytes are for “housekeeping”: checksum byte exclusive-OR of all the data bytes signature string “NVRAM” NULL terminated Re-initialization to all zeros is OK

12 NVRAM Architecture Address Offset Data Size Checksum Signature

13 nvramchar class class nvramchar { char* base; int size; char* data;
int checksum(void); int sigcheck(void); void init(void); public: nvramchar(int offset, unsigned baseaddress, int totalsize); operator =(char); operator char(void); };

14 [ 2 ] What return type does a constructor or destructor have?

15 nvramchar Constructor
nvramchar::nvramchar(int offset, unsigned baseaddress, int totalsize) { base = (char *)baseaddress; size = totalsize; data = base + offset; if (!sigcheck() || checksum() != base[size-7]) init(); }

16 checksum() Function int nvramchar::checksum() { int check=0;
for (int i=0; i<size-7; ++i) check ^= base[i]; return check; }

17 sigcheck() Function int nvramchar::sigcheck() {
if (strcmp(&base[size-6], "NVRAM")) return false; else return true; }

18 [ 3 ] why not: return !strcmp(&base[size-6], "NVRAM");

19 sigcheck() Function int nvramchar::sigcheck() {
if (strcmp(&base[size-6], "NVRAM")) return false; else return true; }

20 init() Function void nvramchar::init() { for (int i=0; i<size; i++)
base[i] = 0; strcpy(&base[size-6], "NVRAM"); }

21 Operator Functions nvramchar::operator =(char value) { *data = value;
base[size-7] = checksum(); } nvramchar::operator char() return *data;

22 Enhancements: Memory Timing
Add private member functions: nvramwrite() nvramread() Exclusive access to actual memory addresses Functionality similar or operator functions Timing algorithm added

23 Enhancement: Reentrancy
nvramchar::nvramchar(int offset, unsigned baseaddress, int totalsize) { base = (char *)baseaddress; size = totalsize; data = base + offset; lock(); if (!sigcheck() || checksum() != base[size-7]) init(); unlock(); }

24 Enhancement: Reentrancy
nvramchar::operator =(char value) { lock(); *data = value; base[size-7] = checksum(); unlock(); }

25 Enhancement: Single Size/Location Spec.
Make base address and size member variables static Add single parameter constructor: nvramchar::nvramchar(int offset) { data = base + offset; } but no guarantee that other constructor has been run first

26 Enhancement: Single Size/Location Spec.
Could eliminate 3 parameter constructor entirely Initialize statics: char* nvramchar::base = (char*)0x80000; int nvramchar::size = 16; Or use inheritance base class for whole NVRAM derived class for individual variables Or use default parameters in constructor

27 Other Enhancements Validity flag for each NVRAM element
Flash RAM implementation Public member function to clear NVRAM

28 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

29 [ 4 ] Are peripheral devices generally “software friendly”?

30 Question review ...

31 [ Review - 2 ] What return type does a constructor or destructor have?

32 sigcheck() Function int nvramchar::sigcheck() {
if (strcmp(&base[size-6], "NVRAM")) return false; else return true; }

33 [ Review - 3 ] why not: return !strcmp(&base[size-6], "NVRAM");

34 Colin Walls


Download ppt "Programming Embedded Systems in C++"

Similar presentations


Ads by Google