-
Notifications
You must be signed in to change notification settings - Fork 2k
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
cpu/stm32/periph/pwm: some bugfixes... #14482
Conversation
Damn! Now I see a new bug in case we use several pwm devices with different modes and one of them uses PWM_RIGHT mode |
1c143c9
to
304d3f9
Compare
cpu/stm32/periph/pwm.c
Outdated
TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC2M_0 | \ | ||
TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2); | ||
|
||
static uint32_t dc_reverse = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used to store PWM devices which are initialized in PWM_RIGHT mode in the pwm_init function and whose duty cycle must be reversed by the pwm_set function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the pwm_set function looks into the CCMR hardware register instead.
No activity for more than a week, do I need to do something else? I'm going on vacation for one or two weeks tomorrow. |
@MrKevinWeiss can you test this, maybe with the HIL setup? |
@hugueslarrive I currently don't have access to a oscilloscope to test your changes, if @MrKevinWeiss can't test maybe you can provide a screenshot of the issue? Otherwise I can test next monday. |
@fjmolinas I left the DSO at the lab for interns but I found an old probes pair so here are some pictures of the tests. PWM_LEFT
Now trying PWM_RIGHT
with master: PWM_CENTER
Also checked in all modes
all work as expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for posting the results which clearly looks good.
ACK!
Contribution description
PWM_RIGHT and PWM_CENTER wasn't handled correctly for stm32 cpus.
In PWM_RIGHT mode output polarity was reversed which indeed right align
rising edge but also reverses the duty cycle which is not desirable.
In PWM_CENTER mode capture/compare mode registers was set to 0 so there
was no output at all.
Moreover it is not possible to set the duty-cycle to 100% because value
is normed to dev(pwm)->ARR which is equal to (res - 1).
I also renamed CCMR_* constants because I have used CCMR_LEFT for both
PWM_LEFT and PWM_CENTER modes.
Finally in PWM_CENTER mode the resolution have to be divided by two in
pwm_init() call so it must be multiplied by 2 for the parameters
verification, prescaler setting and frequency computation, except for
the auto-reload register.
Regarding the PWM_RIGHT mode, I first tried to reverse the timer
direction in CR1 register but it wasn't possible to get 0% duty cycle
this way because there was allways one clock cycle where the output was
activated (when TIMx_CNT=TIMx_CCR1).
Testing procedure
Using tests/periph_pwm and a bluepill or another stm32 board and
checking 2 outputs (A8 and A9 for the bluepill) with a scope.
PWM_LEFT
Initialize a left aligned pwm at 1KHz with
init 0 0 1000 4
then setthe first channel to 25% duty-cycle and the second to 50% with
set 0 0 1
andset 0 1 2
, we get the following oscillograms asexpected:
But if I try to set a 100% duty-cycle with
set 0 0 4
, I only get 75%because of value is normed to dev(pwm)->ARR which is equal to (res - 1).
PWM_RIGHT
Initialize a right aligned pwm at 1KHz with
init 0 1 1000 4
then setthe first channel to 25% duty-cycle and the second to 50% with
set 0 0 1
andset 0 1 2
, we get the following oscillograms.Expected :
But we get :
PWM_CENTER
Initialize a center aligned pwm at 1KHz with
init 0 2 1000 4
then setthe first channel to 25% duty-cycle and the second to 50% with
set 0 0 1
andset 0 1 2
not working at all because of the CCMRregisters are set to 0.
Expected :
But we get a kind of brain dead.
Issues/PRs references