Skip to main content

Über dieses Buch

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.



Language Technologies for Real-Time C++


Chapter 1. Getting Started with Real-Time C++

C++ programs combine class types that encapsulate objects with procedural subroutines in order to embody the functionality of the application. This chapter presents these main language elements of C++ using a short, intuitive program that toggles an LED on a microcontroller output port pin. In addition, other language features are introduced including the syntax of C++, namespaces, the C++ standard library and optimization with compile time constants. This chapter uses our target system with the 8-bit microcontroller.
Christopher Kormanyos

Chapter 2. Working with a Real-Time C++ Program on a Board

This chapter presents a complete example of building, flashing and executing a microcontroller C++ program using the LED program. The LED program will be built with GCC cross tools in the MinGW/MSYS (MinGW, Home of the MinGW and MSYS Projects, 2012, http://​www.​mingw.​org ) environment. Our target microcontroller is an 8-bit ATMEL® AVR® microcontroller (ATMEL®, 8-bit ATMEL® Microcontroller with 4/8/16/32K Bytes In-System Programmable Flash (ATmega48A, ATmega48PA, ATmega88A, ATmega88PA, ATmega168A, ATmega168PA, ATmega328, ATmega328P), Rev. 8271D-AVR-05/11 (ATMEL®, 2011)). This popular microcontroller has state-of-the-art quality and widespread availability. In addition, there is a well-maintained GCC port for this microcontroller making it well-suited for our example. In the second half of this chapter, we will investigate efficiency aspects and compiler warnings and errors based on the example of the LED program.
Christopher Kormanyos

Chapter 3. An Easy Jump Start in Real-Time C++

Developers new to real-time C++ may want to obtain some useful results quickly before taking the time to master all the intricate details of the C++ language. This chapter addresses this desire by presenting a simple, yet effective, subset of the C++ language specifically designed for those seeking a lightweight and reliable jump start in real-time C++. The C++ subset in this chapter represents a judicious selection of some of the most easy-to-do things in C++ that can potentially be used in the widest possible range of programming situations. The strategy of this C++ subset is shown in the first figure of this chapter.
Christopher Kormanyos

Chapter 4. Object-Oriented Techniques for Microcontrollers

Object-oriented programs are built from various class objects that intuitively embody the application through their actions and interrelations among each other. This chapter introduces object-oriented C++ methods in the domain of embedded systems programming using classes for LEDs, PWM signal generators and communication interfaces.
Christopher Kormanyos

Chapter 5. C++ Templates for Microcontrollers

C++ templates use the same source code for different types. This can improve code flexibility and make programs easier to maintain because code can be written and tested once, yet used with different types. Templates can also be used in generic programming that treats different types with the same semantics. This chapter introduces templates and static polymorphism, the STL, template metaprogramming and some generic programming methods, and shows how these can be used effectively for microcontrollers.
Christopher Kormanyos

Chapter 6. Optimized C++ Programming for Microcontrollers

Embedded systems software, possibly even more than other kinds of software, is time critical and has cost-sensitive size constraints. Literally every bit of the microcontroller software costs precious code space and cycles. Even the most minute software weakness can lead to system-debilitating resource problems. Writing efficient C++ code for microcontrollers mandates command of the language and solid development practices. This chapter aids this endeavor by providing a selection of helpful tips for optimized C++ microcontroller programming.
Christopher Kormanyos

Components for Real-Time C++


Chapter 7. Accessing Microcontroller Registers

Microcontroller programming requires efficient techniques for register access. Registers are used to configure the CPU and peripheral hardware devices such as flash access, clocks, I/O ports, timers, communication interfaces (UART, SPI, CAN (ISO, ISO 11898-1:2003: road vehicles—controller area network (CAN)—Part 1: data link layer and physical signaling. International Organization for Standardization, Geneva, 2003)), etc. This chapter describes C++ methods that can be used to manipulate microcontroller registers. The focus of this chapter is placed on template methods that provide for efficient, scalable and nearly portable register access.
Christopher Kormanyos

Chapter 8. The Right Start

The startup code is called by the microcontroller hardware after reset and is the first code to execute before calling the main() subroutine. The startup code predominantly consists of initialization code and may include, among other things, CPU-initialization, zero-clear RAM initialization, ROM-to-RAM static initialization and static ctor call initialization. The compiler’s default startup code is often tightly bound to the compiler’s runtime libraries and may not be available as source code. In addition, even if the source of the startup code is available, it can be hard to understand because it may be written in assembly and cluttered with a multitude of options required for supporting a variety of chip derivatives. This chapter describes how to implement a custom startup code and its initializations written predominantly in C++, from reset to main().
Christopher Kormanyos

Chapter 9. Low-Level Hardware Drivers in C++

Microcontroller applications usually require low-level drivers for peripheral devices such as I/O ports, interrupts, timers, communication interfaces like UART, CAN (ISO, ISO 11898–1:2003: Road Vehicles – Controller Area Network (CAN) – Part 1: data link layer and physical signaling, International Organization for Standardization, Geneva, 2003), SPITM, etc. This chapter presents several efficient methods for programming peripheral hardware drivers in C++. Low-level drivers are inherently dependent on the microcontroller and its peripherals. Even though the low-level hardware drivers in this chapter are primarily designed for our target with the 8-bit microcontroller, an effort has been made to keep them as portable as possible. In this way, they can be adapted to other microcontrollers. The final two sections in this chapter present complete, non-trivial examples. The first (chapter09_07) controls a seven-segment display (Horowitz and Hill, The art of electronics, 2nd edn., Cambridge University Press, Cambridge, 1989). The second (chapter09_08) produces colorful light by animating an RGB LED.
Christopher Kormanyos

Chapter 10. Custom Memory Management

Effective microcontroller programming in C++ mandates dependable memory management beyond that offered by the language’s default mechanisms. Some of the countless situations in microcontroller programming that require customized memory management include dynamic creation polymorphic objects, using STL containers and mapping hardware devices are. This chapter describes memory management methods that are robust and reliable enough to perform these tasks while adhering to the strict constraints of limited microcontroller memory resources.
Christopher Kormanyos

Chapter 11. C++ Multitasking

A multitasking scheduler is an indispensable tool for providing temporal and functional software distribution. In this chapter, we design a cooperative C++ multitasking scheduler that performs a top-down call of its tasks using time slices and a basic priority mechanism. This multitasking scheduler is compact and portable and can be used for a wide variety of projects ranging from small to large. Toward the end of this chapter, we will discuss additional multitasking features such as extended scheduling with yield and sleep functions and the C++ thread support library.
Christopher Kormanyos

Mathematics and Utilities for Real-Time C++


Chapter 12. Floating-Point Mathematics

This chapter describes floating-point mathematics for real-time C++ using built-in floating-point types such as float double and long double. The first sections of this chapter introduce floating-point arithmetic, mathematical constants, elementary transcendental functions and higher transcendental functions. The last sections of this chapter cover more advanced topics including complex-numbered mathematics, compile-time evaluation of floating-point functions and generic numeric programming.
Christopher Kormanyos

Chapter 13. Fixed-Point Mathematics

Many embedded systems applications need to perform floating-point calculations. As mentioned in the previous chapter, however, small-scale microcontrollers may not have hardware support for floating-point calculations with a floating-point unit (FPU). To avoid potentially slow floating-point emulation libraries manipulating 32-bit single-precision float or even 64-bit double-precision double, many developers elect to use integer-based fixed-point arithmetic. The first part of this chapter describes fixed-point data types and presents a scalable template class representation for fixed-point. In the second part of this chapter, we will use our fixed-point class to compute some elementary transcendental functions, discuss fixed-point efficiency and develop a specialization of std::numeric_limits.
Christopher Kormanyos

Chapter 14. High-Performance Digital Filters

There may be no other signal-processing tool more widely used in embedded software than the digital filter because even the simplest applications usually read some kinds of input signals that need filtering. In this chapter, we will implement several types of finite impulse response (FIR) filters. The first section of this chapter presents a simple order 1 floating-point FIR filter. In order to obtain high performance for filters on microcontrollers without a floating-point unit or digital signal processor (DSP), however, the filters in the rest of this chapter use pure-integer mathematics combined with template design.
Christopher Kormanyos

Chapter 15. C++ Utilities

This chapter presents a selection of C++ utilities that are useful for solving recurring problems in microcontroller programming.
Christopher Kormanyos

Chapter 16. Extending the C++ Standard Library and the STL

The C++ standard library and the STL provide a wide selection of functions, classes and generic containers that can be used in common programming situations. There are, however, times when just the right container or function for a particular programming task is missing from the standard library and the STL. In the first part of this chapter, we will extend the C++ standard library and the STL by developing a custom dynamic_array container that has a functionality that lies between those of std::array and std::vector. Furthermore, one often encounters a good C++ compiler that lacks large parts of the C++ standard library such as the STL, C99 compatibility, the time utilities in <chrono> or the thread support library. The second half of this chapter shows how to emulate partial standard library support with certain self-written parts of the C++ standard library and the STL, including a potential extension of the <complex> library.
Christopher Kormanyos

Chapter 17. Using C-Language Code in C++

This chapter shows how to access C-language code within a C++ project. The subject of the example is CRC calculations (cyclic redundancy check). There can be good reasons to mix C and C++ code. Many developers write code in C, particularly in the realm of embedded microcontroller programming. Sometimes a convenient module exists in a C-language implementation and it may be desirable to use it in C++. Consider, for instance, a C language module that exercises a practical function. Let’s imagine that this C implementation is well-tested and stable. Instead of rewriting or porting an existing, stable and tested body of code from C to C++, it might be a wiser choice to simply use the C code unchanged in the C++ project.
Christopher Kormanyos

Chapter 18. Additional Reading

This chapter provides additional references covering background information on C, modern C++ programming, the C++ standard library and STL, C++ coding guidelines, software design, the embedded systems toolchain, and microcontroller software and hardware.
Christopher Kormanyos


Weitere Informationen

Premium Partner

BranchenIndex Online

Die B2B-Firmensuche für Industrie und Wirtschaft: Kostenfrei in Firmenprofilen nach Lieferanten, Herstellern, Dienstleistern und Händlern recherchieren.



Best Practices für die Mitarbeiter-Partizipation in der Produktentwicklung

Unternehmen haben das Innovationspotenzial der eigenen Mitarbeiter auch außerhalb der F&E-Abteilung erkannt. Viele Initiativen zur Partizipation scheitern in der Praxis jedoch häufig. Lesen Sie hier  - basierend auf einer qualitativ-explorativen Expertenstudie - mehr über die wesentlichen Problemfelder der mitarbeiterzentrierten Produktentwicklung und profitieren Sie von konkreten Handlungsempfehlungen aus der Praxis.
Jetzt gratis downloaden!