Skip to content

Commit 7c657ad

Browse files
committed
[STM32F3] Increase the supported period range (#1682)
Introducing the prescaler management allows a wider period range support, from about 65ms before now up to about 32s. We're also introducing asserts in case the period or prescaler is truncated as the HW registers are 16 bits large.
1 parent 9cef243 commit 7c657ad

File tree

7 files changed

+25
-3
lines changed

7 files changed

+25
-3
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F303VC/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F334C8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F302R8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303K8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303RE/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F334R8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct i2c_s {
9999
struct pwmout_s {
100100
PWMName pwm;
101101
PinName pin;
102+
uint32_t prescaler;
102103
uint32_t period;
103104
uint32_t pulse;
104105
uint32_t channel;

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void pwmout_write(pwmout_t* obj, float value)
9696

9797
// Configure channels
9898
sConfig.OCMode = TIM_OCMODE_PWM1;
99-
sConfig.Pulse = obj->pulse;
99+
sConfig.Pulse = obj->pulse / obj->prescaler;
100100
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
101101
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
102102
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
@@ -161,8 +161,24 @@ void pwmout_period_us(pwmout_t* obj, int us)
161161
// Update the SystemCoreClock variable
162162
SystemCoreClockUpdate();
163163

164-
TimHandle.Init.Period = us - 1;
165-
TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
164+
/* To make it simple, we use to possible prescaler values which lead to:
165+
* pwm unit = 1us, period/pulse can be from 1us to 65535us
166+
* or
167+
* pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
168+
* Be careful that all the channels of a PWM shares the same prescaler
169+
*/
170+
if (us > 0xFFFF) {
171+
obj->prescaler = 500;
172+
} else {
173+
obj->prescaler = 1;
174+
}
175+
TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;
176+
177+
MBED_ASSERT(TimHandle.Init.Prescaler < 0xFFFF);
178+
179+
TimHandle.Init.Period = (us - 1) / obj->prescaler;
180+
MBED_ASSERT(TimHandle.Init.Period < 0xFFFF);
181+
166182
TimHandle.Init.ClockDivision = 0;
167183
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
168184

0 commit comments

Comments
 (0)