Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AVR set_pwm_duty unset default frequency #23463

Merged
merged 33 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
15b243c
Fix AVR set_pwm_duty unset default frequency
descipher Jan 6, 2022
e363120
Set MOTOR_CURRENT_PWM_FREQUENCY default as 31400
descipher Jan 6, 2022
0ab6dcb
Merge 'bugfix-2.0.x' into bf2_weird_pwm_PR_23463
thinkyhead Jan 7, 2022
df4f122
defer to pinsformat.js
thinkyhead Jan 7, 2022
359415d
delete red highlighted trailing spaces
thinkyhead Jan 7, 2022
fe1f75d
clarify
thinkyhead Jan 7, 2022
eaef837
trust pins to retain freq after init ?
thinkyhead Jan 7, 2022
8cd296f
track init as a debug option
thinkyhead Jan 7, 2022
ffd9750
arg tweak, keep wrapper ?
thinkyhead Jan 7, 2022
dbd37f2
set_pwm_frequency with all HAL_CAN_SET_PWM_FREQ
thinkyhead Jan 7, 2022
8e10265
Add HAS_LCD_BRIGHTNESS Needs PWM condition check
descipher Jan 7, 2022
1953124
Corrected include and frequency init tracking
descipher Jan 7, 2022
ea74cdc
Cleanup condition check state
descipher Jan 8, 2022
8b334fd
Update fast_pwm.cpp
thinkyhead Jan 9, 2022
741078e
would other platforms ever use it?
thinkyhead Jan 9, 2022
e18008f
tweak wgm
thinkyhead Jan 9, 2022
aaa15d6
Init AVR timers 2,3,4,5 in HAL
descipher Jan 10, 2022
5b5b23b
Merge branch 'ultimate.pwm.fix' of https://github.com/descipher/Marli…
descipher Jan 10, 2022
4b01add
Add AT90 pins
descipher Jan 10, 2022
c1e1b75
kHz => KHz
thinkyhead Jan 10, 2022
e177429
unsigned PWM frequency
thinkyhead Jan 10, 2022
7ffc23a
32 bits may be better
thinkyhead Jan 10, 2022
092666e
fix formatting
thinkyhead Jan 10, 2022
1755e78
suppress warning
thinkyhead Jan 10, 2022
14795aa
Add more pins
descipher Jan 10, 2022
f95f606
unsigned enums
thinkyhead Jan 11, 2022
54501eb
move macros, tweak get_pwm_timer
thinkyhead Jan 11, 2022
78cb32f
tweak var names and maths
thinkyhead Jan 11, 2022
5aeac05
Merge branch 'bugfix-2.0.x' into pr/23463
thinkyhead Jan 11, 2022
90ba936
Timer check default return as non-PWM and not protected.
descipher Jan 11, 2022
0871ea3
return timer structs more directly
thinkyhead Jan 11, 2022
0f5fbe7
fallback to WGM2_PWM_PC
thinkyhead Jan 11, 2022
6a5eaec
style tweaks
thinkyhead Jan 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Marlin/src/HAL/AVR/HAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ inline void HAL_adc_init() {
#define strtof strtod

#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
#define PWM_FREQUENCY 1000 // Default PWM frequency when set_pwm_duty() is called without set_pwm_frequency()

/**
* set_pwm_frequency
Expand Down
9 changes: 6 additions & 3 deletions Marlin/src/HAL/AVR/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "../../inc/MarlinConfigPre.h"
#include "HAL.h"

#if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
static uint16_t timer_freq[5];

struct Timer {
volatile uint8_t* TCCRnQ[3]; // max 3 TCCR registers per timer
Expand Down Expand Up @@ -153,6 +153,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 recognized
timer_freq[timer.n] = f_desired;
uint16_t size;
if (timer.n == 2) size = 255; else size = 65535;

Expand Down Expand Up @@ -228,8 +229,6 @@ 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 NEEDS_HARDWARE_PWM

Expand All @@ -243,6 +242,10 @@ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255
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)
if (timer_freq[timer.n] == 0) { // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
set_pwm_frequency(pin, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
timer_freq[timer.n] = PWM_FREQUENCY;
}
thinkyhead marked this conversation as resolved.
Show resolved Hide resolved
_SET_COMnQ(timer.TCCRnQ, timer.q TERN_(HAS_TCCR2, + (timer.q == 2)), COM_CLEAR_SET + invert); // COM20 is on bit 4 of TCCR2, so +1 for q==2
const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
_SET_OCRnQ(timer.OCRnQ, timer.q, uint16_t(uint32_t(v) * top / v_size)); // Scale 8/16-bit v to top value
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@
#endif

// Add features that need hardware PWM here
#if ANY(FAST_PWM_FAN, SPINDLE_LASER_USE_PWM)
#if ANY(FAST_PWM_FAN, SPINDLE_LASER_USE_PWM, MOTOR_CURRENT_PWM_FEATURE)
#define NEEDS_HARDWARE_PWM 1
#endif

Expand Down
17 changes: 12 additions & 5 deletions Marlin/src/module/stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3258,31 +3258,43 @@ void Stepper::report_positions() {
#elif HAS_MOTOR_CURRENT_PWM

#define _WRITE_CURRENT_PWM(P) set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
#ifdef __SAM3X8E__
#define _WRITE_FREQUENCY_PWM(P) NOOP
#else
#define _WRITE_FREQUENCY_PWM(P) set_pwm_frequency(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), MOTOR_CURRENT_PWM_FREQUENCY)
#endif
switch (driver) {
case 0:
#if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
_WRITE_FREQUENCY_PWM(X);
_WRITE_CURRENT_PWM(X);
#endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
_WRITE_FREQUENCY_PWM(Y);
_WRITE_CURRENT_PWM(Y);
#endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_XY)
_WRITE_FREQUENCY_PWM(XY);
_WRITE_CURRENT_PWM(XY);
#endif
break;
case 1:
#if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
_WRITE_FREQUENCY_PWM(Z);
_WRITE_CURRENT_PWM(Z);
#endif
break;
case 2:
#if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
_WRITE_FREQUENCY_PWM(E);
_WRITE_CURRENT_PWM(E);
#endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_E0)
_WRITE_FREQUENCY_PWM(E0);
_WRITE_CURRENT_PWM(E0);
#endif
#if PIN_EXISTS(MOTOR_CURRENT_PWM_E1)
_WRITE_FREQUENCY_PWM(E1);
_WRITE_CURRENT_PWM(E1);
#endif
break;
Expand Down Expand Up @@ -3325,11 +3337,6 @@ void Stepper::report_positions() {
#endif

refresh_motor_power();

// Set Timer5 to 31khz so the PWM of the motor power is as constant as possible. (removes a buzzing noise)
#ifdef __AVR__
SET_CS5(PRESCALER_1);
#endif
#endif
}

Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/module/stepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ class Stepper {
#ifndef PWM_MOTOR_CURRENT
#define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
#endif
#ifndef MOTOR_CURRENT_PWM_FREQUENCY
#define MOTOR_CURRENT_PWM_FREQUENCY 31400
#endif

#define MOTOR_CURRENT_COUNT LINEAR_AXES
#elif HAS_MOTOR_CURRENT_SPI
static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
Expand Down