Go Down

Topic: Wire lib vs I2C lib - interrupts, performance. (Read 2355 times) previous topic - next topic


From my look at the Wire Lib and the I2C lib (http://dsscircuits.com/articles/arduino-i2c-master-library.html), its apparent that the Wire Lib uses interrupts whereas the I2C lib polls and essentially blocks activity until the function has completed.

I understand the advantage of interrupts in general, but what are the differences in performance between I2C and the Wire lib?


If you scroll down the page on dsscircuits.com you'll find a pretty detailed timing comparison between the I2C and wire library ;-)
That should explain any performance differences.



Feb 27, 2013, 03:44 am Last Edit: Feb 27, 2013, 03:46 am by louarnold Reason: 1

If you scroll down the page on dsscircuits.com you'll find a pretty detailed timing comparison between the I2C and wire library ;-)
That should explain any performance differences.
It does - in favor of the Wire lib. Compare bytes/us or us/byte. You'll see the Wire library is faster (more bytes/us or less us/byte) in all three cases. However, the text says the results are out of date. Note that the interrupt code may not have been removed in that version of the library.

However, micro-controllers are about real-time events and polling tends to block the real-time system. Or does it?


Wire...unless the text at http://playground.arduino.cc/Main/WireLibraryDetailedReference, section "The Wire Library" is out of date, the Wire library uses blocked I/O and not interrupts:

"The Wire library's I/O blocks. This means that when sending or receiving over the I2C bus, your application is prevented from running until the communication is complete. In truth the operation of the TWI hardware in your processor is interrupt-driven, so your application should be free to run at full speed while the TWI communication takes place, but this capability is not utilized by the Wire library. "

Have I misunderstood something?


Mar 05, 2013, 08:17 pm Last Edit: Mar 06, 2013, 06:58 pm by snoozerman Reason: 1
I had a look at the two some time ago and went for the I2C library. The compile size of I2C was the main reason for me. I also did some basic speed tests (reading only) with I2C beeing the better. However, I tweaked the I2C code somewhat and managed to increase the speed a bit more. Unfortunately I don't have the test results left so I take it from the back of my head.

I attach the tweaked files if you would like to test for yourself. NO guarantees given they work - code is not thoroughly verified but they work for me. I have used them with Ardunio mini and Arduino Duemillanove boards and IDE ver 1.0.1 on Windows 7.

I2C can also be set to work with 400 Hz bus. I don't think you can do that with Wire - can you? Not sure.



Mar 06, 2013, 03:39 am Last Edit: Mar 06, 2013, 03:48 am by Headroom Reason: 1
The I2c library is faster than the wire library. The values presented in the table are code-size in bytes and duration of whatever write read function. The calculation you did assumes that the first number given is the number of bytes transferred per time, which, again , is not what the numbers represent.
The I2C signals are realized in hardware, thus the likelyhood that your application will have to wait because of the " blocking" behavior is very small. Removing dependence on interrupts makes this library more flexible to use as it eliminates its chance of interfering with other libraries that may also want to use the same interrupts.

The reason this library worked better for me was because of the timeout and reset, a function the wire library does it offer, or at least did not when I write the software for my prject.



Mar 08, 2013, 06:42 pm Last Edit: Mar 08, 2013, 06:45 pm by louarnold Reason: 1
To snoozerman and Headroom:
Thanks for sharing your experience and my apologies for my hasty review of the I2C stats.

I'll take the modified file and see what its like.


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131