Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pwmout #1706

Merged
merged 4 commits into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct i2c_s {
struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
uint32_t channel;
Expand Down
24 changes: 21 additions & 3 deletions libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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_DISABLE;
Expand Down Expand Up @@ -161,8 +161,26 @@ void pwmout_period_us(pwmout_t* obj, int us)
// 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;

Expand Down
6 changes: 3 additions & 3 deletions libraries/tests/mbed/pwm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ float value = 0.75;
int main() {
#if defined(TARGET_FF_ARDUINO)
PwmOut pwm(D9);
int period_ms = 10;

pwm.period_ms(10);
pwm.period_ms(period_ms);
pwm.write(value);

float result = floor(pwm.read() * 100 + 0.5) / 100; // round it to 0.xx
printf("Initialize PWM on pin D9 with duty cycle: %.2f\n", result);
printf("PWM period = %dms with duty cycle: %d%%\n", period_ms, (int) (result * 100));

notify_completion(result == value ? true : false);

#elif defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || defined(TARGET_LPC11U24) || defined(TARGET_LPC4088) || defined(TARGET_LPC2460)
PwmOut pwm_p25(p25);
PwmOut pwm_p26(p26);
Expand Down