startup_close_seek.v 5.0 KB

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