The Dcdc class becomes struct dcdc with prefixed functions. Private
member functions become static functions. dcdc_check_start_conditions()
and dcdc_control() are still weak, so they can be overridden by custom
implementations.
The constructor is replaced by dcdc_init(), which is called from a
SYS_INIT hook at application level in setup.cpp. This corresponds to the
time the global C++ objects were constructed before, so the unit tests
(which don't run main()) still get initialized objects. The remaining
objects will be moved to this hook when they are converted.
The global device status is declared in device_status.h instead of
setup.h, so that C modules don't need to include the C++ setup header.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DeviceStatus becomes struct device_status with prefixed functions. The
error flag helpers are static inline functions in the header.
The implementation stays in C++ for now, as it reads data from objects
which are not converted yet. The header doesn't include the headers of
these objects anymore.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
DcBus and PowerPort become struct dc_bus and struct power_port with
prefixed functions instead of member functions.
The default member initializers are replaced by dc_bus_init() and
power_port_init(). The only non-zero default value is the series
multiplier of the bus, which is set to 1 by dc_bus_init() and in the
static initializers of the global buses.
The optional voltage parameter of the control voltage functions has to
be passed explicitly now, 0 still selects the voltage intercept of the
bus.
LoadOutput and PwmSwitch still inherit from the C struct until they are
converted, and initialize it with power_port_init().
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the temperature calculation in daq.cpp by the DAQ NTC sensor
driver. Temperatures below -50 °C are still considered as a missing
sensor. A missing MOSFET temperature sensor keeps the previous value
instead of setting an implausible temperature.
The series resistor of the NTCs was specified as multiplier of the DAQ
channels before. It is moved to the pullup-ohm property of the new
ntc-fets and ntc-bat nodes and the gain of the DAQ channels is set to 1.
Note for out-of-tree boards: The temp-fets and temp-bat DAQ channels need
the labels daq_temp_fets and daq_temp_bat and a corresponding
libresolar,daq-ntc-thermistor node with the labels ntc_fets and ntc_bat.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Zephyr's NTC thermistor driver reads the ADC on demand, which is not
possible while the ADC is continuously sampled by the DAQ driver. The new
sensor driver calculates the temperature from the filtered reading of a
DAQ channel using the beta equation.
The DAQ channel is referenced via phandle, the pull-up resistor, the
nominal resistance and the beta value are configured in the devicetree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the register-level timer code in pwm_switch_driver.c by the STM32
PWM power switch driver. The duty cycle steps of the control algorithm
are kept at 100 us on-time per step, independent of the timer resolution
of the driver.
The rising edges of the PWM signal are only reported while the switch
is on. Before, the overvoltage alert of the battery voltage was also
inhibited periodically while the switch was off.
The PWM switch is now a child node of the timer with the label
pwm_switch instead of a child of the outputs node referencing a Zephyr
PWM device. The unit tests use the emulated power switch, so that the
dummy PWM binding is not needed anymore. The obsolete kconfig-flag and
latching-pgood properties are removed from the outputs binding.
Note for out-of-tree boards: The pwm_switch child of the outputs node
must be converted to a libresolar,stm32-pwm-power-switch node below the
st,stm32-timers node.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
Replace the function pointers for the load and USB switches by power
switch devices. The register-level code for the short-circuit comparator
of the PWM 2420 LUS moved to the STM32 LPTIM power switch driver, which
reports short circuits via the fault callback. load_driver.c is removed.
The load and USB switches are now standalone devicetree nodes with the
labels load_switch and usb_switch instead of children of the outputs
node. The separate charge_pump node is replaced by the charge-pump-pwms
property of the load switch.
The comparator interrupt now only clears its own EXTI pending flag
instead of all pending flags.
Note for out-of-tree boards and overlays: The load and usb-pwr children
of the outputs node must be converted to nodes with the compatible
libresolar,gpio-power-switch and the labels load_switch and usb_switch.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a driver class for power switches, e.g. used for load and USB
outputs. Besides switching on and off, drivers may provide a power-good
feedback and report faults that were already handled in hardware.
Two backends are available:
- libresolar,gpio-power-switch: Switch driven by a GPIO with optional
latching power-good feedback and charge pump PWM for high-side gate
drivers. The latch time is handled by delayable work instead of
blocking the caller.
- libresolar,stm32-lptim-power-switch: Switch driven by LPTIM1 of
STM32L0, which is triggered by COMP2 to switch off within a few
microseconds in case of a short circuit (PWM 2420 LUS).
The GPIO backend is tested on native_sim using the GPIO emulator and the
fake PWM driver.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the register-level ADC, DAC and DMA handling in daq_driver.c by
the new DAQ driver. The application only maps the measurements to the
power ports and DC buses and handles the alerts. The factory calibration
values are removed from mcu.h, as they are provided by the driver now.
All boards are migrated to the new libresolar,stm32-daq compatible. The
unit tests use the emulated DAQ.
The high-side overvoltage limit is now set after the DAQ setup. Before,
the limit was calculated before any reading of the internal reference
voltage was available, so that the alert never triggered on boards
without fixed reference voltage (STM32F0 and STM32L0).
Note for out-of-tree boards: The adc-inputs node must use the compatible
libresolar,stm32-daq instead of adc-inputs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a Zephyr driver for the data acquisition (DAQ), i.e. continuous
sampling of all analog inputs with 1 kHz, low-pass filtering of the
readings and checking them against upper and lower limits. It is based
on the existing implementation in the application.
The driver consists of a common part (filtering, alerts, scaling and
calibration) and backends for the different ADC types:
- STM32 ADCs with a fixed channel sequence (STM32F0, STM32L0 and newly
supported STM32G0)
- STM32 ADCs with a configurable channel sequence (STM32G4)
- Emulated DAQ for tests on native_sim
Compared to the application implementation, the position of the
readings in the DMA buffer is determined by the driver, so that the
inputs can be specified in any order in the devicetree. The factory
calibration values for the internal reference voltage and temperature
sensor are taken from the STM32 LL headers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Use pull-ups instead of pull-downs at the capture inputs, so that the
tests fail if the outputs are not actively driven low while the half
bridge is stopped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Verify the timing of the half-bridge outputs at the pins without
requiring a charge controller, where a wrong timer configuration could
destroy the power stage.
The outputs of the half bridge are wired to two input capture channels
of a 32-bit timer (TIM2 or TIM5) on a Nucleo-G474RE board. As both
channels share the same counter, the edges of the high-side and
low-side signals can be put into chronological order to measure:
- frequency and duty cycle (incl. duty cycle limits)
- dead time at both switching transitions of each period
- overlap of high-side and low-side on-time (shoot-through)
- output states when stopped and with forced low-side switch
The test covers the advtim (TIM1), gptim (TIM3) and HRTIM drivers with
separate overlays. It requires the half_bridge_loopback fixture, so it
is only built in CI and run with a twister hardware map on a board
with the loop-back wiring.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Convert the application unit tests to ztest, so that all tests can be
run with twister and the Unity module is not required anymore.
Each test file becomes a ztest suite. The test names got the prefix
test_ as required by twister. Unity assertions were converted as
follows:
- TEST_ASSERT_EQUAL_FLOAT is replaced by assert_float_equal, which uses
the same relative precision of 1e-5 as Unity.
- TEST_ASSERT_EQUAL and other integer assertions are replaced by their
ztest equivalents. Unity compared these as integers, so float values
were truncated before. All tests still pass with exact comparison.
In contrast to Unity, ztest runs the tests of a suite in alphabetical
order. The setup code for the DAQ tests is moved into a before function
and the half bridge is stopped before each buck converter test (same as
for the boost converter tests), as the buck tests previously relied on
running before the boost tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the unit tests of the application next to the driver tests in
tests/drivers, so that the tests directory only contains test suites in
subdirectories.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fuse destruction set a duty cycle of 0 and started the half bridge.
For the STM32 TIM drivers, this did not switch on the low-side MOSFET,
as starting is refused with a compare value of 0. Use the new function
half_bridge_force_low_side() instead.
In addition, the control loop running at 10 Hz could override the
forced state, because it saw an enabled half bridge and continued with
MPPT control. Now the control function returns early as soon as the HS
MOSFET short error is set.
The low-side switch is forced on again with each call in case the half
bridge was stopped in the meantime, e.g. by an ADC alert. The data is
only stored once to avoid unnecessary flash writes if the fuse does not
blow.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add half_bridge_force_low_side() to switch the low-side switch
continuously on, bypassing the compare value limits. This is required
for safety measures like blowing the battery fuse in case of a shorted
high-side MOSFET.
Previously, this was attempted by setting a duty cycle of 0 and
starting the PWM. However, the STM32 timer drivers refuse to start with
a compare value of 0, so the low-side switch was never turned on.
The forced state is kept until half_bridge_stop() is called. Changes of
the compare value or its limits and start requests are rejected with
-EBUSY meanwhile, so that a concurrently running control loop can't
override it.
- advtim: Compare value 0 keeps OCxREF low, so that CHxN stays on. The
output mode can't be changed, as it is locked during initialization.
- gptim: Compare value 0 for both channels, so that the low-side
channel with inverted polarity stays on.
- HRTIM: Output 1 is reset by software and set events are removed, so
that the complementary output 2 stays on.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a build-only twister scenario for boards using the STM32 TIM and
HRTIM half-bridge drivers. The tests are not run on hardware, as they
would switch the power stage.
The driver tests are run in CI with:
west twister -T tests/drivers --integration
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the register-level half_bridge.c module by the new half-bridge
drivers. The Dcdc class gets the half-bridge device injected via its
constructor, so that the emulated driver can be used in unit tests.
Board devicetrees are migrated to the new compatibles:
- TIM1 boards use libresolar,stm32-half-bridge with complementary
outputs on channel 1. The ADC trigger via OC6/TRGO2 is enabled for
the STM32G4 boards as before.
- MPPT 1210 HUS uses libresolar,stm32-half-bridge with TIM3 channel 3
(high side) and channel 4 (low side).
- Nucleo G474RE uses libresolar,stm32-hrtim-half-bridge. The HRTIM
node gets the timer clock source as second clock.
Instead of the half-bridge compatible, the halfbridge node label is now
used to detect if the board has a DC/DC converter.
The half-bridge unit tests are removed, as they are superseded by the
driver test suite in tests/drivers/half_bridge.
Note for out-of-tree boards: Nodes with "half-bridge" compatible must be
migrated to one of the new compatibles, and the deadtime property was
renamed to deadtime-ns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a half-bridge driver for STM32 timers (TIM), replacing the
register-level code for TIM1 and TIM3 in the application.
The timer and channels are configured via devicetree instead of being
derived from the timer base address. Two operating modes are supported:
- Complementary outputs CHx/CHxN of advanced-control timers (e.g. TIM1)
with edge-aligned PWM and hardware dead time generation.
- Two independent channels of general-purpose timers (e.g. TIM3) with
center-aligned PWM and dead time generated by shifting the compare
value of the low-side channel.
Compared to the previous implementation, the timer clock is obtained
from the clock control driver instead of assuming SystemCoreClock, so
that APB prescalers are taken into account. The dead time register is
now encoded properly using __LL_TIM_CALC_DEADTIME instead of masking the
lowest 7 bits, which was wrong for more than 127 timer clocks.
The ADC trigger via OC6/TRGO2 (previously enabled for all STM32G4 MCUs)
must now be enabled explicitly with the adc-trigger property.
The period test of the driver test suite is skipped for non-emulated
devices so that the suite can also be built for STM32 boards.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add an emulated half-bridge driver for tests on native_sim. It behaves
like an edge-aligned STM32 timer, similar to the dummy registers used
in the unit tests so far.
The driver API is tested in a new ztest suite, which can be run with
twister:
west twister -T tests/drivers -p native_sim
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also use new Zephyr CI environment and compile all boards.
Board migration script example:
python ../zephyr/scripts/utils/board_v1_to_v2.py --board-root . \
-v libresolar -g libresolar -b pwm_2420_lus -s stm32l072xx
The format tries to maintain the previously used Libre Solar firmware coding
style as much as possible.
Applying the format fixed some issues and re-ordered some parts of the code.
https://docs.zephyrproject.org/latest/guides/west/workspaces.html
Previously, west manifest and CMake entry point were in zephyr subfolder
as required for PlatformIO. As PlatformIO support was dropped, we can
follow the default Zephyr folder structure more closely.
The application is built and flashed from app subfolder, where as unit
tests are run from tests subfolder.
Rationale: Too much effort to maintain two different tool chains.
PlatformIO does not consider the west.yml files and thus, does not
support custom Zephyr modules. However, they are needed to include
ThingSet as a module.
In addition to that, PlatformIO versions always lag behind official
Zephyr releases and we want to avoid being dependent on PlatformIO.