Embedded Programming with C++ Cookbook: Practical recipes to help you build robust and secure embedded applications using C++17 and C++20

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Build on your knowledge effectively, from building your first embedded application through to solving challenges in performing multithreading, memory management, and microcontrollers Key Features • Get hands-on experience in developing a sample application for an embedded Linux-based system • Explore advanced topics such as concurrency, RTOS, and C++ utilities • Learn how to test and debug your embedded applications by using logs and profiling tools Book Description Developing applications for embedded systems may seem like a daunting task as developers face challenges in terms of limited memory, power consumption, and maintaining real-time responses. This book is a collection of practical examples for understanding how to develop applications for embedded boards and solve 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 understand the use of 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 ll cover topics such as error handling, and guidelines for real-time systems and safety-critical systems. By the end of this book, you will be proficient in building robust and secure embedded applications with C++. What you will learn • Become well-versed with how to deploy software to an embedded system • Create specialized Linux distributives for embedded systems using the Yocto Project • Understand concepts such as interrupt vector array, Separating Device Driver and interrupt controller management • Discover the importance of logging for debugging and failure root cause analysis • Understand the need for custom memory allocators for better memory management • Use toolkit implementers for code optimization 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++language is expected but no previous knowledge of embedded systems is required.

Author(s): Igor Viarheichyk
Edition: 1
Publisher: Packt Publishing
Year: 2020

Language: English
Commentary: True PDF
Pages: 389
City: Birmingham, UK
Tags: C++; Multithreading; Cookbook; Docker; Serialization; Memory Management; Lambda Functions; Microcontrollers; Error Handling; Interrupts; I²C Bus; Embedded Systems; GPIO; Real-Time Systems; GDB

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