| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- `timescale 1ns/1ps
- // Shared multi-axis stepper engine: loadable rising-edge budget per start at
- // tick_500us (1000 steps/s with 500 us half-period). start is ignored while
- // busy (no latest-wins). stop_mask clears axes every clock and may truncate
- // the current high STEP pulse immediately. Used by startup close-seek and host jog.
- module door_motor_engine #(
- parameter integer AXIS_COUNT = 3,
- parameter integer MAX_STEPS = 5000,
- parameter integer DIR_SETUP_TICKS = 2,
- parameter integer STEP_COUNT_WIDTH = (MAX_STEPS <= 1) ? 1 : $clog2(MAX_STEPS + 1)
- ) (
- input wire clk,
- input wire rst_n,
- input wire start,
- input wire [AXIS_COUNT-1:0] active_mask,
- input wire [AXIS_COUNT-1:0] dir_mask,
- input wire [STEP_COUNT_WIDTH-1:0] step_count,
- input wire tick_500us,
- input wire abort,
- input wire [AXIS_COUNT-1:0] stop_mask,
- output reg busy,
- output reg done,
- output reg [AXIS_COUNT-1:0] step,
- output reg [AXIS_COUNT-1:0] dir
- );
- localparam integer SETUP_BITS = (DIR_SETUP_TICKS <= 1) ? 1 : $clog2(DIR_SETUP_TICKS + 1);
- localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}};
- reg [SETUP_BITS-1:0] setup_count;
- reg [STEP_COUNT_WIDTH-1:0] rising_edges;
- reg [STEP_COUNT_WIDTH-1:0] step_limit;
- reg [AXIS_COUNT-1:0] active_reg;
- reg [AXIS_COUNT-1:0] step_next;
- wire [AXIS_COUNT-1:0] active_after_stop = active_reg & ~stop_mask;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- busy <= 1'b0;
- done <= 1'b0;
- step <= AXIS_ZERO;
- dir <= AXIS_ZERO;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- step_limit <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= AXIS_ZERO;
- step_next <= AXIS_ZERO;
- end else begin
- done <= 1'b0;
- if (abort) begin
- busy <= 1'b0;
- step <= AXIS_ZERO;
- active_reg <= AXIS_ZERO;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- done <= 1'b1;
- end else if (start && !busy) begin
- dir <= dir_mask;
- active_reg <= active_mask;
- step_limit <= step_count;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- step <= AXIS_ZERO;
- // Zero active mask or zero step budget: complete immediately.
- if ((active_mask == AXIS_ZERO) || (step_count == {STEP_COUNT_WIDTH{1'b0}})) begin
- busy <= 1'b0;
- done <= 1'b1;
- active_reg <= AXIS_ZERO;
- end else begin
- busy <= 1'b1;
- end
- end else if (busy) begin
- active_reg <= active_after_stop;
- step_next = step & ~stop_mask;
- if (tick_500us) begin
- if (setup_count < DIR_SETUP_TICKS) begin
- setup_count <= setup_count + 1'b1;
- step_next = AXIS_ZERO;
- end else if (step_next == AXIS_ZERO) begin
- if (active_after_stop == AXIS_ZERO) begin
- busy <= 1'b0;
- done <= 1'b1;
- step_next = AXIS_ZERO;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= AXIS_ZERO;
- end else begin
- step_next = active_after_stop;
- end
- end else begin
- step_next = AXIS_ZERO;
- if (rising_edges + 1 >= step_limit) begin
- busy <= 1'b0;
- done <= 1'b1;
- step_next = AXIS_ZERO;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= AXIS_ZERO;
- end else if (active_after_stop == AXIS_ZERO) begin
- busy <= 1'b0;
- done <= 1'b1;
- step_next = AXIS_ZERO;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= AXIS_ZERO;
- end else begin
- rising_edges <= rising_edges + 1'b1;
- end
- end
- end
- step <= step_next & ~stop_mask;
- end else begin
- step <= AXIS_ZERO;
- end
- end
- end
- endmodule
|