1 |
2 |
sfielding |
//////////////////////////////////////////////////////////////////////
|
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 |
20 |
sfielding |
//// 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 |
2 |
sfielding |
////
|
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 |
|
|
`timescale 1ns / 1ps
|
72 |
|
|
`include "usbSerialInterfaceEngine_h.v"
|
73 |
|
|
|
74 |
20 |
sfielding |
module readUSBWireData (RxBitsIn, RxDataInTick, RxBitsOut, SIERxRdyIn, SIERxWEn, fullSpeedRate, TxWireActiveDrive, clk, rst, noActivityTimeOut, RxWireActive, noActivityTimeOutEnable);
|
75 |
2 |
sfielding |
input [1:0] RxBitsIn;
|
76 |
|
|
output RxDataInTick;
|
77 |
|
|
input SIERxRdyIn;
|
78 |
|
|
input clk;
|
79 |
|
|
input fullSpeedRate;
|
80 |
|
|
input rst;
|
81 |
9 |
sfielding |
input TxWireActiveDrive;
|
82 |
2 |
sfielding |
output [1:0] RxBitsOut;
|
83 |
|
|
output SIERxWEn;
|
84 |
9 |
sfielding |
output noActivityTimeOut;
|
85 |
14 |
sfielding |
output RxWireActive;
|
86 |
20 |
sfielding |
input noActivityTimeOutEnable;
|
87 |
2 |
sfielding |
|
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 |
9 |
sfielding |
reg noActivityTimeOut;
|
97 |
14 |
sfielding |
reg RxWireActive;
|
98 |
20 |
sfielding |
wire noActivityTimeOutEnable;
|
99 |
2 |
sfielding |
|
100 |
|
|
// local registers
|
101 |
14 |
sfielding |
reg [2:0]buffer0;
|
102 |
|
|
reg [2:0]buffer1;
|
103 |
|
|
reg [2:0]buffer2;
|
104 |
|
|
reg [2:0]buffer3;
|
105 |
2 |
sfielding |
reg [2:0]bufferCnt;
|
106 |
|
|
reg [1:0]bufferInIndex;
|
107 |
|
|
reg [1:0]bufferOutIndex;
|
108 |
|
|
reg decBufferCnt;
|
109 |
20 |
sfielding |
reg [4:0]sampleCnt;
|
110 |
2 |
sfielding |
reg incBufferCnt;
|
111 |
|
|
reg [1:0]oldRxBitsIn;
|
112 |
5 |
sfielding |
reg [1:0] RxBitsInReg;
|
113 |
9 |
sfielding |
reg [15:0] timeOutCnt;
|
114 |
20 |
sfielding |
reg [7:0] rxActiveCnt;
|
115 |
14 |
sfielding |
reg RxWireEdgeDetect;
|
116 |
20 |
sfielding |
reg RxWireActiveReg;
|
117 |
14 |
sfielding |
reg RxWireActiveReg2;
|
118 |
2 |
sfielding |
|
119 |
|
|
// buffer output state machine state codes:
|
120 |
|
|
`define WAIT_BUFFER_NOT_EMPTY 2'b00
|
121 |
|
|
`define WAIT_SIE_RX_READY 2'b01
|
122 |
|
|
`define SIE_RX_WRITE 2'b10
|
123 |
|
|
|
124 |
|
|
reg [1:0] bufferOutStMachCurrState;
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
always @(posedge clk) begin
|
128 |
|
|
if (rst == 1'b1)
|
129 |
|
|
begin
|
130 |
|
|
bufferCnt <= 3'b000;
|
131 |
5 |
sfielding |
end
|
132 |
2 |
sfielding |
else begin
|
133 |
|
|
if (incBufferCnt == 1'b1 && decBufferCnt == 1'b0)
|
134 |
|
|
bufferCnt <= bufferCnt + 1'b1;
|
135 |
|
|
else if (incBufferCnt == 1'b0 && decBufferCnt == 1'b1)
|
136 |
|
|
bufferCnt <= bufferCnt - 1'b1;
|
137 |
|
|
end
|
138 |
|
|
end
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
//Perform line rate clock recovery
|
143 |
|
|
//Recover the wire data, and store data to buffer
|
144 |
|
|
always @(posedge clk) begin
|
145 |
|
|
if (rst == 1'b1)
|
146 |
|
|
begin
|
147 |
20 |
sfielding |
sampleCnt <= 5'b00000;
|
148 |
5 |
sfielding |
incBufferCnt <= 1'b0;
|
149 |
|
|
bufferInIndex <= 2'b00;
|
150 |
14 |
sfielding |
buffer0 <= 3'b000;
|
151 |
|
|
buffer1 <= 3'b000;
|
152 |
|
|
buffer2 <= 3'b000;
|
153 |
|
|
buffer3 <= 3'b000;
|
154 |
2 |
sfielding |
RxDataInTick <= 1'b0;
|
155 |
14 |
sfielding |
RxWireEdgeDetect <= 1'b0;
|
156 |
20 |
sfielding |
RxWireActiveReg <= 1'b0;
|
157 |
14 |
sfielding |
RxWireActiveReg2 <= 1'b0;
|
158 |
5 |
sfielding |
end
|
159 |
2 |
sfielding |
else begin
|
160 |
20 |
sfielding |
RxWireActiveReg2 <= RxWireActiveReg; //Delay 'RxWireActiveReg' until after 'sampleCnt' has been reset
|
161 |
|
|
RxBitsInReg <= RxBitsIn;
|
162 |
|
|
oldRxBitsIn <= RxBitsInReg;
|
163 |
5 |
sfielding |
incBufferCnt <= 1'b0; //default value
|
164 |
20 |
sfielding |
if ( (TxWireActiveDrive == 1'b0) && (RxBitsIn != RxBitsInReg)) begin //if edge detected then
|
165 |
|
|
sampleCnt <= 5'b00000;
|
166 |
|
|
RxWireEdgeDetect <= 1'b1; // flag receive activity
|
167 |
|
|
RxWireActiveReg <= 1'b1;
|
168 |
|
|
rxActiveCnt <= 8'h00;
|
169 |
9 |
sfielding |
end
|
170 |
|
|
else begin
|
171 |
20 |
sfielding |
sampleCnt <= sampleCnt + 1'b1;
|
172 |
14 |
sfielding |
RxWireEdgeDetect <= 1'b0;
|
173 |
20 |
sfielding |
rxActiveCnt <= rxActiveCnt + 1'b1;
|
174 |
|
|
//clear 'RxWireActiveReg' if no RX transitions for RX_EDGE_DET_TOUT USB bit periods
|
175 |
|
|
if ( (fullSpeedRate == 1'b1 && rxActiveCnt == `RX_EDGE_DET_TOUT * `FS_OVER_SAMPLE_RATE)
|
176 |
|
|
|| (fullSpeedRate == 1'b0 && rxActiveCnt == `RX_EDGE_DET_TOUT * `LS_OVER_SAMPLE_RATE) )
|
177 |
|
|
RxWireActiveReg <= 1'b0;
|
178 |
9 |
sfielding |
end
|
179 |
20 |
sfielding |
if ( (fullSpeedRate == 1'b1 && sampleCnt[1:0] == 2'b10) || (fullSpeedRate == 1'b0 && sampleCnt == 5'b10000) )
|
180 |
5 |
sfielding |
begin
|
181 |
2 |
sfielding |
RxDataInTick <= !RxDataInTick;
|
182 |
9 |
sfielding |
if (TxWireActiveDrive != 1'b1) //do not read wire data when transmitter is active
|
183 |
2 |
sfielding |
begin
|
184 |
|
|
incBufferCnt <= 1'b1;
|
185 |
5 |
sfielding |
bufferInIndex <= bufferInIndex + 1'b1;
|
186 |
|
|
case (bufferInIndex)
|
187 |
20 |
sfielding |
2'b00 : buffer0 <= {RxWireActiveReg2, oldRxBitsIn};
|
188 |
|
|
2'b01 : buffer1 <= {RxWireActiveReg2, oldRxBitsIn};
|
189 |
|
|
2'b10 : buffer2 <= {RxWireActiveReg2, oldRxBitsIn};
|
190 |
|
|
2'b11 : buffer3 <= {RxWireActiveReg2, oldRxBitsIn};
|
191 |
5 |
sfielding |
endcase
|
192 |
2 |
sfielding |
end
|
193 |
5 |
sfielding |
end
|
194 |
2 |
sfielding |
end
|
195 |
|
|
end
|
196 |
|
|
|
197 |
5 |
sfielding |
|
198 |
2 |
sfielding |
|
199 |
|
|
//read from buffer, and output to SIEReceiver
|
200 |
|
|
always @(posedge clk) begin
|
201 |
|
|
if (rst == 1'b1)
|
202 |
|
|
begin
|
203 |
5 |
sfielding |
decBufferCnt <= 1'b0;
|
204 |
|
|
bufferOutIndex <= 2'b00;
|
205 |
|
|
RxBitsOut <= 2'b00;
|
206 |
|
|
SIERxWEn <= 1'b0;
|
207 |
|
|
bufferOutStMachCurrState <= `WAIT_BUFFER_NOT_EMPTY;
|
208 |
|
|
end
|
209 |
2 |
sfielding |
else begin
|
210 |
5 |
sfielding |
case (bufferOutStMachCurrState)
|
211 |
|
|
`WAIT_BUFFER_NOT_EMPTY:
|
212 |
|
|
begin
|
213 |
|
|
if (bufferCnt != 3'b000)
|
214 |
|
|
bufferOutStMachCurrState <= `WAIT_SIE_RX_READY;
|
215 |
|
|
end
|
216 |
|
|
`WAIT_SIE_RX_READY:
|
217 |
|
|
begin
|
218 |
|
|
if (SIERxRdyIn == 1'b1)
|
219 |
|
|
begin
|
220 |
|
|
SIERxWEn <= 1'b1;
|
221 |
|
|
bufferOutStMachCurrState <= `SIE_RX_WRITE;
|
222 |
|
|
decBufferCnt <= 1'b1;
|
223 |
|
|
bufferOutIndex <= bufferOutIndex + 1'b1;
|
224 |
|
|
case (bufferOutIndex)
|
225 |
14 |
sfielding |
2'b00 : begin RxBitsOut <= buffer0[1:0]; RxWireActive <= buffer0[2]; end
|
226 |
|
|
2'b01 : begin RxBitsOut <= buffer1[1:0]; RxWireActive <= buffer1[2]; end
|
227 |
|
|
2'b10 : begin RxBitsOut <= buffer2[1:0]; RxWireActive <= buffer2[2]; end
|
228 |
|
|
2'b11 : begin RxBitsOut <= buffer3[1:0]; RxWireActive <= buffer3[2]; end
|
229 |
5 |
sfielding |
endcase
|
230 |
|
|
end
|
231 |
|
|
end
|
232 |
|
|
`SIE_RX_WRITE:
|
233 |
|
|
begin
|
234 |
|
|
SIERxWEn <= 1'b0;
|
235 |
|
|
decBufferCnt <= 1'b0;
|
236 |
|
|
bufferOutStMachCurrState <= `WAIT_BUFFER_NOT_EMPTY;
|
237 |
|
|
end
|
238 |
|
|
endcase
|
239 |
2 |
sfielding |
end
|
240 |
|
|
end
|
241 |
|
|
|
242 |
20 |
sfielding |
//generate 'noActivityTimeOut' pulse if no tx or rx activity for RX_PACKET_TOUT USB bit periods
|
243 |
|
|
//'noActivityTimeOut' pulse can only be generated when the host or slave getPacket
|
244 |
|
|
//process enables via 'noActivityTimeOutEnable' signal
|
245 |
|
|
//'noActivityTimeOut' pulse is used by host and slave getPacket processes to determine if
|
246 |
|
|
//there has been a response time out.
|
247 |
9 |
sfielding |
always @(posedge clk) begin
|
248 |
|
|
if (rst) begin
|
249 |
|
|
timeOutCnt <= 16'h0000;
|
250 |
|
|
noActivityTimeOut <= 1'b0;
|
251 |
|
|
end
|
252 |
|
|
else begin
|
253 |
20 |
sfielding |
if (TxWireActiveDrive == 1'b1 || RxWireEdgeDetect == 1'b1 || noActivityTimeOutEnable == 1'b0)
|
254 |
9 |
sfielding |
timeOutCnt <= 16'h0000;
|
255 |
20 |
sfielding |
else
|
256 |
9 |
sfielding |
timeOutCnt <= timeOutCnt + 1'b1;
|
257 |
|
|
if ( (fullSpeedRate == 1'b1 && timeOutCnt == `RX_PACKET_TOUT * `FS_OVER_SAMPLE_RATE)
|
258 |
20 |
sfielding |
|| (fullSpeedRate == 1'b0 && timeOutCnt == `RX_PACKET_TOUT * `LS_OVER_SAMPLE_RATE) )
|
259 |
|
|
noActivityTimeOut <= 1'b1;
|
260 |
14 |
sfielding |
else
|
261 |
9 |
sfielding |
noActivityTimeOut <= 1'b0;
|
262 |
|
|
end
|
263 |
|
|
end
|
264 |
2 |
sfielding |
|
265 |
20 |
sfielding |
|
266 |
2 |
sfielding |
endmodule
|