With this book, Christopher Kormanyos delivers a highly practical guide to programming real-time embedded microcontroller systems in C++. It is divided into three parts plus several appendices. Part I provides a foundation for real-time C++ by covering language technologies, including object-oriented methods, template programming and optimization. Next, part II presents detailed descriptions of a variety of C++ components that are widely used in microcontroller programming. It details some of C++’s most powerful language elements, such as class types, templates and the STL, to develop components for microcontroller register access, low-level drivers, custom memory management, embedded containers, multitasking, etc. Finally, part III describes mathematical methods and generic utilities that can be employed to solve recurring problems in real-time C++. The appendices include a brief C++ language tutorial, information on the real-time C++ development environment and instructions for building GNU GCC cross-compilers and a microcontroller circuit. For this second edition, the most recent specification of C++14 in ISO/IEC 14882:2014 is used throughout the text. Several sections on new C++14 functionality have been added, and various others reworked to reflect changes in the standard. Also two new sample projects are introduced, and various user suggestions have been incorporated. To facilitate portability, no libraries other than those specified in the language standard itself are used. Efficiency is always in focus and numerous examples are backed up with real-time performance measurements and size analyses that quantify the true costs of the code down to the very last byte and microsecond. The target audience of this book mainly consists of students and professionals interested in real-time C++. Readers should be familiar with C or another programming language and will benefit most if they have had some previous experience with microcontroller electronics and the performance and size issues prevalent in embedded systems programming.
Author(s): Christopher Kormanyos
Edition: 2
Publisher: Springer
Year: 2015
Language: English
Pages: 378
Preface to the Second Edition
Preface to the First Edition
Contents
Acronyms
Part I Language Technologies for Real-Time C++
1 Getting Started with Real-Time C++
1.1 The LED Program
1.2 The Syntax of C++
1.3 Class Types
1.4 Members
1.5 Objects and Instances
1.6 #include
1.7 Namespaces
1.8 C++ Standard Library
1.9 The main() Subroutine
1.10 Low-Level Register Access
1.11 Compile-Time Constant
References
2 Working with a Real-Time C++ Program on a Board
2.1 The Target Hardware
2.2 Build and Flash the LED Program
2.3 Adding Timing for Visible LED Toggling
2.4 Run and Reset the LED Program
2.5 Recognizing and Handling Errors and Warnings
2.6 Reaching the Right Efficiency
References
3 An Easy Jump-Start in Real-Time C++
3.1 Declare Locals When Used
3.2 Fixed-Size Integer Types
3.3 The bool Type
3.4 Organization with Namespaces
3.5 Basic Classes
3.6 Basic Templates
3.7 nullptr Replaces NULL
3.8 Generalized Constant Expressions with constexpr
3.9 static_assert
3.10 Using
3.11 std::array
3.12 Basic STL Algorithms
3.13
3.14 atomic_load() and atomic_store()
3.15 Digit Separators
3.16 Binary Literals
3.17 User-Defined Literals
Reference
4 Object-Oriented Techniques for Microcontrollers
4.1 Object Oriented Programming
4.2 Objects and Encapsulation
4.3 Inheritance
4.4 Dynamic Polymorphism
4.5 The Real Overhead of Dynamic Polymorphism
4.6 Pure Virtual and Abstract
4.7 Class Relationships
4.8 Non-Copyable Classes
4.9 Constant Methods
4.10 Static Constant Integral Members
4.11 Class Friends
4.12 Virtual Is Unavailable in the Base Class Constructor
References
5 C++ Templates for Microcontrollers
5.1 Template Functions
5.2 Template Scalability, Code Re-Use and Efficiency
5.3 Template Member Functions
5.4 Template Class Types
5.5 Template Default Parameters
5.6 Template Specialization
5.7 Static Polymorphism
5.8 Using the STL with Microcontrollers
5.9 Variadic Templates
5.10 Template Metaprogramming
5.11 Tuples and Generic Metaprogramming
5.12 Variable Templates
References
6 Optimized C++ Programming for Microcontrollers
6.1 Use Compiler Optimization Settings
6.2 Know the Microcontroller's Performance
6.3 Know an Algorithm's Complexity
6.4 Use Assembly Listings
6.5 Use Map Files
6.6 Understand Name Mangling and De-Mangling
6.7 Know When to Use Assembly and When Not to
6.8 Use Comments Sparingly
6.9 Simplify Code with typedef
6.10 Use Native Integer Types
6.11 Use Scaling with Powers of Two
6.12 Potentially Replace Multiply with Shift-and-Add
6.13 Consider Advantageous Hardware Dimensioning
6.14 Consider ROM-Ability
6.15 Minimize the Interrupt Frame
6.16 Use Custom Memory Management
6.17 Use the STL Consistently
6.18 Use Lambda Expressions
6.19 Use Templates and Scalability
6.20 Use Metaprogramming to Unroll Loops
References
Part II Components for Real-Time C++
7 Accessing Microcontroller Registers
7.1 Defining Constant Register Addresses
7.2 Using Templates for Register Access
7.3 Generic Templates for Register Access
7.4 Bit-Mapped Structures
Reference
8 The Right Start
8.1 The Startup Code
8.2 Initializing RAM
8.3 Initializing the Static Constructors
8.4 The Connection between the Linker and Startup
8.5 Understand Static Initialization Rules
8.6 Avoid Using Uninitialized Objects
8.7 Jump to main() and Never return
8.8 When in main(), What Comes Next?
References
9 Low-Level Hardware Drivers in C++
9.1 An I/O Port Pin Driver Template Class
9.2 Programming Interrupts in C++
9.3 Implementing a System-Tick
9.4 A Software PWM Template Class
9.5 A Serial SPITM Driver Class
9.6 CPU-Load Monitors
9.7 Controlling a Seven-Segment Display
References
10 Custom Memory Management
10.1 Dynamic Memory Considerations
10.2 Using Placement-new
10.3 Allocators and STL Containers
10.4 The Standard Allocator
10.5 Writing a Specialized ring_allocator
10.6 Using ring_allocator and Other Allocators
10.7 Recognizing and Handling Memory Limitations
References
11 C++ Multitasking
11.1 Multitasking Schedulers
11.2 Task Timing
11.3 The Task Control Block
11.4 The Task List
11.5 The Scheduler
11.6 Extended Multitasking
11.7 Preemptive Multitasking
11.8 The C++ Thread Support Library
References
Part III Mathematics and Utilities for Real-Time C++
12 Floating-Point Mathematics
12.1 Floating-Point Arithmetic
12.2 Mathematical Constants
12.3 Elementary Functions
12.4 Special Functions
12.5 Complex-Valued Mathematics
12.6 Compile-Time Evaluation of Functions with constexpr
12.7 Generic Numeric Programming
References
13 Fixed-Point Mathematics
13.1 Fixed-Point Data Types
13.2 A Scalable Fixed-Point Template Class
13.3 Using the fixed_point Class
13.4 Fixed-Point Elementary Transcendental Functions
13.5 A Specialization of std::numeric_limits
References
14 High-Performance Digital Filters
14.1 A Floating-Point Order-1 Filter
14.2 An Order-1 Integer Filter
14.3 Order-N Integer FIR Filters
14.4 Some Worked-Out Filter Examples
References
15 C++ Utilities
15.1 The nothing Structure
15.2 The noncopyable Class
15.3 A Template timer Class
15.4 Linear Interpolation
15.5 A circular_buffer Template Class
15.6 The Boost Library
References
16 Extending the C++ Standard Library and the STL
16.1 Defining the Custom dynamic_array Container
16.2 Implementing and Using dynamic_array
16.3 Writing Parts of the C++ Library if None Is Available
16.4 Implementation Notes for Parts of the C++ Library and STL
16.5 Providing now() for 's High-Resolution Clock
Reference
17 Additional Reading
17.1 Literature List
References
Appendix A
A Tutorial for Real-Time C++
Appendix B
A Robust Real-Time C++ Environment
Appendix C
Building and Installing GNU GCC CrossCompilers
Appendix D
Building a Microcontroller Circuit
Glossary
Index