1 |
408 |
julius |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// readUSBWireData.v ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the usbhostslave opencores effort.
|
6 |
|
|
//// <http://www.opencores.org/cores//> ////
|
7 |
|
|
//// ////
|
8 |
|
|
//// Module Description: ////
|
9 |
|
|
//// This module reads data from the differential USB data lines
|
10 |
|
|
//// and writes into a 4 entry FIFO. The data is read from
|
11 |
|
|
//// the fifo and output from the module when the higher level
|
12 |
|
|
//// state machine is ready to receive the data.
|
13 |
|
|
//// This module must recover the clock phase from the incoming
|
14 |
|
|
//// USB data. 'sampleCnt' is reset to zero whenever a RX data
|
15 |
|
|
//// edge is detected. Note that due to metastability the data
|
16 |
|
|
//// at the edge may not be registered correctly, but this does
|
17 |
|
|
//// not matter. All that matters is that an edge was detected. The
|
18 |
|
|
//// data will be accurately sampled in the middle of the USB bit
|
19 |
|
|
//// period without metastability issues.
|
20 |
|
|
//// After the edge detect, 'sampleCnt' is incremented at every clock
|
21 |
|
|
//// tick, and when it indicates the middle of a USB bit period
|
22 |
|
|
//// the RX data is sampled and written to the input buffer.
|
23 |
|
|
//// Single clock tick adjustments to 'sampleCnt' can be made at
|
24 |
|
|
//// every RX data edge detect without double sampling the incoming
|
25 |
|
|
//// data. However, the first RX data bit in a packet may cause
|
26 |
|
|
//// 'sampleCnt' to be adjusted by a value greater than a single
|
27 |
|
|
//// clock tick, and this can result in double sampling of the
|
28 |
|
|
//// first data bit a RX packet. This
|
29 |
|
|
//// double sampled data must be rejected by the higher level module.
|
30 |
|
|
//// This is achieved by
|
31 |
|
|
//// qualifying the outgoing data with 'RxWireActive'. Thus
|
32 |
|
|
//// the first data bit in a RX packet may be double sampled
|
33 |
|
|
//// as the clock recovery mechanism synchronizes to 'RxBitsIn'
|
34 |
|
|
//// but the double sampled data will be rejected by the higher
|
35 |
|
|
//// level module.
|
36 |
|
|
////
|
37 |
|
|
//// ////
|
38 |
|
|
//// To Do: ////
|
39 |
|
|
////
|
40 |
|
|
//// ////
|
41 |
|
|
//// Author(s): ////
|
42 |
|
|
//// - Steve Fielding, sfielding@base2designs.com ////
|
43 |
|
|
//// ////
|
44 |
|
|
//////////////////////////////////////////////////////////////////////
|
45 |
|
|
//// ////
|
46 |
|
|
//// Copyright (C) 2004 Steve Fielding and OPENCORES.ORG ////
|
47 |
|
|
//// ////
|
48 |
|
|
//// This source file may be used and distributed without ////
|
49 |
|
|
//// restriction provided that this copyright statement is not ////
|
50 |
|
|
//// removed from the file and that any derivative work contains ////
|
51 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
52 |
|
|
//// ////
|
53 |
|
|
//// This source file is free software; you can redistribute it ////
|
54 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
55 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
56 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
57 |
|
|
//// later version. ////
|
58 |
|
|
//// ////
|
59 |
|
|
//// This source is distributed in the hope that it will be ////
|
60 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
61 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
62 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
63 |
|
|
//// details. ////
|
64 |
|
|
//// ////
|
65 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
66 |
|
|
//// Public License along with this source; if not, download it ////
|
67 |
|
|
//// from <http://www.opencores.org/lgpl.shtml> ////
|
68 |
|
|
//// ////
|
69 |
|
|
//////////////////////////////////////////////////////////////////////
|
70 |
|
|
//
|
71 |
|
|
`include "timescale.v"
|
72 |
|
|
`include "usbhostslave_serialinterfaceengine_h.v"
|
73 |
|
|
|
74 |
|
|
module readUSBWireData (RxBitsIn, RxDataInTick, RxBitsOut, SIERxRdyIn, SIERxWEn, fullSpeedRate, TxWireActiveDrive, clk, rst, noActivityTimeOut, RxWireActive, noActivityTimeOutEnable);
|
75 |
|
|
input [1:0] RxBitsIn;
|
76 |
|
|
output RxDataInTick;
|
77 |
|
|
input SIERxRdyIn;
|
78 |
|
|
input clk;
|
79 |
|
|
input fullSpeedRate;
|
80 |
|
|
input rst;
|
81 |
|
|
input TxWireActiveDrive;
|
82 |
|
|
output [1:0] RxBitsOut;
|
83 |
|
|
output SIERxWEn;
|
84 |
|
|
output noActivityTimeOut;
|
85 |
|
|
output RxWireActive;
|
86 |
|
|
input noActivityTimeOutEnable;
|
87 |
|
|
|
88 |
|
|
wire [1:0] RxBitsIn;
|
89 |
|
|
reg RxDataInTick;
|
90 |
|
|
wire SIERxRdyIn;
|
91 |
|
|
wire clk;
|
92 |
|
|
wire fullSpeedRate;
|
93 |
|
|
wire rst;
|
94 |
|
|
reg [1:0] RxBitsOut;
|
95 |
|
|
reg SIERxWEn;
|
96 |
|
|
reg noActivityTimeOut;
|
97 |
|
|
reg RxWireActive;
|
98 |
|
|
wire noActivityTimeOutEnable;
|
99 |
|
|
|
100 |
|
|
// local registers
|
101 |
|
|
reg [2:0]buffer0;
|
102 |
|
|
reg [2:0]buffer1;
|
103 |
|
|
reg [2:0]buffer2;
|
104 |
|
|
reg [2:0]buffer3;
|
105 |
|
|
reg [2:0]bufferCnt;
|
106 |
|
|
reg [1:0]bufferInIndex;
|
107 |
|
|
reg [1:0]bufferOutIndex;
|
108 |
|
|
reg decBufferCnt;
|
109 |
|
|
reg [4:0]sampleCnt;
|
110 |
|
|
reg incBufferCnt;
|
111 |
|
|
reg [1:0]oldRxBitsIn;
|
112 |
|
|
reg [1:0] RxBitsInReg;
|
113 |
|
|
reg [15:0] timeOutCnt;
|
114 |
|
|
reg [7:0] rxActiveCnt;
|
115 |
|
|
reg RxWireEdgeDetect;
|
116 |
|
|
reg RxWireActiveReg;
|
117 |
|
|
reg RxWireActiveReg2;
|
118 |
|
|
reg [1:0] RxBitsInSyncReg1;
|
119 |
|
|
reg [1:0] RxBitsInSyncReg2;
|
120 |
|
|
|
121 |
|
|
// buffer output state machine state codes:
|
122 |
|
|
`define WAIT_BUFFER_NOT_EMPTY 2'b00
|
123 |
|
|
`define WAIT_SIE_RX_READY 2'b01
|
124 |
|
|
`define SIE_RX_WRITE 2'b10
|
125 |
|
|
|
126 |
|
|
// re-synchronize incoming bits
|
127 |
|
|
always @(posedge clk) begin
|
128 |
|
|
RxBitsInSyncReg1 <= RxBitsIn;
|
129 |
|
|
RxBitsInSyncReg2 <= RxBitsInSyncReg1;
|
130 |
|
|
end
|
131 |
|
|
|
132 |
|
|
reg [1:0] bufferOutStMachCurrState;
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
always @(posedge clk) begin
|
136 |
|
|
if (rst == 1'b1)
|
137 |
|
|
begin
|
138 |
|
|
bufferCnt <= 3'b000;
|
139 |
|
|
end
|
140 |
|
|
else begin
|
141 |
|
|
if (incBufferCnt == 1'b1 && decBufferCnt == 1'b0)
|
142 |
|
|
bufferCnt <= bufferCnt + 1'b1;
|
143 |
|
|
else if (incBufferCnt == 1'b0 && decBufferCnt == 1'b1)
|
144 |
|
|
bufferCnt <= bufferCnt - 1'b1;
|
145 |
|
|
end
|
146 |
|
|
end
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
//Perform line rate clock recovery
|
151 |
|
|
//Recover the wire data, and store data to buffer
|
152 |
|
|
always @(posedge clk) begin
|
153 |
|
|
if (rst == 1'b1)
|
154 |
|
|
begin
|
155 |
|
|
sampleCnt <= 5'b00000;
|
156 |
|
|
incBufferCnt <= 1'b0;
|
157 |
|
|
bufferInIndex <= 2'b00;
|
158 |
|
|
buffer0 <= 3'b000;
|
159 |
|
|
buffer1 <= 3'b000;
|
160 |
|
|
buffer2 <= 3'b000;
|
161 |
|
|
buffer3 <= 3'b000;
|
162 |
|
|
RxDataInTick <= 1'b0;
|
163 |
|
|
RxWireEdgeDetect <= 1'b0;
|
164 |
|
|
RxWireActiveReg <= 1'b0;
|
165 |
|
|
RxWireActiveReg2 <= 1'b0;
|
166 |
|
|
end
|
167 |
|
|
else begin
|
168 |
|
|
RxWireActiveReg2 <= RxWireActiveReg; //Delay 'RxWireActiveReg' until after 'sampleCnt' has been reset
|
169 |
|
|
RxBitsInReg <= RxBitsInSyncReg2;
|
170 |
|
|
oldRxBitsIn <= RxBitsInReg;
|
171 |
|
|
incBufferCnt <= 1'b0; //default value
|
172 |
|
|
if ( (TxWireActiveDrive == 1'b0) && (RxBitsInSyncReg2 != RxBitsInReg)) begin //if edge detected then
|
173 |
|
|
sampleCnt <= 5'b00000;
|
174 |
|
|
RxWireEdgeDetect <= 1'b1; // flag receive activity
|
175 |
|
|
RxWireActiveReg <= 1'b1;
|
176 |
|
|
rxActiveCnt <= 8'h00;
|
177 |
|
|
end
|
178 |
|
|
else begin
|
179 |
|
|
sampleCnt <= sampleCnt + 1'b1;
|
180 |
|
|
RxWireEdgeDetect <= 1'b0;
|
181 |
|
|
rxActiveCnt <= rxActiveCnt + 1'b1;
|
182 |
|
|
//clear 'RxWireActiveReg' if no RX transitions for RX_EDGE_DET_TOUT USB bit periods
|
183 |
|
|
if ( (fullSpeedRate == 1'b1 && rxActiveCnt == `RX_EDGE_DET_TOUT * `FS_OVER_SAMPLE_RATE)
|
184 |
|
|
|| (fullSpeedRate == 1'b0 && rxActiveCnt == `RX_EDGE_DET_TOUT * `LS_OVER_SAMPLE_RATE) )
|
185 |
|
|
RxWireActiveReg <= 1'b0;
|
186 |
|
|
end
|
187 |
|
|
if ( (fullSpeedRate == 1'b1 && sampleCnt[1:0] == 2'b10) || (fullSpeedRate == 1'b0 && sampleCnt == 5'b10000) )
|
188 |
|
|
begin
|
189 |
|
|
RxDataInTick <= !RxDataInTick;
|
190 |
|
|
if (TxWireActiveDrive != 1'b1) //do not read wire data when transmitter is active
|
191 |
|
|
begin
|
192 |
|
|
incBufferCnt <= 1'b1;
|
193 |
|
|
bufferInIndex <= bufferInIndex + 1'b1;
|
194 |
|
|
case (bufferInIndex)
|
195 |
|
|
2'b00 : buffer0 <= {RxWireActiveReg2, oldRxBitsIn};
|
196 |
|
|
2'b01 : buffer1 <= {RxWireActiveReg2, oldRxBitsIn};
|
197 |
|
|
2'b10 : buffer2 <= {RxWireActiveReg2, oldRxBitsIn};
|
198 |
|
|
2'b11 : buffer3 <= {RxWireActiveReg2, oldRxBitsIn};
|
199 |
|
|
endcase
|
200 |
|
|
end
|
201 |
|
|
end
|
202 |
|
|
end
|
203 |
|
|
end
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
//read from buffer, and output to SIEReceiver
|
208 |
|
|
always @(posedge clk) begin
|
209 |
|
|
if (rst == 1'b1)
|
210 |
|
|
begin
|
211 |
|
|
decBufferCnt <= 1'b0;
|
212 |
|
|
bufferOutIndex <= 2'b00;
|
213 |
|
|
RxBitsOut <= 2'b00;
|
214 |
|
|
SIERxWEn <= 1'b0;
|
215 |
|
|
bufferOutStMachCurrState <= `WAIT_BUFFER_NOT_EMPTY;
|
216 |
|
|
end
|
217 |
|
|
else begin
|
218 |
|
|
case (bufferOutStMachCurrState)
|
219 |
|
|
`WAIT_BUFFER_NOT_EMPTY:
|
220 |
|
|
begin
|
221 |
|
|
if (bufferCnt != 3'b000)
|
222 |
|
|
bufferOutStMachCurrState <= `WAIT_SIE_RX_READY;
|
223 |
|
|
end
|
224 |
|
|
`WAIT_SIE_RX_READY:
|
225 |
|
|
begin
|
226 |
|
|
if (SIERxRdyIn == 1'b1)
|
227 |
|
|
begin
|
228 |
|
|
SIERxWEn <= 1'b1;
|
229 |
|
|
bufferOutStMachCurrState <= `SIE_RX_WRITE;
|
230 |
|
|
decBufferCnt <= 1'b1;
|
231 |
|
|
bufferOutIndex <= bufferOutIndex + 1'b1;
|
232 |
|
|
case (bufferOutIndex)
|
233 |
|
|
2'b00 : begin RxBitsOut <= buffer0[1:0]; RxWireActive <= buffer0[2]; end
|
234 |
|
|
2'b01 : begin RxBitsOut <= buffer1[1:0]; RxWireActive <= buffer1[2]; end
|
235 |
|
|
2'b10 : begin RxBitsOut <= buffer2[1:0]; RxWireActive <= buffer2[2]; end
|
236 |
|
|
2'b11 : begin RxBitsOut <= buffer3[1:0]; RxWireActive <= buffer3[2]; end
|
237 |
|
|
endcase
|
238 |
|
|
end
|
239 |
|
|
end
|
240 |
|
|
`SIE_RX_WRITE:
|
241 |
|
|
begin
|
242 |
|
|
SIERxWEn <= 1'b0;
|
243 |
|
|
decBufferCnt <= 1'b0;
|
244 |
|
|
bufferOutStMachCurrState <= `WAIT_BUFFER_NOT_EMPTY;
|
245 |
|
|
end
|
246 |
|
|
endcase
|
247 |
|
|
end
|
248 |
|
|
end
|
249 |
|
|
|
250 |
|
|
//generate 'noActivityTimeOut' pulse if no tx or rx activity for RX_PACKET_TOUT USB bit periods
|
251 |
|
|
//'noActivityTimeOut' pulse can only be generated when the host or slave getPacket
|
252 |
|
|
//process enables via 'noActivityTimeOutEnable' signal
|
253 |
|
|
//'noActivityTimeOut' pulse is used by host and slave getPacket processes to determine if
|
254 |
|
|
//there has been a response time out.
|
255 |
|
|
always @(posedge clk) begin
|
256 |
|
|
if (rst) begin
|
257 |
|
|
timeOutCnt <= 16'h0000;
|
258 |
|
|
noActivityTimeOut <= 1'b0;
|
259 |
|
|
end
|
260 |
|
|
else begin
|
261 |
|
|
if (TxWireActiveDrive == 1'b1 || RxWireEdgeDetect == 1'b1 || noActivityTimeOutEnable == 1'b0)
|
262 |
|
|
timeOutCnt <= 16'h0000;
|
263 |
|
|
else
|
264 |
|
|
timeOutCnt <= timeOutCnt + 1'b1;
|
265 |
|
|
if ( (fullSpeedRate == 1'b1 && timeOutCnt == `RX_PACKET_TOUT * `FS_OVER_SAMPLE_RATE)
|
266 |
|
|
|| (fullSpeedRate == 1'b0 && timeOutCnt == `RX_PACKET_TOUT * `LS_OVER_SAMPLE_RATE) )
|
267 |
|
|
noActivityTimeOut <= 1'b1;
|
268 |
|
|
else
|
269 |
|
|
noActivityTimeOut <= 1'b0;
|
270 |
|
|
end
|
271 |
|
|
end
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
endmodule
|