diff --git a/FIRMWARE_REQUIREMENTS.md b/FIRMWARE_REQUIREMENTS.md index ca1ef06..4317390 100644 --- a/FIRMWARE_REQUIREMENTS.md +++ b/FIRMWARE_REQUIREMENTS.md @@ -36,6 +36,7 @@ This document outlines the firmware requirements for a motorized turning table b - **FR10:** While in the **ROTATING** state, a double press shall reverse the motor's direction of rotation. - **FR11:** The firmware shall drive the stepper motor in half-step mode to ensure smoother rotation. - **FR12:** Rotation shall be continuous. Apply acceleration control when changing speed and direction to avoid skipping steps. +- **FR19:** The firmware shall control the stepper motor winding current with PWM to reduce power consumption. Each speed level shall use the following winding duty cycle: Speed 1 = 85%, Speed 2 = 90%, Speed 3 = 95%, Speed 4 = 100% (full current, effectively static on). ### 3.5. LED Indicator Logic - **FR13:** In the **POWER_OFF** state, the LED is off. @@ -53,9 +54,9 @@ This document outlines the firmware requirements for a motorized turning table b | Pin | Function | Configuration | Notes | |---|---|---|---| | P02 | LED | Open-Drain Output | Active Low | -| P23 | Motor IN1 | Push-Pull Output | | -| P24 | Motor IN2 | Push-Pull Output | | -| P25 | Motor IN3 | Push-Pull Output | | -| P26 | Motor IN4 | Push-Pull Output | | +| P23 | Motor IN1 | Timer compare output (TIM4_CHA) | PWM-driven by default (GPIO before start) | +| P24 | Motor IN2 | Timer compare output (TIM4_CHB) | PWM-driven by default (GPIO before start) | +| P25 | Motor IN3 | Timer compare output (TIM5_CHA) | PWM-driven by default (GPIO before start) | +| P26 | Motor IN4 | Timer compare output (TIM5_CHB) | PWM-driven by default (GPIO before start) | | P32 | Button | Input with Pull-up | Active Low | | P33 | Power Latch | Push-Pull Output | | diff --git a/Makefile b/Makefile index 025fd12..3ca5685 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ DDL_SRCS := \ src/driver/src/gpio.c \ src/driver/src/bt.c \ src/driver/src/sysctrl.c \ + src/driver/src/adt.c \ src/driver/src/ddl.c SYS_SRCS := \ diff --git a/README.md b/README.md index f1ead45..411918d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ plus one status LED. Power is held by a GPIO latch so the system can switch itse | Pin | Function | Config | |---|---|---| | P02 | LED | open-drain output, active low | -| P23–P26 | Motor IN1–IN4 | push-pull output | +| 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 | @@ -18,6 +18,9 @@ plus one status LED. Power is held by a GPIO latch so the system can switch itse - 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. @@ -51,4 +54,8 @@ Only the DDL modules in use are compiled. Flashing is done with your own SWD pro - 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. + (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. diff --git a/STATE_MACHINE.md b/STATE_MACHINE.md index 4697309..5f64a73 100644 --- a/STATE_MACHINE.md +++ b/STATE_MACHINE.md @@ -109,7 +109,15 @@ Feedback patterns only start while in Solid (rotating); a new request during a p 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). +60 steps/s, 8-entry half-step table (FR11). + +Winding current (FR19): the IN pins are not GPIO anymore — they are the TIM4/TIM5 compare +outputs (P23=TIM4_CHA, P24=TIM4_CHB, P25=TIM5_CHA, P26=TIM5_CHB) running one shared 15.625 kHz +carrier. Each half-step the step ISR writes the four compare registers: an energized coil gets +the compare value of the speed level's duty cycle (85/90/95/100%), a de-energized coil gets a +compare value above the timer period, which never matches and holds the line static low. The +timers stop (outputs low) at final standstill; coils stay energized at the level duty while +parked for a reversal. ```mermaid stateDiagram-v2 diff --git a/src/app/main.c b/src/app/main.c index 101dd5f..9fcdbdd 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -16,6 +16,7 @@ static void PeripheralsInit(void) { Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); Sysctrl_SetPeripheralGate(SysctrlPeripheralBt, TRUE); + Sysctrl_SetPeripheralGate(SysctrlPeripheralAdvTim, TRUE); /* motor PWM (TIM4/TIM5) */ } static void TickInit(void) diff --git a/src/app/motor.c b/src/app/motor.c index 0050340..55679ff 100644 --- a/src/app/motor.c +++ b/src/app/motor.c @@ -1,6 +1,8 @@ #include "app.h" +#include "adt.h" #include "bt.h" #include "gpio.h" +#include "sysctrl.h" #include "board_stkhc32f005.h" #include "interrupts_hc32f005.h" @@ -9,6 +11,20 @@ #define MOTOR_START_SPS 60u /* speed used for the first step from standstill */ #define MOTOR_PRESCALE_DIV 16u /* BtPCLKDiv16 */ +/* Winding current control (FR19): the IN pins are driven by TIM4/TIM5 compare + outputs running a 15.625 kHz PWM carrier. Each coil line has two states: PWM at + the current duty cycle (energized) or static low (de-energized). + The low carrier trades a little current ripple for less ULN2003 switching + loss; it sits at the edge of hearing. The system runs on the 4 MHz internal + RCH by default, so the carrier period is computed from the actual peripheral + clock (do not hardcode it). PCLK/2 = 2 MHz -> 2 MHz / 128 = 15.625 kHz. */ +#define MOTOR_PWM_HZ 15625u /* target carrier frequency */ +#define MOTOR_PWM_CK_DIV 2u /* ADT clock divisor: PCLK/2 (AdtClkPClk0Div2) */ +#define MOTOR_PWM_PULSES_MIN 16u /* min ticks per period for duty resolution */ + +/* Winding current duty cycle per speed level (%): level 1..4 */ +static const uint8_t s_au8DutyPct[MOTOR_SPEED_NUM] = {85u, 90u, 95u, 100u}; + /* Speed levels in half-steps/s (4096 half-steps/rev => 2.5 / 5 / 7.5 / 10 RPM) */ static const uint16_t s_au16SpeedSps[MOTOR_SPEED_NUM] = {171u, 341u, 512u, 683u}; @@ -26,23 +42,49 @@ static uint32_t s_u32CurSps = 0u; static uint32_t s_u32TgtSps = 0u; static uint8_t s_u8Level = 1u; static boolean_t s_bRevPending = FALSE; +static uint16_t s_u16CmpOn = 0u; /* compare value of the active duty cycle */ +static uint16_t s_u16CmpOff = 0u; /* compare value above the period: static low */ +static uint16_t s_u16PwmPeriod = 0u; /* ADT period register value */ +/* Port polarity: the port is set low at period match and high at compare match, + so the on-time is (pulses - cmp) ticks and the duty is (pulses - cmp)/pulses. */ +static uint16_t Motor_CmpForPercent(uint8_t u8Pct) +{ + uint32_t u32On; + uint32_t u32Pulses = (uint32_t)s_u16PwmPeriod + 1u; + + if (u8Pct > 100u) + { + u8Pct = 100u; + } + u32On = (u32Pulses * u8Pct) / 100u; + if (u32On > u32Pulses) + { + u32On = u32Pulses; + } + + return (uint16_t)(u32Pulses - u32On); +} + +/* Energize/de-energize the four windings of the current half-step phase. + An energized coil gets the PWM duty cycle; a de-energized coil gets a compare + value above the period, which never matches and holds the line static low. */ static void Motor_WritePhase(uint8_t u8Phase) { uint8_t u8Val = s_au8PhaseTable[u8Phase & 0x07u]; - Gpio_WriteOutputIO(APP_MOTOR_IN1_PORT, APP_MOTOR_IN1_PIN, (u8Val & 0x01u) ? TRUE : FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN2_PORT, APP_MOTOR_IN2_PIN, (u8Val & 0x02u) ? TRUE : FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN3_PORT, APP_MOTOR_IN3_PIN, (u8Val & 0x04u) ? TRUE : FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN4_PORT, APP_MOTOR_IN4_PIN, (u8Val & 0x08u) ? TRUE : FALSE); + Adt_SetCompareValue(M0P_ADTIM4, AdtCompareA, (u8Val & 0x01u) ? s_u16CmpOn : s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM4, AdtCompareB, (u8Val & 0x02u) ? s_u16CmpOn : s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM5, AdtCompareA, (u8Val & 0x04u) ? s_u16CmpOn : s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM5, AdtCompareB, (u8Val & 0x08u) ? s_u16CmpOn : s_u16CmpOff); } -static void Motor_Release(void) +static void Motor_CoilsOff(void) { - Gpio_WriteOutputIO(APP_MOTOR_IN1_PORT, APP_MOTOR_IN1_PIN, FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN2_PORT, APP_MOTOR_IN2_PIN, FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN3_PORT, APP_MOTOR_IN3_PIN, FALSE); - Gpio_WriteOutputIO(APP_MOTOR_IN4_PORT, APP_MOTOR_IN4_PIN, FALSE); + Adt_SetCompareValue(M0P_ADTIM4, AdtCompareA, s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM4, AdtCompareB, s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM5, AdtCompareA, s_u16CmpOff); + Adt_SetCompareValue(M0P_ADTIM5, AdtCompareB, s_u16CmpOff); } static uint16_t Motor_ArrForSps(uint32_t u32Sps) @@ -80,9 +122,65 @@ static void Motor_TimerStart(void) static void Motor_TimerStop(void) { Bt_Stop(TIM1); + /* stopped advanced timers hold their compare outputs low (PCONR.STPCx=0) */ + Adt_StopCount(M0P_ADTIM4); + Adt_StopCount(M0P_ADTIM5); s_bEnabled = FALSE; } +/* TIM4/TIM5 PWM carrier and compare-output port control */ +static void Motor_PwmInit(void) +{ + stc_adt_basecnt_cfg_t stcBaseCfg; + stc_adt_CHxX_port_cfg_t stcPortCfg; + uint32_t u32AdtHz; + uint32_t u32Pulses; + + /* default system clock is the 4 MHz internal RCH; derive the carrier + period from the actual ADT clock (PCLK / MOTOR_PWM_CK_DIV) */ + u32AdtHz = Sysctrl_GetPClkFreq() / MOTOR_PWM_CK_DIV; + u32Pulses = u32AdtHz / MOTOR_PWM_HZ; + if (u32Pulses < MOTOR_PWM_PULSES_MIN) + { + u32Pulses = MOTOR_PWM_PULSES_MIN; + } + if (u32Pulses > 0xFFFEu) + { + u32Pulses = 0xFFFEu; /* period+1 must fit 16 bits */ + } + s_u16PwmPeriod = (uint16_t)(u32Pulses - 1u); + s_u16CmpOff = (uint16_t)u32Pulses; /* never matches: static low */ + + stcBaseCfg.enCntMode = AdtSawtoothMode; + stcBaseCfg.enCntDir = AdtCntUp; + stcBaseCfg.enCntClkDiv = AdtClkPClk0Div2; + + stcPortCfg.enCap = AdtCHxCompareOutput; + stcPortCfg.bOutEn = TRUE; + stcPortCfg.enPerc = AdtCHxPeriodLow; /* low at period match */ + stcPortCfg.enCmpc = AdtCHxCompareHigh; /* high at compare match */ + stcPortCfg.enStaStp = AdtCHxStateSelSS; /* start/stop level from STACx/STPCx */ + stcPortCfg.enStaOut = AdtCHxPortOutLow; + stcPortCfg.enStpOut = AdtCHxPortOutLow; /* timer stopped -> line low */ + stcPortCfg.enDisVal = AdtTIMxDisValNorm; /* no forced-output condition armed */ + stcPortCfg.enDisSel = AdtCHxDisSel0; + stcPortCfg.bFltEn = FALSE; + stcPortCfg.enFltClk = AdtFltClkPclk0; + + Adt_Init(M0P_ADTIM4, &stcBaseCfg); + Adt_Init(M0P_ADTIM5, &stcBaseCfg); + Adt_SetPeriod(M0P_ADTIM4, s_u16PwmPeriod); + Adt_SetPeriod(M0P_ADTIM5, s_u16PwmPeriod); + + /* TIM4: CHA=IN1 (P23), CHB=IN2 (P24); TIM5: CHA=IN3 (P25), CHB=IN4 (P26) */ + Adt_CHxXPortCfg(M0P_ADTIM4, AdtCHxA, &stcPortCfg); + Adt_CHxXPortCfg(M0P_ADTIM4, AdtCHxB, &stcPortCfg); + Adt_CHxXPortCfg(M0P_ADTIM5, AdtCHxA, &stcPortCfg); + Adt_CHxXPortCfg(M0P_ADTIM5, AdtCHxB, &stcPortCfg); + + Motor_CoilsOff(); /* counters not running yet: lines stay static low */ +} + void MotorInit(void) { stc_gpio_cfg_t stcGpioCfg; @@ -100,7 +198,19 @@ void MotorInit(void) Gpio_Init(APP_MOTOR_IN2_PORT, APP_MOTOR_IN2_PIN, &stcGpioCfg); Gpio_Init(APP_MOTOR_IN3_PORT, APP_MOTOR_IN3_PIN, &stcGpioCfg); Gpio_Init(APP_MOTOR_IN4_PORT, APP_MOTOR_IN4_PIN, &stcGpioCfg); - Motor_Release(); + Gpio_WriteOutputIO(APP_MOTOR_IN1_PORT, APP_MOTOR_IN1_PIN, FALSE); + Gpio_WriteOutputIO(APP_MOTOR_IN2_PORT, APP_MOTOR_IN2_PIN, FALSE); + Gpio_WriteOutputIO(APP_MOTOR_IN3_PORT, APP_MOTOR_IN3_PIN, FALSE); + Gpio_WriteOutputIO(APP_MOTOR_IN4_PORT, APP_MOTOR_IN4_PIN, FALSE); + + /* Route the IN pins to the advanced-timer compare outputs (PWM drive): + P23=TIM4_CHA, P24=TIM4_CHB, P25=TIM5_CHA, P26=TIM5_CHB */ + Gpio_SetAfMode(APP_MOTOR_IN1_PORT, APP_MOTOR_IN1_PIN, GpioAf3); + Gpio_SetAfMode(APP_MOTOR_IN2_PORT, APP_MOTOR_IN2_PIN, GpioAf1); + Gpio_SetAfMode(APP_MOTOR_IN3_PORT, APP_MOTOR_IN3_PIN, GpioAf3); + Gpio_SetAfMode(APP_MOTOR_IN4_PORT, APP_MOTOR_IN4_PIN, GpioAf3); + + Motor_PwmInit(); stcBtCfg.enGateP = BtPositive; stcBtCfg.enGate = BtGateDisable; @@ -120,6 +230,8 @@ void MotorInit(void) s_u32CurSps = 0u; s_u32TgtSps = 0u; s_bRevPending = FALSE; + s_u8Level = 1u; + s_u16CmpOn = Motor_CmpForPercent(s_au8DutyPct[0]); } void MotorStart(void) @@ -135,8 +247,11 @@ void MotorStart(void) s_u32TgtSps = s_au16SpeedSps[0]; s_u8Level = 1u; s_bRevPending = FALSE; + s_u16CmpOn = Motor_CmpForPercent(s_au8DutyPct[0]); - Motor_WritePhase(s_u8Phase); + Motor_WritePhase(s_u8Phase); /* gate the coils before the carrier runs */ + Adt_StartCount(M0P_ADTIM4); + Adt_StartCount(M0P_ADTIM5); Motor_TimerStart(); } @@ -156,6 +271,8 @@ void MotorSpeedLevelSet(uint8_t u8Level) } s_u8Level = u8Level; s_u32TgtSps = s_au16SpeedSps[u8Level - 1u]; + s_u16CmpOn = Motor_CmpForPercent(s_au8DutyPct[u8Level - 1u]); + Motor_WritePhase(s_u8Phase); /* apply the new duty to the live phase */ } void MotorReverse(void) @@ -179,7 +296,7 @@ void MotorReverse(void) void MotorStop(void) { Motor_TimerStop(); - Motor_Release(); + Motor_CoilsOff(); s_u32CurSps = 0u; s_u32TgtSps = 0u; s_bRevPending = FALSE; @@ -224,7 +341,7 @@ void MotorStepIsr(void) else { Motor_TimerStop(); - Motor_Release(); + Motor_CoilsOff(); return; } } @@ -270,7 +387,7 @@ void MotorStepIsr(void) { /* standstill reached (e.g. smooth power-off): park immediately */ Motor_TimerStop(); - Motor_Release(); + Motor_CoilsOff(); return; }