Skip to content

Commit 46ebf64

Browse files
authored
Merge pull request #149 from samguns/dev
Modify PWM timer configuration according to esp32 SDK 2.0.2
2 parents cb4da9c + bc87456 commit 46ebf64

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/drivers/hardware_specific/esp32_mcu.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
124124
mcpwm_stop(mcpwm_unit, MCPWM_TIMER_2);
125125

126126
// manual configuration due to the lack of config flexibility in mcpwm_init()
127-
mcpwm_num->clk_cfg.prescale = 0;
127+
mcpwm_num->clk_cfg.clk_prescale = 0;
128128
// calculate prescaler and period
129129
// step 1: calculate the prescaler using the default pwm resolution
130130
// prescaler = bus_freq / (pwm_freq * default_pwm_res) - 1
@@ -140,35 +140,37 @@ void _configureTimerFrequency(long pwm_frequency, mcpwm_dev_t* mcpwm_num, mcpwm
140140
resolution_corrected = _constrain(resolution_corrected, _PWM_RES_MIN, _PWM_RES_MAX);
141141

142142
// set prescaler
143-
mcpwm_num->timer[0].period.prescale = prescaler;
144-
mcpwm_num->timer[1].period.prescale = prescaler;
145-
mcpwm_num->timer[2].period.prescale = prescaler;
143+
mcpwm_num->timer[0].timer_cfg0.timer_prescale = prescaler;
144+
mcpwm_num->timer[1].timer_cfg0.timer_prescale = prescaler;
145+
mcpwm_num->timer[2].timer_cfg0.timer_prescale = prescaler;
146146
_delay(1);
147147
//set period
148-
mcpwm_num->timer[0].period.period = resolution_corrected;
149-
mcpwm_num->timer[1].period.period = resolution_corrected;
150-
mcpwm_num->timer[2].period.period = resolution_corrected;
148+
mcpwm_num->timer[0].timer_cfg0.timer_period = resolution_corrected;
149+
mcpwm_num->timer[1].timer_cfg0.timer_period = resolution_corrected;
150+
mcpwm_num->timer[2].timer_cfg0.timer_period = resolution_corrected;
151151
_delay(1);
152-
mcpwm_num->timer[0].period.upmethod = 0;
153-
mcpwm_num->timer[1].period.upmethod = 0;
154-
mcpwm_num->timer[2].period.upmethod = 0;
152+
mcpwm_num->timer[0].timer_cfg0.timer_period_upmethod = 0;
153+
mcpwm_num->timer[1].timer_cfg0.timer_period_upmethod = 0;
154+
mcpwm_num->timer[2].timer_cfg0.timer_period_upmethod = 0;
155155
_delay(1);
156156
// _delay(1);
157157
//restart the timers
158158
mcpwm_start(mcpwm_unit, MCPWM_TIMER_0);
159159
mcpwm_start(mcpwm_unit, MCPWM_TIMER_1);
160160
mcpwm_start(mcpwm_unit, MCPWM_TIMER_2);
161161
_delay(1);
162-
// Cast here because MCPWM_SELECT_SYNC_INT0 (1) is not defined
163-
// in the default Espressif MCPWM headers. The correct const may be used
164-
// when https://github.com/espressif/esp-idf/issues/5429 is resolved.
165-
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_0, (mcpwm_sync_signal_t)1, 0);
166-
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_1, (mcpwm_sync_signal_t)1, 0);
167-
mcpwm_sync_enable(mcpwm_unit, MCPWM_TIMER_2, (mcpwm_sync_signal_t)1, 0);
168-
_delay(1);
169-
mcpwm_num->timer[0].sync.out_sel = 1;
170-
_delay(1);
171-
mcpwm_num->timer[0].sync.out_sel = 0;
162+
163+
mcpwm_sync_config_t sync_conf = {
164+
.sync_sig = MCPWM_SELECT_TIMER0_SYNC,
165+
.timer_val = 0,
166+
.count_direction = MCPWM_TIMER_DIRECTION_UP
167+
};
168+
mcpwm_sync_configure(mcpwm_unit, MCPWM_TIMER_0, &sync_conf);
169+
mcpwm_sync_configure(mcpwm_unit, MCPWM_TIMER_1, &sync_conf);
170+
mcpwm_sync_configure(mcpwm_unit, MCPWM_TIMER_2, &sync_conf);
171+
172+
// TIMER0 has itself as sync source, route this sync source directly to sync output for the other two
173+
mcpwm_set_timer_sync_output(mcpwm_unit, MCPWM_TIMER_0, MCPWM_SWSYNC_SOURCE_SYNCIN);
172174
}
173175

174176
// function setting the high pwm frequency to the supplied pins

0 commit comments

Comments
 (0)