Files
charge-controller-firmware/tests/drivers/half_bridge_loopback/src/capture.h
T
Martin JägerandClaude Opus 5.5 8213ec7383 tests: drivers: add half-bridge loop-back tests for Nucleo-G474RE
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>
2026-10-21 20:22:39 +02:00

87 lines
2.0 KiB
C

/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef CAPTURE_H_
#define CAPTURE_H_
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* maximum number of edges recorded per signal */
#define CAPTURE_MAX_EDGES 32
struct capture_edge
{
uint32_t time; /* capture timer counter value */
bool level; /* signal level after the edge */
};
struct capture_signal
{
struct capture_edge edges[CAPTURE_MAX_EDGES];
size_t num_edges;
bool level_at_start;
bool level_at_end;
};
struct capture_result
{
struct capture_signal hs;
struct capture_signal ls;
/* an edge was missed because the capture register was not read in time */
bool overrun;
};
/**
* PWM timing derived from the captured edges (all values in nanoseconds)
*/
struct capture_timing
{
/* number of complete high-side periods */
int num_periods;
uint32_t period_avg;
uint32_t hs_on_avg;
uint32_t ls_on_avg;
/* number of intervals with both outputs off (should be 2 per period) */
int num_deadtimes;
uint32_t deadtime_min;
uint32_t deadtime_max;
/* total time with both outputs on */
uint32_t overlap;
/* captured edges are inconsistent (e.g. two rising edges in a row) */
bool invalid;
};
/**
* Initialize the capture timer and apply the pin configuration of the loop-back inputs
*
* @retval 0 on success
* @retval -errno otherwise
*/
int capture_init(void);
/**
* Record edges of the high-side and low-side signal
*
* Recording stops if CAPTURE_MAX_EDGES were captured for both signals or the timeout expired.
* Interrupts are locked while recording.
*
* @param result Captured edges
* @param timeout_us Maximum recording time in microseconds
*/
void capture_record(struct capture_result *result, uint32_t timeout_us);
/**
* Calculate the PWM timing from captured edges
*
* @param result Captured edges
* @param timing Calculated timing
*/
void capture_analyze(const struct capture_result *result, struct capture_timing *timing);
#endif /* CAPTURE_H_ */