1 |
2 |
yacinenet |
//////////////////////////////////////////////////////////////////////
|
2 |
|
|
//// ////
|
3 |
|
|
//// spi_clgen.v ////
|
4 |
|
|
//// ////
|
5 |
|
|
//// This file is part of the SPI IP core project ////
|
6 |
|
|
//// http://www.opencores.org/projects/spi/ ////
|
7 |
|
|
//// ////
|
8 |
|
|
//// Author(s): ////
|
9 |
|
|
//// - Simon Srot (simons@opencores.org) ////
|
10 |
|
|
//// ////
|
11 |
|
|
//// All additional information is avaliable in the Readme.txt ////
|
12 |
|
|
//// file. ////
|
13 |
|
|
//// ////
|
14 |
|
|
//////////////////////////////////////////////////////////////////////
|
15 |
|
|
//// ////
|
16 |
|
|
//// Copyright (C) 2002 Authors ////
|
17 |
|
|
//// ////
|
18 |
|
|
//// This source file may be used and distributed without ////
|
19 |
|
|
//// restriction provided that this copyright statement is not ////
|
20 |
|
|
//// removed from the file and that any derivative work contains ////
|
21 |
|
|
//// the original copyright notice and the associated disclaimer. ////
|
22 |
|
|
//// ////
|
23 |
|
|
//// This source file is free software; you can redistribute it ////
|
24 |
|
|
//// and/or modify it under the terms of the GNU Lesser General ////
|
25 |
|
|
//// Public License as published by the Free Software Foundation; ////
|
26 |
|
|
//// either version 2.1 of the License, or (at your option) any ////
|
27 |
|
|
//// later version. ////
|
28 |
|
|
//// ////
|
29 |
|
|
//// This source is distributed in the hope that it will be ////
|
30 |
|
|
//// useful, but WITHOUT ANY WARRANTY; without even the implied ////
|
31 |
|
|
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ////
|
32 |
|
|
//// PURPOSE. See the GNU Lesser General Public License for more ////
|
33 |
|
|
//// details. ////
|
34 |
|
|
//// ////
|
35 |
|
|
//// You should have received a copy of the GNU Lesser General ////
|
36 |
|
|
//// Public License along with this source; if not, download it ////
|
37 |
|
|
//// from http://www.opencores.org/lgpl.shtml ////
|
38 |
|
|
//// ////
|
39 |
|
|
//////////////////////////////////////////////////////////////////////
|
40 |
|
|
|
41 |
|
|
`include "spi_defines.v"
|
42 |
|
|
`include "timescale.v"
|
43 |
|
|
|
44 |
|
|
module spi_clgen (clk_in, rst, go, enable, last_clk, divider, clk_out, pos_edge, neg_edge);
|
45 |
|
|
|
46 |
|
|
parameter Tp = 1;
|
47 |
|
|
|
48 |
|
|
input clk_in; // input clock (system clock)
|
49 |
|
|
input rst; // reset
|
50 |
|
|
input enable; // clock enable
|
51 |
|
|
input go; // start transfer
|
52 |
|
|
input last_clk; // last clock
|
53 |
|
|
input [`SPI_DIVIDER_LEN-1:0] divider; // clock divider (output clock is divided by this value)
|
54 |
|
|
output clk_out; // output clock
|
55 |
|
|
output pos_edge; // pulse marking positive edge of clk_out
|
56 |
|
|
output neg_edge; // pulse marking negative edge of clk_out
|
57 |
|
|
|
58 |
|
|
reg clk_out;
|
59 |
|
|
reg pos_edge;
|
60 |
|
|
reg neg_edge;
|
61 |
|
|
|
62 |
|
|
reg [`SPI_DIVIDER_LEN-1:0] cnt; // clock counter
|
63 |
|
|
wire cnt_zero; // conter is equal to zero
|
64 |
|
|
wire cnt_one; // conter is equal to one
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
assign cnt_zero = cnt == {`SPI_DIVIDER_LEN{1'b0}};
|
68 |
|
|
assign cnt_one = cnt == {{`SPI_DIVIDER_LEN-1{1'b0}}, 1'b1};
|
69 |
|
|
|
70 |
|
|
// Counter counts half period
|
71 |
|
|
always @(posedge clk_in or posedge rst)
|
72 |
|
|
begin
|
73 |
|
|
if(rst)
|
74 |
|
|
cnt <= #Tp {`SPI_DIVIDER_LEN{1'b1}};
|
75 |
|
|
else
|
76 |
|
|
begin
|
77 |
|
|
if(!enable || cnt_zero)
|
78 |
|
|
cnt <= #Tp divider;
|
79 |
|
|
else
|
80 |
|
|
cnt <= #Tp cnt - {{`SPI_DIVIDER_LEN-1{1'b0}}, 1'b1};
|
81 |
|
|
end
|
82 |
|
|
end
|
83 |
|
|
|
84 |
|
|
// clk_out is asserted every other half period
|
85 |
|
|
always @(posedge clk_in or posedge rst)
|
86 |
|
|
begin
|
87 |
|
|
if(rst)
|
88 |
|
|
clk_out <= #Tp 1'b0;
|
89 |
|
|
else
|
90 |
|
|
clk_out <= #Tp (enable && cnt_zero && (!last_clk || clk_out)) ? ~clk_out : clk_out;
|
91 |
|
|
end
|
92 |
|
|
|
93 |
|
|
// Pos and neg edge signals
|
94 |
|
|
always @(posedge clk_in or posedge rst)
|
95 |
|
|
begin
|
96 |
|
|
if(rst)
|
97 |
|
|
begin
|
98 |
|
|
pos_edge <= #Tp 1'b0;
|
99 |
|
|
neg_edge <= #Tp 1'b0;
|
100 |
|
|
end
|
101 |
|
|
else
|
102 |
|
|
begin
|
103 |
|
|
pos_edge <= #Tp (enable && !clk_out && cnt_one) || (!(|divider) && clk_out) || (!(|divider) && go && !enable);
|
104 |
|
|
neg_edge <= #Tp (enable && clk_out && cnt_one) || (!(|divider) && !clk_out && enable);
|
105 |
|
|
end
|
106 |
|
|
end
|
107 |
|
|
endmodule
|
108 |
|
|
|