From 82304a2268df12c7ecf2d689b89ef17574ea3ff1 Mon Sep 17 00:00:00 2001 From: bubnikv Date: Fri, 12 Jan 2018 21:16:29 +0100 Subject: [PATCH] Slight optimization of the fan speed interrupt routine by accessing the Arduino millis() without locking interrupts. --- Firmware/Marlin.h | 9 +++++++++ Firmware/Marlin_main.cpp | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index 11a3bc24..482bd51e 100644 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -242,6 +242,15 @@ void prepare_arc_move(char isclockwise); void clamp_to_software_endstops(float target[3]); void refresh_cmd_timeout(void); +// Timer counter, incremented by the 1ms Arduino timer. +// The standard Arduino timer() function returns this value atomically +// by disabling / enabling interrupts. This is costly, if the interrupts are known +// to be disabled. +extern volatile unsigned long timer0_millis; +// An unsynchronized equivalent to a standard Arduino millis() function. +// To be used inside an interrupt routine. +FORCE_INLINE unsigned long timer_nc() { return timer0_millis; } + #ifdef FAST_PWM_FAN void setPwmFrequency(uint8_t pin, int val); #endif diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index a606bbf5..86817cac 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -7443,10 +7443,10 @@ ISR(INT7_vect) { if (fanSpeed < MIN_PRINT_FAN_SPEED) return; if ((1 << 6) & EICRB) { //interrupt was triggered by rising edge - t_fan_rising_edge = millis(); + t_fan_rising_edge = millis_nc(); } else { //interrupt was triggered by falling edge - if ((millis() - t_fan_rising_edge) >= FAN_PULSE_WIDTH_LIMIT) {//this pulse was from sensor and not from pwm + if ((millis_nc() - t_fan_rising_edge) >= FAN_PULSE_WIDTH_LIMIT) {//this pulse was from sensor and not from pwm fan_edge_counter[1] += 2; //we are currently counting all edges so lets count two edges for one pulse } }