1 | ////////////////////////////////////////////////////////////////////// |
---|
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 | |
---|
42 | module spi_clgen (clk_in, rst, go, enable, last_clk, divider, clk_out, pos_edge, neg_edge); |
---|
43 | |
---|
44 | parameter Tp = 1; |
---|
45 | |
---|
46 | input clk_in; // input clock (system clock) |
---|
47 | input rst; // reset |
---|
48 | input enable; // clock enable |
---|
49 | input go; // start transfer |
---|
50 | input last_clk; // last clock |
---|
51 | input [3:0] divider; // clock divider (output clock is divided by this value) |
---|
52 | output clk_out; // output clock |
---|
53 | output pos_edge; // pulse marking positive edge of clk_out |
---|
54 | output neg_edge; // pulse marking negative edge of clk_out |
---|
55 | |
---|
56 | reg clk_out; |
---|
57 | reg pos_edge; |
---|
58 | reg neg_edge; |
---|
59 | |
---|
60 | reg [3:0] cnt; // clock counter |
---|
61 | wire cnt_zero; // conter is equal to zero |
---|
62 | wire cnt_one; // conter is equal to one |
---|
63 | |
---|
64 | |
---|
65 | assign cnt_zero = cnt == {4{1'b0}}; |
---|
66 | assign cnt_one = cnt == {{3{1'b0}}, 1'b1}; |
---|
67 | |
---|
68 | // Counter counts half period |
---|
69 | always @(posedge clk_in or posedge rst) |
---|
70 | begin |
---|
71 | if(rst) |
---|
72 | cnt <= #Tp {4{1'b1}}; |
---|
73 | else |
---|
74 | begin |
---|
75 | if(!enable || cnt_zero) |
---|
76 | cnt <= #Tp divider; |
---|
77 | else |
---|
78 | cnt <= #Tp cnt - {{3{1'b0}}, 1'b1}; |
---|
79 | end |
---|
80 | end |
---|
81 | |
---|
82 | // clk_out is asserted every other half period |
---|
83 | always @(posedge clk_in or posedge rst) |
---|
84 | begin |
---|
85 | if(rst) |
---|
86 | clk_out <= #Tp 1'b0; |
---|
87 | else |
---|
88 | clk_out <= #Tp (enable && cnt_zero && (!last_clk || clk_out)) ? ~clk_out : clk_out; |
---|
89 | end |
---|
90 | |
---|
91 | // Pos and neg edge signals |
---|
92 | always @(posedge clk_in or posedge rst) |
---|
93 | begin |
---|
94 | if(rst) |
---|
95 | begin |
---|
96 | pos_edge <= #Tp 1'b0; |
---|
97 | neg_edge <= #Tp 1'b0; |
---|
98 | end |
---|
99 | else |
---|
100 | begin |
---|
101 | pos_edge <= #Tp (enable && !clk_out && cnt_one) || (!(|divider) && clk_out) || (!(|divider) && go && !enable); |
---|
102 | neg_edge <= #Tp (enable && clk_out && cnt_one) || (!(|divider) && !clk_out && enable); |
---|
103 | end |
---|
104 | end |
---|
105 | endmodule |
---|
106 | |
---|