|
@@ -30,30 +30,35 @@
|
30
|
30
|
static uint16_t timer_freq[TIMER_NUM];
|
31
|
31
|
|
32
|
32
|
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
|
33
|
|
- if (!PWM_PIN(pin)) return; // Don't proceed if no hardware timer
|
34
|
|
- const PinName pin_name = digitalPinToPinName(pin);
|
35
|
|
- TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
|
|
33
|
+ const uint16_t duty = invert ? v_size - v : v;
|
|
34
|
+ if (PWM_PIN(pin)) {
|
|
35
|
+ const PinName pin_name = digitalPinToPinName(pin);
|
|
36
|
+ TIM_TypeDef * const Instance = (TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM);
|
36
|
37
|
|
37
|
|
- const timer_index_t index = get_timer_index(Instance);
|
38
|
|
- const bool needs_freq = (HardwareTimer_Handle[index] == nullptr);
|
39
|
|
- if (needs_freq) // A new instance must be set to the default frequency of PWM_FREQUENCY
|
40
|
|
- HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
|
|
38
|
+ const timer_index_t index = get_timer_index(Instance);
|
|
39
|
+ const bool needs_freq = (HardwareTimer_Handle[index] == nullptr);
|
|
40
|
+ if (needs_freq) // A new instance must be set to the default frequency of PWM_FREQUENCY
|
|
41
|
+ HardwareTimer_Handle[index]->__this = new HardwareTimer((TIM_TypeDef *)pinmap_peripheral(pin_name, PinMap_PWM));
|
41
|
42
|
|
42
|
|
- HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
43
|
|
- const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
|
44
|
|
- const TimerModes_t previousMode = HT->getMode(channel);
|
45
|
|
- if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
|
46
|
|
- HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
|
|
43
|
+ HardwareTimer * const HT = (HardwareTimer *)(HardwareTimer_Handle[index]->__this);
|
|
44
|
+ const uint32_t channel = STM_PIN_CHANNEL(pinmap_function(pin_name, PinMap_PWM));
|
|
45
|
+ const TimerModes_t previousMode = HT->getMode(channel);
|
|
46
|
+ if (previousMode != TIMER_OUTPUT_COMPARE_PWM1)
|
|
47
|
+ HT->setMode(channel, TIMER_OUTPUT_COMPARE_PWM1, pin);
|
47
|
48
|
|
48
|
|
- if (needs_freq && timer_freq[index] == 0) // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
|
49
|
|
- set_pwm_frequency(pin_name, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
|
|
49
|
+ if (needs_freq && timer_freq[index] == 0) // If the timer is unconfigured and no freq is set then default PWM_FREQUENCY
|
|
50
|
+ set_pwm_frequency(pin_name, PWM_FREQUENCY); // Set the frequency and save the value to the assigned index no.
|
50
|
51
|
|
51
|
|
- // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
|
52
|
|
- // 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.
|
53
|
|
- const uint16_t value = invert ? v_size - v : v;
|
54
|
|
- HT->setCaptureCompare(channel, value, RESOLUTION_8B_COMPARE_FORMAT); // Sets the duty, the calc is done in the library :)
|
55
|
|
- pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
|
56
|
|
- if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
|
|
52
|
+ // Note the resolution is sticky here, the input can be upto 16 bits and that would require RESOLUTION_16B_COMPARE_FORMAT (16)
|
|
53
|
+ // 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.
|
|
54
|
+ HT->setCaptureCompare(channel, duty, RESOLUTION_8B_COMPARE_FORMAT); // Set the duty, the calc is done in the library :)
|
|
55
|
+ pinmap_pinout(pin_name, PinMap_PWM); // Make sure the pin output state is set.
|
|
56
|
+ if (previousMode != TIMER_OUTPUT_COMPARE_PWM1) HT->resume();
|
|
57
|
+ }
|
|
58
|
+ else {
|
|
59
|
+ pinMode(pin, OUTPUT);
|
|
60
|
+ digitalWrite(pin, duty < v_size / 2 ? LOW : HIGH);
|
|
61
|
+ }
|
57
|
62
|
}
|
58
|
63
|
|
59
|
64
|
void set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
|