Files
TurningTable/src/app/main.c
T
kyle 66f5bfbf1a Initial commit: HC32F005 turning table firmware
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.
2026-11-06 08:14:55 +00:00

175 lines
4.3 KiB
C

#include "ddl.h"
#include "bt.h"
#include "sysctrl.h"
#include "interrupts_hc32f005.h"
#include "app.h"
#define TICK_HZ 1000u
#define POWER_OFF_LED_MS 50u
#define POWER_OFF_DECEL_TIMEOUT_MS 2000u
static en_app_state_t s_enState = STATE_IDLE;
static uint8_t s_u8Level = 1u;
static volatile uint16_t s_u16TickMs = 0u;
static void PeripheralsInit(void)
{
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
Sysctrl_SetPeripheralGate(SysctrlPeripheralBt, TRUE);
}
static void TickInit(void)
{
stc_bt_cfg_t stcCfg;
uint32_t u32Pclk = Sysctrl_GetPClkFreq();
stcCfg.enGateP = BtPositive;
stcCfg.enGate = BtGateDisable;
stcCfg.enPRS = BtPCLKDiv16;
stcCfg.enTog = BtTogDisable;
stcCfg.enCT = BtTimer;
stcCfg.enMD = BtMode2;
Bt_Init(TIM0, &stcCfg);
/* Mode 2: CNT counts up to 0xFFFF, then reloads from ARR, so the
interval is (0x10000 - ARR) ticks and CNT must start at ARR */
{
uint32_t u32Ticks = u32Pclk / 16u / TICK_HZ;
uint16_t u16Arr;
if (u32Ticks < 2u)
{
u32Ticks = 2u;
}
if (u32Ticks > 0xFFF0u)
{
u32Ticks = 0xFFF0u;
}
u16Arr = (uint16_t)(0x10000u - u32Ticks);
Bt_ARRSet(TIM0, u16Arr);
Bt_Cnt16Set(TIM0, u16Arr);
}
Bt_ClearIntFlag(TIM0);
Bt_EnableIrq(TIM0);
EnableNvic(TIM0_IRQn, IrqLevel3, TRUE);
Bt_Run(TIM0);
}
/* 1 ms system tick: button scanning and LED pattern engine */
void Tim0_IRQHandler(void)
{
if (TRUE == Bt_GetIntFlag(TIM0))
{
Bt_ClearIntFlag(TIM0);
s_u16TickMs++;
ButtonScan1ms();
Led1ms();
}
}
/* stepper step generator */
void Tim1_IRQHandler(void)
{
if (TRUE == Bt_GetIntFlag(TIM1))
{
Bt_ClearIntFlag(TIM1);
MotorStepIsr();
}
}
static void PowerOff(void)
{
uint16_t u16T0;
/* FR12: decelerate to a full stop before cutting power.
The step ISR ramps the motor down and stops the timer at standstill. */
MotorStopSmooth();
u16T0 = s_u16TickMs;
while ((FALSE != MotorIsRunning()) &&
((uint16_t)(s_u16TickMs - u16T0) < POWER_OFF_DECEL_TIMEOUT_MS))
{
; /* motor ISR ramps down while we wait */
}
if (FALSE != MotorIsRunning())
{
MotorStop(); /* timeout guard: force a stop, then cut power anyway */
}
u16T0 = s_u16TickMs;
LedSetState(STATE_POWER_OFF); /* FR13 */
while ((uint16_t)(s_u16TickMs - u16T0) < POWER_OFF_LED_MS)
{
; /* let the LED-off state be visible before power collapses */
}
__disable_irq();
PowerCut(); /* FR3 */
for (;;)
{
; /* terminal: main power is being cut externally */
}
}
int32_t main(void)
{
en_btn_event_t enEvent;
PeripheralsInit();
PowerInit();
PowerHoldOn(); /* FR2 */
LedInit();
MotorInit();
ButtonInit();
TickInit();
s_enState = STATE_IDLE;
s_u8Level = 1u;
LedSetState(STATE_IDLE); /* FR14 */
while (1)
{
enEvent = ButtonGetEvent();
if (BTN_EVT_LONG == enEvent) /* FR3, FR6 */
{
s_enState = STATE_POWER_OFF;
PowerOff();
}
switch (s_enState)
{
case STATE_IDLE:
if ((BTN_EVT_SHORT == enEvent) || (BTN_EVT_DOUBLE == enEvent))
{
MotorStart(); /* FR8 */
s_enState = STATE_ROTATING;
s_u8Level = 1u;
LedSetState(STATE_ROTATING);
LedBlink(1u); /* FR16: feedback for Speed 1 */
}
break;
case STATE_ROTATING:
if (BTN_EVT_SHORT == enEvent) /* FR9, FR16 */
{
s_u8Level = (uint8_t)(((s_u8Level % 4u)) + 1u);
MotorSpeedLevelSet(s_u8Level);
LedBlink(s_u8Level);
}
else if (BTN_EVT_DOUBLE == enEvent) /* FR10, FR17 */
{
MotorReverse();
LedBlinkQuick();
}
break;
case STATE_POWER_OFF:
default:
break;
}
}
}