| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- `timescale 1ns/1ps
- module door_motor_engine #(
- parameter integer FIXED_STEPS = 450,
- parameter integer DIR_SETUP_TICKS = 2
- ) (
- input wire clk,
- input wire rst_n,
- input wire start,
- input wire [1:0] active_mask,
- input wire [1:0] dir_mask,
- input wire tick_500us,
- input wire abort,
- input wire [1:0] stop_mask,
- output reg busy,
- output reg done,
- output reg [1:0] step,
- output reg [1:0] dir
- );
- localparam integer SETUP_BITS = (DIR_SETUP_TICKS <= 1) ? 1 : $clog2(DIR_SETUP_TICKS + 1);
- localparam integer STEP_COUNT_WIDTH = (FIXED_STEPS <= 1) ? 1 : $clog2(FIXED_STEPS + 1);
- reg [SETUP_BITS-1:0] setup_count;
- reg [STEP_COUNT_WIDTH-1:0] rising_edges;
- reg [1:0] active_reg;
- wire [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 <= 2'b00;
- dir <= 2'b00;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= 2'b00;
- end else begin
- done <= 1'b0;
- if (abort) begin
- busy <= 1'b0;
- step <= 2'b00;
- active_reg <= 2'b00;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- done <= 1'b1;
- end else if (!busy) begin
- step <= 2'b00;
- if (start) begin
- dir <= dir_mask;
- active_reg <= active_mask;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- if (active_mask == 2'b00) begin
- done <= 1'b1;
- busy <= 1'b0;
- end else begin
- busy <= 1'b1;
- end
- end
- end else begin
- active_reg <= active_after_stop;
- if (tick_500us) begin
- if (setup_count < DIR_SETUP_TICKS) begin
- setup_count <= setup_count + 1'b1;
- step <= 2'b00;
- end else if (step == 2'b00) begin
- if (active_after_stop == 2'b00) begin
- busy <= 1'b0;
- done <= 1'b1;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= 2'b00;
- end else begin
- step <= active_after_stop;
- end
- end else begin
- step <= 2'b00;
- if ((rising_edges + 1'b1 >= FIXED_STEPS) || (active_after_stop == 2'b00)) begin
- busy <= 1'b0;
- done <= 1'b1;
- setup_count <= {SETUP_BITS{1'b0}};
- rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
- active_reg <= 2'b00;
- end else begin
- rising_edges <= rising_edges + 1'b1;
- end
- end
- end
- end
- end
- end
- endmodule
|