From 271ced73413ebf8d662cce2a582ac9b795104a8c Mon Sep 17 00:00:00 2001 From: Sebastianv650 Date: Sun, 12 Feb 2017 18:00:14 +0100 Subject: [PATCH] Prevent re-entering of temperature ISR If Marlin is inside the temperature ISR, the stepper ISR is enabled. If a stepper event is now happening Marlin will proceed with the stepper ISR. Now, at the end of the stepper ISR, the temperatre ISR gets enabled again. While Marlin proceed the rest of the temperature ISR, it's now vulnerable to a second ISR call. --- Marlin/temperature.cpp | 11 ++++++++++- Marlin/temperature.h | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 8120e0d6c5..d7462bc99d 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1483,8 +1483,15 @@ void Temperature::set_current_temp_raw() { */ ISR(TIMER0_COMPB_vect) { Temperature::isr(); } +volatile bool Temperature::in_temp_isr = false; + void Temperature::isr() { - //Allow UART and stepper ISRs + // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR + // at the end of its run, potentially causing re-entry. This flag prevents it. + if (in_temp_isr) return; + in_temp_isr = true; + + // Allow UART and stepper ISRs CBI(TIMSK0, OCIE0B); //Disable Temperature ISR sei(); @@ -1949,5 +1956,7 @@ void Temperature::isr() { } #endif + cli(); + in_temp_isr = false; SBI(TIMSK0, OCIE0B); //re-enable Temperature ISR } diff --git a/Marlin/temperature.h b/Marlin/temperature.h index 182efd5645..e592148447 100644 --- a/Marlin/temperature.h +++ b/Marlin/temperature.h @@ -61,6 +61,8 @@ class Temperature { current_temperature_bed_raw, target_temperature_bed; + static volatile bool in_temp_isr; + #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT) static float redundant_temperature; #endif