I'm using the STM32duino now for a while and enhanced my model-railroaders library to run on STM32.
This library extensivly uses timer 4 and compare interrupts. It does NOT use the related PWM output pins.
Sometimes my library stopped to work, and I found out, that this is always the case, when a pinMode() command ist running on one of the pins that can be used with PWM and timer 4.
The cause is this code snippet in wirish_digital_f1.cpp:
Code: Select all
gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, outputMode);
if (PIN_MAP[pin].timer_device != NULL) {
/* Enable/disable timer channels if we're switching into or
* out of PWM. */
timer_set_mode(PIN_MAP[pin].timer_device,
PIN_MAP[pin].timer_channel,
pwm ? TIMER_PWM : TIMER_DISABLED);
}
The timer should only be touched if the last mode was a pwm mode, or if the mode to be set is a PWM mode. In all other cases the timer should be left untouched.
I tried with this code, and it worked fine in my tests:
Code: Select all
// remember old mode to see whether we have to change timer mode
gpio_pin_mode oldMode = gpio_get_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit);
gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, outputMode);
if (PIN_MAP[pin].timer_device != NULL) {
/* Enable/disable timer channels if we're switching into or
* out of PWM. */
if ( pwm || oldMode == GPIO_AF_OUTPUT_OD || oldMode == GPIO_AF_OUTPUT_PP ) {
timer_set_mode(PIN_MAP[pin].timer_device,
PIN_MAP[pin].timer_channel,
pwm ? TIMER_PWM : TIMER_DISABLED);
}
}
Regards,
Franz