-
Notifications
You must be signed in to change notification settings - Fork 0
/
design.sv
68 lines (62 loc) · 1.28 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
module spi(input clk,rst,newdata,
input [11:0] din,
output reg sclk,cs,mosi);
// parameter idle=1'b0;
// parameter send=1'b1;
typedef enum bit[1:0] {idle=2'b00, send=2'b10} state_type;
state_type state;
int count;
int countc;
//sclk generation where clk=100MHz and sclk=1MHz
always@(posedge clk)begin
if(rst==1'b1)begin
countc<=0;
sclk<=1'b0;
end
else begin
if(countc<50) countc<=countc+1;
else begin
countc<=0;
sclk <= ~sclk;
end
end
end
//operation
// reg state;
reg[11:0] temp;
always@(posedge sclk)begin
if(rst==1'b1)begin
cs<=1'b1;
mosi<=1'b0;
state<=idle;
end
else begin
case(state)
idle:begin
if(newdata==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];
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