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>
Add the design for the devicetree power model (step 2 of the migration
plan) and an intermediate step 1b converting the application from C++
to C, so that the power model can be implemented in C with static
initializers generated from the devicetree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The STM32 bootloader entry function as well as the flash size and page
size variables were only used by ThingSet items of the DFU group, which
were removed with the switch to the ThingSet SDK. The unused code is
removed together with mcu.h and the obsolete HW_LOG_LEVEL option.
The USB-C dead battery pull-downs of the STM32G4 are already disabled by
the Zephyr SoC initialization if USB device support is not enabled, so
the register access in setup() is not necessary anymore.
The DC/DC duty cycle step size depending on the SoC series is moved from
the code to the new Kconfig option DCDC_DUTY_STEP_SIZE with the same
defaults.
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>
The functions for timer clock, reset and pinctrl setup are not specific
to half bridges and will also be used by the PWM power switch driver.
They are moved to a separate library enabled by the hidden Kconfig
symbol LIBRESOLAR_STM32_TIM.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Describe the target architecture of the firmware and the steps to
migrate the current application towards it.
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>
The clang-format-15 package is not available anymore in the latest
Ubuntu runner image. Installing clang-format from PyPI makes the style
check independent of the runner image and allows to use the same
version locally:
pip install clang-format==23.1.*
The style check script prefers the clang-format-diff.py installed by
pip and prints the clang-format version.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enable NVS in flash for the ThingSet storage. As the STM32G0B1 doesn't
have an RNG, a timer-based number generator is used for the automatic
CAN node address assignment of ThingSet.
The board is added to the firmware build tests, as it is supported by
the DAQ driver now.
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>
Board definition for the Spinvert Boost 4820, an active rectifier with
DC/DC boost converter for small wind, hydro or pedal generators, based
on the KiCad schematic of the hardware.
- STM32G0B1CC with 8 MHz HSE and 64 MHz system clock
- Synchronous boost converter driven by TIM1 CH2/CH2N (LM5107) at
128 kHz
- Dump load output driven by TIM2 CH2
- ADC inputs for DC voltages, DC/DC current (INA299A2 with reference
voltage from DAC1), dump load current, MOSFET temperature and the
generator phase voltages
- FDCAN1 with TCAN334 transceiver, USART1 at the USB-C SBU pins
(debug accessory mode) and USB device
The STM32G0 is not yet supported by the ADC data acquisition of the
application, so the firmware can't be built for this board yet.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the oled-display-library module by the upstream Zephyr
solomon,ssd1306 display driver and the character framebuffer (CFB)
subsystem, so that the external module can be removed from the
manifest.
- A new uext_oled shield adds the display to the UEXT I2C bus, which
gets a uext_i2c node label in all boards. The display is enabled
with -DSHIELD=uext_oled instead of CONFIG_UEXT_OLED_DISPLAY=y.
- The display uses deferred init, so the application can power the
UEXT port via the uext_en GPIO before initializing it.
- The classic Adafruit 5x7 font is ported to a CFB font to keep the
screen layout unchanged. The Libre Solar logo is shown for 2 seconds
after startup.
- CONFIG_UEXT_OLED_BRIGHTNESS is replaced by
CONFIG_SSD1306_DEFAULT_CONTRAST.
- The heap is enlarged for the CFB frame buffer.
- oled.cpp is fixed for boards with PWM port or without load output
and is only built if the display is enabled.
- A build-only twister scenario with the shield is added.
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>
The advtim and gptim drivers left the outputs floating while the half
bridge was stopped, so the state of the MOSFETs only depended on the
pull-down resistors at the gate driver inputs.
- advtim: Enable the off-state selection for idle mode (OSSI), so that
both outputs are driven to their idle level (low) while the main
output is disabled.
- gptim: Keep the output channels enabled and switch between PWM mode
and forced output levels to start and stop. The forced low-side
state uses forced output levels as well.
The HRTIM driver already drives the idle level of disabled outputs.
The requirement is added to the API documentation. External pull-down
resistors are still required for the time during reset and before the
driver is initialized.
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>
Run the application unit tests and the driver tests with a single
twister call and update the documentation accordingly.
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>
Add a build-only twister scenario for the application covering all
boards including non-default revisions, which were not built in CI so
far (e.g. the build for mppt_2420_hc@0.1.0 was broken unnoticed).
Additional boards or revisions only need to be added to app/sample.yaml.
The firmware can be built for all boards locally with:
west twister -T app --integration
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rename the legacy <board>.yaml files to twister.yaml without the
identifier. With the legacy format, twister only provides a platform for
the default board revision, while twister.yaml applies to all board
targets including non-default revisions like mppt_2420_hc@0.1.0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The ThingSet EEPROM storage backend uses the eeprom node label, which
was missing for this board revision, so the build failed.
Add the node label and remove the obsolete label property, as done for
the other boards with the same EEPROM.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fuse destruction in case of a shorted high-side MOSFET was enabled
for all boards with DC/DC converter, even if the board doesn't have a
fuse at the low-side terminal (e.g. Nucleo G474RE).
Add an ls-fuse property to the charge-controller binding to describe
this hardware feature and make HS_MOSFET_FAIL_SAFE_PROTECTION depend on
it. The property is added to all MPPT boards, so the behavior does not
change for them.
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>
Different clang-format versions disagree on the formatting of braced
initializer lists in macros. Disable formatting for the affected macro,
as the version used in CI would put multiple designated initializers
in a single line.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The STM32 timer half-bridge driver supported two operating modes, which
only shared the clock, reset and pinctrl setup, but branched in every
API function. Split it into two drivers with separate compatibles:
- libresolar,stm32-advtim-half-bridge: Advanced-control timers (e.g.
TIM1) with complementary outputs and hardware dead time generation.
- libresolar,stm32-gptim-half-bridge: General-purpose timers (e.g. TIM3)
without dead time generator, using two independent channels with dead
time generated in software.
This allows the devicetree bindings to only contain the properties
valid for the respective timer type (e.g. adc-trigger only for advtim,
high-side-channel and low-side-channel required for gptim) and removes
most of the preprocessor conditionals for MCUs without advanced-control
timers. The common timer setup is moved to half_bridge_stm32_tim.c.
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>
The bus-speed property was deprecated in favor of bitrate. Twister
treats deprecated devicetree properties as errors.
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>
Port the HRTIM implementation from the application to a half-bridge
driver. As before, only timer A with outputs TA1/TA2 is supported and
the higher resolution of the HRTIM is not used yet.
In contrast to the previous implementation, the timer clock source must
be specified as the second clock of the HRTIM node (e.g. TIMPCLK2), the
DLL calibration is aborted with a timeout and the ADC trigger via timer
A compare 3 must be enabled explicitly with the adc-trigger property.
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>
Add infrastructure for out-of-tree drivers in this Zephyr module and
define a driver API for half bridges consisting of two complementary
PWM outputs with dead time, as used in the DC/DC converter.
The API is defined as a __subsystem so that drivers can use the
DEVICE_API() macro. Integer-based compare values allow to change the
PWM with minimum step size, while inline helpers provide the float
duty cycle interface known from the previous application code.
A libresolar vendor prefix is added for the new bindings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CAN requires the use of the external oscillator instead of LSI.
The RNG did not start with the default settings. See also Zephyr
upstream commit 768fe29f904463a34138acc1b62e73b04492d2c6.
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