-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathI2C_WRITE_WDATA.v
138 lines (128 loc) · 3.44 KB
/
I2C_WRITE_WDATA.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
// --------------------------------------------------------------------
// Copyright (c) 2005 by Terasic Technologies Inc.
// --------------------------------------------------------------------
//
// Permission:
//
// Terasic grants permission to use and modify this code for use
// in synthesis for all Terasic Development Boards and Altrea Development
// Kits made by Terasic. Other use of this code, including the selling
// ,duplication, or modification of any portion is strictly prohibited.
//
// Disclaimer:
//
// This VHDL or Verilog source code is intended as a design reference
// which illustrates how these types of functions can be implemented.
// It is the user's responsibility to verify their design for
// consistency and functionality through the use of formal
// verification methods. Terasic provides no warranty regarding the use
// or functionality of this code.
//
// --------------------------------------------------------------------
//
// Terasic Technologies Inc
// 356 Fu-Shin E. Rd Sec. 1. JhuBei City,
// HsinChu County, Taiwan
// 302
//
// web: http://www.terasic.com/
// email: [email protected]
//
// --------------------------------------------------------------------
module I2C_WRITE_WDATA (
input RESET_N ,
input PT_CK,
input GO,
input [15:0] REG_DATA,
input [7:0] SLAVE_ADDRESS,
input SDAI,
output reg SDAO,
output reg SCLO,
output reg END_OK,
//--for test
output reg [7:0] ST ,
output reg [7:0] CNT,
output reg [7:0] BYTE,
output reg ACK_OK,
input [7:0] BYTE_NUM // 4 : 4 byte
);
//===reg/wire
reg [8:0]A ;
reg [7:0]DELY ;
always @( negedge RESET_N or posedge PT_CK )begin
if (!RESET_N ) ST <=0;
else
case (ST)
0: begin //start
SDAO <=1;
SCLO <=1;
ACK_OK <=0;
CNT <=0;
END_OK <=1;
BYTE <=0;
if (GO) ST <=30 ; // inital
end
1: begin //start
ST <=2 ;
{ SDAO, SCLO } <= 2'b01;
A <= {SLAVE_ADDRESS ,1'b1 };//WRITE COMMAND
end
2: begin //start
ST <=3 ;
{ SDAO, SCLO } <= 2'b00;
end
3: begin
ST <=4 ;
{ SDAO, A } <= { A ,1'b0 };
end
4: begin
ST <=5 ;
SCLO <= 1'b1 ;
CNT <= CNT +1 ;
end
5: begin
SCLO <= 1'b0 ;
if (CNT==9) begin
if ( BYTE == BYTE_NUM ) ST <= 6 ;
else begin
CNT <=0 ;
ST <= 2 ;
if ( BYTE ==0 ) begin BYTE <=1 ; A <= {REG_DATA[15:8] ,1'b1 }; end
else if ( BYTE ==1 ) begin BYTE <=2 ; A <= {REG_DATA[7:0] ,1'b1 }; end
end
if (SDAI ) ACK_OK <=1 ;
end
else ST <= 2;
end
6: begin //stop
ST <=7 ;
{ SDAO, SCLO } <= 2'b00;
end
7: begin //stop
ST <=8 ;
{ SDAO, SCLO } <= 2'b01;
end
8: begin //stop
ST <=9 ;
{ SDAO, SCLO } <= 2'b11;
end
9: begin
ST <= 30;
SDAO <=1;
SCLO <=1;
CNT <=0;
END_OK <=1;
BYTE <=0;
end
//--- END ---
30: begin
if (!GO) ST <=31;
end
31: begin //
END_OK<=0;
ACK_OK<=0;
ST <=1;
end
endcase
end
endmodule