~ruther/vhdl-i2c

ref: 987035cd215d71ee109cae9038fe55d6cf83ea76 vhdl-i2c/tb/i2c/tb_i2c_slave_pkg.vhd -rw-r--r-- 3.9 KiB
987035cd — Rutherther fix: full_on skips indices 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
library ieee;
use ieee.std_logic_1164.all;

library vunit_lib;
-- use vunit_lib.check_pkg.all;
context vunit_lib.vunit_context;

use work.tb_pkg.all;
use work.tb_i2c_pkg.all;

package tb_i2c_slave_pkg is

  procedure i2c_slave_check_start (
    constant address : in std_logic_vector(6 downto 0);
    constant rw : in std_logic;
    constant timeout : in time;
    signal scl : inout std_logic;
    signal sda : inout std_logic;
    constant ack : in std_logic := '1');

  procedure i2c_slave_check_stop (
    constant timeout : in time;
    signal scl : inout std_logic;
    signal sda : inout std_logic);

  procedure i2c_slave_transmit (
    constant data        : in    std_logic_vector(7 downto 0);
    constant scl_timeout : in    time;
    signal scl           : inout std_logic;
    signal sda           : inout std_logic;
    constant exp_ack     : in    std_logic := '1');

  procedure i2c_slave_receive (
    constant exp_data       : in    std_logic_vector(7 downto 0);
    constant scl_timeout    : in    time;
    signal scl              : inout std_logic;
    signal sda              : inout std_logic;
    constant ack            : in    std_logic := '1');

end package tb_i2c_slave_pkg;

package body tb_i2c_slave_pkg is

  procedure i2c_slave_check_start (
    constant address : in std_logic_vector(6 downto 0);
    constant rw : in std_logic;
    constant timeout : in time;
    signal scl : inout std_logic;
    signal sda : inout std_logic;
    constant ack : in std_logic := '1') is
    begin
      wait_for_start_condition(timeout, scl, sda);
      i2c_slave_receive(address & rw, timeout, scl, sda, ack);
    end procedure i2c_slave_check_start;

  procedure i2c_slave_check_stop (
    constant timeout : in time;
    signal scl : inout std_logic;
    signal sda : inout std_logic) is
  begin
    wait_for_stop_condition(timeout, scl, sda);
  end procedure i2c_slave_check_stop;

  procedure i2c_slave_transmit (
    constant data        : in    std_logic_vector(7 downto 0);
    constant scl_timeout : in    time;
    signal scl           : inout std_logic;
    signal sda           : inout std_logic;
    constant exp_ack     : in    std_logic := '1') is

  begin  -- procedure transmit
    if scl = 'H' then
        wait_for_scl_fall(scl_timeout, scl);
    end if;

    -- data
    for i in 7 downto 0 loop
      wait until falling_edge(clk);
      sda <= '0' when data(i) = '0' else 'Z';
      wait_for_scl_rise(scl_timeout, scl);
      wait_for_scl_fall(scl_timeout, scl);
    end loop;  -- i

    wait until falling_edge(clk);
    sda <= 'Z';
    wait_for_scl_rise(scl_timeout, scl);

    if exp_ack = '1' then
      check_equal(sda, '0', "No acknowledge");
    elsif exp_ack = '0' then
      check_equal(sda, 'H', "There was acknowledge even though there shouldn't have been");
    end if;

    -- TODO consider removing this?
    wait_for_scl_fall(scl_timeout, scl);
    wait until falling_edge(clk);
    sda <= 'Z';
  end procedure i2c_slave_transmit;

  procedure i2c_slave_receive (
    constant exp_data       : in    std_logic_vector(7 downto 0);
    constant scl_timeout    : in    time;
    signal scl              : inout std_logic;
    signal sda              : inout std_logic;
    constant ack            : in    std_logic := '1') is

  begin  -- procedure transmit
    if scl = 'H' then
      wait_for_scl_fall(scl_timeout, scl);
    end if;

    sda <= 'Z';

    -- data
    for i in 7 downto 0 loop
      wait_for_scl_rise(scl_timeout, scl);
      if exp_data(i) = '1' then
        check(sda = '1' or sda = 'H', result("Received data (sda) not as expected."));
      elsif exp_data(i) = '0' then
        check(sda = '0' or sda = 'L', result("Received data (sda) not as expected."));
      end if;
      wait_for_scl_fall(scl_timeout, scl);
    end loop;  -- i

    if ack = '1' then
      sda <= '0';
    end if;

    wait_for_scl_rise(scl_timeout, scl);
    wait_for_scl_fall(scl_timeout, scl);

    sda <= 'Z';
  end procedure i2c_slave_receive;

end package body tb_i2c_slave_pkg;
Do not follow this link