Pages: [1]   Go Down
Author Topic: Wire (i2c) and Interrupts -- LOW mode Bug/Issue  (Read 448 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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

Code:
  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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't easily check right now but are you sure that interrupt handler 0 is pin 2 on the Seeduino?
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Apologies for this goose chase
If you learned something, it wasn't a wild goose chase.
Logged

Pages: [1]   Go Up
Jump to: