door_motor_engine.v 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. `timescale 1ns/1ps
  2. // Shared multi-axis stepper engine: loadable rising-edge budget per start at
  3. // tick_500us (1000 steps/s with 500 us half-period). start is ignored while
  4. // busy (no latest-wins). stop_mask clears axes every clock and may truncate
  5. // the current high STEP pulse immediately. Used by startup close-seek and host jog.
  6. module door_motor_engine #(
  7. parameter integer AXIS_COUNT = 3,
  8. parameter integer MAX_STEPS = 5000,
  9. parameter integer DIR_SETUP_TICKS = 2,
  10. parameter integer STEP_COUNT_WIDTH = (MAX_STEPS <= 1) ? 1 : $clog2(MAX_STEPS + 1)
  11. ) (
  12. input wire clk,
  13. input wire rst_n,
  14. input wire start,
  15. input wire [AXIS_COUNT-1:0] active_mask,
  16. input wire [AXIS_COUNT-1:0] dir_mask,
  17. input wire [STEP_COUNT_WIDTH-1:0] step_count,
  18. input wire tick_500us,
  19. input wire abort,
  20. input wire [AXIS_COUNT-1:0] stop_mask,
  21. output reg busy,
  22. output reg done,
  23. output reg [AXIS_COUNT-1:0] step,
  24. output reg [AXIS_COUNT-1:0] dir
  25. );
  26. localparam integer SETUP_BITS = (DIR_SETUP_TICKS <= 1) ? 1 : $clog2(DIR_SETUP_TICKS + 1);
  27. localparam [AXIS_COUNT-1:0] AXIS_ZERO = {AXIS_COUNT{1'b0}};
  28. reg [SETUP_BITS-1:0] setup_count;
  29. reg [STEP_COUNT_WIDTH-1:0] rising_edges;
  30. reg [STEP_COUNT_WIDTH-1:0] step_limit;
  31. reg [AXIS_COUNT-1:0] active_reg;
  32. reg [AXIS_COUNT-1:0] step_next;
  33. wire [AXIS_COUNT-1:0] active_after_stop = active_reg & ~stop_mask;
  34. always @(posedge clk or negedge rst_n) begin
  35. if (!rst_n) begin
  36. busy <= 1'b0;
  37. done <= 1'b0;
  38. step <= AXIS_ZERO;
  39. dir <= AXIS_ZERO;
  40. setup_count <= {SETUP_BITS{1'b0}};
  41. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  42. step_limit <= {STEP_COUNT_WIDTH{1'b0}};
  43. active_reg <= AXIS_ZERO;
  44. step_next <= AXIS_ZERO;
  45. end else begin
  46. done <= 1'b0;
  47. if (abort) begin
  48. busy <= 1'b0;
  49. step <= AXIS_ZERO;
  50. active_reg <= AXIS_ZERO;
  51. setup_count <= {SETUP_BITS{1'b0}};
  52. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  53. done <= 1'b1;
  54. end else if (start && !busy) begin
  55. dir <= dir_mask;
  56. active_reg <= active_mask;
  57. step_limit <= step_count;
  58. setup_count <= {SETUP_BITS{1'b0}};
  59. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  60. step <= AXIS_ZERO;
  61. // Zero active mask or zero step budget: complete immediately.
  62. if ((active_mask == AXIS_ZERO) || (step_count == {STEP_COUNT_WIDTH{1'b0}})) begin
  63. busy <= 1'b0;
  64. done <= 1'b1;
  65. active_reg <= AXIS_ZERO;
  66. end else begin
  67. busy <= 1'b1;
  68. end
  69. end else if (busy) begin
  70. active_reg <= active_after_stop;
  71. step_next = step & ~stop_mask;
  72. if (tick_500us) begin
  73. if (setup_count < DIR_SETUP_TICKS) begin
  74. setup_count <= setup_count + 1'b1;
  75. step_next = AXIS_ZERO;
  76. end else if (step_next == AXIS_ZERO) begin
  77. if (active_after_stop == AXIS_ZERO) begin
  78. busy <= 1'b0;
  79. done <= 1'b1;
  80. step_next = AXIS_ZERO;
  81. setup_count <= {SETUP_BITS{1'b0}};
  82. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  83. active_reg <= AXIS_ZERO;
  84. end else begin
  85. step_next = active_after_stop;
  86. end
  87. end else begin
  88. step_next = AXIS_ZERO;
  89. if (rising_edges + 1 >= step_limit) begin
  90. busy <= 1'b0;
  91. done <= 1'b1;
  92. step_next = AXIS_ZERO;
  93. setup_count <= {SETUP_BITS{1'b0}};
  94. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  95. active_reg <= AXIS_ZERO;
  96. end else if (active_after_stop == AXIS_ZERO) begin
  97. busy <= 1'b0;
  98. done <= 1'b1;
  99. step_next = AXIS_ZERO;
  100. setup_count <= {SETUP_BITS{1'b0}};
  101. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  102. active_reg <= AXIS_ZERO;
  103. end else begin
  104. rising_edges <= rising_edges + 1'b1;
  105. end
  106. end
  107. end
  108. step <= step_next & ~stop_mask;
  109. end else begin
  110. step <= AXIS_ZERO;
  111. end
  112. end
  113. end
  114. endmodule