| 1 |
2 |
sshuv2 |
|
| 2 |
|
|
--==============================================================================
|
| 3 |
|
|
-- |
|
| 4 |
|
|
-- Project: IIC Multiple Bus Controller (IICMB) |
|
| 5 |
|
|
-- |
|
| 6 |
|
|
-- Module: Signal conditioner. |
|
| 7 |
|
|
-- Version: |
|
| 8 |
|
|
-- 1.0, April 29, 2016 |
|
| 9 |
|
|
-- |
|
| 10 |
|
|
-- Author: Sergey Shuvalkin, (sshuv2@opencores.org) |
|
| 11 |
|
|
-- |
|
| 12 |
|
|
--==============================================================================
|
| 13 |
|
|
--==============================================================================
|
| 14 |
|
|
-- Copyright (c) 2016, Sergey Shuvalkin |
|
| 15 |
|
|
-- All rights reserved. |
|
| 16 |
|
|
-- |
|
| 17 |
|
|
-- Redistribution and use in source and binary forms, with or without |
|
| 18 |
|
|
-- modification, are permitted provided that the following conditions are met: |
|
| 19 |
|
|
-- |
|
| 20 |
|
|
-- 1. Redistributions of source code must retain the above copyright notice, |
|
| 21 |
|
|
-- this list of conditions and the following disclaimer. |
|
| 22 |
|
|
-- 2. Redistributions in binary form must reproduce the above copyright |
|
| 23 |
|
|
-- notice, this list of conditions and the following disclaimer in the |
|
| 24 |
|
|
-- documentation and/or other materials provided with the distribution. |
|
| 25 |
|
|
-- |
|
| 26 |
|
|
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
| 27 |
|
|
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
| 28 |
|
|
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
| 29 |
|
|
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
| 30 |
|
|
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
| 31 |
|
|
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
| 32 |
|
|
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
| 33 |
|
|
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
| 34 |
|
|
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
| 35 |
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
| 36 |
|
|
-- POSSIBILITY OF SUCH DAMAGE. |
|
| 37 |
|
|
--==============================================================================
|
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
|
|
library ieee;
|
| 41 |
|
|
use ieee.std_logic_1164.all;
|
| 42 |
|
|
|
| 43 |
|
|
|
| 44 |
|
|
--==============================================================================
|
| 45 |
|
|
entity conditioner is
|
| 46 |
|
|
generic
|
| 47 |
|
|
(
|
| 48 |
|
|
------------------------------------
|
| 49 |
|
|
g_f_clk : real := 100000.0; -- Frequency of 'clk' input (in kHz)
|
| 50 |
|
|
g_f_scl : real := 100.0 -- Frequency of 'scl_i' input (in kHz)
|
| 51 |
|
|
------------------------------------
|
| 52 |
|
|
);
|
| 53 |
|
|
port
|
| 54 |
|
|
(
|
| 55 |
|
|
------------------------------------
|
| 56 |
|
|
clk : in std_logic; -- Clock
|
| 57 |
|
|
s_rst : in std_logic; -- Synchronous reset (active high)
|
| 58 |
|
|
------------------------------------
|
| 59 |
|
|
------------------------------------
|
| 60 |
|
|
-- Interface to I2C FSMs:
|
| 61 |
|
|
busy : out std_logic; -- Bus busy indication (busy = high)
|
| 62 |
|
|
--
|
| 63 |
|
|
scl_rx : out std_logic; -- Filtered I2C Clock
|
| 64 |
|
|
sda_rx : out std_logic; -- Filtered I2C Data
|
| 65 |
|
|
--
|
| 66 |
|
|
scl_d_rx : out std_logic; -- Filtered and delayed I2C Clock
|
| 67 |
|
|
--
|
| 68 |
|
|
scl_tx : in std_logic; -- I2C Clock from FSMs
|
| 69 |
|
|
sda_tx : in std_logic; -- I2C Data from FSMs
|
| 70 |
|
|
------------------------------------
|
| 71 |
|
|
------------------------------------
|
| 72 |
|
|
-- I2C bus signals:
|
| 73 |
|
|
scl_i : in std_logic; -- I2C Clock input
|
| 74 |
|
|
sda_i : in std_logic; -- I2C Data input
|
| 75 |
|
|
scl_o : out std_logic; -- I2C Clock output
|
| 76 |
|
|
sda_o : out std_logic -- I2C Data output
|
| 77 |
|
|
------------------------------------
|
| 78 |
|
|
);
|
| 79 |
|
|
end entity conditioner;
|
| 80 |
|
|
--==============================================================================
|
| 81 |
|
|
|
| 82 |
|
|
--==============================================================================
|
| 83 |
|
|
architecture str of conditioner is
|
| 84 |
|
|
|
| 85 |
|
|
------------------------------------------------------------------------------
|
| 86 |
|
|
component bus_state is
|
| 87 |
|
|
generic
|
| 88 |
|
|
(
|
| 89 |
|
|
g_f_clk : real := 100000.0;
|
| 90 |
|
|
g_f_scl : real := 100.0
|
| 91 |
|
|
);
|
| 92 |
|
|
port
|
| 93 |
|
|
(
|
| 94 |
|
|
clk : in std_logic;
|
| 95 |
|
|
s_rst : in std_logic;
|
| 96 |
|
|
busy : out std_logic;
|
| 97 |
|
|
scl_d : out std_logic;
|
| 98 |
|
|
scl : in std_logic;
|
| 99 |
|
|
sda : in std_logic
|
| 100 |
|
|
);
|
| 101 |
|
|
end component bus_state;
|
| 102 |
|
|
------------------------------------------------------------------------------
|
| 103 |
|
|
|
| 104 |
|
|
------------------------------------------------------------------------------
|
| 105 |
|
|
component filter is
|
| 106 |
|
|
generic
|
| 107 |
|
|
(
|
| 108 |
|
|
g_cycles : positive := 10
|
| 109 |
|
|
);
|
| 110 |
|
|
port
|
| 111 |
|
|
(
|
| 112 |
|
|
clk : in std_logic;
|
| 113 |
|
|
s_rst : in std_logic;
|
| 114 |
|
|
sig_in : in std_logic;
|
| 115 |
|
|
sig_out : out std_logic
|
| 116 |
|
|
);
|
| 117 |
|
|
end component filter;
|
| 118 |
|
|
------------------------------------------------------------------------------
|
| 119 |
|
|
|
| 120 |
|
|
------------------------------------------------------------------------------
|
| 121 |
|
|
function get_cycles(a : real) return positive is
|
| 122 |
|
|
variable ret : positive;
|
| 123 |
|
|
begin
|
| 124 |
|
|
ret := 4 + integer((4.0*a)/50000.0);
|
| 125 |
|
|
return ret;
|
| 126 |
|
|
end function get_cycles;
|
| 127 |
|
|
------------------------------------------------------------------------------
|
| 128 |
|
|
|
| 129 |
|
|
constant c_cycles : positive := get_cycles(g_f_clk);
|
| 130 |
|
|
|
| 131 |
|
|
signal scl_i_ndeb_1 : std_logic;
|
| 132 |
|
|
signal sda_i_ndeb_1 : std_logic;
|
| 133 |
|
|
signal scl_i_ndeb_2 : std_logic;
|
| 134 |
|
|
signal sda_i_ndeb_2 : std_logic;
|
| 135 |
|
|
signal scl_i_deb : std_logic;
|
| 136 |
|
|
signal sda_i_deb : std_logic;
|
| 137 |
|
|
|
| 138 |
|
|
begin
|
| 139 |
|
|
|
| 140 |
|
|
-- ###########################################################################
|
| 141 |
|
|
-- # Debouncing SCL and SDA signals #
|
| 142 |
|
|
-- ###########################################################################
|
| 143 |
|
|
------------------------------------------------------------------------------
|
| 144 |
|
|
-- Metastability elimination:
|
| 145 |
|
|
process(clk)
|
| 146 |
|
|
begin
|
| 147 |
|
|
if rising_edge(clk) then
|
| 148 |
|
|
if (s_rst = '1') then
|
| 149 |
|
|
scl_i_ndeb_1 <= '1';
|
| 150 |
|
|
scl_i_ndeb_2 <= '1';
|
| 151 |
|
|
sda_i_ndeb_1 <= '1';
|
| 152 |
|
|
sda_i_ndeb_2 <= '1';
|
| 153 |
|
|
else
|
| 154 |
|
|
scl_i_ndeb_1 <= to_x01(scl_i);
|
| 155 |
|
|
scl_i_ndeb_2 <= scl_i_ndeb_1;
|
| 156 |
|
|
sda_i_ndeb_1 <= to_x01(sda_i);
|
| 157 |
|
|
sda_i_ndeb_2 <= sda_i_ndeb_1;
|
| 158 |
|
|
end if;
|
| 159 |
|
|
end if;
|
| 160 |
|
|
end process;
|
| 161 |
|
|
------------------------------------------------------------------------------
|
| 162 |
|
|
|
| 163 |
|
|
------------------------------------------------------------------------------
|
| 164 |
|
|
scl_filter : filter
|
| 165 |
|
|
generic map
|
| 166 |
|
|
(
|
| 167 |
|
|
g_cycles => c_cycles
|
| 168 |
|
|
)
|
| 169 |
|
|
port map
|
| 170 |
|
|
(
|
| 171 |
|
|
clk => clk,
|
| 172 |
|
|
s_rst => s_rst,
|
| 173 |
|
|
sig_in => scl_i_ndeb_2,
|
| 174 |
|
|
sig_out => scl_i_deb
|
| 175 |
|
|
);
|
| 176 |
|
|
------------------------------------------------------------------------------
|
| 177 |
|
|
|
| 178 |
|
|
------------------------------------------------------------------------------
|
| 179 |
|
|
sda_filter : filter
|
| 180 |
|
|
generic map
|
| 181 |
|
|
(
|
| 182 |
|
|
g_cycles => c_cycles
|
| 183 |
|
|
)
|
| 184 |
|
|
port map
|
| 185 |
|
|
(
|
| 186 |
|
|
clk => clk,
|
| 187 |
|
|
s_rst => s_rst,
|
| 188 |
|
|
sig_in => sda_i_ndeb_2,
|
| 189 |
|
|
sig_out => sda_i_deb
|
| 190 |
|
|
);
|
| 191 |
|
|
------------------------------------------------------------------------------
|
| 192 |
|
|
-- ###########################################################################
|
| 193 |
|
|
-- # End of debouncing SCL and SDA signals #
|
| 194 |
|
|
-- ###########################################################################
|
| 195 |
|
|
|
| 196 |
|
|
------------------------------------------------------------------------------
|
| 197 |
|
|
bus_state_inst0 : bus_state
|
| 198 |
|
|
generic map
|
| 199 |
|
|
(
|
| 200 |
|
|
g_f_clk => g_f_clk,
|
| 201 |
|
|
g_f_scl => g_f_scl
|
| 202 |
|
|
)
|
| 203 |
|
|
port map
|
| 204 |
|
|
(
|
| 205 |
|
|
clk => clk,
|
| 206 |
|
|
s_rst => s_rst,
|
| 207 |
|
|
busy => busy,
|
| 208 |
|
|
scl_d => scl_d_rx,
|
| 209 |
|
|
scl => scl_i_deb,
|
| 210 |
|
|
sda => sda_i_deb
|
| 211 |
|
|
);
|
| 212 |
|
|
------------------------------------------------------------------------------
|
| 213 |
|
|
|
| 214 |
|
|
scl_rx <= scl_i_deb;
|
| 215 |
|
|
sda_rx <= sda_i_deb;
|
| 216 |
|
|
scl_o <= scl_tx;
|
| 217 |
|
|
sda_o <= sda_tx;
|
| 218 |
|
|
|
| 219 |
|
|
end architecture str;
|
| 220 |
|
|
--==============================================================================
|
| 221 |
|
|
|