I2C turns out to be fairly easy and straightforward on the BBB.
There are three I2C busses (I2C0, I2C1, I2C2), of which I2C2 is easily usable (Expansion port P9, see table 11 in the SRM).
I2C0: used for some on-board components (HDMI, EEPROM, power mgmt). Not brought out to any connector/header
I2C1: entirely free to use, but doesn't seem to be enabled
I2C2: entirely free to use, and works
NOTE: In software, the busses are numbered differently; I2C2 is identified as 1. I2C0 is identified as 0.
The C code in the zip file attached worked for me on I2C2 bus (it's just a tidied version of various other peoples code, with some wrapper
functions to make life easier).
// bus=1 for interface I2C2 on BBB
// returns handle to be used in remainder functions
// addr is a 7-bit value (so, for example the datasheet specifies
// 0xEE, so we need to right-shift by 1 and use 0x77 for this function)
int i2c_open(unsigned char bus, unsigned char addr);
// These functions return -1 on error, otherwise return the number of bytes read/written:
int i2c_write(int handle, unsigned char* buf, unsigned char length);
int i2c_read(int handle, unsigned char* buf, unsigned char length);
int i2c_write_read(int handle,
unsigned char addr_w, unsigned char *buf_w, unsigned int len_w,
unsigned char addr_r, unsigned char *buf_r, unsigned int len_r);
int i2c_write_ignore_nack(int handle,
unsigned char addr_w, unsigned char* buf, unsigned int length);
int i2c_read_no_ack(int handle,
unsigned char addr_r, unsigned char* buf, unsigned int length);
int i2c_write_byte(int handle, unsigned char val);
int i2c_read_byte(int handle, unsigned char* val);
// These functions return -1 on error, otherwise return 0 on success
int i2c_close(int handle);
// Provides an inaccurate delay (may be useful for waiting for ADC etc).
// The maximum delay is 999msec
int delay_ms(unsigned int msec);
By default the bus appears to run at 100kbps approx.
I created a test program using the nearest I2C device at hand (which happened to be a BMP085 temperature and pressure sensor),
it is part of the zip contents.
Temperature is 23.6 degrees C
Pressure is 101.027 kPa
I noticed it is not possible to control the power mgmt ic using the code, since presumably it is a protected resource.
I2C2 on Expansion header P9:
pin 19: SCL
pin 20: SDA
pin 1: 0V
pin 3: 3.3V
Message was edited by: Shabaz - added more recent v3 which adds a couple more functions, and added a precompiled library (put it in /usr/lib and put the header in /usr/include)