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+
135157void * _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 },
0 commit comments