~ruther/vhdl-i2c

ref: 01263b874ce8d9b39914f57dd58bf2329428a2e7 vhdl-i2c/src/utils/clock_divider.vhd -rw-r--r-- 1.0 KiB
01263b87 — Rutherther fix: move to bus busy on arbitration err or start condition 1 year, 3 months ago
                                                                                
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
library ieee;
use ieee.std_logic_1164.all;

entity clock_divider is

  generic (
    IN_FREQ       : integer;
    OUT_FREQ   : integer);

  port (
    clk_i  : in  std_logic;
    clk_o  : out std_logic);

end entity clock_divider;

architecture a1 of clock_divider is
  constant MAX : integer := IN_FREQ / OUT_FREQ / 2;
  signal curr_count : integer range 0 to MAX - 1;
  signal next_count : integer range 0 to MAX - 1;

  signal gen_clk : std_logic;
begin  -- architecture a1
  keep_max_freq: if IN_FREQ = OUT_FREQ generate
    clk_o <= clk_i;
  end generate keep_max_freq;

  counter: if IN_FREQ /= OUT_FREQ generate
    clk_o <= gen_clk;
    next_count <= (curr_count - 1) when curr_count > 0 else MAX - 1;

    set_counter: process (clk_i) is
    begin  -- process set_counter
      if rising_edge(clk_i) then          -- rising clock edge
        curr_count <= next_count;

        if curr_count = 0 then
          gen_clk <= not gen_clk;
        end if;
      end if;
    end process set_counter;
  end generate counter;

end architecture a1;
Do not follow this link