Skip to content

Commit 8dae6a3

Browse files
committed
ESP32: Use MCPWM unit 1 istead of 0 as we use the MCPWM0 for the sniffer and there can only be one interrupt routine per MCPWM (at least in the ESP32 API)
1 parent a69139b commit 8dae6a3

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

DCCRMT.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,29 @@ void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) {
133133
if (tt) tt->RMTinterrupt();
134134
if (channel == 0) {
135135
DCCTimer::updateMinimumFreeMemoryISR(0);
136-
MCPWM0.operators[0].gen_stmp_cfg.gen_a_upmethod = 4; // bit 4 means "on sync"
136+
MCPWM1.operators[0].gen_stmp_cfg.gen_a_upmethod = 4; // bit 4 means "on sync"
137137
cutoutCounter = 0;
138138
// not needed here, we keep it enabled all the time
139-
//MCPWM0.int_ena.timer0_tez_int_ena = 1; // Enable interrupt on TEZ
139+
//MCPWM1.int_ena.timer0_tez_int_ena = 1; // Enable interrupt on TEZ
140140
__digitalWrite(13 /*BRKA*/, 0);
141-
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_GEN_A, 464);
141+
mcpwm_set_duty_in_us(MCPWM_UNIT_1, MCPWM_TIMER_0, MCPWM_GEN_A, 464);
142142
mcpwm_sync_config_t sync_conf = {
143143
.sync_sig = MCPWM_SELECT_GPIO_SYNC0,
144144
.timer_val = 939, // in promille of all values
145145
.count_direction = MCPWM_TIMER_DIRECTION_UP,
146146
};
147-
mcpwm_sync_configure(MCPWM_UNIT_0, MCPWM_TIMER_0, &sync_conf);
147+
mcpwm_sync_configure(MCPWM_UNIT_1, MCPWM_TIMER_0, &sync_conf);
148148
}
149149
}
150150

151151
// This intrrupt is called on sync which is configured on mcpwmPulseOn()
152152
static void IRAM_ATTR mcpwmIsrHandler(void* arg) {
153-
if (MCPWM0.int_st.timer0_tez_int_st) {
154-
MCPWM0.int_clr.timer0_tez_int_clr = 1;
153+
if (MCPWM1.int_st.timer0_tez_int_st) {
154+
MCPWM1.int_clr.timer0_tez_int_clr = 1;
155155
if (cutoutCounter == 1) {
156156
__digitalWrite(13 /*BRKA*/, 1);
157157
// this does not work as the enable does not go into effect immidiately
158-
// MCPWM0.int_ena.timer0_tez_int_ena = 0; // Disable interrupt on TEZ
158+
// MCPWM1.int_ena.timer0_tez_int_ena = 0; // Disable interrupt on TEZ
159159
//cutoutCounter = 0;
160160
}
161161
if (cutoutCounter == 2) {
@@ -166,16 +166,16 @@ static void IRAM_ATTR mcpwmIsrHandler(void* arg) {
166166
cutoutCounter++;
167167
}
168168
if (cutoutCounter < 2) {
169-
MCPWM0.operators[0].gen_stmp_cfg.gen_a_upmethod = 1; // bit 1 means "TEZ = timer zero"
170-
mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_GEN_A, 0);
171-
mcpwm_sync_disable(MCPWM_UNIT_0, MCPWM_TIMER_0);
169+
MCPWM1.operators[0].gen_stmp_cfg.gen_a_upmethod = 1; // bit 1 means "TEZ = timer zero"
170+
mcpwm_set_duty_in_us(MCPWM_UNIT_1, MCPWM_TIMER_0, MCPWM_GEN_A, 0);
171+
mcpwm_sync_disable(MCPWM_UNIT_1, MCPWM_TIMER_0);
172172
cutoutCounter++;
173173
}
174174
}
175175
}
176176

177177

178-
// Configure MCPWM unit 0.
178+
// Configure MCPWM unit 1.
179179
// https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/api-reference/peripherals/mcpwm.html
180180
// Connect Operator 0 output A PWM0A to pin XXX (this should go to brake then)
181181
// Connect Timer0 input sync 0 SYNC0 from pin YYY (this should connect to RMT channel 0 output
@@ -194,8 +194,8 @@ static void IRAM_ATTR mcpwmPulseOn() {
194194
.duty_mode = MCPWM_DUTY_MODE_0,
195195
.counter_mode = MCPWM_UP_COUNTER,
196196
};
197-
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
198-
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, 26 /*PWMA?*/);
197+
mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_0, &pwm_config);
198+
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM0A, 26 /*PWMA?*/);
199199

200200
mcpwm_sync_config_t sync_conf = {
201201
.sync_sig = MCPWM_SELECT_GPIO_SYNC0,
@@ -204,15 +204,15 @@ static void IRAM_ATTR mcpwmPulseOn() {
204204
};
205205
// default is pos edge trigger, handled by mcpwm_sync_invert_gpio_synchro()
206206
// if neg edge needed
207-
mcpwm_sync_configure(MCPWM_UNIT_0, MCPWM_TIMER_0, &sync_conf);
208-
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_SYNC_0, 0 /*MAIN DIRA pin as placeholder*/ );
207+
mcpwm_sync_configure(MCPWM_UNIT_1, MCPWM_TIMER_0, &sync_conf);
208+
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM_SYNC_0, 0 /*MAIN DIRA pin as placeholder*/ );
209209
// use internal pin instead of 5 /*DIRA external shield aka DIRC*/.
210-
// mux name of sync 0 input: PWM0_SYNC0_IN_IDX
210+
// mux name of sync 0 input: PWM1_SYNC0_IN_IDX
211211
// mux name of RMT output: RMT_SIG_OUT0_IDX
212212
// mux empty input mirror: SIG_IN_FUNC227_IDX
213213
// https://docs.espressif.com/projects/rust/esp-hal/1.0.0-beta.0/esp32/src/esp_hal/soc/esp32/psram.rs.html
214214
gpio_matrix_out(30 /*unused-silicon*/, RMT_SIG_OUT0_IDX, false, false);
215-
gpio_matrix_in (30 /*unused-silicon*/, PWM0_SYNC0_IN_IDX, false);
215+
gpio_matrix_in (30 /*unused-silicon*/, PWM1_SYNC0_IN_IDX, false);
216216
}
217217

218218

@@ -308,13 +308,13 @@ RMTChannel::RMTChannel(pinpair pins, bool isMain) {
308308
// test with mcpwm
309309
mcpwmPulseOn();
310310
ESP_ERROR_CHECK(mcpwm_isr_register(
311-
MCPWM_UNIT_0, mcpwmIsrHandler, NULL,
311+
MCPWM_UNIT_1, mcpwmIsrHandler, NULL,
312312
ESP_INTR_FLAG_LOWMED|
313313
ESP_INTR_FLAG_SHARED,
314314
/*ESP_INTR_FLAG_IRAM,*/
315315
NULL)); //Set ISR Handler
316-
MCPWM0.int_clr.timer0_tez_int_clr = 1; // Clear the interrupt flag
317-
MCPWM0.int_ena.timer0_tez_int_ena = 1; // Enable interrupt on TEZ
316+
MCPWM1.int_clr.timer0_tez_int_clr = 1; // Clear the interrupt flag
317+
MCPWM1.int_ena.timer0_tez_int_ena = 1; // Enable interrupt on TEZ
318318

319319
}
320320

@@ -415,7 +415,7 @@ bool RMTChannel::addRCPin(int16_t brakePin) {
415415
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpioNum], PIN_FUNC_GPIO);
416416
err = gpio_set_direction(gpioNum, GPIO_MODE_OUTPUT);
417417
if (err != ESP_OK) return false;
418-
gpio_matrix_out(gpioNum, PWM0_OUT0A_IDX, inverted, 0);
418+
gpio_matrix_out(gpioNum, PWM1_OUT0A_IDX, inverted, 0);
419419
if (err != ESP_OK) return false;
420420
return true;
421421

0 commit comments

Comments
 (0)