added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
Diff: targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c
- Revision:
- 123:5dbefb20d136
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
--- 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;