Skip to content

Commit 36df015

Browse files
Merge pull request #491 from uLipe/feature/esp32_low_side_adc_iram
ESP32: move adc runtime reading functions to IRAM.
2 parents 7f45dca + a3991eb commit 36df015

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "esp32_mcu.h"
33
#include "esp32_adc_driver.h"
44

5-
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
5+
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
66
#define SIMPLEFOC_ADC_ATTEN ADC_11db
77
#define SIMPLEFOC_ADC_RES 12
88

@@ -12,12 +12,12 @@
1212
#include "soc/sens_reg.h"
1313

1414
// configure the ADCs in RTC mode
15-
// saves about 3us per call
15+
// saves about 3us per call
1616
// going from 12us to 9us
17-
//
17+
//
1818
// TODO: See if we need to configure both ADCs or we can just configure the one we'll use
1919
// For the moment we will configure both
20-
void __configFastADCs(){
20+
void IRAM_ATTR __configFastADCs(){
2121

2222
SIMPLEFOC_ESP32_CS_DEBUG("Configuring fast ADCs");
2323

@@ -46,9 +46,9 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
4646
int8_t channel = digitalPinToAnalogChannel(pin);
4747
if(channel < 0){
4848
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
49-
return false; //not adc pin
49+
return false; //not adc pin
5050
}
51-
51+
5252
// channels <= MAX_CHANNEL_NUM belong to ADC1
5353
// channels > MAX_CHANNEL_NUM belong to ADC2 (where the channel number is number-SOC_ADC_MAX_CHANNEL_NUM)
5454
uint8_t adc_num = (channel >= SOC_ADC_MAX_CHANNEL_NUM) ? 2 : 1;
@@ -65,7 +65,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
6565
SET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_START_SAR_M);
6666

6767
// wait for conversion
68-
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR) == 0);
68+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DONE_SAR) == 0);
6969
// read the value
7070
value = GET_PERI_REG_BITS2(SENS_SAR_MEAS_START1_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S);
7171
break;
@@ -76,7 +76,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
7676
SET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_START_SAR_M);
7777

7878
// wait for conversion
79-
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR) == 0);
79+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DONE_SAR) == 0);
8080
// read the value
8181
value = GET_PERI_REG_BITS2(SENS_SAR_MEAS_START2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S);
8282
break;
@@ -91,10 +91,10 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
9191
#include "soc/sens_reg.h"
9292

9393

94-
// configure the ADCs in RTC mode
94+
// configure the ADCs in RTC mode
9595
// no real gain - see if we do something with it later
9696
// void __configFastADCs(){
97-
97+
9898
// SET_PERI_REG_MASK(SENS_SAR_READER1_CTRL_REG, SENS_SAR1_DATA_INV);
9999
// SET_PERI_REG_MASK(SENS_SAR_READER2_CTRL_REG, SENS_SAR2_DATA_INV);
100100

@@ -137,7 +137,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
137137
SET_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_START_SAR_M);
138138

139139
// wait for conversion
140-
while (GET_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_DONE_SAR) == 0);
140+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_DONE_SAR) == 0);
141141
// read the value
142142
value = GET_PERI_REG_BITS2(SENS_SAR_MEAS1_CTRL2_REG, SENS_MEAS1_DATA_SAR, SENS_MEAS1_DATA_SAR_S);
143143
break;
@@ -148,7 +148,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
148148
SET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_START_SAR_M);
149149

150150
// wait for conversion
151-
while (GET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DONE_SAR) == 0);
151+
while (GET_PERI_REG_MASK(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DONE_SAR) == 0);
152152
// read the value
153153
value = GET_PERI_REG_BITS2(SENS_SAR_MEAS2_CTRL2_REG, SENS_MEAS2_DATA_SAR, SENS_MEAS2_DATA_SAR_S);
154154
break;
@@ -171,7 +171,7 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin){
171171
// configure the ADC for the pin
172172
bool IRAM_ATTR adcInit(uint8_t pin){
173173
static bool initialized = false;
174-
174+
175175
int8_t channel = digitalPinToAnalogChannel(pin);
176176
if(channel < 0){
177177
SIMPLEFOC_ESP32_CS_DEBUG("ERROR: Not ADC pin: "+String(pin));
@@ -186,7 +186,7 @@ bool IRAM_ATTR adcInit(uint8_t pin){
186186
if(! initialized){
187187
analogSetAttenuation(SIMPLEFOC_ADC_ATTEN);
188188
analogReadResolution(SIMPLEFOC_ADC_RES);
189-
}
189+
}
190190
pinMode(pin, ANALOG);
191191
analogRead(pin);
192192
analogSetPinAttenuation(pin, SIMPLEFOC_ADC_ATTEN);

src/current_sense/hardware_specific/esp32/esp32_mcpwm_mcu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444

4545
// function reading an ADC value and returning the read voltage
46-
float _readADCVoltageLowSide(const int pin, const void* cs_params){
46+
float IRAM_ATTR _readADCVoltageLowSide(const int pin, const void* cs_params){
4747
ESP32CurrentSenseParams* p = (ESP32CurrentSenseParams*)cs_params;
4848
int no_channel = 0;
4949
for(int i=0; i < 3; i++){
@@ -59,7 +59,7 @@ float _readADCVoltageLowSide(const int pin, const void* cs_params){
5959

6060

6161
// function configuring low-side current sensing
62-
void* _configureADCLowSide(const void* driver_params, const int pinA,const int pinB,const int pinC){
62+
void* IRAM_ATTR _configureADCLowSide(const void* driver_params, const int pinA,const int pinB,const int pinC){
6363
// check if driver timer is already running
6464
// fail if it is
6565
// the easiest way that I've found to check if timer is running
@@ -116,7 +116,7 @@ static bool IRAM_ATTR _mcpwmTriggerADCCallback(mcpwm_timer_handle_t tim, const m
116116
return true;
117117
}
118118

119-
void* _driverSyncLowSide(void* driver_params, void* cs_params){
119+
void* IRAM_ATTR _driverSyncLowSide(void* driver_params, void* cs_params){
120120
#ifdef SIMPLEFOC_ESP32_INTERRUPT_DEBUG
121121
pinMode(DEBUGPIN, OUTPUT);
122122
#endif

src/current_sense/hardware_specific/esp32/esp32_mcu.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
#if defined(ESP_H) && defined(ARDUINO_ARCH_ESP32)
44

55
/**
6-
* Inline adc reading implementation
6+
* Inline adc reading implementation
77
*/
88
// function reading an ADC value and returning the read voltage
9-
float _readADCVoltageInline(const int pinA, const void* cs_params){
9+
float IRAM_ATTR _readADCVoltageInline(const int pinA, const void* cs_params){
1010
uint32_t raw_adc = adcRead(pinA);
1111
return raw_adc * ((ESP32CurrentSenseParams*)cs_params)->adc_voltage_conv;
1212
}
1313

1414
// function reading an ADC value and returning the read voltage
15-
void* _configureADCInline(const void* driver_params, const int pinA, const int pinB, const int pinC){
15+
void* IRAM_ATTR _configureADCInline(const void* driver_params, const int pinA, const int pinB, const int pinC){
1616

1717
ESP32CurrentSenseParams* params = new ESP32CurrentSenseParams {
1818
.pins = { pinA, pinB, pinC },

0 commit comments

Comments
 (0)