Files
TurningTable/src/app/main.c
T
kyle c0849f91b6 Add PWM winding-current control with per-speed duty (FR19)
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.
2026-11-08 12:34:48 +00:00

176 lines
4.4 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);
Sysctrl_SetPeripheralGate(SysctrlPeripheralAdvTim, TRUE); /* motor PWM (TIM4/TIM5) */
}
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;
}
}
}