Skip to content
Libre Computer
Platform

Week 7 ยท Lecture outline

I2C, SPI and the logic analyser

Module 10: I/O. Lab: Lab 7, I2C and SPI. Due: Lab 6 and the project proposal. The practical midterm is this week.

Learning goals

By the end of the week a student can:

  • Describe an I2C transaction: addressing, open-drain signalling and a register read.
  • Describe SPI: clock, two data lines, chip select, the four modes, full duplex.
  • Read a part's register map from its datasheet and use it by hand.
  • Capture and decode a transaction on a logic analyser.

Lecture 1, block 1: I2C

  • Two shared wires, addressed devices, open-drain signalling.
  • From Linux: i2c-tools, the i2c-dev interface, or a kernel driver.
  • i2cdetect finds the devices on a bus.

Lecture 1, block 2: the register map is the contract

  • A datasheet's register map says what each address holds.
  • Lab 7 reads the DS3231 clock's time registers by hand before using any library.
  • Then the SSD1306 OLED display, on the same bus, shows the time.

Lecture 1, block 3: the final project

  • A small system using at least two of the course's buses, started on boot as a service, surviving a power pull, with a README another student could rebuild it from.
  • Two buses means two of I2C, SPI and UART; GPIO and PWM do not count toward them. Examples: a data logger that reads the SPI ADC, timestamps each reading from the I2C clock and shows its status on the I2C OLED; a sensor node that reads the SPI ADC and sends its readings over a UART link to the bench PC; or a fan or pump controller that reads a temperature through the SPI ADC, shows it on the I2C OLED and switches a small motor through a MOSFET.
  • Proposals are due this week; the labs page states the requirements to students.

Lecture 2, block 1: SPI

  • Clock, two data lines and a chip select; four modes; full duplex.
  • From Linux: spidev, or a kernel driver.
  • The MCP3008 8-channel ADC: the datasheet gives the mode and the bytes of a conversion.

Lecture 2, block 2: the logic analyser

  • It shows what actually happened on the wires, which is often not what the code meant.
  • Clip to the bus, capture, and let the analyser's decoder turn edges into bytes.
  • Compare the decoded bytes with what the program meant to send.

Lecture 2, block 3: from counts to a unit

  • An ADC returns counts, not a temperature or a light level.
  • Calibrate: readings at known conditions turn counts into the sensor's unit.
  • Power parts from 3.3 V so their signals stay at the header's level.

Board demo

  1. Enable an I2C bus with the wiring tool's overlay, and run i2cdetect with the DS3231 wired.
  2. Read its time registers by hand and decode one on the projector, with the logic analyser's decode beside it.
  3. Read the potentiometer through the MCP3008 over spidev, turning it while the values change.

Check questions

How does an I2C controller reach one of several parts on the same two wires?

Each part has an address; the controller sends it at the start of a transaction and only that part answers. The wires are shared and open-drain.

Your SPI program reads the wrong values from an ADC whose wiring is correct. What do you check first, and with what?

The SPI mode and the bytes the program sends, against the datasheet, on the logic analyser: it shows what actually went over the wires.

The practical midterm

Individually, at a station, after lab 6: students boot a board, enable a header bus with an overlay and drive a GPIO circuit from the pinout, with no notes beyond the datasheets, man pages and --help output, and the printed 40-pin header map. The assessment table has its weight.