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 |
4 |
skeptonomi |
signal pio : std_logic; --switch of ds2405
|
114 |
2 |
skeptonomi |
|
115 |
|
|
begin
|
116 |
|
|
|
117 |
|
|
-------------------------------------
|
118 |
|
|
-- global timing signals ---
|
119 |
|
|
-------------------------------------
|
120 |
|
|
|
121 |
|
|
p_osc : process
|
122 |
|
|
begin
|
123 |
|
|
clk <= '0';
|
124 |
|
|
wait for halfperiodns;
|
125 |
|
|
clk <= '1';
|
126 |
|
|
wait for halfperiodns;
|
127 |
|
|
end process p_osc;
|
128 |
|
|
|
129 |
|
|
p_rst : process
|
130 |
|
|
begin
|
131 |
|
|
srst <= '1';
|
132 |
|
|
wait for 5 us;
|
133 |
|
|
wait until clk = '1';
|
134 |
|
|
srst <= '0';
|
135 |
|
|
wait;
|
136 |
|
|
end process p_rst;
|
137 |
|
|
|
138 |
|
|
--generate a 1 us strobe for timing
|
139 |
|
|
p_stb1us : process(clk)
|
140 |
|
|
begin
|
141 |
|
|
if rising_edge(clk) then
|
142 |
|
|
if srst = '1' then
|
143 |
|
|
stb1us <= '0';
|
144 |
|
|
stb1us_cntr <= 1;
|
145 |
|
|
else
|
146 |
|
|
if stb1us_cntr = clkfreqmhz then
|
147 |
|
|
stb1us <= '1';
|
148 |
|
|
stb1us_cntr <= 1;
|
149 |
|
|
else
|
150 |
|
|
stb1us <= '0';
|
151 |
|
|
stb1us_cntr <= stb1us_cntr + 1;
|
152 |
|
|
end if;
|
153 |
|
|
end if;
|
154 |
|
|
end if;
|
155 |
|
|
end process p_stb1us;
|
156 |
|
|
|
157 |
|
|
-------------------------------------
|
158 |
|
|
-- DUT ---
|
159 |
|
|
-------------------------------------
|
160 |
|
|
--ow_mstr - DUT (device under test)
|
161 |
|
|
-- one wire master - this performs all the one wire logic
|
162 |
|
|
-- such as configuring and reading the DS1820 devices
|
163 |
|
|
u_dut : entity work.ds1820_mstr(rtl)
|
164 |
|
|
port map(
|
165 |
|
|
--global signals
|
166 |
|
|
clk => clk,
|
167 |
|
|
srst => srst,
|
168 |
|
|
stb1us => stb1us, --1 us strobe
|
169 |
|
|
busy => ow_busy,
|
170 |
|
|
err => ow_err,
|
171 |
|
|
--high level interfaces, lets this module do the heavy lifting. For microprocessor control,
|
172 |
|
|
search_stb => search_init, --searches for devices on bus
|
173 |
|
|
temp_init => temp_init, --initiates temperature read from all devices
|
174 |
|
|
temp_conv => temp_conv, --initiates temperature read from all devices
|
175 |
|
|
temp_read => temp_read, --initiates temperature read from all devices
|
176 |
|
|
temp => temp, --temperatures read from temp devices
|
177 |
|
|
tempidx => tempidx, --temperatures index
|
178 |
|
|
tempstb => tempstb, --temperatures ready strobe
|
179 |
|
|
--one wire bus interface, requires external 5k resistor on bus
|
180 |
|
|
owin => owin, --one wire input
|
181 |
|
|
owout => owout --one wire output
|
182 |
|
|
);
|
183 |
|
|
|
184 |
|
|
-----------------------------------------
|
185 |
|
|
-- one wire bus, open collector ---
|
186 |
|
|
-----------------------------------------
|
187 |
|
|
--handle in/out nature of one wire interface
|
188 |
|
|
dio <= '0' when owout = '0' else 'Z'; --output, only drives low, tristates when not low, external 5k pullup
|
189 |
|
|
owin <= '0' when dio = '0' else '1'; --input, make sure H,Z,1 all map to '1' for simulation
|
190 |
|
|
dio <= 'H'; --simulates the external pullup resistor
|
191 |
|
|
|
192 |
|
|
-----------------------------------------
|
193 |
|
|
-- test bench control and checks ---
|
194 |
|
|
-----------------------------------------
|
195 |
|
|
|
196 |
|
|
--p_control - controls the testing, initiates commands to the DUT
|
197 |
|
|
p_control : process
|
198 |
|
|
begin
|
199 |
|
|
search_init <= '0'; --searches for devices on bus
|
200 |
|
|
temp_init <= '0'; --initiates temperature read from all devices
|
201 |
|
|
temp_conv <= '0'; --initiates temperature read from all devices
|
202 |
|
|
temp_read <= '0'; --initiates temperature read from all devices
|
203 |
|
|
--wait for reset and a bit of time to settle
|
204 |
|
|
wait for 10 us;
|
205 |
|
|
--initiate a search of the one wire devices on the bus
|
206 |
|
|
wait until clk = '0';
|
207 |
|
|
search_init <= '1';
|
208 |
|
|
wait until clk = '0';
|
209 |
|
|
search_init <= '0';
|
210 |
|
|
wait until clk = '0';
|
211 |
|
|
--wait for search to complete
|
212 |
|
|
wait until ow_busy = '0';
|
213 |
|
|
wait until clk = '0';
|
214 |
|
|
--initialize all the temperature sensors on the bus
|
215 |
|
|
wait until clk = '0';
|
216 |
|
|
temp_init <= '1';
|
217 |
|
|
wait until clk = '0';
|
218 |
|
|
temp_init <= '0';
|
219 |
|
|
wait until clk = '0';
|
220 |
|
|
--wait for search to complete
|
221 |
|
|
wait until ow_busy = '0';
|
222 |
|
|
wait for 5 us;
|
223 |
|
|
--start conversions on all the sensors
|
224 |
|
|
wait until clk = '0';
|
225 |
|
|
temp_conv <= '1';
|
226 |
|
|
wait until clk = '0';
|
227 |
|
|
temp_conv <= '0';
|
228 |
|
|
wait until clk = '0';
|
229 |
|
|
--wait for conversion commands to complete
|
230 |
|
|
wait until ow_busy = '0';
|
231 |
|
|
--now wait until the conversions are actually complete
|
232 |
|
|
wait for 190 ms;
|
233 |
|
|
--read the temp from all the sensors
|
234 |
|
|
wait until clk = '0';
|
235 |
|
|
temp_read <= '1';
|
236 |
|
|
wait until clk = '0';
|
237 |
|
|
temp_read <= '0';
|
238 |
|
|
wait until clk = '0';
|
239 |
|
|
--wait for all reads to take place
|
240 |
|
|
wait until ow_busy = '0';
|
241 |
|
|
wait until clk = '0';
|
242 |
|
|
--now wait until the conversions are actually complete
|
243 |
|
|
wait;
|
244 |
|
|
end process p_control;
|
245 |
|
|
|
246 |
|
|
--p_check - checks the output of the DUT and alerts if it is incorrect
|
247 |
|
|
p_check : process
|
248 |
|
|
variable exptemp : signed(15 downto 0);
|
249 |
|
|
variable expidx : unsigned(4 downto 0);
|
250 |
|
|
begin
|
251 |
|
|
--check data from sensor 1
|
252 |
|
|
wait until tempstb = '1';
|
253 |
|
|
exptemp := to_signed(-24,16); -- -1.5C
|
254 |
|
|
expidx := to_unsigned(0,5);
|
255 |
|
|
check_temp(temp,tempidx,exptemp,expidx);
|
256 |
|
|
--check data from sensor 2
|
257 |
|
|
wait until tempstb = '1';
|
258 |
|
|
exptemp := to_signed(1844,16); -- +115.25C
|
259 |
|
|
expidx := to_unsigned(1,5);
|
260 |
|
|
check_temp(temp,tempidx,exptemp,expidx);
|
261 |
|
|
--check data from sensor 2
|
262 |
|
|
wait until tempstb = '1';
|
263 |
|
|
exptemp := to_signed(916,16); -- +57.25C
|
264 |
|
|
expidx := to_unsigned(2,5);
|
265 |
|
|
check_temp(temp,tempidx,exptemp,expidx);
|
266 |
|
|
wait for 5 us;
|
267 |
|
|
assert false report "Test completed" severity note;
|
268 |
|
|
wait;
|
269 |
|
|
end process;
|
270 |
|
|
|
271 |
|
|
p_error : process
|
272 |
|
|
begin
|
273 |
|
|
wait until ow_err = '1';
|
274 |
|
|
assert true report "Bus error reported" severity error;
|
275 |
|
|
end process;
|
276 |
|
|
|
277 |
|
|
--------------------------------------------
|
278 |
|
|
-- Simulated OW bus devices, ds1820 ---
|
279 |
|
|
--------------------------------------------
|
280 |
|
|
--simulated temperature sensor
|
281 |
|
|
u_ds18b20_1 : entity work.ds18b20_sim(sim)
|
282 |
|
|
generic map (
|
283 |
|
|
timing => "min",
|
284 |
|
|
devid => x"00a0458d3ea2be28"
|
285 |
|
|
)
|
286 |
|
|
port map (
|
287 |
|
|
--dio => dio1,
|
288 |
|
|
pwrin => '1',
|
289 |
|
|
dio => dio,
|
290 |
|
|
tempin => -1.54
|
291 |
|
|
);
|
292 |
|
|
|
293 |
|
|
--simulated temperature sensor
|
294 |
|
|
u_ds18b20_2 : entity work.ds18b20_sim(sim)
|
295 |
|
|
generic map (
|
296 |
|
|
timing => "min",
|
297 |
|
|
devid => x"002984456a32bf28"
|
298 |
|
|
)
|
299 |
|
|
port map (
|
300 |
|
|
--dio => dio2,
|
301 |
|
|
pwrin => '1',
|
302 |
|
|
dio => dio,
|
303 |
|
|
tempin => 115.3
|
304 |
|
|
);
|
305 |
|
|
|
306 |
|
|
--simulated temperature sensor
|
307 |
|
|
u_ds18b20_3 : entity work.ds18b20_sim(sim)
|
308 |
|
|
generic map (
|
309 |
|
|
timing => "min",
|
310 |
4 |
skeptonomi |
devid => x"0083726d2b32bf28"
|
311 |
2 |
skeptonomi |
)
|
312 |
|
|
port map (
|
313 |
|
|
--dio => dio3,
|
314 |
|
|
pwrin => '1',
|
315 |
|
|
dio => dio,
|
316 |
|
|
tempin => 57.25
|
317 |
|
|
);
|
318 |
4 |
skeptonomi |
u_ds18b20_4 : entity work.ds18b20_sim(sim)
|
319 |
|
|
generic map (
|
320 |
|
|
timing => "min",
|
321 |
|
|
devid => x"0083726d3b32bf28"
|
322 |
|
|
)
|
323 |
|
|
port map (
|
324 |
|
|
--dio => dio3,
|
325 |
|
|
pwrin => '1',
|
326 |
|
|
dio => dio,
|
327 |
|
|
tempin => 57.25
|
328 |
|
|
);
|
329 |
|
|
|
330 |
|
|
--simulated addressable switch
|
331 |
|
|
u_ds2405 : entity work.ds2405_sim(sim)
|
332 |
|
|
generic map (
|
333 |
|
|
timing => "min",
|
334 |
|
|
devid => x"0034756483522105"
|
335 |
|
|
)
|
336 |
|
|
port map (
|
337 |
|
|
--dio => dio3,
|
338 |
|
|
pio => pio,
|
339 |
|
|
dio => dio
|
340 |
|
|
);
|
341 |
|
|
pio <= 'H';
|
342 |
|
|
|
343 |
2 |
skeptonomi |
end sim;
|