Skip to content

Commit

Permalink
Merge branch 'bugfix-2.0.x' into pr/23058
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Nov 4, 2021
2 parents 66bc1d2 + 399faa9 commit 53124e3
Show file tree
Hide file tree
Showing 92 changed files with 2,799 additions and 854 deletions.
101 changes: 53 additions & 48 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@
#define LIN_ADVANCE_K 0.22 // Unit: mm compression per 1mm/s extruder speed
//#define LA_DEBUG // If enabled, this will generate debug information output over USB.
//#define EXPERIMENTAL_SCURVE // Enable this option to permit S-Curve Acceleration
//#define ALLOW_LOW_EJERK // Allow a DEFAULT_EJERK value of <10. Recommended for direct drive hotends.
#endif

// @section leveling
Expand Down Expand Up @@ -1988,65 +1989,69 @@

/**
* Thermal Probe Compensation
* Probe measurements are adjusted to compensate for temperature distortion.
* Use G76 to calibrate this feature. Use M871 to set values manually.
* For a more detailed explanation of the process see G76_M871.cpp.
*
* Adjust probe measurements to compensate for distortion associated with the temperature
* of the probe, bed, and/or hotend.
* Use G76 to automatically calibrate this feature for probe and bed temperatures.
* (Extruder temperature/offset values must be calibrated manually.)
* Use M871 to set temperature/offset values manually.
* For more details see https://marlinfw.org/docs/features/probe_temp_compensation.html
*/
#if HAS_BED_PROBE && TEMP_SENSOR_PROBE && TEMP_SENSOR_BED
// Enable thermal first layer compensation using bed and probe temperatures
#define PROBE_TEMP_COMPENSATION
//#define PTC_PROBE // Compensate based on probe temperature
//#define PTC_BED // Compensate based on bed temperature
//#define PTC_HOTEND // Compensate based on hotend temperature

#if ANY(PTC_PROBE, PTC_BED, PTC_HOTEND)
/**
* If the probe is outside the defined range, use linear extrapolation with the closest
* point and the point with index PTC_LINEAR_EXTRAPOLATION. e.g., If set to 4 it will use the
* linear extrapolation between data[0] and data[4] for values below PTC_PROBE_START.
*/
//#define PTC_LINEAR_EXTRAPOLATION 4

#if ENABLED(PTC_PROBE)
// Probe temperature calibration generates a table of values starting at PTC_PROBE_START
// (e.g., 30), in steps of PTC_PROBE_RES (e.g., 5) with PTC_PROBE_COUNT (e.g., 10) samples.
#define PTC_PROBE_START 30 // (°C)
#define PTC_PROBE_RES 5 // (°C)
#define PTC_PROBE_COUNT 10
#define PTC_PROBE_ZOFFS { 0 } // (µm) Z adjustments per sample
#endif

#if ENABLED(PTC_BED)
// Bed temperature calibration builds a similar table.
#define PTC_BED_START 60 // (°C)
#define PTC_BED_RES 5 // (°C)
#define PTC_BED_COUNT 10
#define PTC_BED_ZOFFS { 0 } // (µm) Z adjustments per sample
#endif

#if ENABLED(PTC_HOTEND)
// Note: There is no automatic calibration for the hotend. Use M871.
#define PTC_HOTEND_START 180 // (°C)
#define PTC_HOTEND_RES 5 // (°C)
#define PTC_HOTEND_COUNT 20
#define PTC_HOTEND_ZOFFS { 0 } // (µm) Z adjustments per sample
#endif

// Add additional compensation depending on hotend temperature
// Note: this values cannot be calibrated and have to be set manually
#if ENABLED(PROBE_TEMP_COMPENSATION)
// G76 options
#if BOTH(PTC_PROBE, PTC_BED)
// Park position to wait for probe cooldown
#define PTC_PARK_POS { 0, 0, 100 }

// Probe position to probe and wait for probe to reach target temperature
//#define PTC_PROBE_POS { 12.0f, 7.3f } // Example: MK52 magnetic heatbed
#define PTC_PROBE_POS { 90, 100 }

// Enable additional compensation using hotend temperature
// Note: this values cannot be calibrated automatically but have to be set manually via M871.
//#define USE_TEMP_EXT_COMPENSATION

// Probe temperature calibration generates a table of values starting at PTC_SAMPLE_START
// (e.g., 30), in steps of PTC_SAMPLE_RES (e.g., 5) with PTC_SAMPLE_COUNT (e.g., 10) samples.

//#define PTC_SAMPLE_START 30 // (°C)
//#define PTC_SAMPLE_RES 5 // (°C)
//#define PTC_SAMPLE_COUNT 10

// Bed temperature calibration builds a similar table.

//#define BTC_SAMPLE_START 60 // (°C)
//#define BTC_SAMPLE_RES 5 // (°C)
//#define BTC_SAMPLE_COUNT 10

#if ENABLED(USE_TEMP_EXT_COMPENSATION)
//#define ETC_SAMPLE_START 180 // (°C)
//#define ETC_SAMPLE_RES 5 // (°C)
//#define ETC_SAMPLE_COUNT 20
#endif

// The temperature the probe should be at while taking measurements during bed temperature
// calibration.
//#define BTC_PROBE_TEMP 30 // (°C)
// The temperature the probe should be at while taking measurements during
// bed temperature calibration.
#define PTC_PROBE_TEMP 30 // (°C)

// Height above Z=0.0 to raise the nozzle. Lowering this can help the probe to heat faster.
// Note: the Z=0.0 offset is determined by the probe offset which can be set using M851.
//#define PTC_PROBE_HEATING_OFFSET 0.5

// Height to raise the Z-probe between heating and taking the next measurement. Some probes
// may fail to untrigger if they have been triggered for a long time, which can be solved by
// increasing the height the probe is raised to.
//#define PTC_PROBE_RAISE 15

// If the probe is outside of the defined range, use linear extrapolation using the closest
// point and the PTC_LINEAR_EXTRAPOLATION'th next point. E.g. if set to 4 it will use data[0]
// and data[4] to perform linear extrapolation for values below PTC_SAMPLE_START.
//#define PTC_LINEAR_EXTRAPOLATION 4
// Note: The Z=0.0 offset is determined by the probe Z offset (e.g., as set with M851 Z).
#define PTC_PROBE_HEATING_OFFSET 0.5
#endif
#endif
#endif // PTC_PROBE || PTC_BED || PTC_HOTEND

// @section extras

Expand Down
2 changes: 1 addition & 1 deletion Marlin/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* here we define this default string as the date where the latest release
* version was tagged.
*/
//#define STRING_DISTRIBUTION_DATE "2021-11-01"
//#define STRING_DISTRIBUTION_DATE "2021-11-04"

/**
* Defines a generic printer name to be output to the LCD after booting Marlin.
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/AVR/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ void set_pwm_frequency(const pin_t pin, int f_desired);

/**
* set_pwm_duty
* Sets the PWM duty cycle of the provided pin to the provided value
* Set the PWM duty cycle of the provided pin to the provided value
* Optionally allows inverting the duty cycle [default = false]
* Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
*/
Expand Down
62 changes: 30 additions & 32 deletions Marlin/src/HAL/AVR/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
#ifdef __AVR__

#include "../../inc/MarlinConfigPre.h"
#include "HAL.h"

#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM

#include "HAL.h"

struct Timer {
volatile uint8_t* TCCRnQ[3]; // max 3 TCCR registers per timer
volatile uint16_t* OCRnQ[3]; // max 3 OCR registers per timer
Expand Down Expand Up @@ -153,7 +152,7 @@ Timer get_pwm_timer(const pin_t pin) {

void set_pwm_frequency(const pin_t pin, int f_desired) {
Timer timer = get_pwm_timer(pin);
if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
uint16_t size;
if (timer.n == 2) size = 255; else size = 65535;

Expand Down Expand Up @@ -243,40 +242,39 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
_SET_ICRn(timer.ICRn, res); // Set ICRn value (TOP) = res
}

#endif // NEEDS_HARDWARE_PWM

void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
// If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
// Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
if (v == 0)
digitalWrite(pin, invert);
else if (v == v_size)
digitalWrite(pin, !invert);
else {
Timer timer = get_pwm_timer(pin);
if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
// Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
_SET_COMnQ(timer.TCCRnQ, (timer.q
#ifdef TCCR2
+ (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
#endif
), COM_CLEAR_SET + invert
);
#if NEEDS_HARDWARE_PWM

uint16_t top;
if (timer.n == 2) { // if TIMER2
top = (
#if ENABLED(USE_OCR2A_AS_TOP)
*timer.OCRnQ[0] // top = OCR2A
#else
255 // top = 0xFF (max)
#endif
// If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
// Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
if (v == 0)
digitalWrite(pin, invert);
else if (v == v_size)
digitalWrite(pin, !invert);
else {
Timer timer = get_pwm_timer(pin);
if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
// Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
_SET_COMnQ(timer.TCCRnQ, (timer.q
#ifdef TCCR2
+ (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
#endif
), COM_CLEAR_SET + invert
);

uint16_t top = (timer.n == 2) ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
_SET_OCRnQ(timer.OCRnQ, timer.q, (v * top + v_size / 2) / v_size); // Scale 8/16-bit v to top value
}
else
top = *timer.ICRn; // top = ICRn

_SET_OCRnQ(timer.OCRnQ, timer.q, v * float(top) / float(v_size)); // Scale 8/16-bit v to top value
}
#else

analogWrite(pin, v);
UNUSED(v_size);
UNUSED(invert);

#endif
}

#endif // NEEDS_HARDWARE_PWM
#endif // __AVR__
5 changes: 5 additions & 0 deletions Marlin/src/HAL/DUE/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ inline void HAL_adc_init() {}//todo
void HAL_adc_start_conversion(const uint8_t ch);
uint16_t HAL_adc_get_result();

//
// PWM
//
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }

//
// Pin Map
//
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/HAL/ESP32/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ void HAL_adc_init();

void HAL_adc_start_conversion(const uint8_t adc_pin);

// PWM
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }

// Pin Map
#define GET_PIN_MAP_PIN(index) index
#define GET_PIN_MAP_INDEX(pin) pin
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/HAL/LINUX/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ void HAL_adc_enable_channel(const uint8_t ch);
void HAL_adc_start_conversion(const uint8_t ch);
uint16_t HAL_adc_get_result();

// PWM
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }

// Reset source
inline void HAL_clear_reset_source(void) {}
inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
Expand Down
16 changes: 8 additions & 8 deletions Marlin/src/HAL/LPC1768/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@
#ifdef TARGET_LPC1768

#include "../../inc/MarlinConfigPre.h"

#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM

#include <pwm.h>

void set_pwm_frequency(const pin_t pin, int f_desired) {
LPC176x::pwm_set_frequency(pin, f_desired);
}

void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
LPC176x::pwm_write_ratio(pin, invert ? 1.0f - (float)v / v_size : (float)v / v_size);
}

#endif // NEEDS_HARDWARE_PWM
#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM

void set_pwm_frequency(const pin_t pin, int f_desired) {
LPC176x::pwm_set_frequency(pin, f_desired);
}

#endif

#endif // TARGET_LPC1768
3 changes: 3 additions & 0 deletions Marlin/src/HAL/NATIVE_SIM/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ void HAL_adc_enable_channel(const uint8_t ch);
void HAL_adc_start_conversion(const uint8_t ch);
uint16_t HAL_adc_get_result();

// PWM
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }

// Reset source
inline void HAL_clear_reset_source(void) {}
inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; }
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/HAL/SAMD51/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ void HAL_adc_init();

void HAL_adc_start_conversion(const uint8_t adc_pin);

//
// PWM
//
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }

//
// Pin Map
//
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void HAL_adc_start_conversion(const uint8_t adc_pin) { HAL_adc_result = analogRe
uint16_t HAL_adc_get_result() { return HAL_adc_result; }

// Reset the system to initiate a firmware flash
void flashFirmware(const int16_t) { HAL_reboot(); }
WEAK void flashFirmware(const int16_t) { HAL_reboot(); }

// Maple Compatibility
volatile uint32_t systick_uptime_millis = 0;
Expand Down
37 changes: 18 additions & 19 deletions Marlin/src/HAL/STM32/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,9 @@

#ifdef HAL_STM32

#include "../../inc/MarlinConfigPre.h"

#if NEEDS_HARDWARE_PWM

#include "HAL.h"
#include "../../inc/MarlinConfig.h"
#include "timers.h"

void set_pwm_frequency(const pin_t pin, int f_desired) {
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer

PinName pin_name = digitalPinToPinName(pin);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance

LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
return;

pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
}

void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
PinName pin_name = digitalPinToPinName(pin);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
Expand All @@ -58,5 +41,21 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
}
}

#endif // NEEDS_HARDWARE_PWM
#if NEEDS_HARDWARE_PWM

void set_pwm_frequency(const pin_t pin, int f_desired) {
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer

PinName pin_name = digitalPinToPinName(pin);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance

LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
return;

pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
}

#endif

#endif // HAL_STM32
3 changes: 1 addition & 2 deletions Marlin/src/HAL/STM32F1/HAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ uint16_t analogRead(pin_t pin) {

// Wrapper to maple unprotected analogWrite
void analogWrite(pin_t pin, int pwm_val8) {
if (PWM_PIN(pin))
analogWrite(uint8_t(pin), pwm_val8);
if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8);
}

void HAL_reboot() { nvic_sys_reset(); }
Expand Down
Loading

0 comments on commit 53124e3

Please sign in to comment.