[esp_ldo] Add channels 1&2 support and passthrough mode (#14177)

This commit is contained in:
Oliver Kleinecke
2026-02-26 21:12:52 +01:00
committed by GitHub
parent 08035261b8
commit 54edc46c7f
4 changed files with 75 additions and 25 deletions
+56 -11
View File
@@ -13,22 +13,63 @@ esp_ldo_ns = cg.esphome_ns.namespace("esp_ldo")
EspLdo = esp_ldo_ns.class_("EspLdo", cg.Component) EspLdo = esp_ldo_ns.class_("EspLdo", cg.Component)
AdjustAction = esp_ldo_ns.class_("AdjustAction", Action) AdjustAction = esp_ldo_ns.class_("AdjustAction", Action)
CHANNELS = (3, 4) CHANNELS = (1, 2, 3, 4)
CHANNELS_INTERNAL = (1, 2)
CONF_ADJUSTABLE = "adjustable" CONF_ADJUSTABLE = "adjustable"
CONF_ALLOW_INTERNAL_CHANNEL = "allow_internal_channel"
CONF_PASSTHROUGH = "passthrough"
adjusted_ids = set() adjusted_ids = set()
def validate_ldo_voltage(value):
if isinstance(value, str) and value.lower() == CONF_PASSTHROUGH:
return CONF_PASSTHROUGH
value = cv.voltage(value)
if 0.5 <= value <= 2.7:
return value
raise cv.Invalid(
f"LDO voltage must be in range 0.5V-2.7V or 'passthrough' (bypass mode), got {value}V"
)
def validate_ldo_config(config):
channel = config[CONF_CHANNEL]
allow_internal = config[CONF_ALLOW_INTERNAL_CHANNEL]
if allow_internal and channel not in CHANNELS_INTERNAL:
raise cv.Invalid(
f"'{CONF_ALLOW_INTERNAL_CHANNEL}' is only valid for internal channels (1, 2). "
f"Channel {channel} is a user-configurable channel — its usage depends on your board schematic.",
path=[CONF_ALLOW_INTERNAL_CHANNEL],
)
if channel in CHANNELS_INTERNAL and not allow_internal:
raise cv.Invalid(
f"LDO channel {channel} is normally used internally by the chip (flash/PSRAM). "
f"Set '{CONF_ALLOW_INTERNAL_CHANNEL}: true' to confirm you know what you are doing.",
path=[CONF_CHANNEL],
)
if config[CONF_VOLTAGE] == CONF_PASSTHROUGH and config[CONF_ADJUSTABLE]:
raise cv.Invalid(
"Passthrough mode passes the supply voltage directly to the output and does not support "
"runtime voltage adjustment.",
path=[CONF_ADJUSTABLE],
)
return config
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = cv.All(
cv.ensure_list( cv.ensure_list(
cv.COMPONENT_SCHEMA.extend( cv.All(
{ cv.COMPONENT_SCHEMA.extend(
cv.GenerateID(): cv.declare_id(EspLdo), {
cv.Required(CONF_VOLTAGE): cv.All( cv.GenerateID(): cv.declare_id(EspLdo),
cv.voltage, cv.float_range(min=0.5, max=2.7) cv.Required(CONF_VOLTAGE): validate_ldo_voltage,
), cv.Required(CONF_CHANNEL): cv.one_of(*CHANNELS, int=True),
cv.Required(CONF_CHANNEL): cv.one_of(*CHANNELS, int=True), cv.Optional(CONF_ADJUSTABLE, default=False): cv.boolean,
cv.Optional(CONF_ADJUSTABLE, default=False): cv.boolean, cv.Optional(CONF_ALLOW_INTERNAL_CHANNEL, default=False): cv.boolean,
} }
),
validate_ldo_config,
) )
), ),
cv.only_on_esp32, cv.only_on_esp32,
@@ -40,7 +81,11 @@ async def to_code(configs):
for config in configs: for config in configs:
var = cg.new_Pvariable(config[CONF_ID], config[CONF_CHANNEL]) var = cg.new_Pvariable(config[CONF_ID], config[CONF_CHANNEL])
await cg.register_component(var, config) await cg.register_component(var, config)
cg.add(var.set_voltage(config[CONF_VOLTAGE])) voltage = config[CONF_VOLTAGE]
if voltage == CONF_PASSTHROUGH:
cg.add(var.set_voltage(3300))
else:
cg.add(var.set_voltage(int(round(voltage * 1000))))
cg.add(var.set_adjustable(config[CONF_ADJUSTABLE])) cg.add(var.set_adjustable(config[CONF_ADJUSTABLE]))
+11 -9
View File
@@ -10,32 +10,34 @@ static const char *const TAG = "esp_ldo";
void EspLdo::setup() { void EspLdo::setup() {
esp_ldo_channel_config_t config{}; esp_ldo_channel_config_t config{};
config.chan_id = this->channel_; config.chan_id = this->channel_;
config.voltage_mv = (int) (this->voltage_ * 1000.0f); config.voltage_mv = this->voltage_mv_;
config.flags.adjustable = this->adjustable_; config.flags.adjustable = this->adjustable_;
auto err = esp_ldo_acquire_channel(&config, &this->handle_); auto err = esp_ldo_acquire_channel(&config, &this->handle_);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to acquire LDO channel %d with voltage %fV", this->channel_, this->voltage_); ESP_LOGE(TAG, "Failed to acquire LDO channel %d with voltage %dmV", this->channel_, this->voltage_mv_);
this->mark_failed(LOG_STR("Failed to acquire LDO channel")); this->mark_failed(LOG_STR("Failed to acquire LDO channel"));
} else { } else {
ESP_LOGD(TAG, "Acquired LDO channel %d with voltage %fV", this->channel_, this->voltage_); ESP_LOGD(TAG, "Acquired LDO channel %d with voltage %dmV", this->channel_, this->voltage_mv_);
} }
} }
void EspLdo::dump_config() { void EspLdo::dump_config() {
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
"ESP LDO Channel %d:\n" "ESP LDO Channel %d:\n"
" Voltage: %fV\n" " Voltage: %dmV\n"
" Adjustable: %s", " Adjustable: %s",
this->channel_, this->voltage_, YESNO(this->adjustable_)); this->channel_, this->voltage_mv_, YESNO(this->adjustable_));
} }
void EspLdo::adjust_voltage(float voltage) { void EspLdo::adjust_voltage(float voltage) {
if (!std::isfinite(voltage) || voltage < 0.5f || voltage > 2.7f) { if (!std::isfinite(voltage) || voltage < 0.5f || voltage > 2.7f) {
ESP_LOGE(TAG, "Invalid voltage %fV for LDO channel %d", voltage, this->channel_); ESP_LOGE(TAG, "Invalid voltage %fV for LDO channel %d (must be 0.5V-2.7V)", voltage, this->channel_);
return; return;
} }
auto erro = esp_ldo_channel_adjust_voltage(this->handle_, (int) (voltage * 1000.0f)); int voltage_mv = (int) roundf(voltage * 1000.0f);
if (erro != ESP_OK) { auto err = esp_ldo_channel_adjust_voltage(this->handle_, voltage_mv);
ESP_LOGE(TAG, "Failed to adjust LDO channel %d to voltage %fV: %s", this->channel_, voltage, esp_err_to_name(erro)); if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to adjust LDO channel %d to voltage %dmV: %s", this->channel_, voltage_mv,
esp_err_to_name(err));
} }
} }
+2 -2
View File
@@ -15,7 +15,7 @@ class EspLdo : public Component {
void dump_config() override; void dump_config() override;
void set_adjustable(bool adjustable) { this->adjustable_ = adjustable; } void set_adjustable(bool adjustable) { this->adjustable_ = adjustable; }
void set_voltage(float voltage) { this->voltage_ = voltage; } void set_voltage(int voltage_mv) { this->voltage_mv_ = voltage_mv; }
void adjust_voltage(float voltage); void adjust_voltage(float voltage);
float get_setup_priority() const override { float get_setup_priority() const override {
return setup_priority::BUS; // LDO setup should be done early return setup_priority::BUS; // LDO setup should be done early
@@ -23,7 +23,7 @@ class EspLdo : public Component {
protected: protected:
int channel_; int channel_;
float voltage_{2.7}; int voltage_mv_{2700};
bool adjustable_{false}; bool adjustable_{false};
esp_ldo_channel_handle_t handle_{}; esp_ldo_channel_handle_t handle_{};
}; };
@@ -3,10 +3,13 @@ esp_ldo:
channel: 3 channel: 3
voltage: 2.5V voltage: 2.5V
adjustable: true adjustable: true
- id: ldo_4 - id: ldo_4_passthrough
channel: 4 channel: 4
voltage: 2.0V voltage: passthrough
setup_priority: 900 - id: ldo_1_internal
channel: 1
voltage: 1.8V
allow_internal_channel: true
esphome: esphome:
on_boot: on_boot: