Arduino Due: TWI/I2C communication using TC - timer counter

I don't know why it stays in that while-loop and I don't the Atmel library. But I can tell how to make the Slave code better.

Use the "volatile" keyword for variables that are used both in the interrupts and in the loop(). For example 'state' and 'automatic' and maybe more.

Don't use Serial functions in the interrupt routines receiveHandler() and requestHandler(). The Serial functions use interrupts themself. That means it might work until something changes and then it suddenly goes wrong.

Don't use this in the receiveHandler:

while(!Wire.available()) {
  }

Test in the receiveHandler if the number of received bytes is correct. Some users on this forum tell that the arduino Due sometimes receives a false interrupt with zero bytes.

void receiveHandler(int mBytes) {
  if( mBytes == 1) {
    byte newMessage = Wire.read();
    if (automatic) {
      ...

I don't know if the outputBuffer is handled correctly.