~ruther/vhdl-i2c

ref: 0f8e8a5290dbcd5644f04e88b4ab973021e69f88 vhdl-i2c/tb/mcu_slave/regs_tb.vhd -rw-r--r-- 3.6 KiB
0f8e8a52 — Rutherther feat: handle errors in slave state 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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

library vunit_lib;
context vunit_lib.vunit_context;

library mcu_slave;

library utils;

library i2c_tb;
use i2c_tb.tb_pkg.all;
use i2c_tb.tb_i2c_pkg.all;
use i2c_tb.tb_i2c_master_pkg.all;

entity regs_tb is

  generic (
    runner_cfg : string);

end entity regs_tb;

architecture tb of regs_tb is
  constant ADDRESS : std_logic_vector(6 downto 0) := "1110101";
  constant CLK_PERIOD : time := 10 ns;
  constant REGS_COUNT : integer := 20;

  signal rst_n : std_logic := '0';
  signal rst : std_logic;

  signal not_scl : std_logic;

  signal err_noack          : std_logic;
  signal bus_busy, dev_busy : std_logic;

  signal one : std_logic := '1';
begin  -- architecture tb
  uut : entity mcu_slave.regs
    generic map (
      DELAY => 1)
    port map (
      clk_i       => clk,
      rst_i       => rst,
      rst_on      => open,
      err_noack_o => err_noack,
      dev_busy_o  => dev_busy,
      bus_busy_o  => bus_busy,
      sda_io      => sda,
      scl_io      => scl
    );

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

  not_scl <= not scl;

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

  -- TODO: allow conditions from master...
  -- sda_stability_check: check_stable(clk, one, scl, not_scl, sda);

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

    while test_suite loop
      if run("write_all_ones_read") then
        i2c_master_start(ADDRESS, '0', scl, sda);

        -- seek address 0
        i2c_master_transmit("00000000", scl, sda);
        for i in 0 to REGS_COUNT - 1 loop
          i2c_master_transmit("00000001", scl, sda);
        end loop;  -- i

        i2c_master_start(ADDRESS, '1', scl, sda);
        for i in 0 to REGS_COUNT - 1 loop
          i2c_master_receive("00000001", scl, sda);
        end loop;  -- i

        i2c_master_stop(scl, sda);
      elsif run("write_sequence_read") then
        i2c_master_start(ADDRESS, '0', scl, sda);

        i2c_master_transmit("00000000", scl, sda);
        for i in 0 to REGS_COUNT - 1 loop
          i2c_master_transmit(std_logic_vector(to_unsigned(i, 8)), scl, sda);
        end loop;  -- i

        i2c_master_stop(scl, sda);

        i2c_master_start(ADDRESS, '0', scl, sda);
        -- seek address 0
        i2c_master_transmit("00000000", scl, sda);
        i2c_master_start(ADDRESS, '1', scl, sda);

        for i in 0 to REGS_COUNT - 1 loop
          i2c_master_receive(std_logic_vector(to_unsigned(i, 8)), scl, sda);
        end loop;  -- i

        i2c_master_stop(scl, sda);
      elsif run("write_five_read_all") then
        i2c_master_start(ADDRESS, '0', scl, sda);

        -- seek address 5
        i2c_master_transmit("00000101", scl, sda);

        for i in 0 to 4 loop
          i2c_master_transmit(std_logic_vector(to_unsigned(i, 8)), scl, sda);
        end loop;  -- i

        i2c_master_stop(scl, sda);

        i2c_master_start(ADDRESS, '0', scl, sda);
        -- seek address 0
        i2c_master_transmit("00000000", scl, sda);
        i2c_master_start(ADDRESS, '1', scl, sda);

        for i in 0 to 4 loop
          i2c_master_receive("00000000", scl, sda);
        end loop;  -- i

        for i in 0 to 4 loop
          i2c_master_receive(std_logic_vector(to_unsigned(i, 8)), scl, sda);
        end loop;  -- i

        for i in 10 to REGS_COUNT - 1 loop
          i2c_master_receive("00000000", scl, sda);
        end loop;  -- i

        i2c_master_stop(scl, sda);
      end if;
    end loop;

    test_runner_cleanup(runner);
  end process main;

end architecture tb;
Do not follow this link