EBC I2C

From eLinux.org
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder


I²C is a "two-wire interface" standard that is used to attach low-speed peripherals to an embedded system. In this exercise we will wire up a couple of I²C temperature sensors (TC74) and learn how to read their values.

The Hardware

The AM3359 on BeagleBone Black has three I²C controllers (Section 21 of the TRM). You can see which ones are configured at boot time by running the following on the Beagle:

bone$ dmesg | grep i2c
[    0.131250] omap_i2c 44e0b000.i2c: bus 0 rev0.11 at 400 kHz
[    0.132146] input: tps65217_pwr_but as /devices/ocp.3/44e0b000.i2c/i2c-0/0-0024/input/input0
[    0.140590] omap_i2c 44e0b000.i2c: unable to select pin group
[    0.141057] omap_i2c 4819c000.i2c: bus 1 rev0.11 at 100 kHz
[    0.142404] omap_i2c 4819c000.i2c: unable to select pin group
[    0.748189] i2c /dev entries driver

Here we see two buses, one running at 400 kHz and the other at 100 kHz. Table 13 from the Rev C1 SRM shows buses 1 and 2 are brought out to the P9 Expansion Header, however what the table starts numbering with 1 and the software starts with 0, so these are really buses 0 and 1. We'll use 1 (called 2 in the table).

You can see what's on the i2c buses with

bone$ i2cdetect -y -r 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- UU -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- UU -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: UU -- -- -- -- -- -- -- 
bone$ i2cdetect -y -r 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- 4f 
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: 70 -- -- -- -- -- -- -- 

I have something at addresses 0x48, 0x4f and 0x70 on bus 1.

.jpg

These signals are 3.3V and the TMP101 runs on 2.7 to 5.5V so we are in luck.

I²C is a two-wire bus. The two wires are

  1. Serial Clock (SCL), is an input to the TMP101 and is used to clock data into and out of the TMP101.
  2. Serial Data (SDA), is bidirectional and carries the data to and from the TMP101.

The only other two pins on the TMP101 that you need to use are the Power Supply (Vdd) and Ground, unless you want to use OS/ALERT, in which case you need to add a pull up resistor to Vdd, and then run another wire to a GPIO in order to properly trigger an interrupt.

Bone gpio.JPG BoneGPIO.png

Wire up the TMP101 to the Beagle by attaching the Vdd to the 3.3V + bus, the GND to the - bus and SDA to SDA (pin 20) and SCL to SCL (pin 19). You will also need to attach two pull-up resistors. Get two 4.7KΩ resistors. Attach one between SDA and Vdd. Attach the other between SCL and Vdd.

Your TMP101 should be labeled with T101. If you have another TMP101 you can wire it in parallel with the first be sure to wire the ADD0 pin differently. That is, attach SDA to SDA and SCL to SCL, etc. No need for additional pull up resistors.

The Software

Do this, to be sure you have all you needed installed:

bone$ apt-get update
bone$ apt-get install i2c-tools

From the Shell

The Beagle brings out I²C bus 1 to the Expansion Header. You can see what devices are on the bus by using the i2cdetect command. On your Beagle try:

bone$ i2cdetect -y -r 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- 48 48 4a -- -- -- -- -- 
50: -- -- -- -- UU UU UU UU -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --  

What you see is a list of all the devices found on the bus. I've attached three TMP101's, and wired their ADD0 line differently. Their address are 1001 000, 1001 001 and 1001 010 respectively. Converting to hex you get 0x48, 0x49 and 0x4a. You can see the three appear in the ic2detect.

Each TMP101 has four registers. Check the TMP101 manual for details. We're interested in the TEMP register. You can read it with:

bone$ i2cget -y 1 0x48 0
0x1b

The -y says don't ask me, just do it. 1 says use bus 1. 0x48 is the device address and 0 is the register number. The value returned is the temperature in degrees C.

  1. Convert the hex temperature to decimal. Is the value reasonable?
  2. Write a script to run ic2get in a loop and watch the temperature. Hold the device between your fingers. Does the temp go up?

From JavaScript (skip)

There are packages that make using i2c from JavaScript easy.

bone$ npm install -g i2c
bone$ cd exercises/i2c
bone$ ./testi2c.js
err: null
res: [255,136]
-120

Here's the code

var i2c = require('i2c');
var address = 0x40;

// point to your i2c address, debug provides REPL interface
var wire = new i2c(address, {device: '/dev/i2c-1', debug: false});
var command = 0;
var length = 2;
wire.readBytes(command, length, function(err, res) {
  // result contains a buffer of bytes
  console.log("err: " + err);
  console.log("res: " + JSON.stringify(res));
  var hi = res.readInt8(0);
  var lo = res.readUInt8(1);
  console.log((hi << 8) | lo);
});

The device I'm using returns a 16-bit value so you have to put the upper an lower byte together yourself.

From C (skip)

Another approach to using I²C on the Beagle is from a C program. You can open /dev/i2c-1 and do ioctl calls on it to read and write data.

Pull the exercises

bone$ cd exercises
bone$ git pull
bone$ cd i2c

Compile and run myi2cget.c.

bone$ gcc myi2cget.c -o myi2cget
bone$ ./myi2cget
Usage:  ./myi2cget <i2c-bus> <i2c-address> <register>
bone$ ./myi2cget 1 72 0
0x1b (27)

It takes many of the arguments as i2cget, but none of the flags. It's very stripped down version of i2cget. Note that 72 is the decimal representation of 0x48. myi2cget requires the use of decimal numbers, while the "traiditional" i2cget can use either decimal or hex representations.

The tools directory under /exercises/i2c/ic2-tools-3.1.0 contains the original i2cget code. It came from here.

References

  1. Introduction to i2c and SPI
  2. TMP101 I2C Temperature Sensor information.
  3. I got a lot information from Interfacing with I2C Devices.
  4. This appears to have some nice I2C information for the Gumstix. It should also work for the Beagle.
  5. i2c via Python




thumb‎ Embedded Linux Class by Mark A. Yoder