uart_rx_byte.v 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. `timescale 1ns/1ps
  2. module uart_rx_byte #(
  3. parameter integer CLKS_PER_BIT = 217
  4. ) (
  5. input wire clk,
  6. input wire rst_n,
  7. input wire rx_i,
  8. output reg byte_valid,
  9. output reg [7:0] byte_data,
  10. output reg framing_error
  11. );
  12. localparam integer HALF_BIT = (CLKS_PER_BIT < 2) ? 1 : (CLKS_PER_BIT / 2);
  13. localparam integer COUNTER_BITS = (CLKS_PER_BIT <= 2) ? 1 : $clog2(CLKS_PER_BIT);
  14. /* verilator lint_off WIDTH */
  15. localparam [COUNTER_BITS-1:0] BIT_LAST = CLKS_PER_BIT - 1;
  16. localparam [COUNTER_BITS-1:0] HALF_LAST = HALF_BIT - 1;
  17. /* verilator lint_on WIDTH */
  18. localparam [1:0] ST_IDLE = 2'd0;
  19. localparam [1:0] ST_START = 2'd1;
  20. localparam [1:0] ST_DATA = 2'd2;
  21. localparam [1:0] ST_STOP = 2'd3;
  22. reg [1:0] rx_sync_pipe;
  23. reg [1:0] state;
  24. reg [COUNTER_BITS-1:0] bit_counter;
  25. reg [2:0] bit_index;
  26. reg [7:0] data_reg;
  27. // Keep the asynchronous input synchronizer on an ordinary, explicitly
  28. // clocked two-flop path. TD maps async_reg plus async-reset metadata to an
  29. // unclocked first-stage primitive on EF2; synchronous reset avoids that
  30. // unsafe mapping while preserving two full settling clocks.
  31. always @(posedge clk) begin
  32. if (!rst_n)
  33. rx_sync_pipe <= 2'b11;
  34. else
  35. rx_sync_pipe <= {rx_sync_pipe[0], rx_i};
  36. end
  37. always @(posedge clk or negedge rst_n) begin
  38. if (!rst_n) begin
  39. state <= ST_IDLE;
  40. bit_counter <= {COUNTER_BITS{1'b0}};
  41. bit_index <= 3'd0;
  42. data_reg <= 8'd0;
  43. byte_valid <= 1'b0;
  44. byte_data <= 8'd0;
  45. framing_error <= 1'b0;
  46. end else begin
  47. byte_valid <= 1'b0;
  48. framing_error <= 1'b0;
  49. case (state)
  50. ST_IDLE: begin
  51. if (!rx_sync_pipe[1]) begin
  52. bit_counter <= HALF_LAST;
  53. state <= ST_START;
  54. end
  55. end
  56. ST_START: begin
  57. if (bit_counter == 0) begin
  58. if (!rx_sync_pipe[1]) begin
  59. bit_index <= 3'd0;
  60. bit_counter <= BIT_LAST;
  61. state <= ST_DATA;
  62. end else begin
  63. state <= ST_IDLE;
  64. end
  65. end else begin
  66. bit_counter <= bit_counter - 1'b1;
  67. end
  68. end
  69. ST_DATA: begin
  70. if (bit_counter == 0) begin
  71. data_reg[bit_index] <= rx_sync_pipe[1];
  72. bit_counter <= BIT_LAST;
  73. if (bit_index == 3'd7) begin
  74. state <= ST_STOP;
  75. end else begin
  76. bit_index <= bit_index + 1'b1;
  77. end
  78. end else begin
  79. bit_counter <= bit_counter - 1'b1;
  80. end
  81. end
  82. ST_STOP: begin
  83. if (bit_counter == 0) begin
  84. state <= ST_IDLE;
  85. if (rx_sync_pipe[1]) begin
  86. byte_data <= data_reg;
  87. byte_valid <= 1'b1;
  88. end else begin
  89. framing_error <= 1'b1;
  90. end
  91. end else begin
  92. bit_counter <= bit_counter - 1'b1;
  93. end
  94. end
  95. default: state <= ST_IDLE;
  96. endcase
  97. end
  98. end
  99. endmodule