Homepage Wiki Forum Buy

I2C

From GNUBLIN

severity (level) requirement Gnublin family
Gnublin logo advanced.png Kernel compile, Toolchain Alle


Contents

I2C Interface

I2C [1] is a classic microcontroller bus. With two wires it possible to address up to 1136 members. Gnublin offers a I2C interface for controlling external blocks.

Gnublin board spi1.jpg

Gnublin board spi new.png

Assigment:

  • 9 - I2C_SDA1
  • 10 - I2C_SCL1

Beginners

Controlling example with i2c-tools

The I2C-Tool are helpfull if you want to send data on the bus by your own. You can send and recive data and listen for the members addresses.

Send

i2cset <I2CINTERFACE> <SLAVEADDRESSE> <REGISTERADDRESS> <DATA>
  • I2INTERFACE //defines the used dev file (e. g. i2c-1 = 1)
  • SLAVEADDRESS //Slave-Address
  • REGISTERADDRESS //comontly a address in the slaves addressspace
  • DATA //Data which are send to the slave

Example:

root@gnublin-debian:~# i2cset 1 0x20 0x07 0x00
root@gnublin-debian:~# i2cset -y 1 0x20 0x03 0x00

Recive

i2cget <I2CINTERFACE> <SLAVEADDRESS> <REGISTERADDRESS> <MODE>
  • I2INTERFACE //defines the used dev file (e. g. i2c-1 = 1)
  • SLAVEADDRESS //Slave-Address
  • REGISTERADDRESS //comontly a address in the slaves addressspace
  • MODE //Defines how to reade the data, Byte b or Word w

Exaple:

root@gnublin:~# i2cget 1 0x49 0x00 b
root@gnublin:~# i2cget 1 0x49 0x00 w

Get the members Address

In the following example the LM75 temperature sensor was connected to the bus.

root@gnublin:~# i2cdetect 1 WARNING! This program can confuse your I2C bus, cause data loss and worse! I will probe file /dev/i2c-1. I will probe address range 0x03-0x77. Continue? [Y/n] y x 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- 49 4a 4b 4c -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 6e -- 70: -- -- -- -- -- -- -- --

How you can see there are 5 detected devices on the bus: 0x49 - 0x4c and 0x6e. The Address 0x6e ist the own Adress of th I"C-Controller. The slave function is not supported by the driver.

Note: The kernel sends timeoutmessages by older I"C-driver versions on the default output device (On GNUBLIN this is the terminal). These messages can be ignored.

For advanced

As advanced user it is time to write our own little programm in C to access the I2C interface.

Controlling example in C

You can find the following example in the directiory /root/examples/misc/pca9555/i2c-test. It's already pre-compiled and can just be executed.

#include <stdio.h> #include <fcntl.h> #include <linux/i2c.h> #include <linux/i2c-dev.h> #define ADDR 0x20 //<----Adresse des Slaves int main (int argc, char **argv) { int fd; char filename[32]; char buffer[128]; int n, err; if (argc == 0) { printf("usage: %s <device>\n", argv[0]); exit(1); } sprintf(filename, argv[1]); printf("device = %s\n", filename); int slave_address = ADDR; if ((fd = open(filename, O_RDWR)) < 0) { printf("i2c open error"); return -1; } printf("i2c device = %d\n", fd); //<----Vorbereiten des I2C Slaves if (ioctl(fd, I2C_SLAVE, slave_address) < 0) { printf("ioctl I2C_SLAVE error"); return -1; } /* slave address is not in buffer */ buffer[0] = 0x06; /* command byte: write config regs */ buffer[1] = 0x00; /* port0 all outputs */ err = write(fd, buffer, 2); //<---- Weitergabe der zu sendenden Daten an den Controller Treiber if (err != 2) { printf("write: error %d\n", err); return -1; } /* slave address is not in buffer */ buffer[0] = 0x07; /* command byte: write config regs */ buffer[1] = 0x00; /* port1 all outputs */ err = write(fd, buffer, 2); //<---- Weitergabe der zu sendenden Daten an den Controller Treiber if (err != 2) { printf("write: error %d\n", err); return -1; } n = 0; while (1) { buffer[0] = 0x03; /* command byte: write output regs */ buffer[1] = 0x01; /* port1 data */ //<---- Weitergabe der zu sendenden Daten an den Controller Treiber if (write(fd, buffer, 2) != 2) { printf("write error 0"); return -1; } usleep(100000); buffer[0] = 0x03; /* command byte: write output regs */ buffer[1] = 0xff; /* port1 data */ //<---- Weitergabe der zu sendenden Daten an den Controller Treiber if (write(fd, buffer, 2) != 2) { printf("write error 1"); return -1; } printf("%d\n", n++); usleep(100000); } }

You can now comile the program:

gcc "c-filename" -o programname

And execute:

./programname

If u have connected the PCA9555 chip, you should see blinking a LED on any pin of the port 0 of the PCA9555. Or if u have connected a oszilloscop you should see following output:

Tek-i2c.jpg

At the top u see the clock signal SCL, the other is the data signal SDA. You can see the startbit, the adress 0x20, the acknowledgebit and the stopbit.

Attention: You have to be carefull with the busaddress of the pca9555. The slave address stands in the first byte but is only seven bits long. The first bit (LSB) is the R/W bit. If A0, A1 and A3 are zero the write address is 0x20.

In other languages