From f89b375fb9ca9730a4165ac3bf9f0f9dc100a338 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 13 Mar 2019 06:51:15 -0500 Subject: [PATCH] Fixes and improvements for PWM pins (#13383) --- Marlin/src/HAL/HAL_AVR/fastio_AVR.h | 54 +++---- Marlin/src/HAL/HAL_DUE/fastio_Due.h | 7 +- Marlin/src/HAL/HAL_ESP32/fastio_ESP32.h | 31 ++-- Marlin/src/HAL/HAL_LINUX/fastio.h | 8 +- Marlin/src/HAL/HAL_LPC1768/fastio.h | 4 + Marlin/src/HAL/HAL_STM32/fastio_STM32.h | 7 +- Marlin/src/HAL/HAL_STM32F1/fastio_STM32F1.h | 7 +- Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h | 7 +- Marlin/src/HAL/HAL_STM32F7/fastio_STM32F7.h | 7 +- .../src/HAL/HAL_TEENSY31_32/fastio_Teensy.h | 8 +- .../src/HAL/HAL_TEENSY35_36/fastio_Teensy.h | 8 +- Marlin/src/Marlin.cpp | 6 +- Marlin/src/feature/caselight.cpp | 4 +- Marlin/src/feature/controllerfan.cpp | 2 +- Marlin/src/feature/leds/leds.cpp | 22 ++- Marlin/src/gcode/control/M3-M5.cpp | 4 +- Marlin/src/gcode/control/M42.cpp | 2 +- Marlin/src/gcode/feature/caselight/M355.cpp | 2 +- Marlin/src/gcode/host/M115.cpp | 2 +- Marlin/src/inc/Conditionals_post.h | 21 --- Marlin/src/inc/SanityCheck.h | 8 +- Marlin/src/lcd/menu/menu_configuration.cpp | 2 +- Marlin/src/module/endstops.cpp | 2 +- Marlin/src/module/planner.cpp | 10 +- Marlin/src/module/stepper.cpp | 16 +- Marlin/src/module/temperature.cpp | 138 ++++++++---------- 26 files changed, 196 insertions(+), 193 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/fastio_AVR.h b/Marlin/src/HAL/HAL_AVR/fastio_AVR.h index c7e331b691..846c4bec82 100644 --- a/Marlin/src/HAL/HAL_AVR/fastio_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/fastio_AVR.h @@ -94,6 +94,8 @@ #define extDigitalRead(IO) digitalRead(IO) #endif +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + #define READ(IO) _READ(IO) #define WRITE(IO,V) _WRITE(IO,V) #define TOGGLE(IO) _TOGGLE(IO) @@ -102,6 +104,8 @@ #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _WRITE(IO, HIGH); }while(0) #define SET_OUTPUT(IO) _SET_OUTPUT(IO) +#define SET_PWM(IO) SET_OUTPUT(IO) + #define GET_INPUT(IO) _GET_INPUT(IO) #define GET_OUTPUT(IO) _GET_OUTPUT(IO) #define GET_TIMER(IO) _GET_TIMER(IO) @@ -279,75 +283,75 @@ enum ClockSource2 : char { // Determine which harware PWMs are already in use #if PIN_EXISTS(CONTROLLER_FAN) - #define PWM_CHK_FAN_B(p) (p == CONTROLLER_FAN_PIN || p == E0_AUTO_FAN_PIN || p == E1_AUTO_FAN_PIN || p == E2_AUTO_FAN_PIN || p == E3_AUTO_FAN_PIN || p == E4_AUTO_FAN_PIN || p == E5_AUTO_FAN_PIN || p == CHAMBER_AUTO_FAN_PIN) + #define PWM_CHK_FAN_B(P) (P == CONTROLLER_FAN_PIN || P == E0_AUTO_FAN_PIN || P == E1_AUTO_FAN_PIN || P == E2_AUTO_FAN_PIN || P == E3_AUTO_FAN_PIN || P == E4_AUTO_FAN_PIN || P == E5_AUTO_FAN_PIN || P == CHAMBER_AUTO_FAN_PIN) #else - #define PWM_CHK_FAN_B(p) (p == E0_AUTO_FAN_PIN || p == E1_AUTO_FAN_PIN || p == E2_AUTO_FAN_PIN || p == E3_AUTO_FAN_PIN || p == E4_AUTO_FAN_PIN || p == E5_AUTO_FAN_PIN || p == CHAMBER_AUTO_FAN_PIN) + #define PWM_CHK_FAN_B(P) (P == E0_AUTO_FAN_PIN || P == E1_AUTO_FAN_PIN || P == E2_AUTO_FAN_PIN || P == E3_AUTO_FAN_PIN || P == E4_AUTO_FAN_PIN || P == E5_AUTO_FAN_PIN || P == CHAMBER_AUTO_FAN_PIN) #endif #if PIN_EXISTS(FAN) || PIN_EXISTS(FAN1) || PIN_EXISTS(FAN2) #if PIN_EXISTS(FAN2) - #define PWM_CHK_FAN_A(p) (p == FAN_PIN || p == FAN1_PIN || p == FAN2_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN_PIN || P == FAN1_PIN || P == FAN2_PIN) #elif PIN_EXISTS(FAN1) - #define PWM_CHK_FAN_A(p) (p == FAN_PIN || p == FAN1_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN_PIN || P == FAN1_PIN) #else - #define PWM_CHK_FAN_A(p) (p == FAN_PIN) + #define PWM_CHK_FAN_A(P) (P == FAN_PIN) #endif #else - #define PWM_CHK_FAN_A(p) false + #define PWM_CHK_FAN_A(P) false #endif #if HAS_MOTOR_CURRENT_PWM #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY) - #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E || p == MOTOR_CURRENT_PWM_Z || p == MOTOR_CURRENT_PWM_XY) + #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E || P == MOTOR_CURRENT_PWM_Z || P == MOTOR_CURRENT_PWM_XY) #elif PIN_EXISTS(MOTOR_CURRENT_PWM_Z) - #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E || p == MOTOR_CURRENT_PWM_Z) + #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E || P == MOTOR_CURRENT_PWM_Z) #else - #define PWM_CHK_MOTOR_CURRENT(p) (p == MOTOR_CURRENT_PWM_E) + #define PWM_CHK_MOTOR_CURRENT(P) (P == MOTOR_CURRENT_PWM_E) #endif #else - #define PWM_CHK_MOTOR_CURRENT(p) false + #define PWM_CHK_MOTOR_CURRENT(P) false #endif #ifdef NUM_SERVOS #if AVR_ATmega2560_FAMILY - #define PWM_CHK_SERVO(p) (p == 5 || (NUM_SERVOS > 12 && p == 6) || (NUM_SERVOS > 24 && p == 46)) // PWMS 3A, 4A & 5A + #define PWM_CHK_SERVO(P) (P == 5 || (NUM_SERVOS > 12 && P == 6) || (NUM_SERVOS > 24 && P == 46)) // PWMS 3A, 4A & 5A #elif AVR_ATmega2561_FAMILY - #define PWM_CHK_SERVO(p) (p == 5) // PWM3A + #define PWM_CHK_SERVO(P) (P == 5) // PWM3A #elif AVR_ATmega1284_FAMILY - #define PWM_CHK_SERVO(p) false + #define PWM_CHK_SERVO(P) false #elif AVR_AT90USB1286_FAMILY - #define PWM_CHK_SERVO(p) (p == 16) // PWM3A + #define PWM_CHK_SERVO(P) (P == 16) // PWM3A #elif AVR_ATmega328_FAMILY - #define PWM_CHK_SERVO(p) false + #define PWM_CHK_SERVO(P) false #endif #else - #define PWM_CHK_SERVO(p) false + #define PWM_CHK_SERVO(P) false #endif #if ENABLED(BARICUDA) #if HAS_HEATER_1 && HAS_HEATER_2 - #define PWM_CHK_HEATER(p) (p == HEATER_1_PIN || p == HEATER_2_PIN) + #define PWM_CHK_HEATER(P) (P == HEATER_1_PIN || P == HEATER_2_PIN) #elif HAS_HEATER_1 - #define PWM_CHK_HEATER(p) (p == HEATER_1_PIN) + #define PWM_CHK_HEATER(P) (P == HEATER_1_PIN) #endif #else - #define PWM_CHK_HEATER(p) false + #define PWM_CHK_HEATER(P) false #endif -#define PWM_CHK(p) (PWM_CHK_HEATER(p) || PWM_CHK_SERVO(p) || PWM_CHK_MOTOR_CURRENT(p) || PWM_CHK_FAN_A(p) || PWM_CHK_FAN_B(p)) +#define PWM_CHK(P) (PWM_CHK_HEATER(P) || PWM_CHK_SERVO(P) || PWM_CHK_MOTOR_CURRENT(P) || PWM_CHK_FAN_A(P) || PWM_CHK_FAN_B(P)) // define which hardware PWMs are available for the current CPU // all timer 1 PWMS deleted from this list because they are never available #if AVR_ATmega2560_FAMILY - #define PWM_PIN(p) ((p >= 2 && p <= 10) || p == 13 || p == 44 || p == 45 || p == 46) + #define PWM_PIN(P) ((P >= 2 && P <= 10) || P == 13 || P == 44 || P == 45 || P == 46) #elif AVR_ATmega2561_FAMILY - #define PWM_PIN(p) ((p >= 2 && p <= 6) || p == 9) + #define PWM_PIN(P) ((P >= 2 && P <= 6) || P == 9) #elif AVR_ATmega1284_FAMILY - #define PWM_PIN(p) (p == 3 || p == 4 || p == 14 || p == 15) + #define PWM_PIN(P) (P == 3 || P == 4 || P == 14 || P == 15) #elif AVR_AT90USB1286_FAMILY - #define PWM_PIN(p) (p == 0 || p == 1 || p == 14 || p == 15 || p == 16 || p == 24) + #define PWM_PIN(P) (P == 0 || P == 1 || P == 14 || P == 15 || P == 16 || P == 24) #elif AVR_ATmega328_FAMILY - #define PWM_PIN(p) (p == 3 || p == 5 || p == 6 || p == 11) + #define PWM_PIN(P) (P == 3 || P == 5 || P == 6 || P == 11) #else #error "unknown CPU" #endif diff --git a/Marlin/src/HAL/HAL_DUE/fastio_Due.h b/Marlin/src/HAL/HAL_DUE/fastio_Due.h index 8ca3fe2fb7..a5b1a10fab 100644 --- a/Marlin/src/HAL/HAL_DUE/fastio_Due.h +++ b/Marlin/src/HAL/HAL_DUE/fastio_Due.h @@ -45,7 +45,8 @@ // Due has 12 PWMs assigned to logical pins 2-13. // 6, 7, 8 & 9 come from the PWM controller. The others come from the timers. -#define USEABLE_HARDWARE_PWM(p) WITHIN(p, 2, 13) +#define PWM_PIN(P) WITHIN(P, 2, 13) +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) #ifndef MASK #define MASK(PIN) (1 << PIN) @@ -172,6 +173,8 @@ #define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0) // Set pin as output (wrapper) - reads the pin and sets the output to that value #define SET_OUTPUT(IO) _SET_OUTPUT(IO) +// Set pin as PWM +#define SET_PWM(IO) SET_OUTPUT(IO) // Check if pin is an input #define GET_INPUT(IO) ((digitalPinToPort(IO)->PIO_OSR & digitalPinToBitMask(IO)) == 0) @@ -187,6 +190,8 @@ #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + /** * Ports and functions * Added as necessary or if I feel like it- not a comprehensive list! diff --git a/Marlin/src/HAL/HAL_ESP32/fastio_ESP32.h b/Marlin/src/HAL/HAL_ESP32/fastio_ESP32.h index cb2fa7760a..42646e9b8b 100644 --- a/Marlin/src/HAL/HAL_ESP32/fastio_ESP32.h +++ b/Marlin/src/HAL/HAL_ESP32/fastio_ESP32.h @@ -28,34 +28,43 @@ */ // Set pin as input -#define _SET_INPUT(IO) pinMode(IO, INPUT) +#define _SET_INPUT(IO) pinMode(IO, INPUT) // Set pin as output -#define _SET_OUTPUT(IO) pinMode(IO, OUTPUT) +#define _SET_OUTPUT(IO) pinMode(IO, OUTPUT) // Set pin as input with pullup mode -#define _PULLUP(IO, v) pinMode(IO, v ? INPUT_PULLUP : INPUT) +#define _PULLUP(IO, v) pinMode(IO, v ? INPUT_PULLUP : INPUT) // Read a pin wrapper -#define READ(IO) digitalRead(IO) +#define READ(IO) digitalRead(IO) // Write to a pin wrapper -#define WRITE(IO, v) (TEST(IO, 7) ? i2s_write(IO & 0x7F, v) : digitalWrite(IO, v)) +#define WRITE(IO, v) (TEST(IO, 7) ? i2s_write(IO & 0x7F, v) : digitalWrite(IO, v)) // Set pin as input wrapper -#define SET_INPUT(IO) _SET_INPUT(IO) +#define SET_INPUT(IO) _SET_INPUT(IO) // Set pin as input with pullup wrapper -#define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0) +#define SET_INPUT_PULLUP(IO) do{ _SET_INPUT(IO); _PULLUP(IO, HIGH); }while(0) // Set pin as output wrapper -#define SET_OUTPUT(IO) do{ _SET_OUTPUT(IO); WRITE(IO, LOW); }while(0) +#define SET_OUTPUT(IO) do{ _SET_OUTPUT(IO); }while(0) -#define OUT_WRITE(IO,V) do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0) +// Set pin as PWM +#define SET_PWM(IO) SET_OUTPUT(IO) + +// Set pin as output and init +#define OUT_WRITE(IO,V) do{ _SET_OUTPUT(IO); WRITE(IO,V); }while(0) // digitalRead/Write wrappers -#define extDigitalRead(IO) digitalRead(IO) -#define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define extDigitalRead(IO) digitalRead(IO) +#define extDigitalWrite(IO,V) digitalWrite(IO,V) + +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + +#define PWM_PIN(P) true +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) // // Ports and functions diff --git a/Marlin/src/HAL/HAL_LINUX/fastio.h b/Marlin/src/HAL/HAL_LINUX/fastio.h index 44a3d18198..dd4aecff29 100644 --- a/Marlin/src/HAL/HAL_LINUX/fastio.h +++ b/Marlin/src/HAL/HAL_LINUX/fastio.h @@ -28,8 +28,6 @@ #include #include -#define USEABLE_HARDWARE_PWM(pin) false - #define SET_DIR_INPUT(IO) Gpio::setDir(IO, 1) #define SET_DIR_OUTPUT(IO) Gpio::setDir(IO, 0) @@ -110,6 +108,8 @@ #define SET_INPUT_PULLDOWN(IO) do{ _SET_INPUT(IO); _PULLDOWN(IO, HIGH); }while(0) /// set pin as output wrapper - reads the pin and sets the output to that value #define SET_OUTPUT(IO) do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0) +// set pin as PWM +#define SET_PWM(IO) SET_OUTPUT(IO) /// check if pin is an input wrapper #define GET_INPUT(IO) _GET_INPUT(IO) @@ -125,3 +125,7 @@ // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) + +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) diff --git a/Marlin/src/HAL/HAL_LPC1768/fastio.h b/Marlin/src/HAL/HAL_LPC1768/fastio.h index 5fbb1b8c1b..e731f1b32b 100644 --- a/Marlin/src/HAL/HAL_LPC1768/fastio.h +++ b/Marlin/src/HAL/HAL_LPC1768/fastio.h @@ -111,6 +111,8 @@ #define SET_INPUT_PULLDOWN(IO) do{ _SET_INPUT(IO); _PULLDOWN(IO, HIGH); }while(0) /// set pin as output wrapper - reads the pin and sets the output to that value #define SET_OUTPUT(IO) do{ _WRITE(IO, _READ(IO)); _SET_OUTPUT(IO); }while(0) +// set pin as PWM +#define SET_PWM(IO) SET_OUTPUT(IO) /// check if pin is an input wrapper #define GET_INPUT(IO) _GET_INPUT(IO) @@ -126,3 +128,5 @@ // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) + +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) diff --git a/Marlin/src/HAL/HAL_STM32/fastio_STM32.h b/Marlin/src/HAL/HAL_STM32/fastio_STM32.h index a648c4327c..8974a9ce0c 100644 --- a/Marlin/src/HAL/HAL_STM32/fastio_STM32.h +++ b/Marlin/src/HAL/HAL_STM32/fastio_STM32.h @@ -72,14 +72,17 @@ void FastIO_init(); // Must be called before using fast io macros #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, INPUT_PULLUP) /*!< Input with Pull-up activation */ #define SET_INPUT_PULLDOWN(IO) _SET_MODE(IO, INPUT_PULLDOWN) /*!< Input with Pull-down activation */ #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) +#define SET_PWM(IO) _SET_MODE(IO, PWM) #define GET_INPUT(IO) #define GET_OUTPUT(IO) #define GET_TIMER(IO) -#define PWM_PIN(p) digitalPinHasPWM(p) -#define USEABLE_HARDWARE_PWM(p) PWM_PIN(p) +#define PWM_PIN(P) digitalPinHasPWM(P) +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) + +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) diff --git a/Marlin/src/HAL/HAL_STM32F1/fastio_STM32F1.h b/Marlin/src/HAL/HAL_STM32F1/fastio_STM32F1.h index 9f6747c7a8..edc49aaac2 100644 --- a/Marlin/src/HAL/HAL_STM32F1/fastio_STM32F1.h +++ b/Marlin/src/HAL/HAL_STM32F1/fastio_STM32F1.h @@ -43,14 +43,17 @@ #define SET_INPUT(IO) _SET_MODE(IO, GPIO_INPUT_FLOATING) #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, GPIO_INPUT_PU) #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) +#define SET_PWM(IO) pinMode(IO, PWM) // do{ gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, GPIO_AF_OUTPUT_PP); timer_set_mode(PIN_MAP[pin].timer_device, PIN_MAP[pin].timer_channel, TIMER_PWM); }while(0) #define GET_INPUT(IO) (_GET_MODE(IO) == GPIO_INPUT_FLOATING || _GET_MODE(IO) == GPIO_INPUT_ANALOG || _GET_MODE(IO) == GPIO_INPUT_PU || _GET_MODE(IO) == GPIO_INPUT_PD) #define GET_OUTPUT(IO) (_GET_MODE(IO) == GPIO_OUTPUT_PP) #define GET_TIMER(IO) (PIN_MAP[IO].timer_device != NULL) -#define PWM_PIN(p) true -#define USEABLE_HARDWARE_PWM(p) PWM_PIN(p) +#define PWM_PIN(P) digitalPinHasPWM(P) +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) + +#define ANALOG_WRITE(IO,V) analogWrite(IO,(V)*65535/255) diff --git a/Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h b/Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h index 140ed9650c..8d5e82eada 100644 --- a/Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h +++ b/Marlin/src/HAL/HAL_STM32F4/fastio_STM32F4.h @@ -44,6 +44,7 @@ #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, INPUT_PULLUP) /*!< Input with Pull-up activation */ #define SET_INPUT_PULLDOWN(IO) _SET_MODE(IO, INPUT_PULLDOWN) /*!< Input with Pull-down activation */ #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) +#define SET_PWM(IO) pinMode(IO, PWM) #define TOGGLE(IO) OUT_WRITE(IO, !READ(IO)) @@ -51,13 +52,15 @@ #define GET_OUTPUT(IO) #define GET_TIMER(IO) -#define PWM_PIN(p) true -#define USEABLE_HARDWARE_PWM(p) PWM_PIN(p) +#define PWM_PIN(P) true +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define ANALOG_WRITE(IO,V) analogWrite(IO,(V)*65535/255) + // // Pins Definitions // diff --git a/Marlin/src/HAL/HAL_STM32F7/fastio_STM32F7.h b/Marlin/src/HAL/HAL_STM32F7/fastio_STM32F7.h index 3f365d02eb..f835358901 100644 --- a/Marlin/src/HAL/HAL_STM32F7/fastio_STM32F7.h +++ b/Marlin/src/HAL/HAL_STM32F7/fastio_STM32F7.h @@ -43,6 +43,7 @@ #define SET_INPUT_PULLUP(IO) _SET_MODE(IO, INPUT_PULLUP) /*!< Input with Pull-up activation */ #define SET_INPUT_PULLDOWN(IO) _SET_MODE(IO, INPUT_PULLDOWN) /*!< Input with Pull-down activation */ #define SET_OUTPUT(IO) OUT_WRITE(IO, LOW) +#define SET_PWM(IO) _SET_MODE(IO, PWM) #define TOGGLE(IO) OUT_WRITE(IO, !READ(IO)) @@ -50,13 +51,15 @@ #define GET_OUTPUT(IO) #define GET_TIMER(IO) -#define PWM_PIN(p) true -#define USEABLE_HARDWARE_PWM(p) PWM_PIN(p) +#define PWM_PIN(P) true +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) // digitalRead/Write wrappers #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define ANALOG_WRITE(IO,V) analogWrite(IO,(V)*65535/255) + // // Pins Definitions // diff --git a/Marlin/src/HAL/HAL_TEENSY31_32/fastio_Teensy.h b/Marlin/src/HAL/HAL_TEENSY31_32/fastio_Teensy.h index 3f43151e33..7573094551 100644 --- a/Marlin/src/HAL/HAL_TEENSY31_32/fastio_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY31_32/fastio_Teensy.h @@ -43,7 +43,7 @@ * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html */ -#define _READ(p) bool(CORE_PIN ## p ## _PINREG & CORE_PIN ## p ## _BITMASK) +#define _READ(P) bool(CORE_PIN ## P ## _PINREG & CORE_PIN ## P ## _BITMASK) #define _WRITE(P,V) do{ \ if (V) CORE_PIN ## P ## _PORTSET = CORE_PIN ## P ## _BITMASK; \ @@ -79,6 +79,7 @@ #define SET_INPUT(IO) _SET_INPUT(IO) #define SET_INPUT_PULLUP(IO) _SET_INPUT_PULLUP(IO) #define SET_OUTPUT(IO) _SET_OUTPUT(IO) +#define SET_PWM(IO) SET_OUTPUT(IO) #define GET_INPUT(IO) _GET_INPUT(IO) #define GET_OUTPUT(IO) _GET_OUTPUT(IO) @@ -89,6 +90,11 @@ #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + +#define PWM_PIN(P) digitalPinHasPWM(P) +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) + /** * Ports, functions, and pins */ diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/fastio_Teensy.h b/Marlin/src/HAL/HAL_TEENSY35_36/fastio_Teensy.h index ac724ab26f..98f98bf3bf 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/fastio_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/fastio_Teensy.h @@ -42,7 +42,7 @@ * Why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html */ -#define _READ(p) bool(CORE_PIN ## p ## _PINREG & CORE_PIN ## p ## _BITMASK) +#define _READ(P) bool(CORE_PIN ## P ## _PINREG & CORE_PIN ## P ## _BITMASK) #define _WRITE(P,V) do{ \ if (V) CORE_PIN ## P ## _PORTSET = CORE_PIN ## P ## _BITMASK; \ @@ -78,6 +78,7 @@ #define SET_INPUT(IO) _SET_INPUT(IO) #define SET_INPUT_PULLUP(IO) _SET_INPUT_PULLUP(IO) #define SET_OUTPUT(IO) _SET_OUTPUT(IO) +#define SET_PWM(IO) SET_OUTPUT(IO) #define GET_INPUT(IO) _GET_INPUT(IO) #define GET_OUTPUT(IO) _GET_OUTPUT(IO) @@ -88,6 +89,11 @@ #define extDigitalRead(IO) digitalRead(IO) #define extDigitalWrite(IO,V) digitalWrite(IO,V) +#define ANALOG_WRITE(IO,V) analogWrite(IO,V) + +#define PWM_PIN(P) digitalPinHasPWM(P) +#define USEABLE_HARDWARE_PWM(P) PWM_PIN(P) + /** * Ports, functions, and pins */ diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index badda0fd23..174210687f 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -975,8 +975,8 @@ void setup() { OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3) #endif #if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0 - SET_OUTPUT(SPINDLE_LASER_PWM_PIN); - analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed + SET_PWM(SPINDLE_LASER_PWM_PIN); + ANALOG_WRITE(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed #endif #endif @@ -1022,7 +1022,7 @@ void setup() { #if HAS_CASE_LIGHT #if DISABLED(CASE_LIGHT_USE_NEOPIXEL) - SET_OUTPUT(CASE_LIGHT_PIN); + if (PWM_PIN(CASE_LIGHT_PIN)) SET_PWM(CASE_LIGHT_PIN); else SET_OUTPUT(CASE_LIGHT_PIN); #endif update_case_light(); #endif diff --git a/Marlin/src/feature/caselight.cpp b/Marlin/src/feature/caselight.cpp index ba662360b2..dc32d67814 100644 --- a/Marlin/src/feature/caselight.cpp +++ b/Marlin/src/feature/caselight.cpp @@ -69,8 +69,8 @@ void update_case_light() { #else // !CASE_LIGHT_USE_NEOPIXEL - if (USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) - analogWrite(CASE_LIGHT_PIN, n10ct); + if (PWM_PIN(CASE_LIGHT_PIN)) + ANALOG_WRITE(CASE_LIGHT_PIN, n10ct); else { const bool s = case_light_on ? !INVERT_CASE_LIGHT : INVERT_CASE_LIGHT; WRITE(CASE_LIGHT_PIN, s ? HIGH : LOW); diff --git a/Marlin/src/feature/controllerfan.cpp b/Marlin/src/feature/controllerfan.cpp index 81af957873..e1f3bfcdbb 100644 --- a/Marlin/src/feature/controllerfan.cpp +++ b/Marlin/src/feature/controllerfan.cpp @@ -81,7 +81,7 @@ void controllerfan_update() { // allows digital or PWM fan output to be used (see M42 handling) WRITE(CONTROLLER_FAN_PIN, speed); - analogWrite(CONTROLLER_FAN_PIN, speed); + ANALOG_WRITE(CONTROLLER_FAN_PIN, speed); } } diff --git a/Marlin/src/feature/leds/leds.cpp b/Marlin/src/feature/leds/leds.cpp index f2b1a965ab..26ced7df4f 100644 --- a/Marlin/src/feature/leds/leds.cpp +++ b/Marlin/src/feature/leds/leds.cpp @@ -61,11 +61,11 @@ LEDLights leds; void LEDLights::setup() { #if ENABLED(RGB_LED) || ENABLED(RGBW_LED) - SET_OUTPUT(RGB_LED_R_PIN); - SET_OUTPUT(RGB_LED_G_PIN); - SET_OUTPUT(RGB_LED_B_PIN); + if (PWM_PIN(RGB_LED_R_PIN)) SET_PWM(RGB_LED_R_PIN); else SET_OUTPUT(RGB_LED_R_PIN); + if (PWM_PIN(RGB_LED_G_PIN)) SET_PWM(RGB_LED_G_PIN); else SET_OUTPUT(RGB_LED_G_PIN); + if (PWM_PIN(RGB_LED_B_PIN)) SET_PWM(RGB_LED_B_PIN); else SET_OUTPUT(RGB_LED_B_PIN); #if ENABLED(RGBW_LED) - SET_OUTPUT(RGB_LED_W_PIN); + if (PWM_PIN(RGB_LED_W_PIN)) SET_PWM(RGB_LED_W_PIN); else SET_OUTPUT(RGB_LED_W_PIN); #endif #endif #if ENABLED(NEOPIXEL_LED) @@ -112,16 +112,12 @@ void LEDLights::set_color(const LEDColor &incol // This variant uses 3-4 separate pins for the RGB(W) components. // If the pins can do PWM then their intensity will be set. - WRITE(RGB_LED_R_PIN, incol.r ? HIGH : LOW); - WRITE(RGB_LED_G_PIN, incol.g ? HIGH : LOW); - WRITE(RGB_LED_B_PIN, incol.b ? HIGH : LOW); - analogWrite(RGB_LED_R_PIN, incol.r); - analogWrite(RGB_LED_G_PIN, incol.g); - analogWrite(RGB_LED_B_PIN, incol.b); - + #define UPDATE_RGBW(C,c) do{ if (PWM_PIN(RGB_LED_##C##_PIN)) ANALOG_WRITE(RGB_LED_##C##_PIN, incol.r); else WRITE(RGB_LED_##C##_PIN, incol.c ? HIGH : LOW); }while(0) + UPDATE_RGBW(R,r); + UPDATE_RGBW(G,g); + UPDATE_RGBW(B,b); #if ENABLED(RGBW_LED) - WRITE(RGB_LED_W_PIN, incol.w ? HIGH : LOW); - analogWrite(RGB_LED_W_PIN, incol.w); + UPDATE_RGBW(W,w); #endif #endif diff --git a/Marlin/src/gcode/control/M3-M5.cpp b/Marlin/src/gcode/control/M3-M5.cpp index 39405e1cbe..c133cd2802 100644 --- a/Marlin/src/gcode/control/M3-M5.cpp +++ b/Marlin/src/gcode/control/M3-M5.cpp @@ -74,7 +74,7 @@ inline void delay_for_power_down() { safe_delay(SPINDLE_LASER_POWERDOWN_DELAY); inline void set_spindle_laser_ocr(const uint8_t ocr) { WRITE(SPINDLE_LASER_ENABLE_PIN, SPINDLE_LASER_ENABLE_INVERT); // turn spindle on (active low) - analogWrite(SPINDLE_LASER_PWM_PIN, (SPINDLE_LASER_PWM_INVERT) ? 255 - ocr : ocr); + ANALOG_WRITE(SPINDLE_LASER_PWM_PIN, (SPINDLE_LASER_PWM_INVERT) ? 255 - ocr : ocr); } #if ENABLED(SPINDLE_LASER_PWM) @@ -82,7 +82,7 @@ inline void set_spindle_laser_ocr(const uint8_t ocr) { void update_spindle_laser_power() { if (spindle_laser_power == 0) { WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // turn spindle off (active low) - analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // only write low byte + ANALOG_WRITE(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // only write low byte delay_for_power_down(); } else { // Convert RPM to PWM duty cycle diff --git a/Marlin/src/gcode/control/M42.cpp b/Marlin/src/gcode/control/M42.cpp index bafc40762e..2db8f2cac8 100644 --- a/Marlin/src/gcode/control/M42.cpp +++ b/Marlin/src/gcode/control/M42.cpp @@ -51,7 +51,7 @@ void GcodeSuite::M42() { pinMode(pin, OUTPUT); extDigitalWrite(pin, pin_status); - analogWrite(pin, pin_status); + ANALOG_WRITE(pin, pin_status); #if FAN_COUNT > 0 switch (pin) { diff --git a/Marlin/src/gcode/feature/caselight/M355.cpp b/Marlin/src/gcode/feature/caselight/M355.cpp index 324a50f6ee..d3bc2c8399 100644 --- a/Marlin/src/gcode/feature/caselight/M355.cpp +++ b/Marlin/src/gcode/feature/caselight/M355.cpp @@ -57,7 +57,7 @@ SERIAL_ECHOLNPGM("Case light: off"); } else { - if (!USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) SERIAL_ECHOLNPGM("Case light: on"); + if (!PWM_PIN(CASE_LIGHT_PIN)) SERIAL_ECHOLNPGM("Case light: on"); else SERIAL_ECHOLNPAIR("Case light: ", case_light_brightness); } } diff --git a/Marlin/src/gcode/host/M115.cpp b/Marlin/src/gcode/host/M115.cpp index 952fef93db..715652f163 100644 --- a/Marlin/src/gcode/host/M115.cpp +++ b/Marlin/src/gcode/host/M115.cpp @@ -125,7 +125,7 @@ void GcodeSuite::M115() { ); cap_line(PSTR("CASE_LIGHT_BRIGHTNESS") #if HAS_CASE_LIGHT - , USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN) + , PWM_PIN(CASE_LIGHT_PIN) #endif ); diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index f0ddefb74f..204c141d87 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -958,27 +958,6 @@ #define HAS_AUTO_FAN_5 (HOTENDS > 5 && PIN_EXISTS(E5_AUTO_FAN)) #define HAS_AUTO_CHAMBER_FAN (PIN_EXISTS(CHAMBER_AUTO_FAN)) #define HAS_AUTO_FAN (HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3 || HAS_AUTO_FAN_4 || HAS_AUTO_FAN_5 || HAS_AUTO_CHAMBER_FAN) -#define AUTO_1_IS_0 (E1_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_2_IS_0 (E2_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_2_IS_1 (E2_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) -#define AUTO_3_IS_0 (E3_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_3_IS_1 (E3_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) -#define AUTO_3_IS_2 (E3_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) -#define AUTO_4_IS_0 (E4_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_4_IS_1 (E4_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) -#define AUTO_4_IS_2 (E4_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) -#define AUTO_4_IS_3 (E4_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) -#define AUTO_5_IS_0 (E5_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_5_IS_1 (E5_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) -#define AUTO_5_IS_2 (E5_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) -#define AUTO_5_IS_3 (E5_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) -#define AUTO_5_IS_4 (E5_AUTO_FAN_PIN == E4_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_0 (CHAMBER_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_1 (CHAMBER_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_2 (CHAMBER_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_3 (CHAMBER_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_4 (CHAMBER_AUTO_FAN_PIN == E4_AUTO_FAN_PIN) -#define AUTO_CHAMBER_IS_5 (CHAMBER_AUTO_FAN_PIN == E5_AUTO_FAN_PIN) // Other fans #define HAS_FAN0 (PIN_EXISTS(FAN)) diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index fee81aa636..f6a3070f53 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -1700,13 +1700,13 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS #if HAS_AUTO_FAN && EXTRUDER_AUTO_FAN_SPEED != 255 #define AF_ERR_SUFF "_AUTO_FAN_PIN is not a PWM pin. Set EXTRUDER_AUTO_FAN_SPEED to 255." #if HAS_AUTO_FAN_0 - static_assert(GET_TIMER(E0_AUTO_FAN_PIN), "E0" AF_ERR_SUFF); + static_assert(PWM_PIN(E0_AUTO_FAN_PIN), "E0" AF_ERR_SUFF); #elif HAS_AUTO_FAN_1 - static_assert(GET_TIMER(E1_AUTO_FAN_PIN), "E1" AF_ERR_SUFF); + static_assert(PWM_PIN(E1_AUTO_FAN_PIN), "E1" AF_ERR_SUFF); #elif HAS_AUTO_FAN_2 - static_assert(GET_TIMER(E2_AUTO_FAN_PIN), "E2" AF_ERR_SUFF); + static_assert(PWM_PIN(E2_AUTO_FAN_PIN), "E2" AF_ERR_SUFF); #elif HAS_AUTO_FAN_3 - static_assert(GET_TIMER(E3_AUTO_FAN_PIN), "E3" AF_ERR_SUFF); + static_assert(PWM_PIN(E3_AUTO_FAN_PIN), "E3" AF_ERR_SUFF); #endif #endif diff --git a/Marlin/src/lcd/menu/menu_configuration.cpp b/Marlin/src/lcd/menu/menu_configuration.cpp index 0b353b354c..dd19f9d7cb 100644 --- a/Marlin/src/lcd/menu/menu_configuration.cpp +++ b/Marlin/src/lcd/menu/menu_configuration.cpp @@ -299,7 +299,7 @@ void menu_configuration() { // Set Case light on/off/brightness // #if ENABLED(MENU_ITEM_CASE_LIGHT) - if (USEABLE_HARDWARE_PWM(CASE_LIGHT_PIN)) + if (PWM_PIN(CASE_LIGHT_PIN)) MENU_ITEM(submenu, MSG_CASE_LIGHT, menu_case_light); else MENU_ITEM_EDIT_CALLBACK(bool, MSG_CASE_LIGHT, (bool*)&case_light_on, update_case_light); diff --git a/Marlin/src/module/endstops.cpp b/Marlin/src/module/endstops.cpp index c5185f1834..fe076b6d38 100644 --- a/Marlin/src/module/endstops.cpp +++ b/Marlin/src/module/endstops.cpp @@ -886,7 +886,7 @@ void Endstops::update() { ES_REPORT_CHANGE(Z3_MAX); #endif SERIAL_ECHOLNPGM("\n"); - analogWrite(LED_PIN, local_LED_status); + ANALOG_WRITE(LED_PIN, local_LED_status); local_LED_status ^= 255; old_live_state_local = live_state_local; } diff --git a/Marlin/src/module/planner.cpp b/Marlin/src/module/planner.cpp index fbe6ca7e1a..e5234a5b35 100644 --- a/Marlin/src/module/planner.cpp +++ b/Marlin/src/module/planner.cpp @@ -1290,13 +1290,13 @@ void Planner::check_axes_activity() { #else #if HAS_FAN0 - analogWrite(FAN_PIN, CALC_FAN_SPEED(0)); + ANALOG_WRITE(FAN_PIN, CALC_FAN_SPEED(0)); #endif #if HAS_FAN1 - analogWrite(FAN1_PIN, CALC_FAN_SPEED(1)); + ANALOG_WRITE(FAN1_PIN, CALC_FAN_SPEED(1)); #endif #if HAS_FAN2 - analogWrite(FAN2_PIN, CALC_FAN_SPEED(2)); + ANALOG_WRITE(FAN2_PIN, CALC_FAN_SPEED(2)); #endif #endif @@ -1308,10 +1308,10 @@ void Planner::check_axes_activity() { #if ENABLED(BARICUDA) #if HAS_HEATER_1 - analogWrite(HEATER_1_PIN, tail_valve_pressure); + ANALOG_WRITE(HEATER_1_PIN, tail_valve_pressure); #endif #if HAS_HEATER_2 - analogWrite(HEATER_2_PIN, tail_e_to_p_pressure); + ANALOG_WRITE(HEATER_2_PIN, tail_e_to_p_pressure); #endif #endif } diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index a5b86631f9..be91e7d808 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -2510,7 +2510,7 @@ void Stepper::report_positions() { if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) motor_current_setting[driver] = current; // update motor_current_setting - #define _WRITE_CURRENT_PWM(P) analogWrite(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)) + #define _WRITE_CURRENT_PWM(P) ANALOG_WRITE(MOTOR_CURRENT_PWM_## P ##_PIN, 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { case 0: #if PIN_EXISTS(MOTOR_CURRENT_PWM_X) @@ -2560,25 +2560,25 @@ void Stepper::report_positions() { #elif HAS_MOTOR_CURRENT_PWM #if PIN_EXISTS(MOTOR_CURRENT_PWM_X) - SET_OUTPUT(MOTOR_CURRENT_PWM_X_PIN); + SET_PWM(MOTOR_CURRENT_PWM_X_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y) - SET_OUTPUT(MOTOR_CURRENT_PWM_Y_PIN); + SET_PWM(MOTOR_CURRENT_PWM_Y_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_XY) - SET_OUTPUT(MOTOR_CURRENT_PWM_XY_PIN); + SET_PWM(MOTOR_CURRENT_PWM_XY_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z) - SET_OUTPUT(MOTOR_CURRENT_PWM_Z_PIN); + SET_PWM(MOTOR_CURRENT_PWM_Z_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_E) - SET_OUTPUT(MOTOR_CURRENT_PWM_E_PIN); + SET_PWM(MOTOR_CURRENT_PWM_E_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_E0) - SET_OUTPUT(MOTOR_CURRENT_PWM_E0_PIN); + SET_PWM(MOTOR_CURRENT_PWM_E0_PIN); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_E1) - SET_OUTPUT(MOTOR_CURRENT_PWM_E1_PIN); + SET_PWM(MOTOR_CURRENT_PWM_E1_PIN); #endif refresh_motor_power(); diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index 4718ae60cc..53171db22d 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -610,6 +610,28 @@ int Temperature::getHeaterPower(const int heater) { #if HAS_AUTO_FAN + #define AUTO_1_IS_0 (E1_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_2_IS_0 (E2_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_2_IS_1 (E2_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_3_IS_0 (E3_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_3_IS_1 (E3_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_3_IS_2 (E3_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_4_IS_0 (E4_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_4_IS_1 (E4_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_4_IS_2 (E4_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_4_IS_3 (E4_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) + #define AUTO_5_IS_0 (E5_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_5_IS_1 (E5_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_5_IS_2 (E5_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_5_IS_3 (E5_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) + #define AUTO_5_IS_4 (E5_AUTO_FAN_PIN == E4_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_0 (CHAMBER_AUTO_FAN_PIN == E0_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_1 (CHAMBER_AUTO_FAN_PIN == E1_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_2 (CHAMBER_AUTO_FAN_PIN == E2_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_3 (CHAMBER_AUTO_FAN_PIN == E3_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_4 (CHAMBER_AUTO_FAN_PIN == E4_AUTO_FAN_PIN) + #define AUTO_CHAMBER_IS_5 (CHAMBER_AUTO_FAN_PIN == E5_AUTO_FAN_PIN) + void Temperature::checkExtruderAutoFans() { static const uint8_t fanBit[] PROGMEM = { 0, @@ -633,11 +655,11 @@ int Temperature::getHeaterPower(const int heater) { SBI(fanState, pgm_read_byte(&fanBit[6])); #endif - #define _UPDATE_AUTO_FAN(P,D,A) do{ \ - if (USEABLE_HARDWARE_PWM(P##_AUTO_FAN_PIN)) \ - analogWrite(P##_AUTO_FAN_PIN, A); \ - else \ - WRITE(P##_AUTO_FAN_PIN, D); \ + #define _UPDATE_AUTO_FAN(P,D,A) do{ \ + if (PWM_PIN(P##_AUTO_FAN_PIN) && EXTRUDER_AUTO_FAN_SPEED < 255) \ + ANALOG_WRITE(P##_AUTO_FAN_PIN, A); \ + else \ + WRITE(P##_AUTO_FAN_PIN, D); \ }while(0) uint8_t fanDone = 0; @@ -1280,6 +1302,25 @@ void Temperature::updateTemperaturesFromRawValues() { SPIclass max6675_spi; #endif +// Init fans according to whether they're native PWM or Software PWM +#define _INIT_SOFT_FAN(P) OUT_WRITE(P, FAN_INVERTING ? LOW : HIGH) +#if ENABLED(FAN_SOFT_PWM) + #define _INIT_FAN_PIN(P) _INIT_SOFT_FAN(P) +#else + #define _INIT_FAN_PIN(P) do{ if (PWM_PIN(P)) SET_PWM(P); else _INIT_SOFT_FAN(P); }while(0) +#endif +#if ENABLED(FAST_PWM_FAN) + #define SET_FAST_PWM_FREQ(P) set_pwm_frequency(P, FAST_PWM_FAN_FREQUENCY) +#else + #define SET_FAST_PWM_FREQ(P) NOOP +#endif +#define INIT_FAN_PIN(P) do{ _INIT_FAN_PIN(P); SET_FAST_PWM_FREQ(P); }while(0) +#if EXTRUDER_AUTO_FAN_SPEED != 255 + #define INIT_AUTO_FAN_PIN(P) do{ if (P == FAN1_PIN || P == FAN2_PIN) { SET_PWM(P); SET_FAST_PWM_FREQ(FAST_PWM_FAN_FREQUENCY); } else SET_OUTPUT(P); }while(0) +#else + #define INIT_AUTO_FAN_PIN(P) SET_OUTPUT(P) +#endif + /** * Initialize the temperature manager * The manager is implemented by periodic calls to manage_heater() @@ -1329,32 +1370,18 @@ void Temperature::init() { #if HAS_HEATED_CHAMBER OUT_WRITE(HEATER_CHAMBER_PIN, HEATER_CHAMBER_INVERTING); #endif + #if HAS_FAN0 - SET_OUTPUT(FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif + INIT_FAN_PIN(FAN_PIN); #endif - #if HAS_FAN1 - SET_OUTPUT(FAN1_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(FAN1_PIN, FAST_PWM_FAN_FREQUENCY); - #endif + INIT_FAN_PIN(FAN1_PIN); #endif - #if HAS_FAN2 - SET_OUTPUT(FAN2_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(FAN2_PIN, FAST_PWM_FAN_FREQUENCY); - #endif + INIT_FAN_PIN(FAN2_PIN); #endif - #if ENABLED(USE_CONTROLLER_FAN) - SET_OUTPUT(CONTROLLER_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(CONTROLLER_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif + INIT_FAN_PIN(CONTROLLER_FAN_PIN); #endif #if MAX6675_SEPARATE_SPI @@ -1408,74 +1435,25 @@ void Temperature::init() { ENABLE_TEMPERATURE_INTERRUPT(); #if HAS_AUTO_FAN_0 - #if E0_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E0_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E0_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E0_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E0_AUTO_FAN_PIN); #endif #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0 - #if E1_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E1_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E1_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E1_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E1_AUTO_FAN_PIN); #endif #if HAS_AUTO_FAN_2 && !(AUTO_2_IS_0 || AUTO_2_IS_1) - #if E2_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E2_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E2_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E2_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E2_AUTO_FAN_PIN); #endif #if HAS_AUTO_FAN_3 && !(AUTO_3_IS_0 || AUTO_3_IS_1 || AUTO_3_IS_2) - #if E3_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E3_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E3_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E3_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E3_AUTO_FAN_PIN); #endif #if HAS_AUTO_FAN_4 && !(AUTO_4_IS_0 || AUTO_4_IS_1 || AUTO_4_IS_2 || AUTO_4_IS_3) - #if E4_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E4_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E4_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E4_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E4_AUTO_FAN_PIN); #endif #if HAS_AUTO_FAN_5 && !(AUTO_5_IS_0 || AUTO_5_IS_1 || AUTO_5_IS_2 || AUTO_5_IS_3 || AUTO_5_IS_4) - #if E5_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(E5_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(E5_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(E5_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(E5_AUTO_FAN_PIN); #endif #if HAS_AUTO_CHAMBER_FAN && !(AUTO_CHAMBER_IS_0 || AUTO_CHAMBER_IS_1 || AUTO_CHAMBER_IS_2 || AUTO_CHAMBER_IS_3 || AUTO_CHAMBER_IS_4 || AUTO_CHAMBER_IS_5) - #if CHAMBER_AUTO_FAN_PIN == FAN1_PIN - SET_OUTPUT(CHAMBER_AUTO_FAN_PIN); - #if ENABLED(FAST_PWM_FAN) - set_pwm_frequency(CHAMBER_AUTO_FAN_PIN, FAST_PWM_FAN_FREQUENCY); - #endif - #else - SET_OUTPUT(CHAMBER_AUTO_FAN_PIN); - #endif + INIT_AUTO_FAN_PIN(CHAMBER_AUTO_FAN_PIN); #endif // Wait for temperature measurement to settle