-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree_wire_controller.v
More file actions
150 lines (145 loc) · 3.43 KB
/
Copy paththree_wire_controller.v
File metadata and controls
150 lines (145 loc) · 3.43 KB
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
// --------------------------------------------------------------------
// 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 Altera 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/Verilog or C/C++ 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: support@terasic.com
//
// --------------------------------------------------------------------
//
// Major Functions: 3-Wire Serial Bus Controller
//
// --------------------------------------------------------------------
//
// Revision History :
// --------------------------------------------------------------------
// Ver :| Author :| Mod. Date :| Changes Made:
// V1.0 :| Johnny Chen :| 06/03/23 :| Initial Revision
// --------------------------------------------------------------------
module three_wire_controller( // Host Side
iCLK,
iRST,
iDATA,
iSTR,
oACK,
oRDY,
oCLK,
// Serial Side
oSCEN,
SDA,
oSCLK );
// Host Side
input iCLK;
input iRST;
input iSTR;
input [15:0] iDATA;
output oACK;
output oRDY;
output oCLK;
// Serial Side
output oSCEN;
inout SDA;
output oSCLK;
// Internal Register and Wire
reg mSPI_CLK;
reg [15:0] mSPI_CLK_DIV;
reg mSEN;
reg mSDATA;
reg mSCLK;
reg mACK;
reg [4:0] mST;
parameter CLK_Freq = 50000000; // 50 MHz
parameter SPI_Freq = 20000; // 20 KHz
// Serial Clock Generator
always@(posedge iCLK or negedge iRST)
begin
if(!iRST)
begin
mSPI_CLK <= 0;
mSPI_CLK_DIV <= 0;
end
else
begin
if( mSPI_CLK_DIV < (CLK_Freq/SPI_Freq) )
mSPI_CLK_DIV <= mSPI_CLK_DIV+1;
else
begin
mSPI_CLK_DIV <= 0;
mSPI_CLK <= ~mSPI_CLK;
end
end
end
// Parallel to Serial
always@(negedge mSPI_CLK or negedge iRST)
begin
if(!iRST)
begin
mSEN <= 1'b1;
mSCLK <= 1'b0;
mSDATA <= 1'bz;
mACK <= 1'b0;
mST <= 4'h00;
end
else
begin
if(iSTR)
begin
if(mST<17)
mST <= mST+1'b1;
if(mST==0)
begin
mSEN <= 1'b0;
mSCLK <= 1'b1;
end
else if(mST==8)
mACK <= SDA;
else if(mST==16 && mSCLK)
begin
mSEN <= 1'b1;
mSCLK <= 1'b0;
end
if(mST<16)
mSDATA <= iDATA[15-mST];
end
else
begin
mSEN <= 1'b1;
mSCLK <= 1'b0;
mSDATA <= 1'bz;
mACK <= 1'b0;
mST <= 4'h00;
end
end
end
assign oACK = mACK;
assign oRDY = (mST==17) ? 1'b1 : 1'b0;
assign oSCEN = mSEN;
assign oSCLK = mSCLK & mSPI_CLK;
assign SDA = (mST==8) ? 1'bz :
(mST==17) ? 1'bz :
mSDATA ;
assign oCLK = mSPI_CLK;
endmodule