11#include " ../../hardware_api.h"
22#include " ../../../drivers/hardware_api.h"
3- #include " ../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
4- #include " ../../../drivers/hardware_specific/esp32/mcpwm_private.h"
53
64#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
75
1210#error SimpleFOC: ESP-IDF version 4 or lower detected. Please update to ESP-IDF 5.x and Arduino-esp32 3.0 (or higher)
1311#endif
1412
15-
16- #include " esp32_adc_driver.h"
13+ #include " esp32_mcu.cpp"
14+ #include " ../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
15+ #include " ../../../drivers/hardware_specific/esp32/mcpwm_private.h"
1716
1817#include " driver/mcpwm_prelude.h"
1918#include " soc/mcpwm_reg.h"
2019#include " soc/mcpwm_struct.h"
2120
22- #include < soc/sens_reg.h>
23- #include < soc/sens_struct.h>
2421
25- #define SIMPLEFOC_ESP32_INTERRUPT_DEBUG
22+
23+ // adding a debug toggle pin to measure the time of the interrupt with oscilloscope
24+
25+ // #define SIMPLEFOC_ESP32_INTERRUPT_DEBUG
2626
2727#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
2828#include " driver/gpio.h"
2929
30-
3130#ifdef CONFIG_IDF_TARGET_ESP32S3
3231#define DEBUGPIN 16
3332#define GPIO_NUM GPIO_NUM_16
3837
3938#endif
4039
41- #define _ADC_VOLTAGE 3 .3f
42- #define _ADC_RESOLUTION 4095 .0f
43-
44-
45-
46- #define SIMPLEFOC_ESP32_CS_DEBUG (str )\
47- SIMPLEFOC_ESP32_DEBUG (" CS" , str);\
48-
49- #define CHECK_CS_ERR (func_call, message ) \
50- if ((func_call) != ESP_OK) { \
51- SIMPLEFOC_ESP32_CS_DEBUG (" ERROR - " + String (message)); \
52- return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED; \
53- }
54-
55- typedef struct ESP32MCPWMCurrentSenseParams {
56- int pins[3 ];
57- float adc_voltage_conv;
58- int adc_buffer[3 ] = {};
59- int buffer_index = 0 ;
60- int no_adc_channels = 0 ;
61- } ESP32MCPWMCurrentSenseParams;
62-
63-
64- /* *
65- * Inline adc reading implementation
66- */
67- // function reading an ADC value and returning the read voltage
68- float _readADCVoltageInline (const int pinA, const void * cs_params){
69- uint32_t raw_adc = adcRead (pinA);
70- return raw_adc * ((ESP32MCPWMCurrentSenseParams*)cs_params)->adc_voltage_conv ;
71- }
72-
73- // function reading an ADC value and returning the read voltage
74- void * _configureADCInline (const void * driver_params, const int pinA, const int pinB, const int pinC){
75-
76- ESP32MCPWMCurrentSenseParams* params = new ESP32MCPWMCurrentSenseParams {
77- .pins = { pinA, pinB, pinC },
78- .adc_voltage_conv = (_ADC_VOLTAGE)/(_ADC_RESOLUTION)
79- };
80-
81- // initialize the ADC pins
82- // fail if the pin is not an ADC pin
83- for (int i = 0 ; i < 3 ; i++){
84- if (_isset (params->pins [i])){
85- pinMode (params->pins [i], ANALOG);
86- if (!adcInit (params->pins [i])) {
87- SIMPLEFOC_ESP32_CS_DEBUG (" ERROR: Failed to initialise ADC pin: " +String (params->pins [i]) + String (" , maybe not an ADC pin?" ));
88- return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
89- }
90- }
91- }
92-
93- return params;
94- }
9540
9641
9742/* *
@@ -101,7 +46,7 @@ void* _configureADCInline(const void* driver_params, const int pinA, const int p
10146
10247// function reading an ADC value and returning the read voltage
10348float _readADCVoltageLowSide (const int pin, const void * cs_params){
104- ESP32MCPWMCurrentSenseParams * p = (ESP32MCPWMCurrentSenseParams *)cs_params;
49+ ESP32CurrentSenseParams * p = (ESP32CurrentSenseParams *)cs_params;
10550 int no_channel = 0 ;
10651 for (int i=0 ; i < 3 ; i++){
10752 if (!_isset (p->pins [i])) continue ;
@@ -132,7 +77,7 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
13277 }
13378
13479
135- ESP32MCPWMCurrentSenseParams * params = new ESP32MCPWMCurrentSenseParams {};
80+ ESP32CurrentSenseParams * params = new ESP32CurrentSenseParams {};
13681 int no_adc_channels = 0 ;
13782
13883 // initialize the ADC pins
@@ -178,7 +123,7 @@ void* _driverSyncLowSide(void* driver_params, void* cs_params){
178123 // - on_sync - sync event (not used with simplefoc)
179124 auto cbs = mcpwm_timer_event_callbacks_t {
180125 .on_full = [](mcpwm_timer_handle_t tim, const mcpwm_timer_event_data_t * edata, void * user_data){
181- ESP32MCPWMCurrentSenseParams *p = (ESP32MCPWMCurrentSenseParams *)user_data;
126+ ESP32CurrentSenseParams *p = (ESP32CurrentSenseParams *)user_data;
182127#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope
183128 gpio_set_level (GPIO_NUM,1 ); // cca 250ns for on+off
184129#endif
0 commit comments