[−][src]Module rppal::i2c
Interface for the I2C peripheral.
The Broadcom Serial Controller (BSC) peripheral controls a proprietary bus
compliant with the I2C bus/interface. RPPAL communicates with the BSC
using the i2cdev
device interface.
I2C buses
The Raspberry Pi's BCM283x SoC supports three hardware I2C buses, however only the I2C bus on physical pins 3 and 5 should be used to communicate with slave devices. The other two buses are used internally as an HDMI interface, and for HAT identification.
The I2C bus connected to physical pins 3 (SDA) and 5 (SCL) is disabled by
default. You can enable it through sudo raspi-config
, or by manually
adding dtparam=i2c_arm=on
to /boot/config.txt
. Remember to reboot
the Raspberry Pi afterwards.
In addition to the hardware I2C buses, it's possible to configure a
bit-banged software I2C bus on any available GPIO pins through the i2c-gpio
device tree overlay. More details on enabling and configuring i2c-gpio
can be found in /boot/overlays/README
.
Transmission speed
The BSC supports I2C data transfer rates up to 400 kbit/s (Fast-mode).
By default, the I2C bus clock speed is set to 100 kHz. Transferring
1 bit takes 1 clock cycle. You can change the
transfer rate by adding dtparam=i2c_arm_baudrate=X
to
/boot/config.txt
, where X
should be replaced with the
clock frequency in hertz (Hz). Remember to reboot
the Raspberry Pi afterwards.
Not supported
Some I2C and SMBus features aren't fully supported by the i2cdev
interface, the underlying driver or
the BCM283x SoC: 10-bit slave addresses, SMBus Block Read, SMBus Block Process Call, SMBus Host Notify,
SMBus Read/Write 32/64, and the SMBus Address Resolution Protocol.
While clock stretching is supported, a bug exists in the implementation on the BCM283x SoC that will result in corrupted data when a slave device uses clock stretching at arbitrary points during the transfer. Clock stretching only works properly during read operations, directly after the ACK phase, when the additional delay is longer than half of a clock period. More information can be found here.
A possible workaround for slave devices that require clock stretching at other points during the transfer is
to use a bit-banged software I2C bus by configuring the i2c-gpio
device tree overlay as described in /boot/overlays/README
.
Troubleshooting
Permission denied
If new
returns an io::ErrorKind::PermissionDenied
error, make sure the file permissions for /dev/i2c-1
or /dev/i2c-0
are correct, and the current user is a member of the i2c
group.
Timed out
Transactions return an io::ErrorKind::TimedOut
error when their duration
exceeds the timeout value. You can change the timeout using set_timeout
.
Structs
Capabilities | Lists the features supported by the underlying drivers. |
I2c | Provides access to the Raspberry Pi's I2C peripheral. |
Enums
Error | Errors that can occur when accessing the I2C peripheral. |
Type Definitions
Result | Result type returned from methods that can have |