Drive motor IN1-IN4 from TIM4/TIM5 compare outputs (P23=TIM4_CHA, P24=TIM4_CHB, P25=TIM5_CHA, P26=TIM5_CHB) on a 15.625 kHz carrier derived at init from the actual PCLK (PCLK/2, 128-tick period; MCU boots on the 4 MHz internal RCH). Each half-step writes the four compare registers: an energized coil gets the speed level's duty cycle (85/90/95/100%), a de-energized coil gets a compare value above the period so its output stays static low. The advanced timers stop (outputs low) at standstill and power-off; coils stay energized at the level duty while parked for a reversal. No forced-output conditions are used, and the half-step sequencing and trapezoidal ramp are unchanged. Docs updated: FR19 in FIRMWARE_REQUIREMENTS.md, README.md, STATE_MACHINE.md.
62 lines
2.8 KiB
Markdown
62 lines
2.8 KiB
Markdown
# Turning Table Firmware (HC32F005C6PA)
|
||
|
||
Bare-metal C firmware for a motorized turning table. Drives a 28BYJ-48 stepper motor
|
||
(ULN2003 board, half-step, trapezoidal acceleration) and is controlled with a single button
|
||
plus one status LED. Power is held by a GPIO latch so the system can switch itself off.
|
||
|
||
## Hardware
|
||
|
||
| Pin | Function | Config |
|
||
|---|---|---|
|
||
| P02 | LED | open-drain output, active low |
|
||
| P23–P26 | Motor IN1–IN4 | timer compare output (TIM4_CHA/CHB, TIM5_CHA/CHB), PWM |
|
||
| P32 | Button | input, internal pull-up, active low |
|
||
| P33 | Power latch | push-pull output, high = hold power, low = cut power |
|
||
|
||
## Behavior
|
||
|
||
- Power-on → IDLE (LED slow blink 0.5 Hz). Holding the button at power-on is ignored.
|
||
- Short press in IDLE → rotate at Speed 1; short press again cycles Speed 1→2→3→4→1
|
||
(2.5 / 5 / 7.5 / 10 RPM), LED blinks N times as feedback.
|
||
- Winding current is PWM-limited per speed level (85% / 90% / 95% / 100%, FR19): the IN pins are
|
||
driven by TIM4/TIM5 compare outputs at a 15.625 kHz carrier, each half-step gating the two
|
||
active coils between PWM and static off.
|
||
- Double press → reverse direction (LED quick double-blink); speed changes and reversal ramp
|
||
smoothly to avoid skipped steps.
|
||
- Long press (> 2 s) → motor decelerates to a full stop, LED off, power cut via P33.
|
||
|
||
Full specification: `FIRMWARE_REQUIREMENTS.md`. State machine details: `STATE_MACHINE.md`.
|
||
|
||
## Repository layout
|
||
|
||
```
|
||
src/app/ application: main.c (system state machine), button.c, led.c, motor.c, power.c
|
||
src/driver/ vendor device driver library (gpio, bt, sysctrl, ...)
|
||
src/common/ vendor system headers, board pin map (board_stkhc32f005.h)
|
||
src/gcc/ startup code and linker script (32K flash / 4K RAM)
|
||
docs/ MCU datasheet, reference manual, DDL docs, vendor examples
|
||
```
|
||
|
||
## Build
|
||
|
||
Requires `arm-none-eabi-gcc` and `make`:
|
||
|
||
```
|
||
make # builds build/turntable.elf/.bin/.hex (~4.5K flash, ~2.1K RAM used)
|
||
make clean
|
||
```
|
||
|
||
Only the DDL modules in use are compiled. Flashing is done with your own SWD programmer
|
||
(no flash target is provided).
|
||
|
||
## Notes
|
||
|
||
- The base timers run in mode 2 (reload): interval = `0x10000 − ARR` with CNT primed to ARR
|
||
(HC32F005 reference manual §9) — see `TickInit()` and `Motor_ArrForSps()`.
|
||
- The 1 ms system tick (TIM0) drives button scanning and LED patterns; the stepper generator
|
||
(TIM1) runs autonomously in its own ISR. Winding current is chopped by TIM4/TIM5 in hardware:
|
||
both run the same 15.625 kHz carrier (ADT clock = PCLK/2, period derived at init from
|
||
`Sysctrl_GetPClkFreq()` — the MCU boots on the 4 MHz internal RCH — 2 MHz / 128 = 15.625 kHz),
|
||
and each half-step writes the four compare registers — active coils get the duty-cycle compare
|
||
value, inactive coils get a value above the period so their output stays static low.
|