uBit.i2c¶
Overview¶
i2c is a very common, widely used protocol for communicating with other devices over a wire. i2c uses a very simple addressing scheme to access these other devices.
Onboard the micro:bit itself there are two components which use i2c bus as a communication mechanism, the accelerometer and the compass.
As well as being used internally, the i2c bus is exposed on two edge connector pins, P19 and P20. This means other accessories that use i2c to communicate can be used in conjunction with the micro:bit.
Message Bus ID¶
None.
Message Bus Events¶
None.
API¶
Constructor¶
MicroBitI2C( PinName sda, PinName scl)¶
Description¶
Constructor.
Create an instance of MicroBitI2C for I2C communication.
Parameters¶
PinNamesda - the Pin to be used for SDAPinNamescl - the Pin to be used for SCL
Example¶
MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Note
This class presents a wrapped mbed call to capture failed I2C operations caused by a known silicon bug in the nrf51822. Attempts to automatically reset and restart the I2C hardware if this case is detected.
read¶
int read( int address, char * data, int length)¶
Description¶
Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
Parameters¶
intaddress - 8-bit I2C slave address [ addr | 1 ]char *data - A pointer to a byte buffer used for storing retrieved data.intlength - Number of bytes to read.
Returns¶
MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved read failure is detected.
int read( int address, char * data, int length, bool repeated)¶
Description¶
Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
Parameters¶
intaddress - 8-bit I2C slave address [ addr | 1 ]char *data - A pointer to a byte buffer used for storing retrieved data.intlength - Number of bytes to read.boolrepeated - if true, stop is not sent at the end. Defaults to false.
Returns¶
MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved read failure is detected.
write¶
int write( int address, const char * data, int length)¶
Description¶
Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
Parameters¶
intaddress - 8-bit I2C slave address [ addr | 0 ]const char *data - A pointer to a byte buffer containing the data to write.intlength - Number of bytes to write
Returns¶
MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected.
int write( int address, const char * data, int length, bool repeated)¶
Description¶
Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
Parameters¶
intaddress - 8-bit I2C slave address [ addr | 0 ]const char *data - A pointer to a byte buffer containing the data to write.intlength - Number of bytes to writeboolrepeated - if true, stop is not sent at the end. Defaults to false.
Returns¶
MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected.