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

Subversion Repositories baudgen

[/] [baudgen/] [web_uploads/] [am_baud_rate_gen.vhd] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 root
-----------------------------------------------------------------------------
2
--      Filename:       am_baud_rate_gen.vhd
3
--
4
--      Description:
5
--              a paramatizable baud rate generator
6
--
7
--      input a 'high speed' clock, and get out a clock enable of x times the baud rate, and the baud rate.
8
--        paramiters are the high speed clock frequency, the baud rate required, and the over sample needed.
9
--
10
-- works by having two counters,
11
-- fast counter, counts down to x time baud rate
12
-- slow counter, then divides this to give baud rate.
13
--
14
--
15
--      Copyright (c) 2007 by Andrew Mulcock 
16
--              an OpenCores.org Project
17
--              free to use, but see documentation for conditions 
18
--
19
--      Revision        History:
20
--      Revision        Date                    Author          Comment
21
--      --------        ----------              ---------       -----------
22
--      1.0             26/Nov/07       A Mulcock       Initial revision
23
--
24
-----------------------------------------------------------------------------
25
library ieee ;
26
use ieee.std_logic_1164.all ;
27
use ieee.numeric_std.all ;
28
 
29
entity am_baud_rate_gen is
30
   generic(
31
      baudrate       : integer := 115200;
32
      clock_freq_mhz : real    := 200.0;
33
      over_sample    : integer := 4
34
   );
35
        port(
36
                clk              : in std_logic;
37
                rst              : in std_logic;
38
                baud_x_en : out std_logic;
39
                baud_en   : out std_logic
40
                );
41
end entity;
42
 
43
-- ==========================================================================================
44
 
45
architecture baud_rtl of am_baud_rate_gen is
46
 
47
-- calculate from the clock freq, the baud rate, and the over sample ratio
48
--  the size and count of the two counters.
49
 
50
constant        div_ratio_real  : real     := ( clock_freq_mhz * 1000000.0) / ((real(baudrate) * real(over_sample)) );
51
constant        div_ratio_int           : integer       := integer ( div_ratio_real - 0.5); -- 0.5 gives rounding up / down 
52
constant over_sample_ratio : integer   := over_sample -1;
53
constant max_count         : integer   := div_ratio_int;
54
 
55
signal   fast_counter      : integer range 0 to div_ratio_int;
56
signal   slow_counter      : integer range 0 to over_sample_ratio;
57
signal   slow_cnt_en       : std_logic;
58
 
59
begin
60
 
61
 
62
 
63
------------------------------------------------------------
64
------------ baud rate counter -----------------------------
65
------------------------------------------------------------
66
 
67
-- in an fpga, don't need to reset a wrap around counter,
68
-- but somepeople still like to for simulation
69
-- so comparmise and reset syncronously, as suits the syncronous counter.
70
 
71
process(clk)
72
begin
73
   if rising_edge(clk) then
74
      if ( (rst = '1')  or (fast_counter = 0) ) then
75
         fast_counter <= max_count;
76
         slow_cnt_en <= not( rst );
77
      else
78
         fast_counter <= fast_counter - 1;
79
         slow_cnt_en <= '0';
80
      end if;
81
        end if;
82
end process;
83
 
84
process(clk)
85
begin
86
   if rising_edge(clk) then
87
      if (rst = '1') or ( slow_counter = 0 and slow_cnt_en = '1' ) then
88
         slow_counter <= over_sample_ratio;
89
         baud_en <= not( rst);
90
      elsif slow_cnt_en = '1' then
91
         slow_counter <= slow_counter - 1;
92
         baud_en <= '0';
93
      else
94
         slow_counter <= slow_counter;
95
         baud_en <= '0';
96
      end if;
97
        end if;
98
end process;
99
 
100
         baud_x_en <= slow_cnt_en;
101
 
102
end baud_rtl;
103
 

powered by: WebSVN 2.1.0

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