1
1
#include " ../../hardware_api.h"
2
2
#include " ../../../drivers/hardware_api.h"
3
- #include " ../../../drivers/hardware_specific/esp32/esp32_driver_mcpwm.h"
4
- #include " ../../../drivers/hardware_specific/esp32/mcpwm_private.h"
5
3
6
4
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
7
5
12
10
#error SimpleFOC: ESP-IDF version 4 or lower detected. Please update to ESP-IDF 5.x and Arduino-esp32 3.0 (or higher)
13
11
#endif
14
12
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"
17
16
18
17
#include " driver/mcpwm_prelude.h"
19
18
#include " soc/mcpwm_reg.h"
20
19
#include " soc/mcpwm_struct.h"
21
20
22
- #include < soc/sens_reg.h>
23
- #include < soc/sens_struct.h>
24
21
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
26
26
27
27
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
28
28
#include " driver/gpio.h"
29
29
30
-
31
30
#ifdef CONFIG_IDF_TARGET_ESP32S3
32
31
#define DEBUGPIN 16
33
32
#define GPIO_NUM GPIO_NUM_16
38
37
39
38
#endif
40
39
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
- }
95
40
96
41
97
42
/* *
@@ -101,7 +46,7 @@ void* _configureADCInline(const void* driver_params, const int pinA, const int p
101
46
102
47
// function reading an ADC value and returning the read voltage
103
48
float _readADCVoltageLowSide (const int pin, const void * cs_params){
104
- ESP32MCPWMCurrentSenseParams * p = (ESP32MCPWMCurrentSenseParams *)cs_params;
49
+ ESP32CurrentSenseParams * p = (ESP32CurrentSenseParams *)cs_params;
105
50
int no_channel = 0 ;
106
51
for (int i=0 ; i < 3 ; i++){
107
52
if (!_isset (p->pins [i])) continue ;
@@ -132,7 +77,7 @@ void* _configureADCLowSide(const void* driver_params, const int pinA,const int p
132
77
}
133
78
134
79
135
- ESP32MCPWMCurrentSenseParams * params = new ESP32MCPWMCurrentSenseParams {};
80
+ ESP32CurrentSenseParams * params = new ESP32CurrentSenseParams {};
136
81
int no_adc_channels = 0 ;
137
82
138
83
// initialize the ADC pins
@@ -178,7 +123,7 @@ void* _driverSyncLowSide(void* driver_params, void* cs_params){
178
123
// - on_sync - sync event (not used with simplefoc)
179
124
auto cbs = mcpwm_timer_event_callbacks_t {
180
125
.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;
182
127
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG // debugging toggle pin to measure the time of the interrupt with oscilloscope
183
128
gpio_set_level (GPIO_NUM,1 ); // cca 250ns for on+off
184
129
#endif
0 commit comments