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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [examples/] [usb-fpga-2.13/] [2.13d/] [memfifo/] [fpga/] [ezusb_io.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   memfifo -- implementation of EZ-USB slave FIFO's (input and output) a FIFO using the DDR3 SDRAM for ZTEX USB-FPGA Modules 2.13
3
   Copyright (C) 2009-2014 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
/*
19
   Implements the EZ-USB Slave FIFO interface for both
20
   directions. It also includes an scheduler (required if both
21
   directions are used at the same time) and short packets (PKTEND).
22
*/
23
module ezusb_io #(
24
        parameter OUTEP = 2,            // EP for FPGA -> EZ-USB transfers
25
        parameter INEP = 6              // EP for EZ-USB -> FPGA transfers 
26
    ) (
27
        output ifclk,
28
        input reset,                    // asynchronous reset input
29
        output reset_out,               // synchronous reset output
30
        // pins
31
        input ifclk_in,
32
        inout [15:0] fd,
33
        output reg SLWR, PKTEND,
34
        output SLRD, SLOE,
35
        output [1:0] FIFOADDR,
36
        input EMPTY_FLAG, FULL_FLAG,
37
        // signals for FPGA -> EZ-USB transfer
38
        input [15:0] DI,                // data written to EZ-USB
39
        input DI_valid,                 // 1 indicates data valid; DI and DI_valid must be hold if DI_ready is 0
40
        output DI_ready,                // 1 if new data are accepted
41
        input DI_enable,                // setting to 0 disables FPGA -> EZ-USB transfers
42
        input [15:0] pktend_timeout,     // timeout in multiples of 65536 clocks before a short packet committed
43
                                        // setting to 0 disables this feature
44
        // signals for EZ-USB -> FPGA transfer
45
        output reg [15:0] DO,           // data read from EZ-USB
46
        output reg DO_valid,            // 1 indicated valid data
47
        input DO_ready,                 // setting to 1 enables writing new data to DO in next clock; DO and DO_valid are hold if DO_ready is 0
48
                                        // set to 0 to disable data reads 
49
        // debug output
50
        output [3:0] status
51
    );
52
 
53
 
54
    wire ifclk_inbuf, ifclk_fbin, ifclk_fbout, ifclk_out, locked;
55
 
56
    IBUFG ifclkin_buf (
57
        .I(ifclk_in),
58
        .O(ifclk_inbuf)
59
    );
60
 
61
    BUFG ifclk_fb_buf (
62
        .I(ifclk_fbout),
63
        .O(ifclk_fbin)
64
     );
65
 
66
    BUFG ifclk_out_buf (
67
        .I(ifclk_out),
68
        .O(ifclk)
69
     );
70
 
71
    MMCME2_BASE #(
72
       .BANDWIDTH("OPTIMIZED"),
73
       .CLKFBOUT_MULT_F(20.0),
74
       .CLKFBOUT_PHASE(0.0),
75
       .CLKIN1_PERIOD(0.0),
76
       .CLKOUT0_DIVIDE_F(20.0),
77
       .CLKOUT1_DIVIDE(1),
78
       .CLKOUT2_DIVIDE(1),
79
       .CLKOUT3_DIVIDE(1),
80
       .CLKOUT4_DIVIDE(1),
81
       .CLKOUT5_DIVIDE(1),
82
       .CLKOUT0_DUTY_CYCLE(0.5),
83
       .CLKOUT1_DUTY_CYCLE(0.5),
84
       .CLKOUT2_DUTY_CYCLE(0.5),
85
       .CLKOUT3_DUTY_CYCLE(0.5),
86
       .CLKOUT4_DUTY_CYCLE(0.5),
87
       .CLKOUT5_DUTY_CYCLE(0.5),
88
       .CLKOUT0_PHASE(0.0),
89
       .CLKOUT1_PHASE(0.0),
90
       .CLKOUT2_PHASE(0.0),
91
       .CLKOUT3_PHASE(0.0),
92
       .CLKOUT4_PHASE(0.0),
93
       .CLKOUT5_PHASE(0.0),
94
       .CLKOUT4_CASCADE("FALSE"),
95
       .DIVCLK_DIVIDE(1),
96
       .REF_JITTER1(0.0),
97
       .STARTUP_WAIT("FALSE")
98
    )  isclk_mmcm_inst (
99
       .CLKOUT0(ifclk_out),
100
       .CLKFBOUT(ifclk_fbout),
101
       .CLKIN1(ifclk_inbuf),
102
       .PWRDWN(1'b0),
103
       .RST(reset),
104
       .CLKFBIN(ifclk_fbin),
105
       .LOCKED(locked)
106
    );
107
 
108
    reg reset_ifclk = 1;
109
    reg if_out, if_in;
110
    reg [4:0] if_out_buf;
111
    reg [15:0] fd_buf;
112
    reg resend;
113
    reg SLRD_buf, pktend_req, pktend_en;
114
    reg [31:0] pktend_cnt;
115
 
116
    // FPGA <-> EZ-USB signals
117
    assign SLOE = if_out;
118
//    assign FIFOADDR[0] = 1'b0;
119
//    assign FIFOADDR[1] = !if_out;
120
    assign FIFOADDR = if_out ? OUTEP/2-1 : INEP/2-1;
121
    assign fd = if_out ? fd_buf : {16{1'bz}};
122
    assign SLRD = SLRD_buf || !DO_ready;
123
 
124
    assign status = { !SLRD_buf, !SLWR, resend, if_out };
125
 
126
    assign DI_ready = !reset_ifclk && FULL_FLAG && if_out & if_out_buf[4] && !resend;
127
    assign reset_out = reset || reset_ifclk;
128
 
129
    always @ (posedge ifclk)
130
    begin
131
        reset_ifclk <= reset || !locked;
132
        // FPGA -> EZ-USB
133
        if ( reset_ifclk )
134
        begin
135
            SLWR <= 1'b1;
136
            if_out <= DI_enable;  // direction of EZ-USB interface: 1 means FPGA writes / EZ_USB reads
137
            resend <= 1'b0;
138
            SLRD_buf <= 1'b1;
139
            if_out_buf = {5{!DI_enable}};
140
        end else if ( FULL_FLAG && if_out && if_out_buf[4] && ( resend || DI_valid) )   // FPGA -> EZ-USB
141
        begin
142
            SLWR <= 1'b0;
143
            SLRD_buf <= 1'b1;
144
            resend <= 1'b0;
145
            if ( !resend ) fd_buf <= DI;
146
        end else if ( EMPTY_FLAG && !if_out && !if_out_buf[4] && DO_ready )             // EZ-USB -> FPGA
147
        begin
148
            SLWR <= 1'b1;
149
            DO <= fd;
150
            SLRD_buf <= 1'b0;
151
        end else if (if_out == if_out_buf[4])
152
        begin
153
            if ( !SLWR && !FULL_FLAG ) resend <= 1'b1;  // FLAGS are received two clocks after data. If FULL_FLAG was asserted last data was ignored and has to be re-sent.
154
            SLRD_buf <= 1'b1;
155
            SLWR <= 1'b1;
156
            if_out <= DI_enable && (!DO_ready || !EMPTY_FLAG);
157
        end
158
        if_out_buf <= { if_out_buf[3:0], if_out };
159
        if ( DO_ready ) DO_valid <= !if_out && !if_out_buf[4] && EMPTY_FLAG && !SLRD_buf;  // assertion of SLRD_buf takes two clocks to take effect
160
 
161
        // PKTEND processing
162
        if ( reset_ifclk || DI_valid )
163
        begin
164
            pktend_req <= 1'b0;
165
            pktend_en <= !reset_ifclk;
166
            pktend_cnt <= 32'd0;
167
            PKTEND <= 1'b1;
168
        end else
169
        begin
170
            pktend_req <= pktend_req || ( pktend_en && (pktend_timeout != 16'd0) && (pktend_timeout == pktend_cnt[31:16]) );
171
            pktend_cnt <= pktend_cnt + 1;
172
            if ( pktend_req && if_out && if_out_buf[4] )
173
            begin
174
                PKTEND <= 1'b0;
175
                pktend_req <= 1'b0;
176
                pktend_en <= 1'b0;
177
            end else
178
            begin
179
                PKTEND <= 1'b1;
180
                pktend_req <= pktend_req || ( pktend_en && (pktend_timeout != 16'd0) && (pktend_timeout == pktend_cnt[31:16]) );
181
            end
182
        end
183
 
184
 
185
 
186
    end
187
 
188
 
189
endmodule
190
 

powered by: WebSVN 2.1.0

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