🏗️ Fix Maple HAL/STM32F1 PWM (#23211)
This commit is contained in:
parent
d7abb891cd
commit
48358d6a5c
@ -109,8 +109,8 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
|
|||||||
* (otherwise, characters will be lost due to UART overflow).
|
* (otherwise, characters will be lost due to UART overflow).
|
||||||
* Then: Stepper, Endstops, Temperature, and -finally- all others.
|
* Then: Stepper, Endstops, Temperature, and -finally- all others.
|
||||||
*/
|
*/
|
||||||
#define HAL_timer_isr_prologue(TIMER_NUM)
|
#define HAL_timer_isr_prologue(T)
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
|
||||||
/* 18 cycles maximum latency */
|
/* 18 cycles maximum latency */
|
||||||
#ifndef HAL_STEP_TIMER_ISR
|
#ifndef HAL_STEP_TIMER_ISR
|
||||||
|
@ -125,4 +125,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
|
|||||||
pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
|
pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -136,5 +136,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
|
|||||||
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
||||||
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
||||||
|
|
||||||
#define HAL_timer_isr_prologue(TIMER_NUM)
|
#define HAL_timer_isr_prologue(T)
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -92,5 +92,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
|
|||||||
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
||||||
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
||||||
|
|
||||||
#define HAL_timer_isr_prologue(TIMER_NUM)
|
#define HAL_timer_isr_prologue(T)
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -170,4 +170,4 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -87,5 +87,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
|
|||||||
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
||||||
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
||||||
|
|
||||||
#define HAL_timer_isr_prologue(TIMER_NUM)
|
#define HAL_timer_isr_prologue(T)
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -97,9 +97,15 @@
|
|||||||
#define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
|
#define STEP_TIMER_DEV _TIMER_DEV(STEP_TIMER)
|
||||||
#define TEMP_TIMER_DEV _TIMER_DEV(TEMP_TIMER)
|
#define TEMP_TIMER_DEV _TIMER_DEV(TEMP_TIMER)
|
||||||
|
|
||||||
// ------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
// Local defines
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#define NUM_HARDWARE_TIMERS 2
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
// Private Variables
|
// Private Variables
|
||||||
// ------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
|
HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
|
||||||
|
|
||||||
|
@ -41,7 +41,6 @@
|
|||||||
#define HAL_TIMER_TYPE_MAX UINT16_MAX
|
#define HAL_TIMER_TYPE_MAX UINT16_MAX
|
||||||
|
|
||||||
// Marlin timer_instance[] content (unrelated to timer selection)
|
// Marlin timer_instance[] content (unrelated to timer selection)
|
||||||
#define NUM_HARDWARE_TIMERS 2
|
|
||||||
#define MF_TIMER_STEP 0 // Timer Index for Stepper
|
#define MF_TIMER_STEP 0 // Timer Index for Stepper
|
||||||
#define MF_TIMER_TEMP 1 // Timer Index for Temperature
|
#define MF_TIMER_TEMP 1 // Timer Index for Temperature
|
||||||
#define MF_TIMER_PULSE MF_TIMER_STEP
|
#define MF_TIMER_PULSE MF_TIMER_STEP
|
||||||
@ -117,5 +116,5 @@ FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const ha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAL_timer_isr_prologue(TIMER_NUM)
|
#define HAL_timer_isr_prologue(T)
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -27,21 +27,35 @@
|
|||||||
#include "HAL.h"
|
#include "HAL.h"
|
||||||
#include "timers.h"
|
#include "timers.h"
|
||||||
|
|
||||||
|
#define NR_TIMERS TERN(STM32_XL_DENSITY, 14, 8) // Maple timers, 14 for STM32_XL_DENSITY (F/G chips), 8 for HIGH density (C D E)
|
||||||
|
|
||||||
|
static uint16_t timer_freq[NR_TIMERS];
|
||||||
|
|
||||||
|
inline uint8_t timer_and_index_for_pin(const pin_t pin, timer_dev **timer_ptr) {
|
||||||
|
*timer_ptr = PIN_MAP[pin].timer_device;
|
||||||
|
for (uint8_t i = 0; i < NR_TIMERS; i++) if (*timer_ptr == HAL_get_timer_dev(i))
|
||||||
|
return i;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
||||||
if (!PWM_PIN(pin)) return;
|
if (!PWM_PIN(pin)) return;
|
||||||
timer_dev *timer = PIN_MAP[pin].timer_device;
|
|
||||||
if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled
|
timer_dev *timer; UNUSED(timer);
|
||||||
|
if (timer_freq[timer_and_index_for_pin(pin, &timer)] == 0)
|
||||||
set_pwm_frequency(pin, PWM_FREQUENCY);
|
set_pwm_frequency(pin, PWM_FREQUENCY);
|
||||||
uint16_t max_val = timer->regs.bas->ARR * v / v_size;
|
|
||||||
if (invert) max_val = v_size - max_val;
|
const uint8_t channel = PIN_MAP[pin].timer_channel;
|
||||||
pwmWrite(pin, max_val);
|
const uint16_t duty = invert ? v_size - v : v;
|
||||||
|
timer_set_compare(timer, channel, duty);
|
||||||
|
timer_set_mode(timer, channel, TIMER_PWM); // PWM Output Mode
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_pwm_frequency(const pin_t pin, int f_desired) {
|
void set_pwm_frequency(const pin_t pin, int f_desired) {
|
||||||
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
||||||
|
|
||||||
timer_dev *timer = PIN_MAP[pin].timer_device;
|
timer_dev *timer; UNUSED(timer);
|
||||||
uint8_t channel = PIN_MAP[pin].timer_channel;
|
timer_freq[timer_and_index_for_pin(pin, &timer)] = f_desired;
|
||||||
|
|
||||||
// Protect used timers
|
// Protect used timers
|
||||||
if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
|
if (timer == HAL_get_timer_dev(MF_TIMER_TEMP)) return;
|
||||||
@ -53,8 +67,10 @@ void set_pwm_frequency(const pin_t pin, int f_desired) {
|
|||||||
if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled
|
if (!(timer->regs.bas->SR & TIMER_CR1_CEN)) // Ensure the timer is enabled
|
||||||
timer_init(timer);
|
timer_init(timer);
|
||||||
|
|
||||||
|
const uint8_t channel = PIN_MAP[pin].timer_channel;
|
||||||
timer_set_mode(timer, channel, TIMER_PWM);
|
timer_set_mode(timer, channel, TIMER_PWM);
|
||||||
uint16_t preload = 255; // Lock 255 PWM resolution for high frequencies
|
// Preload (resolution) cannot be equal to duty of 255 otherwise it may not result in digital off or on.
|
||||||
|
uint16_t preload = 254;
|
||||||
int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
|
int32_t prescaler = (HAL_TIMER_RATE) / (preload + 1) / f_desired - 1;
|
||||||
if (prescaler > 65535) { // For low frequencies increase prescaler
|
if (prescaler > 65535) { // For low frequencies increase prescaler
|
||||||
prescaler = 65535;
|
prescaler = 65535;
|
||||||
|
@ -188,7 +188,7 @@ FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
|
||||||
// No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
|
// No command is available in framework to turn off ARPE bit, which is turned on by default in libmaple.
|
||||||
// Needed here to reset ARPE=0 for stepper timer
|
// Needed here to reset ARPE=0 for stepper timer
|
||||||
|
@ -110,4 +110,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
|||||||
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
||||||
|
|
||||||
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -109,4 +109,4 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
|
|||||||
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
||||||
|
|
||||||
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
@ -114,4 +114,4 @@ bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
|
|||||||
|
|
||||||
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
void HAL_timer_isr_prologue(const uint8_t timer_num);
|
||||||
//void HAL_timer_isr_epilogue(const uint8_t timer_num) {}
|
//void HAL_timer_isr_epilogue(const uint8_t timer_num) {}
|
||||||
#define HAL_timer_isr_epilogue(TIMER_NUM)
|
#define HAL_timer_isr_epilogue(T)
|
||||||
|
Loading…
Reference in New Issue
Block a user