-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_write.v
207 lines (162 loc) · 2.69 KB
/
i2c_write.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* TODO: check lock */
include <SystemVerilog>;
module i2c_write (
sda,
scl,
addr,
register,
data,
sys_clk,
sys_freq,
i2c_freq,
write,
done
);
input [31:0] sys_freq;
input [31:0] i2c_freq;
input sys_clk;
input write;
input [6:0] addr; // 7b address
input [7:0] register;
input [7:0] data;
inout sda;
output scl;
output done; reg done;
reg i2c_clk;
gen_clock i2c_sys_clock
(
.clock_in(sys_clk),
.in_freq(sys_freq),
.clock_out(i2c_clk),
.out_freq(i2c_freq)
);
reg i2c_start;
reg i2c_stop;
reg i2c_write;
reg i2c_done;
reg i2c_status;
reg [7:0]i2c_data;
reg [7:0]i2c_addr;
reg [7:0]i2c_reg;
reg [7:0]data_being_sent;
i2c my_bus
(
.sda(sda),
.scl(scl),
.start(i2c_start),
.stop(i2c_stop),
.data(data_being_sent),
.write(i2c_write),
.cmd_done(i2c_done),
.cmd_status(i2c_status),
.sys_clk(i2c_clk)
);
/* test it */
reg [7:0] state = 0;
reg phase_done = 0;
always@(posedge sys_clk)
begin
/* can we start transmittion? */
if (state == 0)
begin
if (done)
done <= 0;
else if (write == 1)
begin
state <= 1;
i2c_data <= data;
i2c_addr <= addr;
i2c_reg <= register;
end
end
/* Start bit */
if (state == 1)
begin
// do once: send start bit
if (i2c_done == 0 && phase_done == 0)
begin
i2c_start <= 1;
phase_done <= 1;
end
if (i2c_done == 1 && phase_done == 1 )
// clear request bit
begin
i2c_start <= 0;
phase_done <= 0;
state <= 2;
end
end
// Address
if (state == 2)
begin
// do once: send data
if (i2c_done == 0 && phase_done == 0)
begin
data_being_sent <= i2c_addr;
i2c_write <= 1;
phase_done <= 1;
end
// clear request bit
if (i2c_done == 1 && phase_done == 1)
begin
i2c_write <= 0;
phase_done <= 0;
state <= 3;
/*
if (i2c_status == 1)
state <= 3;
else
state <= 20; //some error
*/
end
end
// Register
if (state == 3)
begin
if (i2c_done == 0 && phase_done == 0)
begin
data_being_sent <= i2c_reg;
i2c_write <= 1;
phase_done <= 1;
end
if (i2c_done == 1 && phase_done == 1)
begin
i2c_write <= 0;
phase_done <= 0;
state <= 4;
end
end
// Data
if (state == 4)
begin
if (i2c_done == 0 && phase_done == 0)
begin
data_being_sent <= i2c_data;
i2c_write <= 1;
phase_done <= 1;
end
if (i2c_done == 1 && phase_done == 1)
begin
i2c_write <= 0;
phase_done <= 0;
state <= 5;
end
end
// send stop bit
if (state == 5)
begin
if (i2c_done == 0 && phase_done == 0)
begin
i2c_stop <= 1;
phase_done <= 1;
end
if (i2c_done == 1 && phase_done == 1)
begin
i2c_stop <= 0;
phase_done <= 0;
state <= 0;
done <= 1;
end
end
end
endmodule