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_led: esp32: add inverted pwm polarity #80673

Closed

Conversation

epc-ake
Copy link
Contributor

@epc-ake epc-ake commented Oct 31, 2024

This PR enables the PWM to be inverted by making use of the PWM_POLARITY_INVERTED flag.
It also fixes an overflow bug when setting duty-cycle to 100 percent.

Copy link
Member

@uLipe uLipe left a comment

Choose a reason for hiding this comment

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

LGTM so far, just consider the point from @wmrsouza and I will also approve it.

wmrsouza
wmrsouza previously approved these changes Oct 31, 2024
sylvioalves
sylvioalves previously approved these changes Oct 31, 2024
uLipe
uLipe previously approved these changes Oct 31, 2024
raffarost
raffarost previously approved these changes Nov 1, 2024
@sylvioalves sylvioalves modified the milestone: v4.0.0 Nov 4, 2024
@mmahadevan108
Copy link
Collaborator

@epc-ake Can you please add an issue with details and link that issue to this PR. After RC2 only bug fixes with an associated Github ssue will be merged into the upcoming 4.0 release.

@mmahadevan108 mmahadevan108 added the bug The issue is a bug, or the PR is fixing a bug label Nov 4, 2024
@raffarost
Copy link
Collaborator

raffarost commented Nov 4, 2024

hi @epc-ake,
can you check that you really are able to get a 100% duty with this solution?
I checked with the oscilloscope and see a pulse (99% duty, which is expected), so we think this might not be the best solution.
can you confirm?

Please have a look at #80878 if possible

@epc-ake epc-ake dismissed stale reviews from raffarost, uLipe, sylvioalves, and wmrsouza via 71a6c00 November 5, 2024 10:29
@epc-ake epc-ake force-pushed the fix/drivers/pwm_polarity_invert branch from fa5a4f5 to 71a6c00 Compare November 5, 2024 10:29
@epc-ake
Copy link
Contributor Author

epc-ake commented Nov 5, 2024

hi @epc-ake, can you check that you really are able to get a 100% duty with this solution? I checked with the oscilloscope and see a pulse (99% duty, which is expected), so we think this might not be the best solution. can you confirm?

Please have a look at #80878 if possible

Thanks for pointing this out. I didn't notice it actually.
I pushed a fix now but we can also use the changed from your PR.
However, I have an jet an other issue with this driver. It seems like the driver ignores the output states defined in the pinctrl definition of the devicetree. e.g. output-high; has no effect.
It's kind of important for an inverted pwm to also initialize with 100% dut i think.

@raffarost please tell me how you'd like to proceed with this and I'll adopt.

@epc-ake epc-ake marked this pull request as draft November 5, 2024 12:30
@raffarost
Copy link
Collaborator

@epc-ake I think the way you implemented is also alright, but I'd personally leave the possibility to set the value in the DT, including the max res (for applications which won't use duty = 100%). Probably not that relevant but I think leaving open the possibility of using the config is better.

@sylvioalves what do you think?

@epc-ake
Copy link
Contributor Author

epc-ake commented Nov 5, 2024

I suggest that I remove my changes regarding 100% pwm issue so we can use your PR for that. Looks cleaner to me.
Meanwile I will try to fix the initialization state issue for inverted pwm polarity in this PR.

@raffarost
Copy link
Collaborator

hi @epc-ake,
I spoke to Sylvio about this and we concluded you can keep the fix in your PR.
Would you mind adding a comment to explain why SOC_LEDC_TIMER_BIT_WIDTH - 1 is used?
thank you

@epc-ake epc-ake force-pushed the fix/drivers/pwm_polarity_invert branch from 3a14f4e to 231a780 Compare November 6, 2024 16:52
@epc-ake
Copy link
Contributor Author

epc-ake commented Nov 6, 2024

Hi all
I needed to refactor some code here to be able to initialize the pwm with high state (needed for inverted pwm's)
This includes moving the channel and pin configuration out of pwm_led_esp32_set_cycles() to pwm_led_esp32_init().
The only way I found setting the initial state of the pwm's was by using the ledc_hal_set_idle_level() function in combination with a new dt-property init-high.

I intentionally made this changes in a separate commit but if you prefer, I can also squeeze them into one.
@raffarost and @sylvioalves, could you have a look?

@epc-ake epc-ake force-pushed the fix/drivers/pwm_polarity_invert branch from 231a780 to 155b219 Compare November 6, 2024 17:07
Copy link
Collaborator

@raffarost raffarost left a comment

Choose a reason for hiding this comment

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

LGTM
you'll need to rebase it with latest main

.speed_mode = DT_REG_ADDR(node_id) < SOC_LEDC_CHANNEL_NUM ? LEDC_LOW_SPEED_MODE \
: !LEDC_LOW_SPEED_MODE, \
.clock_src = CLOCK_SOURCE, \
.inverted = DT_PROP(node_id, init_high), \
Copy link
Collaborator

Choose a reason for hiding this comment

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

I couldn't be sure this flag would fit in the field flags already foreseen in the DT (pwm_dt_spec / pwm_flags_t)
It would align better with the runtime check made in function set_cycles

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know how to get the value of the flags field here?
I‘d actually prefer that over adding an extra dt-property but I didn’t find a way to do it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

in include/zephyr/drivers/pwm.h stay all the macros to unpack dts values:

 *    const struct pwm_dt_spec spec =
 *        PWM_DT_SPEC_GET_BY_NAME(DT_NODELABEL(n), alpha);
 *
 *    // Initializes 'spec' to:
 *    // {
 *    //         .dev = DEVICE_DT_GET(DT_NODELABEL(pwm1)),
 *    //         .channel = 1,
 *    //         .period = 1000,
 *    //         .flags = PWM_POLARITY_NORMAL,
 *    // }

does it make sense to declare a pwms field for this driver (and include the flag)?
I don't know enough about the API to give a solid opinion now, but others might know better.

pwms = <&pwm2 3 2000 PWM_POLARITY_INVERTED>;

uLipe
uLipe previously approved these changes Nov 12, 2024
@raffarost
Copy link
Collaborator

@epc-ake do you intend to keep working on this PR?
we shall at some point add these functionalities to the driver, so just asking to have an idea

@epc-ake
Copy link
Contributor Author

epc-ake commented Dec 9, 2024

Hi @raffarost.
I'm a bit busy currently but I might find some time at the end of this week for this.
If you want to speed it up feel free to create a new PR on top of this :-)

@raffarost
Copy link
Collaborator

Hi @raffarost. I'm a bit busy currently but I might find some time at the end of this week for this. If you want to speed it up feel free to create a new PR on top of this :-)

no rush, if you'll submit it at some point it's fine
thank you

This PR enables the PWM to be inverted by making use of the
`PWM_POLARITY_INVERTED` flag.
It also fixes an overflow bug when setting duty-cycle to 100 percent.

Signed-off-by: Armin Kessler <ake@espros.com>
Set pwm init state to value defined in flags field.
Furthermore 100 percent is now actually 100 percent and not 99.

Signed-off-by: Armin Kessler <ake@espros.com>
@epc-ake epc-ake force-pushed the fix/drivers/pwm_polarity_invert branch from 155b219 to d788349 Compare December 10, 2024 17:19
@epc-ake epc-ake marked this pull request as ready for review December 10, 2024 17:21
@epc-ake
Copy link
Contributor Author

epc-ake commented Dec 10, 2024

@raffarost
Could you please test it on your side?

@raffarost
Copy link
Collaborator

@epc-ake I did some testing and think a fix regarding frequency update is needed (please check #82820, last commit). I tested on all devices and it seems to work fine. If you agree and can also kindly test them, feel free to incorporate the changes in your PR.

@breiden can you check if this PR with these changes solve the problem you reported in #82177?

.speed_mode = DT_REG_ADDR(node_id) < SOC_LEDC_CHANNEL_NUM ? LEDC_LOW_SPEED_MODE \
: !LEDC_LOW_SPEED_MODE, \
.clock_src = CLOCK_SOURCE, \
.inverted = DT_PWMS_FLAGS(node_id), \
Copy link
Collaborator

Choose a reason for hiding this comment

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

regarding this field, I checked other PWM drivers and these macros and pwms field seems to be only used by application, not directly by the driver. Considering this apparent 'standard' for the drivers, I'm not sure we can use it here. Possibly your earlier solution was more appropriate.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds the earlier solution is indeed more appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see the issue with using 'DT_PWMS_FLAGS' here.
To me this seems to be better since we avoid having redundant information in the device tree.

@epc-ake
Copy link
Contributor Author

epc-ake commented Dec 12, 2024

@epc-ake I did some testing and think a fix regarding frequency update is needed (please check #82820, last commit). I tested on all devices and it seems to work fine. If you agree and can also kindly test them, feel free to incorporate the changes in your PR.

@raffarost Since you already made a PR on top of this one (#82820), would you take over this?
I wont find enough time this year any more.

@raffarost
Copy link
Collaborator

@epc-ake I did some testing and think a fix regarding frequency update is needed (please check #82820, last commit). I tested on all devices and it seems to work fine. If you agree and can also kindly test them, feel free to incorporate the changes in your PR.

@raffarost Since you already made a PR on top of this one (#82820), would you take over this? I wont find enough time this year any more.

hi @epc-ake @braiden
sure, I'll evaluate the issues you mentioned and work on it.
thanks for the input

@epc-ake epc-ake closed this Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: PWM Pulse Width Modulation bug The issue is a bug, or the PR is fixing a bug platform: ESP32 Espressif ESP32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants