From ccb1f1722e81e3d1661f1b085f1a565bcc832920 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 20 Nov 2012 22:29:26 +1100 Subject: [PATCH] SITL: fixed a interrupt recursion bug this can be triggered by the timer scheduler re-enabling interrupts --- libraries/Desktop/support/sitl.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/Desktop/support/sitl.cpp b/libraries/Desktop/support/sitl.cpp index 363f7840ad..ae4bd11612 100644 --- a/libraries/Desktop/support/sitl.cpp +++ b/libraries/Desktop/support/sitl.cpp @@ -232,6 +232,7 @@ static void timer_handler(int signum) if (_interrupts_are_blocked()) { return; } + uint8_t oldSREG = SREG; cli(); #ifndef __CYGWIN__ @@ -255,9 +256,6 @@ static void timer_handler(int signum) /* check for packet from flight sim */ sitl_fdm_input(); - // trigger all timers - timer_scheduler.run(); - // trigger RC input if (isr_registry._registry[ISR_REGISTRY_TIMER4_CAPT]) { isr_registry._registry[ISR_REGISTRY_TIMER4_CAPT](); @@ -268,12 +266,12 @@ static void timer_handler(int signum) if (update_count == 0) { sitl_update_gps(0, 0, 0, 0, 0, false); - sei(); + SREG = oldSREG; return; } if (update_count == last_update_count) { - sei(); + SREG = oldSREG; return; } last_update_count = update_count; @@ -292,7 +290,11 @@ static void timer_handler(int signum) // so the ADC code doesn't get stuck ADCSRA &= ~_BV(ADSC); - sei(); + // trigger all APM timers. We do this last as it can re-enable + // interrupts, which can lead to recursion + timer_scheduler.run(); + + SREG = oldSREG; }