Firmware (src/app): - system state machine: IDLE / ROTATING / POWER_OFF - button: 20 ms debounce, short/double/long classification, boot hold-off - LED: idle slow blink, solid, speed/direction feedback patterns - motor: 8-phase half-step drive with trapezoidal acceleration, speed levels 2.5/5/7.5/10 RPM, reversal via standstill - power latch on P33 (push-pull, high = hold, low = cut) with decelerate-to-stop sequence before power-off (FR18) - base timer mode-2 reload semantics (interval = 0x10000 - ARR) Build: Makefile for arm-none-eabi-gcc, selective DDL compile, vendor startup + linker script under src/. Docs: FIRMWARE_REQUIREMENTS.md, STATE_MACHINE.md, README.md. Vendor datasheet/RM/DDL references under docs/ are kept locally and tracked in a separate shared repo.
6.9 KiB
State Machines — HC32F005 Turning Table Firmware
This document describes every state machine in the firmware, how they interact, and which
requirement (FR) each behavior implements. Reference: FIRMWARE_REQUIREMENTS.md.
Implementation: src/app/ (main.c, button.c, led.c, motor.c, power.c).
There are four state machines:
- System state machine (
main.c) — the three system states and button-driven transitions - Button event machine (
button.c) — debouncing, press classification, boot hold-off - LED pattern engine (
led.c) — idle blink, solid, feedback blinks - Motor speed machine (
motor.c) — half-step sequencing with trapezoidal ramping
The button and LED machines run from the 1 ms system tick (TIM0 ISR); the motor machine runs in the TIM1 step ISR. The main loop only dispatches button events into the system machine.
1. System state machine (main.c)
stateDiagram-v2
[*] --> IDLE : power applied (FR1)
IDLE --> ROTATING : SHORT or DOUBLE (FR8)
ROTATING --> ROTATING : SHORT, cycle speed 1-2-3-4-1 (FR9)
ROTATING --> ROTATING : DOUBLE, reverse direction (FR10)
IDLE --> POWER_OFF : LONG (FR3)
ROTATING --> POWER_OFF : LONG, decel to stop first (FR18)
POWER_OFF --> [*] : P33 low, power cut (FR3)
Behavior per state:
| State | Entry action | LED | Exit triggers |
|---|---|---|---|
STATE_IDLE |
power latch already held (P33 high, FR2) | slow blink 1 s on / 1 s off (FR14) | SHORT/DOUBLE → ROTATING; LONG → POWER_OFF |
STATE_ROTATING |
motor starts at speed 1, ramps up | solid on (FR15) | SHORT → next speed + N blinks (FR9/FR16); DOUBLE → reverse + double blink (FR10/FR17); LONG → decelerate → POWER_OFF |
STATE_POWER_OFF |
motor decelerated to standstill (FR18), LED off (FR13) | off | terminal: P33 low → power cut |
Power-off sequence (FR18/FR3): MotorStopSmooth() → wait for standstill (bounded, 2 s guard,
force-stop on timeout) → LED off → 50 ms visibility delay → P33 low → park forever.
2. Button event machine (button.c)
One scan per 1 ms from the TIM0 tick ISR. Timing constants: debounce 20 ms, short < 1 s (FR5), long ≥ 2 s (FR6), double window 300 ms (FR7).
stateDiagram-v2
[*] --> BootGate : power-on, button sampled (held = arm)
BootGate --> Idle : stable release 20 ms (boot press swallowed)
Idle --> Pressed : press debounced 20 ms
Pressed --> Held : hold >= 2 s, fire LONG live (FR6)
Held --> Idle
Pressed --> Released : release
Released --> Window : hold < 1 s, arm 300 ms (FR5/FR7)
Released --> Idle : hold 1-2 s gray zone, no event
Window --> Windowed : second press inside window (FR7)
Window --> Idle : window expires, fire SHORT (FR5)
Windowed --> Window : second release < 1 s, fire DOUBLE (FR7)
Rules and rationale:
- Boot gate: if the button reads pressed at
ButtonInit(), every event is swallowed until a stable 20 ms raw release is seen (the power-on hold must not rotate, reverse, or power off). - LONG fires live while held so power-off responds promptly; once fired, nothing else from that hold generates events.
- SHORT is delayed 300 ms — necessary to distinguish a single press from the first half of a double press (FR7). This dispatch latency is intentional.
- Double press consumes the window: only one DOUBLE per pair.
- Gray zone (1 s ≤ hold < 2 s) produces no event (spec defines only short <1 s and long >2 s).
3. LED pattern engine (led.c)
Stepped every 1 ms from the same tick ISR. Non-blocking: patterns overlay the base mode and return to it automatically. LED is open-drain, active low (P02).
stateDiagram-v2
[*] --> Off
Off --> SlowBlink : STATE_IDLE (FR14)
Off --> Solid : STATE_ROTATING (FR15)
SlowBlink --> Solid : STATE_ROTATING (FR15)
SlowBlink --> Off : STATE_POWER_OFF (FR13)
Solid --> Pattern : blink(N) speed feedback (FR16)
Solid --> Pattern : blinkQuick double-blink feedback (FR17)
Pattern --> Solid : pattern sequence finished
Solid --> Off : STATE_POWER_OFF (FR13)
| Mode | Timing | Set by |
|---|---|---|
| Slow blink | 1000 ms on / 1000 ms off (0.5 Hz) | LedSetState(STATE_IDLE) |
| Solid | on | LedSetState(STATE_ROTATING) |
| Pattern (speed) | N × (150 ms on / 150 ms off) | LedBlink(N) on speed change |
| Pattern (direction) | 2 × (80 ms on / 80 ms off) | LedBlinkQuick() on reversal |
| Off | off | LedSetState(STATE_POWER_OFF) |
Feedback patterns only start while in Solid (rotating); a new request during a pattern is ignored.
4. Motor speed machine (motor.c)
Runs in the TIM1 step ISR. Speed is stepless (steps/s); levels 1–4 map to 171 / 341 / 512 / 683 steps/s (2.5 / 5 / 7.5 / 10 RPM at 4096 half-steps/rev). Accel/decel 500 steps/s², start speed 60 steps/s, 8-entry half-step table on P23–P26 (FR11).
stateDiagram-v2
[*] --> Stopped : coils released
Stopped --> Accel : start or new target (FR8/FR9)
Accel --> Steady : speed == target
Steady --> Accel : target raised (FR9)
Steady --> Decel : target lowered
Steady --> Decel : reversal requested (FR10)
Steady --> Decel : smooth stop requested (FR18)
Decel --> Accel : speed settles at new target
Decel --> Flip : speed == 0 and reversal pending (FR10)
Flip --> Accel : direction flipped, ramp to level speed
Decel --> Stopped : speed == 0, no reversal pending (FR18)
Ramp rule: each step applies Δv = accel / v (min 1 step/s) toward the target and recomputes the
timer reload, so the profile is trapezoidal and no steps are skipped on speed/direction changes
(FR12). Reversal deliberately passes through standstill (coils stay energized at zero speed, then
flip direction and ramp back up).
TIM0 note: timer mode 2 is a reload timer — the interval is 0x10000 − ARR ticks with CNT
primed to ARR (HC32F005 RM §9).
Cross-machine interaction
TIM0 ISR (1 ms) main loop TIM1 ISR
┌───────────────┐ BTN_EVT_* ┌───────────────┐ ┌──────────────┐
│ button scan │───────────────▶ │ system machine│────────▶ │ motor ramp │
│ LED patterns │◀─────────────── │ Led/Motor API │ │ half-step ISR│
└───────────────┘ LED commands └───────────────┘ └──────────────┘
- Button events are produced only by the button machine and consumed only by the main loop.
- LED state follows the system machine; feedback blinks are triggered by the same events that change the motor.
- The motor machine is autonomous once a target is set — the main loop never waits on it, except during the bounded power-off deceleration wait (FR18).