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. " New or Significantly Modified Sections......Page 6 Improved or New Examples and Code Snippets......Page 7 Further Notes on Coding Style......Page 8 Updated Trademarks and Acknowledgments......Page 9 Preface to the Second Edition......Page 10 More Notes on Coding Style......Page 11 About This Book......Page 13 Companion Code, Targets and Tools......Page 14 Notes on Coding Style......Page 15 Acknowledgements......Page 19 Contents......Page 20 Acronyms......Page 26 References......Page 29 Part I Language Technologies for Real-Time C++......Page 31 1.1 The LED Program......Page 32 1.3 Class Types......Page 35 1.4 Members......Page 39 1.5 Objects and Instances......Page 41 1.6 #include......Page 42 1.7 Namespaces......Page 43 1.9 The main() Subroutine......Page 45 1.10 Low-Level Register Access......Page 46 1.11 Compile-Time Constant......Page 47 References......Page 48 2.1 The Target Hardware......Page 49 2.2 Build and Flash the LED Program......Page 50 2.3 Adding Timing for Visible LED Toggling......Page 54 2.5 Recognizing and Handling Errors and Warnings......Page 56 2.6 Reaching the Right Efficiency......Page 58 References......Page 61 3.1 Declare Locals when Used......Page 62 3.2 Fixed-Size Integer Types......Page 63 3.3 The bool Type......Page 65 3.4 Organization with Namespaces......Page 66 3.5 Basic Classes......Page 68 3.6 Basic Templates......Page 69 3.7 nullptr Replaces NULL......Page 72 3.8 Generalized Constant Expressions with constexpr......Page 73 3.10 Using ......Page 74 3.11 std::array......Page 75 3.12 Basic STL Algorithms......Page 76 3.13 ......Page 77 3.15 Digit Separators......Page 78 3.16 Binary Literals......Page 79 3.17 User-Defined Literals......Page 80 3.18 Using alignof and alignas......Page 83 3.19 The Specifier final......Page 84 3.20 Alias as an Alternative to typedef......Page 85 References......Page 87 4.1 Object Oriented Programming......Page 88 4.2 Objects and Encapsulation......Page 93 4.3 Inheritance......Page 94 4.4 Dynamic Polymorphism......Page 96 4.5 The Real Overhead of Dynamic Polymorphism......Page 97 4.6 Pure Virtual and Abstract......Page 98 4.7 Class Relationships......Page 99 4.8 Non-copyable Classes......Page 101 4.9 Constant Methods......Page 102 4.11 Class Friends......Page 106 4.12 Virtual Is Unavailable in the Base Class Constructor......Page 108 References......Page 111 5.1 Template Functions......Page 112 5.2 Template Scalability, Code Re-Use and Efficiency......Page 114 5.3 Template Member Functions......Page 117 5.4 Template Class Types......Page 120 5.5 Template Default Parameters......Page 121 5.6 Template Specialization......Page 122 5.7 Static Polymorphism......Page 124 5.8 Using the STL with Microcontrollers......Page 127 5.9 Variadic Templates......Page 129 5.10 Template Metaprogramming......Page 131 5.11 Tuples and Generic Metaprogramming......Page 134 5.12 Variable Templates......Page 137 References......Page 139 6.1 Use Compiler Optimization Settings......Page 140 6.2 Know the Microcontroller's Performance......Page 144 6.3 Know an Algorithm's Complexity......Page 145 6.5 Use Map Files......Page 147 6.6 Understand Name Mangling and De-mangling......Page 148 6.7 Know When to Use Assembly and When Not to......Page 150 6.8 Use Comments Sparingly......Page 151 6.9 Simplify Code with typedef and Alias......Page 152 6.10 Use Native Integer Types......Page 154 6.11 Use Scaling with Powers of Two......Page 156 6.12 Potentially Replace Multiply with Shift-and-Add......Page 157 6.13 Consider Advantageous Hardware Dimensioning......Page 158 6.14 Consider ROM-Ability......Page 160 6.15 Minimize the Interrupt Frame......Page 161 6.17 Use the STL Consistently......Page 164 6.18 Use Lambda Expressions......Page 166 6.19 Use Templates and Scalability......Page 167 References......Page 168 Part II Components for Real-Time C++......Page 169 7.1 Defining Constant Register Addresses......Page 170 7.2 Using Templates for Register Access......Page 172 7.3 Generic Templates for Register Access......Page 174 7.4 Bit-Mapped Structures......Page 177 Reference......Page 179 8.1 The Startup Code......Page 180 8.2 Initializing RAM......Page 183 8.3 Initializing the Static Constructors......Page 185 8.4 The Connection Between the Linker and Startup......Page 187 8.5 Understand Static Initialization Rules......Page 189 8.6 Avoid Using Uninitialized Objects......Page 190 8.7 Jump to main() and Never return......Page 192 8.8 When in main(), What Comes Next?......Page 193 References......Page 194 9.1 An I/O Port Pin Driver Template Class......Page 195 9.2 Programming Interrupts in C++......Page 198 9.3 Implementing a System-Tick......Page 202 9.4 A Software PWM Template Class......Page 205 9.5 A Serial SPITM Driver Class......Page 209 9.6 CPU-Load Monitors......Page 214 9.7 Controlling a Seven-Segment Display......Page 216 9.8 Animating an RGB LED......Page 222 References......Page 228 10.1 Dynamic Memory Considerations......Page 229 10.2 Using Placement-new......Page 231 10.3 Allocators and STL Containers......Page 232 10.4 The Standard Allocator......Page 233 10.5 Writing a Specialized ring allocator......Page 234 10.6 Using ring allocator and Other Allocators......Page 237 10.7 Recognizing and Handling Memory Limitations......Page 239 References......Page 241 11.1 Multitasking Schedulers......Page 242 11.2 Task Timing......Page 244 11.3 The Task Control Block......Page 245 11.4 The Task List......Page 247 11.5 The Scheduler......Page 248 11.6 Extended Multitasking......Page 249 11.7 Preemptive Multitasking......Page 251 11.8 The C++ Thread Support Library......Page 252 References......Page 253 Part III Mathematics and Utilities for Real-Time C++......Page 254 12.1 Floating-Point Arithmetic......Page 255 12.2 Mathematical Constants......Page 258 12.3 Elementary Functions......Page 260 12.4 Special Functions......Page 261 12.5 Complex-Valued Mathematics......Page 271 12.6 Compile-Time Evaluation of Functions with constexpr......Page 275 12.7 Generic Numeric Programming......Page 279 References......Page 286 13.1 Fixed-Point Data Types......Page 288 13.2 A Scalable Fixed-Point Template Class......Page 291 13.3 Using the fixed point Class......Page 295 13.4 Fixed-Point Elementary Transcendental Functions......Page 297 13.5 A Specialization of std::numeric limits......Page 308 References......Page 310 14.1 A Floating-Point Order-1 Filter......Page 311 14.2 An Order-1 Integer Filter......Page 314 14.3 Order-N Integer FIR Filters......Page 318 14.4 Some Worked-Out Filter Examples......Page 323 References......Page 327 15.1 The nothing Structure......Page 328 15.2 The noncopyable Class......Page 331 15.3 A Template timer Class......Page 333 15.4 Linear Interpolation......Page 336 15.5 A circular buffer Template Class......Page 339 15.6 The Boost Library......Page 343 References......Page 344 16.1 Defining the Custom dynamic array Container......Page 345 16.2 Implementing and Using dynamic array......Page 348 16.4 Implementation Notes for Parts of the C++ Library and STL......Page 352 16.5 Providing now() for 's High-Resolution Clock......Page 361 16.6 Extended-Complex Number Templates......Page 363 References......Page 366 17.1 Accessing C Language Code in C++......Page 367 17.2 An Existing C-Language CRC Library......Page 368 17.3 Wrapping the C-Based CRC Library with C++ Classes......Page 370 17.4 Return to Investigations of Efficiency and Optimization......Page 373 References......Page 374 Appendices......Page 375 References......Page 377 A.1 C++ Cast Operators......Page 380 A.2 Uniform Initialization Syntax......Page 381 A.3 Overloading......Page 383 A.5 Numeric Limits......Page 384 A.6 STL Containers......Page 388 A.7 STL Iterators......Page 390 A.8 STL Algorithms......Page 392 A.9 Lambda Expressions......Page 396 A.10 Initializer Lists......Page 397 A.11 Type Inference and Type Declaration with auto and decltype......Page 398 A.13 Tuple......Page 399 A.14 Regular Expressions......Page 402 A.15 The Library......Page 404 A.16 Using std::any......Page 406 A.17 Structured Binding Declarations......Page 409 References......Page 410 B.1 Addressing the Challenges of Real-Time C++......Page 411 B.2 Software Architecture......Page 413 B.3 Establishing and Adhering to Runtime Limits......Page 414 References......Page 415 C.1 The GCC Prerequisites......Page 416 C.2 Getting Started......Page 417 C.3 Building GMP......Page 418 C.5 Building MPC......Page 419 C.6 Building PPL......Page 420 C.8 Building the Binary Utilities for the Cross Compiler......Page 421 C.9 Building the Cross Compiler......Page 423 C.10 Using the Cross Compiler......Page 424 References......Page 425 D.1 The Circuit Schematic......Page 426 D.2 Assembling the Circuit on a Breadboard......Page 428 References......Page 429 Glossary......Page 430 Index......Page 432 "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 third edition, the most recent specification of C++17 in ISO/IEC 14882:2017 is used throughout the text. Several sections on new C++17 functionality have been added, and various others reworked to reflect changes in the standard. Also several new sample projects are introduced and existing ones extended, 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." -- provided by publisher 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 third edition, the most recent specification of C 17 in ISO/IEC 14882:2017 is used throughout the text. Several sections on new C 17 functionality have been added, and various others reworked to reflect changes in the standard. Also several new sample projects are introduced and existing ones extended, 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.