mcpwm peripheral issues

vastar
Posts: 3
Joined: Sat Dec 30, 2017 7:36 pm

mcpwm peripheral issues

Postby vastar » Tue Mar 27, 2018 7:10 pm

Hi all,
I'm a switch mode power supply designer. I hope Espressif don't produce esp wroom 32 chips for only makers :)
Our traditional way is using arm mcu , if we need iot our way is arm mcu + ble or wifi mcu combined. But when we meet esp32 there are a lot of good things inside the chip. 2 core, 160 mhz, all peripherals what we need and wifi + ble all in one.

But we have some pwm issues. Pwm is most important thing for us ;)

I'm using esp-idf + eclipse + windows 10 and mcpwm api on the esp-idf

1) We need higher pwm frequencies. Above 50kHz

2) Above the 1kHz esp output is not stable. Real pin frequency is not what I set in the software

3) mcpwm page is not good documented in the esp-idf programming guide
http://esp-idf.readthedocs.io/en/latest ... mcpwm.html

4) ESP32 Technical Reference Manual V3.0 page: 391
https://www.espressif.com/sites/default ... ual_en.pdf
160Mhz input clock frequency of mcpwm module. So it is very good in theory.
160Mhz clock pulse ----> 8 bit prescaler ----> 16 bit configurable timer ----> pwm_pin

5) In the ESP32 Technical Reference Manual V3.0 page: 397, there are registers for pwm. But how can we reach and edit this registers in safe way?

6) My basic code:

Code: Select all

void mcpwm_gpio_config (){
	printf("PWM Module pin attachments......\n");
	mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, PWM0A_PIN);    //Set GPIO 15 as PWM0A
}

void mcpwm_internal_config (){
	printf("Configuring Initial Parameters of mcpwm0......\n");
	mcpwm_config_t pwm_config;
	pwm_config.frequency = 1000;    //frequency
	pwm_config.cmpr_a = 0;    //duty cycle of PWMxA = 0
	pwm_config.cmpr_b = 0;    //duty cycle of PWMxb = 0
	pwm_config.counter_mode = MCPWM_UP_COUNTER;
	pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
	mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);    //Configure PWM0A & PWM0B with above settings
}
.............

Code: Select all

void app_main() {
	mcpwm_gpio_config();
	mcpwm_internal_config();

	gpio_pad_select_gpio(GPIO_NUM_2);
	gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);

	uint8_t i = 0;
	float dutyCounter = 0.0;

	while (1) {

		for (i = 0; i < 100; i++){
			gpio_set_level(GPIO_NUM_2, 1);
			dutyCounter = i * 1.0;
			mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, dutyCounter);
			printf("Duty = %f",dutyCounter);
			printf("\n");
			vTaskDelay(10);
			gpio_set_level(GPIO_NUM_2, 0);
		}
	}
}
I have Rohde&Schwarz RTB2004 oscilloscope

Results:
pwm_config.frequency = 1000; real pin frequency = 999,01Hz
pwm_config.frequency = 25000; real pin frequency = 24.390Hz
pwm_config.frequency = 50000; real pin frequency = 47.619Hz
pwm_config.frequency = 75000; real pin frequency = 71.429Hz
pwm_config.frequency = 150000; real pin frequency = 142.857Hz
pwm_config.frequency = 225000; real pin frequency = 200.000Hz
pwm_config.frequency = 300000; real pin frequency = 250.000Hz
pwm_config.frequency = 500000; real pin frequency = 333.333,33Hz
pwm_config.frequency = 1000000; real pin frequency = nothing on the oscilloscope, and there is no compile error

I need help and advice and all kind of helpfull things
Best regards.

vastar
Posts: 3
Joined: Sat Dec 30, 2017 7:36 pm

Re: mcpwm peripheral issues

Postby vastar » Thu Apr 05, 2018 8:43 am

Hi all,
130+ views but nobody answer. Are there any forum member who are Espressif's engineer?
If I do a mistake about writing code pls show me the right way.

Best regards.

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: mcpwm peripheral issues

Postby loboris » Thu Apr 05, 2018 12:43 pm

You can also use LEDC peripheral to generate PWM output up to 20 MHz (40MHz with duty cycle of 50%).
Tested and works very well.

michprev
Posts: 92
Joined: Fri Aug 04, 2017 8:57 pm

Re: mcpwm peripheral issues

Postby michprev » Tue Apr 17, 2018 10:55 am

Take a look at https://github.com/espressif/esp-idf/issues/1101. It is better to write your own driver for mcpwm.

EDIT:
You can MCPWM registers directly via MCPWM object in https://github.com/espressif/esp-idf/bl ... m_struct.h.You still need to assign pins to MCPWM signals. For this you can use mcpwm_gpio_init or mcpwm_set_pin functions (http://esp-idf.readthedocs.io/en/latest ... signals_ti).

Who is online

Users browsing this forum: No registered users and 15 guests