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.
153 lines
4.6 KiB
C
153 lines
4.6 KiB
C
#include "app.h"
|
|
#include "gpio.h"
|
|
#include "board_stkhc32f005.h"
|
|
|
|
#define BTN_DEBOUNCE_MS 20u
|
|
#define BTN_SHORT_MAX_MS 1000u /* FR5: short press < 1 s */
|
|
#define BTN_LONG_MS 2000u /* FR6: long press > 2 s */
|
|
#define BTN_DOUBLE_WINDOW_MS 300u /* FR7: release-to-press gap < 300 ms */
|
|
|
|
static uint8_t s_u8DebCnt = 0u;
|
|
static uint8_t s_u8GateRelCnt = 0u;
|
|
static boolean_t s_bPressed = FALSE; /* debounced state, TRUE = pressed (active low) */
|
|
static boolean_t s_bWaitRelease = FALSE; /* boot gate: button held at power-on */
|
|
static boolean_t s_bLongFired = FALSE;
|
|
static boolean_t s_bMaybeDbl = FALSE;
|
|
static uint16_t s_u16HoldMs = 0u;
|
|
static uint16_t s_u16DblWndMs = 0u;
|
|
static en_btn_event_t s_enEvent = BTN_EVT_NONE;
|
|
|
|
void ButtonInit(void)
|
|
{
|
|
stc_gpio_cfg_t stcCfg;
|
|
|
|
stcCfg.enDir = GpioDirIn;
|
|
stcCfg.enDrv = GpioDrvL;
|
|
stcCfg.enPu = GpioPuEnable;
|
|
stcCfg.enPd = GpioPdDisable;
|
|
stcCfg.enOD = GpioOdDisable;
|
|
|
|
Gpio_Init(APP_BUTTON_PORT, APP_BUTTON_PIN, &stcCfg);
|
|
|
|
/* if the button is held at power-on, require a release before
|
|
any button event is recognized */
|
|
if (FALSE == Gpio_GetInputIO(APP_BUTTON_PORT, APP_BUTTON_PIN))
|
|
{
|
|
s_bWaitRelease = TRUE;
|
|
}
|
|
}
|
|
|
|
void ButtonScan1ms(void)
|
|
{
|
|
boolean_t bRaw;
|
|
boolean_t bPrev;
|
|
|
|
bRaw = (FALSE == Gpio_GetInputIO(APP_BUTTON_PORT, APP_BUTTON_PIN)) ? TRUE : FALSE;
|
|
|
|
/* debounce: track the debounced press state */
|
|
bPrev = s_bPressed;
|
|
if (bRaw == s_bPressed)
|
|
{
|
|
s_u8DebCnt = 0u;
|
|
}
|
|
else if (++s_u8DebCnt >= BTN_DEBOUNCE_MS)
|
|
{
|
|
s_u8DebCnt = 0u;
|
|
s_bPressed = bRaw;
|
|
}
|
|
|
|
/* boot gate: swallow every event until the button has been truly
|
|
released. The exit condition must be a stable raw release, not the
|
|
debounced state: s_bPressed is still FALSE on the first scans while
|
|
the power-on press is held. */
|
|
if (TRUE == s_bWaitRelease)
|
|
{
|
|
if (TRUE == bRaw) /* still pressed (active low) */
|
|
{
|
|
s_u8GateRelCnt = 0u;
|
|
}
|
|
else if (++s_u8GateRelCnt >= BTN_DEBOUNCE_MS)
|
|
{
|
|
s_u8GateRelCnt = 0u;
|
|
s_bWaitRelease = FALSE;
|
|
s_bPressed = FALSE; /* sync: next press is a clean edge */
|
|
s_u16HoldMs = 0u;
|
|
s_u16DblWndMs = 0u;
|
|
s_bMaybeDbl = FALSE;
|
|
s_bLongFired = FALSE;
|
|
}
|
|
return; /* no classification, no hold timing, no events */
|
|
}
|
|
|
|
/* debounced edge classification */
|
|
if (bPrev != s_bPressed)
|
|
{
|
|
if (TRUE == s_bPressed)
|
|
{
|
|
/* press edge */
|
|
s_u16HoldMs = 0u;
|
|
s_bLongFired = FALSE;
|
|
if (s_u16DblWndMs > 0u)
|
|
{
|
|
s_bMaybeDbl = TRUE;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* release edge: classify this hold */
|
|
if (FALSE == s_bLongFired)
|
|
{
|
|
if (TRUE == s_bMaybeDbl)
|
|
{
|
|
/* second press of a candidate double */
|
|
s_bMaybeDbl = FALSE;
|
|
if (s_u16HoldMs < BTN_SHORT_MAX_MS)
|
|
{
|
|
s_enEvent = BTN_EVT_DOUBLE;
|
|
s_u16DblWndMs = 0u;
|
|
}
|
|
else
|
|
{
|
|
s_u16DblWndMs = 0u; /* second press invalid: discard pair */
|
|
}
|
|
}
|
|
else if (s_u16HoldMs < BTN_SHORT_MAX_MS)
|
|
{
|
|
/* short release: arm the double-press window, dispatch if it expires */
|
|
s_u16DblWndMs = BTN_DOUBLE_WINDOW_MS;
|
|
}
|
|
/* release after 1 s < hold < 2 s: spec gray zone, no event */
|
|
}
|
|
s_bLongFired = FALSE;
|
|
s_u16HoldMs = 0u;
|
|
}
|
|
}
|
|
|
|
if (TRUE == s_bPressed)
|
|
{
|
|
s_u16HoldMs++;
|
|
if ((FALSE == s_bLongFired) && (s_u16HoldMs >= BTN_LONG_MS))
|
|
{
|
|
s_bLongFired = TRUE;
|
|
s_enEvent = BTN_EVT_LONG; /* fired while still held */
|
|
s_u16DblWndMs = 0u;
|
|
s_bMaybeDbl = FALSE;
|
|
}
|
|
}
|
|
else if (s_u16DblWndMs > 0u)
|
|
{
|
|
s_u16DblWndMs--;
|
|
if (0u == s_u16DblWndMs)
|
|
{
|
|
s_enEvent = BTN_EVT_SHORT; /* window expired: single short press */
|
|
}
|
|
}
|
|
}
|
|
|
|
en_btn_event_t ButtonGetEvent(void)
|
|
{
|
|
en_btn_event_t enEvent = s_enEvent;
|
|
s_enEvent = BTN_EVT_NONE;
|
|
return enEvent;
|
|
}
|