Skip to content

Commit

Permalink
track init as a debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 7, 2022
1 parent eaef837 commit 8cd296f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Marlin/src/HAL/AVR/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
#include "../../inc/MarlinConfigPre.h"
#include "HAL.h"

static uint16_t timer_freq[5];
//#define DEBUG_PWM_INIT

#if ENABLED(DEBUG_PWM_INIT)
#include "../../core/serial.h"
static uint16_t timer_freq[5];
#endif

struct Timer {
volatile uint8_t* TCCRnQ[3]; // max 3 TCCR registers per timer
Expand Down Expand Up @@ -153,9 +158,10 @@ 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;

TERN_(DEBUG_PWM_INIT, timer_freq[timer.n - 1] = f_desired);

const uint16_t size = (timer.n == 2) ? 255 : 65535;

uint16_t res = 255; // resolution (TOP value)
uint8_t j = 0; // prescaler index
Expand Down Expand Up @@ -242,10 +248,12 @@ 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;
}
#if ENABLED(DEBUG_PWM_INIT)
if (timer_freq[timer.n - 1] == 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.
SERIAL_ECHOLNPGM("PWM Timer ", timer.n, " Frequency Not Initialized!");
}
#endif
_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

0 comments on commit 8cd296f

Please sign in to comment.