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:
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:
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:
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?