From 7b21811a9a3f3f411c438bf1bc7093c0f61376eb Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:51:48 -0700 Subject: [PATCH 1/7] Flags for matching auto-fans --- Marlin/Conditionals_post.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Marlin/Conditionals_post.h b/Marlin/Conditionals_post.h index de2e6e2af9..67f522516b 100644 --- a/Marlin/Conditionals_post.h +++ b/Marlin/Conditionals_post.h @@ -440,6 +440,12 @@ #define HAS_AUTO_FAN_1 (PIN_EXISTS(EXTRUDER_1_AUTO_FAN)) #define HAS_AUTO_FAN_2 (PIN_EXISTS(EXTRUDER_2_AUTO_FAN)) #define HAS_AUTO_FAN_3 (PIN_EXISTS(EXTRUDER_3_AUTO_FAN)) + #define AUTO_1_IS_0 (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) + #define AUTO_2_IS_0 (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) + #define AUTO_2_IS_1 (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN) + #define AUTO_3_IS_0 (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN) + #define AUTO_3_IS_1 (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN) + #define AUTO_3_IS_2 (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN) #define HAS_AUTO_FAN (HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3) #define HAS_FAN0 (PIN_EXISTS(FAN)) #define HAS_FAN1 (PIN_EXISTS(FAN1) && CONTROLLERFAN_PIN != FAN1_PIN && EXTRUDER_0_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_1_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_2_AUTO_FAN_PIN != FAN1_PIN) From 7272ec5608441c13c457663eb6d4c114c77e4ffa Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:52:10 -0700 Subject: [PATCH 2/7] Update HAS_FAN flags for 4 auto fans --- Marlin/Conditionals_post.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Marlin/Conditionals_post.h b/Marlin/Conditionals_post.h index 67f522516b..6e0f10f1c4 100644 --- a/Marlin/Conditionals_post.h +++ b/Marlin/Conditionals_post.h @@ -448,8 +448,8 @@ #define AUTO_3_IS_2 (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN) #define HAS_AUTO_FAN (HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3) #define HAS_FAN0 (PIN_EXISTS(FAN)) - #define HAS_FAN1 (PIN_EXISTS(FAN1) && CONTROLLERFAN_PIN != FAN1_PIN && EXTRUDER_0_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_1_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_2_AUTO_FAN_PIN != FAN1_PIN) - #define HAS_FAN2 (PIN_EXISTS(FAN2) && CONTROLLERFAN_PIN != FAN2_PIN && EXTRUDER_0_AUTO_FAN_PIN != FAN2_PIN && EXTRUDER_1_AUTO_FAN_PIN != FAN2_PIN && EXTRUDER_2_AUTO_FAN_PIN != FAN2_PIN) + #define HAS_FAN1 (PIN_EXISTS(FAN1) && CONTROLLERFAN_PIN != FAN1_PIN && EXTRUDER_0_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_1_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_2_AUTO_FAN_PIN != FAN1_PIN && EXTRUDER_3_AUTO_FAN_PIN != FAN1_PIN) + #define HAS_FAN2 (PIN_EXISTS(FAN2) && CONTROLLERFAN_PIN != FAN2_PIN && EXTRUDER_0_AUTO_FAN_PIN != FAN2_PIN && EXTRUDER_1_AUTO_FAN_PIN != FAN2_PIN && EXTRUDER_2_AUTO_FAN_PIN != FAN2_PIN && EXTRUDER_3_AUTO_FAN_PIN != FAN2_PIN) #define HAS_CONTROLLERFAN (PIN_EXISTS(CONTROLLERFAN)) #define HAS_SERVOS (defined(NUM_SERVOS) && NUM_SERVOS > 0) #define HAS_SERVO_0 (PIN_EXISTS(SERVO0)) From e88575c8ec6a740b0e3bde7f0ba3b132e3f51cbb Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:52:24 -0700 Subject: [PATCH 3/7] Init next_auto_fan_check_ms to zero --- Marlin/temperature.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 4cb3ed98ca..a81237f1d4 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -185,7 +185,7 @@ int Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , #endif #if HAS_AUTO_FAN - millis_t Temperature::next_auto_fan_check_ms; + millis_t Temperature::next_auto_fan_check_ms = 0; #endif unsigned char Temperature::soft_pwm[HOTENDS]; From abf9696db119d906d78973e4fdb833db96bb8979 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:55:11 -0700 Subject: [PATCH 4/7] Loop fan-pins based on array size --- Marlin/temperature.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index a81237f1d4..96b4d42a4b 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -479,7 +479,7 @@ int Temperature::getHeaterPower(int heater) { SBI(fanState, fanBit[e]); } uint8_t fanDone = 0; - for (int8_t f = 0; f <= 3; f++) { + for (int8_t f = 0; f < COUNT(fanPin); f++) { int8_t pin = fanPin[f]; if (pin >= 0 && !TEST(fanDone, fanBit[f])) { unsigned char newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0; From 3475fd7b0fc1da787368c2a40b58815180fdd637 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:57:33 -0700 Subject: [PATCH 5/7] Init PWM-able auto fan pins with `SET_OUTPUT` --- Marlin/temperature.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 96b4d42a4b..4c4c0bb4cd 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1068,16 +1068,44 @@ void Temperature::init() { #endif #if HAS_AUTO_FAN_0 - pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT); + #if EXTRUDER_0_AUTO_FAN_PIN == FAN1_PIN + SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(EXTRUDER_0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 + #endif + #else + pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT); + #endif #endif #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) - pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT); + #if EXTRUDER_1_AUTO_FAN_PIN == FAN1_PIN + SET_OUTPUT(EXTRUDER_1_AUTO_FAN_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(EXTRUDER_1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 + #endif + #else + pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT); + #endif #endif #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) - pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT); + #if EXTRUDER_2_AUTO_FAN_PIN == FAN1_PIN + SET_OUTPUT(EXTRUDER_2_AUTO_FAN_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(EXTRUDER_2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 + #endif + #else + pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT); + #endif #endif #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN) - pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT); + #if EXTRUDER_3_AUTO_FAN_PIN == FAN1_PIN + SET_OUTPUT(EXTRUDER_3_AUTO_FAN_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(EXTRUDER_3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 + #endif + #else + pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT); + #endif #endif // Use timer0 for temperature measurement From c63003ed3e7f9abb8241efe92716009a3296ee28 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 19:57:58 -0700 Subject: [PATCH 6/7] Use matching auto-fan flags --- Marlin/temperature.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 4c4c0bb4cd..7fb11d8538 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -465,19 +465,19 @@ int Temperature::getHeaterPower(int heater) { void Temperature::checkExtruderAutoFans() { const int8_t fanPin[] = { EXTRUDER_0_AUTO_FAN_PIN, EXTRUDER_1_AUTO_FAN_PIN, EXTRUDER_2_AUTO_FAN_PIN, EXTRUDER_3_AUTO_FAN_PIN }; - const int fanBit[] = { 0, - EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : 1, - EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : - EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 : 2, - EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : - EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 : - EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN ? 2 : 3 + const int fanBit[] = { + 0, + AUTO_1_IS_0 ? 0 : 1, + AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2, + AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3 }; uint8_t fanState = 0; + HOTEND_LOOP() { if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE) SBI(fanState, fanBit[e]); } + uint8_t fanDone = 0; for (int8_t f = 0; f < COUNT(fanPin); f++) { int8_t pin = fanPin[f]; @@ -1077,7 +1077,7 @@ void Temperature::init() { pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT); #endif #endif - #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) + #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0 #if EXTRUDER_1_AUTO_FAN_PIN == FAN1_PIN SET_OUTPUT(EXTRUDER_1_AUTO_FAN_PIN); #if ENABLED(FAST_PWM_FAN) @@ -1087,7 +1087,7 @@ void Temperature::init() { pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT); #endif #endif - #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) + #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1 #if EXTRUDER_2_AUTO_FAN_PIN == FAN1_PIN SET_OUTPUT(EXTRUDER_2_AUTO_FAN_PIN); #if ENABLED(FAST_PWM_FAN) @@ -1097,7 +1097,7 @@ void Temperature::init() { pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT); #endif #endif - #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN) + #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2 #if EXTRUDER_3_AUTO_FAN_PIN == FAN1_PIN SET_OUTPUT(EXTRUDER_3_AUTO_FAN_PIN); #if ENABLED(FAST_PWM_FAN) From 3a0a94b21ad9d6a00c936077a9436992a9b6e268 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 13 Aug 2016 20:16:13 -0700 Subject: [PATCH 7/7] Always init configured fan pins --- Marlin/temperature.cpp | 52 +++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 7fb11d8538..78168c2e96 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -987,39 +987,35 @@ void Temperature::init() { SET_OUTPUT(HEATER_BED_PIN); #endif - #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM) - - #if HAS_FAN0 - SET_OUTPUT(FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 - #endif - #if ENABLED(FAN_SOFT_PWM) - soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2; - #endif + #if HAS_FAN0 + SET_OUTPUT(FAN_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 #endif - - #if HAS_FAN1 - SET_OUTPUT(FAN1_PIN); - #if ENABLED(FAST_PWM_FAN) - setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 - #endif - #if ENABLED(FAN_SOFT_PWM) - soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2; - #endif + #if ENABLED(FAN_SOFT_PWM) + soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2; #endif + #endif - #if HAS_FAN2 - SET_OUTPUT(FAN2_PIN); - #if ENABLED(FAST_PWM_FAN) - setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 - #endif - #if ENABLED(FAN_SOFT_PWM) - soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2; - #endif + #if HAS_FAN1 + SET_OUTPUT(FAN1_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 #endif + #if ENABLED(FAN_SOFT_PWM) + soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2; + #endif + #endif - #endif // FAST_PWM_FAN || FAN_SOFT_PWM + #if HAS_FAN2 + SET_OUTPUT(FAN2_PIN); + #if ENABLED(FAST_PWM_FAN) + setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8 + #endif + #if ENABLED(FAN_SOFT_PWM) + soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2; + #endif + #endif #if ENABLED(HEATER_0_USES_MAX6675)