1 |
7 |
kuzmi4 |
////////////////////////////////////////////////////////
|
2 |
|
|
// RS-232 RX and TX module
|
3 |
|
|
// (c) fpga4fun.com & KNJN LLC - 2003 to 2013
|
4 |
|
|
|
5 |
|
|
// The RS-232 settings are fixed
|
6 |
|
|
// TX: 8-bit data, 2 stop, no-parity
|
7 |
|
|
// RX: 8-bit data, 1 stop, no-parity (the receiver can accept more stop bits of course)
|
8 |
|
|
|
9 |
|
|
//`define SIMULATION // in this mode, TX outputs one bit per clock cycle
|
10 |
|
|
// and RX receives one bit per clock cycle (for fast simulations)
|
11 |
|
|
|
12 |
|
|
////////////////////////////////////////////////////////
|
13 |
|
|
module async_transmitter(
|
14 |
|
|
input clk,
|
15 |
|
|
input TxD_start,
|
16 |
|
|
input [7:0] TxD_data,
|
17 |
|
|
output TxD,
|
18 |
|
|
output TxD_busy
|
19 |
|
|
);
|
20 |
|
|
|
21 |
|
|
// Assert TxD_start for (at least) one clock cycle to start transmission of TxD_data
|
22 |
|
|
// TxD_data is latched so that it doesn't have to stay valid while it is being sent
|
23 |
|
|
|
24 |
|
|
parameter ClkFrequency = 25000000; // 25MHz
|
25 |
|
|
parameter Baud = 115200;
|
26 |
|
|
|
27 |
|
|
generate
|
28 |
|
|
if(ClkFrequency<Baud*8 && (ClkFrequency % Baud!=0)) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Frequency incompatible with requested Baud rate");
|
29 |
|
|
endgenerate
|
30 |
|
|
|
31 |
|
|
////////////////////////////////
|
32 |
|
|
`ifdef SIMULATION
|
33 |
|
|
wire BitTick = 1'b1; // output one bit per clock cycle
|
34 |
|
|
`else
|
35 |
|
|
wire BitTick;
|
36 |
|
|
BaudTickGen #(ClkFrequency, Baud) tickgen(.clk(clk), .enable(TxD_busy), .tick(BitTick));
|
37 |
|
|
`endif
|
38 |
|
|
|
39 |
|
|
reg [3:0] TxD_state = 0;
|
40 |
|
|
wire TxD_ready = (TxD_state==0);
|
41 |
|
|
assign TxD_busy = ~TxD_ready;
|
42 |
|
|
|
43 |
|
|
reg [7:0] TxD_shift = 0;
|
44 |
|
|
always @(posedge clk)
|
45 |
|
|
begin
|
46 |
|
|
if(TxD_ready & TxD_start)
|
47 |
|
|
TxD_shift <= TxD_data;
|
48 |
|
|
else
|
49 |
|
|
if(TxD_state[3] & BitTick)
|
50 |
|
|
TxD_shift <= (TxD_shift >> 1);
|
51 |
|
|
|
52 |
|
|
case(TxD_state)
|
53 |
|
|
4'b0000: if(TxD_start) TxD_state <= 4'b0100;
|
54 |
|
|
4'b0100: if(BitTick) TxD_state <= 4'b1000; // start bit
|
55 |
|
|
4'b1000: if(BitTick) TxD_state <= 4'b1001; // bit 0
|
56 |
|
|
4'b1001: if(BitTick) TxD_state <= 4'b1010; // bit 1
|
57 |
|
|
4'b1010: if(BitTick) TxD_state <= 4'b1011; // bit 2
|
58 |
|
|
4'b1011: if(BitTick) TxD_state <= 4'b1100; // bit 3
|
59 |
|
|
4'b1100: if(BitTick) TxD_state <= 4'b1101; // bit 4
|
60 |
|
|
4'b1101: if(BitTick) TxD_state <= 4'b1110; // bit 5
|
61 |
|
|
4'b1110: if(BitTick) TxD_state <= 4'b1111; // bit 6
|
62 |
|
|
4'b1111: if(BitTick) TxD_state <= 4'b0010; // bit 7
|
63 |
|
|
4'b0010: if(BitTick) TxD_state <= 4'b0011; // stop1
|
64 |
|
|
4'b0011: if(BitTick) TxD_state <= 4'b0000; // stop2
|
65 |
|
|
default: if(BitTick) TxD_state <= 4'b0000;
|
66 |
|
|
endcase
|
67 |
|
|
end
|
68 |
|
|
|
69 |
|
|
assign TxD = (TxD_state<4) | (TxD_state[3] & TxD_shift[0]); // put together the start, data and stop bits
|
70 |
|
|
endmodule
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
////////////////////////////////////////////////////////
|
74 |
|
|
module async_receiver(
|
75 |
|
|
input clk,
|
76 |
|
|
input RxD,
|
77 |
|
|
output reg RxD_data_ready = 0,
|
78 |
|
|
output reg [7:0] RxD_data = 0, // data received, valid only (for one clock cycle) when RxD_data_ready is asserted
|
79 |
|
|
|
80 |
|
|
// We also detect if a gap occurs in the received stream of characters
|
81 |
|
|
// That can be useful if multiple characters are sent in burst
|
82 |
|
|
// so that multiple characters can be treated as a "packet"
|
83 |
|
|
output RxD_idle, // asserted when no data has been received for a while
|
84 |
|
|
output reg RxD_endofpacket = 0 // asserted for one clock cycle when a packet has been detected (i.e. RxD_idle is going high)
|
85 |
|
|
);
|
86 |
|
|
|
87 |
|
|
parameter ClkFrequency = 25000000; // 25MHz
|
88 |
|
|
parameter Baud = 115200;
|
89 |
|
|
|
90 |
|
|
parameter Oversampling = 8; // needs to be a power of 2
|
91 |
|
|
// we oversample the RxD line at a fixed rate to capture each RxD data bit at the "right" time
|
92 |
|
|
// 8 times oversampling by default, use 16 for higher quality reception
|
93 |
|
|
|
94 |
|
|
generate
|
95 |
|
|
if(ClkFrequency<Baud*Oversampling) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Frequency too low for current Baud rate and oversampling");
|
96 |
|
|
if(Oversampling<8 || ((Oversampling & (Oversampling-1))!=0)) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Invalid oversampling value");
|
97 |
|
|
endgenerate
|
98 |
|
|
|
99 |
|
|
////////////////////////////////
|
100 |
|
|
reg [3:0] RxD_state = 0;
|
101 |
|
|
|
102 |
|
|
`ifdef SIMULATION
|
103 |
|
|
wire RxD_bit = RxD;
|
104 |
|
|
wire sampleNow = 1'b1; // receive one bit per clock cycle
|
105 |
|
|
|
106 |
|
|
`else
|
107 |
|
|
wire OversamplingTick;
|
108 |
|
|
BaudTickGen #(ClkFrequency, Baud, Oversampling) tickgen(.clk(clk), .enable(1'b1), .tick(OversamplingTick));
|
109 |
|
|
|
110 |
|
|
// synchronize RxD to our clk domain
|
111 |
|
|
reg [1:0] RxD_sync = 2'b11;
|
112 |
|
|
always @(posedge clk) if(OversamplingTick) RxD_sync <= {RxD_sync[0], RxD};
|
113 |
|
|
|
114 |
|
|
// and filter it
|
115 |
|
|
reg [1:0] Filter_cnt = 2'b11;
|
116 |
|
|
reg RxD_bit = 1'b1;
|
117 |
|
|
|
118 |
|
|
always @(posedge clk)
|
119 |
|
|
if(OversamplingTick)
|
120 |
|
|
begin
|
121 |
|
|
if(RxD_sync[1]==1'b1 && Filter_cnt!=2'b11) Filter_cnt <= Filter_cnt + 1'd1;
|
122 |
|
|
else
|
123 |
|
|
if(RxD_sync[1]==1'b0 && Filter_cnt!=2'b00) Filter_cnt <= Filter_cnt - 1'd1;
|
124 |
|
|
|
125 |
|
|
if(Filter_cnt==2'b11) RxD_bit <= 1'b1;
|
126 |
|
|
else
|
127 |
|
|
if(Filter_cnt==2'b00) RxD_bit <= 1'b0;
|
128 |
|
|
end
|
129 |
|
|
|
130 |
|
|
// and decide when is the good time to sample the RxD line
|
131 |
|
|
function integer log2(input integer v); begin log2=0; while(v>>log2) log2=log2+1; end endfunction
|
132 |
|
|
localparam l2o = log2(Oversampling);
|
133 |
|
|
reg [l2o-2:0] OversamplingCnt = 0;
|
134 |
|
|
always @(posedge clk) if(OversamplingTick) OversamplingCnt <= (RxD_state==0) ? 1'd0 : OversamplingCnt + 1'd1;
|
135 |
|
|
wire sampleNow = OversamplingTick && (OversamplingCnt==Oversampling/2-1);
|
136 |
|
|
`endif
|
137 |
|
|
|
138 |
|
|
// now we can accumulate the RxD bits in a shift-register
|
139 |
|
|
always @(posedge clk)
|
140 |
|
|
case(RxD_state)
|
141 |
|
|
4'b0000: if(~RxD_bit) RxD_state <= `ifdef SIMULATION 4'b1000 `else 4'b0001 `endif; // start bit found?
|
142 |
|
|
4'b0001: if(sampleNow) RxD_state <= 4'b1000; // sync start bit to sampleNow
|
143 |
|
|
4'b1000: if(sampleNow) RxD_state <= 4'b1001; // bit 0
|
144 |
|
|
4'b1001: if(sampleNow) RxD_state <= 4'b1010; // bit 1
|
145 |
|
|
4'b1010: if(sampleNow) RxD_state <= 4'b1011; // bit 2
|
146 |
|
|
4'b1011: if(sampleNow) RxD_state <= 4'b1100; // bit 3
|
147 |
|
|
4'b1100: if(sampleNow) RxD_state <= 4'b1101; // bit 4
|
148 |
|
|
4'b1101: if(sampleNow) RxD_state <= 4'b1110; // bit 5
|
149 |
|
|
4'b1110: if(sampleNow) RxD_state <= 4'b1111; // bit 6
|
150 |
|
|
4'b1111: if(sampleNow) RxD_state <= 4'b0010; // bit 7
|
151 |
|
|
4'b0010: if(sampleNow) RxD_state <= 4'b0000; // stop bit
|
152 |
|
|
default: RxD_state <= 4'b0000;
|
153 |
|
|
endcase
|
154 |
|
|
|
155 |
|
|
always @(posedge clk)
|
156 |
|
|
if(sampleNow && RxD_state[3]) RxD_data <= {RxD_bit, RxD_data[7:1]};
|
157 |
|
|
|
158 |
|
|
//reg RxD_data_error = 0;
|
159 |
|
|
always @(posedge clk)
|
160 |
|
|
begin
|
161 |
|
|
RxD_data_ready <= (sampleNow && RxD_state==4'b0010 && RxD_bit); // make sure a stop bit is received
|
162 |
|
|
//RxD_data_error <= (sampleNow && RxD_state==4'b0010 && ~RxD_bit); // error if a stop bit is not received
|
163 |
|
|
end
|
164 |
|
|
|
165 |
|
|
`ifndef SIMULATION
|
166 |
|
|
reg [l2o+1:0] GapCnt = 0;
|
167 |
|
|
always @(posedge clk) if (RxD_state!=0) GapCnt<=0; else if(OversamplingTick & ~GapCnt[log2(Oversampling)+1]) GapCnt <= GapCnt + 1'h1;
|
168 |
|
|
assign RxD_idle = GapCnt[l2o+1];
|
169 |
|
|
always @(posedge clk) RxD_endofpacket <= OversamplingTick & ~GapCnt[l2o+1] & &GapCnt[l2o:0];
|
170 |
|
|
`else
|
171 |
|
|
assign RxD_idle = 0;
|
172 |
|
|
`endif // SIMULATION
|
173 |
|
|
endmodule
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
////////////////////////////////////////////////////////
|
177 |
|
|
// dummy module used to be able to raise an assertion in Verilog
|
178 |
|
|
module ASSERTION_ERROR();
|
179 |
|
|
endmodule
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
////////////////////////////////////////////////////////
|
183 |
|
|
module BaudTickGen(
|
184 |
|
|
input clk, enable,
|
185 |
|
|
output tick // generate a tick at the specified baud rate * oversampling
|
186 |
|
|
);
|
187 |
|
|
parameter ClkFrequency = 25000000;
|
188 |
|
|
parameter Baud = 115200;
|
189 |
|
|
parameter Oversampling = 1;
|
190 |
|
|
|
191 |
|
|
function integer log2(input integer v); begin log2=0; while(v>>log2) log2=log2+1; end endfunction
|
192 |
|
|
localparam AccWidth = log2(ClkFrequency/Baud)+8; // +/- 2% max timing error over a byte
|
193 |
|
|
reg [AccWidth:0] Acc = 0;
|
194 |
|
|
localparam ShiftLimiter = log2(Baud*Oversampling >> (31-AccWidth)); // this makes sure Inc calculation doesn't overflow
|
195 |
|
|
localparam Inc = ((Baud*Oversampling << (AccWidth-ShiftLimiter))+(ClkFrequency>>(ShiftLimiter+1)))/(ClkFrequency>>ShiftLimiter);
|
196 |
|
|
always @(posedge clk) if(enable) Acc <= Acc[AccWidth-1:0] + Inc[AccWidth:0]; else Acc <= Inc[AccWidth:0];
|
197 |
|
|
assign tick = Acc[AccWidth];
|
198 |
|
|
endmodule
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
////////////////////////////////////////////////////////
|