I2C Slave problem with stretching SCL

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

This time, I'm fighting with I2C and need some help with my Slave.

I'm using the Hardware TWI on Master and on Slave Side.
Everything is working fine, except one thing:
When the Master wants to get data from the slave, but the slave is not ready yet, the slave ISR(TWI_vect) is executed again and again, because the slave is not clearing the TWINT Flag (to hold down the SCL). This repeated calls of the ISR Vector are slowing down all other things.

So, the idea was to switch off the TWI Interrupt (TWIE) after the SLA+R is received and switch the Interrupt back on when the data is ready to send.
But when I do this, everything goes wrong. The Master isn't waiting for the slave and the communication breaks down.

My question: Is my idea (switching the Interrupt off) bad and I should find another way? Or should this method work, but I have to search the problem somewhere else?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely you design a Slave to always have a reply?

If you know that your Slave can be preoccupied at times, use a 'Read_Busy' command.

The Master checks the Slave's 'status' before reading the reply. Exactly the same procedure that you use with any hardware chip.

Look at the way a 24Cxx works. The Slave does not ACK its address while it is busy doing a page write.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely you design a Slave to always have a reply?

If you know that your Slave can be preoccupied at times, use a 'Read_Busy' command.

The Master checks the Slave's 'status' before reading the reply. Exactly the same procedure that you use with any hardware chip.

Look at the way a 24Cxx works. The Slave does not ACK its address while it is busy doing a page write.

Yes. You can just disable the TWEN while busy. So it will not bother to respond to a Start.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, you mean the idea to extend the SCL is not the best way and I should switch of the Slave TWI until the data is ready, while the Master is polling the TWI until the slave is answering?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That would be the common way of doing it. [it prevents hogging/hanging of the bus]. With your method, the master is stuck waiting on you, and cannot service any other devices on the bus in the meantime. If the slave died in that state, the whole system would die.

Note that you don't need to switch off TWI, just clear the TWEA bit, so that the slave will not acknowledge its own address. This is important, so that the TWI's hardware state-machine remains in sync with what is happening on the bus.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.