|
@@ -27,37 +27,67 @@
|
27
|
27
|
#include "../../inc/MarlinConfig.h"
|
28
|
28
|
#include "timers.h"
|
29
|
29
|
|
30
|
|
-void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
31
|
|
- if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
30
|
+// Array to support sticky frequency sets per timer
|
|
31
|
+static uint16_t timer_freq[TIMER_NUM];
|
32
|
32
|
|
|
33
|
+void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
|
34
|
+ if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
35
|
+ bool needs_freq;
|
33
|
36
|
PinName pin_name = digitalPinToPinName(pin);
|
34
|
37
|
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
|
|
38
|
+ HardwareTimer *HT;
|
|
39
|
+ TimerModes_t previousMode;
|
35
|
40
|
|
36
|
|
- uint16_t adj_val = Instance->ARR * v / v_size;
|
37
|
|
- if (invert) adj_val = Instance->ARR - adj_val;
|
38
|
|
- switch (get_pwm_channel(pin_name)) {
|
39
|
|
- case TIM_CHANNEL_1: LL_TIM_OC_SetCompareCH1(Instance, adj_val); break;
|
40
|
|
- case TIM_CHANNEL_2: LL_TIM_OC_SetCompareCH2(Instance, adj_val); break;
|
41
|
|
- case TIM_CHANNEL_3: LL_TIM_OC_SetCompareCH3(Instance, adj_val); break;
|
42
|
|
- case TIM_CHANNEL_4: LL_TIM_OC_SetCompareCH4(Instance, adj_val); break;
|
43
|
|
- }
|
44
|
|
-}
|
|
41
|
+ uint16_t value = v;
|
|
42
|
+ if (invert) value = v_size - value;
|
45
|
43
|
|
46
|
|
-#if NEEDS_HARDWARE_PWM
|
|
44
|
+ uint32_t index = get_timer_index(Instance);
|
47
|
45
|
|
48
|
|
- void set_pwm_frequency(const pin_t pin, int f_desired) {
|
49
|
|
- if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
46
|
+ if (HardwareTimer_Handle[index] == nullptr) {
|
|
47
|
+ HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
|
|
48
|
+ needs_freq = true; // The instance must be new set the default frequency of PWM_FREQUENCY
|
|
49
|
+ }
|
50
|
50
|
|
51
|
|
- PinName pin_name = digitalPinToPinName(pin);
|
52
|
|
- TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
|
|
51
|
+ HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
|
52
|
+ uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
|
|
53
|
+ previousMode = HT->getMode(channel);
|
53
|
54
|
|
54
|
|
- LOOP_S_L_N(i, 0, NUM_HARDWARE_TIMERS) // Protect used timers
|
55
|
|
- if (timer_instance[i] && timer_instance[i]->getHandle()->Instance == Instance)
|
56
|
|
- return;
|
|
55
|
+ if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
|
|
56
|
+ HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
|
57
|
57
|
|
58
|
|
- pwm_start(pin_name, f_desired, 0, RESOLUTION_8B_COMPARE_FORMAT);
|
|
58
|
+ if (needs_freq) {
|
|
59
|
+ if (timer_freq[index] == 0 ) { // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY.
|
|
60
|
+ timer_freq[index] = PWM_FREQUENCY;
|
|
61
|
+ set_pwm_frequency(pin_name, timer_freq[index]); // Set the frequency and save the value to the assigned index no.
|
|
62
|
+ }
|
59
|
63
|
}
|
|
64
|
+ // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
|
|
65
|
+ // If such a need were to manifest then we would need to calc the resolution based on the v_size parameter and add code for it.
|
|
66
|
+ HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :)
|
|
67
|
+ pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
|
|
68
|
+ if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
|
|
69
|
+}
|
60
|
70
|
|
61
|
|
-#endif
|
|
71
|
+void set_pwm_frequency(const pin_t pin, int f_desired) {
|
|
72
|
+ if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
|
73
|
+ HardwareTimer *HT;
|
|
74
|
+ PinName pin_name = digitalPinToPinName(pin);
|
|
75
|
+ TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM); // Get HAL timer instance
|
|
76
|
+
|
|
77
|
+ uint32_t index = get_timer_index(Instance);
|
|
78
|
+
|
|
79
|
+ // Protect used timers
|
|
80
|
+ if (index == TEMP_TIMER_NUM || index == STEP_TIMER_NUM
|
|
81
|
+ #if PULSE_TIMER_NUM != STEP_TIMER_NUM
|
|
82
|
+ || index == PULSE_TIMER_NUM
|
|
83
|
+ #endif
|
|
84
|
+ ) return;
|
|
85
|
+
|
|
86
|
+ if (HardwareTimer_Handle[index] == nullptr) // If frequency is set before duty we need to create a handle here.
|
|
87
|
+ HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
|
|
88
|
+ HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
|
89
|
+ timer_freq[index] = f_desired; // Save the last frequency so duty will not set the default for this timer number.
|
|
90
|
+ HT->setOverflow(f_desired, HERTZ_FORMAT);
|
|
91
|
+}
|
62
|
92
|
|
63
|
93
|
#endif // HAL_STM32
|