Skip to content

Commit 52e8d9d

Browse files
committed
added the support for esp32s3 to read all the current phases in one interrupt
1 parent 63fda20 commit 52e8d9d

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,28 @@
2222
#include <soc/sens_reg.h>
2323
#include <soc/sens_struct.h>
2424

25+
#define SIMPLEFOC_ESP32_INTERRUPT_DEBUG
26+
2527
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
2628
#include "driver/gpio.h"
29+
30+
31+
// if the MCU is not ESP32S3, the ADC read time is too long to
32+
// sample all three phase currents in one interrupt
33+
// so we will sample one phase per interrupt
34+
#ifdef CONFIG_IDF_TARGET_ESP32S3
35+
#define SIMPLEFOC_SAMPLE_ONCE_PER_INTERRUPT
36+
#endif
37+
38+
39+
#ifdef CONFIG_IDF_TARGET_ESP32S3
40+
#define DEBUGPIN 16
41+
#define GPIO_NUM GPIO_NUM_16
42+
#else
43+
#define DEBUGPIN 19
44+
#define GPIO_NUM GPIO_NUM_19
45+
#endif
46+
2747
#endif
2848

2949
#define _ADC_VOLTAGE 3.3f
@@ -132,9 +152,11 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
132152
return params;
133153
}
134154

155+
156+
135157
void* _driverSyncLowSide(void* driver_params, void* cs_params){
136158
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
137-
pinMode(19, OUTPUT);
159+
pinMode(DEBUGPIN, OUTPUT);
138160
#endif
139161
ESP32MCPWMDriverParams *p = (ESP32MCPWMDriverParams*)driver_params;
140162
mcpwm_timer_t* t = (mcpwm_timer_t*) p->timers[0];
@@ -150,22 +172,29 @@ void* _driverSyncLowSide(void* driver_params, void* cs_params){
150172
// mcpwm_timer_event_callbacks_t can be used to set the callback
151173
// for three timer events
152174
// - on_full - low-side
153-
// - on_empty - high-side
175+
// - on_empty - high-side
154176
// - on_sync - sync event (not used with simplefoc)
155177
auto cbs = mcpwm_timer_event_callbacks_t{
156178
.on_full = [](mcpwm_timer_handle_t tim, const mcpwm_timer_event_data_t* edata, void* user_data){
157179
ESP32MCPWMCurrentSenseParams *p = (ESP32MCPWMCurrentSenseParams*)user_data;
158-
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
159-
gpio_set_level(GPIO_NUM_19,1); //cca 250ns for on+off
180+
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope
181+
gpio_set_level(GPIO_NUM,1); //cca 250ns for on+off
160182
#endif
183+
184+
#ifdef SIMPLEFOC_SAMPLE_ONCE_PER_INTERRUPT // sample the phase currents one at a time
185+
// ex. ESP32's adc read takes around 10us which is very long
161186
// increment buffer index
162187
p->buffer_index = (p->buffer_index + 1) % p->no_adc_channels;
163-
// sample the phase currents one at a time
164-
// adc read takes around 10us which is very long
165188
// so we are sampling one phase per call
166189
p->adc_buffer[p->buffer_index] = adcRead(p->pins[p->buffer_index]);
167-
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
168-
gpio_set_level(GPIO_NUM_19,0); //cca 250ns for on+off
190+
#else // sample all available phase currents at once
191+
// ex. ESP32S3's adc read takes around 1us which is good enough
192+
for(int i=0; i < p->no_adc_channels; i++)
193+
p->adc_buffer[p->buffer_index] = adcRead(p->pins[p->buffer_index]);
194+
#endif
195+
196+
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope
197+
gpio_set_level(GPIO_NUM,0); //cca 250ns for on+off
169198
#endif
170199
return true;
171200
},

src/drivers/hardware_specific/esp32/esp32_driver_mcpwm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ void* _configurePinsMCPWM(long pwm_frequency, int mcpwm_group, int timer_no, int
489489

490490
// function setting the duty cycle to the MCPWM pin
491491
void _setDutyCycle(mcpwm_cmpr_handle_t cmpr, uint32_t mcpwm_period, float duty_cycle){
492-
float duty = constrain(duty_cycle, 0.0, 1.0);
492+
float duty = _constrain(duty_cycle, 0.0, 1.0);
493493
mcpwm_comparator_set_compare_value(cmpr, (uint32_t)(mcpwm_period*duty));
494494
}
495495

0 commit comments

Comments
 (0)