OpenCores
URL https://opencores.org/ocsvn/onewire/onewire/trunk

Subversion Repositories onewire

[/] [onewire/] [trunk/] [HDL/] [ds1820_mstr_tb.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 skeptonomi
----------------------------------------------------------------------------------
2
--  <c>2018 william b hunter
3
--    This file is part of ow2rtd.
4
--
5
--    ow2rtd is free software: you can redistribute it and/or modify
6
--    it under the terms of the GNU Lessor General Public License as published by
7
--    the Free Software Foundation, either version 3 of the License, or
8
--    (at your option) any later version.
9
--
10
--    ow2rtd is distributed in the hope that it will be useful,
11
--    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
--    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
--    GNU General Public License for more details.
14
--
15
--    You should have received a copy of the GNU Lessor General Public License
16
--    along with ow2rtd.  If not, see <https://www.gnu.org/licenses/>.
17
-----------------------------------------------------------------------------------  
18
--  Create Date: 5/15/2018
19
--  file: ds1820_mstr_tb.vhd
20
--  description: Testbench for both the ow_mstr and ds1820_mster modules
21
--
22
-------------------------------------------------------------------------------------
23
 
24
library IEEE;
25
use IEEE.STD_LOGIC_1164.ALL;
26
use IEEE.numeric_std.all;
27
use ieee.std_logic_textio.all;
28
library std;
29
use STD.textio.all;
30
 
31
-------------------------------------------------------------------------------------
32
-- Entity declaration
33
-------------------------------------------------------------------------------------
34
entity ds1820_mstr_tb is
35
end ds1820_mstr_tb;
36
 
37
-------------------------------------------------------------------------------------
38
-- Architecture declaration
39
-------------------------------------------------------------------------------------
40
architecture sim of ds1820_mstr_tb is
41
 
42
  -------------------------------------
43
  --    Check Temps Proceedure      ---
44
  -------------------------------------
45
  --check_temp - proceedure to check the output data, temp and tempidx, against expected values
46
  procedure check_temp (
47
    signal temp    : in signed(15 downto 0);  --the temp reported from the DUT
48
    signal idx     : in unsigned(4 downto 0); --the index of the sensor (sensor number) from the DUT
49
    exptemp : in signed(15 downto 0);         --expected temperature value
50
    expidx  : in unsigned(4 downto 0))        --expected index value
51
  is
52
    variable myline : line;
53
    variable dec : integer;
54
    variable frac : integer;
55
  begin
56
    --line := "";
57
    if idx /= expidx then
58
      write(myline,string'("Error: wrong idx, expected "));
59
      write(myline,to_integer(expidx));
60
      write(myline,string'(", got "));
61
      write(myline,to_integer(idx));
62
      writeline(output,myline);
63
      assert false report "bad tempidx value" severity error;
64
    end if;
65
    if temp /= exptemp then
66
      --write(output, std_logic_vector(temp));
67
      write(myline,string'("Error: wrong temp on sensor "));
68
      write(myline,to_integer(expidx));
69
      write(myline,string'(", expected "));
70
      dec := to_integer(exptemp) / 16;
71
      frac := ((to_integer(exptemp) - 16*dec)*100)/16;
72
      write(myline,dec);
73
      write(myline,string'("."));
74
      write(myline,frac);
75
      write(myline,string'(", got "));
76
      dec := to_integer(temp) / 16;
77
      frac := ((to_integer(temp) - 16*dec)*100)/16;
78
      write(myline,dec);
79
      write(myline,string'("."));
80
      write(myline,frac);
81
      write(myline,string'(", got "));
82
      writeline(output,myline);
83
      assert false report "bad temp value" severity error;
84
    end if;
85
  end procedure check_temp;
86
 
87
  -------------------------------------------
88
  ---       Signal declaration            ---
89
  -------------------------------------------
90
  constant clkfreqmhz : integer := 100;                   --clock frequency in mhz
91
  constant halfperiodns : time := 1000 ns /(clkfreqmhz*2);--half the clock period
92
  signal clk      : std_logic;
93
 
94
  constant CONVERSION_TIME : integer := 188;   --ADC conversion time in ms
95
 
96
  signal srst  : std_logic := '0';           --synchronous reset
97
  signal rst_cntr : unsigned(7 downto 0):= x"ff";
98
  signal stb1us_cntr  : integer range 1 to clkfreqmhz := 1; --counter used to generate stb1us
99
  signal stb1us  : std_logic := '0';  --strobe 1 us, goes high for one clock every 1 us
100
 
101
  signal search_init  : std_logic := '0';     --triggers the search module
102
  signal temp_init    : std_logic := '0';     --triggers the initialization module
103
  signal temp_conv    : std_logic := '0';     --triggers the temperature conversion
104
  signal temp_read    : std_logic := '0';     --triggers the reading of the temperature results
105
  signal ow_busy      : std_logic;            -- the one wire bus is busy
106
  signal ow_err       : std_logic;            --there is an error on the one wire bus
107
  signal temp         : signed(15 downto 0);  --signed temp from sensor, value is 16 times the temp in C
108
  signal tempidx      : unsigned(4 downto 0); --index of the current temp sensor
109
  signal tempstb      : std_logic;            --strobe to indicate an updated temp sensor value
110
  signal owin         : std_logic;            --one wire input to dut
111
  signal owout        : std_logic;            --one wire output from dut
112
  signal dio          : std_logic;            --one wire bus
113
 
114
begin
115
 
116
  -------------------------------------
117
  --    global timing signals       ---
118
  -------------------------------------
119
 
120
  p_osc : process
121
  begin
122
    clk <= '0';
123
    wait for halfperiodns;
124
    clk <= '1';
125
    wait for halfperiodns;
126
  end process p_osc;
127
 
128
  p_rst : process
129
  begin
130
    srst <= '1';
131
    wait for 5 us;
132
    wait until clk = '1';
133
    srst <= '0';
134
    wait;
135
  end process p_rst;
136
 
137
  --generate a 1 us strobe for timing
138
  p_stb1us : process(clk)
139
  begin
140
    if rising_edge(clk) then
141
      if srst = '1' then
142
        stb1us <= '0';
143
        stb1us_cntr <= 1;
144
      else
145
        if stb1us_cntr = clkfreqmhz then
146
          stb1us <= '1';
147
          stb1us_cntr <= 1;
148
        else
149
          stb1us <= '0';
150
          stb1us_cntr <= stb1us_cntr + 1;
151
        end if;
152
      end if;
153
    end if;
154
  end process p_stb1us;
155
 
156
  -------------------------------------
157
  --              DUT               ---
158
  -------------------------------------
159
 --ow_mstr - DUT (device under test)
160
  -- one wire master - this performs all the one wire logic
161
  --  such as configuring and reading the DS1820 devices
162
  u_dut : entity work.ds1820_mstr(rtl)
163
  port map(
164
    --global signals
165
    clk => clk,
166
    srst => srst,
167
    stb1us => stb1us,            --1 us strobe
168
    busy => ow_busy,
169
    err  => ow_err,
170
    --high level interfaces, lets this module do the heavy lifting. For microprocessor control,
171
    search_stb  => search_init,  --searches for devices on bus
172
    temp_init   => temp_init,    --initiates temperature read from all devices
173
    temp_conv   => temp_conv,    --initiates temperature read from all devices
174
    temp_read   => temp_read,    --initiates temperature read from all devices
175
    temp        => temp,         --temperatures read from temp devices
176
    tempidx     => tempidx,      --temperatures index
177
    tempstb     => tempstb,      --temperatures ready strobe
178
    --one wire bus interface, requires external 5k resistor on bus
179
    owin        => owin,       --one wire input
180
    owout       => owout       --one wire output
181
  );
182
 
183
  -----------------------------------------
184
  --    one wire bus, open collector    ---
185
  -----------------------------------------
186
  --handle in/out nature of one wire interface
187
  dio <= '0' when owout = '0' else 'Z';  --output, only drives low, tristates when not low, external 5k pullup
188
  owin <= '0' when dio = '0' else '1';   --input, make sure H,Z,1 all map to '1' for simulation
189
  dio <= 'H';  --simulates the external pullup resistor
190
 
191
  -----------------------------------------
192
  --   test bench control and checks    ---
193
  -----------------------------------------
194
 
195
  --p_control - controls the testing, initiates commands to the DUT
196
  p_control : process
197
  begin
198
        search_init  <= '0';  --searches for devices on bus
199
    temp_init   <= '0';   --initiates temperature read from all devices
200
    temp_conv   <= '0';   --initiates temperature read from all devices
201
    temp_read    <= '0';  --initiates temperature read from all devices
202
    --wait for reset and a bit of time to settle
203
    wait for 10 us;
204
    --initiate a search of the one wire devices on the bus
205
    wait until clk = '0';
206
    search_init <= '1';
207
    wait until clk = '0';
208
    search_init <= '0';
209
    wait until clk = '0';
210
    --wait for search to complete
211
    wait until ow_busy = '0';
212
    wait until clk = '0';
213
    --initialize all the temperature sensors on the bus
214
    wait until clk = '0';
215
    temp_init   <= '1';
216
    wait until clk = '0';
217
    temp_init   <= '0';
218
    wait until clk = '0';
219
    --wait for search to complete
220
    wait until ow_busy = '0';
221
    wait for 5 us;
222
    --start conversions on all the sensors
223
    wait until clk = '0';
224
    temp_conv   <= '1';
225
    wait until clk = '0';
226
    temp_conv   <= '0';
227
    wait until clk = '0';
228
    --wait for conversion commands to complete
229
    wait until ow_busy = '0';
230
    --now wait until the conversions are actually complete
231
    wait for 190 ms;
232
    --read the temp from all the sensors
233
    wait until clk = '0';
234
    temp_read   <= '1';
235
    wait until clk = '0';
236
    temp_read   <= '0';
237
    wait until clk = '0';
238
    --wait for all reads to take place
239
    wait until ow_busy = '0';
240
    wait until clk = '0';
241
    --now wait until the conversions are actually complete
242
    wait;
243
  end process p_control;
244
 
245
  --p_check - checks the output of the DUT and alerts if it is incorrect
246
  p_check : process
247
    variable exptemp : signed(15 downto 0);
248
    variable expidx : unsigned(4 downto 0);
249
  begin
250
    --check data from sensor 1
251
    wait until tempstb = '1';
252
    exptemp := to_signed(-24,16); --  -1.5C
253
    expidx := to_unsigned(0,5);
254
    check_temp(temp,tempidx,exptemp,expidx);
255
    --check data from sensor 2
256
    wait until tempstb = '1';
257
    exptemp := to_signed(1844,16);  --  +115.25C
258
    expidx := to_unsigned(1,5);
259
    check_temp(temp,tempidx,exptemp,expidx);
260
    --check data from sensor 2
261
    wait until tempstb = '1';
262
    exptemp := to_signed(916,16);  --  +57.25C
263
    expidx := to_unsigned(2,5);
264
    check_temp(temp,tempidx,exptemp,expidx);
265
    wait for 5 us;
266
    assert false report "Test completed" severity note;
267
    wait;
268
  end process;
269
 
270
  p_error : process
271
  begin
272
    wait until ow_err = '1';
273
    assert true report "Bus error reported" severity error;
274
  end process;
275
 
276
  --------------------------------------------
277
  --   Simulated OW bus devices, ds1820    ---
278
  --------------------------------------------
279
  --simulated temperature sensor
280
  u_ds18b20_1 : entity work.ds18b20_sim(sim)
281
  generic map (
282
    timing => "min",
283
    devid => x"00a0458d3ea2be28"
284
    )
285
  port map (
286
    --dio => dio1,
287
    pwrin => '1',
288
    dio => dio,
289
    tempin => -1.54
290
    );
291
 
292
  --simulated temperature sensor
293
  u_ds18b20_2 : entity work.ds18b20_sim(sim)
294
  generic map (
295
    timing => "min",
296
    devid => x"002984456a32bf28"
297
    )
298
  port map (
299
    --dio => dio2,
300
    pwrin => '1',
301
    dio => dio,
302
    tempin => 115.3
303
    );
304
 
305
  --simulated temperature sensor
306
  u_ds18b20_3 : entity work.ds18b20_sim(sim)
307
  generic map (
308
    timing => "min",
309
    devid => x"0083726dab32bf28"
310
    )
311
  port map (
312
    --dio => dio3,
313
    pwrin => '1',
314
    dio => dio,
315
    tempin => 57.25
316
    );
317
 
318
end sim;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.