22
22
#include < soc/sens_reg.h>
23
23
#include < soc/sens_struct.h>
24
24
25
+ #define SIMPLEFOC_ESP32_INTERRUPT_DEBUG
26
+
25
27
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
26
28
#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
+
27
47
#endif
28
48
29
49
#define _ADC_VOLTAGE 3 .3f
@@ -132,9 +152,11 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
132
152
return params;
133
153
}
134
154
155
+
156
+
135
157
void * _driverSyncLowSide (void * driver_params, void * cs_params){
136
158
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
137
- pinMode (19 , OUTPUT);
159
+ pinMode (DEBUGPIN , OUTPUT);
138
160
#endif
139
161
ESP32MCPWMDriverParams *p = (ESP32MCPWMDriverParams*)driver_params;
140
162
mcpwm_timer_t * t = (mcpwm_timer_t *) p->timers [0 ];
@@ -150,22 +172,29 @@ void* _driverSyncLowSide(void* driver_params, void* cs_params){
150
172
// mcpwm_timer_event_callbacks_t can be used to set the callback
151
173
// for three timer events
152
174
// - on_full - low-side
153
- // - on_empty - high-side
175
+ // - on_empty - high-side
154
176
// - on_sync - sync event (not used with simplefoc)
155
177
auto cbs = mcpwm_timer_event_callbacks_t {
156
178
.on_full = [](mcpwm_timer_handle_t tim, const mcpwm_timer_event_data_t * edata, void * user_data){
157
179
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
160
182
#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
161
186
// increment buffer index
162
187
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
165
188
// so we are sampling one phase per call
166
189
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
169
198
#endif
170
199
return true ;
171
200
},
0 commit comments