October 6, 2016

The Kyu real-time operating system -- BBB i2c routines

The BBB has 3 hardware i2c controllers. Kyu also supports "bit bang" style i2c on any pair of GPIO pins

struct i2c * = i2c_hw_new ( num ) where num = 0, 1, 2

struct i2c * = i2c_gpio_new ( sda_pin, scl_pin )

i2c_send ( ip, addr, buf, n )

i2c_recv ( ip, addr, buf, n ) The above are all you truly need, not that if you want to use a bit banged interface, you make a call like i2c_gpio_new ( GPIO_2_2, GPIO_2_5 ) and use the GPIO definitions in the omap_gpio.h header. Using the hardware i2c is far better though. Some convenience routines are provided:

i2c_write_8 ( ip, addr, reg, val )

i2c_read_reg_n ( ip, addr, reg, buf, n )

n = i2c_read_8 ( ip, addr, reg )

n = i2c_read_16 ( ip, addr, reg )

n = i2c_read_24 ( ip, addr, reg )

A file with a number of i2c demos for popular chips is supplied as i2c_demos.c and is worthy of study. It has code for:

bmp180 temperature and pressure sensor
hdc1008 temperature and humidity sensor
mcp4725 DAC
mcp23008 io expander (8 bit)
mcp23017 io expander (16 bit)

Have any comments? Questions? Drop me a line!

Kyu / tom@mmto.org