Skip to content

Commit 710edb4

Browse files
committed
more refactoring of the current sensing
1 parent d89fa26 commit 710edb4

File tree

6 files changed

+98
-99
lines changed

6 files changed

+98
-99
lines changed

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "esp32_adc_driver.h"
22

3+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
4+
#include "esp32_mcu.h"
35

4-
// maybe go to the fast ADC in future even outside the MCPWM (we'd have to remove the SOC_MCPWM_SUPPORTED flag)
5-
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
6+
#define SIMPLEFOC_ADC_ATTEN ADC_11db
7+
#define SIMPLEFOC_ADC_RES 12
68

79

810
#ifdef CONFIG_IDF_TARGET_ESP32 // if esp32 variant
@@ -38,6 +40,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
3840
{
3941
int8_t channel = digitalPinToAnalogChannel(pin);
4042
if(channel < 0){
43+
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
4144
return false;//not adc pin
4245
}
4346

@@ -76,7 +79,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
7679

7780

7881
// configure the ADCs in RTC mode
79-
// no real gain
82+
// no real gain - see if we do something with it later
8083
// void __configFastADCs(){
8184

8285
// SET_PERI_REG_MASK(SENS_SAR_READER1_CTRL_REG, SENS_SAR1_DATA_INV);
@@ -101,6 +104,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
101104
{
102105
int8_t channel = digitalPinToAnalogChannel(pin);
103106
if(channel < 0){
107+
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
104108
return false;//not adc pin
105109
}
106110

@@ -147,6 +151,7 @@ bool IRAM_ATTR adcInit(uint8_t pin){
147151

148152
int8_t channel = digitalPinToAnalogChannel(pin);
149153
if(channel < 0){
154+
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
150155
return false;//not adc pin
151156
}
152157

src/current_sense/hardware_specific/esp32/esp32_adc_driver.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32) && defined(SOC_MCPWM_SUPPORTED) && !defined(SIMPLEFOC_ESP32_USELEDC)
77

8-
9-
#define SIMPLEFOC_ADC_ATTEN ADC_11db
10-
#define SIMPLEFOC_ADC_RES 12
11-
128
/**
139
* Get ADC value for pin
1410
* @param pin - pin number

src/current_sense/hardware_specific/esp32/esp32_ledc_mcu.cpp

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
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

@@ -12,22 +10,23 @@
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
@@ -38,60 +37,6 @@
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
10348
float _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
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "../../hardware_api.h"
2+
#include "../../../drivers/hardware_api.h"
3+
4+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
5+
6+
#include "esp32_adc_driver.h"
7+
#include "esp32_mcu.h"
8+
9+
10+
/**
11+
* Inline adc reading implementation
12+
*/
13+
// function reading an ADC value and returning the read voltage
14+
float _readADCVoltageInline(const int pinA, const void* cs_params){
15+
uint32_t raw_adc = adcRead(pinA);
16+
return raw_adc * ((ESP32CurrentSenseParams*)cs_params)->adc_voltage_conv;
17+
}
18+
19+
// function reading an ADC value and returning the read voltage
20+
void* _configureADCInline(const void* driver_params, const int pinA, const int pinB, const int pinC){
21+
22+
ESP32CurrentSenseParams* params = new ESP32CurrentSenseParams {
23+
.pins = { pinA, pinB, pinC },
24+
.adc_voltage_conv = (_ADC_VOLTAGE)/(_ADC_RESOLUTION)
25+
};
26+
27+
// initialize the ADC pins
28+
// fail if the pin is not an ADC pin
29+
for (int i = 0; i < 3; i++){
30+
if(_isset(params->pins[i])){
31+
pinMode(params->pins[i], ANALOG);
32+
if(!adcInit(params->pins[i])) {
33+
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Failed to initialise ADC pin: "+String(params->pins[i]) + String(", maybe not an ADC pin?"));
34+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED;
35+
}
36+
}
37+
}
38+
39+
return params;
40+
}
41+
42+
43+
#endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef ESP32_MCU_CURRENT_SENSING_H
2+
#define ESP32_MCU_CURRENT_SENSING_H
3+
4+
5+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
6+
7+
#include "../../hardware_api.h"
8+
#include "../../../drivers/hardware_api.h"
9+
10+
#include "esp32_adc_driver.h"
11+
12+
13+
// esp32 current sense parameters
14+
typedef struct ESP32CurrentSenseParams {
15+
int pins[3];
16+
float adc_voltage_conv;
17+
int adc_buffer[3] = {};
18+
int buffer_index = 0;
19+
int no_adc_channels = 0;
20+
} ESP32CurrentSenseParams;
21+
22+
// macros for debugging wuing the simplefoc debug system
23+
#define SIMPLEFOC_ESP32_CS_DEBUG(str)\
24+
SimpleFOCDebug::println( "ESP32-CS"+String(tag)+ ": "+ String(str));\
25+
26+
#define CHECK_CS_ERR(func_call, message) \
27+
if ((func_call) != ESP_OK) { \
28+
SIMPLEFOC_ESP32_CS_DEBUG("ERROR - " + String(message)); \
29+
return SIMPLEFOC_CURRENT_SENSE_INIT_FAILED; \
30+
}
31+
32+
33+
#define _ADC_VOLTAGE 3.3f
34+
#define _ADC_RESOLUTION 4095.0f
35+
36+
#endif // ESP_H && ARDUINO_ARCH_ESP32
37+
#endif

0 commit comments

Comments
 (0)