Skip to content

Commit

Permalink
Merge pull request #9 from Konstantin-G/ESP32C3
Browse files Browse the repository at this point in the history
added ESP32C3 support
  • Loading branch information
madhephaestus authored May 30, 2022
2 parents d878d18 + 94e58f8 commit 7c0a4be
Showing 1 changed file with 149 additions and 1 deletion.
150 changes: 149 additions & 1 deletion ESP32AnalogRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ float ESP32AnalogRead::readVoltage()
ADC1_CHANNEL_7, !< ADC1 channel 7 is GPIO35
ADC1_CHANNEL_MAX,
} adc1_channel_t;
typedef enum {
ADC2_CHANNEL_0 = 0, !< ADC2 channel 0 is GPIO4
ADC2_CHANNEL_1, !< ADC2 channel 1 is GPIO0
Expand Down Expand Up @@ -282,6 +281,81 @@ uint32_t ESP32AnalogRead::readMiliVolts()
}
voltage = esp_adc_cal_raw_to_voltage(raw, &characteristics);
return voltage;

#elif defined(CONFIG_IDF_TARGET_ESP32C3)
if (!attached)
return 0;
analogRead(myPin);
// Configure ADC
adc_unit_t unit;
if (myPin < 5)
{
adc1_config_width(ADC_WIDTH_12Bit);
adc1_channel_t chan = ADC1_CHANNEL_0;
unit = ADC_UNIT_1;
switch (myPin)
{

case 0:
chan = ADC1_CHANNEL_0;
break;
case 1:
chan = ADC1_CHANNEL_1;
break;
case 2:
chan = ADC1_CHANNEL_2;
break;
case 3:
chan = ADC1_CHANNEL_3;
break;
case 4:
chan = ADC1_CHANNEL_4;
break;
}
adc1_channel = chan;
adc1_config_channel_atten(chan, ADC_ATTEN_11db);
}
else
{
adc2_channel_t chan = ADC2_CHANNEL_0;
unit = ADC_UNIT_2;
switch (myPin)
{
case 5:
chan = ADC2_CHANNEL_0;
break;
}
adc2_channel = chan;
adc2_config_channel_atten(chan, ADC_ATTEN_11db);
}
// Calculate ADC characteristics i.e. gain and offset factors
#ifdef ESP_IDF_VERSION
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(3, 1, 0)
esp_adc_cal_characterize(unit,
ADC_ATTEN_DB_11,
ADC_WIDTH_BIT_12,
V_REF,
&characteristics);
#else
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, &characteristics);
#endif
#else
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, &characteristics);
#endif

int raw = 0;
uint32_t voltage = 0;
// Read ADC and obtain result in mV
if (unit == ADC_UNIT_1)
{
raw = adc1_get_raw(adc1_channel);
}
else
{
adc2_get_raw(adc2_channel, ADC_WIDTH_12Bit, &raw);
}
voltage = esp_adc_cal_raw_to_voltage(raw, &characteristics);
return voltage;
#endif
}

Expand Down Expand Up @@ -510,5 +584,79 @@ uint16_t ESP32AnalogRead::readRaw()
adc2_get_raw(adc2_channel, ADC_WIDTH_12Bit, &raw);
}
return raw;

#elif defined(CONFIG_IDF_TARGET_ESP32C3)
if (!attached)
return 0;
analogRead(myPin);
// Configure ADC
adc_unit_t unit;
if (myPin < 5)
{
adc1_config_width(ADC_WIDTH_12Bit);
adc1_channel_t chan = ADC1_CHANNEL_0;
unit = ADC_UNIT_1;
switch (myPin)
{

case 0:
chan = ADC1_CHANNEL_0;
break;
case 1:
chan = ADC1_CHANNEL_1;
break;
case 2:
chan = ADC1_CHANNEL_2;
break;
case 3:
chan = ADC1_CHANNEL_3;
break;
case 4:
chan = ADC1_CHANNEL_4;
break;
}
adc1_channel = chan;
adc1_config_channel_atten(chan, ADC_ATTEN_11db);
}
else
{
adc2_channel_t chan = ADC2_CHANNEL_0;
unit = ADC_UNIT_2;
switch (myPin)
{
case 5:
chan = ADC2_CHANNEL_0;
break;
}
adc2_channel = chan;
adc2_config_channel_atten(chan, ADC_ATTEN_11db);
}
// Calculate ADC characteristics i.e. gain and offset factors
#ifdef ESP_IDF_VERSION
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(3, 1, 0)
esp_adc_cal_characterize(unit,
ADC_ATTEN_DB_11,
ADC_WIDTH_BIT_12,
V_REF,
&characteristics);
#else
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, &characteristics);
#endif
#else
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, &characteristics);
#endif

int raw = 0;
uint32_t voltage = 0;
// Read ADC and obtain result in mV
if (unit == ADC_UNIT_1)
{
raw = adc1_get_raw(adc1_channel);
}
else
{
adc2_get_raw(adc2_channel, ADC_WIDTH_12Bit, &raw);
}
return raw;
#endif
}

0 comments on commit 7c0a4be

Please sign in to comment.