Cover image for Making Embedded Systems

Book description

Making embedded systems takes a more disciplined approach to programming than you may be used to, because these systems don't tolerate inefficiency. This easy-to-read book helps you cultivate a host of good development practices, based on classic software design patterns as well as new patterns unique to embedded programming. You learn not only system architecture, but specific techniques for dealing with system constraints and manufacturing requirements. Ideal for intermediate and experienced programmers.

Table of Contents

  1. Making Embedded Systems
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. Preface
    1. About This Book
      1. About the Author
      2. Acknowledgments
    2. Organization of This Book
    3. Conventions Used in This Book
      1. Typography
      2. Terminology
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
  4. 1. Introduction
    1. Compilers, Languages, and Object-Oriented Programming
    2. Embedded System Development
      1. Debugging
      2. More Challenges
      3. Principles to Confront Those Challenges
    3. Further Reading
  5. 2. Creating a System Architecture
    1. Creating System Diagrams
      1. The Block Diagram
      2. Hierarchy of Control
      3. Layered View
    2. From Diagram to Architecture
      1. Encapsulate Modules
      2. Delegation of Tasks
      3. Driver Interface: Open, Close, Read, Write, IOCTL
      4. Adapter Pattern
      5. Getting Started with Other Interfaces
      6. Example: A Logging Interface
        1. Typical calls needed for logging
        2. State of logging
        3. Pattern: Singleton
        4. Sharing private globals
    3. A Sandbox to Play In
    4. Further Reading
  6. 3. Getting Your Hands on the Hardware
    1. Hardware/Software Integration
      1. Ideal Project Flow
      2. Board Bring-Up
    2. Reading a Datasheet
      1. Datasheet Sections You Need When Things Go Wrong
      2. Important Text for Software Developers
      3. Evaluating Components Using the Datasheet
    3. Your Processor Is a Language
    4. Reading a Schematic
    5. Having a Debugging Toolbox (and a Fire Extinguisher)
      1. Keep Your Board Safe
      2. Toolbox
      3. Digital Multimeter
      4. Oscilloscopes and Logic Analyzers
        1. Setting up a scope
    6. Testing the Hardware (and Software)
      1. Building Tests
      2. Flash Test Example
        1. Test 1: Read existing data
        2. Test 2: Byte access
        3. Test 3: Block access
        4. Test wrap-up
      3. Command and Response
        1. Creating a command
        2. Invoking a command
      4. Command Pattern
    7. Dealing with Errors
      1. Consistent Methodology
      2. Error-Handling Library
      3. Debugging Timing Errors
    8. Further Reading
  7. 4. Outputs, Inputs, and Timers
    1. Toggling an Output
      1. Starting with Registers
      2. Set the Pin to Be an Output
      3. Turn On the LED
      4. Blinking the LED
      5. Troubleshooting
    2. Separating the Hardware from the Action
      1. Board-Specific Header File
      2. I/O-Handling Code
      3. Main Loop
      4. Facade Pattern
    3. The Input in I/O
      1. A Simple Interface to a Button
    4. Momentary Button Press
      1. Interrupt on a Button Press
      2. Configuring the Interrupt
      3. Debouncing Switches
    5. Runtime Uncertainty
      1. Dependency Injection
    6. Using a Timer
      1. Timer Pieces
      2. Doing the Math
      3. A Long Wait Between Timer Ticks
      4. Using the Timer
    7. Using Pulse-Width Modulation
    8. Shipping the Product
    9. Further Reading
  8. 5. 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. Interrupts
      1. An IRQ Happens
        1. Multiple sources for one interrupt
        2. Interrupt priority
        3. Nested interrupts
        4. Nonmaskable interrupts
      2. Save the Context
        1. Calculating system latency
        2. Reentrant functions
      3. Get the ISR from the Vector Table
        1. Initializing the vector table
        2. Looking up the ISR
      4. Calling the ISR
        1. Disabling interrupts
        2. Critical sections
      5. Restore the Context
      6. When to Use Interrupts
    4. How Not to Use Interrupts
      1. Polling
      2. System Tick
      3. Time-Based Events
      4. A Very Small Scheduler
    5. Watchdog
    6. Further Reading
  9. 6. Communicating with Peripherals
    1. The Wide Reach of Peripherals
      1. External Memory
      2. Buttons and Key Matrices
      3. Sensors
        1. Analog sensors
        2. Digital sensors
      4. Actuators
        1. Motors
        2. Position encoding
        3. PID control
      5. Displays
        1. Segmented displays
        2. Pixel displays
    2. So Many Ways of Communicating
      1. Serial
        1. RS-232 and TTL
        2. SPI
        3. I2C
        4. 1-wire
        5. USB
        6. Miscellaneous other protocols
      2. Parallel
      3. Ethernet and WiFi
    3. Putting Peripherals and Communication Together
      1. Data Handling
        1. Circular buffers
        2. Hardware FIFOs
        3. Direct memory access
        4. Comparison of buffering schemes
      2. Adding Robustness to the Communication
        1. Version everything and then checksum it
        2. Authentication and encryption
      3. Changing Data
      4. Changing Algorithms
    4. Further Reading
  10. 7. Updating Code
    1. Onboard Bootloader
    2. Build Your Own Updater
      1. Modifying the Resident Updater
    3. Brick Loader
      1. Copy Loader to RAM
      2. Run the Loader
      3. Copy New Code to Scratch
      4. Dangerous Time: Erase and Program
      5. Reset to New Code
    4. Security
    5. Linker Scripts
    6. Summary
  11. 8. Doing More with Less
    1. Code Space
      1. Reading a Map File (Part 1)
      2. Process of Elimination
      3. Libraries
      4. Functions and Macros
      5. Constants and Strings
    2. RAM
      1. Remove malloc
      2. Reading a Map File (Part 2)
      3. Registers and Local Variables
        1. Function parameters
        2. Minimize scope
        3. Look at the assembly
      4. Function Chains
      5. Pros and Cons of Globals
      6. Memory Overlays
    3. Speed
      1. Profiling
        1. I/O lines and an O-scope
        2. Timer profiler 1 (function timer)
        3. Timer profiler 2 (response timer)
        4. Sampling profiler
      2. Optimizing
        1. Memory timing
        2. Variable size
        3. One last look at function chains
        4. Consider the instructions
        5. Reduce math in loops
        6. Loop unrolling
        7. Lookup tables
        8. Coding in assembly language
    4. Summary
    5. Further Reading
  12. 9. Math
    1. Identifying Fast and Slow Operations
      1. Taking an Average
    2. Use 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
        1. Implicit input
        2. Linear interpolation
        3. Explicit input in the table
    4. Fake Floating-Point Numbers
      1. Rational Numbers
      2. Precision
      3. Addition (and Subtraction)
      4. Multiplication (and Division)
      5. Determining the Error
    5. Further Reading
  13. 10. Reducing Power Consumption
    1. Understanding 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. Slowing 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
  14. Index
  15. About the Author
  16. Colophon
  17. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  18. Copyright