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: ow_temp.vhd
|
20 |
|
|
-- description: configures each ds1820 device, starts a conversion on each device, or reads the results
|
21 |
|
|
-- of each device. temps are set to 10 bit to shorten conversion times (and who needs 1/16 C resolution
|
22 |
|
|
-- when the accuracy is +-0.5C ). The temperatures are output on a bus with a strobe and a device index.
|
23 |
|
|
-----------------------------
|
24 |
|
|
|
25 |
|
|
library IEEE;
|
26 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
27 |
|
|
use IEEE.numeric_std.all;
|
28 |
|
|
--use work.p_ow_types.all;
|
29 |
|
|
|
30 |
|
|
-------------------------------------------------------------------------------------
|
31 |
|
|
-- Entity declaration
|
32 |
|
|
-------------------------------------------------------------------------------------
|
33 |
|
|
entity ow_temp is
|
34 |
|
|
generic (
|
35 |
|
|
BITS : integer := 12;
|
36 |
|
|
DEFTEMP : std_logic_vector(11 downto 0) := x"7ff";
|
37 |
|
|
CRC : boolean := false
|
38 |
|
|
);
|
39 |
|
|
port (
|
40 |
|
|
--global signals
|
41 |
|
|
clk : in std_logic;
|
42 |
|
|
srst : in std_logic;
|
43 |
|
|
busyin : in std_logic; --busy signal from either ow_byte or ow_bit
|
44 |
|
|
--signals to upper layer hierarchy
|
45 |
|
|
init : in std_logic; --init a device for temp reading
|
46 |
|
|
conv : in std_logic; --send convert command to the device
|
47 |
|
|
read : in std_logic; --read a device
|
48 |
|
|
temp : out signed(15 downto 0); --current device temp
|
49 |
|
|
tempidx : out unsigned(4 downto 0); --current device index
|
50 |
|
|
tempstb : out std_logic; --stobe indicating a temperature output
|
51 |
|
|
busy : out std_logic; --busy indication to higher level modules
|
52 |
|
|
error : out std_logic; --indicates a problem on the bus
|
53 |
|
|
--signals to lower layer hierarchy (ow_bit and ow_byte)
|
54 |
|
|
zzbit : out std_logic; --reset strobe to ow_bit interface
|
55 |
|
|
wrbit : out std_logic; --write strobe to ow_bit interface
|
56 |
|
|
ibit : in std_logic; --the data read from the ow_bit interface
|
57 |
|
|
obit : out std_logic; --the data written to the ow_bit interface
|
58 |
|
|
rdbyte : out std_logic; --read strobe to ow_byte interface
|
59 |
|
|
wrbyte : out std_logic; --write strobe to ow_byte interface
|
60 |
|
|
obyte : out std_logic_vector(7 downto 0); --data to write to the ow_byte
|
61 |
|
|
ibyte : in std_logic_vector(7 downto 0); --data read from the ow_byte
|
62 |
|
|
--interface to id ram
|
63 |
|
|
id_num : out std_logic_vector(4 downto 0); --index of the id to read or write
|
64 |
|
|
id_bit : out std_logic_vector(5 downto 0); --index of the bit to read or write
|
65 |
|
|
id_rbit : in std_logic --bit value of the currently indexed bit of the current rom
|
66 |
|
|
);
|
67 |
|
|
end ow_temp;
|
68 |
|
|
|
69 |
|
|
-------------------------------------------------------------------------------------
|
70 |
|
|
-- Architecture declaration
|
71 |
|
|
-------------------------------------------------------------------------------------
|
72 |
|
|
architecture rtl of ow_temp is
|
73 |
|
|
|
74 |
|
|
type t_state_type is (T_IDLE, T_GETTYPE, T_TXID, T_RST, T_TXTOUT, T_TXTOUT2, T_ERROR,
|
75 |
|
|
T_INIT, T_INIT1, T_INIT2, T_INIT3, T_INIT4,
|
76 |
|
|
T_CONV, T_CONV1, T_CONV2, T_CONV3, T_CONV4,
|
77 |
|
|
T_READ, T_READ1, T_READ2, T_READ3, T_READ4, T_READ5, T_READ6, T_READ7
|
78 |
|
|
);
|
79 |
|
|
signal t_state : t_state_type := T_IDLE;
|
80 |
|
|
signal t_next : t_state_type := T_IDLE;
|
81 |
|
|
|
82 |
|
|
type slv8ary is array (integer range <>) of std_logic_vector(7 downto 0);
|
83 |
|
|
|
84 |
|
|
constant ROMSTR : integer := 1;
|
85 |
|
|
constant ROMTXT : std_logic_vector( 7 downto 0) := x"55";
|
86 |
|
|
constant ROMTXTLEN : integer := 1;
|
87 |
|
|
constant INITSTR : integer := 2;
|
88 |
|
|
constant INITTXT : slv8ary(1 to 4) := ( x"4e",x"7f",x"80",x"3f");
|
89 |
|
|
constant INITTXTLEN : integer := INITTXT'length;
|
90 |
|
|
constant CONVSTR : integer := 3;
|
91 |
|
|
constant CONVTXT : std_logic_vector( 7 downto 0) := x"44";
|
92 |
|
|
constant CONVTXTLEN : integer := 1;
|
93 |
|
|
constant READSTR : integer := 4;
|
94 |
|
|
constant READTXT : std_logic_vector( 7 downto 0) := x"be";
|
95 |
|
|
constant READTXTLEN : integer := 1; --READTXT'length;
|
96 |
|
|
constant LASTSTR : integer := READSTR;
|
97 |
|
|
|
98 |
|
|
constant DS1822 : std_logic_vector(7 downto 0) := x"28";
|
99 |
|
|
|
100 |
|
|
signal txtlen : integer range 1 to 4;
|
101 |
|
|
signal txtstr : integer range 1 to LASTSTR;
|
102 |
|
|
signal txtptr : integer range 1 to 4;
|
103 |
|
|
signal txbyte : std_logic_vector(7 downto 0);
|
104 |
|
|
|
105 |
|
|
signal ididx : integer range 0 to 31; --idx of the current device
|
106 |
|
|
signal t_err : std_logic := '0';
|
107 |
|
|
signal irdbyte : std_logic := '0';
|
108 |
|
|
signal iwrbyte : std_logic := '0';
|
109 |
|
|
signal izzbit : std_logic := '0';
|
110 |
|
|
signal iwrbit : std_logic := '0';
|
111 |
|
|
--signal iobit : std_logic := '0';
|
112 |
|
|
|
113 |
|
|
signal readbits : integer range 1 to 10;
|
114 |
|
|
signal shift : std_logic_vector(15 downto 0);
|
115 |
|
|
signal bytecnt : integer range 0 to 9;
|
116 |
|
|
signal bitcnt : integer range 0 to 63;
|
117 |
|
|
signal curid : std_logic_vector(71 downto 0);
|
118 |
|
|
|
119 |
|
|
signal stall : std_logic := '0'; -- this signal is used to stall the state machine one clock after each action
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
--attribute mark_debug : string;
|
123 |
|
|
--attribute mark_debug of pulse_state : signal is "true";
|
124 |
|
|
--attribute mark_debug of timer : signal is "true";
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
begin
|
128 |
|
|
------------------------------------------------------
|
129 |
|
|
-- TX MUX - mux between output commands ---
|
130 |
|
|
------------------------------------------------------
|
131 |
|
|
--select the byte to transmit from the text strings or the rom value
|
132 |
|
|
txbyte <= CONVTXT when txtstr = CONVSTR else READTXT when txtstr = READSTR
|
133 |
|
|
else ROMTXT when txtstr = ROMSTR else INITTXT(txtptr);
|
134 |
|
|
txtlen <= CONVTXTLEN when txtstr = CONVSTR else READTXTLEN when txtstr = READSTR
|
135 |
|
|
else ROMTXTLEN when txtstr = ROMSTR else INITTXTLEN;
|
136 |
|
|
|
137 |
|
|
------------------------------------------------------
|
138 |
|
|
-- P_TEMP - INIT, CONV, READ temp sensors ---
|
139 |
|
|
------------------------------------------------------
|
140 |
|
|
--p_temp - sends rom match command followed by init command, convert command or
|
141 |
|
|
-- read command to the targeted temp sensor and reads data back (if a read command)
|
142 |
|
|
p_temp : process (clk)
|
143 |
|
|
begin
|
144 |
|
|
if rising_edge(clk) then
|
145 |
|
|
if srst = '1' then
|
146 |
|
|
tempstb <= '0';
|
147 |
|
|
t_state <= T_IDLE;
|
148 |
|
|
t_err <= '0';
|
149 |
|
|
izzbit <= '0';
|
150 |
|
|
iwrbit <= '0';
|
151 |
|
|
irdbyte <= '0';
|
152 |
|
|
iwrbyte <= '0';
|
153 |
|
|
ididx <= 0;
|
154 |
|
|
bytecnt <= 0;
|
155 |
|
|
bitcnt <= 0;
|
156 |
|
|
elsif stall = '1' then
|
157 |
|
|
tempstb <= '0';
|
158 |
|
|
iwrbit <= '0';
|
159 |
|
|
izzbit <= '0';
|
160 |
|
|
iwrbyte <= '0';
|
161 |
|
|
irdbyte <= '0';
|
162 |
|
|
stall <= '0';
|
163 |
|
|
else
|
164 |
|
|
--allow 1 extra clock for ram read, temperature conversions
|
165 |
|
|
-- but not when idle or we will miss the triggers
|
166 |
|
|
if t_state /= T_IDLE then
|
167 |
|
|
stall <= '1';
|
168 |
|
|
end if;
|
169 |
|
|
case t_state is
|
170 |
|
|
when T_IDLE =>
|
171 |
|
|
tempstb <= '0';
|
172 |
|
|
obyte <= x"00";
|
173 |
|
|
--shift <= x"000000000000000000";
|
174 |
|
|
shift <= x"0000";
|
175 |
|
|
izzbit <= '0';
|
176 |
|
|
irdbyte <= '0';
|
177 |
|
|
iwrbyte <= '0';
|
178 |
|
|
izzbit <= '0';
|
179 |
|
|
ididx <= 0;
|
180 |
|
|
--shift <= x"00" & ids(ididx);
|
181 |
|
|
--idtype <= x"00";
|
182 |
|
|
bytecnt <= 0;
|
183 |
|
|
bitcnt <= 0;
|
184 |
|
|
if init = '1' then
|
185 |
|
|
t_state <= T_GETTYPE;
|
186 |
|
|
t_next <= T_INIT;
|
187 |
|
|
t_err <= '0';
|
188 |
|
|
elsif conv = '1' then
|
189 |
|
|
t_state <= T_GETTYPE;
|
190 |
|
|
t_next <= T_CONV;
|
191 |
|
|
t_err <= '0';
|
192 |
|
|
elsif read = '1' then
|
193 |
|
|
t_state <= T_GETTYPE;
|
194 |
|
|
t_next <= T_READ;
|
195 |
|
|
t_err <= '0';
|
196 |
|
|
end if;
|
197 |
|
|
when T_GETTYPE =>
|
198 |
|
|
--get the device type from the idrom
|
199 |
|
|
shift(7 downto 0) <= id_rbit & shift(7 downto 1);
|
200 |
|
|
if bitcnt = 7 then
|
201 |
|
|
bitcnt <= 0;
|
202 |
|
|
t_state <= t_next;
|
203 |
|
|
else
|
204 |
|
|
bitcnt <= bitcnt + 1;
|
205 |
|
|
end if;
|
206 |
|
|
when T_TXID =>
|
207 |
|
|
if busyin = '0' then
|
208 |
|
|
iwrbit <= '1';
|
209 |
|
|
if bitcnt = 63 then
|
210 |
|
|
t_state <= t_next;
|
211 |
|
|
bitcnt <= 0;
|
212 |
|
|
else
|
213 |
|
|
bitcnt <= bitcnt + 1;
|
214 |
|
|
end if;
|
215 |
|
|
end if;
|
216 |
|
|
when T_INIT =>
|
217 |
|
|
if busyin = '0' then
|
218 |
|
|
if shift(7 downto 0) = DS1822 then
|
219 |
|
|
t_state <= T_RST;
|
220 |
|
|
t_next <= T_INIT1;
|
221 |
|
|
izzbit <= '1';
|
222 |
|
|
else
|
223 |
|
|
t_state <= T_INIT4;
|
224 |
|
|
end if;
|
225 |
|
|
end if;
|
226 |
|
|
when T_INIT1 =>
|
227 |
|
|
txtptr <= 1;
|
228 |
|
|
txtstr <= ROMSTR;
|
229 |
|
|
t_state <= T_TXTOUT;
|
230 |
|
|
t_next <= T_INIT2;
|
231 |
|
|
when T_INIT2 =>
|
232 |
|
|
t_state <= T_TXID;
|
233 |
|
|
t_next <= T_INIT3;
|
234 |
|
|
when T_INIT3 =>
|
235 |
|
|
txtptr <= 1;
|
236 |
|
|
txtstr <= INITSTR;
|
237 |
|
|
t_state <= T_TXTOUT;
|
238 |
|
|
t_next <= T_INIT4;
|
239 |
|
|
when T_INIT4 =>
|
240 |
|
|
if ididx = 31 then
|
241 |
|
|
t_state <= T_IDLE;
|
242 |
|
|
else
|
243 |
|
|
ididx <= ididx + 1;
|
244 |
|
|
t_state <= T_GETTYPE;
|
245 |
|
|
t_next <= T_INIT;
|
246 |
|
|
end if;
|
247 |
|
|
when T_CONV =>
|
248 |
|
|
--this state cycles through all devices and initiates a conversion on all ds1822s
|
249 |
|
|
if busyin = '0' then
|
250 |
|
|
if shift(7 downto 0) = DS1822 then
|
251 |
|
|
t_state <= T_RST;
|
252 |
|
|
t_next <= T_CONV1;
|
253 |
|
|
izzbit <= '1';
|
254 |
|
|
else
|
255 |
|
|
t_state <= T_CONV4;
|
256 |
|
|
end if;
|
257 |
|
|
end if;
|
258 |
|
|
when T_CONV1 =>
|
259 |
|
|
txtptr <= 1;
|
260 |
|
|
txtstr <= ROMSTR;
|
261 |
|
|
t_state <= T_TXTOUT;
|
262 |
|
|
t_next <= T_CONV2;
|
263 |
|
|
when T_CONV2 =>
|
264 |
|
|
t_state <= T_TXID;
|
265 |
|
|
t_next <= T_CONV3;
|
266 |
|
|
when T_CONV3 =>
|
267 |
|
|
txtptr <= 1;
|
268 |
|
|
txtstr <= CONVSTR;
|
269 |
|
|
t_state <= T_TXTOUT;
|
270 |
|
|
t_next <= T_CONV4;
|
271 |
|
|
when T_CONV4 =>
|
272 |
|
|
if ididx = 31 then
|
273 |
|
|
t_state <= T_IDLE;
|
274 |
|
|
else
|
275 |
|
|
ididx <= ididx + 1;
|
276 |
|
|
t_state <= T_GETTYPE;
|
277 |
|
|
t_next <= T_CONV;
|
278 |
|
|
end if;
|
279 |
|
|
when T_READ =>
|
280 |
|
|
--this state cycles through all devices and reads all ds18B20s
|
281 |
|
|
if busyin = '0' then
|
282 |
|
|
if shift(7 downto 0) = DS1822 then
|
283 |
|
|
t_state <= T_RST;
|
284 |
|
|
t_next <= T_READ1;
|
285 |
|
|
izzbit <= '1';
|
286 |
|
|
else
|
287 |
|
|
t_state <= T_READ7;
|
288 |
|
|
end if;
|
289 |
|
|
end if;
|
290 |
|
|
when T_READ1 =>
|
291 |
|
|
txtptr <= 1;
|
292 |
|
|
txtstr <= ROMSTR;
|
293 |
|
|
t_state <= T_TXTOUT;
|
294 |
|
|
t_next <= T_READ2;
|
295 |
|
|
when T_READ2 =>
|
296 |
|
|
t_state <= T_TXID;
|
297 |
|
|
t_next <= T_READ3;
|
298 |
|
|
when T_READ3 =>
|
299 |
|
|
txtptr <= 1;
|
300 |
|
|
txtstr <= READSTR;
|
301 |
|
|
t_state <= T_TXTOUT;
|
302 |
|
|
t_next <= T_READ4;
|
303 |
|
|
bytecnt <= 0;
|
304 |
|
|
when T_READ4 =>
|
305 |
|
|
if busyin = '0' then
|
306 |
|
|
irdbyte <= '1';
|
307 |
|
|
bytecnt <= 0;
|
308 |
|
|
t_state <= T_READ5;
|
309 |
|
|
end if;
|
310 |
|
|
when T_READ5 =>
|
311 |
|
|
--this state cycles through the 9 bytes to read
|
312 |
|
|
if busyin = '0' then
|
313 |
|
|
--if CRC = true then
|
314 |
|
|
-- shift <= ibyte & shift(71 downto 8);
|
315 |
|
|
--else
|
316 |
|
|
-- shift <= x"00000000000000" & ibyte & shift(15 downto 8);
|
317 |
|
|
--end if;
|
318 |
|
|
shift <= ibyte & shift(15 downto 8);
|
319 |
|
|
if (bytecnt = 1 and CRC = false) or (bytecnt = 9 and CRC = true) then
|
320 |
|
|
t_state <= T_READ6;
|
321 |
|
|
else
|
322 |
|
|
bytecnt <= bytecnt + 1;
|
323 |
|
|
irdbyte <= '1';
|
324 |
|
|
end if;
|
325 |
|
|
end if;
|
326 |
|
|
when T_READ6 =>
|
327 |
|
|
tempstb <= '1';
|
328 |
|
|
t_state <= T_READ7;
|
329 |
|
|
when T_READ7 =>
|
330 |
|
|
tempstb <= '0';
|
331 |
|
|
if ididx = 31 then
|
332 |
|
|
t_state <= T_IDLE;
|
333 |
|
|
else
|
334 |
|
|
ididx <= ididx + 1;
|
335 |
|
|
t_state <= T_GETTYPE;
|
336 |
|
|
t_next <= T_READ;
|
337 |
|
|
end if;
|
338 |
|
|
when T_RST =>
|
339 |
|
|
if busyin = '0' then
|
340 |
|
|
if ibit = '1' then
|
341 |
|
|
t_state <= T_ERROR;
|
342 |
|
|
else
|
343 |
|
|
t_state <= t_next;
|
344 |
|
|
end if;
|
345 |
|
|
end if;
|
346 |
|
|
when T_TXTOUT =>
|
347 |
|
|
if busyin = '0' then
|
348 |
|
|
--if txtstr = ROMSTR and txtptr > 1 then
|
349 |
|
|
-- obyte <= shift(7 downto 0);
|
350 |
|
|
-- shift <= shift(7 downto 0) & shift(71 downto 8);
|
351 |
|
|
--else
|
352 |
|
|
obyte <= txbyte;
|
353 |
|
|
--end if;
|
354 |
|
|
iwrbyte <= '1';
|
355 |
|
|
t_state <= T_TXTOUT2;
|
356 |
|
|
end if;
|
357 |
|
|
when T_TXTOUT2 =>
|
358 |
|
|
iwrbyte <= '0';
|
359 |
|
|
if txtptr = txtlen then
|
360 |
|
|
t_state <= t_next;
|
361 |
|
|
txtptr <= 1;
|
362 |
|
|
else
|
363 |
|
|
txtptr <= txtptr + 1;
|
364 |
|
|
t_state <= T_TXTOUT;
|
365 |
|
|
end if;
|
366 |
|
|
when T_ERROR =>
|
367 |
|
|
t_err <= '1';
|
368 |
|
|
t_state <= T_IDLE;
|
369 |
|
|
end case;
|
370 |
|
|
end if;
|
371 |
|
|
end if;
|
372 |
|
|
end process p_temp;
|
373 |
|
|
|
374 |
|
|
------------------------------------------------------
|
375 |
|
|
-- External Signals ---
|
376 |
|
|
------------------------------------------------------
|
377 |
|
|
|
378 |
|
|
temp <= signed(shift(15 downto 0));
|
379 |
|
|
tempidx <= to_unsigned(ididx,5);
|
380 |
|
|
|
381 |
|
|
busy <= '0' when t_state = T_IDLE and conv = '0' and init = '0' and read = '0' else '1';
|
382 |
|
|
wrbyte <= iwrbyte;
|
383 |
|
|
rdbyte <= irdbyte;
|
384 |
|
|
zzbit <= izzbit;
|
385 |
|
|
wrbit <= iwrbit;
|
386 |
|
|
obit <= id_rbit;
|
387 |
|
|
id_num <= std_logic_vector(to_unsigned(ididx,5));
|
388 |
|
|
id_bit <= std_logic_vector(to_unsigned(bitcnt,6));
|
389 |
|
|
|
390 |
|
|
error <= t_err;
|
391 |
|
|
|
392 |
|
|
end rtl;
|