Page 1 of 1

mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Fri Sep 22, 2023 4:59 am
by ChrisP_SD
Hello Community,
Does anyone have a working example of how to update the PWM frequency using the new mcpwm driver in IDF v5.0? This was simple in v4.4 using the provided mpcwm_set_frequency() function, however in IDF v5.0, mcpwm_set_frequency was removed. According to the migration documentation from https://docs.espressif.com/projects/esp ... erals.html

"mcpwm_set_frequency: PWM frequency is determined by mcpwm_timer_config_t::resolution_hz, mcpwm_timer_config_t::count_mode and mcpwm_timer_config_t::period_ticks."

It's not clear what the steps are to actually change the frequency of the PWM while maintaining for example a steady 50% duty cycle.

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Sun Sep 24, 2023 6:12 am
by username
have you looked at any of the examples here ?
https://github.com/espressif/esp-idf/tr ... rals/mcpwm

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Tue Sep 26, 2023 3:51 am
by ChrisP_SD
Yes, thanks for the suggestion. I did look through all of those and ran the mcpwm_servo_control on my ESP32-S3 to generate a PWM signal. The mcpwm_bldc_speed_control is almost what I need except it modulates duty cycle to control speed and I need to modulate frequency. I know there has to be a simple way to dynamically update the frequency of the PWM signal like while a stepper motor is stepping, but I'm just not finding enough of a clue in these examples and don't know enough about the v5.0 implementation of PWM control. I understand how the comparator is used to change duty cycle for a constant frequency PWM output, but then how do you change the period_ticks of the mcpwm_timer?

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Wed Oct 11, 2023 8:10 am
by ESP_morris
Thanks for reporting. Yes, we will provide an API to change the period dynamically.

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Thu Oct 12, 2023 3:43 am
by ChrisP_SD
That would be awesome, thank you!

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Fri Oct 27, 2023 2:38 am
by ESP_morris
New API available on the master branch now: https://github.com/espressif/esp-idf/co ... 2bfd341a70

We will backport that API to release/v5.0 and release/v5.1 as well.

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Fri Nov 03, 2023 3:29 pm
by ChrisP_SD
awesome, will check it out, thank you!

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Sun Feb 11, 2024 5:12 am
by gseqbe
@esp_morris
Was this also backported to 5.1?
Thanks!

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Wed Apr 03, 2024 3:11 am
by mura_lb
ESP_morris wrote:
Wed Oct 11, 2023 8:10 am
Thanks for reporting. Yes, we will provide an API to change the period dynamically.
Whether API has been added, I found that the programming guide of 5.2.1 still does not mention setting (or adjusting) PWM operating frequency. Also, how to set up two sets of MCPWM in version 5?

Re: mcpwm_set_frequency() equivalent for esp-idf v5.0

Posted: Mon Apr 08, 2024 6:33 am
by mura_lb
ChrisP_SD wrote:
Fri Sep 22, 2023 4:59 am
Hello Community,
Does anyone have a working example of how to update the PWM frequency using the new mcpwm driver in IDF v5.0? This was simple in v4.4 using the provided mpcwm_set_frequency() function, however in IDF v5.0, mcpwm_set_frequency was removed. According to the migration documentation from https://docs.espressif.com/projects/esp ... erals.html

"mcpwm_set_frequency: PWM frequency is determined by mcpwm_timer_config_t::resolution_hz, mcpwm_timer_config_t::count_mode and mcpwm_timer_config_t::period_ticks."

It's not clear what the steps are to actually change the frequency of the PWM while maintaining for example a steady 50% duty cycle.
Version 5 of MCPWM is much more complex than version 4. Version 5 introduces a series of concepts including generators and comparators. resolution_hz should be the granularity of the specified timer and period_ticks/resolution_hz should be the PWM period.
As for whether period_tick is related to the set duty cycle, I don't understand it yet (and this process also needs to generate mcpwm_comparator_set_compare_value through comparator comparison value, and the input value is a uint32_t volume value. Simple guess period_tick can be understood as the graininess in PWM period, and the duty cycle of 50% should be period_tick*0.5). The new MCPWM is too hard to learn.
I looked at LEDC examples, and if you don't mind, LEDC can actually meet your needs.