Skip to content

TIM2 SetDutyCycle wrong CCR value computation #185

@GianluigiForte

Description

@GianluigiForte

I'm using the PWM example with the Nucleo G431 board configuring the PA5 pin as PWM to drive the LED present on the board.

I think that there is a problem with the SetDutyCycle implementation when is used the TIM2 because for what I understood the $typ in case of TIM2 is 32bit

since

tim_hal! {
TIM1: (tim1, u16, 16, DIR: cms, BDTR: bdtr, set_bit, af1, clear_bit, clear_bit),
TIM2: (tim2, u32, 32, DIR: cms),
TIM3: (tim3, u16, 16, DIR: cms),
TIM4: (tim4, u16, 16, DIR: cms),
}

And so when is called the SetDutyCycle

fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
// hal trait always has 16 bit resolution
let duty = (duty as $typ) << (8(size_of::<$typ>() - size_of::()));*
let tim = unsafe { &*$TIMX::ptr() };
Ok(tim.$ccrx().write(|w| unsafe { w.ccr().bits(duty.into()) }))
}

The duty value is wrongly shifted by 16bits on the left, creating many troubles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions