drivers: power_switch: add PWM support

Extend the power switch API for switches driven by a PWM signal, as used
in PWM solar charge controllers:

- Set and get the duty cycle
- Check if the switch is currently conducting (momentary PWM state)
- Generic event callback replacing the fault callback, which also
  reports the rising edges of the PWM signal

New backends:

- libresolar,stm32-pwm-power-switch: PWM signal generated by a channel
  of an STM32 timer. The output is driven inactive while switched off
  instead of disabling the channel, which left the pin floating.
- libresolar,power-switch-emul: Emulated switch for tests on native_sim
  supporting all features of the API. Power-good failures and events
  can be injected by the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Jäger
2026-11-01 14:20:21 +01:00
co-authored by Claude Opus 5.5
parent 0adc9edb7a
commit da59ab8276
17 changed files with 769 additions and 26 deletions
+5 -3
View File
@@ -40,7 +40,8 @@ LoadOutput::LoadOutput(DcBus *dc_bus, const struct device *sw, int led_pos)
}
else {
// not supported by all switches, so the return value is ignored
power_switch_set_fault_callback(power_switch, short_circuit_callback, this);
power_switch_set_callback(power_switch, POWER_SWITCH_EVENT_FAULT, short_circuit_callback,
this);
}
switch_set(false);
@@ -158,7 +159,8 @@ void LoadOutput::stop(uint32_t flag)
// flicker the load LED if failure was most probably caused by the user
if (flags_check(&error_flags,
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT)) {
ERR_LOAD_OVERCURRENT | ERR_LOAD_VOLTAGE_DIP | ERR_LOAD_SHORT_CIRCUIT))
{
#if LED_EXISTS(load)
leds_flicker(LED_POS(load), LED_TIMEOUT_INFINITE);
#endif
@@ -190,7 +192,7 @@ void LoadOutput::switch_set(bool on)
}
}
void LoadOutput::short_circuit_callback(const struct device *dev, void *user_data)
void LoadOutput::short_circuit_callback(const struct device *dev, uint32_t events, void *user_data)
{
static_cast<LoadOutput *>(user_data)->stop(ERR_LOAD_SHORT_CIRCUIT);
}