Performing multiple Wire.writes() - does endtransmission slow things down? Here's a question for anyone knowledgeable of the ways of I2C. 

I'm curious about sending multiple writes to a device and was looking at the code for a MPU9250 module. 

There is a nice library that compiled straight off the shelf by Kris Winer here.

In one of his code examples he makes multiple calls to a function called writeByte(): 

// Wire.h read and write protocols
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
    Wire.beginTransmission(address);  // Initialize the Tx buffer
    Wire.write(subAddress);           // Put slave register address in Tx buffer
    Wire.write(data);                 // Put data in Tx buffer
    Wire.endTransmission();           // Send the Tx buffer
}


here is a (truncated) example of the code calling writeByte():

void initMPU9250()
{  
  writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x00); 
  delay(100); 

  writeByte(MPU9250_ADDRESS, PWR_MGMT_1, 0x01);  
  delay(200); 
  writeByte(MPU9250_ADDRESS, CONFIG, 0x03);  
  writeByte(MPU9250_ADDRESS, SMPLRT_DIV, 0x04); 
  .
  .
  .
  etc
}


My question is why call Wire.endtransmission() each time your sending I2C to the same device? I can see one reason straight off the bat which is you may need to call delay after your write() - but in cases where you do not need to call delay, wouldnt it be faster to do something like:

Wire.beginTransmission(address); 
Wire.write(data1);
Wire.write(data2);
Wire.write(data3);
Wire.write(data4);
Wire.endTransmission();


at least when it's possible?
No, Wire.endTransmission() doesn't "slow things down". It is, however, critical to the operation of the Wire library.

When you do a Wire transaction all the data you send is first buffered in an internal array. Only when you call Wire.endTransmission() does that data actually get sent out. The actual Wire.endTransmission() doesn't add anything extra to the transmission though.

In your example you are sending a total of 5 bytes (address + 4 data bytes) plus start/ACK/stop bits. In general most devices require you to send specific numbers of bytes in a specific order - the most common being a register address followed by the content of that register. That means 3 bytes. Any more bytes after that in the same transaction will either be ignored or would overwrite the first bytes. 

Some devices though would allow &lt;address&gt;&lt;value&gt;&lt;address&gt;&lt;value&gt; to be sent, thus removing the need for the repeated I2C address byte for the device each time. Many won't though.

Other devices (most notably flash memory or SRAM chips) would allow you to do page mode accesses, which would be &lt;address&gt;&lt;data1&gt;&lt;data2&gt;...&lt;dataN&gt; where the address would automatically increment for each data byte. Again, some may, some won't.

It is entirely down to the device you are sending data to as to whether you are able to batch together multiple blocks of data, and the form that batching has to be in if it is supported at all. So you should look in the datasheet for the device to see just what it can support.