Timer interrupt & i2c

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Timer interrupt & i2c

Post by majenko » Thu Jul 02, 2015 12:06 pm

Yes, your two I2C operations are overlapping. You start an I2C transaction for the LCD outside of the interrupt routine, then the interrupt fires, and a new i2C transaction is started mid-way through the LCD transaction. That is bad.

You cannot use I2C both in an interrupt *and* outside the interrupt without having some kind of mutex or semaphore to ensure there is no conflict like this. The simplest solution is, as you have noticed, to not do I2C in the interrupt.

Other options include:
  • Disable the timer interrupt whenever you're doing an LCD operation
  • Use a separate I2C bus for the timer-based I2C
  • Examine the state of the Wire object in the interrupt and don't perform the interrupt transaction if one is already in progress
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

ElmaWebber
Posts: 1
Joined: Tue Nov 17, 2015 5:12 pm

Re: Timer interrupt & i2c

Post by ElmaWebber » Tue Nov 17, 2015 5:15 pm

As per my experience I2C uses interrupts itself to handle the transfers.
And it is possible to use interrupts inside interrupts, but only if you do it right.
Actually when you are in an interrupt the only interrupts that are allowed to interrupt it are those of a higher priority.
Also I2C interrupt runs at priority 3. If you ensure the timer's interrupt runs at a priority than the I2C's interrupt then it should work.

User avatar
majenko
Site Admin
Posts: 2164
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK
Contact:

Re: Timer interrupt & i2c

Post by majenko » Tue Nov 17, 2015 7:15 pm

The problem is there is only one I2C transmit buffer per I2C channel. If you start a transmission (which starts filling the buffer) then start another midway through (so the buffer gets corrupted) and then try and send the first transmission (which happens in a block at the endTransmission() call) then you just get gibberish.
Why not visit my shop? http://majenko.co.uk/catalog
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply