EBC Exercise 12 I2C

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.

xM
The DM3730 on the BeagleBoard-xM has four I²C controllers (Section 17 of the TRM). You can see which ones are configured at boot time by running the following on the Beagle:

beagle$ dmesg | grep i2c [   0.000000] Beagle cameraboard: registering i2c2 bus for lbcm3m1 [   8.946075] i2c_omap i2c_omap.1: bus 1 rev4.0 at 2600 kHz [   8.966064] i2c_omap i2c_omap.2: bus 2 rev4.0 at 400 kHz [   8.979858] i2c_omap i2c_omap.3: bus 3 rev4.0 at 100 kHz [  10.323608] input: twl4030_pwrbutton as /devices/platform/i2c_omap.1/i2c-1/1-0049/twl4030_pwrbutton/input/input1 [  10.334381] i2c /dev entries driver

Here we see three buses, each running at a different speed. Bus 2 is brought out to the Expansion Header. These signals are 1.8V and the TC74 runs on 2.7 to 5.5V. For now I'm going to use the BeagleBoard Trainer since it brings the voltages up to 3.3V.

I²C is a two-wire bus. The two wires are
 * 1) Serial Clock (SCLK on the data sheet, SCL on the Beagle), is an input to the TC74 and is used to clock data into and out of the TC74.
 * 2) Serial Data (SDA on both), is bidirection and carries the data to and from the TC74.

The only other two pins on the TC74 that you need to use are the Power Supply (Vdd) and Ground.

Wire up the TC74 to the Beagle by attaching the Vdd to the 3.3V pad on the Trainer, the GND to a GND on the Trainer and SDA to SDA and SCLK to SCL. You will also need to attach two pull-up resistors. Get two 4.7K&Omega; resistors. Attach one between SDA and Vdd. Attach the other between SCL and Vdd.

Your TC74 should be labeled with TC74a# where the # is a digit. This digit tells the address of the device. If you have another TC74 with a different address, you can wire it in parallel with the first. That is, attach SDA to SDA and SCL to SCL, etc. No need for additional pull up resistors.

bone
The DM3730 on the BeagleBoard-xM has four I²C controllers (Section 17 of the TRM). You can see which ones are configured at boot time by running the following on the Beagle:

beagle$ dmesg | grep i2c dmesg | grep i2c [   0.102508]  omap_i2c.1: alias fck already exists [   0.116668] omap_i2c omap_i2c.1: bus 1 rev2.4.0 at 100 kHz [   0.241516]  omap_i2c.3: alias fck already exists [   0.241790] omap_i2c omap_i2c.3: bus 3 rev2.4.0 at 100 kHz [   0.532775] i2c /dev entries driver

Here we see two buses, each running at a different speed. Bus 3 is brought out to the P9 Expansion Header. These signals are 3.3V and the TC74 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 (SCLK on the data sheet, SCL on the Beagle), is an input to the TC74 and is used to clock data into and out of the TC74.
 * 2) Serial Data (SDA on both), is bidirection and carries the data to and from the TC74.

The only other two pins on the TC74 that you need to use are the Power Supply (Vdd) and Ground.

Wire up the TC74 to the Beagle by attaching the Vdd to the 3.3V pad on the bone, the GND to a GND on the bone and SDA to SDA and SCLK to SCL. You will also need to attach two pull-up resistors. Get two 4.7K&Omega; resistors. Attach one between SDA and Vdd. Attach the other between SCL and Vdd.

Your TC74 should be labeled with TC74a# where the # is a digit. This digit tells the address of the device. If you have another TC74 with a different address, you can wire it in parallel with the first. 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:

beagle$ opkg update beagle$ opkg install i2c-tools beagle$ opkg install i2c-tools-dev

From the Shell
The Beagle brings out I²C bus 2 (bus 3 on the bone) to the Expansion Header. You can see what devices are on the bus by using the i2cdetect command. On your Beagle try:

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

What you see is a list of all the devices found on the bus. I've attached two TC74's, a TC74A0 and a TC74A2. Their address are  and   respectively. Converting to hex you get  and. You can see the two appear in the ic2detect.

Each TC74 has two registers.

Check the TC74 manual for detail on the configuration register. We're interested in the TEMP register. You can read it with:

beagle$ i2get -y 2 0x48 0 0x1b

The -y says don't ask me, just do it. 2 says use bus 2. 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 C
Another approach to using I²C on the Beagle is from a C program. You can open  and do   calls on it to read and write data.

Pull the exercises beagle$ cd exercises beagle$ git pull beagle$ cd i2c

Compile and run myi2cget.c.

beagle$ gcc myi2cget -o myi2cget beagle$ ./myi2cget Usage: ./myi2cget   beagle$ ./myi2cget 2 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.

The tools directory contains the original i2cget code. It came from here.

Assignment

 * 1) Look over myi2cget.
 * 2) Find the open which opens the device.
 * 3) Find the ioctl call that sets the address.  What other values can be used instead of  ?  Hint:  Look in the include files for the definition of I2C_SLAVE.
 * 4) Find the ioctl call reads the register.  Hint: There are a couple of wrappers hiding it. Find where i2c_smbus_read_byte_data is defined and then keep going until you find ioctl.  I2C_SLAVE is used by the previous ioctl to set the slave address.  What's used at the 2nd argument to ioctl to read a byte?
 * 5) Challenge 1:  Write a C program that will print the current temperature every time the USER button is pressed. Print the temp in F.
 * 6) Challenge 2:  Modify your program to update the temperature every second if the USER button isn't pressed.