OpenCores
URL https://opencores.org/ocsvn/spi/spi/trunk

Subversion Repositories spi

[/] [spi/] [tags/] [rel_1/] [rtl/] [verilog/] [spi_clgen.v] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.