-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathread_temp.v
269 lines (231 loc) · 6.16 KB
/
read_temp.v
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
module read_temp #(
parameter BWD = 32, // Bus width of the data register
parameter CDR_N = 24 * 7 - 1, // Normal mode cycle divider
parameter CDR_O = 24 * 1 - 1 // Overdrive mode cycle divider
) (
input i_clk, // Basic IO
input i_rst,
output o_led_r, // RGB LED's
output o_led_g,
output o_led_b,
input i_owr, // One-wire in and out
output o_owr,
output o_owr_p
);
// State machine registers
reg [7:0] r_state = 'b0;
// LED registeres
reg r_led_r;
reg r_led_g;
reg r_led_b;
// DS18B20
reg [5:0] r_command = 'b0;
reg r_enable = 'b0;
wire w_busy;
wire w_irq;
wire w_detect;
wire [15:0] w_data;
// DS18B20 controller module
ds18b20 #(
.BWD(BWD), // Bus width of the data register
.CDR_N(CDR_N), // Normal mode cycle divider
.CDR_O(CDR_O) // Overdrive mode cycle divider
) my_ds18b20 (
.i_clk(i_clk),
.i_rst(i_rst),
.i_command(r_command),
.i_enable(r_enable),
.o_busy(w_busy),
.o_irq(w_irq),
.o_detect(w_detect),
.o_data(w_data),
// ONE-WIRE
.i_owr(i_owr),
.o_owr(o_owr),
.o_owr_p(o_owr_p)
);
/*
* The temperatures the LEDS change on are
* 25 'C = 'h0191
* 20 'C = 'h0151
* Temperature indication
* T<20'C Blue
* 20'C<T<25'C Green
* T>25'C Red
*/
parameter
p_temp_high = 'h0191,
p_temp_low = 'h0151;
// Command enumeration
parameter
c_idle = 0,
c_reset_detect = 1,
c_skip_rom = 2,
c_convert_t = 3,
c_read_scratch = 4,
c_output_temp = 5,
c_poll_wait = 6;
// State enumeration
parameter
s_start = 0,
// First cycle to trigger the conversion
s_reset_detect = 1,
s_wait_detect = 2,
s_skip_rom = 3,
s_wait_skip_rom = 4,
s_convert = 5,
s_poll_convert = 6,
s_poll_wait_convert = 7,
s_wait_convert = 8,
// Second cycle to read the result
s_2_reset_detect = 10,
s_2_wait_detect = 11,
s_2_skip_rom = 12,
s_2_wait_skip_rom = 13,
s_2_read_scratch = 14,
s_2_wait_scratch = 15,
s_2_get_temp = 16,
s_2_wait_temp = 17,
s_2_proc_temp = 18,
// Generic states
s_error = 99;
// Task send command
task send_command;
input [5:0]a_command;
input [7:0]a_next_state;
begin
// Send command
r_command <= a_command;
r_enable <= 1'b1;
// Next stage
r_state <= a_next_state;
end
endtask
// Wait for the reset/detect to finish
task wait_detect_command;
input [7:0] a_next_state;
input [7:0] a_no_detect_state;
// Check the status register when the irq is passed
begin
if (w_irq == 1'b1) begin
if (w_detect == 1'b1)
r_state <= a_next_state;
else
r_state <= a_no_detect_state;
end
end
endtask
// Task wait for command to complete
task wait_command;
input [7:0] a_next_state;
begin
if (w_irq == 1'b1) begin
r_state <= a_next_state;
end
end
endtask
// Process the temperature and output results using the RGB LED
task proc_temp;
input [7:0] a_next_state;
input [15:0] a_data;
begin
// By default all LEDS off
r_led_r <= 1'b1;
r_led_g <= 1'b1;
r_led_b <= 1'b1;
// Enable the correct LED
if(a_data > p_temp_high)
r_led_r <= 1'b0;
else if (a_data < p_temp_low)
r_led_b <= 1'b0;
else
r_led_g <= 1'b0;
// Measure next value
r_state <= a_next_state;
end
endtask
// Main state machine
always @(posedge i_clk)
begin
// Signals to their default state
r_enable <= 1'b0;
r_command <= c_idle;
// Handle reset
if (i_rst == 1'b1) begin
// NO LEDS during reset
r_led_r <= 1'b1; // LEDS
r_led_g <= 1'b1;
r_led_b <= 1'b1;
// Start over
r_state <= s_start;
end
// State machine
case (r_state)
// Start read
s_start : begin
// Wait for the reset to clear
if (i_rst == 1'b0) begin
r_state <= s_reset_detect;
end
end
// Send reset/detect signal
s_reset_detect :
// Send command
send_command(c_reset_detect, s_wait_detect);
// Wait for detection
s_wait_detect :
wait_detect_command(s_convert, s_error);
// Send Convert temperature command 44h
s_convert :
send_command(c_convert_t, s_wait_convert);
// Wait for the conversion to finish sending
s_wait_convert :
wait_command(s_poll_convert);
// Poll for conversion to have finished
s_poll_convert :
send_command(c_poll_wait, s_poll_wait_convert);
// Wait for the conversion to finish sending
s_poll_wait_convert :
wait_command(s_2_reset_detect);
// Reset the DS18B20 and detect for round 2
s_2_reset_detect :
send_command(c_reset_detect, s_2_wait_detect);
// Wait for the detection
s_2_wait_detect :
wait_detect_command(s_2_read_scratch, s_2_read_scratch);
// Send the read scratch command
s_2_read_scratch :
send_command(c_read_scratch, s_2_wait_scratch);
// Wait for the read scratch to be finished
s_2_wait_scratch :
wait_command(s_2_get_temp);
// Retrieve the 9 bytes to the FPGA
s_2_get_temp :
// nothing yet
send_command(c_output_temp, s_2_wait_temp);
// Wait for all bytes to arrive
s_2_wait_temp :
wait_command(s_2_proc_temp);
// Process the temperature, and show leds
s_2_proc_temp :
proc_temp(s_start, w_data);
// Error
s_error : begin
// Red LED until reset for now
// TODO: Change this
r_led_r <= 1'b0;
r_led_g <= 1'b0;
r_led_b <= 1'b0;
end
// Handles default
default : begin
// Just go to error when non-existing states are set
r_state <= s_error;
end
endcase
end
// assign outputs
assign o_led_r = r_led_r; // LEDS
assign o_led_g = r_led_g;
assign o_led_b = r_led_b;
endmodule