startup_close_seek.v 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. `timescale 1ns/1ps
  2. // Power-on / reconfig close-seek one-shot: logical CLOSE toward close_limit up to
  3. // CLOSE_STEPS rising edges, latch found_close, then optional logical OPEN backoff
  4. // up to BACKOFF_STEPS for axes that never saw close_limit. startup_done is sticky
  5. // after success. motor_dir_mask is logical (1=close, 0=open); physical DIR is
  6. // applied only at the top-level IO adapter.
  7. // Step pacing uses door_motor_engine tick_500us; this FSM has no local tick port.
  8. module startup_close_seek #(
  9. parameter integer CLOSE_STEPS = 5000,
  10. parameter integer BACKOFF_STEPS = 500,
  11. parameter integer AXIS_COUNT = 3,
  12. parameter integer STEP_COUNT_WIDTH =
  13. (CLOSE_STEPS >= BACKOFF_STEPS) ?
  14. ((CLOSE_STEPS <= 1) ? 1 : $clog2(CLOSE_STEPS + 1)) :
  15. ((BACKOFF_STEPS <= 1) ? 1 : $clog2(BACKOFF_STEPS + 1)),
  16. parameter [AXIS_COUNT-1:0] CLOSE_DIR_MASK = {AXIS_COUNT{1'b1}},
  17. parameter [AXIS_COUNT-1:0] AXIS_ENABLE_MASK = {AXIS_COUNT{1'b1}}
  18. ) (
  19. input wire clk,
  20. input wire rst_n,
  21. input wire limit_sync_valid,
  22. input wire [AXIS_COUNT-1:0] close_limit,
  23. input wire [AXIS_COUNT-1:0] open_limit,
  24. output reg startup_done,
  25. output reg motor_start,
  26. output reg [AXIS_COUNT-1:0] motor_active_mask,
  27. output reg [AXIS_COUNT-1:0] motor_dir_mask,
  28. output reg [STEP_COUNT_WIDTH-1:0] motor_step_count,
  29. output wire [AXIS_COUNT-1:0] motor_stop_mask,
  30. input wire motor_done
  31. );
  32. localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}};
  33. localparam [2:0] ST_WAIT_SYNC = 3'd0;
  34. localparam [2:0] ST_CHECK = 3'd1;
  35. localparam [2:0] ST_CLOSE_START = 3'd2;
  36. localparam [2:0] ST_CLOSE_WAIT = 3'd3;
  37. localparam [2:0] ST_EVAL = 3'd4;
  38. localparam [2:0] ST_BACKOFF_START = 3'd5;
  39. localparam [2:0] ST_BACKOFF_WAIT = 3'd6;
  40. localparam [2:0] ST_DONE = 3'd7;
  41. reg [2:0] state;
  42. reg [AXIS_COUNT-1:0] found_close;
  43. reg [AXIS_COUNT-1:0] close_participants;
  44. wire [AXIS_COUNT-1:0] missed_close_mask = close_participants & ~found_close;
  45. assign motor_stop_mask =
  46. (state == ST_CLOSE_WAIT) ? (close_limit & motor_active_mask) :
  47. (state == ST_BACKOFF_WAIT) ? (open_limit & motor_active_mask) :
  48. AXIS_ZERO;
  49. always @(posedge clk or negedge rst_n) begin
  50. if (!rst_n) begin
  51. state <= ST_WAIT_SYNC;
  52. startup_done <= 1'b0;
  53. motor_start <= 1'b0;
  54. motor_active_mask <= AXIS_ZERO;
  55. motor_dir_mask <= AXIS_ZERO;
  56. motor_step_count <= {STEP_COUNT_WIDTH{1'b0}};
  57. found_close <= AXIS_ZERO;
  58. close_participants <= AXIS_ZERO;
  59. end else begin
  60. if (state == ST_CLOSE_WAIT)
  61. found_close <= found_close | close_limit;
  62. case (state)
  63. ST_WAIT_SYNC: begin
  64. motor_start <= 1'b0;
  65. if (limit_sync_valid)
  66. state <= ST_CHECK;
  67. end
  68. ST_CHECK: begin
  69. motor_start <= 1'b0;
  70. found_close <= close_limit & AXIS_ENABLE_MASK;
  71. close_participants <= (~close_limit) & AXIS_ENABLE_MASK;
  72. if ((~close_limit & AXIS_ENABLE_MASK) == AXIS_ZERO) begin
  73. state <= ST_DONE;
  74. startup_done <= 1'b1;
  75. end else begin
  76. state <= ST_CLOSE_START;
  77. end
  78. end
  79. ST_CLOSE_START: begin
  80. motor_active_mask <= close_participants;
  81. motor_dir_mask <= CLOSE_DIR_MASK;
  82. motor_step_count <= CLOSE_STEPS[STEP_COUNT_WIDTH-1:0];
  83. motor_start <= 1'b1;
  84. state <= ST_CLOSE_WAIT;
  85. end
  86. ST_CLOSE_WAIT: begin
  87. motor_start <= 1'b0;
  88. if (motor_done)
  89. state <= ST_EVAL;
  90. end
  91. ST_EVAL: begin
  92. if (missed_close_mask == AXIS_ZERO) begin
  93. state <= ST_DONE;
  94. startup_done <= 1'b1;
  95. end else begin
  96. state <= ST_BACKOFF_START;
  97. end
  98. end
  99. ST_BACKOFF_START: begin
  100. motor_active_mask <= missed_close_mask;
  101. motor_dir_mask <= AXIS_ZERO;
  102. motor_step_count <= BACKOFF_STEPS[STEP_COUNT_WIDTH-1:0];
  103. motor_start <= 1'b1;
  104. state <= ST_BACKOFF_WAIT;
  105. end
  106. ST_BACKOFF_WAIT: begin
  107. motor_start <= 1'b0;
  108. if (motor_done) begin
  109. state <= ST_DONE;
  110. startup_done <= 1'b1;
  111. end
  112. end
  113. ST_DONE: begin
  114. startup_done <= 1'b1;
  115. motor_start <= 1'b0;
  116. motor_active_mask <= AXIS_ZERO;
  117. motor_dir_mask <= AXIS_ZERO;
  118. end
  119. default: state <= ST_DONE;
  120. endcase
  121. end
  122. end
  123. endmodule