Skip to content

Commit

Permalink
squish get_pwm_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 11, 2022
1 parent cb2d476 commit 4514758
Showing 1 changed file with 14 additions and 68 deletions.
82 changes: 14 additions & 68 deletions Marlin/src/HAL/AVR/fast_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ struct Timer {
#define _SET_ICRn(ICRn, V) (*(ICRn) = int(V) & 0xFFFF)

/**
* get_pwm_timer
* Get the timer information and register of the provided pin.
* Return a Timer struct containing this information.
* Used by set_pwm_frequency, set_pwm_duty
* Return a Timer struct describing a pin's timer.
*/
Timer get_pwm_timer(const pin_t pin) {

uint8_t q = 0;

switch (digitalPinToTimer(pin)) {
// Protect reserved timers (TIMER0 & TIMER1)
#ifdef TCCR0A
IF_DISABLED(AVR_AT90USB1286_FAMILY, case TIMER0A:)
case TIMER0B:
Expand All @@ -71,90 +67,40 @@ Timer get_pwm_timer(const pin_t pin) {
case TIMER1A: case TIMER1B:
#endif

break;
break; // Protect reserved timers (TIMER0 & TIMER1)

#if HAS_TCCR2
case TIMER2:
return Timer({
{ &TCCR2, nullptr, nullptr },
{ (uint16_t*)&OCR2, nullptr, nullptr },
nullptr,
2, 0,
true, false
});
return Timer({ { &TCCR2, nullptr, nullptr }, { (uint16_t*)&OCR2, nullptr, nullptr }, nullptr, 2, 0, true, false });
#elif ENABLED(USE_OCR2A_AS_TOP)
case TIMER2A: break; // protect TIMER2A since its OCR is used by TIMER2B
case TIMER2B:
return Timer({ { &TCCR2A, &TCCR2B, nullptr }, { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr }, nullptr, 2, 1, true, false });
#elif defined(TCCR2A)
#if ENABLED(USE_OCR2A_AS_TOP)
case TIMER2A: break; // protect TIMER2A
case TIMER2B:
return Timer({
{ &TCCR2A, &TCCR2B, nullptr },
{ (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
nullptr,
2, 1,
true, false
});
#else
case TIMER2B: ++q; case TIMER2A:
return Timer({
{ &TCCR2A, &TCCR2B, nullptr },
{ (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr },
nullptr,
2, q,
true, false
});
#endif
case TIMER2B: ++q; case TIMER2A:
return Timer({ { &TCCR2A, &TCCR2B, nullptr }, { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr }, nullptr, 2, q, true, false });
#endif

#ifdef OCR3C
case TIMER3C: ++q; case TIMER3B: ++q; case TIMER3A:
return Timer({
{ &TCCR3A, &TCCR3B, &TCCR3C },
{ &OCR3A, &OCR3B, &OCR3C },
&ICR3,
3, q,
true, false
});
return Timer({ { &TCCR3A, &TCCR3B, &TCCR3C }, { &OCR3A, &OCR3B, &OCR3C }, &ICR3, 3, q, true, false });
#elif defined(OCR3B)
case TIMER3B: ++q; case TIMER3A:
return Timer({
{ &TCCR3A, &TCCR3B, nullptr },
{ &OCR3A, &OCR3B, nullptr },
&ICR3,
3, q,
true, false
});
return Timer({ { &TCCR3A, &TCCR3B, nullptr }, { &OCR3A, &OCR3B, nullptr }, &ICR3, 3, q, true, false });
#endif

#ifdef TCCR4A
case TIMER4C: ++q; case TIMER4B: ++q; case TIMER4A:
return Timer({
{ &TCCR4A, &TCCR4B, &TCCR4C },
{ &OCR4A, &OCR4B, &OCR4C },
&ICR4,
4, q,
true, false
});
return Timer({ { &TCCR4A, &TCCR4B, &TCCR4C }, { &OCR4A, &OCR4B, &OCR4C }, &ICR4, 4, q, true, false });
#endif

#ifdef TCCR5A
case TIMER5C: ++q; case TIMER5B: ++q; case TIMER5A:
return Timer({
{ &TCCR5A, &TCCR5B, &TCCR5C },
{ &OCR5A, &OCR5B, &OCR5C },
&ICR5,
5, q,
true, false
});
return Timer({ { &TCCR5A, &TCCR5B, &TCCR5C }, { &OCR5A, &OCR5B, &OCR5C }, &ICR5, 5, q, true, false });
#endif
}

return Timer({
{ nullptr, nullptr, nullptr },
{ nullptr, nullptr, nullptr },
nullptr,
0, 0,
false, false
});
return Timer();
}

void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
Expand Down

0 comments on commit 4514758

Please sign in to comment.