From 8dd900cb724d7d47560414d17f1be440817a0bd1 Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Fri, 22 Apr 2022 22:54:57 -0700 Subject: Oversample RS232 RX uart. --- hdl/rs232.sv | 79 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 30 deletions(-) (limited to 'hdl/rs232.sv') diff --git a/hdl/rs232.sv b/hdl/rs232.sv index 9050464..31beb1e 100644 --- a/hdl/rs232.sv +++ b/hdl/rs232.sv @@ -87,6 +87,8 @@ module rs232_tx endmodule module rs232_rx + #( OVERSAMPLE = 0 + ) ( input bit clock , input bit reset @@ -108,6 +110,8 @@ module rs232_rx bit [$clog2(`UART_BYTE_BITS):0] data_bits; bit parity; + bit [$clog2(OVERSAMPLE+1):0] sample; + always @(posedge clock, posedge reset) begin if (reset) begin in_valid = 0; @@ -115,47 +119,62 @@ module rs232_rx buffer = 0; data_bits = 0; parity = 0; + sample = 0; end else begin + automatic bit ok = 0; + if (in_ready && in_valid) in_valid = 0; - case (state) + if (state == state.first) begin + ok = 1; + end else begin + ++sample; + if (sample == OVERSAMPLE+1) begin + sample = 0; + ok = 1; + end + end + + if (ok) begin + case (state) - START: begin - if (rx == 0) begin - state = state.next; - buffer = 0; - data_bits = 0; - parity = 0; + START: begin + if (rx == 0) begin + state = state.next; + buffer = 0; + data_bits = 0; + parity = 0; + end end - end - DATA: begin - buffer[data_bits] = rx; - parity = parity ^ rx; - if (data_bits == `UART_BYTE_BITS-1) - state = state.next; - else - ++data_bits; - end + DATA: begin + buffer[data_bits] = rx; + parity = parity ^ rx; + if (data_bits == `UART_BYTE_BITS-1) + state = state.next; + else + ++data_bits; + end - PARITY: begin - parity = parity ^ rx; - if (parity == 0) - state = state.next; - else - state = state.first; - end + PARITY: begin + parity = parity ^ rx; + if (parity == 0) + state = state.next; + else + state = state.first; + end - STOP: begin - if (!in_valid && rx == 1) begin - in_valid = 1; - in_data = buffer; + STOP: begin + if (!in_valid && rx == 1) begin + in_valid = 1; + in_data = buffer; + end + state = state.next; end - state = state.next; - end - endcase + endcase + end end end -- cgit v1.2.3