-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspi_clgen.v
111 lines (97 loc) · 2.34 KB
/
spi_clgen.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
`include "spi_defines.v"
module spi_clgen (wb_clk_in,
wb_rst,
tip,
go,
last_clk,
divider,
sclk_out,
cpol_0,
cpol_1);
input wb_clk_in;
input wb_rst;
input tip;
input go;
input last_clk;
input [`SPI_DIVIDER_LEN-1:0] divider;
output sclk_out;
output cpol_0;
output cpol_1;
reg sclk_out;
reg cpol_0;
reg cpol_1;
reg [`SPI_DIVIDER_LEN-1:0] cnt;
// Counter counts half period
always@(posedge wb_clk_in or posedge wb_rst)
begin
if(wb_rst)
begin
cnt <= {{`SPI_DIVIDER_LEN{1'b0}},1'b1};
end
else if(tip)
begin
if(cnt == (divider + 1))
begin
cnt <= {{`SPI_DIVIDER_LEN{1'b0}},1'b1};
end
else
begin
cnt <= cnt + 1;
end
end
else if(cnt == 0)
begin
cnt <= {{`SPI_DIVIDER_LEN{1'b0}},1'b1};
end
end
// Generation of the serial clock
always@(posedge wb_clk_in or posedge wb_rst)
begin
if(wb_rst)
begin
sclk_out <= 1'b0;
end
else if(tip)
begin
if(cnt == (divider + 1))
begin
if(!last_clk || sclk_out)
sclk_out <= ~sclk_out;
end
end
end
// Posedge and negedge detection of sclk
always@(posedge wb_clk_in or posedge wb_rst)
begin
if(wb_rst)
begin
cpol_0 <= 1'b0;
cpol_1 <= 1'b0;
end
else
begin
cpol_0 <= 0;
cpol_1 <= 0;
if(tip)
begin
if(~sclk_out)
begin
if(cnt == divider)
begin
cpol_0 <= 1;
end
end
end
if(tip)
begin
if(sclk_out)
begin
if(cnt == divider)
begin
cpol_1 <= 1;
end
end
end
end
end
endmodule