Worked System: A Motor Controller
A motor controller meets a hard deadline 20,000 times a second. It must also stop a fault faster than any program can react.
You Will Be Able To
- Walk one PWM period and show that the control loop meets its deadline.
- Decide which jobs must be hardware, from their rates and their deadlines.
- Replace power modes by safe states, under reset, under a fault and under a debug halt.
- Lesson
- S11.11
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
The engineer halts the controller with the debugger, to read one variable. The motor jerks, and a switch on the board burns out.
The software was right. Which column of the review was left empty?
Before You Start
You need Design Reviews: "Design This SoC" and the board of Worked System: A Sensor Hub.
You need Timers: Prescaler, Compare, Capture and PWM: a counter, a compare value and a duty cycle.
You need the safe state of Analog Blocks in Test Mode, and Interrupts at the Core.
| Question | |
|---|---|
| 1 | What deadline must the control loop meet in every period? |
| 2 | Why must dead time, edge counting and the fault path be hardware? |
| 3 | What replaces power modes on a motor controller? |
| 4 | What must the outputs do when a debugger halts the core? |
| 5 | The loop needs 3000 cycles. What does the lab print last? |
The Idea: A Deadline in Every Period
An electric motor turns when its coils are switched on and off in the right order. A motor controller chip does the switching, thousands of times a second.
The first idea is a deadline in every period. The chip measures the currents, works out new duty cycles and writes them before the next period starts. Late is wrong.
The second idea is that some jobs are too fast for any program. They must be hardware: the gap between two switches, the counting of pulses from the shaft, and the reaction to a fault.
The third idea is that power modes become safe states. The question is not how to save power. It is what the outputs do when software is not in control.
New Words
| Word | Meaning |
|---|---|
| Half-bridge | Two switches in series between the supply and ground, with a motor coil at their middle. Three phases need three. |
| Shoot-through | Both switches of a half-bridge on at once. The supply is shorted, and a switch can burn out. |
| Dead time | A short gap in which both switches of a half-bridge are off, between one turning off and the other turning on. |
| Centre-aligned PWM | A PWM whose counter counts up to a top value and back down in every period. |
| Valley of the counter | The point where a centre-aligned counter is at zero. New duty values are loaded there. |
| ADC | Analog-to-digital converter: a block that turns a voltage, here a motor current, into a number. |
| Motor control loop | The program that reads the motor currents and writes new duty cycles, once every period. |
| Encoder | A sensor on the shaft that gives two pulse trains. Counting their edges gives the angle and the speed. |
| Break input | An input of the PWM block that forces every output off at once, with no program involved. |
| Masked section | A stretch of code that runs with interrupts disabled. A request that arrives meanwhile waits. |
One Picture: The Board, Filled for a Motor
- File
- Processor
- Memory
- Clock / reset
- Control / FSM
- Power
- Debug
- Report
- 1. Requirements
The numbers are rates and deadlines. Nothing here is large; everything here is late if it is slow.
- 2. Block diagram
Three blocks exist because software would be too slow: dead time, edge counting and the fault path.
- 3. Memory map
A small map. The control loop needs a sine table and a stack, and telemetry needs a buffer.
- 4. Clocks and resets
One clock for the counter and the core keeps the arithmetic simple. Reset must never switch a phase on.
- 5. Interrupts
The control request comes when the conversions end. A masked section delays it, and the delay counts.
- 6. Power modes
A motor chip does not sleep to save power. Its modes are safe states.
- 7. Debug
A halted loop must never leave a phase switched on. Telemetry shows the loop without stopping it.
- 8. Risks
Each is listed with a test: measure the handler, measure the driver, move the sample point.
Read the diagram as text
- Requirements: PWM at 20 kHz on three phases; 1024 steps or more; 500 ns dead time; off within 1 µs of a fault
- Block diagram: core; PWM timer with dead time and a break input; ADC; encoder counter; comparator; UART; SRAM
- Memory map: ap_soc map; PWM at 1000_8000, ADC at 1000_9000, encoder at 1000_A000; SRAM of 8 KiB
- Clocks and resets: PLL 100 MHz for the core and the PWM counter; outputs low from reset until armed
- Interrupts: 21,000 requests a second: the control loop first, then the speed loop
- Power modes: run; standby with the outputs off; fault, latched off until cleared
- Debug: on a debug halt the outputs go off; a fault-cause register; a telemetry stream
- Risks: control takes 44 of 50 percent; dead time against the real gate driver; noise at the sample point
- Requirements → Block diagram: what to build
- Block diagram → Memory map: what to place
- Memory map → Clocks and resets: what to time
- Clocks and resets → Interrupts: what to count
- Interrupts → Power modes: when to wake
- Power modes → Debug: what to see
- Debug → Risks: what is thin
- Risks → Requirements: change and run again
The eight columns of S11.4, filled for a motor controller. Compare column 6 with the sensor hub.
The Smallest Example, Worked by Hand
No simulator yet. Start with the PWM. The counter runs at 100 MHz and counts up and down once in each 50 µs period.
| Entry | Sum | Result |
|---|---|---|
| Top value | 100,000,000 ÷ (2 × 20,000) | 2500 counts |
| Duty steps | one step a count | 2500: more than the 1024 asked for |
| Dead time | 500 ns × 100 MHz | 50 ticks |
| Encoder edges | 1024 lines × 4 edges × 6000 ÷ 60 turns a second | 409,600 a second |
Now walk one period. The core also runs at 100 MHz, so a cycle is 10 ns.
| Time | What happens | Sum |
|---|---|---|
| 0 µs | The counter reaches its valley and starts the two conversions | start |
| 2.0 µs | Both conversions are done; the control request is raised | 2 × 1 µs |
| 4.3 µs | The handler starts, after the worst masked section and its own entry | 2.0 + (200 + 30) cycles × 10 ns |
| 24.3 µs | The loop has written the new duties | 4.3 + 2000 cycles × 10 ns |
| 50 µs | The next valley: the counter loads the new duties | 25.7 µs to spare |
Last, the fault. A handler waits behind the same masked section: (200 + 30 + 50) cycles is 2.8 µs. The limit is 1 µs. Software cannot meet it, even when it is ready.
The Rules
Rule 1: Walk the worst period.
Add the conversions, the longest masked section, the entry and the loop. The sum must end before the next valley.
Rule 2: A job faster than a request is hardware.
If events come many times in one period, count them in hardware and read the count.
Rule 3: A deadline shorter than the worst latency is hardware.
The fault limit of 1 µs is shorter than the 2.8 µs of the best handler. The break input acts alone.
Rule 4: Outputs are off unless software has armed them.
From reset, after a fault and under a debug halt, every switch is off.
Rule 5: Leave half the period free.
Other work, and later growth of the loop, need room. This design keeps the loop under 50 percent.
Rule 6: Size telemetry for the longest stall.
Bytes pile up while the link waits. The buffer holds the rate times the stall.
Sources for These Rules
A counter, a compare value and a duty cycle: Timers: Prescaler, Compare, Capture and PWM. A request waits while interrupts are disabled: Interrupts at the Core.
Half-bridges, dead time, centre-aligned PWM and sampling at the valley are common practice in motor control. So are encoders with four edges a line, break inputs and outputs off under a halt. No book in our library covers them.
Every rate, time and cycle count here is an exercise value. The PLL of 100 MHz is that of ap_soc (docs/soc/ap_soc-facts.md, section 10).
Now Run It: The Sizing Sheet
The lab at the end of this page is the sizing sheet of the controller. Its last two rows come from one PWM period, walked in steps of 10 ns.
pwm top=2500 steps=2500 (11 bits) dead_time=50 ticks ok
encoder edges=409600/s per_period=20.4 -> count in hardware
irq control=20000/s speed=1000/s total=21000/s
telemetry rate=32000 B/s uart=92160 B/s stall=5 ms buffer=160 B -> 256 B
memory sine=2048 telemetry=256 stack=2048 total=4352 -> sram=8192 B
fault hardware=30 ns software=2800 ns limit=1000 ns -> break input in hardware
period adc_done=2000 ns isr_start=4300 ns isr_end=24300 ns next_valley=50000 ns slack=25700 ns
cpu control_share=44% budget=50% okThe pwm row: a top of 2500 gives 2500 steps, which is 11 whole bits. Dead time is 50 ticks.
Rule 2, in the encoder row: 409,600 edges a second, 20.4 in each period. A counter in hardware counts them; the loop reads it once a period.
Rule 3, in the fault row. The break input turns the outputs off in 30 ns: three ticks of the counter clock. A handler would need 2800 ns.
Rule 1, in the period row. The handler starts at 4300 ns and the new duties are written at 24,300 ns. The next valley is at 50,000 ns, so 25,700 ns are left.
Rule 5, in the last row. From the end of the conversions to the new duties is 44 percent of the period, under the budget of 50.
Rule 6, in the telemetry row. 32,000 bytes a second, and the link may stall for 5 ms: 160 bytes pile up, so the buffer is 256.
This lab leaves things out. There is no motor, no current and no control law: the loop is a count of cycles. All timing is in whole steps of 10 ns.
The Rest of the Board
| Entry | Choice | Reason |
|---|---|---|
| PWM timer | 1000_8000, 4 KiB | Six outputs with dead time and a break input. The Timer and PWM of ap_soc has none of these. |
| ADC | 1000_9000, 4 KiB | Two conversions, started by the valley of the counter, not by software. |
| Encoder counter | 1000_A000, 4 KiB | Counts every edge; the loop reads the count once a period. |
| SRAM | 8000_0000, 8 KiB | 4352 bytes: a sine table, the telemetry buffer and the stack. |
| Clocks | PLL at 100 MHz for core and counter | One clock: the duty and dead-time sums use one tick. |
| Reset | outputs low and the gate drivers disabled until armed | A reset in the middle of a period must not leave a switch on. |
| Source 7 | conversions done: the control loop, highest priority | 20,000 a second. |
| Source 8 | fault, after the break input has acted | Only to record the cause; the hardware has already stopped the motor. |
| Entry | Choice | Reason |
|---|---|---|
| Run | outputs follow the duties | The only state with switches on. |
| Standby | outputs off, gate drivers disabled | The state after reset and before arming. |
| Fault | outputs off, latched until software clears it | A fault must not clear itself and restart the motor. |
| Debug halt | outputs off while the core is halted | The hook of this page: a frozen loop left a phase on. |
| Risk: loop time | 44 of 50 percent | Measure the real loop on the real core. |
| Risk: dead time | 500 ns against the real switches | Measure the turn-off time of the gate driver and switch. |
| Risk: sample point | switching noise near the valley | Check on a board; move the start of the conversions if needed. |
Why It Is Built This Way
| Choice | The alternative | What it buys, and what it costs |
|---|---|---|
| A break input in hardware | A fault interrupt with the top priority | 30 ns in place of 2800 ns. The cost is a wire from a comparator and a latch. |
| Conversions started by the counter | Started by software | The sample point is the same in every period. Software start would wander with the latency. |
| Duties loaded at the valley | Loaded at once when written | No period ever mixes old and new duties. The cost is a full period of delay. |
| Outputs off under a debug halt | Outputs frozen as they were | A halt is safe. The cost: the motor cannot run while you step through code. |
The Trap: Halting a Running Motor
This is the hook. The debugger halted the core. The PWM block kept its last duty, and one switch stayed on. The current rose until the switch burnt out.
Rule 4 forbids it. Column 7 should have asked what the outputs do under a halt. The answer is off.
The second trap is the fault interrupt. It was given the top priority and thought fast enough. Behind a masked section of 200 cycles it took 2.8 µs.
The third trap is the average. On most periods the handler starts at once. On the period with the longest masked section, the loop takes 44 percent.
At Scale
This section looks ahead, to controllers for cars and robots.
Many motors
Six motors need six PWM blocks whose valleys are spread out, so that six loops do not all start at once.
Safety as a requirement
In cars, column 1 holds safety goals with their own standards and tests. Column 8 becomes a formal analysis of faults.
Two cores
Some chips run the loop on one core and everything else on another, so a masked section elsewhere cannot delay it.
Try It
Run the lab. Find the deadline of the loop in the period row, and the fault times in the fault row.
Set FOC_CYCLES to 2400. Does the loop still meet the next valley? Does it meet the 50 percent budget?
Set CNT_HZ to 40000000. Which requirement fails first, steps or dead time?
Names Printed by the Lab
| Name | Meaning |
|---|---|
| top, steps, bits | The count at which the counter turns round; the duty steps in a period; the whole bits they give. |
| dead_time | The dead time in ticks of the counter clock, rounded up. |
| edges, per_period | Encoder edges each second at top speed, and in one PWM period. |
| control, speed, total | Interrupt requests each second, by source. |
| rate, uart, stall, buffer | Telemetry bytes each second; what the UART can send; the longest stall of the link; the bytes that pile up meanwhile. |
| sine, telemetry, stack | The SRAM for a sine table, the telemetry buffer and the stack, in bytes. |
| hardware, software | The time from a fault to the outputs off, by a break input or by a handler behind the worst masked section. |
| adc_done, isr_start, isr_end | In one period: the conversions end, the handler starts, the new duties are written. |
| next_valley, slack | The time at which the counter loads the duties, and the time left before it. |
| control_share | The time from the end of the conversions to the new duties, as a share of the period. |
pwm top=2500 steps=2500 (11 bits) dead_time=50 ticks ok encoder edges=409600/s per_period=20.4 -> count in hardware irq control=20000/s speed=1000/s total=21000/s telemetry rate=32000 B/s uart=92160 B/s stall=5 ms buffer=160 B -> 256 B memory sine=2048 telemetry=256 stack=2048 total=4352 -> sram=8192 B fault hardware=30 ns software=2800 ns limit=1000 ns -> break input in hardware period adc_done=2000 ns isr_start=4300 ns isr_end=24300 ns next_valley=50000 ns slack=25700 ns cpu control_share=44% budget=50% okCheck Yourself
Common Mistakes
Wrong Time the control loop without the masked sections.
The worst period has the longest masked section in front of the loop.
Fix Add the longest masked section to the latency.
Wrong Handle an over-current with a top-priority interrupt.
Behind a masked section, the handler takes 2.8 µs against a limit of 1 µs.
Fix Use a break input that turns the outputs off in hardware.
Wrong Let the PWM keep its outputs when the core halts.
A frozen loop can leave a switch on.
Fix Turn every output off while the core is halted.
Remember
- Walk the worst period: conversions, the longest masked section, entry and loop must end before the next valley.
- Jobs faster than a request, and deadlines shorter than the worst latency, are hardware.
- Safe states replace power modes: outputs are off unless software has armed them.
- Under a debug halt, the outputs turn off.