The Book of I2C

Book description

If you work with embedded systems, youâ??re bound to encounter the ubiquitous Inter-Integrated Circuit bus (IIC, I2C, or I²C)â??a serial protocol for connecting integrated circuits in a computer system. In The Book of I²C, the first comprehensive guide to this bus, bestselling author Randall Hyde draws on 40 years of industry experience to get you started designing and programming I²C systems.

For hardware hackers, electronics hobbyists, and software engineers of every skill level, the extensive coverage in this book will make it a go-to referenceâ??whether you need an overview of I²C signal protocols, an introduction to variants of the I²C, like the SMBus and VESA E-DCC, or programming examples for common I²C peripheral ICs. Youâ??ll also learn the I²C implementations of the Arduino, Teensy, and Raspberry Pi systems among others, as well as how to:

�Analyze and debug I²C bus transactions

�Program devices on the I²C

�Implement an I²C with bare-metal programming

�Work with I²C bus expanders

The Book of I²C includes over 100 figures and many annotated source code listings, along with online chapters that will be updated to include new controllers and peripherals.

Table of contents

  1. Title Page
  2. Copyright
  3. Dedication
  4. About the Author
  5. Acknowledgments
  6. Introduction
    1. Expectations and Prerequisites
    2. Source Code in This Book
    3. Typography and Pedantry
    4. A Note About Terminology
    5. Organization
  7. Part I: Low-Level Protocols and Hardware
    1. Chapter 1: I2C Low-Level Hardware
      1. 1.1 I2C Overview
      2. 1.2 Open-Drain (Open-Collector) Logic and Four-Wire Mode
      3. 1.3 I2C Signal Levels
        1. 1.3.1 Level Shifting
      4. 1.4 Choosing Pullup Resistor Sizes
      5. 1.5 Bus Capacitance and Pullup Resistor Values
        1. 1.5.1 What If the Bus Capacitance Is Too High?
      6. 1.6 I2C Bus Speeds
      7. 1.7 Multicontroller I2C Bus Clock Synchronization
      8. 1.8 Multicontroller I2C Bus Arbitration
      9. 1.9 Clock Stretching
      10. 1.10 Cross Talk
      11. 1.11 Chapter Summary
    2. Chapter 2: I2C Protocol
      1. 2.1 Data on the I2C Bus
      2. 2.2 I2C Addresses and Read/Write Control
      3. 2.3 Repeated Start Conditions
      4. 2.4 Clock Stretching
      5. 2.5 Special Addresses
        1. 2.5.1 The General Call Address
        2. 2.5.2 Hardware General Calls
        3. 2.5.3 Start Byte
        4. 2.5.4 CBUS and Reserved Addresses
        5. 2.5.5 High-Speed Mode Controller Code
        6. 2.5.6 10-Bit Peripheral Addressing
        7. 2.5.7 Device ID
      6. 2.6 Resetting the I2C Bus
      7. 2.7 Detecting I2C Peripherals on the Bus
      8. 2.8 Creating Custom Devices
      9. 2.9 Chapter Summary
    3. Chapter 3: A Software Implementation of the I2C Bus
      1. 3.1 A Software I2C Implementation on the Teensy 3.2
        1. 3.1.1 A Software-Based I2C Controller for the Teensy 3.2
        2. 3.1.2 A Software-Based I2C Peripheral for the Teensy 3.2
        3. 3.1.3 Some Final Comments on the Teensy 3.2 Software I2C Code
      2. 3.2 Basic ATtiny84 and ATtiny85 Hardware
        1. 3.2.1 Atto84 Software-Based I2C Peripheral
      3. 3.3 Chapter Summary
    4. Chapter 4: Tools for Analyzing and Debugging I2C Transmissions
      1. 4.1 Generic Hardware Testing and Debugging Tools
      2. 4.2 Logic Analyzers
      3. 4.3 The I2C Driver
      4. 4.4 The Bus Pirate
      5. 4.5 The Saleae Logic Analyzers
      6. 4.6 A Final Comment on I2C Monitors and Logic Analyzers
      7. 4.7 Chapter Summary
    5. Chapter 5: I2C Variants
      1. 5.1 SMBus
        1. 5.1.1 Differences Between SMBus and Standard I2C
        2. 5.1.2 SMBus Electrical Specifications
        3. 5.1.3 SMBus Reserved Addresses
        4. 5.1.4 SMBus Protocol Commands
      2. 5.2 VESA DDC and E-DDC
      3. 5.3 ACCESS.bus
      4. 5.4 Two-Wire Interface and Two-Wire Serial Interface
      5. 5.5 Chapter Summary
  8. Part II: Hardware Implementations
    1. Chapter 6: I2C On Common Single-Board Computers
      1. 6.1 The Arduino Family
        1. 6.1.1 The Arduino Uno Rev3 and Leonardo
        2. 6.1.2 The Arduino Nano
        3. 6.1.3 The Arduino Micro
        4. 6.1.4 The Arduino Nano Every
        5. 6.1.5 The Arduino Mega 2560 Rev3
        6. 6.1.6 The Arduino Zero
        7. 6.1.7 The Arduino Due
        8. 6.1.8 Other Arduino-Brand Single-Board Computers
      2. 6.2 Adafruit Single-Board Computers
      3. 6.3 SparkFun Single-Board Computers
      4. 6.4 The Teensy Family
      5. 6.5 Other Arduino-Compatible Single-Board Computers
      6. 6.6 The Raspberry Pi
      7. 6.7 The Raspberry Pi Pico
      8. 6.8 The BeagleBone Black
      9. 6.9 The PINE A64 and ROCKPro64
      10. 6.10 The Onion Omega
      11. 6.11 The STM32 Single-Board Computer Family
        1. 6.11.1 STM32F767/Nucleo-144
        2. 6.11.2 STM32F746G-Disco
        3. 6.11.3 STM32 Boards Galore
      12. 6.12 The NetBurner MOD54415
      13. 6.13 I2C on the Personal Computer
      14. 6.14 Chapter Summary
    2. Chapter 7: I2C On Vendor Buses
      1. 7.1 The Adafruit Feather Bus
        1. 7.1.1 Feather Bus Pinouts
        2. 7.1.2 I2C on the Feather Bus
        3. 7.1.3 Multicontroller Operation
        4. 7.1.4 Feathers and FeatherWings
      2. 7.2 I2C on the SparkFun Qwiic Bus
      3. 7.3 Qwiic Bus Peripherals
      4. 7.4 I2C on the Seeed Studio Grove Bus
      5. 7.5 Chapter Summary
  9. Part III: Programming the I2c Bus
    1. Chapter 8: Arduino I2C Programming
      1. 8.1 Basic I2C Programming
      2. 8.2 Basic Wire Programming
        1. 8.2.1 Wire Utility Functions
        2. 8.2.2 Wire Read Operations
        3. 8.2.3 Wire Write Operations
        4. 8.2.4 Wire Peripheral Functions
      3. 8.3 Arduino I2C Write Example
      4. 8.4 Arduino I2C Read Example
        1. 8.5 Arduino I2C Peripheral Example
      5. 8.6 Multiple I2C Port Programming
      6. 8.7 Chapter Summary
    2. Chapter 9: Raspberry Pi (and Linux) I2C Programming
      1. 9.1 The I2C Bus Pins on the Pi General-Purpose Input/Output Header
      2. 9.2 Manually Activating the I2C Buses
      3. 9.3 Changing the I2C Clock Frequency
      4. 9.4 I2C Clock Stretching Issues and Solutions
      5. 9.5 Raspberry Pi OS (Linux) I2C Utilities
      6. 9.6 Reading and Writing I2C Data
      7. 9.7 Advanced I2C Kernel Calls
        1. 9.7.1 The i2c-dev Functions
        2. 9.7.2 The i2c_smbus_write_quick Function
        3. 9.7.3 The i2c_smbus_read_byte Function
        4. 9.7.4 The i2c_smbus_write_byte() Function
        5. 9.7.5 The i2c_smbus_read_byte_data() Function
        6. 9.7.6 The i2c_smbus_write_byte_data() Function
        7. 9.7.7 The i2c_smbus_read_word_data() Function
        8. 9.7.8 The i2c_smbus_write_word_data() Function
        9. 9.7.9 The i2c_smbus_read_block_data() Function
        10. 9.7.10 The i2c_smbus_write_block_data() Function
        11. 9.7.11 Miscellaneous Functions
      8. 9.8 Reentrancy Issues with I2C Operations
      9. 9.9 Multicontroller Operation Under Linux
      10. 9.10 Other Linux Systems
        1. 9.10.1 PINE A64 and ROCKPro64
        2. 9.10.2 BeagleBone Black
        3. 9.10.3 Onion Omega2+
      11. 9.11 Using the Raspberry Pi as an I2C Peripheral Device
      12. 9.12 Chapter Summary
    3. Chapter 10: I2C Programming in Real-Time Operating Systems
      1. 10.1 Real-Time Operating System Basics
        1. 10.1.1 Processes and Threads
        2. 10.1.2 Multithreading and Multitasking
        3. 10.1.3 Reentrancy
        4. 10.1.4 Synchronization
        5. 10.1.5 Safety Critical Systems
      2. 10.2 Real-Time Operating System I2C Programming
        1. 10.2.1 µC/OS
        2. 10.2.2 FreeRTOS I2C Programming
        3. 10.2.3 Teensy Threads I2C Programming
        4. 10.2.4 Mbed I2C Programming
      3. 10.3 Other Real-Time Operating System I2C Programming
      4. 10.4 Chapter Summary
    4. Chapter 11: Bare-Metal I2C Controller Programming
      1. 11.1 Teensy 4.x Controller Programming
        1. 11.1.1 i.MX RT1062 I2C Registers
        2. 11.1.2 Teensy 4.x Wire Code
      2. 11.2 ATtiny Controller Programming
        1. 11.2.1 The Atto84 Triangle Wave Demonstration Program
      3. 11.3 Chapter Summary
  10. Part IV: I2c Peripheral Programming Examples
    1. Chapter 12: The TCA9548A I2C Bus Expander
      1. 12.1 The TCA9548A I2C Multiplexer
        1. 12.1.1 Upstream and Downstream Devices
        2. 12.1.2 The TCA9548A Selection Register
        3. 12.1.3 TCA9548A Address and Reset Lines
        4. 12.1.4 The TCA9548A Power Supply, Pullups, and Level Shifting
        5. 12.1.5 Reducing Bus Loading and Bus Speed
        6. 12.1.6 Switching Between Buses
        7. 12.1.7 Cascading TCA9548A Multiplexers
      2. 12.2 The Adafruit TCA9548A I2C Expander
      3. 12.3 The SparkFun I2C Mux
      4. 12.4 Chapter Summary
    2. Chapter 13: The MCP23017 and MCP23008 GPIO Expanders
      1. 13.1 The MCP23017 and MCP23008 Pinouts
      2. 13.2 MCP230xx Registers
        1. 13.2.1 Accessing MCP230xx Registers
        2. 13.2.2 MCP230xx Initialization
        3. 13.2.3 Programming the Data Direction
        4. 13.2.4 Programming Input Pullup Resistors
        5. 13.2.5 Programming the Input Polarity
        6. 13.2.6 Sequential Register Operations
        7. 13.2.7 Slew Rate Control
        8. 13.2.8 Reading General-Purpose Input/Output Pins on the MCP230xx
      3. 13.3 Writing General-Purpose Input/Output Pins on the MCP230xx
      4. 13.4 Demonstrating Input/Output on an MCP23017
      5. 13.5 Interrupts on the MCP230xx
        1. 13.5.1 Interrupt Actions on the MCP230xx
        2. 13.5.2 Interrupt Service Routines
        3. 13.5.3 Mirroring INTx Pins (MCP23017 Only)
        4. 13.5.4 Open-Drain INTx Output
        5. 13.5.5 Enabling Interrupts on the MCP230xx
        6. 13.5.6 Testing and Clearing Interrupts
      6. 13.6 A Sample Interrupt-Driven MCP230xx
      7. 13.7 MCP230xx Library Code
      8. 13.8 I2C Performance
      9. 13.9 MCP23Sxx Parts
      10. 13.10 Chapter Summary
    3. Chapter 14: The ADS1015 and ADS1115 Analog-to-Digital Converters
      1. 14.1 Analog-to-Digital Converter Specifications
        1. 14.1.1 Analog-to-Digital Converter Resolution
        2. 14.1.2 Analog-to-Digital Converter Channel Count
        3. 14.1.3 Analog-to-Digital Converter Polarity
        4. 14.1.4 Analog-to-Digital Converter Range
        5. 14.1.5 Differential vs. Single-Ended Modes
        6. 14.1.6 Sample Frequency
        7. 14.1.7 Miscellaneous ADS1x15 Features
      2. 14.2 Analog Conditioning
      3. 14.3 ADS1x15 Analog-to-Digital Converter Registers
        1. 14.3.1 The Conversion Register
        2. 14.3.2 The Configuration Register
        3. 14.3.3 The Low and High Threshold Registers
      4. 14.4 The Adafruit ADS1x15 Breakout Boards
      5. 14.5 An ADS1x15 Programming Example
      6. 14.6 Improving Polling Performance
      7. 14.7 Improving Performance Using Continuous Scanning
      8. 14.8 Interrupts and the ADS1x15
      9. 14.9 Filtering Noise
        1. 14.9.1 Computing Means and Medians
      10. 14.10 Chapter Summary
    4. Chapter 15: The MCP4725 Digital-to-Analog Converter
      1. 15.1 MCP4275 Overview
      2. 15.2 The Fast Write Command
      3. 15.3 The Write Command
      4. 15.4 Power-Down Modes
      5. 15.5 The Read Command
      6. 15.6 Chapter Summary
    5. Chapter 16: Bare-Metal Peripheral Programming
      1. 16.1 The ATtiny as an I2C Peripheral
      2. 16.2 Introducing the Memory Peripheral
      3. 16.3 The Memory Peripheral Software Architecture
        1. 16.3.1 The Main File
        2. 16.3.2 The Interrupt Service Routine Library
        3. 16.3.3 A Sample Controller Application
      4. 16.4 Chapter Summary
  11. Epilogue
  12. Appendix A: The Adafruit I2C Address Compilation
  13. Appendix B: Online Chapters
  14. Glossary
  15. Index

Product information

  • Title: The Book of I2C
  • Author(s): Randall Hyde
  • Release date: October 2022
  • Publisher(s): No Starch Press
  • ISBN: 9781718502468