interrupt hangs program

Post Reply
YOBE.
Posts: 43
Joined: Thu Nov 29, 2012 1:38 am

interrupt hangs program

Post by YOBE. » Thu Mar 03, 2016 1:52 pm

Hi,

any smart guys who know how to solve this professionally? I use Fubarino, but Chipkit has same issue.
As soon as I start to generate interrupts the software hangs,
These are the defined interrupts.

attachInterrupt(4, leftEncoder, RISING);
attachInterrupt(0, rightEncoder, RISING);

When I execute a routine which involves wire.h and simultaneously external interrupts are generated, the software hangs.
Without generating interrupts the software runs perfectly.
External interrupts come from 2 motor-encoders who generate a high amount of pulses.
For the moment I can solve it by disabling the interrupts when the wire routine is executed. But looks amateurism... (see code below).

Anybody has suggestions how to solve this as a pro. Or is this the only way?

Thanks!

const uint8_t IMUAddress = 0x68; // AD0 is logic low on the PCB
const uint16_t I2C_TIMEOUT = 1000; // Used to check for errors in I2C communication

uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop) {
return i2cWrite(registerAddress,&data,1,sendStop); // Returns 0 on success
}

uint8_t i2cWrite(uint8_t registerAddress, uint8_t *data, uint8_t length, bool sendStop) {
Wire.beginTransmission(IMUAddress);
Wire.send(registerAddress);
Wire.send(data, length);
uint8_t rcode = Wire.endTransmission();//sendStop); // Returns 0 on success
if (rcode) {
Serial1.print(("i2cWrite failed: "));
Serial1.println(rcode);
}
return rcode;
}

uint8_t i2cRead(uint8_t registerAddress, uint8_t *data, uint8_t nbytes) {
uint32_t timeOutTimer;

Wire.beginTransmission(IMUAddress);
Wire.send(registerAddress);

THIS PART NEEDS ISOLATION!!!!
clearIntEnable(_EXTERNAL_0_VECTOR); used to disable external interrupt, crashes with wire.h
clearIntEnable(_EXTERNAL_4_VECTOR);

uint8_t rcode = Wire.endTransmission();//false); // Don't release the bus
if (rcode) {
Serial1.print(("i2cRead failed: "));
Serial1.println(rcode);
return rcode;
}
Wire.requestFrom(IMUAddress, nbytes);//,(uint8_t)true); // Send a repeated start and then release the bus after reading

setIntEnable(_EXTERNAL_0_VECTOR); // used to enable external interrupt, crashes with wire!!
setIntEnable(_EXTERNAL_4_VECTOR);
BEYOND THIS PART, NO NEED ISOLATION!!!!


for (uint8_t i = 0; i < nbytes; i++) {
if (Wire.available())
data = Wire.receive();
else {
timeOutTimer = micros();
while (((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available());
if (Wire.available())
data = Wire.receive();
else {
Serial1.println(("i2cRead timeout"));
return 5; // This error value is not already taken by endTransmission
}
}
}


return 0; // Success
}

User avatar
Jacob Christ
Posts: 723
Joined: Sat May 28, 2011 6:34 am
Location: Southern California
Contact:

Re: interrupt hangs program

Post by Jacob Christ » Sun Jun 12, 2016 2:28 am

Which version of the tools are you using? I was trying to use interrupts with the Fubarino SD a couple of days ago with no luck with chipKIT-core 1.1.0. I'm going to dig in to it in about a week if someone else doesn't get to it first.

Jacob
PONTECH Quick240 an industrial form factor platform for chipKIT and Arduino users.

Post Reply