I2C Entity¶
API Documentation: [cpp] [python] [.NET] [LabVIEW]
BrainStem modules may have the ability to read, write data on up to 2 I2C bus’s
Read¶
i2c [ index ] . read => (unsigned char) address, (unsigned char) length <= (unsigned char*) data
Reads up to 26 bytes from the i2c bus given by the index. The parameters are the I2C address of the device on the bus, and the number of bytes to read. The result is the data that was read or an error.
Write¶
i2c [ index ] . write => (unsigned char) address, (unsigned char) length, (unsigned char*) data <= (unsigned char) result
Writes up to 26 bytes to the i2c bus given by the index. The parameters are the I2C address of the device on the bus, the number of bytes to write, and the data to write. The result is the result error condition or none.
Set Pullup¶
i2c [ index ] . setPullup => (unsigned char) bool
Sets software controlled pullup state on modules which have software controllable pullups. This setting is saved when a call to system.save is made so that Pullup settings on bus 0 can persist.
Speed (Get/Set)¶
i2c [ index ] . getSpeed <= (unsigned char) speed
i2c [ index ] . setSpeed => (unsigned char) speed
Returns or sets the I2C bus communication speed. The speed setting controls the clock frequency for I2C transactions on the specified bus.
Value |
Name |
Description |
|---|---|---|
0 |
Default |
Default I2C speed (typically 100 kHz) |
1 |
100Khz |
Standard I2C speed (100 kHz) |
2 |
400Khz |
Fast I2C speed (400 kHz) |
3 |
1000Khz |
Fast Plus I2C speed (1 MHz) |
Code Examples¶
C++¶
// All commands return aErr values when errors are encountered and aErrNone on
// success.
char buff[2];
stem.i2c[0].read(0x42, 0x02, buff); // reads two from device with address 0x42.
char wrbuff[] = {0xBE, 0xEF};
stem.i2c[0].write(0x42, 0x02, wrbuff); // writes 0xBEEF to the device with address 0x42
stem.i2c[0].setPullup(true); //enables pullup on bus 0
stem.i2c[0].setSpeed(1); // sets I2C speed to 100 kHz (i2cSpeed_100Khz)
stem.i2c[0].setSpeed(2); // sets I2C speed to 400 kHz (i2cSpeed_400Khz)
stem.i2c[0].setSpeed(3); // sets I2C speed to 1 MHz (i2cSpeed_1000Khz)
unsigned char speed;
stem.i2c[0].getSpeed(&speed); // gets current I2C speed
Reflex¶
Currently this entity is not available from within the reflex language.
Python¶
The length parameter for I2C write is not used in python.
result = stem.i2c[0].read(0x42, 0x02) # reads two bytes from the i2c bus. The value is given in result.value
print result.value
err = stem.i2c[0].write(0x42, b'\xbe\xef') # writes b'\xbe\xef' to the i2c bus.
print err
err = stem.i2c[0].setPullup(True)
print err
err = stem.i2c[0].setSpeed(1) # sets I2C speed to 100 kHz (i2cSpeed_100Khz)
print err
speed = stem.i2c[0].getSpeed() # gets current I2C speed
print speed.value