~ruther/vhdl-i2c

ref: 9b0b309f2f2c064ca2317d51637edef05a0bb2bf vhdl-i2c/tb/i2c/address_detector_tb.vhd -rw-r--r-- 6.1 KiB
9b0b309f — Rutherther tests: update address detector to use new bus mod 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
library ieee;
use ieee.std_logic_1164.all;

library vunit_lib;
context vunit_lib.vunit_context;
context vunit_lib.com_context;

use work.i2c_bus_pkg;

library i2c;

entity address_detector_tb is

  generic (
    runner_cfg : string);

end entity address_detector_tb;

architecture tb of address_detector_tb is
  signal clk : std_logic := '0';
  constant CLK_PERIOD : time := 1 us;
  constant SCL_FREQ : real := 100_000.0;

  signal rst_n : std_logic := '0';

  signal dev_sda : std_logic;
  signal scl_rising, scl_falling : std_logic := '0';

  signal scl : std_logic;
  signal sda : std_logic;

  signal start : std_logic;

  signal address : std_logic_vector(6 downto 0);
  signal success, fail, rw : std_logic;

  signal sda_enable : std_logic;

  signal one : std_logic := '1';

  constant bus_inst_name : string := "i2c_bus_mod";
  constant bus_actor : actor_t := i2c_bus_pkg.get_actor(bus_inst_name);

  constant monitor_inst_name : string := "monitor";
  constant monitor_actor : actor_t := i2c_bus_pkg.get_actor(monitor_inst_name);
begin  -- architecture tb

  clk <= not clk after CLK_PERIOD / 2;
  rst_n <= '1' after 2 * CLK_PERIOD;

  sda <= 'H';
  scl <= 'H';

  sda <= '0' when sda_enable = '1' else 'Z';

  dev_sda <= '1' when sda = 'H' else sda;

  uut : entity i2c.address_detector
    port map (
      clk_i                 => clk,
      rst_in                => rst_n,
      store_address_i       => '1',
      address_i             => address,
      scl_rising            => scl_rising,
      scl_falling_delayed_i => scl_falling,
      sda_i                 => dev_sda,
      sda_enable_o          => sda_enable,
      start_i               => start,
      rw_o                  => rw,
      success_o             => success,
      fail_o                => fail);

  bus_mod : entity work.i2c_bus_mod
    generic map (
      inst_name        => bus_inst_name,
      default_scl_freq => SCL_FREQ)
    port map (
      sda_io        => sda,
      scl_io        => scl,

      clk_i         => clk,
      scl_falling_o => scl_falling,
      scl_rising_o  => scl_rising);

  -- monitor_mod : entity work.i2c_bus_mod
  --   generic map (
  --     inst_name        => monitor_inst_name,
  --     default_scl_freq => SCL_FREQ)
  --   port map (
  --     sda_io        => sda,
  --     scl_io        => scl,

  --     clk_i         => '0',
  --     scl_falling_o => open,
  --     scl_rising_o  => open);

  main: process is
  begin  -- process main
    wait until rst_n = '1';
    wait until falling_edge(clk);

    test_runner_setup(runner, runner_cfg);
    set_stop_level(failure);

    while test_suite loop
      if run("matching") then
        address <= "1100011";
        check_equal(success, '0');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11000110", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '1', 1 ms, bus_actor);

        start <= '1';
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '1');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_stop_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.wait_until_idle(net, bus_actor);

      elsif run("read") then
        address <= "1100011";
        check_equal(success, '0');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11000111", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '1', 1 ms, bus_actor);

        start <= '1';
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '1');
        check_equal(fail, '0');
        check_equal(rw, '1');
      elsif run("write") then
        address <= "1100011";
        check_equal(success, '0');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11000110", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '1', 1 ms, bus_actor);

        start <= '1';
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '1');
        check_equal(fail, '0');
        check_equal(rw, '0');
      elsif run("not_matching") then
        address <= "1100011";
        check_equal(success, '0');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11100011", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '0', 1 ms, bus_actor);

        start <= '1';
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '0');
        check_equal(fail, '1');
      elsif run("not_matching_then_matching") then
        address <= "1100011";
        check_equal(success, '0');
        check_equal(fail, '0');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11100011", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '0', 1 ms, bus_actor);
        i2c_bus_pkg.gen_stop_cond(net, 1 ms, bus_actor);

        start <= '1';
        wait until rising_edge(clk);
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '0');
        check_equal(fail, '1');

        i2c_bus_pkg.gen_start_cond(net, 1 ms, bus_actor);
        i2c_bus_pkg.send_data_and_clock(net, "11000110", 1 ms, bus_actor);
        i2c_bus_pkg.check_ack_gen_clock(net, '1', 1 ms, bus_actor);
        i2c_bus_pkg.gen_stop_cond(net, 1 ms, bus_actor);

        start <= '1';
        wait until rising_edge(clk);
        wait until falling_edge(clk);
        start <= '0';

        i2c_bus_pkg.wait_until_idle(net, bus_actor);

        check_equal(success, '1');
        check_equal(fail, '0');
      end if;
    end loop;

    test_runner_cleanup(runner);
  end process main;

  test_runner_watchdog(runner, 10 ms);
end architecture tb;
Do not follow this link