We're porting our motor control firmware to the XMC4200 platform and I need a fresh SVPWM implementation written in C. The target files should be svpwm_output/svpwm.c and svpwm_output/svpwm.h.
Some context: the previous engineer left behind a bunch of reference material and older code in the workspace — theory docs, transform references, config files, an old SVPWM implementation from a different project, sector tables, test vectors, hardware correction notes, etc. Feel free to use whatever's helpful, but be careful: some of these files are from earlier hardware revisions and may not match our current platform requirements. The PWM peripheral spec (docs/pwm_peripheral_spec.md) and the motor parameters in config/motor_params.json are the authoritative sources for this build. The hardware integration team also left a correction note in config/edge_aligned_correction.json that may be relevant — make sure you've read both the spec and the correction note before finalizing your T0 distribution approach.
The implementation should include:
A function that takes Valpha and Vbeta (floats, in volts) as inputs and computes the three-phase PWM compare values (U, V, W) as integers in the range [0, PWM_PERIOD].
Proper sector determination (sectors 1–6) based on the alpha-beta voltage components.
Correct calculation of dwell times T1, T2, and T0, normalized against the DC bus voltage.
Conversion of dwell times to PWM compare register values appropriate for the PWM mode our hardware actually uses.
The zero vector (Valpha=0, Vbeta=0) should produce 50% duty on all three phases.
Clamping so compare values never exceed the valid range.
The header file (svpwm_output/svpwm.h) must include proper include guards, the type definitions (input/output structs), and the function prototype. Use #define constants for PWM_PERIOD and DC bus voltage in the .c file and reference them in the header.
Make sure the output matches the test vectors in test/test_vectors.csv — those were validated on the bench and are our ground truth. Include clear comments explaining the sector logic, dwell time formulas, and the T0 distribution approach chosen for this hardware.