Das U-Boot supports I2C busses and provides a simple C API to people developing code as well as a command line interface.
For hardware I2C busses, you need the hard define as well as a specific driver:
#define CONFIG_HARD_I2C /* Always defined for hardware I2C support */ #define CONFIG_BFIN_TWI_I2C /* Selects the Blackfin on-chip TWI peripheral driver */ #define CONFIG_SYS_I2C_SPEED /* default speed (in HZ) */ #define CONFIG_SYS_I2C_SLAVE 0
For software I2C busses, you need the soft define as well as a bunch of helper macros (see the top level README):
#define CONFIG_SOFT_I2C /* Always defined for software I2C support */ #define I2C_INIT /* initialize your software i2c */ #define I2C_ACTIVE /* activate SDA */ #define I2C_TRISTATE /* deactivate SDA */ #define I2C_READ /* read SDA */ #define I2C_SDA(bit) /* set SDA to <bit> */ #define I2C_SCL(bit) /* set SCL to <bit> */ #define I2C_DELAY /* delay some time */ #define CONFIG_SYS_I2C_SPEED /* default speed (in HZ) */ #define CONFIG_SYS_I2C_SLAVE 0
For the command line I2C interface:
#define CONFIG_CMD_I2C
The I2C API is documented in the include/i2c.h header. Consult that for more information.
With the i2c command, you can get low level access (usually for debugging purposes) straight from the command line.
bfin> help i2c i2c - I2C sub-system Usage: i2c speed [speed] - show or set I2C bus speed i2c dev [dev] - show or set current I2C bus i2c md chip address[.0, .1, .2] [# of objects] - read from I2C device i2c mm chip address[.0, .1, .2] - write to I2C device (auto-incrementing) i2c mw chip address[.0, .1, .2] value [count] - write to I2C device (fill) i2c nm chip address[.0, .1, .2] - write to I2C device (constant address) i2c crc32 chip address[.0, .1, .2] count - compute CRC32 checksum i2c probe - show devices on the I2C bus i2c reset - re-init the I2C Controller i2c loop chip address[.0, .1, .2] [# of objects] - looping read of device
Here you can easily set the I2C speed, select a different bus (for when the driver supports more than 1 I2C bus at a time), or reset the cotroller.
The i2c probe command allows for quickly discovering any clients on the I2C bus. Here you can see an I2C slave being found at address 0x20:
bfin> i2c probe Valid chip addresses: 20
The i2c md command allows you to quickly read specific registers from an I2C device. To read 16 registers starting at register 0 from slave 0x20:
bfin> i2c md 0x20 0 0x10
There are a few I2C sub-commands to write to a device in a variety of ways.
You can interactively enter values starting at register 0x5 (and auto-incrementing to the next register) to slave 0x20:
bfin> i2c mm 0x20 0x5
Or you can interactively enter values at register 0x5 over and over to slave 0x20:
bfin> i2c nm 0x20 0x5
You can write a 0x30 registers starting at register 0x5 with a specific value of 0x18 to slave 0x20:
bfin> i2c mw 0x20 0x5 0x18 0x30