From 423c5694d08924b85d13ab1e6962da71526804a5 Mon Sep 17 00:00:00 2001 From: Nico Tonnhofer Date: Thu, 24 Mar 2016 21:50:11 +0100 Subject: [PATCH] STM32F411: Respect configured PWM frequencies in heater-stm32.c Test: the PWM frequency on the scope should be similar to the one configures in the board file with DEFINE_HEATER(). --- heater-stm32.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/heater-stm32.c b/heater-stm32.c index 8a86c3d..1b1aa62 100644 --- a/heater-stm32.c +++ b/heater-stm32.c @@ -142,6 +142,7 @@ void heater_init() { */ if (NUM_HEATERS) { // At least one channel in use. + uint32_t freq; uint8_t macro_mask; // Auto-generate pin setup. @@ -173,7 +174,12 @@ void heater_init() { pin ## _TIMER->ARR = PWM_SCALE - 1; /* reset on auto reload at 254 */ \ /* PWM_SCALE - 1, so CCR = 255 is full off. */ \ pin ## _TIMER-> EXPANDER(CCR, pin ## _CHANNEL,) = 0; /* start off */ \ - pin ## _TIMER->PSC = F_CPU / PWM_SCALE / 1000 - 1; /* 1kHz */ \ + freq = F_CPU / PWM_SCALE / pwm; /* Figure PWM freq. */ \ + if (freq > 65535) \ + freq = 65535; \ + if (freq < 1) \ + freq = 1; \ + pin ## _TIMER->PSC = freq - 1; /* 1kHz */ \ macro_mask = pin ## _CHANNEL > 2 ? 2 : 1; \ if (macro_mask == 1) { \ pin ## _TIMER->CCMR1 |= 0x0D << (3 + (8 * (pin ## _CHANNEL / macro_mask - 1))); \