I2C programming


26 posts   Page 1 of 2   1, 2
by radu » Tue Nov 22, 2011 6:52 am
One of the first things I will do when I get my board is to install a barometric sensor (BMP085).
I did that with a PIC and the programming is straight forward, because there is no OS, so no need to worry about drivers and stuff.
So I was wondering how would we be able to access the I2C port? Is there a driver for it? Will there be a library for it? Or do we have to access it via MMIO?
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by psycho_moggie » Tue Nov 22, 2011 10:30 am
Hopefully it will be like an ordinary boring pc. They make extensive use of the i2c bus for temperature measurement and other things. In Ubuntu and, I assume Debian, the package lm-sensors gives access to the internal bus and the i2c interfaces on the vga and dvi connectors. I wired a DS1621 to the vga connector of my Asus 1001HA, and had a response back just using shell commands in no time at all. I haven't tried it, but have a look at http://www.paintyourdragon.com/?p=43 for a programming guide.
Posts: 18
Joined: Tue Nov 22, 2011 9:40 am
by radu » Tue Nov 22, 2011 10:42 am
Thanks for the info. Sounds really, really simple, the code would look much cleaner than my PIC implementation :) Only downside is that root access is required, but for my needs this should not be a problem. I was thinking to make a web interface for my data, so could either build my own minimalistic web server, or use Apache with the I2C code in crontab or something.
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by chris evans » Tue Nov 22, 2011 12:49 pm
AIUI Some I2C devices use 1-6V signalling others only 5V, do we know yet which will be used?
Also is I2C on the GPIO connector or the other set of pins shown on the final layout?
http://www.cjemicros.co.uk/rpi/
Posts: 4
Joined: Mon Nov 07, 2011 9:00 pm
by TonyD » Tue Nov 22, 2011 1:47 pm
From what I've read else where, I2C on R-Pi is 3.3V
Tony
User avatar
Posts: 343
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
by slurp » Tue Nov 22, 2011 1:58 pm
Quote from TonyD on November 22, 2011, 13:47
From what I've read else where, I2C on R-Pi is 3.3V


It does not need to be a problem, this is where the voltage / level translators can be used.

There's a typical solution here:-

http://www.maxim-ic.com/app-no.....vp/id/1159

Best regards,
Colin
Posts: 16
Joined: Sun Oct 16, 2011 8:40 pm
by radu » Sun Nov 27, 2011 9:51 pm
So, can we get an official answer on this? How will we be able to access I2C devices?
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by liz » Sun Nov 27, 2011 10:05 pm
There's no I2C driver at the moment. We'll provide documentation, but one of you is going to have to knuckle down and write a driver.
--
Head of Comms, Raspberry Pi Foundation
User avatar
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 4115
Joined: Thu Jul 28, 2011 7:22 pm
by tntexplosivesltd » Sun Nov 27, 2011 10:10 pm
I guess I could give one a go when I get my Rasp Pi. I am definitely wanting to use I2C

I2C is complicated though. This should be fun
Posts: 72
Joined: Fri Sep 30, 2011 8:08 am
by radu » Sun Nov 27, 2011 10:17 pm
Quote from tntexplosivesltd on November 27, 2011, 22:10
I guess I could give one a go when I get my Rasp Pi. I am definitely wanting to use I2C

I2C is complicated though. This should be fun


Did you ever write a Linux driver? I would be interested to help, I have a lot of C experience, but not that much Linux specific experience.
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by tntexplosivesltd » Sun Nov 27, 2011 10:30 pm
I have had a small amount of experience, but I might not be the best candidate. I tent to start things and not finish them, usually due to other work getting the way (uni and such). I'll give it a go though
Posts: 72
Joined: Fri Sep 30, 2011 8:08 am
by joep » Tue Nov 29, 2011 3:20 pm
I've done a fair amount linux/unix/minix driver work, and I have some I2C experience, so I'd be willing to look at it as well, but it depends on how accessible the hardware details are going to be.
Posts: 17
Joined: Sat Nov 26, 2011 4:27 pm
by psycho_moggie » Thu Dec 01, 2011 9:39 pm
It's pretty simple to give yourself access to i2c devices with a udev rule, see
https://xgoat.com/wp/2008/01/29/i2c-device-udev-rule/
I used the tutorial on the same site to knock out a simple c program, gst-launch to capture a frame from a webcam in a bash script, and cron to upload that and the temperature to a web site. I have a friend who is rather twitchy about his central heating going off while he's away in the winter.
Posts: 18
Joined: Tue Nov 22, 2011 9:40 am
by radu » Fri Dec 02, 2011 5:10 am
Can you post some samples of your C code that talk to I2C devices?
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by joep » Fri Dec 02, 2011 3:04 pm
Does anyone know if the I2C on this processor supports high speed mode?
Posts: 17
Joined: Sat Nov 26, 2011 4:27 pm
by psycho_moggie » Sat Dec 03, 2011 11:39 am
I've only written a single program to access a DS1621, I'll post that with a bit of an explanation and links to relevant sources. I think I've got an i2c adc chip somewhere, if I can find it I'll have a go at some code, because that would be really useful with the Raspi.
Edit: I've found a PCF8591 i2c adc and dac chip, and an MCP4822 spi dac, watch this space.
Posts: 18
Joined: Tue Nov 22, 2011 9:40 am
by Gert van Loo » Sat Dec 03, 2011 3:36 pm
Does anyone know if the I2C on this processor supports high speed mode?

It does 400KHz but not beyond. I2C is inherently not suited for high speed. Engineers have tried to push it past the 400KHz but that requires some nasty I/O pads. That is why, on my I/O expansion board, I chose to go for SPI . There where a lot more I2C I/O expansion devices but I ignored them and went straight for SPI. With a 20MHz SPI your latency is much lower then a 400KHz I2C. That makes it easier to do fast I/O.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2081
Joined: Tue Aug 02, 2011 7:27 am
by psycho_moggie » Sat Dec 03, 2011 3:54 pm
This was all done on an Asus 1001HA running Kubuntu 10.04. The
executable runs on a 701 as well.

The chip datasheet is here:
http://pdfserv.maxim-ic.com/en.....DS1621.pdf

Install build-essential, i2c-tools and libi2c-dev

The DS1621 is wired to a VGA connector as follows
vga > ds1621
5 > 4, 5, 6 & 7
9 > 8
12 > 1
15 > 2
I wired a 100nF capacitor between 4 & 8.

Just to try things out

sudo modprobe i2c-dev

sudo i2cdetect -l
Mine returns:
i2c-0 i2c intel drm CRTDDC_A I2C adapter
i2c-1 i2c intel drm LVDSDDC_C I2C adapter

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

set up continuous measurement
sudo i2cset -y 0 0x48 0xac 0x00 b

start measurements
sudo i2cset -y 0 0x48 0xee

read the temperature
sudo i2cget -y 0 0x48 0xaa
0x14
It return a hexadecimal value which is a twos-complement temperature,
20 degrees, I reckon.

Ok, that's enough for one posting, next, a program.
Posts: 18
Joined: Tue Nov 22, 2011 9:40 am
by psycho_moggie » Sat Dec 03, 2011 4:34 pm
To allow yourself access to the i2c hardware

Create a file /etc/udev/rules.d/30-i2c.rules, containing the line:
KERNEL=="i2c-[0-9]*", GROUP="i2c"

Make yourself a member of the group i2c:
sudo adduser jonathan i2c

Add the line "modprobe i2c-dev" to /etc/init.d/rc.local to load the
module on boot.

It's easiest to reboot for the above to take effect.

On to the code:

Here is "temperature.c". Note that although the chip stores the
temperature as two bytes, you only have to read one byte to get a
signed one byte value.

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>

int main(void)
{
int file;
char filename[20];
int addr = 0x48; /* The I2C address */
int32_t temperature;

// Open a file giving access to i2c interface 0

sprintf(filename,"/dev/i2c-0");
if ((file = open(filename,O_RDWR)) < 0) {
printf("No temperature measurement\n");
exit(1);
}

// Check that the chip is there

if (ioctl(file,I2C_SLAVE,addr) < 0) {
printf("No temperature measurement\n");
exit(1);
}

// Set up continuous measurement

if (i2c_smbus_write_byte_data(file, 0xac, 0x00) < 0) {
printf("No temperature measurement\n");
exit(1);
}

// Start measurements

if (i2c_smbus_write_byte(file, 0xee) < 0) {
printf("No temperature measurement\n");
exit(1);
}

// read the temperature

if ((temperature = i2c_smbus_read_byte_data(file, 0xaa)) < 0) {
printf("No temperature measurement\n");
exit(1);
}

printf("Temperature %d degrees Celsius\n", (int8_t)temperature);
close(file);
exit(0);
}

Compile it:
gcc temperature.c -o temperature

Run it:
./temperature
Temperature 21 degrees Celsius
Posts: 18
Joined: Tue Nov 22, 2011 9:40 am
by radu » Sun Dec 04, 2011 1:01 am
Thanks, will keep this code in mind for when I start my project!
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by bsimpson888 » Wed Dec 21, 2011 1:26 pm
Hello together,
I'am looking forward to get my hands on the first device to start porting my libraries I already developed.
I got 15 years of experience with I2C and Linux, and I developed C and Python libraries for serval I2C devices. I also created kernel modules for different I2C adapters in the past. The libraries cover serval I2C sensor devices like DS1621, I/O expanders , RTC's , Memories, ADC'c and I2C Multiplexer. I also developed a library for 1Wire devices like DS1820 which I will port as soon I get the hardware.
I will port them to ARM-Fedora 15 because I developed my stuff for Fedora and I got already RPMs for the version 15 prepared. Most of them compile already on the arm emulator in qemu, so just the hardware related stuff needs some changes.

Greets
Marco
Posts: 8
Joined: Wed Dec 21, 2011 12:50 pm
by radu » Sat Dec 24, 2011 10:23 am
Great, thanks :)
Posts: 110
Joined: Mon Nov 21, 2011 8:19 pm
by David Calvo » Wed Dec 28, 2011 6:12 am
Very good news. I'm excited. I worried about this issue
Posts: 14
Joined: Sat Aug 20, 2011 12:40 am
by azarashi55 » Wed Mar 07, 2012 2:44 am
Very good news. We are developping Global Versatile Controller(GVC) using Linux and the Arduino with I2C bus connection. We are very happy if they could be aggregated into a single master Rasp Pi.
About GVC, look at our web site http://www.gvc-on.net
User avatar
Posts: 9
Joined: Wed Mar 07, 2012 2:05 am
Location: Tokyo
by techpaul » Sat Jul 14, 2012 7:19 pm
Gert van Loo wrote:Does anyone know if the I2C on this processor supports high speed mode?
It does 400KHz but not beyond. I2C is inherently not suited for high speed. Engineers have tried to push it past the 400KHz but that requires some nasty I/O pads. That is why, on my I/O expansion board, I chose to go for SPI . There where a lot more I2C I/O expansion devices but I ignored them and went straight for SPI. With a 20MHz SPI your latency is much lower then a 400KHz I2C. That makes it easier to do fast I/O.


Hmm I2C spec available from NXP has gone up to 3.4MHz for years, not many chip manufacturers have supported it. Ramtron produce various FerroElectric Memories and RTC devices in SPI and I2C with I2C upto 1MHz.

One advantage of I2C over SPI is that it is inherently suited to battery operation and most common devices will work with supplies down to 2V, and have low current draw. Classic PCF8574[A] and PCF8591 are specced that way.

I have run various I2C devices over the years at varying speeds (>1MHz) and some over many metres of cable.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK