~ruther/vhdl-i2c

ref: 0f8e8a5290dbcd5644f04e88b4ab973021e69f88 vhdl-i2c/tb/i2c/tb_i2c_master_pkg.vhd -rw-r--r-- 4.5 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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_master_pkg is

  procedure i2c_master_stop (
    signal scl : inout std_logic;
    signal sda : inout std_logic);

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

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

  procedure i2c_master_start (
    constant address    : in    std_logic_vector(6 downto 0);
    constant rw         : in    std_logic;
    signal scl : inout std_logic;
    signal sda : inout std_logic;
    constant exp_ack    : in    std_logic := '1');

end package tb_i2c_master_pkg;

package body tb_i2c_master_pkg is

  procedure i2c_master_stop (
    signal scl : inout std_logic;
    signal sda : inout std_logic) is
  begin  -- procedure stop_tx

    scl_fall(scl);
    sda_fall(sda);
    scl_rise(scl);

    -- stop condition
    sda_rise(sda, '0');

  end procedure i2c_master_stop;

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

  begin  -- procedure transmit
    check_equal(scl, 'H', "Cannot start sending when scl is not in default state (1). Seems like the slave is clock stretching. This is not supported by transmit since data have to be supplied or read.", failure);

    scl_fall(scl);

    -- data
    for i in 7 downto 0 loop
      sda <= '0' when data(i) = '0' else 'Z';
      scl_pulse(scl);
    end loop;  -- i

    sda <= 'Z';
    scl_rise(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;

    if stop_condition = '1' then
      if sda = '0' then
        -- keep sda low
        sda <= '0';
      end if;

      i2c_master_stop(scl, sda);

    end if;
  end procedure i2c_master_transmit;

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

  begin  -- procedure transmit
    check_equal(scl, 'H', "Cannot start receiving when scl is not in default state (1). Seems like the slave is clock stretching. This is not supported by transmit since data have to be supplied or read.", failure);

    scl_fall(scl);
    sda <= 'Z';

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

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

    scl_rise(scl);

    if stop_condition = '1' then
      if sda = '0' then
        -- keep sda low
        sda <= 'Z';
      end if;

      i2c_master_stop(scl, sda);

    end if;
  end procedure i2c_master_receive;

  procedure i2c_master_start (
    constant address : in    std_logic_vector(6 downto 0);
    constant rw      : in    std_logic;
    signal scl       : inout std_logic;
    signal sda       : inout std_logic;
    constant exp_ack : in    std_logic := '1') is
  begin
    if scl = 'H' and sda = '0' then
      scl_fall(scl);
    end if;

    if sda = '0' then
      sda_rise(sda);
    end if;

    if scl = '0' then
      scl_rise(scl);
    end if;

    check_equal(sda, 'H', "Cannot start sending when sda is not in default state (1).", failure);
    check_equal(scl, 'H', "Cannot start sending when scl is not in default state (1).", failure);

    -- start condition
    sda_fall(sda, '0');

    i2c_master_transmit(address & rw, scl, sda, stop_condition => '0', exp_ack => exp_ack);

  end procedure i2c_master_start;

end package body tb_i2c_master_pkg;
Do not follow this link