From 6dc9501153e48a32b12829404c897c56cd410c0b Mon Sep 17 00:00:00 2001 From: Laurent MEUNIER Date: Wed, 29 Jun 2016 11:03:53 +0200 Subject: [PATCH] [STM32L4] Handle higher range pwm periods As first reported on STM32F3 family in #1682, we need to cope with periods in the seconds range as well. This is fixed here in the same way as was done for STM32F3 by using the pre-scaler. --- .../TARGET_STM32L4/common_objects.h | 1 + .../TARGET_STM/TARGET_STM32L4/pwmout_api.c | 36 ++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/common_objects.h b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/common_objects.h index 69a0cb6add..a3c3410cb3 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/common_objects.h +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/common_objects.h @@ -42,6 +42,7 @@ extern "C" { struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint8_t channel; diff --git a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/pwmout_api.c b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/pwmout_api.c index 901f256059..8d12141400 100644 --- a/hal/targets/hal/TARGET_STM/TARGET_STM32L4/pwmout_api.c +++ b/hal/targets/hal/TARGET_STM/TARGET_STM32L4/pwmout_api.c @@ -77,6 +77,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) obj->pin = pin; obj->period = 0; obj->pulse = 0; + obj->prescaler = 1; pwmout_period_us(obj, 20000); // 20 ms per default } @@ -104,7 +105,7 @@ void pwmout_write(pwmout_t* obj, float value) // Configure channels sConfig.OCMode = TIM_OCMODE_PWM1; - sConfig.Pulse = obj->pulse; + sConfig.Pulse = obj->pulse / obj->prescaler; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCFastMode = TIM_OCFAST_ENABLE; @@ -168,22 +169,39 @@ void pwmout_period_us(pwmout_t* obj, int us) SystemCoreClockUpdate(); - TimHandle.Init.Period = us - 1; - TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick - TimHandle.Init.ClockDivision = 0; - TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; - TimHandle.Init.RepetitionCounter = 0; + /* To make it simple, we use to possible prescaler values which lead to: + * pwm unit = 1us, period/pulse can be from 1us to 65535us + * or + * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec + * Be careful that all the channels of a PWM shares the same prescaler + */ + if (us > 0xFFFF) { + obj->prescaler = 500; + } else { + obj->prescaler = 1; + } + TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1; + + if (TimHandle.Init.Prescaler > 0xFFFF) + error("PWM: out of range prescaler"); + + TimHandle.Init.Period = (us - 1) / obj->prescaler; + if (TimHandle.Init.Period > 0xFFFF) + error("PWM: out of range period"); + + TimHandle.Init.ClockDivision = 0; + TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) { error("Cannot initialize PWM\n"); } - // Set duty cycle again - pwmout_write(obj, dc); - // Save for future use obj->period = us; + // Set duty cycle again + pwmout_write(obj, dc); + __HAL_TIM_ENABLE(&TimHandle); }