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

drivers: pwm: check if pulse > period and other cleanups #44583

Merged
merged 5 commits into from
Apr 7, 2022

Conversation

gmarull
Copy link
Member

@gmarull gmarull commented Apr 6, 2022

Calling pwm_pin_set_cycles with a pulse > period doesn't make sense. By
definition, pulse ranges from 0 up to the period value.

Also:

  • The API spec states that calling pin_set() with period set to 0 is
    equivalent to set the PWM channel to an inactive level. Some drivers
    treat this input as invalid (-EINVAL), however, it's an unsupported
    feature. Maybe it's due to copy&paste effect? This changes error message
    to be clear, and changes return value to -ENOTSUP for this case.
  • Other minor fixes I found while doing this work (please check commit messages)

gmarull added 5 commits April 6, 2022 12:53
Calling pwm_pin_set_cycles with a pulse > period doesn't make sense. By
definition, pulse ranges from 0 up to the period value.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
The API call checks for this condition before calling the pin_set driver
OP call, so drivers don't have to do this check now.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
The API spec states that calling pin_set() with period set to 0 is
equivalent to set the PWM channel to an inactive level. Some drivers
treat this input as invalid (-EINVAL), however, it's an unsupported
feature. Maybe it's due to copy&paste effect? This changes error message
to be clear and changes return value to -ENOTSUP for this case.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
If dev was null, caller would have faulted before since dev->api needs
to be accessed before reaching this point. Also, a well-defined device
will never have a NULL dev->config.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
All these sort of helpers were removed from tree a while ago, this one
was missed as it uses a custom name.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
@@ -193,6 +194,10 @@ static inline int z_impl_pwm_pin_set_cycles(const struct device *dev,
const struct pwm_driver_api *api =
(const struct pwm_driver_api *)dev->api;

if (pulse > period) {
return -EINVAL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do pulse = period; instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an option to clamp pulse to period value, but still, I think that a pulse > period is wrong, so it may indicate an application issue.

@carlescufi carlescufi merged commit 8ff797b into zephyrproject-rtos:main Apr 7, 2022
@gmarull gmarull deleted the pwm-common-checks branch April 24, 2023 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: PWM Pulse Width Modulation platform: NXP NXP platform: STM32 ST Micro STM32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants