#ifndef PLSR_HAL_F407_H #define PLSR_HAL_F407_H #include "plsr_types.h" #include #ifdef __cplusplus extern "C" { #endif #define PLSR_HW_AXIS_COUNT (PLSR_AXIS_COUNT) #define PLSR_HW_DIR_POINT_NONE (0xFFU) typedef enum { PLSR_HW_STATE_IDLE = 0, PLSR_HW_STATE_DIR_SETTLING, /* DIR 已置位,等待方向建立延时 */ PLSR_HW_STATE_PWM_PENDING, /* 延时已到,等待首个非零频率启动 PWM */ PLSR_HW_STATE_RUNNING, /* PWM 输出中,更新中断计数 */ PLSR_HW_STATE_DONE } PLSR_HW_STATE; typedef struct { uint32_t frequencyHz; /* 初始频率(起始速度,可为 0) */ int64_t targetPulses; /* 本段目标脉冲数 */ PLSR_OUTPUT_MODE outputMode; /* PULSE/DIR、AB 或 CW/CCW */ uint8_t directionPoint; /* DIR 输出点(Y 点号,0xFF=无) */ uint8_t directionPositive; uint8_t directionNegativeLogic; /* SFD900 Bit1: 1 reverses DIR ON/OFF */ uint16_t directionDelayMs; } PLSR_HW_START_PARAMS; PLSR_RESULT PlsrHwInit(void); PLSR_RESULT PlsrHwStartPulse(uint8_t axis, const PLSR_HW_START_PARAMS *params); PLSR_RESULT PlsrHwSetFrequency(uint8_t axis, uint32_t frequencyHz); /* Re-arm a PAUSE-stopped timer without clearing its emitted/target counters. */ PLSR_RESULT PlsrHwResumePulse(uint8_t axis); /* Complete the in-flight PULSE/DIR period, then stop and post * PLSR_EVENT_SEGMENT_COMPLETE. Used by asynchronous ACT/EXT path exits. */ PLSR_RESULT PlsrHwStopPulseAtBoundary(uint8_t axis); PLSR_RESULT PlsrHwStopPulse(uint8_t axis); /* Batch related multi-axis DIR changes into one short GPIO commit window. */ void PlsrHwBeginDirectionBatch(void); void PlsrHwEndDirectionBatch(void); uint8_t PlsrHwIsPulseActive(uint8_t axis); PLSR_HW_STATE PlsrHwGetState(uint8_t axis); uint32_t PlsrHwGetTimerClockHz(uint8_t axis); uint32_t PlsrHwGetCurrentFrequencyHz(uint8_t axis); int64_t PlsrHwGetEmittedPulses(uint8_t axis); uint8_t PlsrHwIsAbStartupPriming(uint8_t axis); uint8_t PlsrHwUsesHardwareCounter(uint8_t axis); uint32_t PlsrHwGetMaxOutputIsrCycles(void); uint32_t PlsrHwGetMaxCounterIsrCycles(void); uint32_t PlsrHwGetMaxControlIsrCycles(void); /* Fast gate from the final AB 00 boundary; budget is one 100kHz quarter * period (420 cycles at 168MHz). */ uint32_t PlsrHwGetMaxAbGateCycles(void); /* Atomically sample the free-running CPU counter and the accumulated time * spent inside PLSR timer ISRs. PlsrProcess uses the pair to distinguish its * own CPU cost from wall-clock response time under high-rate preemption. */ void PlsrHwGetCycleSnapshot(uint32_t *cycleCount, uint64_t *plsrIsrCycles); /* 每 1ms tick 推进 HAL 状态机(DIR 延时等)。 */ void PlsrHwTick(uint8_t axis); /* 输出定时器更新中断入口(生产由 IRQHandler 调用,host 由测试调用)。 */ void PlsrHwOnTimerUpdate(uint8_t axis); /* 输出点(Y0~Y20)引脚映射表:DIR 点解析用。 */ uint8_t PlsrHwResolveDirectionPoint(uint8_t pointNumber); /* Validation-only slow digital output control. It is deliberately restricted * to Y1..Y4 and is used by the P19 physical-input loopback fixture. */ PLSR_RESULT PlsrHwSetValidationOutput(uint8_t pointNumber, uint8_t terminalOn); void PlsrHwValidationOutputsOff(void); #ifdef PLSR_HOST_TEST /* 模拟寄存器访问与中断触发(测试用)。 */ uint32_t PlsrHwTestGetArr(uint8_t axis); uint32_t PlsrHwTestGetCcr(uint8_t axis); uint32_t PlsrHwTestGetCnt(uint8_t axis); uint32_t PlsrHwTestGetCcmr1(uint8_t axis); uint32_t PlsrHwTestGetCr1(uint8_t axis); uint32_t PlsrHwTestGetPsc(uint8_t axis); uint8_t PlsrHwTestGetPwmEnabled(uint8_t axis); uint8_t PlsrHwTestGetCc1PolarityInverted(uint8_t axis); uint8_t PlsrHwTestGetDirLevel(uint8_t axis); uint8_t PlsrHwTestGetAbPhaseA(uint8_t axis); uint8_t PlsrHwTestGetAbPhaseB(uint8_t axis); uint8_t PlsrHwTestGetAbQuarter(uint8_t axis); /* Model the short source-CNT/ITR synchronization window without advancing * the slave raw counter. */ void PlsrHwTestSetAbQuarterWithoutCounter(uint8_t axis, uint8_t quarter); void PlsrHwTestTriggerUpdate(uint8_t axis); void PlsrHwTestSetUpdatePending(uint8_t axis); void PlsrHwTestTriggerUpdateAndCompare(uint8_t axis); void PlsrHwTestTriggerCompare(uint8_t axis); void PlsrHwTestAdvanceAbQuarter(uint8_t axis); uint32_t PlsrHwTestGetAbFullGateCount(void); void PlsrHwTestSignalDualAbFinalBoundary(uint8_t firstAxis); void PlsrHwTestSignalDualAbStaggeredFinalBoundary(uint8_t firstAxis); #endif #ifdef __cplusplus } #endif #endif /* PLSR_HAL_F407_H */