source: trunk/MultiChannelUSB/i2c_fifo.v@ 69

Last change on this file since 69 was 66, checked in by demin, 15 years ago

first working version

  • Property svn:executable set to *
File size: 3.1 KB
Line 
1module i2c_fifo
2 (
3 input wire clk, aclr,
4 input wire wrreq,
5 input wire [15:0] data,
6 output wire full,
7
8 inout wire i2c_sda,
9 output wire i2c_scl
10 );
11
12 wire int_rdempty, i2c_clk, start, stop;
13 wire [15:0] int_q;
14
15 reg int_rdreq, int_clken, int_sdo, int_scl, int_ack;
16 reg [15:0] int_data;
17 reg [8:0] counter;
18 reg [4:0] state;
19
20 assign i2c_clk = counter[8];
21
22 assign i2c_sda = int_sdo ? 1'bz : 1'b0;
23 assign i2c_scl = int_scl | (int_clken ? ~i2c_clk : 1'b0);
24// assign i2c_scl = counter[8];
25
26 assign start = int_data[8];
27 assign stop = int_data[9];
28
29 dcfifo #(
30 .intended_device_family("Cyclone III"),
31 .lpm_numwords(16),
32 .lpm_showahead("ON"),
33 .lpm_type("dcfifo"),
34 .lpm_width(16),
35 .lpm_widthu(4),
36 .rdsync_delaypipe(4),
37 .wrsync_delaypipe(4),
38 .overflow_checking("ON"),
39 .underflow_checking("ON"),
40 .use_eab("OFF"),
41 .write_aclr_synch("OFF")) fifo_tx (
42 .aclr(aclr),
43 .data(data),
44 .rdclk(i2c_clk),
45 .rdreq((~int_rdempty) & int_rdreq),
46 .wrclk(clk),
47 .wrreq(wrreq),
48 .q(int_q),
49 .rdempty(int_rdempty),
50 .wrfull(full),
51 .rdfull(),
52 .rdusedw(),
53 .wrempty(),
54 .wrusedw());
55
56 always @ (posedge clk)
57 begin
58 counter <= counter + 9'd1;
59 end
60
61 always @ (posedge i2c_clk)
62 begin
63 case (state)
64 0:
65 begin
66 int_sdo <= 1'b1;
67 int_scl <= 1'b1;
68 int_rdreq <= 1'b1;
69 state <= 5'd1;
70 end
71
72 1:
73 begin
74 if (~int_rdempty)
75 begin
76 int_data <= int_q;
77 int_rdreq <= 1'b0;
78 state <= 5'd2;
79 end
80 end
81
82 2:
83 begin
84 if (start)
85 begin
86 int_sdo <= 1'b1;
87 int_scl <= 1'b1;
88 state <= 5'd3;
89 end
90 else
91 begin
92 state <= 5'd5;
93 end
94 end
95
96 3:
97 begin // start
98 int_sdo <= 1'b0;
99 state <= 5'd4;
100 end
101
102 4:
103 begin
104 int_scl <= 1'b0;
105 state <= 5'd5;
106 end
107
108 5:
109 begin // data
110 int_clken <= 1'b1;
111 int_sdo <= int_data[7];
112 state <= 5'd6;
113 end
114
115 6:
116 begin
117 int_sdo <= int_data[6];
118 state <= 5'd7;
119 end
120
121 7:
122 begin
123 int_sdo <= int_data[5];
124 state <= 5'd8;
125 end
126
127 8:
128 begin
129 int_sdo <= int_data[4];
130 state <= 5'd9;
131 end
132
133 9:
134 begin
135 int_sdo <= int_data[3];
136 state <= 5'd10;
137 end
138
139 10:
140 begin
141 int_sdo <= int_data[2];
142 state <= 5'd11;
143 end
144
145 11:
146 begin
147 int_sdo <= int_data[1];
148 state <= 5'd12;
149 end
150
151 12:
152 begin
153 int_sdo <= int_data[0];
154 state <= 5'd13;
155 end
156
157 13:
158 begin // ack
159 int_sdo <= 1'b1;
160 int_rdreq <= 1'b1;
161 state <= 5'd14;
162 end
163
164 14:
165 begin
166 int_ack <= i2c_sda;
167 int_rdreq <= 1'b0;
168 if (stop | int_rdempty)
169 begin
170 int_clken <= 1'b0;
171 int_sdo <= 1'b0;
172 int_scl <= 1'b0;
173 state <= 5'd15;
174 end
175 else if (~int_rdempty)
176 begin
177 int_data <= int_q;
178 int_sdo <= int_q[7];
179 state <= 5'd6;
180 end
181 end
182
183 15:
184 begin // stop
185 int_scl <= 1'b1;
186 state <= 5'd16;
187 end
188
189 16:
190 begin
191 int_sdo <= 1'b1;
192 state <= 5'd0;
193 end
194
195 endcase
196 end
197
198endmodule
Note: See TracBrowser for help on using the repository browser.