Go Down

Topic: TWI as Slave Write Issue (Read 2014 times) previous topic - next topic

veeman

I'm not sure what the main intention of using write during a twi request in the onReuqest function is, but if I do several writes, only the data of the last write is send to the master.

Example:
Code: [Select]

char buf[4];
...
void requestEvent()
{
  Wire.write(0xFE);  // skipped
  Wire.write(0x32);  // skipped
  Wire.write(buf, sizeof(buf));  // only this will be send
}


If you look in the source code of the wire/twi library you find that write calls twi_transmit(data, size).
A close up view at this function shows the problem:
Code: [Select]

uint8_t twi_transmit(const uint8_t* data, uint8_t length)
{
  uint8_t i;

  // ensure data will fit into buffer
  if(TWI_BUFFER_LENGTH < length){
    return 1;
  }
 
  // ensure we are currently a slave transmitter
  if(TWI_STX != twi_state){
    return 2;
  }
 
  // set length and copy data into tx buffer
  twi_txBufferLength = length;
  for(i = 0; i < length; ++i){
    twi_txBuffer[i] = data[i];
  }
 
  return 0;
}


-> The function overwrites every call the transmit buffer (standard length 32Bytes).
Maybe it will be better to change the function to append data to the buffer which submitted by write.
I think there shouldn't be any errors, because the TransmitIndex and TransmitLen is reseted before the event call.

I change the function to the following and it seems to work fine:
Code: [Select]

uint8_t twi_transmit(const uint8_t* data, uint8_t length)
{
  uint8_t i, t;

  // ensure data will fit into buffer
  if(TWI_BUFFER_LENGTH < twi_txBufferLength+length){
    return 1;
  }
 
  // ensure we are currently a slave transmitter
  if(TWI_STX != twi_state){
    return 2;
  }
 
  // set length and copy data into tx buffer
  t = twi_txBufferLength;
  twi_txBufferLength += length;
  for(i = 0; i < length; ++i){
    twi_txBuffer[t+i] = data[i];
  }
 
  return 0;
}


Did I missed something?
What do you think?

Erdin

In the onRequest handler, only one Wire.write may be used.
I don't know if that is written somewhere, but its a fact.
You have to build an array with data in the onRequest handler.

It would be great if your changes are okay.
The Wire.write can be used anywhere in any situation. I don't know if it can be changed while maintaining the compatibility.

I have a problem with Master-receiver myself, http://forum.arduino.cc/index.php?topic=172296.0

Erdin

I opened an issue on Github for this, https://github.com/arduino/Arduino/issues/1475

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!

Arduino
via Egeo 16
Torino, 10131
Italy