TWI Slave - How does it tell master it's done sending

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

Hi Guys,

 

If a master expects 4 bytes of data from a slave on a request, but the slave only has 2 bytes for a given register, how does the master know that's all it's going to get? The docs say that the master may get less than it's asking for but doesn't explain how it knows it not going to get anymore.

 

regards

 

Nik Middleton

 

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

nikm wrote:
If a master expects 4 bytes of data from a slave on a request, but the slave only has 2 bytes for a given register

Why would the master not know the slave only can provide 2 bytes for a given request? 

The master asks for data until it decides it's done by sending a NAK, at which point the slave releases control to the master, as long as the master ACKs the data, the slave must send more.

The slave can refuse to answer the master with a NAK to it's address, but if it ACKs its address, it must supply any data the master requests.

 

Jim

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

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

Thanks

 

I agree the master should know. I'm just trying to protect the user from themselves ie trying to make the slave robust

 

regards

 

Nik Middleton

 

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

To signal the master that there is no more useful data, the slave may simply send a special byte after the last valid one:

[1] A byte which cannot occur in the data stream (this depends on the application).

or

[2] A byte which is related, in a way or another, to the last valid byte sent; for example, all/some bits inverted or its 2's complement.

 

There is another solution but it is time consuming. It is by keeping the I2C bus busy (by not sending a byte for a while). An overflow of a master timer would signal the end of transmission; ended by the slave.

 

Also, if the slave acts temporarily as a master, the arbitration will be lost and the master will detect it before it resumes another transfer.

 

Last Edited: Thu. Jul 11, 2019 - 06:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

nikm wrote:

Thanks

 

I agree the master should know. I'm just trying to protect the user from themselves ie trying to make the slave robust

 

regards

 

 

well if you are trying to protect the user, make sure the master can never send more than the expected bytes, so before asking the slave to send bytes, check if the number is correct and if not do either nothing or throw an error. Or make sure the maximum can never be exceeded during entry by the user.

 

as already said the slave should keep on sending data till the master is done. You cannot get it more robust than that. If the master is not under your control, and you have clearly stated that no more than X bytes should be read consecutive at any time, then if the user decides to read more, they should be accepting the fact that they will get garbage, as they do not adhere to the specification.

If the expected data is to be within a specific range you could just be nice to the user and send a value outside the range. That should trigger them.... Other than that there is no way to keep stupidity from happening, just make sure you adhere to the specifications you made or follow.