# From control model to C code

When the controller is digital, the model should lead to code you can put on the microcontroller. Voltecx generates C from the same blocks you simulated, and tells you what it did not generate.

## Portable C99 from your control blocks

**Tools › Code Generation** turns the sampled control blocks of a circuit into two files, a header and a source. The code has no `malloc` and no global variables: call `<prefix>_init()` once and `<prefix>_step()` at every sample. Choose `float` or `double`.

Covered blocks include gains, sums, limiters and comparators, the Clarke, Park and dq transforms, SVPWM, ADC and zero-order hold, discrete transfer functions, FIR and IIR filters up to 32 taps, discrete integrators, and discrete PI and PID with anti-windup.

Blocks that are not generated (PWM carriers, continuous blocks, time sources, logic) are listed in the reply with the reason. Nothing is guessed. A signal that crosses between generated and non-generated blocks becomes a field of the input or output structure.

## Microcontroller targets

| Target | What is added |
|---|---|
| `stm32g474` | TIM1 PWM with complementary output and dead time (a three-phase PWM on channels 1 to 3, a second phase-shifted bridge on TIM8), ADC1 triggered by the timer, and the interrupt that runs the controller and writes the duties |
| `f28379d` | ePWM1 with dead band (a three-phase PWM on ePWM1 to ePWM3, a second phase-shifted bridge on ePWM2, synchronised by ePWM1), ADC-A started by SOCA, the interrupt, a `main`, and a Code Composer Studio project spec |

Timer counts follow the circuit's PWM and dead-time blocks. The dead time is rounded up to whole timer counts, so it is never shorter than in the model.

## Fixed point and software in the loop

`design.digitalize` turns a compensator into a 2p2z or 3p3z difference equation (Tustin with prewarping, or matched poles and zeros), rounds the coefficients to `float32`, `q15`, `q31` or a format such as `q7.24`, and reads the loop gain again with the rounded coefficients, the computation delay and the PWM delay. You see the phase margin the firmware will really have. With `apply`, the compensator goes into the circuit as a `ztf` block holding the stored coefficients, with the analog block's output limits and anti-windup, so the simulation, the spec check, the exported C and both in-the-loop tests run one controller: the one the MCU will run. `q15` and `q31` choose the integer bits that hold every constant of the code, so a 3.3 V reference and the coefficients share one format and the export writes the stored integers exactly. In automatic design, the firmware and its software-in-the-loop files come out with the rest of the hand-off, next to the report.

## See what the real controller computes

Add a UART log to the generated code and read the capture back with `debug.serialLog`, next to the simulated signals. Frames carry a step counter and a CRC, and the logger never waits for the UART.

## How it was checked, and what to do next

The tests run the generated assignments against the simulator's own block evaluation, and they agree to 1e-9. Compile the code with your own toolchain and check it against your simulation before you rely on it.
