diff options
| author | Julian Blake Kongslie | 2022-04-22 22:54:57 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2022-04-22 22:54:57 -0700 |
| commit | 8dd900cb724d7d47560414d17f1be440817a0bd1 (patch) | |
| tree | bcbebe989a31b73494701199f9094bb716454546 | |
| parent | Asynchronous reset on RS232 uart. (diff) | |
| download | multipdp8-8dd900cb724d7d47560414d17f1be440817a0bd1.tar.xz | |
Oversample RS232 RX uart.
| -rw-r--r-- | hdl/rs232.sv | 79 | ||||
| -rw-r--r-- | hdl/top.sv | 40 |
2 files changed, 77 insertions, 42 deletions
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 | |||
| 87 | endmodule | 87 | endmodule |
| 88 | 88 | ||
| 89 | module rs232_rx | 89 | module rs232_rx |
| 90 | #( OVERSAMPLE = 0 | ||
| 91 | ) | ||
| 90 | ( input bit clock | 92 | ( input bit clock |
| 91 | , input bit reset | 93 | , input bit reset |
| 92 | 94 | ||
| @@ -108,6 +110,8 @@ module rs232_rx | |||
| 108 | bit [$clog2(`UART_BYTE_BITS):0] data_bits; | 110 | bit [$clog2(`UART_BYTE_BITS):0] data_bits; |
| 109 | bit parity; | 111 | bit parity; |
| 110 | 112 | ||
| 113 | bit [$clog2(OVERSAMPLE+1):0] sample; | ||
| 114 | |||
| 111 | always @(posedge clock, posedge reset) begin | 115 | always @(posedge clock, posedge reset) begin |
| 112 | if (reset) begin | 116 | if (reset) begin |
| 113 | in_valid = 0; | 117 | in_valid = 0; |
| @@ -115,47 +119,62 @@ module rs232_rx | |||
| 115 | buffer = 0; | 119 | buffer = 0; |
| 116 | data_bits = 0; | 120 | data_bits = 0; |
| 117 | parity = 0; | 121 | parity = 0; |
| 122 | sample = 0; | ||
| 118 | end else begin | 123 | end else begin |
| 124 | automatic bit ok = 0; | ||
| 125 | |||
| 119 | if (in_ready && in_valid) | 126 | if (in_ready && in_valid) |
| 120 | in_valid = 0; | 127 | in_valid = 0; |
| 121 | 128 | ||
| 122 | case (state) | 129 | if (state == state.first) begin |
| 130 | ok = 1; | ||
| 131 | end else begin | ||
| 132 | ++sample; | ||
| 133 | if (sample == OVERSAMPLE+1) begin | ||
| 134 | sample = 0; | ||
| 135 | ok = 1; | ||
| 136 | end | ||
| 137 | end | ||
| 138 | |||
| 139 | if (ok) begin | ||
| 140 | case (state) | ||
| 123 | 141 | ||
| 124 | START: begin | 142 | START: begin |
| 125 | if (rx == 0) begin | 143 | if (rx == 0) begin |
| 126 | state = state.next; | 144 | state = state.next; |
| 127 | buffer = 0; | 145 | buffer = 0; |
| 128 | data_bits = 0; | 146 | data_bits = 0; |
| 129 | parity = 0; | 147 | parity = 0; |
| 148 | end | ||
| 130 | end | 149 | end |
| 131 | end | ||
| 132 | 150 | ||
| 133 | DATA: begin | 151 | DATA: begin |
| 134 | buffer[data_bits] = rx; | 152 | buffer[data_bits] = rx; |
| 135 | parity = parity ^ rx; | 153 | parity = parity ^ rx; |
| 136 | if (data_bits == `UART_BYTE_BITS-1) | 154 | if (data_bits == `UART_BYTE_BITS-1) |
| 137 | state = state.next; | 155 | state = state.next; |
| 138 | else | 156 | else |
| 139 | ++data_bits; | 157 | ++data_bits; |
| 140 | end | 158 | end |
| 141 | 159 | ||
| 142 | PARITY: begin | 160 | PARITY: begin |
| 143 | parity = parity ^ rx; | 161 | parity = parity ^ rx; |
| 144 | if (parity == 0) | 162 | if (parity == 0) |
| 145 | state = state.next; | 163 | state = state.next; |
| 146 | else | 164 | else |
| 147 | state = state.first; | 165 | state = state.first; |
| 148 | end | 166 | end |
| 149 | 167 | ||
| 150 | STOP: begin | 168 | STOP: begin |
| 151 | if (!in_valid && rx == 1) begin | 169 | if (!in_valid && rx == 1) begin |
| 152 | in_valid = 1; | 170 | in_valid = 1; |
| 153 | in_data = buffer; | 171 | in_data = buffer; |
| 172 | end | ||
| 173 | state = state.next; | ||
| 154 | end | 174 | end |
| 155 | state = state.next; | ||
| 156 | end | ||
| 157 | 175 | ||
| 158 | endcase | 176 | endcase |
| 177 | end | ||
| 159 | end | 178 | end |
| 160 | end | 179 | end |
| 161 | 180 | ||
| @@ -34,28 +34,42 @@ module top | |||
| 34 | 34 | ||
| 35 | //assign clock_out = internal_clock; | 35 | //assign clock_out = internal_clock; |
| 36 | 36 | ||
| 37 | bit rs232_clock = 0; | 37 | bit rs232_tx_clock = 0; |
| 38 | bit [17:0] rs232_div = 0; | 38 | bit [17:0] rs232_tx_div = 0; |
| 39 | always @(posedge internal_clock) begin | 39 | always @(posedge internal_clock) begin |
| 40 | if (internal_reset) begin | 40 | if (internal_reset) begin |
| 41 | rs232_clock = 0; | 41 | rs232_tx_clock = 0; |
| 42 | rs232_div = 0; | 42 | rs232_tx_div = 0; |
| 43 | end else begin | 43 | end else begin |
| 44 | if (++rs232_div == 49987) begin // (30MHz/2)/300 | 44 | if (++rs232_tx_div == 130) begin // (30MHz/2)/115200 |
| 45 | ++rs232_clock; | 45 | ++rs232_tx_clock; |
| 46 | rs232_div = 0; | 46 | rs232_tx_div = 0; |
| 47 | end | 47 | end |
| 48 | end | 48 | end |
| 49 | end | 49 | end |
| 50 | 50 | ||
| 51 | assign clock_out = rs232_clock; | 51 | assign clock_out = rs232_tx_clock; |
| 52 | |||
| 53 | bit rs232_rx_clock = 0; | ||
| 54 | bit [4:0] rs232_rx_div = 0; | ||
| 55 | always @(posedge internal_clock) begin | ||
| 56 | if (internal_reset) begin | ||
| 57 | rs232_rx_clock = 0; | ||
| 58 | rs232_rx_div = 0; | ||
| 59 | end else begin | ||
| 60 | if (++rs232_rx_div == 16) begin // (30MHz/2)/(115200*8) | ||
| 61 | ++rs232_rx_clock; | ||
| 62 | rs232_rx_div = 0; | ||
| 63 | end | ||
| 64 | end | ||
| 65 | end | ||
| 52 | 66 | ||
| 53 | bit wire_tx_ready; | 67 | bit wire_tx_ready; |
| 54 | bit wire_tx_valid; | 68 | bit wire_tx_valid; |
| 55 | uart_byte_t wire_tx_data; | 69 | uart_byte_t wire_tx_data; |
| 56 | 70 | ||
| 57 | rs232_tx wiretx | 71 | rs232_tx wiretx |
| 58 | ( .clock(rs232_clock) | 72 | ( .clock(rs232_tx_clock) |
| 59 | , .reset(internal_reset) | 73 | , .reset(internal_reset) |
| 60 | 74 | ||
| 61 | , .out_ready(wire_tx_ready) | 75 | , .out_ready(wire_tx_ready) |
| @@ -73,7 +87,7 @@ module top | |||
| 73 | #( .WIDTH_BITS($bits(uart_byte_t)) | 87 | #( .WIDTH_BITS($bits(uart_byte_t)) |
| 74 | ) fifotx | 88 | ) fifotx |
| 75 | ( .clock_in(internal_clock) | 89 | ( .clock_in(internal_clock) |
| 76 | , .clock_out(rs232_clock) | 90 | , .clock_out(rs232_tx_clock) |
| 77 | 91 | ||
| 78 | , .in_ready(rs232_tx_ready) | 92 | , .in_ready(rs232_tx_ready) |
| 79 | , .in_valid(rs232_tx_valid) | 93 | , .in_valid(rs232_tx_valid) |
| @@ -88,8 +102,10 @@ module top | |||
| 88 | bit wire_rx_valid; | 102 | bit wire_rx_valid; |
| 89 | uart_byte_t wire_rx_data; | 103 | uart_byte_t wire_rx_data; |
| 90 | 104 | ||
| 91 | rs232_rx wirerx | 105 | rs232_rx |
| 92 | ( .clock(rs232_clock) | 106 | #( .OVERSAMPLE(7) |
| 107 | ) wirerx | ||
| 108 | ( .clock(rs232_rx_clock) | ||
| 93 | , .reset(internal_reset) | 109 | , .reset(internal_reset) |
| 94 | 110 | ||
| 95 | , .in_ready(wire_rx_ready) | 111 | , .in_ready(wire_rx_ready) |
