| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- `timescale 1ns/1ps
- // Candidate-B Host receiver. A phase accumulator schedules 27/28-clock bit
- // intervals for 25 MHz / 921600 without a PLL.
- module uart_rx_byte_fractional #(
- parameter integer CLOCK_HZ = 25000000,
- parameter integer BAUD = 921600
- ) (
- input wire clk,
- input wire rst_n,
- input wire rx_i,
- output reg byte_valid,
- output reg [7:0] byte_data,
- output reg framing_error
- );
- localparam [1:0] ST_IDLE = 2'd0;
- localparam [1:0] ST_START = 2'd1;
- localparam [1:0] ST_DATA = 2'd2;
- localparam [1:0] ST_STOP = 2'd3;
- (* async_reg = "true" *) reg [1:0] rx_sync_pipe;
- reg [1:0] state;
- reg [31:0] phase_accumulator;
- reg [2:0] bit_index;
- reg [7:0] data_reg;
- wire [32:0] phase_sum = {1'b0, phase_accumulator} + BAUD;
- wire [32:0] clock_hz_extended = {1'b0, CLOCK_HZ[31:0]};
- wire [31:0] phase_remainder = phase_sum[31:0] - CLOCK_HZ[31:0];
- wire phase_tick = phase_sum >= clock_hz_extended;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- rx_sync_pipe <= 2'b11;
- state <= ST_IDLE;
- phase_accumulator <= 0;
- bit_index <= 0;
- data_reg <= 0;
- byte_valid <= 0;
- byte_data <= 0;
- framing_error <= 0;
- end else begin
- rx_sync_pipe <= {rx_sync_pipe[0], rx_i};
- byte_valid <= 0;
- framing_error <= 0;
- case (state)
- ST_IDLE: begin
- phase_accumulator <= 0;
- if (!rx_sync_pipe[1]) begin
- phase_accumulator <= CLOCK_HZ / 2;
- state <= ST_START;
- end
- end
- ST_START: begin
- if (phase_tick) begin
- phase_accumulator <= phase_remainder;
- if (!rx_sync_pipe[1]) begin
- bit_index <= 0;
- state <= ST_DATA;
- end else state <= ST_IDLE;
- end else phase_accumulator <= phase_sum[31:0];
- end
- ST_DATA: begin
- if (phase_tick) begin
- phase_accumulator <= phase_remainder;
- data_reg[bit_index] <= rx_sync_pipe[1];
- if (bit_index == 7) state <= ST_STOP;
- else bit_index <= bit_index + 1'b1;
- end else phase_accumulator <= phase_sum[31:0];
- end
- ST_STOP: begin
- if (phase_tick) begin
- phase_accumulator <= phase_remainder;
- state <= ST_IDLE;
- if (rx_sync_pipe[1]) begin
- byte_data <= data_reg;
- byte_valid <= 1;
- end else framing_error <= 1;
- end else phase_accumulator <= phase_sum[31:0];
- end
- default: state <= ST_IDLE;
- endcase
- end
- end
- endmodule
|