OK
Fork of mbed-dev by
Revision 123:5dbefb20d136, committed 2016-05-05
- Comitter:
- mbed_official
- Date:
- Thu May 05 21:00:11 2016 +0100
- Parent:
- 122:18a6c3abfbc7
- Child:
- 124:6a4a5b7d7324
- Commit message:
- Synchronized with git revision 53b54323ba1513a5aa427d241594a7c0d1ed8786
Full URL: https://github.com/mbedmicro/mbed/commit/53b54323ba1513a5aa427d241594a7c0d1ed8786/
Fix pwmout for STM32F3
Changed in this revision
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F303VC/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F303VC/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F334C8/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_DISCO_F334C8/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F302R8/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F302R8/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303K8/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303K8/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303RE/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F303RE/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F334R8/objects.h Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/TARGET_NUCLEO_F334R8/objects.h Thu May 05 21:00:11 2016 +0100 @@ -99,6 +99,7 @@ struct pwmout_s { PWMName pwm; PinName pin; + uint32_t prescaler; uint32_t period; uint32_t pulse; uint32_t channel;
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c Wed May 04 22:30:12 2016 +0100 +++ b/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c Thu May 05 21:00:11 2016 +0100 @@ -96,7 +96,7 @@ // 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_DISABLE; @@ -161,8 +161,26 @@ // Update the SystemCoreClock variable SystemCoreClockUpdate(); - TimHandle.Init.Period = us - 1; - TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick + /* 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;