Syncing Timers with MCPWM (v.4.3)
Posted: Thu Jan 11, 2024 8:02 pm
I'm trying to generate 6 synchronized PWM signals. The code below generates the 6 signals at the correct frequency and duty cycle (which I can verify on a scope) but each signal is only synchronized with its pair within the same timer. For instance, MCPWM0A and MCPWM0B on MCPWM_TIMER_0 are synced, but they are not synced to the two signals on timer 1 or timer 2. I'm using the legacy MCPWM library with v4.3, but I'm willing to switch to any version that works. I'm attempting to use sync_enable on lines 38 and 39, but I know that the third parameter (sync_sig) is not set up. I would like to use an internal sync to sync all 3 timers (such as syncing Timer 1 and Timer 2 to Timer 0). I've attached images from a scope comparing MCPWM0A and MCPWM2B.
https://docs.espressif.com/projects/esp ... tml#adjust
https://docs.espressif.com/projects/esp ... tml#adjust
Code: Select all
[env:esp32dev]
platform = espressif32@4.3
board = esp32dev
framework = espidf
- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/mcpwm.h"
- #include "soc/mcpwm_periph.h"
- void mcpwm_example_gpio_initialize(void)
- {
- printf("initializing mcpwm gpio...\n");
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, 18); // Channel 0: GPIO 18
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, 15); // Channel 1: GPIO 15
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1A, 21); // Channel 2: GPIO 21
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, 5); // Channel 3: GPIO 5
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM2A, 4); // Channel 4: GPIO 4
- mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM2B, 19); // Channel 5: GPIO 19
- }
- void app_main()
- {
- // Initialize MCPWM GPIO
- mcpwm_example_gpio_initialize();
- // Configuration for MCPWM
- mcpwm_config_t pwm_config = {
- .frequency = 800, // Frequency (Hz) for channels
- .cmpr_a = 50.0, // Duty cycle for channels 0
- .cmpr_b = 50.0, // Duty cycle for channels 1
- .counter_mode = MCPWM_UP_COUNTER,
- .duty_mode = MCPWM_DUTY_MODE_0
- };
- // Initialize MCPWM units and timers
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); // Timer 0 for channels 0, 1,
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config); // Timer 1 for channels 2, 3
- mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_2, &pwm_config); // Timer 2 for channels 3, 4
- //I need to synchronize all 3 of Unit 0's timers (i.e. sync timer1 and timer2 to timer0)
- mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, 1, 0);
- mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_2, 1, 0);
- }