|
1 /** |
|
2 * @file task_ds18b20.c |
|
3 * @brief Task that reads the DS18B20 sensors on two one-wire busses. |
|
4 * The task can also be compiled with simulated sensors. |
|
5 */ |
|
6 |
|
7 |
|
8 #include "owb.h" |
|
9 #include "owb_rmt.h" |
|
10 #include "ds18b20.h" |
|
11 #include "config.h" |
|
12 |
|
13 |
|
14 #ifdef CONFIG_TEMP_SENSORS_ONEWIRE |
|
15 #define GPIO_DS18B20_MLT (CONFIG_ONE_WIRE_MLT) |
|
16 #define GPIO_DS18B20_HLT (CONFIG_ONE_WIRE_HLT) |
|
17 #define MAX_DEVICES (8) |
|
18 #define DS18B20_RESOLUTION (DS18B20_RESOLUTION_12_BIT) |
|
19 #define SAMPLE_PERIOD (2000) // milliseconds |
|
20 #endif |
|
21 |
|
22 #ifdef CONFIG_TEMP_SENSORS_SIMULATOR |
|
23 #define SAMPLE_PERIOD (750) // milliseconds |
|
24 |
|
25 float Fake_MLT = 18.90; |
|
26 float Fake_HLT = 18.70; |
|
27 #endif |
|
28 |
|
29 static const char *TAG = "task_ds18b20"; |
|
30 |
|
31 SemaphoreHandle_t xSemaphoreDS18B20 = NULL; |
|
32 DS18B20_State *ds18b20_state; |
|
33 |
|
34 |
|
35 /* |
|
36 * Task to read temperature sensors. |
|
37 */ |
|
38 void task_ds18b20(void *pvParameter) |
|
39 { |
|
40 #ifdef CONFIG_TEMP_SENSORS_ONEWIRE |
|
41 int num_devices = 0; |
|
42 float readings = 0; |
|
43 bool found = false; |
|
44 DS18B20_ERROR errors = 0; |
|
45 TickType_t last_wake_time = xTaskGetTickCount(); |
|
46 |
|
47 ESP_LOGI(TAG, "Starting DS18B20 sensors"); |
|
48 ds18b20_state = malloc(sizeof(DS18B20_State)); |
|
49 ds18b20_state->mlt_valid = ds18b20_state->hlt_valid = false; |
|
50 ds18b20_state->mlt_temperature = ds18b20_state->hlt_temperature = 0.0; |
|
51 ds18b20_state->mlt_error = ds18b20_state->hlt_error = DS18B20_ERR_NOSENSOR; |
|
52 |
|
53 /* |
|
54 * Initialize the MLT and HLT one-wire busses. |
|
55 */ |
|
56 OneWireBus *owb_mlt; |
|
57 OneWireBus *owb_hlt; |
|
58 owb_rmt_driver_info rmt_driver_info_mlt; |
|
59 owb_rmt_driver_info rmt_driver_info_hlt; |
|
60 owb_mlt = owb_rmt_initialize(&rmt_driver_info_mlt, GPIO_DS18B20_MLT, RMT_CHANNEL_1, RMT_CHANNEL_0); |
|
61 owb_hlt = owb_rmt_initialize(&rmt_driver_info_hlt, GPIO_DS18B20_HLT, RMT_CHANNEL_3, RMT_CHANNEL_2); |
|
62 owb_use_crc(owb_mlt, true); // enable CRC check for ROM code |
|
63 owb_use_crc(owb_hlt, true); |
|
64 |
|
65 DS18B20_Info * ds18b20_info = ds18b20_malloc(); |
|
66 |
|
67 /* |
|
68 * Task loop forever. |
|
69 */ |
|
70 while (1) { |
|
71 |
|
72 last_wake_time = xTaskGetTickCount(); |
|
73 |
|
74 num_devices = 0; |
|
75 OneWireBus_SearchState mlt_search_state = {0}; |
|
76 found = false; |
|
77 owb_search_first(owb_mlt, &mlt_search_state, &found); |
|
78 while (found) { |
|
79 ++num_devices; |
|
80 owb_search_next(owb_mlt, &mlt_search_state, &found); |
|
81 } |
|
82 |
|
83 if (num_devices == 1) { |
|
84 ds18b20_init_solo(ds18b20_info, owb_mlt); // only one device on bus |
|
85 ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings |
|
86 ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); // returns true if ok. |
|
87 |
|
88 // Read temperatures more efficiently by starting conversions on all devices at the same time |
|
89 ds18b20_convert_all(owb_mlt); |
|
90 ds18b20_wait_for_conversion(ds18b20_info); |
|
91 |
|
92 errors = ds18b20_read_temp(ds18b20_info, &readings); |
|
93 |
|
94 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
95 if (errors == DS18B20_OK) { |
|
96 ds18b20_state->mlt_error = DS18B20_ERR_NONE; |
|
97 ds18b20_state->mlt_valid = true; |
|
98 ds18b20_state->mlt_temperature = readings; |
|
99 } else { |
|
100 if (errors == DS18B20_ERROR_CRC) |
|
101 ds18b20_state->mlt_error = DS18B20_ERR_CRC; |
|
102 if (errors == DS18B20_ERROR_OWB) |
|
103 ds18b20_state->mlt_error = DS18B20_ERR_READ; |
|
104 if (errors == DS18B20_ERROR_DEVICE) |
|
105 ds18b20_state->mlt_error = DS18B20_ERR_READ; |
|
106 ds18b20_state->mlt_valid = false; |
|
107 ds18b20_state->mlt_temperature = 0.0; |
|
108 } |
|
109 xSemaphoreGive(xSemaphoreDS18B20); |
|
110 } |
|
111 |
|
112 } else { |
|
113 /* |
|
114 * Zero or more then one device, this is an error. |
|
115 */ |
|
116 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
117 if (num_devices == 0) |
|
118 ds18b20_state->mlt_error = DS18B20_ERR_NOSENSOR; |
|
119 else |
|
120 ds18b20_state->mlt_error = DS18B20_ERR_TOOMANY; |
|
121 ds18b20_state->mlt_valid = false; |
|
122 ds18b20_state->mlt_temperature = 0.0; |
|
123 xSemaphoreGive(xSemaphoreDS18B20); |
|
124 } |
|
125 } // if num_devices == 1 |
|
126 |
|
127 num_devices = 0; |
|
128 OneWireBus_SearchState hlt_search_state = {0}; |
|
129 found = false; |
|
130 owb_search_first(owb_hlt, &hlt_search_state, &found); |
|
131 while (found) { |
|
132 ++num_devices; |
|
133 owb_search_next(owb_hlt, &hlt_search_state, &found); |
|
134 } |
|
135 |
|
136 if (num_devices == 1) { |
|
137 ds18b20_init_solo(ds18b20_info, owb_hlt); // only one device on bus |
|
138 ds18b20_use_crc(ds18b20_info, true); // enable CRC check for temperature readings |
|
139 ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION); |
|
140 |
|
141 // Read temperatures more efficiently by starting conversions on all devices at the same time |
|
142 ds18b20_convert_all(owb_hlt); |
|
143 ds18b20_wait_for_conversion(ds18b20_info); |
|
144 |
|
145 errors = ds18b20_read_temp(ds18b20_info, &readings); |
|
146 |
|
147 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
148 if (errors == DS18B20_OK) { |
|
149 ds18b20_state->hlt_error = DS18B20_ERR_NONE; |
|
150 ds18b20_state->hlt_valid = true; |
|
151 ds18b20_state->hlt_temperature = readings; |
|
152 } else { |
|
153 if (errors == DS18B20_ERROR_CRC) |
|
154 ds18b20_state->hlt_error = DS18B20_ERR_CRC; |
|
155 if (errors == DS18B20_ERROR_OWB) |
|
156 ds18b20_state->hlt_error = DS18B20_ERR_READ; |
|
157 if (errors == DS18B20_ERROR_DEVICE) |
|
158 ds18b20_state->hlt_error = DS18B20_ERR_READ; |
|
159 ds18b20_state->hlt_valid = false; |
|
160 ds18b20_state->hlt_temperature = 0.0; |
|
161 } |
|
162 xSemaphoreGive(xSemaphoreDS18B20); |
|
163 } |
|
164 } else { |
|
165 /* |
|
166 * Zero or more then one device, this is an error. |
|
167 */ |
|
168 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
169 if (num_devices == 0) |
|
170 ds18b20_state->hlt_error = DS18B20_ERR_NOSENSOR; |
|
171 else |
|
172 ds18b20_state->hlt_error = DS18B20_ERR_TOOMANY; |
|
173 ds18b20_state->hlt_valid = false; |
|
174 ds18b20_state->hlt_temperature = 0.0; |
|
175 xSemaphoreGive(xSemaphoreDS18B20); |
|
176 } |
|
177 } // if num_devices == 1 |
|
178 |
|
179 #if 0 |
|
180 printf("MLT %.3f %d err %s, HLT %.3f, %d err %s\n", |
|
181 ds18b20_state->mlt_temperature, ds18b20_state->mlt_error, (ds18b20_state->mlt_valid) ? "valid":" N/A ", |
|
182 ds18b20_state->hlt_temperature, ds18b20_state->hlt_error, (ds18b20_state->hlt_valid) ? "valid":" N/A "); |
|
183 #endif |
|
184 vTaskDelayUntil(&last_wake_time, SAMPLE_PERIOD / portTICK_PERIOD_MS); |
|
185 } |
|
186 #endif |
|
187 |
|
188 #ifdef CONFIG_TEMP_SENSORS_SIMULATOR |
|
189 |
|
190 float Plate_MLT = 18.90; |
|
191 extern int MLT_pin, HLT_pin; |
|
192 |
|
193 ESP_LOGI(TAG, "Starting Fake sensors"); |
|
194 ds18b20_state = malloc(sizeof(DS18B20_State)); |
|
195 ds18b20_state->mlt_valid = ds18b20_state->hlt_valid = true; |
|
196 ds18b20_state->mlt_temperature = 18.90; |
|
197 ds18b20_state->hlt_temperature = 18.70; |
|
198 ds18b20_state->mlt_error = ds18b20_state->hlt_error = DS18B20_ERR_NONE; |
|
199 |
|
200 /* |
|
201 * Task loop forever. Update the temperatures each 750 mSeconds. |
|
202 */ |
|
203 while (1) { |
|
204 |
|
205 /* |
|
206 * Make this fake heater a bit more real by using a simulated heatplate. |
|
207 * We heatup that plate and then transfer the heat to the water. |
|
208 * That way we get a nice overshoot like in real life. |
|
209 */ |
|
210 if (MLT_pin) { |
|
211 if (Plate_MLT < 250.0) |
|
212 Plate_MLT += SAMPLE_PERIOD * 0.001; // Simulate plate upto 250 degrees |
|
213 } else { |
|
214 if (Plate_MLT > Fake_MLT) |
|
215 Plate_MLT -= SAMPLE_PERIOD * 0.00002 * (Plate_MLT - Fake_MLT); |
|
216 } |
|
217 // If plate is hotter then the water with a offset so that cooling later works. |
|
218 if (Plate_MLT > (Fake_MLT + 5.0)) { |
|
219 if (Fake_MLT < 100.05) |
|
220 Fake_MLT += SAMPLE_PERIOD * 0.000001 * (Plate_MLT - Fake_MLT); |
|
221 } |
|
222 // Allways loose heat to the air |
|
223 if (Fake_MLT > 16.0) { |
|
224 Fake_MLT -= SAMPLE_PERIOD * 0.00000010 * (Fake_MLT - 16.0); |
|
225 } |
|
226 |
|
227 if ((equipment.SSR2 == SSR2_HLT_SHARE) || (equipment.SSR2 == SSR2_HLT_IND)) { |
|
228 /* |
|
229 * There is a HLT function configured. |
|
230 */ |
|
231 if (HLT_pin) { |
|
232 if (Fake_HLT < 100.05) |
|
233 Fake_HLT += SAMPLE_PERIOD * 0.000055; |
|
234 } else { |
|
235 if (Fake_HLT > 16.0) |
|
236 Fake_HLT -= SAMPLE_PERIOD * 0.00000006 * (Fake_HLT - 16.0); |
|
237 } |
|
238 } |
|
239 |
|
240 if (xSemaphoreTake(xSemaphoreDS18B20, 25) == pdTRUE) { |
|
241 ds18b20_state->mlt_temperature = ((int)(Fake_MLT * 16)) / 16.0; |
|
242 ds18b20_state->hlt_temperature = ((int)(Fake_HLT * 16)) / 16.0; |
|
243 xSemaphoreGive(xSemaphoreDS18B20); |
|
244 } |
|
245 |
|
246 vTaskDelay(SAMPLE_PERIOD / portTICK_PERIOD_MS); |
|
247 } |
|
248 |
|
249 #endif |
|
250 } |
|
251 |
|
252 |