Embedded Programming with Modern C++ Cookbook

Book description

Explore various constraints and challenges that embedded developers encounter in their daily tasks and learn how to build effective programs using the latest standards of C++

Key Features

  • Get hands-on experience in developing a sample application for an embedded Linux-based system
  • Explore advanced topics such as concurrency, real-time operating system (RTOS), and C++ utilities
  • Learn how to test and debug your embedded applications using logs and profiling tools

Book Description

Developing applications for embedded systems may seem like a daunting task as developers face challenges related to limited memory, high power consumption, and maintaining real-time responses. This book is a collection of practical examples to explain how to develop applications for embedded boards and overcome the challenges that you may encounter while developing.

The book will start with an introduction to embedded systems and how to set up the development environment. By teaching you to build your first embedded application, the book will help you progress from the basics to more complex concepts, such as debugging, logging, and profiling. Moving ahead, you will learn how to use specialized memory and custom allocators. From here, you will delve into recipes that will teach you how to work with the C++ memory model, atomic variables, and synchronization. The book will then take you through recipes on inter-process communication, data serialization, and timers. Finally, you will cover topics such as error handling and guidelines for real-time systems and safety-critical systems.

By the end of this book, you will have become proficient in building robust and secure embedded applications with C++.

What you will learn

  • Get to grips with the fundamentals of an embedded system
  • Understand how to optimize code for the targeted hardware platforms
  • Explore cross-compilation, build types, and remote debugging
  • Discover the importance of logging for debugging and root cause analysis of failures
  • Uncover concepts such as interrupt service routine, memory model, and ring buffer
  • Recognize the need for custom memory management in embedded systems
  • Delve into static code analyzers and tools to improve code quality

Who this book is for

This book is for developers, electronic hardware professionals, and software and system-on-chip engineers who want to build effective embedded programs in C++. Familiarity with the C++ programming language is expected, but no previous knowledge of embedded systems is required.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Embedded Programming with Modern C++ Cookbook
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    5. Get in touch
      1. Reviews
  7. Fundamentals of Embedded Systems
    1. Exploring embedded systems
      1. How are they different from desktop or web applications?
      2. Types of embedded systems
      3. Microcontrollers
      4. System on Chip
      5. Application-specific integrated circuits
      6. Field programmable gate arrays
    2. Working with limited resources
    3. Looking at performance implications
    4. Working with different architectures
      1. Endianness
      2. Alignment
      3. Fixed-width integer types
    5. Working with hardware errors
      1. Early versions of hardware
      2. Hardware is unreliable
      3. The influence of environmental conditions
    6. Using C++ for embedded development
      1. You don't pay for what you don't use
      2. Object-oriented programming to time the code complexity
      3. Resource acquisition is initialization
      4. Exceptions
      5. The powerful standard library
      6. Threads and a memory model as part of the language specification
    7. Deploying software remotely
    8. Running software remotely
    9. Logging and diagnostics
    10. Summary
  8. Setting Up the Environment
    1. Setting up the build system in a Docker container
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Working with emulators
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Cross-compilation
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Connecting to the embedded system
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Debugging embedded applications
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    6. Using gdbserver for remote debugging
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    7. Using CMake as a build system
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
  9. Working with Different Architectures
    1. Exploring fixed-width integer types
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Working with the size_t type
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Detecting the endianness of the platform
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Converting the endianness
      1. How to do it...
      2. How it works...
    5. Working with data alignment
      1. How to do it...
      2. How it works...
      3. There's more... 
    6. Working with packed structures
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Aligning data with cache lines
      1. How to do it...
      2. How it works...
      3. There's more...
  10. Handling Interrupts
    1. Data polling
    2. Interrupt service routines
    3. General considerations for ISRs
    4. 8051 microcontroller interrupts
    5. Implementing an interrupt service routine
      1. How to do it...
      2. How it works...
    6. Generating a 5 kHz square signal using 8-bit auto-reload mode
      1. How to do it...
      2. How it works...
    7. Using Timer 1 as an event counter to count a 1 Hz pulse
      1. How to do it...
      2. How it works...
      3. There's more...
    8. Receiving and transmitting data serially
      1. How to do it...
      2. How it works...
      3. There's more...
  11. Debugging, Logging, and Profiling
    1. Technical requirements
    2. Running your applications in the GDB
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Working with breakpoints
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Working with core dumps
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Using gdbserver for debugging
      1. Getting ready...
      2. How to do it...
      3. How it works...
    6. Adding debug logging
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Working with debug and release builds
      1. How to do it...
      2. How it works...
      3. There's more...
  12. Memory Management
    1. Using dynamic memory allocation
      1. How to do it...
      2. How it works...
    2. Exploring object pools
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Using ring buffers
      1. How to do it...
      2. How it works...
    4. Using shared memory
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Using specialized memory
      1. How to do it...
      2. How it works...
      3. There's more...
  13. Multithreading and Synchronization
    1. Exploring thread support in C++
      1. How to do it...
      2. How it works...
    2. Exploring data synchronization
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Using condition variables
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Using atomic variables
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Using the C++ memory model
      1. How to do it...
      2. How it works...
      3. There's more...
    6. Exploring lock-free synchronization
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Using atomic variables in shared memory
      1. How to do it...
      2. How it works...
    8. Exploring async functions and futures
      1. How to do it...
      2. How it works...
      3. There's more...
  14. Communication and Serialization
    1. Using inter-process communication in applications
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Exploring the mechanisms of inter-process communication
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    3. Learning about message queue and publisher-subscriber models
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Using C++ lambdas for callbacks
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Exploring data serialization
      1. How to do it...
      2. How it works...
      3. There's more...
    6. Using the FlatBuffers library
      1. How to do it...
      2. How it works...
      3. There's more...
  15. Peripherals
    1. Controlling devices connected via GPIO
      1. How to do it...
      2. How it works...
    2. Exploring pulse-width modulation
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Using ioctl to access a real-time clock in Linux
      1. How to do it...
      2. How it works...
      3. There's more
    4. Using libgpiod to control GPIO pins
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Controlling I2C peripheral devices
      1. How to do it...
      2. How it works...
      3. There's more...
  16. Reducing Power Consumption
    1. Technical requirements
    2. Exploring power-saving modes in Linux
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Waking up using RTC
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Controlling the autosuspend of USB devices
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Configuring CPU frequency
      1. How to do it...
      2. How it works...
      3. There's more...
    6. Using events for waiting
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Profiling power consumption with PowerTOP
      1. How to do it...
      2. How it works...
      3. There's more...
  17. Time Points and Intervals
    1. Exploring the C++ Chrono library
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Measuring time intervals
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Working with delays
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Using the monotonic clock
      1. How to do it...
      2. How it works...
      3. There's more...
    5. Using POSIX timestamps
      1. How to do it...
      2. How it works...
      3. There's more...
  18. Error Handling and Fault Tolerance
    1. Working with error codes
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Using exceptions for error handling
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Using constant references when catching exceptions
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Tackling static objects
      1. How to do it...
      2. How it works...
    5. Working with watchdogs
      1. How to do it...
      2. How it works...
    6. Exploring heartbeats for highly available systems
      1. How to do it...
      2. How it works...
      3. There's more...
    7. Implementing software debouncing logic
      1. How to do it...
      2. How it works...
  19. Guidelines for Real-Time Systems
    1. Using real-time schedulers in Linux
      1. How to do it...
      2. How it works...
    2. Using statically allocated memory
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Avoiding exceptions for error handling
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Exploring real-time operating systems
      1. How to do it...
      2. How it works...
      3. There's more...
  20. Guidelines for Safety-Critical Systems
    1. Using the return values of all functions
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Using static code analyzers
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Using preconditions and postconditions
      1. How to do it...
      2. How it works...
      3. There's more...
    4. Exploring the formal validation of code correctness
      1. How to do it...
      2. How it works...
      3. There's more...
  21. Microcontroller Programming
    1. Setting up the development environment
      1. How to do it...
      2. How it works...
      3. There's more...
    2. Compiling and uploading a program
      1. How to do it...
      2. How it works...
      3. There's more...
    3. Debugging microcontroller code
      1. How to do it...
      2. How it works...
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Embedded Programming with Modern C++ Cookbook
  • Author(s): Igor Viarheichyk
  • Release date: April 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838821043