Changing the Raspberry Pi/Pi2 I2C bus speed, device tree style

Pi2v1Recent releases of Raspbian have started to use the device tree which has the effect of altering the procedure for controlling the I2C bus speed (amongst other things). The new procedure involves a device tree overlay or device tree parameter. These instructions have been distilled from this page.

On the latest Raspbian releases, the required overrides are already present. All that’s needed is to set a device tree parameter. To do this, edit /boot/config.txt and add the line:

dtparam=i2c1_baudrate=400000

Reboot and then you should see a message ( or use the dmesg command) indicating that the I2C speed is indeed 400000. If the release is older, a device tree overlay is needed to add the overrides.

The new device tree source file should look like this:

// Definitions for the missing i2c baudrate parameters
/dts-v1/;
/plugin/;
/ {
    compatible = "brcm,bcm2708";
    __overrides__ {
        i2c0_baudrate = <&i2c0>,"clock-frequency:0";
        i2c1_baudrate = <&i2c1>,"clock-frequency:0";
    };
};

The text should be put into a file called i2c-speed.dts for example. This needs to be compiled into a blob using the device tree compiler. This can be done on the Pi itself:

sudo apt-get install device-tree-compiler

This will install dtc if it isn’t already there. Then:

sudo dtc -I dts -O dtb -@ -o /boot/overlays/i2c-speed-overlay.dtb i2c-speed.dts

Then, edit /boot/config.txt and add the line:

dtoverlay=i2c-speed,i2c1_baudrate=400000

The “400000” sets the speed to 400kHz for example. Then reboot to kick in the new configuration.

Advertisements

2 thoughts on “Changing the Raspberry Pi/Pi2 I2C bus speed, device tree style

  1. Pingback: Setting the Raspberry Pi I2C ports to operate at 400kHz | richards-tech notes

  2. JohnN

    Thanks for the post. It was informative, but didn’t quite provide everything I needed to solve a problem. I did a distribution upgrade to …

    Linux rpi0 4.1.18+ #846 Thu Feb 25 14:11:56 GMT 2016 armv6l GNU/Linux

    …and my i2c bus stopped working. After putting a scope on the bus I determined it was running at 800KHz despite my changing values for i2c1_baudrate in /boot/config.txt. The upgrade apparently rewrote /etc/modprobe.d/i2c.conf to set the 800KHz. I had thought the devicetree changes had deprecated that config file, but apparently not.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s