Making Embedded Systems, 2nd Edition

Book description

Interested in developing embedded systems? Since they don't tolerate inefficiency, these systems require a disciplined approach to programming. This easy-to-read guide helps you cultivate good development practices based on classic software design patterns and new patterns unique to embedded programming. You'll learn how to build system architecture for processors, not for operating systems, and you'll discover techniques for dealing with hardware difficulties, changing designs, and manufacturing requirements.

Written by an expert who has created systems ranging from DNA scanners to children's toys, this book is ideal for intermediate and experienced programmers, no matter what platform you use. This expanded second edition includes new chapters on IoT and networked sensors, motors and movement, debugging, data handling strategies, and more.

  • Optimize your system to reduce cost and increase performance
  • Develop an architecture that makes your software robust in resource-constrained environments
  • Explore sensors, displays, motors, and other I/O devices
  • Reduce RAM and power consumption, code space, and processor cycles
  • Learn how to interpret schematics, datasheets, and power requirements
  • Discover how to implement complex mathematics and machine learning on small processors
  • Design effective embedded systems for IoT and networked sensors

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. About This Book
    2. Who This Book Is For
    3. About the Author
    4. Organization of This Book
    5. Terminology
    6. Conventions Used in This Book
    7. Using Code Examples
    8. O’Reilly Online Learning
    9. How to Contact Us
    10. Acknowledgments
  2. 1. Introduction
    1. Embedded Systems Development
      1. Compilers and Languages
      2. Debugging
      3. Resource Constraints
      4. Principles to Confront Those Challenges
    2. Prototypes and Maker Boards
    3. Further Reading
  3. 2. Creating a System Architecture
    1. Getting Started
    2. Creating System Diagrams
      1. The Context Diagram
      2. The Block Diagram
      3. Organigram
      4. Layering Diagram
    3. Designing for Change
      1. Encapsulate Modules
      2. Delegation of Tasks
      3. Driver Interface: Open, Close, Read, Write, IOCTL
      4. Adapter Pattern
    4. Creating Interfaces
      1. Example: A Logging Interface
    5. A Sandbox to Play In
    6. Back to the Drawing Board
    7. Further Reading
  4. 3. Getting Your Hands on the Hardware
    1. Hardware/Software Integration
      1. Ideal Project Flow
      2. Hardware Design
      3. Board Bring-Up
    2. Reading a Datasheet
      1. Datasheet Sections You Need When Things Go Wrong
      2. Datasheet Sections for Software Developers
      3. Evaluating Components Using the Datasheet
    3. Your Processor Is a Language
    4. Reading a Schematic
    5. Practice Reading a Schematic: Arduino!
    6. Keep Your Board Safe
    7. Creating Your Own Debugging Toolbox
      1. Digital Multimeter
      2. Oscilloscopes and Logic Analyzers
      3. Setting Up a Scope
    8. Testing the Hardware (and Software)
      1. Building Tests
      2. Flash Test Example
      3. Command and Response
      4. Command Pattern
    9. Dealing with Errors
      1. Consistent Methodology
      2. Error Checking Flow
      3. Error-Handling Library
      4. Debugging Timing Errors
    10. Further Reading
  5. 4. Inputs, Outputs, and Timers
    1. Handling Registers
      1. Binary and Hexadecimal Math
      2. Bitwise Operations
      3. Test, Set, Clear, and Toggle
    2. Toggling an Output
      1. Setting the Pin to Be an Output
      2. Turning On the LED
      3. Blinking the LED
      4. Troubleshooting
    3. Separating the Hardware from the Action
      1. Board-Specific Header File
      2. I/O-Handling Code
      3. Main Loop
      4. Facade Pattern
    4. The Input in I/O
    5. Momentary Button Press
      1. Interrupt on a Button Press
      2. Configuring the Interrupt
      3. Debouncing Switches
    6. Runtime Uncertainty
      1. Increasing Code Flexibility
      2. Dependency Injection
    7. Using a Timer
      1. Timer Pieces
      2. Doing the Math
      3. More Math: Difficult Goal Frequency
      4. A Long Wait Between Timer Ticks
      5. Using a Timer
    8. Using Pulse-Width Modulation
    9. Shipping the Product
    10. Further Reading
  6. 5. Interrupts
    1. A Chicken Presses a Button
    2. An IRQ Happens
      1. Nonmaskable Interrupts
      2. Interrupt Priority
      3. Nested Interrupts
    3. Save the Context
    4. Retrieve the ISR from the Vector Table
      1. Initializing the Vector Table
      2. Looking Up the ISR
    5. Call the ISR
      1. Multiple Sources for One Interrupt
      2. Disabling Interrupts
      3. Critical Sections
    6. Restore the Context
    7. Configuring Interrupts
    8. When and When Not to Use Interrupts
      1. How to Avoid Using Interrupts
      2. Polling
      3. System Tick
      4. Time-Based Events
      5. A Very Small Scheduler
    9. Further Reading
  7. 6. Managing the Flow of Activity
    1. Scheduling and Operating System Basics
      1. Tasks
      2. Communication Between Tasks
      3. Avoiding Race Conditions
      4. Priority Inversion
    2. State Machines
      1. State Machine Example: Stoplight Controller
      2. State-Centric State Machine
      3. State-Centric State Machine with Hidden Transitions
      4. Event-Centric State Machine
      5. State Pattern
      6. Table-Driven State Machine
      7. Choosing a State Machine Implementation
    3. Watchdog
    4. Main Loops
      1. Polling and Waiting
      2. Timer Interrupt
      3. Interrupts Do Everything
      4. Interrupts Cause Events
      5. Very Small Scheduler
      6. Active Objects
    5. Further Reading
  8. 7. Communicating with Peripherals
    1. Serial Communication
      1. TTL Serial
      2. RS-232 Serial
      3. SPI
      4. I2C and TWI
      5. 1-Wire
      6. Parallel
      7. Dual and Quad SPI
      8. USB
      9. Considering Other Protocols
    2. Communications in Practice
      1. External ADC Example: Data Ready with SPI
      2. Use a FIFO If Available
      3. Direct Memory Access (DMA) Is Faster
      4. External ADC Example: SPI and DMA
    3. Circular Buffers
    4. Further Reading
  9. 8. Putting Together a System
    1. Key Matrices
    2. Segmented Displays
    3. Pixel Displays
      1. Display Assets
      2. Changeable Data? Flyweight and Factory Patterns
    4. External Flash Memory
      1. Display Assets
      2. Emulated EEPROMs and KV Stores
      3. Little File Systems
      4. Data Storage
    5. Analog Signals
    6. Digital Sensors
    7. Data Handling
      1. Changing Algorithms: Strategy
      2. Algorithm Stages: Pipelines and Filters
    8. Calculating Needs: Speeds and Feeds
      1. Data Bandwidth
      2. Memory Throughput and Buffering
    9. Further Reading
  10. 9. Getting into Trouble
    1. Fighting with the Compiler Optimizations
    2. Impossible Bugs
      1. Reproduce the Bug
      2. Explain the Bug
    3. Creating Chaos and Hard Faults
      1. Dividing by Zero
      2. Talking to Things That Aren’t There
      3. Running Undefined Instructions
      4. Incorrect Memory Access (Unaligned Access)
      5. Returning a Pointer to Stack Memory
      6. Stack Overflows and Buffer Overflows
    4. Debugging Hard Faults
      1. Processor Registers: What Went Wrong?
      2. Creating a Core Dump
      3. Using the Core Dump
    5. Merely Very Difficult Bugs
    6. Consequences of Being Clever
    7. Further Reading
  11. 10. Building Connected Devices
    1. Connecting Remotely
      1. Directly: Ethernet and WiFi
      2. Through a Gateway
      3. Via a Mesh
    2. Robust Communication
      1. Version!
      2. Checksums, CRCs, Hashes
      3. Encryption and Authentication
      4. Risk Analysis
    3. Updating Code
      1. Firmware Update Security
      2. Multiple Pieces of Code
      3. Fallback Lifeboat
      4. Staged Rollout
    4. Managing Large Systems
    5. Manufacturing
    6. Further Reading
  12. 11. Doing More with Less
    1. Need More Code Space
      1. Reading a Map File (Part 1)
      2. Process of Elimination
      3. Libraries
      4. Functions Versus Macros: Which Are Smaller?
      5. Constants and Strings
    2. Need More RAM
      1. Remove malloc
      2. Reading a Map File (Part 2)
      3. Registers and Local Variables
      4. Function Chains
      5. Pros and Cons of Globals: RAM Versus Stack
      6. Clever Memory Overlays
    3. Need More Speed
      1. Profiling
      2. Optimizing for Processor Cycles
    4. Summary
    5. Further Reading
  13. 12. Math
    1. Identifying Fast and Slow Operations
      1. Taking an Average
      2. Different Averages: Cumulative and Median
    2. Using an Existing Algorithm
    3. Designing and Modifying Algorithms
      1. Factor Polynomials
      2. Taylor Series
      3. Dividing by a Constant
      4. Scaling the Input
      5. Lookup Tables
    4. Fake Floating-Point Numbers
      1. Rational Numbers
      2. Precision
      3. Addition (and Subtraction)
      4. Multiplication (and Division)
    5. Machine Learning
    6. Look Up the Answer!
    7. Further Reading
  14. 13. Reducing Power Consumption
    1. Understanding Power Consumption
      1. Measuring Power Consumption
      2. Designing for Lower Power Consumption
    2. Turn Off the Light When You Leave the Room
      1. Turn Off Peripherals
      2. Turn Off Unused I/O Devices
      3. Turn Off Processor Subsystems
      4. Slow Down to Conserve Energy
    3. Putting the Processor to Sleep
      1. Interrupt-Based Code Flow Model
      2. A Closer Look at the Main Loop
      3. Processor Watchdog
      4. Avoid Frequent Wake-Ups
      5. Chained Processors
    4. Further Reading
  15. 14. Motors and Movement
    1. Creating Movement
    2. Position Encoding
    3. Driving a Simple DC Motor with PWM
    4. Motor Control
      1. PID Control
      2. Motion Profiles
    5. Ten Things I Hate About Motors
    6. Further Reading
  16. Index
  17. About the Author

Product information

  • Title: Making Embedded Systems, 2nd Edition
  • Author(s): Elecia White
  • Release date: March 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098151546