Search This Blog

Monday, 31 March 2014

Speeding up the I2C bus on Raspberry Pi and Arduino

From Wikipedia

The I²C reference design has a 7-bit or a 10-bit (depending on the device used) address space.[3] Common I²C bus speeds are the 100 kbit/s standard mode and the 10 kbit/s low-speed mode, but arbitrarily low clock frequencies are also allowed. Recent revisions of I²C can host more nodes and run at faster speeds (400 kbit/s Fast mode, 1 Mbit/sFast mode plus or Fm+, and 3.4 Mbit/s High Speed mode). These speeds are more widely used on embedded systems than on PCs.
So by default the I2C bus is running at 100kHz. Not bad, but no blazing speed demon either. 

Most I2C devices can readily run at 400khz, if you keep the lines short.

On the Raspberry Pi, the bcm2708 chip manages I2C (smbus) 

#define I2C_TIMEOUT_MS 150
#define DRV_NAME "bcm2708_i2c"
static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(baudrate, "The I2C baudrate");
To increase the speed on the Raspberry Pi, you can pass an optional baudrate parameter via modprobe to the I2C module
                                                                         sudo modprobe -r i2c_bcm2708 && sudo modprobe i2c_bcm2708 baudrate=400000  
To make that change permanent, you have to create a new i2c.conf file:
sudo nano /etc/modprobe.d/i2c.conf
type    options i2c_bcm2708 baudrate=400000
                                                             ctrl-x to save (answer yes)
This will force your Raspberry Pi to initiate communications at 400k baud.

On the Arduino side, you have to locate the file twi.h 

sudo nano /usr/share/arduino/libraries/Wire/utility/twi.h
find the string “#define TWI_FREQ 100000L” and change it to:
#define TWI_FREQ 400000L (was 100000L)

This should speed up your I2C communications sufficiently, without breaking anything.


No comments:

Post a Comment