User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

SOLVED: I2C clock speed / performance

Mon May 19, 2014 1:01 pm

How do I check / set the i2c bus speed?

I read 14 bytes of sensor data over the i2c from my MPU6050. The data registers are updated every 4ms (configurable). The code does the read up to 150 times a second.

So currently it reads 14bytes = 112 bits (plus i2c overheads) requiring > 17kbps over the I2C.

The code loops ever 6.666ms and 60% of that time is spent reading the i2c data - i.e. 4ms.

This all works fine but I'd like to speed it up.

Speeding up the rate of the MPU6050 data register update has zero effect (still 60% of 6.666ms), and in fact, if I increase it much more, the code blocks as it's unable to read a full data set before the data is updated again suggesting possibly it may be the i2c bus itself which is taking time.

How do I check / set the i2c bus speed? The MPU6050 can do 400kbps, but even 100kbps should suffice - I mention 100kbps because it's a value mentioned on the forum, but not how to set / check it.

Thanks.
Last edited by Hove on Mon May 19, 2014 3:19 pm, edited 1 time in total.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
joan
Posts: 14198
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C clock speed / performance

Mon May 19, 2014 1:06 pm

I believe 100 kbps is the default I2C speed on the Pi.

Yes. A fragment of i2cdetect.
i2c.png
i2c.png (40.69 KiB) Viewed 13318 times
About 30 I2C transactions from itcdetect
i2cdetect.png
i2cdetect.png (41.05 KiB) Viewed 13311 times
Last edited by joan on Mon May 19, 2014 1:30 pm, edited 1 time in total.

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: I2C clock speed / performance

Mon May 19, 2014 1:20 pm

The speed of the I2c buses can be set.
i2c bus has to be quiescent before changing speeds
100k, 400k and 1.2Mhz are considered standard speeds.

Code: Select all

#To set the speed to 1.2 Mhz (FM+)
modprobe -r i2c_bcm2708
modprobe i2c_bcm2708 baudrate=1200000
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: I2C clock speed / performance

Mon May 19, 2014 1:58 pm

Thanks Joan, Richard.

@Joan - what tool were you using to capture the trace - digital 'scope, or is there an RPi tool?

@Richard - does the config of i2c bus speed require doing every reboot?

Cheers,

Andy
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
joan
Posts: 14198
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C clock speed / performance

Mon May 19, 2014 1:59 pm

Hove wrote: ...
@Joan - what tool were you using to capture the trace - digital 'scope, or is there an RPi tool?
...
http://abyz.co.uk/rpi/pigpio/piscope.html

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: I2C clock speed / performance

Mon May 19, 2014 2:30 pm

Increasing the bus speed to 400kbps has had a significant effect:

@100kbps I2C baudrate: total code processing loop ~ 6.7ms, 60% of which reading sensors (4ms read time)
@400kbps I2C baudrate: total code processing loop ~ 5ms, 41.6% of which reading sensors (2ms read time)

I think that then means there are place in the code where I'm waiting for the data ready interrupt which can have the 'miss' timer reduced speeding things up further.

One last question - can the 400kbps remain post reboot, or do I need to add it to a bash script in init.d?
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
joan
Posts: 14198
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C clock speed / performance

Mon May 19, 2014 2:39 pm

I think you can add the parameter to /etc/modules

Code: Select all

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

#snd-bcm2835
i2c-bcm2708 baudrate=400000
i2c-dev

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: I2C clock speed / performance

Mon May 19, 2014 3:19 pm

joan wrote:I think you can add the parameter to /etc/modules

Code: Select all

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

#snd-bcm2835
i2c-bcm2708 baudrate=400000
i2c-dev
That didn't work for me, but the following did:

"To apply a setting as a default to be used every time the spidev driver is loaded we need to create a file in /etc/modprobe.d/ that contains the line:

Code: Select all

options i2c_bcm2708 baudrate=400000
"

That came from http://raspberrypi.znix.com/hipidocs/topic_i2cdev.htm

Cheers!
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: SOLVED: I2C clock speed / performance

Wed May 21, 2014 3:53 am

You can set the speed manually at any time. No reboot required. All that is required is that the bus be quiescent.

I put the code I posted into /etc/rc.local so that the speed is set correctly after every reboot.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: SOLVED: I2C clock speed / performance

Wed May 21, 2014 5:58 am

Richard-TX wrote:You can set the speed manually at any time. No reboot required. All that is required is that the bus be quiescent.
A bit tedious to do every boot though...
Richard-TX wrote:I put the code I posted into /etc/rc.local so that the speed is set correctly after every reboot.
I agree that would work fine, but I think the "official" way is to add a config file into /etc/modprobe.d/ I found with some googling (see previous post). Worked every time for me on Raspian. That way, modprobe starts the i2c_bcm2708 @ 400kbps on boot.
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: SOLVED: I2C clock speed / performance

Wed May 21, 2014 2:01 pm

Hove wrote:
Richard-TX wrote:You can set the speed manually at any time. No reboot required. All that is required is that the bus be quiescent.
A bit tedious to do every boot though...
Richard-TX wrote:I put the code I posted into /etc/rc.local so that the speed is set correctly after every reboot.
I agree that would work fine, but I think the "official" way is to add a config file into /etc/modprobe.d/ I found with some googling (see previous post). Worked every time for me on Raspian. That way, modprobe starts the i2c_bcm2708 @ 400kbps on boot.
As with most things *nix, there is more than one "right" way to do something. I remember reviewing a CBT (Computer Based Training) course some years ago for the SMU School of Engineering. The answer to one of the first questions was "ls -al" and "ls -la" was considered an erroneous response. Suffice to say that package was rejected.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

User avatar
Hove
Posts: 1205
Joined: Sun Oct 21, 2012 6:55 pm
Location: Cotswolds, UK
Contact: Website

Re: SOLVED: I2C clock speed / performance

Wed May 21, 2014 2:40 pm

Richard-TX wrote:...As with most things *nix, there is more than one "right" way to do something. I remember reviewing a CBT (Computer Based Training) course some years ago for the SMU School of Engineering. The answer to one of the first questions was "ls -al" and "ls -la" was considered an erroneous response. Suffice to say that package was rejected.
Oh dear, that CBT was rather pedantic and wrong too - no wonder people get scared off from computing
www.pistuffing.co.uk - Raspberry Pi and other stuffing!

laksh
Posts: 7
Joined: Mon Apr 11, 2016 1:02 am

Re: SOLVED: I2C clock speed / performance

Wed Apr 13, 2016 5:54 am

which code u work with mpu6050.i m using i2cdev c language(pibits,gitub).on running demo_dmp,only loops 1200 times on either b+ or rpi3.evenarduino @16mhz is faster in looping 4000times.i also want to run under 10ms but how?
laksh

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: SOLVED: I2C clock speed / performance

Sun Apr 17, 2016 4:16 am

To get high speed from the I2C bus connected device a few things have to be done.

- set the I2c clock (bus) speed as high as the device can handle it. In the case of the MPU6050, the clock speed is limited to 400KC. Unless you get the SPI version, that is as fast as it will go. The SPI version is rated at 1 Mhz.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Return to “Interfacing (DSI, CSI, I2C, etc.)”