|
| 1 | +/* |
| 2 | +
|
| 3 | +Copyright (c) 2014-2017 Alex Forencich |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in |
| 13 | +all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +THE SOFTWARE. |
| 22 | +
|
| 23 | +*/ |
| 24 | + |
| 25 | +// Language: Verilog 2001 |
| 26 | + |
| 27 | +`timescale 1ns / 1ps |
| 28 | + |
| 29 | +/* |
| 30 | + * AXI4-Stream UART |
| 31 | + */ |
| 32 | +module uart_rx( |
| 33 | + input wire clk, |
| 34 | + input wire rst, |
| 35 | + |
| 36 | + /* |
| 37 | + * AXI output |
| 38 | + */ |
| 39 | + output wire [8-1:0] m_axis_tdata, |
| 40 | + output wire m_axis_tvalid, |
| 41 | + input wire m_axis_tready, |
| 42 | + |
| 43 | + /* |
| 44 | + * UART interface |
| 45 | + */ |
| 46 | + input wire rxd, |
| 47 | + |
| 48 | + /* |
| 49 | + * Status |
| 50 | + */ |
| 51 | + output wire busy, |
| 52 | + output wire overrun_error, |
| 53 | + output wire frame_error, |
| 54 | + |
| 55 | + /* |
| 56 | + * Configuration |
| 57 | + */ |
| 58 | + input wire [15:0] prescale |
| 59 | + |
| 60 | +); |
| 61 | + |
| 62 | +reg [8-1:0] m_axis_tdata_reg = 0; |
| 63 | +reg m_axis_tvalid_reg = 0; |
| 64 | + |
| 65 | +reg rxd_reg = 1; |
| 66 | + |
| 67 | +reg busy_reg = 0; |
| 68 | +reg overrun_error_reg = 0; |
| 69 | +reg frame_error_reg = 0; |
| 70 | + |
| 71 | +reg [8-1:0] data_reg = 0; |
| 72 | +reg [18:0] prescale_reg = 0; |
| 73 | +reg [3:0] bit_cnt = 0; |
| 74 | + |
| 75 | +assign m_axis_tdata = m_axis_tdata_reg; |
| 76 | +assign m_axis_tvalid = m_axis_tvalid_reg; |
| 77 | + |
| 78 | +assign busy = busy_reg; |
| 79 | +assign overrun_error = overrun_error_reg; |
| 80 | +assign frame_error = frame_error_reg; |
| 81 | + |
| 82 | +always @(posedge clk) begin |
| 83 | + if (rst) begin |
| 84 | + m_axis_tdata_reg <= 0; |
| 85 | + m_axis_tvalid_reg <= 0; |
| 86 | + rxd_reg <= 1; |
| 87 | + prescale_reg <= 0; |
| 88 | + bit_cnt <= 0; |
| 89 | + busy_reg <= 0; |
| 90 | + overrun_error_reg <= 0; |
| 91 | + frame_error_reg <= 0; |
| 92 | + end else begin |
| 93 | + rxd_reg <= rxd; |
| 94 | + overrun_error_reg <= 0; |
| 95 | + frame_error_reg <= 0; |
| 96 | + |
| 97 | + if (m_axis_tvalid && m_axis_tready) begin |
| 98 | + m_axis_tvalid_reg <= 0; |
| 99 | + end |
| 100 | + |
| 101 | + if (prescale_reg > 0) begin |
| 102 | + prescale_reg <= prescale_reg - 1; |
| 103 | + end else if (bit_cnt > 0) begin |
| 104 | + if (bit_cnt > 8+1) begin |
| 105 | + if (!rxd_reg) begin |
| 106 | + bit_cnt <= bit_cnt - 1; |
| 107 | + prescale_reg <= (prescale << 3)-1; |
| 108 | + end else begin |
| 109 | + bit_cnt <= 0; |
| 110 | + prescale_reg <= 0; |
| 111 | + end |
| 112 | + end else if (bit_cnt > 1) begin |
| 113 | + bit_cnt <= bit_cnt - 1; |
| 114 | + prescale_reg <= (prescale << 3)-1; |
| 115 | + data_reg <= {rxd_reg, data_reg[8-1:1]}; |
| 116 | + end else if (bit_cnt == 1) begin |
| 117 | + bit_cnt <= bit_cnt - 1; |
| 118 | + if (rxd_reg) begin |
| 119 | + m_axis_tdata_reg <= data_reg; |
| 120 | + m_axis_tvalid_reg <= 1; |
| 121 | + overrun_error_reg <= m_axis_tvalid_reg; |
| 122 | + end else begin |
| 123 | + frame_error_reg <= 1; |
| 124 | + end |
| 125 | + end |
| 126 | + end else begin |
| 127 | + busy_reg <= 0; |
| 128 | + if (!rxd_reg) begin |
| 129 | + prescale_reg <= (prescale << 2)-2; |
| 130 | + bit_cnt <= 8+2; |
| 131 | + data_reg <= 0; |
| 132 | + busy_reg <= 1; |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | +end |
| 137 | + |
| 138 | +endmodule |
0 commit comments