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. Part I Language Technologies For Real-time C++ -- 1 Getting Started With Real-time C++ -- 2 Working With A Real-time C++ Program On A Board -- 3 An Easy Jump-start In Real-time C++ -- 4 Object-oriented Techniques For Microcontrollers -- 5 C++ Templates For Microcontrollers -- 6 Optimized C++ Programming For Microcontrollers -- Part Ii Components For Real-time C++ -- 7 Accessing Microcontroller Registers -- 8 The Right Start -- 9 Low-level Hardware Drivers In C++ -- 10 Custom Memory Management -- 11 C++ Multitasking -- Part Iii Mathematics And Utilities For Real-time C++ -- 12 Floating-point Mathematics -- 13 Fixed-point Mathematics -- 14 High-performance Digital Filters -- 15 C++ Utilities -- 16 Extending The C++ Standard Library And The Stl -- 17 Additional Reading -- Part Iv Appendices -- A A Tutorial For Real-time C++ -- B A Robust Real-time C++ Environment -- C Building And Installing Gnu Gcc Cross Compilers -- D Building A Microcontroller Circuit. By Christopher Kormanyos. Preface to the Second Edition 6 Preface to the First Edition 9 Contents 16 Acronyms 22 Part I Language Technologies for Real-Time C++ 26 1 Getting Started with Real-Time C++ 27 1.1 The LED Program 27 1.2 The Syntax of C++ 30 1.3 Class Types 30 1.4 Members 33 1.5 Objects and Instances 35 1.6 #include 36 1.7 Namespaces 37 1.8 C++ Standard Library 39 1.9 The main() Subroutine 39 1.10 Low-Level Register Access 40 1.11 Compile-Time Constant 41 References 42 2 Working with a Real-Time C++ Program on a Board 43 2.1 The Target Hardware 43 2.2 Build and Flash the LED Program 44 2.3 Adding Timing for Visible LED Toggling 48 2.4 Run and Reset the LED Program 50 2.5 Recognizing and Handling Errors and Warnings 51 2.6 Reaching the Right Efficiency 53 References 55 3 An Easy Jump-Start in Real-Time C++ 57 3.1 Declare Locals When Used 57 3.2 Fixed-Size Integer Types 58 3.3 The bool Type 60 3.4 Organization with Namespaces 61 3.5 Basic Classes 62 3.6 Basic Templates 63 3.7 nullptr Replaces NULL 64 3.8 Generalized Constant Expressions with constexpr 65 3.9 static_assert 66 3.10 Using 67 3.11 std::array 67 3.12 Basic STL Algorithms 68 3.13 69 3.14 atomic_load() and atomic_store() 70 3.15 Digit Separators 70 3.16 Binary Literals 71 3.17 User-Defined Literals 72 Reference 73 4 Object-Oriented Techniques for Microcontrollers 74 4.1 Object Oriented Programming 74 4.2 Objects and Encapsulation 79 4.3 Inheritance 80 4.4 Dynamic Polymorphism 82 4.5 The Real Overhead of Dynamic Polymorphism 83 4.6 Pure Virtual and Abstract 84 4.7 Class Relationships 85 4.8 Non-Copyable Classes 87 4.9 Constant Methods 88 4.10 Static Constant Integral Members 91 4.11 Class Friends 92 4.12 Virtual Is Unavailable in the Base Class Constructor 94 References 97 5 C++ Templates for Microcontrollers 98 5.1 Template Functions 98 5.2 Template Scalability, Code Re-Use and Efficiency 100 5.3 Template Member Functions 102 5.4 Template Class Types 105 5.5 Template Default Parameters 106 5.6 Template Specialization 108 5.7 Static Polymorphism 109 5.8 Using the STL with Microcontrollers 112 5.9 Variadic Templates 114 5.10 Template Metaprogramming 116 5.11 Tuples and Generic Metaprogramming 119 5.12 Variable Templates 122 References 124 6 Optimized C++ Programming for Microcontrollers 125 6.1 Use Compiler Optimization Settings 125 6.2 Know the Microcontroller's Performance 128 6.3 Know an Algorithm's Complexity 130 6.4 Use Assembly Listings 131 6.5 Use Map Files 132 6.6 Understand Name Mangling and De-Mangling 133 6.7 Know When to Use Assembly and When Not to 134 6.8 Use Comments Sparingly 136 6.9 Simplify Code with typedef 136 6.10 Use Native Integer Types 138 6.11 Use Scaling with Powers of Two 140 6.12 Potentially Replace Multiply with Shift-and-Add 141 6.13 Consider Advantageous Hardware Dimensioning 142 6.14 Consider ROM-Ability 144 6.15 Minimize the Interrupt Frame 145 6.16 Use Custom Memory Management 148 6.17 Use the STL Consistently 148 6.18 Use Lambda Expressions 150 6.19 Use Templates and Scalability 151 6.20 Use Metaprogramming to Unroll Loops 152 References 152 Part II Components for Real-Time C++ 153 7 Accessing Microcontroller Registers 154 7.1 Defining Constant Register Addresses 154 7.2 Using Templates for Register Access 156 7.3 Generic Templates for Register Access 158 7.4 Bit-Mapped Structures 161 Reference 163 8 The Right Start 164 8.1 The Startup Code 164 8.2 Initializing RAM 166 8.3 Initializing the Static Constructors 168 8.4 The Connection between the Linker and Startup 170 8.5 Understand Static Initialization Rules 172 8.6 Avoid Using Uninitialized Objects 173 8.7 Jump to main() and Never return 175 8.8 When in main(), What Comes Next? 176 References 177 9 Low-Level Hardware Drivers in C++ 178 9.1 An I/O Port Pin Driver Template Class 178 9.2 Programming Interrupts in C++ 181 9.3 Implementing a System-Tick 185 9.4 A Software PWM Template Class 188 9.5 A Serial SPITM Driver Class 192 9.6 CPU-Load Monitors 197 9.7 Controlling a Seven-Segment Display 199 References 204 10 Custom Memory Management 206 10.1 Dynamic Memory Considerations 206 10.2 Using Placement-new 207 10.3 Allocators and STL Containers 209 10.4 The Standard Allocator 210 10.5 Writing a Specialized ring_allocator 211 10.6 Using ring_allocator and Other Allocators 214 10.7 Recognizing and Handling Memory Limitations 216 References 218 11 C++ Multitasking 219 11.1 Multitasking Schedulers 219 11.2 Task Timing 221 11.3 The Task Control Block 222 11.4 The Task List 224 11.5 The Scheduler 225 11.6 Extended Multitasking 226 11.7 Preemptive Multitasking 228 11.8 The C++ Thread Support Library 229 References 230 Part III Mathematics and Utilities for Real-Time C++ 231 12 Floating-Point Mathematics 232 12.1 Floating-Point Arithmetic 232 12.2 Mathematical Constants 235 12.3 Elementary Functions 237 12.4 Special Functions 238 12.5 Complex-Valued Mathematics 244 12.6 Compile-Time Evaluation of Functions with constexpr 247 12.7 Generic Numeric Programming 250 References 257 13 Fixed-Point Mathematics 259 13.1 Fixed-Point Data Types 259 13.2 A Scalable Fixed-Point Template Class 262 13.3 Using the fixed_point Class 265 13.4 Fixed-Point Elementary Transcendental Functions 268 13.5 A Specialization of std::numeric_limits 278 References 280 14 High-Performance Digital Filters 281 14.1 A Floating-Point Order-1 Filter 281 14.2 An Order-1 Integer Filter 284 14.3 Order-N Integer FIR Filters 287 14.4 Some Worked-Out Filter Examples 292 References 297 15 C++ Utilities 298 15.1 The nothing Structure 298 15.2 The noncopyable Class 301 15.3 A Template timer Class 303 15.4 Linear Interpolation 306 15.5 A circular_buffer Template Class 309 15.6 The Boost Library 313 References 314 16 Extending the C++ Standard Library and the STL 315 16.1 Defining the Custom dynamic_array Container 315 16.2 Implementing and Using dynamic_array 317 16.3 Writing Parts of the C++ Library if None Is Available 321 16.4 Implementation Notes for Parts of the C++ Library and STL 322 16.5 Providing now() for 's High-Resolution Clock 328 Reference 329 17 Additional Reading 330 17.1 Literature List 330 References 331 Appendix A A Tutorial for Real-Time C++ 334 Appendix B A Robust Real-Time C++ Environment 359 Appendix C Building and Installing GNU GCC CrossCompilers 364 Appendix D Building a Microcontroller Circuit 375 Glossary 379 Index 380 Front Matter....Pages i-xxviii Front Matter....Pages 1-1 Getting Started with Real-Time C++....Pages 3-18 Working with a Real-Time C++ Program on a Board....Pages 19-32 An Easy Jump-Start in Real-Time C++....Pages 33-49 Object-Oriented Techniques for Microcontrollers....Pages 51-74 C++ Templates for Microcontrollers....Pages 75-101 Optimized C++ Programming for Microcontrollers....Pages 103-130 Front Matter....Pages 131-131 Accessing Microcontroller Registers....Pages 133-142 The Right Start....Pages 143-156 Low-Level Hardware Drivers in C++....Pages 157-184 Custom Memory Management....Pages 185-197 C++ Multitasking....Pages 199-210 Front Matter....Pages 211-211 Floating-Point Mathematics....Pages 213-239 Fixed-Point Mathematics....Pages 241-262 High-Performance Digital Filters....Pages 263-279 C++ Utilities....Pages 281-297 Extending the C++ Standard Library and the STL....Pages 299-313 Additional Reading....Pages 315-318 Back Matter....Pages 319-378