Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/current_sense/hardware_specific/esp32/esp32_adc_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define SIMPLEFOC_ADC_ATTEN ADC_11db
#define SIMPLEFOC_ADC_RES 12

static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;

#if CONFIG_IDF_TARGET_ESP32 // if esp32 variant

Expand Down Expand Up @@ -38,6 +39,7 @@ void IRAM_ATTR __configFastADCs(){
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT2, 0x1, SENS_SAR_AMP_WAIT2_S);
SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_SAR_AMP_WAIT3, 0x1, SENS_SAR_AMP_WAIT3_S);
while (GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR1_REG, 0x7, SENS_MEAS_STATUS_S) != 0); //wait det_fsm==

}


Expand All @@ -56,6 +58,10 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)

// variable to hold the ADC value
uint16_t value = 0;

//Protects against core migration, on single core chips this is noop.
portENTER_CRITICAL(&spinlock);

// do the ADC conversion
switch(adc_num){
case 1:
Expand All @@ -82,6 +88,8 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
break;
}

portEXIT_CRITICAL(&spinlock);

// return value
return value;
}
Expand Down Expand Up @@ -128,6 +136,10 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)

// variable to hold the ADC value
uint16_t value = 0;

//Protects against core migration, on single core chips this is noop.
portENTER_CRITICAL(&spinlock);

// do the ADC conversion
switch(adc_num){
case 1:
Expand All @@ -154,6 +166,8 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
break;
}

portEXIT_CRITICAL(&spinlock);

return value;
}

Expand Down