Does Wire Library Interrupt Main Sketch

I'm quite a beginner with Arduino programming and I'm learning about I2C communication between 2 Arduino Unos. I'm reading whatever I can find and think I'm getting there.

My master Uno will be running a sketch and will initiate the I2C communication when it is ready so no issues there.

My slave Uno on the other hand will have to answer the master when it "calls".

From what I have studied, am I correct in saying the following:

1). The I2C is hardware interrupt based so the slave will "down tools" and have it's running sketch interrupted when the master wants to start communication.

2). When the I2C communication is finished the slaves sketch will carry on from where it was interrupted.

This will not be a problem for my slaves sketch, but I just want to confirm this is how the wire library works.

I think this will answer your question.

Thanks for the link Lui, but I can't see my question answered there.

Is it not as simple as saying my statements in the opening post are correct or incorrect ? or is my question too simplistic and thus it can't be answered that way.

I'm a beginner so may not immediately see what other more accomplished guys might see.

Is it not as simple as saying my statements in the opening post are correct or incorrect ? or is my question too simplistic and thus it can't be answered that way.

The master WILL interrupt what the slave is doing, when it needs an answer.

2). When the I2C communication is finished the slaves sketch will carry on from where it was interrupted.

Yes. That is how interrupts work.

What @PaulS has said is also true for Serial communication. There is nothing strange about it.

...R

beefy23:
Thanks for the link Lui, but I can't see my question answered there.

Is it not as simple as saying my statements in the opening post are correct or incorrect ? or is my question too simplistic and thus it can't be answered that way.

I'm a beginner so may not immediately see what other more accomplished guys might see.

The code that you have in the example:

#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

don't have anything in the main loop() only:

void loop()
{
  delay(100);
}

So, if the loop() does nothing, and the sketch does something, the receiver's loop() must to be interrupted during the communication.

serialEventRun sure does (NOT) look as an interrupt code to me.
There is a difference between an event and interrupt, but if you use "blink without delay" it is all same.

("Sarcasm" sign displayed )

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Both questions, answer is yes.

Additionally, all I2C transfers (master or slave) rely on lots of little interrupts you don't see. The data register is one byte, and usually you want to send or receive more than one of those - a little ISR runs every byte sent/received and shuffles the next byte between the buffer and the data register. You don't see these, though - until you try to use I2C with interrupts disabled. The same is true of serial and SPI, which work much the same way.

serialEventRun sure does (NOT) look as an interrupt code to me.

It isn't. But that has NOTHING to do with the fact that sending and receiving serial data IS interrupt driven.

How YOUR sketch deals with serial data does NOT involve interrupts.

Thank you all very much, everything's a lot clearer now.

I've also came across some more info explaining how the wire library interacts with, and uses the AVRs TWI hardware, and how the TWI interrupt gets activated by various conditions.

Keith.