Skip to the text
Modern C++ for Embedded Systems Sandwizz edition · Review draft 1.0
Sandwizz Academy
Microaware ApS · Sandwizz

Modern C++ for Embedded Systems

Building class-based hardware abstraction layers on the Sandwizz Breadboard

Edition
Sandwizz edition
Version
Review draft 1.0: all 21 chapters, 7 appendices and the Lab Companion
Date
November 2026

Review draft: the whole book is written. Measurements marked as coming from the digital twin will be repeated on the board.

The Sandwizz Dashboard with the schematic, the breadboard and the code editor

Contents

  1. Preface
  2. Part I · Foundations
  3. 1The Foundation of Hardware-Constrained Systems
    1. 1.1The comfortable illusion
    2. 1.2The Pentagon of Constraints
    3. 1.3Power, polling and interrupts
    4. 1.4The silicon spectrum
    5. 1.5Anatomy of a microcontroller
    6. 1.6Choosing a language
    7. 1.7The C++ we use
    8. 1.8The hardware abstraction layer
    9. 1.9The Sandwizz ecosystem
    10. 1.10The component kit
    11. 1.11Installing the dashboard
    12. 1.12Connecting the board
    13. 1.13Lab 1.1: light without code
  4. 2From Code to Silicon
    1. 2.1Ruining the magic
    2. 2.2Host and target
    3. 2.3Decoding arm-none-eabi-gcc
    4. 2.4The ABI: a contract written in registers
    5. 2.5Stage one: the compiler sees one file at a time
    6. 2.6Stage two: the linker and the memory map
    7. 2.7Sections: where everything lives
    8. 2.8The illusion of main
    9. 2.9Falling off the end of main
    10. 2.10Getting the program into the chip
    11. 2.11Sharing the processor with the board
    12. 2.12The Heisenbug and the price of printf
    13. 2.13Debugging without stopping the world
    14. 2.14The code editor
    15. 2.15Lab 2.1: Hello, bare metal
    16. 2.16Lab 2.2: Where does my data live?
    17. 2.17Lab 2.3: The price of printf
    18. 2.18Lab 2.4: Breakpoints while the board runs
  5. 3Forging the Hardware Abstraction Layer
    1. 3.1The keyword that saves your hardware
    2. 3.2Dead stores and vanishing loops
    3. 3.3The blind brain and the bus
    4. 3.4Memory-mapped I/O
    5. 3.5A register is a physical thing
    6. 3.6Bit masks
    7. 3.7Read-modify-write and the race
    8. 3.8Four ways to an atomic change
    9. 3.9Bit-banding
    10. 3.10What your program may touch
    11. 3.11From raw pointers to a struct overlay
    12. 3.12The DigitalOut class
    13. 3.13A row that is known at compile time
    14. 3.14Zero cost? Measure it
    15. 3.15Lab 3.1: The warning light
    16. 3.16Lab 3.2: The vanishing delay loop
    17. 3.17Lab 3.3: Atomic flags with bit-banding
  6. Part II · Inputs, Time and Events
  7. 4Digital Inputs and Contact Bounce
    1. 4.1From voltage to logic level
    2. 4.2An input must never float
    3. 4.3Contact bounce
    4. 4.4Debouncing in hardware
    5. 4.5Debouncing in software
    6. 4.6From levels to events
    7. 4.7A HAL for inputs
    8. 4.8Lab 4.1: What a button really does
    9. 4.9Lab 4.2: A hardware debouncer
    10. 4.10Lab 4.3: Two debouncers in software
  8. 5Interrupts and Concurrency
    1. 5.1Why interrupts
    2. 5.2Exceptions on the Cortex-M3
    3. 5.3Entering and leaving a handler
    4. 5.4The NVIC: enabling, pending and priorities
    5. 5.5Interrupts on the Sandwizz Breadboard
    6. 5.6Writing an interrupt handler in C++
    7. 5.7Shared data and races
    8. 5.8Critical sections
    9. 5.9Atomic operations
    10. 5.10A lock-free queue
    11. 5.11Lab 5.1: A timer interrupt
    12. 5.12Lab 5.2: A button with interrupts
    13. 5.13Lab 5.3: A race you can count
    14. 5.14Lab 5.4: A rotary encoder
  9. 6Time, Timers and Scheduling
    1. 6.1Where time comes from
    2. 6.2Three counters and their limits
    3. 6.3Surviving the wrap
    4. 6.4Time with types: std::chrono
    5. 6.5Waiting without blocking
    6. 6.6Many jobs, one processor: cooperative scheduling
    7. 6.7Timer interrupt or task?
    8. 6.8Hardware timers
    9. 6.9Lab 6.1: Drift
    10. 6.10Lab 6.2: A scheduler
    11. 6.11Lab 6.3: A counter in hardware
  10. Part III · Driving the World
  11. 7Pulse-Width Modulation, Light and Sound
    1. 7.1What PWM is
    2. 7.2Why the average is all that counts
    3. 7.3PWM in hardware
    4. 7.4PWM in software
    5. 7.5Brightness and the eye
    6. 7.6One interface, two kinds of polymorphism
    7. 7.7Sound
    8. 7.8PWM as a digital-to-analogue converter
    9. 7.9Lab 7.1: PWM in software
    10. 7.10Lab 7.2: Breathing, with and without gamma
    11. 7.11Lab 7.3: A melody
    12. 7.12Lab 7.4: PWM as an analogue output
  12. 8Motors and Motion
    1. 8.1Motors are not LEDs
    2. 8.2The transistor as a switch
    3. 8.3The inductive kick
    4. 8.4Speed and direction
    5. 8.5Servos
    6. 8.6Stepper motors
    7. 8.7Ramps
    8. 8.8Lab 8.1: A DC motor
    9. 8.9Lab 8.2: A servo
    10. 8.10Lab 8.3: A stepper motor with ramps
  13. Part IV · Sensing the World
  14. 9Analogue Signals, Filters and Fixed Point
    1. 9.1From a voltage to a number
    2. 9.2Sampling
    3. 9.3Quantisation and noise
    4. 9.4How an ADC works
    5. 9.5Voltages as a type
    6. 9.6Numbers without a floating-point unit
    7. 9.7Digital filters
    8. 9.8Calibration
    9. 9.9Sensors on the breadboard
    10. 9.10Lab 9.1: Noise
    11. 9.11Lab 9.2: Aliasing
    12. 9.12Lab 9.3: Filters
    13. 9.13Lab 9.4: A thermometer
  15. 10Memory without a Heap
    1. 10.1Storage duration: where and for how long
    2. 10.2The stack
    3. 10.3Why not the heap?
    4. 10.4Containers with a fixed capacity
    5. 10.5Pools
    6. 10.6Vocabulary types instead of pointers
    7. 10.7Layout and padding
    8. 10.8Lab 10.1: Where things live
    9. 10.9Lab 10.2: Fragmentation
    10. 10.10Lab 10.3: Vocabulary types
    11. 10.11Lab 10.4: How deep is the stack?
  16. Part V · Talking to Other Chips
  17. 11Serial Communication and the UART
    1. 11.1Serial and parallel, synchronous and asynchronous
    2. 11.2The UART frame
    3. 11.3Two clocks must agree
    4. 11.4Electrical levels
    5. 11.5A UART in hardware, and one in software
    6. 11.6From bytes to messages
    7. 11.7Lab 11.1: Sending
    8. 11.8Lab 11.2: Packets
    9. 11.9Lab 11.3: Receiving
  18. 12I²C, Buses and Device Drivers
    1. 12.1The bus
    2. 12.2A transaction
    3. 12.3Drivers as class templates
    4. 12.4The decorator: adding behaviour without changing anything
    5. 12.5Errors as values
    6. 12.6A display driver
    7. 12.7A sensor driver
    8. 12.8Lab 12.1: Who is on the bus?
    9. 12.9Lab 12.2: An OLED display
    10. 12.10Lab 12.3: A sensor driver
    11. 12.11Lab 12.4: A weather display
  19. 13SPI, Shift Registers and Multiplexing
    1. 13.1The bus
    2. 13.2Four modes
    3. 13.3An SPI master in software
    4. 13.4More outputs: the 74HC595
    5. 13.5Multiplexing
    6. 13.6UART, I²C or SPI?
    7. 13.7Lab 13.1: SPI in a loop
    8. 13.8Lab 13.2: Eight LEDs on three rows
    9. 13.9Lab 13.3: A four-digit display
  20. 14A Command Line for the Board
    1. 14.1Text in, without blocking
    2. 14.2Words without copies
    3. 14.3Numbers from text
    4. 14.4A table of commands
    5. 14.5Robust by construction
    6. 14.6Lab 14.1: Lines without blocking
    7. 14.7Lab 14.2: A command line
  21. Part VI · Building Systems
  22. 15Direct Memory Access and Double Buffering
    1. 15.1A second bus master
    2. 15.2What copying costs
    3. 15.3The board's DMA
    4. 15.4Double buffering
    5. 15.5Barriers: when another master reads
    6. 15.6Lab 15.1: What copying costs
    7. 15.7Lab 15.2: Double buffering
  23. 16Power, Energy and Sleep
    1. 16.1Power, energy and charge
    2. 16.2Where the current goes
    3. 16.3Sleep modes
    4. 16.4Event-driven design
    5. 16.5Measuring
    6. 16.6A budget the compiler checks
    7. 16.7Lab 16.1: Awake or asleep
    8. 16.8Lab 16.2: Measuring a current
    9. 16.9Lab 16.3: A budget in the compiler
  24. 17Reliability, Watchdogs and Faults
    1. 17.1How programs fail
    2. 17.2Every wait has a limit
    3. 17.3Watchdogs
    4. 17.4Remembering what happened
    5. 17.5Assertions
    6. 17.6Faults on the Cortex-M3
    7. 17.7Coding standards and tools
    8. 17.8Lab 17.1: An assertion that survives
    9. 17.9Lab 17.2: A supervised watchdog
    10. 17.10Lab 17.3: A fault
  25. 18State Machines
    1. 18.1Programs that remember
    2. 18.2UML state machines
    3. 18.3Four ways to write it
    4. 18.4Events, queues and run to completion
    5. 18.5Hierarchy: superstates and history
    6. 18.6Testing a machine
    7. 18.7Lab 18.1: One machine, four ways
    8. 18.8Lab 18.2: A pedestrian crossing
    9. 18.9Lab 18.3: An encoder menu
  26. 19Testing Embedded C++
    1. 19.1Why test, and where
    2. 19.2A test framework in modern C++
    3. 19.3Seams and dependency injection
    4. 19.4Test doubles
    5. 19.5Properties instead of examples
    6. 19.6A bug in Chapter 9
    7. 19.7Coverage and continuous testing
    8. 19.8Lab 19.1: Unit tests for the HAL
    9. 19.9Lab 19.2: Drivers against a fake
    10. 19.10Lab 19.3: Properties
  27. 20Architecture and a Complete Product
    1. 20.1What architecture is
    2. 20.2Layers and the dependency rule
    3. 20.3Sharing the processor
    4. 20.4Budgets
    5. 20.5Designing for failure
    6. 20.6Settings that survive
    7. 20.7Recording decisions
    8. 20.8Lab 20.1: A greenhouse monitor
    9. 20.9Lab 20.2: Budgets and tests
  28. Part VII · Beyond Software
  29. 21Hardware in Verilog
    1. 21.1From instructions to circuits
    2. 21.2Verilog in one section
    3. 21.3State machines in hardware
    4. 21.4Testbenches
    5. 21.5Hardware or software?
    6. 21.6The board's programmable logic
    7. 21.7Lab 21.1: A counter, and the rule for <=
    8. 21.8Lab 21.2: An SPI master in hardware
  30. Appendices
  31. AC++ for Embedded Developers
    1. A.1The build
    2. A.2Language features
    3. A.3The library
    4. A.4Memory order for std::atomic
    5. A.5Undefined behaviour to watch for
  32. BThe Sandwizz BSP and the Book's HAL
    1. B.1Rows
    2. B.2Voltages and time
    3. B.3I²C and the OLED display
    4. B.4Interrupts and timers
    5. B.5The Program tab, the watchdog and restarts
    6. B.6The memory a program has
    7. B.7The book's HAL
  33. CCortex-M3 Cheat Sheet
    1. C.1Registers
    2. C.2The calling convention (AAPCS)
    3. C.3Typical instruction timing
    4. C.4Exceptions
    5. C.5System addresses
    6. C.6Bit-banding
  34. DComponents and Extension Parts
    1. D.1Passive parts
    2. D.2Semiconductors
    3. D.3Sound
    4. D.4Motors
    5. D.5Chips and modules
    6. D.6Rows used in the labs
  35. EAnswers to Selected Exercises
    1. E.1Chapter 1
    2. E.2Chapter 2
    3. E.3Chapter 3
    4. E.4Chapter 4
    5. E.5Chapter 5
    6. E.6Chapter 6
    7. E.7Chapter 7
    8. E.8Chapter 8
    9. E.9Chapter 9
    10. E.10Chapter 10
    11. E.11Chapter 11
    12. E.12Chapter 12
    13. E.13Chapter 13
    14. E.14Chapter 14
    15. E.15Chapter 15
    16. E.16Chapter 16
    17. E.17Chapter 17
    18. E.18Chapter 18
    19. E.19Chapter 19
    20. E.20Chapter 20
    21. E.21Chapter 21
  36. FGlossary
  37. GFurther Reading
    1. G.1C++
    2. G.2Embedded systems
    3. G.3Electronics
    4. G.4Interfaces and parts
    5. G.5Reliability, testing and architecture
    6. G.6State machines
    7. G.7Digital design and Verilog
  38. Part VIII · Lab Companion
  39. L1Before You Start
    1. L1.1The board's rows
    2. L1.2Names instead of rows
    3. L1.3Roles
    4. L1.4The connector at the end
    5. L1.5Three rules that protect the board
    6. L1.6Twin or board
    7. L1.7From schematic to holes
    8. L1.8Several setups on one board
    9. L1.9How to read a setup page
  40. L2Lights, Buttons and Time
    1. L2.1Which setup for which lab
    2. L2.2Setup A: an LED, a resistor and three roles
    3. L2.3Setup B: the warning light
    4. L2.4Setup C: a button and an LED
    5. L2.5Setup C2: a hardware debouncer
    6. L2.6Setup D: a rotary encoder
    7. L2.7Setup E: a counter in hardware
    8. L2.8Setup F: two LEDs that breathe
    9. L2.9Setup G: a passive buzzer
    10. L2.10Setup H: a voltage from PWM
  41. L3Motors, Sensors and Text
    1. L3.1The 5 V supply for the motors
    2. L3.2Setup I: a DC motor
    3. L3.3Setup J: a servo
    4. L3.4Setup K: a stepper motor
    5. L3.5Setup L: a potentiometer and an LED
    6. L3.6Setup M: a frequency that is not there
    7. L3.7Setup N: a thermometer
    8. L3.8Setup O: text from outside
  42. L4Buses, Displays and a Product
    1. L4.1Setup P: two modules on one bus
    2. L4.2Setup Q: a shift register and eight LEDs
    3. L4.3Setup R: a multiplexed stopwatch
    4. L4.4Setup S: an LED's current through a shunt
    5. L4.5Setup T: a pedestrian crossing
    6. L4.6Setup U: a menu with a knob
    7. L4.7Setup V: the greenhouse monitor
  43. L5Parts
    1. L5.1The Sandwizz component kit
    2. L5.2Extension parts
    3. L5.3Setup by setup
  44. L6When It Does Not Work
    1. L6.1Five questions, in order
    2. L6.2The instruments as test equipment
    3. L6.3Mistakes that look alike
    4. L6.4When the board itself seems wrong
  45. L7For Teachers and Study Groups
    1. L7.1Twin first, board second
    2. L7.2Organising the parts
    3. L7.3Handing in work
    4. L7.4Accessibility
    5. L7.5About the Sandwizz Breadboard

All figures and listings · use the search field at the top to look up a term, a class or a function.

Copyright and licences

Copyright © 2026 Microaware ApS. Sandwizz® is a trademark of Microaware ApS. All rights not granted below are reserved.

The book. The text, figures and pictures are licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International licence (CC BY-NC-ND 4.0, creativecommons.org/licenses/by-nc-nd/4.0). You may copy and share it free of charge and unchanged, with credit to Microaware ApS.

The book's code. The programs in the book and the HAL you build in it (the projects and HAL headers that come with the book, and the listings printed from them) are licensed under the MIT licence. You may use, change and share them in your own projects, also commercially, as long as the copyright notice and the licence text go with them. The licence text is in the LICENSE file next to the code.

The Sandwizz BSP and the firmware. The Sandwizz BSP (sandwizz.h and the runtime that programs link against) and the firmware on the board are proprietary: © Microaware ApS, all rights reserved, in source and in binary form. They are not covered by the licences above. A program from the book links against the BSP: the MIT licence covers the program's own code, not the BSP.