From 7903a8a46ca0a9eab10eb0fef20427fe48d69043 Mon Sep 17 00:00:00 2001 From: JM Date: Tue, 13 Jun 2017 06:01:13 -0600 Subject: [PATCH] stm32/stm32l4 PWM: While attempting to output a 70 MHz square wave from the timer output of a STM32 clocked at 140 MHz (which works fine in baremetal C), I stumbled on what I believe to be an error in arch/arm/src/stm32/stm32_pwm.c. Line 1304 we are told that reload = timclk / info->frequency; which I belive to be incorrect, it should be reload = timclk / info->frequency - 1; since starting to count from 0, if I want to output half of the TIM clock, I must count to 1 and not to 2. Surely enough, the original code did output 140/3=47 MHz, while this correction does allow the output up to 70 MHz. I am not sure this affects most users generating slow PWM (e.g. PX4) but for frequencies close to the PCLK, indeed the difference becomes significant. --- arch/arm/src/stm32/stm32_pwm.c | 2 +- arch/arm/src/stm32l4/stm32l4_pwm.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm/src/stm32/stm32_pwm.c b/arch/arm/src/stm32/stm32_pwm.c index 85ed3554fc9..48640f98690 100644 --- a/arch/arm/src/stm32/stm32_pwm.c +++ b/arch/arm/src/stm32/stm32_pwm.c @@ -1300,7 +1300,7 @@ static int pwm_timer(FAR struct stm32_pwmtimer_s *priv, timclk = priv->pclk / prescaler; - reload = timclk / info->frequency; + reload = timclk / info->frequency - 1; if (reload < 1) { reload = 1; diff --git a/arch/arm/src/stm32l4/stm32l4_pwm.c b/arch/arm/src/stm32l4/stm32l4_pwm.c index fe46e8cc4ef..3c0c8dab18f 100644 --- a/arch/arm/src/stm32l4/stm32l4_pwm.c +++ b/arch/arm/src/stm32l4/stm32l4_pwm.c @@ -841,7 +841,7 @@ static int stm32l4pwm_timer(FAR struct stm32l4_pwmtimer_s *priv, timclk = priv->pclk / prescaler; - reload = timclk / info->frequency; + reload = timclk / info->frequency - 1; if (reload < 1) { reload = 1;