Skip to main content

Subsection 12.1.1 The Bus You Already Have

The I2C bus we built in Chapter 10 has two wires: SDA (serial data) and SCL (serial clock), both held HIGH by pull-up resistors unless a device is pulling one LOW. One device is in charge of the bus (the controller, which for us is the STM32C031C6), and every other device on the bus is a target. Each target has its own 7-bit address, and every transaction begins with the controller sending the address of the device it wants to talk to. All the targets hear that address; the one that recognizes it as its own acknowledges and takes part in the rest of the transaction, and the other devices on the bus don’t respond. If you’d like to review more than this paragraph covers, go to Section 10.4, which has the protocol in full: START and STOP, the ACK handover, and the byte-by-byte framing.
The STM32C031C6’s I2C peripheral produces the bit-level traffic in hardware, and the I2C library we handed you is what initializes the hardware and interfaces with it. It is the same library the seven-segment display driver you wrote is built on, and it is all we need for the accelerometer too. If you’d like a refresher see Subsection 10.4.9. We’ll build a similar device driver for the accelerometer.