Interrupts for I2C Master (UNO R4 Wifi)

Dear forum,
I'm working on the I2C connection between UNO R4 and the Modulino Button module. The button status I want to catch by an interrupt routine.

Please refer to the code I attach.

void setup() {

  Wire1.begin();            // join i2c bus (address optional for master)
  Serial.begin(9600);       // start serial for output
 
  Wire1.onReceive(receiveEvent);

  Serial.println();
  Serial.println("Start");

}

void loop() {

  Wire1.requestFrom(62, 6);       // request amount bytes from peripheral device #62

  delay(500);
 
}

void receiveEvent(int howMany) {
  
  while (howMany-- > 0) {

    uint8_t c = Wire1.read();
    Serial.print(c, HEX);
    Serial.print(" ");   
  }

  Serial.println();

}  // end of receiveEvent

What I'm doing is: The requestFrom() is in the loop() placed, but the answer from the I2C device shall be catched by the interrupt routine. This does not work for what reason?
I seems that the ISR routine is well attached but not called.

Does anyone now the reason?
Thanks for any hint by
CTrenet

It's not? The sketch seems to be incomplete!

Yeah! Thanks for the response!
But what is missing? :wink:

It is said that the I2C interrupt is managed internally (so it's not a straight interrupt).

Refer to: Gammon Forum : Electronics : Microprocessors : Interrupts

"In this case the I2C library is designed to handle incoming I2C bytes internally, and then call your supplied function at the end of the incoming data stream. In this case receiveEvent is not strictly an ISR (it has an argument) but it is called by an inbuilt ISR."

Any idea?
Greetings!

"...

i2c doesn't need the user to program or account for an interrupt.

Ok, I see, not a regular interrupt. Have you checked out the library Modulino?

Yes, I did. I could not see a code line calling the receiveEvent handler in the Moduline lib.
I'm working with the Wire.h lib. I can see there the transfer of pointer by receveEvent and the call to the ISR (receiveEvent), but obviously it's not supported without further code.

from Wire.cpp

// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
  // don't bother if user hasn't registered a callback
  if(!user_onReceive){
    return;
  }
  // don't bother if rx buffer is in use by a master requestFrom() op
  // i know this drops data, but it allows for slight stupidity
  // meaning, they may not have read all the master requestFrom() data yet
  if(rxBufferIndex < rxBufferLength){
    return;
  }
  // copy twi rx buffer into local read buffer
  // this enables new reads to happen in parallel
  for(uint8_t i = 0; i < numBytes; ++i){
    rxBuffer[i] = inBytes[i];    
  }
  // set rx iterator vars
  rxBufferIndex = 0;
  rxBufferLength = numBytes;
  // alert user program
  user_onReceive(numBytes);
}

Regards

Did you try Nick Gammon's I2C Tutorial?

He does things that you do not.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.