mcpwm_set_frequency() equivalent for esp-idf v5.0

ChrisP_SD
Posts: 4
Joined: Fri Sep 22, 2023 12:27 am

mcpwm_set_frequency() equivalent for esp-idf v5.0

Postby ChrisP_SD » 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.

username
Posts: 538
Joined: Thu May 03, 2018 1:18 pm

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

Postby username » Sun Sep 24, 2023 6:12 am

have you looked at any of the examples here ?
https://github.com/espressif/esp-idf/tr ... rals/mcpwm

ChrisP_SD
Posts: 4
Joined: Fri Sep 22, 2023 12:27 am

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

Postby ChrisP_SD » Tue Sep 26, 2023 3:51 am

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?

ESP_morris
Posts: 290
Joined: Wed Sep 05, 2018 6:23 am

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

Postby ESP_morris » Wed Oct 11, 2023 8:10 am

Thanks for reporting. Yes, we will provide an API to change the period dynamically.

ChrisP_SD
Posts: 4
Joined: Fri Sep 22, 2023 12:27 am

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

Postby ChrisP_SD » Thu Oct 12, 2023 3:43 am

That would be awesome, thank you!

ESP_morris
Posts: 290
Joined: Wed Sep 05, 2018 6:23 am

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

Postby ESP_morris » Fri Oct 27, 2023 2:38 am

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.

ChrisP_SD
Posts: 4
Joined: Fri Sep 22, 2023 12:27 am

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

Postby ChrisP_SD » Fri Nov 03, 2023 3:29 pm

awesome, will check it out, thank you!

gseqbe
Posts: 2
Joined: Sun Feb 11, 2024 5:06 am

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

Postby gseqbe » Sun Feb 11, 2024 5:12 am

@esp_morris
Was this also backported to 5.1?
Thanks!

mura_lb
Posts: 12
Joined: Fri Jul 16, 2021 3:06 am

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

Postby mura_lb » Wed Apr 03, 2024 3:11 am

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?

mura_lb
Posts: 12
Joined: Fri Jul 16, 2021 3:06 am

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

Postby mura_lb » Mon Apr 08, 2024 6:33 am

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.

Who is online

Users browsing this forum: Google [Bot] and 86 guests