| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- `timescale 1ns/1ps
- module prod2u_p4_pwm_outputs #(
- parameter integer PTC_STEP_CLKS = 25000,
- parameter integer FAN_STEP_CLKS = 10
- ) (
- input wire clk,
- input wire rst_n,
- input wire [191:0] ptc_percent,
- input wire [31:0] fan_percent,
- input wire [7:0] solenoid_percent,
- output reg [23:0] ptc_pwm_out,
- output reg [3:0] fan_pwm_out,
- output wire solenoid_on
- );
- localparam integer PTC_COUNTER_BITS =
- (PTC_STEP_CLKS <= 1) ? 1 : $clog2(PTC_STEP_CLKS);
- localparam integer FAN_COUNTER_BITS =
- (FAN_STEP_CLKS <= 1) ? 1 : $clog2(FAN_STEP_CLKS);
- localparam [31:0] PTC_LAST_COUNT_32 = PTC_STEP_CLKS - 1;
- localparam [31:0] FAN_LAST_COUNT_32 = FAN_STEP_CLKS - 1;
- localparam [PTC_COUNTER_BITS - 1:0] PTC_LAST_COUNT =
- PTC_LAST_COUNT_32[PTC_COUNTER_BITS-1:0];
- localparam [FAN_COUNTER_BITS - 1:0] FAN_LAST_COUNT =
- FAN_LAST_COUNT_32[FAN_COUNTER_BITS-1:0];
- reg [PTC_COUNTER_BITS - 1:0] ptc_step_counter;
- reg [FAN_COUNTER_BITS - 1:0] fan_step_counter;
- reg [6:0] ptc_phase;
- reg [6:0] fan_phase;
- integer i;
- assign solenoid_on = (solenoid_percent == 8'd100);
- generate
- if ((PTC_STEP_CLKS < 1) || (FAN_STEP_CLKS < 1)) begin : g_pwm_step_must_be_positive
- prod2u_p4_pwm_step_must_be_positive u_pwm_step_error();
- end
- endgenerate
- always @(*) begin
- ptc_pwm_out = 24'd0;
- for (i = 0; i < 24; i = i + 1) begin
- if (ptc_percent[i * 8 +: 8] >= 8'd100) begin
- ptc_pwm_out[i] = 1'b1;
- end else begin
- ptc_pwm_out[i] = ptc_phase < ptc_percent[i * 8 +: 7];
- end
- end
- end
- always @(*) begin
- fan_pwm_out = 4'd0;
- for (i = 0; i < 4; i = i + 1) begin
- if (fan_percent[i * 8 +: 8] >= 8'd100) begin
- fan_pwm_out[i] = 1'b0;
- end else begin
- fan_pwm_out[i] = fan_phase < (7'd100 - fan_percent[i * 8 +: 7]);
- end
- end
- end
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- ptc_step_counter <= {PTC_COUNTER_BITS{1'b0}};
- fan_step_counter <= {FAN_COUNTER_BITS{1'b0}};
- ptc_phase <= 7'd0;
- fan_phase <= 7'd0;
- end else begin
- if (ptc_step_counter == PTC_LAST_COUNT) begin
- ptc_step_counter <= {PTC_COUNTER_BITS{1'b0}};
- if (ptc_phase == 7'd99) begin
- ptc_phase <= 7'd0;
- end else begin
- ptc_phase <= ptc_phase + 7'd1;
- end
- end else begin
- ptc_step_counter <= ptc_step_counter + 1'b1;
- end
- if (fan_step_counter == FAN_LAST_COUNT) begin
- fan_step_counter <= {FAN_COUNTER_BITS{1'b0}};
- if (fan_phase == 7'd99) begin
- fan_phase <= 7'd0;
- end else begin
- fan_phase <= fan_phase + 7'd1;
- end
- end else begin
- fan_step_counter <= fan_step_counter + 1'b1;
- end
- end
- end
- endmodule
|