| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- `timescale 1ns/1ps
- // Power-on / reconfig close-seek one-shot: logical CLOSE toward close_limit up to
- // CLOSE_STEPS rising edges, latch found_close, then optional logical OPEN backoff
- // up to BACKOFF_STEPS for axes that never saw close_limit. startup_done is sticky
- // after success. motor_dir_mask is logical (1=close, 0=open); physical DIR is
- // applied only at the top-level IO adapter.
- // Step pacing uses door_motor_engine tick_500us; this FSM has no local tick port.
- module startup_close_seek #(
- parameter integer CLOSE_STEPS = 5000,
- parameter integer BACKOFF_STEPS = 500,
- parameter integer AXIS_COUNT = 3,
- parameter integer STEP_COUNT_WIDTH =
- (CLOSE_STEPS >= BACKOFF_STEPS) ?
- ((CLOSE_STEPS <= 1) ? 1 : $clog2(CLOSE_STEPS + 1)) :
- ((BACKOFF_STEPS <= 1) ? 1 : $clog2(BACKOFF_STEPS + 1)),
- parameter [AXIS_COUNT-1:0] CLOSE_DIR_MASK = {AXIS_COUNT{1'b1}},
- parameter [AXIS_COUNT-1:0] AXIS_ENABLE_MASK = {AXIS_COUNT{1'b1}}
- ) (
- input wire clk,
- input wire rst_n,
- input wire limit_sync_valid,
- input wire [AXIS_COUNT-1:0] close_limit,
- input wire [AXIS_COUNT-1:0] open_limit,
- output reg startup_done,
- output reg motor_start,
- output reg [AXIS_COUNT-1:0] motor_active_mask,
- output reg [AXIS_COUNT-1:0] motor_dir_mask,
- output reg [STEP_COUNT_WIDTH-1:0] motor_step_count,
- output wire [AXIS_COUNT-1:0] motor_stop_mask,
- input wire motor_done
- );
- localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}};
- localparam [2:0] ST_WAIT_SYNC = 3'd0;
- localparam [2:0] ST_CHECK = 3'd1;
- localparam [2:0] ST_CLOSE_START = 3'd2;
- localparam [2:0] ST_CLOSE_WAIT = 3'd3;
- localparam [2:0] ST_EVAL = 3'd4;
- localparam [2:0] ST_BACKOFF_START = 3'd5;
- localparam [2:0] ST_BACKOFF_WAIT = 3'd6;
- localparam [2:0] ST_DONE = 3'd7;
- reg [2:0] state;
- reg [AXIS_COUNT-1:0] found_close;
- reg [AXIS_COUNT-1:0] close_participants;
- wire [AXIS_COUNT-1:0] missed_close_mask = close_participants & ~found_close;
- assign motor_stop_mask =
- (state == ST_CLOSE_WAIT) ? (close_limit & motor_active_mask) :
- (state == ST_BACKOFF_WAIT) ? (open_limit & motor_active_mask) :
- AXIS_ZERO;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- state <= ST_WAIT_SYNC;
- startup_done <= 1'b0;
- motor_start <= 1'b0;
- motor_active_mask <= AXIS_ZERO;
- motor_dir_mask <= AXIS_ZERO;
- motor_step_count <= {STEP_COUNT_WIDTH{1'b0}};
- found_close <= AXIS_ZERO;
- close_participants <= AXIS_ZERO;
- end else begin
- if (state == ST_CLOSE_WAIT)
- found_close <= found_close | close_limit;
- case (state)
- ST_WAIT_SYNC: begin
- motor_start <= 1'b0;
- if (limit_sync_valid)
- state <= ST_CHECK;
- end
- ST_CHECK: begin
- motor_start <= 1'b0;
- found_close <= close_limit & AXIS_ENABLE_MASK;
- close_participants <= (~close_limit) & AXIS_ENABLE_MASK;
- if ((~close_limit & AXIS_ENABLE_MASK) == AXIS_ZERO) begin
- state <= ST_DONE;
- startup_done <= 1'b1;
- end else begin
- state <= ST_CLOSE_START;
- end
- end
- ST_CLOSE_START: begin
- motor_active_mask <= close_participants;
- motor_dir_mask <= CLOSE_DIR_MASK;
- motor_step_count <= CLOSE_STEPS[STEP_COUNT_WIDTH-1:0];
- motor_start <= 1'b1;
- state <= ST_CLOSE_WAIT;
- end
- ST_CLOSE_WAIT: begin
- motor_start <= 1'b0;
- if (motor_done)
- state <= ST_EVAL;
- end
- ST_EVAL: begin
- if (missed_close_mask == AXIS_ZERO) begin
- state <= ST_DONE;
- startup_done <= 1'b1;
- end else begin
- state <= ST_BACKOFF_START;
- end
- end
- ST_BACKOFF_START: begin
- motor_active_mask <= missed_close_mask;
- motor_dir_mask <= AXIS_ZERO;
- motor_step_count <= BACKOFF_STEPS[STEP_COUNT_WIDTH-1:0];
- motor_start <= 1'b1;
- state <= ST_BACKOFF_WAIT;
- end
- ST_BACKOFF_WAIT: begin
- motor_start <= 1'b0;
- if (motor_done) begin
- state <= ST_DONE;
- startup_done <= 1'b1;
- end
- end
- ST_DONE: begin
- startup_done <= 1'b1;
- motor_start <= 1'b0;
- motor_active_mask <= AXIS_ZERO;
- motor_dir_mask <= AXIS_ZERO;
- end
- default: state <= ST_DONE;
- endcase
- end
- end
- endmodule
|