[Solved] Maximal number of external interrupts?

I am having weird problems with external interrupts. It all works great with 5 of them, but when I add 6th, core crashes (red blinking status LED). All pins are used to handle buttons without any debouncing (done in software for now). Code is quite complex already (a large library plus client code I am developing), but what works:

Interrupts are attached to pins D2, D3, D4, A0, A1, relevant, incomplete code:

int                 gaugeA              = D2;
int                 gaugeB              = D3;
// several more pins defined...
volatile int8_t     gaugeChange;

void setup() {
    pinMode(gaugeB,         INPUT_PULLUP);
    pinMode(gaugeA,         INPUT_PULLUP);
    // ...
    attachInterrupt(gaugeA,         isr_gaugeMinus,     RISING);
    attachInterrupt(gaugeB,         isr_gaugePlus,      RISING);
    // ...
}

void isr_gaugeMinus() {
    gaugeChange = -1;
}
void isr_gaugePlus() {
    gaugeChange = 1;
}

All ISRs look about the same as in example, main code just checks variables controlled by ISR and acts on them (and resets them to neutral value). And crash I am seeing seems not related to any my interrupts.

Core also communicares using UDP and dumps debug informations on Serial console. At the start, it waits for communication on Serial console, calling while(!Serial.available()) Spark.process(); in setup(). All works great, until I add 6th interrupt. Several variants of strange behaviour happen:

  1. Button on A6 - code crashes after a second or so after communication starts, without touching any buttons (no interrupts). Some UDP (send and receive 2-3 packets) and Serial communication happens, core locks up in same moment in the middle of receiving text over serial. Code processes UDP packets as they arrive, so it is hard to tell when exactly it crashes. Commenting out attachInterrupt() (keeping pin setup) makes code run as befrore.

  2. Button on A7 - no crashes, except interrupt is not working.

  3. Commented out other interrupt, button on A6 or A7 - no crashes, no interrupt

  4. Button on A7, polling it in addition to interrupt - no interrupt, but digitalRead() catches the value

Is there any limit on external interrupts? Datasheet says 16 interrupt vectors. I expect some interrupts are used for Spark itself (WiFi, buttons), but is should leave at least 6 interrupts free (although I would like to have 7 at least). Or any other ideas?

From @peekay123’s post which is sadly not accessible it looks like there are indeed 5 external interrupts. It may be possible to add additional ones. Perhaps @BDub or @mdma can chime in?

#define DMA1_CHANNEL5_IRQ_PRIORITY		0	//CC3000_SPI_TX_DMA Interrupt
#define EXTI15_10_IRQ_PRIORITY			1	//CC3000_WIFI_INT_EXTI & User Interrupt
#define USB_LP_IRQ_PRIORITY			2	//USB_LP_CAN1_RX0 Interrupt
#define RTCALARM_IRQ_PRIORITY			3	//RTC Alarm Interrupt
#define RTC_IRQ_PRIORITY			4	//RTC Seconds Interrupt
#define TIM1_CC_IRQ_PRIORITY			5	//TIM1_CC4 Interrupt
#define EXTI2_IRQ_PRIORITY			6	//BUTTON1_EXTI Interrupt
#define USART2_IRQ_PRIORITY			7	//USART2 Interrupt
#define EXTI0_IRQ_PRIORITY			11	//User Interrupt
#define EXTI1_IRQ_PRIORITY			11	//User Interrupt
#define EXTI3_IRQ_PRIORITY			11	//User Interrupt
#define EXTI4_IRQ_PRIORITY			11	//User Interrupt
#define EXTI9_5_IRQ_PRIORITY			12	//User Interrupt
#define SYSTICK_IRQ_PRIORITY			13	//CORTEX_M3 Systick Interrupt
#define SVCALL_IRQ_PRIORITY			14	//CORTEX_M3 SVCall Interrupt
#define PENDSV_IRQ_PRIORITY			15	//CORTEX_M3 PendSV Interrupt

Thanks, so it seems my guess was right. I can’t access the topic, but what you wrote is enough to get the problem. I will change design accordingly.

By the way, it might be good idea to add it to the documentation for attachInterrupt(), might save a headache to someone :smile:

Good point. Feel free to add an issue to https://github.com/spark/docs referencing this post!

Good idea, issue posted. Thanks!

1 Like

@harrisonhjones and @lami, I am working on a topic describing how interrupts, interrupt service routines and interrupt priorities are setup on the Spark. I will be tagging the firmware guys at Spark to comment on the issues you had. I have found nothing which limits the number of interrupts you can attach however, user interrupts share the same interrupt service code stub and this may be part of the problem. Stay tuned :smile:

2 Likes

Is there any update on the topic? I also checked on https://github.com/spark/docs/issues/253 but I didn’t find any additional information.
Thanks

As far as I understood on the Core the maximum number of external interrupts is 5.

I wonder what is the maximum number of external interrupts on the Photon.
(I’m not sure this is the right place to post this question, but since this thread discuss a specific issue I feel it would be good to have also information on Photon)