-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign.sv
144 lines (118 loc) · 2.41 KB
/
design.sv
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
module spi_master(
input clk, newd,rst,
input [11:0] din,
output reg sclk,cs,mosi
);
typedef enum bit [1:0] {idle = 2'b00, enable = 2'b01, send = 2'b10, comp = 2'b11 } state_type;
state_type state = idle;
int countc = 0;
int count = 0;
// generation of synchronised clk
always@(posedge clk)
begin
if(rst == 1'b1) begin
countc <= 0;
sclk <= 1'b0;
end
else begin
if(countc < 10 )
countc <= countc + 1;
else
begin
countc <= 0;
sclk <= ~sclk;
end
end
end
reg [11:0] temp;
always@(posedge sclk)
begin
if(rst == 1'b1) begin
cs <= 1'b1;
mosi <= 1'b0;
end
else begin
case(state)
idle:
begin
if(newd == 1'b1) begin
state <= send;
temp <= din;
cs <= 1'b0;
end
else begin
state <= idle;
temp <= 8'h00;
end
end
send : begin
if(count <= 11) begin
mosi <= temp[count]; /////sending lsb first
count <= count + 1;
end
else
begin
count <= 0;
state <= idle;
cs <= 1'b1;
mosi <= 1'b0;
end
end
default : state <= idle;
endcase
end
end
endmodule
module spi_slave (
input sclk, cs, mosi,
output [11:0] dout,
output reg done
);
typedef enum bit {detect_start = 1'b0, read_data = 1'b1} state_type;
state_type state = detect_start;
reg [11:0] temp = 12'h000;
int count = 0;
always@(posedge sclk)
begin
case(state)
detect_start:
begin
done <= 1'b0;
if(cs == 1'b0)
state <= read_data;
else
state <= detect_start;
end
read_data : begin
if(count <= 11)
begin
count <= count + 1;
temp <= { mosi, temp[11:1]};
end
else
begin
count <= 0;
done <= 1'b1;
state <= detect_start;
end
end
endcase
end
assign dout = temp;
endmodule
// synchronising master-slave operations
module top (
input clk, rst, newd,
input [11:0] din,
output [11:0] dout,
output done
);
wire sclk, cs, mosi;
spi_master m1 (clk, newd, rst, din, sclk, cs, mosi);
spi_slave s1 (sclk, cs, mosi, dout, done);
endmodule
//spi interfaca
interface spi_if;
logic clk,newd,rst,sclk,cs,mosi;
logic [11:0] din;
endinterface