Comment

Comment by inamberclad

Probably one of the least painful digital buses.

If anyone is wondering how to access an I2C bus from a Linux computer, say, a raspberry pi:

int fd = open("/dev/i2c-1", O_RDWR);

ioctl(fd, I2C_SLAVE, [slave address here]);

Then you can read() and write() to the device with the kernel taking care of all the transmission details. Usually all that's exposed is a few bytes for the registers. To set a register, write two bytes: first the register address, and then the value. To read a register, write the register address and then read a byte. Most of the devices have linear address spaces, so reading out multiple registers is as simple as reading multiple bytes.

The i2c-tools package has some very handy CLI tools for exploring an I2C bus.

Electrically, the bus is an open-collector design on both ends, so devices can only pull the lines to low, and they release them to set them high. Don't forget pull-up resistors!

parent inamberclad 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton