66#define SIMPLEFOC_ADC_ATTEN ADC_11db
77#define SIMPLEFOC_ADC_RES 12
88
9+ static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
910
1011#if CONFIG_IDF_TARGET_ESP32 // if esp32 variant
1112
@@ -38,6 +39,7 @@ void IRAM_ATTR __configFastADCs(){
3839 SET_PERI_REG_BITS (SENS_SAR_MEAS_WAIT1_REG, SENS_SAR_AMP_WAIT2, 0x1 , SENS_SAR_AMP_WAIT2_S);
3940 SET_PERI_REG_BITS (SENS_SAR_MEAS_WAIT2_REG, SENS_SAR_AMP_WAIT3, 0x1 , SENS_SAR_AMP_WAIT3_S);
4041 while (GET_PERI_REG_BITS2 (SENS_SAR_SLAVE_ADDR1_REG, 0x7 , SENS_MEAS_STATUS_S) != 0 ); // wait det_fsm==
42+
4143}
4244
4345
@@ -56,6 +58,10 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
5658
5759 // variable to hold the ADC value
5860 uint16_t value = 0 ;
61+
62+ // Protects against core migration, on single core chips this is noop.
63+ portENTER_CRITICAL (&spinlock);
64+
5965 // do the ADC conversion
6066 switch (adc_num){
6167 case 1 :
@@ -82,6 +88,8 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
8288 break ;
8389 }
8490
91+ portEXIT_CRITICAL (&spinlock);
92+
8593 // return value
8694 return value;
8795}
@@ -128,6 +136,10 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
128136
129137 // variable to hold the ADC value
130138 uint16_t value = 0 ;
139+
140+ // Protects against core migration, on single core chips this is noop.
141+ portENTER_CRITICAL (&spinlock);
142+
131143 // do the ADC conversion
132144 switch (adc_num){
133145 case 1 :
@@ -154,6 +166,8 @@ uint16_t IRAM_ATTR adcRead(uint8_t pin)
154166 break ;
155167 }
156168
169+ portEXIT_CRITICAL (&spinlock);
170+
157171 return value;
158172}
159173
0 commit comments