-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c_api.v
241 lines (209 loc) · 8.46 KB
/
i2c_api.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* i2c_api.v
*
* copyright (c) 2021 hirosh dabui <[email protected]>
*
* permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* the software is provided "as is" and the author disclaims all warranties
* with regard to this software including all implied warranties of
* merchantability and fitness. in no event shall the author be liable for
* any special, direct, indirect, or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in an
* action of contract, negligence or other tortious action, arising out of
* or in connection with the use or performance of this software.
*
*/
`timescale 1 ns/10 ps
`default_nettype none
`include "i2c_api.vh"
`include "i2c.vh"
module i2c_api
#(
parameter TRANSFER_RATE = 100_000,
parameter CLK_FREQ = 25_000_000
) (
input clk,
input resetn,
input enable,
input [6:0] slave_addr,
input [7:0] device_register,
input [7:0] data_tx,
output [7:0] data_rx,
output scl,
output sda_oe,
input [7:0] i2c_function,
`ifdef SIM
inout sda,
`endif
input sda_in,
output sda_out,
output reg done,
output reg ready
);
`MY_I2C_DECLS
`I2C_API_DECLS
wire i2c_ready;
reg rd_wr;
reg start_stop;
reg [7:0] data_out;
reg [5:0] state;
reg [7:0] i2c_atomic;
`ifdef SIM
reg [255:0] i2c_primitiv_str;
always @(*) begin
case (i2c_atomic)
MY_I2C_IDLE : i2c_primitiv_str = "MY_I2C_IDLE";
MY_I2C_READ : i2c_primitiv_str = "MY_I2C_READ";
MY_I2C_WRITE : i2c_primitiv_str = "MY_I2C_WRITE";
MY_I2C_START : i2c_primitiv_str = "MY_I2C_START";
MY_I2C_STOP : i2c_primitiv_str = "MY_I2C_STOP";
MY_I2C_TRANS_START : i2c_primitiv_str = "MY_I2C_TRANS_START";
MY_I2C_TRANS_END : i2c_primitiv_str = "MY_I2C_TRANS_END";
endcase
end
`endif
my_i2c
#(
.TRANSFER_RATE(TRANSFER_RATE),
.CLK_FREQ(CLK_FREQ)
) my_i2c_i(
.clk(clk),
.resetn(resetn),
.enable(enable),
.transaction(i2c_function == I2C_WRITE_RAW | transaction_i2c),
.i2c_atomic(i2c_atomic),
.slave_addr(slave_addr),
.rd_wr(rd_wr),
.data_out(data_out),
.data_in(data_rx),
.scl(scl),
.sda_oe(sda_oe),
.sda_in(sda_in),
.sda_out(sda_out),
`ifdef SIM
.sda(sda),
`endif
.ready(i2c_ready)
);
reg [6:0] i2c_atomic_pc;
reg ld_register;
reg ld_data_out;
reg ld_data_in;
reg transaction_i2c;
reg [8+6+1-1:0] ctrl;
always @(*) begin
case (i2c_atomic_pc)
/* device_register => register */
/* data_out => value for write, data_rx <= value from read */
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
00: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0};
/* readU8(register, value) */
/* Read an unsigned byte from the specified register */
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
10: ctrl = {MY_I2C_START, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1};
11: ctrl = {MY_I2C_WRITE, 1'b0, 1'b1, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1};
12: ctrl = {MY_I2C_STOP, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0};
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
13: ctrl = {MY_I2C_START, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0, 1'b1};
14: ctrl = {MY_I2C_READ, 1'b0, 1'b0, 1'b1, 1'b1, 1'b0, 1'b0, 1'b1};
15: ctrl = {MY_I2C_STOP, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0};
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
16: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
/* write8(register, value) */
/* Write an 8-bit value to the specified register */
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
20: ctrl = {MY_I2C_START, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1};
21: ctrl = {MY_I2C_WRITE, 1'b0, 1'b1, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1};
22: ctrl = {MY_I2C_WRITE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1};
23: ctrl = {MY_I2C_STOP, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0, 1'b0, 1'b0};
24: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
/* writeRaw8(value)
/* Write an 8-bit value to the specified register */
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
40: ctrl = {MY_I2C_WRITE, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0, 1'b0, 1'b1};
41: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
/* START Trasaction */
/* cmd ldi ld_reg ld_dat r_w done , ready transaction*/
50: ctrl = {MY_I2C_START, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0}; /* transaction 1? */
51: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
/* STOP Trasaction */
60: ctrl = {MY_I2C_STOP, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0, 1'b0};
61: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
127: ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
default: begin
/* cmd ldi ld_reg ld_dat r_w done ready */
ctrl = {MY_I2C_IDLE, 1'b0, 1'b0, 1'b0, 1'b0, 1'b1, 1'b0, 1'b0};
end
endcase
end
always @(posedge clk) begin
if (~resetn) begin
i2c_atomic_pc <= 0;
state <= 0;
ready <= 1'b0;
done <= 1'b0;
i2c_atomic_pc <= 0;
end else begin
if (enable) begin
{i2c_atomic, ld_data_in, ld_register, ld_data_out, rd_wr, done, ready, transaction_i2c} <= ctrl;
if (ld_register) begin
data_out <= device_register;
end else if (ld_data_out) begin
data_out <= data_tx;
end
case (state)
0: begin
if (i2c_ready) begin
case (i2c_function)
I2C_READ_U8: begin
i2c_atomic_pc <= 10;
state <= 10;
end
I2C_WRITE_8: begin
i2c_atomic_pc <= 20;
state <= 10;
end
I2C_WRITE_RAW: begin
i2c_atomic_pc <= 40;
state <= 10;
end
I2C_START: begin
i2c_atomic_pc <= 50;
state <= 10;
end
I2C_STOP: begin
i2c_atomic_pc <= 60;
state <= 10;
end
I2C_NOP: begin
i2c_atomic_pc <= 127;
state <= 10;
end
default: begin
i2c_atomic_pc <= 0;
state <= 0;
end
endcase
end
end
10: begin
if (i2c_ready) begin
if (done) begin
i2c_atomic_pc <= 0;
state <= 0;
end else begin
i2c_atomic_pc <= i2c_atomic_pc + 1;
end
end
end
default: begin
state <= 0;
end
endcase
end
end
end
endmodule