Skip to content

Commit

Permalink
Initial ESP32-C3-DevKitM-1 board support (esphome#2062)
Browse files Browse the repository at this point in the history
Co-authored-by: Stijn Tintel <[email protected]>
  • Loading branch information
agners and stintel authored Jul 26, 2021
1 parent 159744e commit d9f09a7
Show file tree
Hide file tree
Showing 10 changed files with 984 additions and 887 deletions.
875 changes: 875 additions & 0 deletions esphome/boards.py

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions esphome/components/adc/adc_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static const char *const TAG = "adc";
void ADCSensor::set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }

inline adc1_channel_t gpio_to_adc1(uint8_t pin) {
#if CONFIG_IDF_TARGET_ESP32
switch (pin) {
case 36:
return ADC1_CHANNEL_0;
Expand All @@ -34,6 +35,22 @@ inline adc1_channel_t gpio_to_adc1(uint8_t pin) {
default:
return ADC1_CHANNEL_MAX;
}
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
switch (pin) {
case 0:
return ADC1_CHANNEL_0;
case 1:
return ADC1_CHANNEL_1;
case 2:
return ADC1_CHANNEL_2;
case 3:
return ADC1_CHANNEL_3;
case 4:
return ADC1_CHANNEL_4;
default:
return ADC1_CHANNEL_MAX;
}
#endif
}
#endif

Expand All @@ -46,8 +63,10 @@ void ADCSensor::setup() {
#ifdef ARDUINO_ARCH_ESP32
adc1_config_channel_atten(gpio_to_adc1(pin_), attenuation_);
adc1_config_width(ADC_WIDTH_BIT_12);
#if !CONFIG_IDF_TARGET_ESP32C3 && !CONFIG_IDF_TARGET_ESP32H2
adc_gpio_init(ADC_UNIT_1, (adc_channel_t) gpio_to_adc1(pin_));
#endif
#endif
}
void ADCSensor::dump_config() {
LOG_SENSOR("", "ADC Sensor", this);
Expand Down Expand Up @@ -89,6 +108,7 @@ float ADCSensor::sample() {
#ifdef ARDUINO_ARCH_ESP32
int raw = adc1_get_raw(gpio_to_adc1(pin_));
float value_v = raw / 4095.0f;
#if CONFIG_IDF_TARGET_ESP32
switch (this->attenuation_) {
case ADC_ATTEN_DB_0:
value_v *= 1.1;
Expand All @@ -105,6 +125,24 @@ float ADCSensor::sample() {
default: // This is to satisfy the unused ADC_ATTEN_MAX
break;
}
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
switch (this->attenuation_) {
case ADC_ATTEN_DB_0:
value_v *= 0.84;
break;
case ADC_ATTEN_DB_2_5:
value_v *= 1.13;
break;
case ADC_ATTEN_DB_6:
value_v *= 1.56;
break;
case ADC_ATTEN_DB_11:
value_v *= 3.0;
break;
default: // This is to satisfy the unused ADC_ATTEN_MAX
break;
}
#endif
return value_v;
#endif

Expand Down
11 changes: 11 additions & 0 deletions esphome/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from esphome.coroutine import coroutine, coroutine_with_priority # noqa
from esphome.helpers import ensure_unique_string, is_hassio
from esphome.util import OrderedDict
from esphome import boards

if TYPE_CHECKING:
from ..cpp_generator import MockObj, MockObjClass, Statement
Expand Down Expand Up @@ -593,10 +594,20 @@ def is_esp8266(self):

@property
def is_esp32(self):
"""Check if the ESP32 platform is used.
This checks if the ESP32 platform is in use, which
support ESP32 as well as other chips such as ESP32-C3
"""
if self.esp_platform is None:
raise ValueError("No platform specified")
return self.esp_platform == "ESP32"

@property
def is_esp32_c3(self):
"""Check if the ESP32-C3 SoC is being used."""
return self.is_esp32 and self.board in boards.ESP32_C3_BOARD_PINS

def add_job(self, func, *args, **kwargs):
self.event_loop.add_job(func, *args, **kwargs)

Expand Down
13 changes: 7 additions & 6 deletions esphome/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation, pins
from esphome import automation, boards
from esphome.const import (
CONF_ARDUINO_VERSION,
CONF_BOARD,
Expand Down Expand Up @@ -50,18 +50,19 @@
CONF_NAME_ADD_MAC_SUFFIX = "name_add_mac_suffix"


def validate_board(value):
def validate_board(value: str):
if CORE.is_esp8266:
board_pins = pins.ESP8266_BOARD_PINS
boardlist = boards.ESP8266_BOARD_PINS.keys()
elif CORE.is_esp32:
board_pins = pins.ESP32_BOARD_PINS
boardlist = list(boards.ESP32_BOARD_PINS.keys())
boardlist += list(boards.ESP32_C3_BOARD_PINS.keys())
else:
raise NotImplementedError

if value not in board_pins:
if value not in boardlist:
raise cv.Invalid(
"Could not find board '{}'. Valid boards are {}".format(
value, ", ".join(sorted(board_pins.keys()))
value, ", ".join(sorted(boardlist))
)
)
return value
Expand Down
Loading

0 comments on commit d9f09a7

Please sign in to comment.