prod2u_p4_pwm_outputs.v 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. `timescale 1ns/1ps
  2. module prod2u_p4_pwm_outputs #(
  3. parameter integer PTC_STEP_CLKS = 25000,
  4. parameter integer FAN_STEP_CLKS = 10
  5. ) (
  6. input wire clk,
  7. input wire rst_n,
  8. input wire [191:0] ptc_percent,
  9. input wire [31:0] fan_percent,
  10. input wire [7:0] solenoid_percent,
  11. output reg [23:0] ptc_pwm_out,
  12. output reg [3:0] fan_pwm_out,
  13. output wire solenoid_on
  14. );
  15. localparam integer PTC_COUNTER_BITS =
  16. (PTC_STEP_CLKS <= 1) ? 1 : $clog2(PTC_STEP_CLKS);
  17. localparam integer FAN_COUNTER_BITS =
  18. (FAN_STEP_CLKS <= 1) ? 1 : $clog2(FAN_STEP_CLKS);
  19. localparam [31:0] PTC_LAST_COUNT_32 = PTC_STEP_CLKS - 1;
  20. localparam [31:0] FAN_LAST_COUNT_32 = FAN_STEP_CLKS - 1;
  21. localparam [PTC_COUNTER_BITS - 1:0] PTC_LAST_COUNT =
  22. PTC_LAST_COUNT_32[PTC_COUNTER_BITS-1:0];
  23. localparam [FAN_COUNTER_BITS - 1:0] FAN_LAST_COUNT =
  24. FAN_LAST_COUNT_32[FAN_COUNTER_BITS-1:0];
  25. reg [PTC_COUNTER_BITS - 1:0] ptc_step_counter;
  26. reg [FAN_COUNTER_BITS - 1:0] fan_step_counter;
  27. reg [6:0] ptc_phase;
  28. reg [6:0] fan_phase;
  29. integer i;
  30. assign solenoid_on = (solenoid_percent == 8'd100);
  31. generate
  32. if ((PTC_STEP_CLKS < 1) || (FAN_STEP_CLKS < 1)) begin : g_pwm_step_must_be_positive
  33. prod2u_p4_pwm_step_must_be_positive u_pwm_step_error();
  34. end
  35. endgenerate
  36. always @(*) begin
  37. ptc_pwm_out = 24'd0;
  38. for (i = 0; i < 24; i = i + 1) begin
  39. if (ptc_percent[i * 8 +: 8] >= 8'd100) begin
  40. ptc_pwm_out[i] = 1'b1;
  41. end else begin
  42. ptc_pwm_out[i] = ptc_phase < ptc_percent[i * 8 +: 7];
  43. end
  44. end
  45. end
  46. always @(*) begin
  47. fan_pwm_out = 4'd0;
  48. for (i = 0; i < 4; i = i + 1) begin
  49. if (fan_percent[i * 8 +: 8] >= 8'd100) begin
  50. fan_pwm_out[i] = 1'b0;
  51. end else begin
  52. fan_pwm_out[i] = fan_phase < (7'd100 - fan_percent[i * 8 +: 7]);
  53. end
  54. end
  55. end
  56. always @(posedge clk or negedge rst_n) begin
  57. if (!rst_n) begin
  58. ptc_step_counter <= {PTC_COUNTER_BITS{1'b0}};
  59. fan_step_counter <= {FAN_COUNTER_BITS{1'b0}};
  60. ptc_phase <= 7'd0;
  61. fan_phase <= 7'd0;
  62. end else begin
  63. if (ptc_step_counter == PTC_LAST_COUNT) begin
  64. ptc_step_counter <= {PTC_COUNTER_BITS{1'b0}};
  65. if (ptc_phase == 7'd99) begin
  66. ptc_phase <= 7'd0;
  67. end else begin
  68. ptc_phase <= ptc_phase + 7'd1;
  69. end
  70. end else begin
  71. ptc_step_counter <= ptc_step_counter + 1'b1;
  72. end
  73. if (fan_step_counter == FAN_LAST_COUNT) begin
  74. fan_step_counter <= {FAN_COUNTER_BITS{1'b0}};
  75. if (fan_phase == 7'd99) begin
  76. fan_phase <= 7'd0;
  77. end else begin
  78. fan_phase <= fan_phase + 7'd1;
  79. end
  80. end else begin
  81. fan_step_counter <= fan_step_counter + 1'b1;
  82. end
  83. end
  84. end
  85. endmodule