Go Down

Topic: Wire (i2c) and Interrupts -- LOW mode Bug/Issue (Read 505 times) previous topic - next topic

rallen

Hello,

I am having an issue with Wire and interrupts. When I attach an interrupt under LOW mode, and use the Wire library, the program ends up crashing when Wire tries to finish transmitting. The program itself is quite long, but I was able to create a minimal sketch that I think effectively illustrates the issue:

Code: [Select]
#include <Wire.h>

void do_nothing(){}

void setup()
{
  // Comment out (will work) or Uncomment (will not work).
  // Crashes under any interrupt number [0 - 7] inclusive only
  // using LOW mode. Works for all other modes.
  attachInterrupt(0, do_nothing, LOW);
 
 
  Wire.begin(); // start I2C as master

  // Send something random to some random slave
  Wire.beginTransmission(0xAA);
  Wire.write((byte) 0xBB);
  // Program will crash or deadlock HERE
  Wire.endTransmission();

  // If program is *working* LED should light up
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); 
}

void loop(){}


Altering or removing the attachInterrupt line will let the program run to the end of setup() and turn the LED on. Otherwise, it will hang at Wire.endTransmission() and the LED will never go on (indicating failure).

Wire seems to work with the other modes, but not LOW mode. I could not see anything about this issue in attachInterrupt(), but perhaps there is info about it somewhere else that I missed. My project is using a library that requires the use of a LOW mode interrupt, so I am unable to alter it. I am using a Seeeduino w/ ATMega1280. Any help or input would be greatly appreciated (even if just testing that it also fails for you).

Thanks.

PaulS

What is connected to pin 2? If nothing, and the pin is INPUT (as it is by default) and LOW, the useless interrupt handler will be called over and over, since the pin is LOW. There will never be time for other interrupts.

Nick Gammon

LOW interrupts are called repeatedly while the level is low. Once the interrupt handler is exited, it does one more instruction, then goes back into the interrupt handler if the level is still low. This will very seriously degrade performance. You probably want a FALLING interrupt, not a LOW one.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

rallen

Hi,

Thanks for getting back to me so quickly. I might be misunderstanding your explanation, but I tried adding two extra lines:

Code: [Select]
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
  // Comment out (will work) or Uncomment (will not work).
  // Crashes under any interrupt number [0 - 7] inclusive only
  // using LOW mode. Works for all other modes.
  attachInterrupt(0, do_nothing, LOW);


and the code is still not turning on the LED. There could be something I missed, but by setting it HIGH the interrupt should never fire, yet it is still crashing / deadlocking.

Nick Gammon

What is connected to pin 2? What is the purpose of this? I don't understand why you are using LOW interrupts, and you acknowledge that this is causing the problem. Clearly the interrupt is firing or it would not lock up. Try turning on an LED in the interrupt handler to prove this.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

I can't easily check right now but are you sure that interrupt handler 0 is pin 2 on the Seeduino?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
but by setting it HIGH the interrupt should never fire, yet it is still crashing / deadlocking.

You are not setting the INPUT pin HIGH. You are turning on the pullup resistor.

rallen

I spent a few hours fiddling with the library and altered it to use FALLING (Side note: pin 2 is the int0 for the Seeeduino Mega). The problem I was having is resolved. Apologies for this goose chase and thanks again for your help.

PaulS

Quote
Apologies for this goose chase

If you learned something, it wasn't a wild goose chase.

Go Up