door_motor_engine.v 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. `timescale 1ns/1ps
  2. module door_motor_engine #(
  3. parameter integer FIXED_STEPS = 450,
  4. parameter integer DIR_SETUP_TICKS = 2
  5. ) (
  6. input wire clk,
  7. input wire rst_n,
  8. input wire start,
  9. input wire [1:0] active_mask,
  10. input wire [1:0] dir_mask,
  11. input wire tick_500us,
  12. input wire abort,
  13. input wire [1:0] stop_mask,
  14. output reg busy,
  15. output reg done,
  16. output reg [1:0] step,
  17. output reg [1:0] dir
  18. );
  19. localparam integer SETUP_BITS = (DIR_SETUP_TICKS <= 1) ? 1 : $clog2(DIR_SETUP_TICKS + 1);
  20. localparam integer STEP_COUNT_WIDTH = (FIXED_STEPS <= 1) ? 1 : $clog2(FIXED_STEPS + 1);
  21. reg [SETUP_BITS-1:0] setup_count;
  22. reg [STEP_COUNT_WIDTH-1:0] rising_edges;
  23. reg [1:0] active_reg;
  24. wire [1:0] active_after_stop = active_reg & ~stop_mask;
  25. always @(posedge clk or negedge rst_n) begin
  26. if (!rst_n) begin
  27. busy <= 1'b0;
  28. done <= 1'b0;
  29. step <= 2'b00;
  30. dir <= 2'b00;
  31. setup_count <= {SETUP_BITS{1'b0}};
  32. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  33. active_reg <= 2'b00;
  34. end else begin
  35. done <= 1'b0;
  36. if (abort) begin
  37. busy <= 1'b0;
  38. step <= 2'b00;
  39. active_reg <= 2'b00;
  40. setup_count <= {SETUP_BITS{1'b0}};
  41. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  42. done <= 1'b1;
  43. end else if (!busy) begin
  44. step <= 2'b00;
  45. if (start) begin
  46. dir <= dir_mask;
  47. active_reg <= active_mask;
  48. setup_count <= {SETUP_BITS{1'b0}};
  49. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  50. if (active_mask == 2'b00) begin
  51. done <= 1'b1;
  52. busy <= 1'b0;
  53. end else begin
  54. busy <= 1'b1;
  55. end
  56. end
  57. end else begin
  58. active_reg <= active_after_stop;
  59. if (tick_500us) begin
  60. if (setup_count < DIR_SETUP_TICKS) begin
  61. setup_count <= setup_count + 1'b1;
  62. step <= 2'b00;
  63. end else if (step == 2'b00) begin
  64. if (active_after_stop == 2'b00) begin
  65. busy <= 1'b0;
  66. done <= 1'b1;
  67. setup_count <= {SETUP_BITS{1'b0}};
  68. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  69. active_reg <= 2'b00;
  70. end else begin
  71. step <= active_after_stop;
  72. end
  73. end else begin
  74. step <= 2'b00;
  75. if ((rising_edges + 1'b1 >= FIXED_STEPS) || (active_after_stop == 2'b00)) begin
  76. busy <= 1'b0;
  77. done <= 1'b1;
  78. setup_count <= {SETUP_BITS{1'b0}};
  79. rising_edges <= {STEP_COUNT_WIDTH{1'b0}};
  80. active_reg <= 2'b00;
  81. end else begin
  82. rising_edges <= rising_edges + 1'b1;
  83. end
  84. end
  85. end
  86. end
  87. end
  88. end
  89. endmodule