# MCU Output Stage (`mcu`)

> Adds the timing of a digital controller to a duty command before the PWM.

![mcu](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/img/mcu.svg)

[Component reference](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/) · [繁體中文](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/control/mcu/)

**Group:** Control · **Kind:** `{"block":"mcu","kind":"control"}`

## When to use it

Use it between a duty calculation and a PWM block to see what a digital controller does to your loop. Leave it out for an ideal controller.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `a` | signal | Duty command for phase a, 0 to 1 |
| `b` | signal | Duty command for phase b, 0 to 1 |
| `c` | signal | Duty command for phase c, 0 to 1 |
| `da` | signal | Duty for phase a; goes to Three-Phase PWM |
| `db` | signal | Duty for phase b; goes to Three-Phase PWM |
| `dc` | signal | Duty for phase c; goes to Three-Phase PWM |
| `overrun` | signal | 1 when the code takes the whole period or more, else 0 |
| `load` | signal | CPU load of the code, tIsr times fs (0 without tIsr) |

## Common parameters

- `fs`: How often the controller code runs (Default: required)
- `fpwm`: Carrier frequency of the PWM it drives (Default: required)
- `delay`: Computing delay, counted in samples (Default: 1)
- `align`: Where in the carrier the code runs: `start` or `peak` (Default: start)
- `update`: When a new duty takes effect: `period` or `immediate` (`end`) (Default: period)

## Parameters

| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
| `fs` | Hz | required |  | How often the controller code runs |
| `fpwm` | Hz | required |  | Carrier frequency of the PWM it drives |
| `delay` | — | 1 |  | Computing delay, counted in samples |
| `align` | — | start |  | Where in the carrier the code runs: `start` or `peak` |
| `update` | — | period |  | When a new duty takes effect: `period` or `immediate` (`end`) |
| `counts` | — | optional (off) |  | Timer counts that set duty resolution |
| `bits` | — | optional (off) |  | Duty resolution in bits |
| `tIsr` | s | optional (off) |  | Time the code takes each run (replaces delay) |
| `cycles` | — | optional (off) |  | CPU cycles the code takes each run |
| `fclk` | Hz | optional (off) |  | CPU clock that counts the cycles |

_Parameter descriptions are in English in both languages._

## Model

The ISR fires every 1/fs at the carrier valley t = k/fpwm (align = start) or the peak t = (k + 1/2)/fpwm (peak). At each instant the three commands are clamped to 0..1 and ready after delay samples. With update = period they reach the registers at the next carrier valley, with immediate (or end) as soon as they are ready, and they are rounded to a multiple of 1/counts (bits gives counts = 2^bits). The registers are 0 until the first update. With an execution time (tIsr, or cycles of the clock fclk) in place of delay, each ISR starts at its instant, or when the previous one returns if that is later, and its duty is ready tIsr after it starts. An ISR that cannot start before the next instant is lost, and its duty is never written. The output load is tIsr times fs, and overrun is 1 when load is 1 or more. Only the timing is modelled, not the controller arithmetic.

## Common mistakes

- The block captures whatever its inputs hold at the interrupt time. Give the Zero-Order Hold and Discrete PI Controller ahead of it ts = 1/fs and the matching offset (0 for start, half a PWM period for peak), or the controller runs at a different moment than the PWM stage expects.
- delay counts from the block's own interrupt instant, not from the ADC sample. With the sample at that instant (zoh offset 0 for start), delay 1 and update period load the duty of the sample at k/fs into the register at (k + 1)/fs, the classic one-period latency. If the ADC is triggered later than the interrupt instant (an offset after the valley, such as a trigger delay that cancels a sensor's group delay), the block captures the duty computed from the previous sample, which already costs one period: use delay 0 for the classic latency, since delay 1 then gives two periods.
- fpwm must equal the freq of the Three-Phase PWM it feeds. The valley timing assumes a triangle carrier with phase 0, so a sawtooth shape or a phase setting does not match.
- The inputs must be duty values from 0 to 1. A modulation index from Space-Vector Modulator in its default output m (-1 to 1) loses its negative half, so set that output to duty; delay counts interrupt samples, not seconds.
- With the fixed-step solver the interrupt and register instants must fall on the step grid, so maxStep should divide 1/fs and 1/fpwm evenly. A maxStep above 1/fs skips interrupt instants.

## Datasheet and sources

There is no datasheet. fs is the interrupt or ADC trigger rate of your timer setup, and fpwm follows from the timer clock and period register (f_timer divided by 2 times the period for an up-down counter). delay is the measured interrupt run time divided by 1/fs, and counts is the period register value, or bits its width. tIsr is the measured interrupt run time; cycles is a cycle count, such as the estimate cargo xtask armgcc prints for the generated STM32G474 code, with fclk the CPU clock (170 MHz on that part).

## Example

Open the example with the command `file.openExample` and the name `demo:mcu`.

---
Generated by `cargo xtask reference` from the usage texts and parameter specs. Do not edit by hand.
