door_motor_engine.v 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. `timescale 1ns/1ps
  2. module door_motor_engine #(
  3. parameter integer FIXED_STEPS = 450,
  4. parameter integer STEP_HALF_CYCLES = 12500,
  5. parameter integer DIR_SETUP_CYCLES = 25000
  6. ) (
  7. input wire clk,
  8. input wire rst_n,
  9. input wire start,
  10. input wire inlet_dir,
  11. input wire outlet_dir,
  12. output reg busy,
  13. output reg done,
  14. output wire x_step,
  15. output wire x_dir,
  16. output wire y_step,
  17. output wire y_dir
  18. );
  19. localparam [2:0] ST_IDLE = 3'd0;
  20. localparam [2:0] ST_INLET_SETUP = 3'd1;
  21. localparam [2:0] ST_INLET_HIGH = 3'd2;
  22. localparam [2:0] ST_INLET_LOW = 3'd3;
  23. localparam [2:0] ST_OUTLET_SETUP = 3'd4;
  24. localparam [2:0] ST_OUTLET_HIGH = 3'd5;
  25. localparam [2:0] ST_OUTLET_LOW = 3'd6;
  26. reg [2:0] state;
  27. reg [31:0] timer;
  28. reg [31:0] step_count;
  29. reg inlet_dir_reg;
  30. reg outlet_dir_reg;
  31. assign x_step = (state == ST_INLET_HIGH);
  32. assign y_step = (state == ST_OUTLET_HIGH);
  33. assign x_dir = inlet_dir_reg;
  34. assign y_dir = outlet_dir_reg;
  35. always @(posedge clk or negedge rst_n) begin
  36. if (!rst_n) begin
  37. state <= ST_IDLE;
  38. timer <= 32'd0;
  39. step_count <= 32'd0;
  40. inlet_dir_reg <= 1'b0;
  41. outlet_dir_reg <= 1'b0;
  42. busy <= 1'b0;
  43. done <= 1'b0;
  44. end else begin
  45. done <= 1'b0;
  46. case (state)
  47. ST_IDLE: begin
  48. busy <= 1'b0;
  49. if (start) begin
  50. inlet_dir_reg <= inlet_dir;
  51. outlet_dir_reg <= outlet_dir;
  52. step_count <= 32'd0;
  53. timer <= (DIR_SETUP_CYCLES <= 1) ? 0 : DIR_SETUP_CYCLES - 1;
  54. busy <= 1'b1;
  55. state <= ST_INLET_SETUP;
  56. end
  57. end
  58. ST_INLET_SETUP: begin
  59. if (timer == 0) begin
  60. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  61. state <= ST_INLET_HIGH;
  62. end else timer <= timer - 1'b1;
  63. end
  64. ST_INLET_HIGH: begin
  65. if (timer == 0) begin
  66. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  67. state <= ST_INLET_LOW;
  68. end else timer <= timer - 1'b1;
  69. end
  70. ST_INLET_LOW: begin
  71. if (timer == 0) begin
  72. if (step_count + 1 >= FIXED_STEPS) begin
  73. step_count <= 32'd0;
  74. timer <= (DIR_SETUP_CYCLES <= 1) ? 0 : DIR_SETUP_CYCLES - 1;
  75. state <= ST_OUTLET_SETUP;
  76. end else begin
  77. step_count <= step_count + 1'b1;
  78. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  79. state <= ST_INLET_HIGH;
  80. end
  81. end else timer <= timer - 1'b1;
  82. end
  83. ST_OUTLET_SETUP: begin
  84. if (timer == 0) begin
  85. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  86. state <= ST_OUTLET_HIGH;
  87. end else timer <= timer - 1'b1;
  88. end
  89. ST_OUTLET_HIGH: begin
  90. if (timer == 0) begin
  91. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  92. state <= ST_OUTLET_LOW;
  93. end else timer <= timer - 1'b1;
  94. end
  95. ST_OUTLET_LOW: begin
  96. if (timer == 0) begin
  97. if (step_count + 1 >= FIXED_STEPS) begin
  98. state <= ST_IDLE;
  99. busy <= 1'b0;
  100. done <= 1'b1;
  101. end else begin
  102. step_count <= step_count + 1'b1;
  103. timer <= (STEP_HALF_CYCLES <= 1) ? 0 : STEP_HALF_CYCLES - 1;
  104. state <= ST_OUTLET_HIGH;
  105. end
  106. end else timer <= timer - 1'b1;
  107. end
  108. default: state <= ST_IDLE;
  109. endcase
  110. end
  111. end
  112. endmodule