Table Of ContentHands-On Embedded
Programming with C++17
Create versatile and robust embedded solutions for MCUs
and RTOSes with modern C++
Maya Posch
BIRMINGHAM - MUMBAI
Hands-On Embedded Programming with
C++17
Copyright © 2019 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form
or by any means, without the prior written permission of the publisher, except in the case of brief quotations
embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented.
However, the information contained in this book is sold without warranty, either express or implied. Neither the
author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to
have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products
mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy
of this information.
Commissioning Editor: Aaron Lazar
Acquisition Editor: Chaitanya Nair
Content Development Editor: Rohit Kumar Singh
Technical Editor: Ketan Kamble
Copy Editor: Safis Editing
Project Coordinator: Vaidehi Sawant
Proofreader: Safis Editing
Indexer: Mariammal Chettiyar
Graphics: Alishon Mendonsa
Production Coordinator: Arvindkumar Gupta
First published: January 2019
Production reference: 1310119
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-930-0
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as
well as industry leading tools to help you plan your personal development and advance
your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and videos
from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Packt.com
Did you know that Packt offers eBook versions of every book published, with PDF and
ePub files available? You can upgrade to the eBook version at www.packt.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
customercare@packtpub.com for more details.
At www.packt.com, you can also read a collection of free technical articles, sign up for a
range of free newsletters, and receive exclusive discounts and offers on Packt books and
eBooks.
Contributors
About the author
Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering
the joys of programming early on, and later the joys of electronics, she has always
expressed a profound interest in technology, a passion that she gladly shares with others.
Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the
limits of what can be done with the minimum of code and hardware to accomplish
everything that is cool, new, and exciting.
She also enjoys FPGA development, AI, and robotics research, in addition to creative
writing, music, and drawing.
About the reviewers
Frans Faase studied computer science at the University of Twente and received a master's
degree in the field of compiler design and formal methods. He worked on some research
projects in academia, but mostly worked as a software engineer in industry. He has about
20 years of experience of working with C++, both professional as recreational. He has
contributed to several open source projects. Recently, he also gained some experience
developing software for micro-controllers, mostly using the Arduino environment.
Patrick Mintram is a software engineer who has made his way up from electronics
technician. He has made a career in the world of embedded systems, including developing
and testing in a safety-critical environment and line-level repair and maintenance. He is
married with two cats, Duke and Daisy, and spends his free time tinkering and running.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com
and apply today. We have worked with thousands of developers and tech professionals,
just like you, to help them share their insight with the global tech community. You can
make a general application, apply for a specific hot topic that we are recruiting an author
for, or submit your own idea.
Table of Contents
Preface 1
Section 1: The Fundamentals - Embedded
programming and the role of C++
Chapter 1: What Are Embedded Systems? 7
The many faces of embedded systems 7
Microcontrollers 9
TMS 1000 9
Intel MCS-48 11
Intel MCS-51 13
PIC 16
AVR 22
M68k and Z80-based 27
ARM Cortex-M 27
H8 (SuperH) 28
ESP8266/ESP32 28
Others 32
Challenges 32
System-on-Chip/Single Board Computer 33
Challenges 34
Summary 34
Chapter 2: C++ as an Embedded Language 35
C++ relative to C 35
C++ as an embedded language 38
C++ language features 40
Namespaces 41
Strongly typed 41
Type conversions 43
Classes 43
Inheritance 47
Virtual base classes 48
Function inlining 49
Runtime type information 49
Exception handling 50
Templates 50
The standard template library 51
Maintainability 51
Summary 51
Table of Contents
Chapter 3: Developing for Embedded Linux and Similar Systems 52
Embedded operating systems 52
Real-time OSes 55
Custom peripherals and drivers 56
Adding an RTC 57
Custom drivers 58
Resource limitations 60
Example – club room monitoring 60
Hardware 61
Relays 62
Debounce 64
Debounce HAT 68
Power 70
Implementation 70
Listener 74
Club 77
HTTP request handler 93
Status handler 94
Data handler 95
Service configuration 98
Permissions 99
Final results 99
Example – basic media player 101
Summary 103
Chapter 4: Resource-Restricted Embedded Systems 104
The big picture for small systems 104
Example – Machine controller for a laser cutter 108
Functional specification 109
The design requirements 110
Implementation-related choices 112
Embedded IDEs and frameworks 117
Programming MCUs 118
Memory programming and device debugging 119
Boot loader 122
Memory management 123
Stack and heap 124
Interrupts, ESP8266 IRAM_ATTR 126
Concurrency 128
AVR development with Nodate 130
Enter Nodate 131
Example – CMOS IC Tester 132
Usage 137
ESP8266 development with Sming 142
ARM MCU development 142
[ ii ]
Table of Contents
RTOS usage 143
Summary 144
Chapter 5: Example - Soil Humidity Monitor with Wi-Fi 145
Keeping plants happy 145
Our solution 148
The hardware 148
The firmware 153
Setting up Sming 153
Plant module code 155
Makefile-user.mk 155
Main 156
OtaCore 157
BaseModule 177
PlantModule 181
Index.html 187
Compiling and flashing 187
First-time configuration 188
Using the system 189
Taking it further 190
Complications 190
Summary 191
Section 2: Testing, Monitoring
Chapter 6: Testing OS-Based Applications 193
Avoiding real hardware 193
Cross-compiling for SBCs 195
Integration test for club status service 196
Mock versus hardware 205
Testing with Valgrind 208
Multi-target build system 210
Remote testing on real hardware 219
Summary 221
Chapter 7: Testing Resource-Restricted Platforms 222
Reducing wear 222
Planning out a design 223
Platform-independent build systems 224
Using cross-compilers 225
Local and on-chip debugging 225
Example – ESP8266 integration test 226
The server 227
Makefile 252
The node 253
Makefile 281
[ iii ]
Table of Contents
Building the project 285
Summary 286
Chapter 8: Example - Linux-Based Infotainment System 287
One box that does everything 287
Hardware needed 288
Software requirements 289
Bluetooth audio sources and sinks 290
Online streaming 292
Voice-driven user interface 292
Usage scenarios 292
Source code 293
Building the project 303
Extending the system 303
Summary 304
Chapter 9: Example - Building Monitoring and Control 305
Plants, rooms, and beyond 305
Developmental history 306
Functional modules 308
Firmware source 308
Core 308
Modules 309
CO2 module 314
Jura 318
JuraTerm 322
Motion 325
PWM 327
I/O 332
Switch 341
Command and control server 344
Administration tool 357
Air-conditioning service 358
InfluxDB for recording sensor readings 361
Security aspects 364
Future developments 365
Summary 365
Section 3: Integration with other tools and
frameworks
Chapter 10: Developing Embedded Systems with Qt 367
The power of the right framework 367
Qt for command-line use 369
GUI-based Qt applications 370
[ iv ]