OpenCores
URL https://opencores.org/ocsvn/usbhostslave/usbhostslave/trunk

Subversion Repositories usbhostslave

[/] [usbhostslave/] [trunk/] [RTL/] [serialInterfaceEngine/] [readUSBWireData.v] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 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
////      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 "usbSerialInterfaceEngine_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
 
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
  end
132
  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
    sampleCnt <= 5'b00000;
148
    incBufferCnt <= 1'b0;
149
    bufferInIndex <= 2'b00;
150
    buffer0 <= 3'b000;
151
    buffer1 <= 3'b000;
152
    buffer2 <= 3'b000;
153
    buffer3 <= 3'b000;
154
    RxDataInTick <= 1'b0;
155
    RxWireEdgeDetect <= 1'b0;
156
    RxWireActiveReg <= 1'b0;
157
    RxWireActiveReg2 <= 1'b0;
158
  end
159
  else begin
160
    RxWireActiveReg2 <= RxWireActiveReg; //Delay 'RxWireActiveReg' until after 'sampleCnt' has been reset
161
    RxBitsInReg <= RxBitsIn;
162
    oldRxBitsIn <= RxBitsInReg;
163
    incBufferCnt <= 1'b0;         //default value
164
    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
    end
170
    else begin
171
      sampleCnt <= sampleCnt + 1'b1;
172
      RxWireEdgeDetect <= 1'b0;
173
      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
    end
179
    if ( (fullSpeedRate == 1'b1 && sampleCnt[1:0] == 2'b10) || (fullSpeedRate == 1'b0 && sampleCnt == 5'b10000) )
180
    begin
181
      RxDataInTick <= !RxDataInTick;
182
      if (TxWireActiveDrive != 1'b1)  //do not read wire data when transmitter is active
183
      begin
184
        incBufferCnt <= 1'b1;
185
        bufferInIndex <= bufferInIndex + 1'b1;
186
        case (bufferInIndex)
187
          2'b00 : buffer0 <= {RxWireActiveReg2, oldRxBitsIn};
188
          2'b01 : buffer1 <= {RxWireActiveReg2, oldRxBitsIn};
189
          2'b10 : buffer2 <= {RxWireActiveReg2, oldRxBitsIn};
190
          2'b11 : buffer3 <= {RxWireActiveReg2, oldRxBitsIn};
191
        endcase
192
      end
193
    end
194
  end
195
end
196
 
197
 
198
 
199
//read from buffer, and output to SIEReceiver
200
always @(posedge clk) begin
201
  if (rst == 1'b1)
202
  begin
203
    decBufferCnt <= 1'b0;
204
    bufferOutIndex <= 2'b00;
205
    RxBitsOut <= 2'b00;
206
    SIERxWEn <= 1'b0;
207
    bufferOutStMachCurrState <= `WAIT_BUFFER_NOT_EMPTY;
208
  end
209
  else begin
210
    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
            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
          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
  end
240
end
241
 
242
//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
always @(posedge clk) begin
248
  if (rst) begin
249
    timeOutCnt <= 16'h0000;
250
    noActivityTimeOut <= 1'b0;
251
  end
252
  else begin
253
    if (TxWireActiveDrive == 1'b1 || RxWireEdgeDetect == 1'b1 || noActivityTimeOutEnable == 1'b0)
254
      timeOutCnt <= 16'h0000;
255
    else
256
      timeOutCnt <= timeOutCnt + 1'b1;
257
    if ( (fullSpeedRate == 1'b1 && timeOutCnt == `RX_PACKET_TOUT * `FS_OVER_SAMPLE_RATE)
258
      || (fullSpeedRate == 1'b0 && timeOutCnt == `RX_PACKET_TOUT * `LS_OVER_SAMPLE_RATE) )
259
      noActivityTimeOut <= 1'b1;
260
    else
261
      noActivityTimeOut <= 1'b0;
262
  end
263
end
264
 
265
 
266
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.