I thought I remember reading about this before, but I am having a crash on the GIGA
using attachInterrupt, followed by detachInterrupt and maybe it is when I then later try to reattach the interrupt.
My quick search in this forum, I did not find any hits, but I probably missed it.
The following code is a quick and dirty extract from some other code (SoftwareSerial),
that I am playing with which crashes.
#define rxpin 2
void setup() {
// put your setup code here, to run once:
while (!Serial && millis() < 4000) {}
Serial.begin(9600);
delay(1000);
Serial.println("Before Serial1 begin"); Serial.flush();
Serial1.begin(9600); //
Serial.println("Before SerialSoft begin"); Serial.flush();
pinMode(rxpin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(rxpin), start_bit_falling_edge, FALLING);
pinMode(LED_BUILTIN, OUTPUT);
}
volatile bool isr_called = false;
void start_bit_falling_edge() {
isr_called = true;
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
detachInterrupt(digitalPinToInterrupt(rxpin));
}
void loop() {
// first of simple TX only...
int ich = Serial.read();
if (ich != -1) Serial1.write(ich);
// ich = SerialSoft.read();
// if (ich != -1) SerialSoft.write(ich);
// See if We received anything on Serial1...
ich = Serial1.read();
if (ich != -1) Serial.write(ich);
if (isr_called) {
Serial.println("ISR Was called"); Serial.flush();
delay(50); // make sure it has time to finish the whole byte
isr_called = false;
//attachInterrupt(digitalPinToInterrupt(rxpin), start_bit_falling_edge, FALLING);
Serial.println("ISR Attached again"); Serial.flush();
}
}
Note If I comment out the detachInterrupt it does not crash...
My current hookup has a jumper wire between pins 1 and 2, and it crashes when
I enter something into serial monitor. I commented out the simulated restart (2nd attach) as I thought it was the thing that was faulting.
The idea is. With the RX code for SoftwareSerial I am playing with. Is that we set an ISR
on the RX Pin, that when it receives the Start bit, the ISR is triggered, it turns off that ISR, and turns on a Timer Interrupt, where at each of these interrupts is samples the RX pin. When it receives the stop bit, it turns off the timer and turns back the pin interrupt.
Other side note: I was trying to see if there is an open issue on this, but I am not sure where these are located, my first guess was at:
arduino/mbed-os: Arm Mbed OS is a platform operating system designed for the internet of things (github.com)
But this does not show any issues. Maybe at:
ARMmbed/mbed-os: Arm Mbed OS is a platform operating system designed for the internet of things (github.com)
But would think that might be too generic a location? Also did not find any hits
for detachInterrupt and one closed in 2019 on attachInterrupt
Thanks