Mon, 19 Feb 2024 15:07:28 +0100
Fix current year display
0 | 1 | /* |
2 | * MIT License | |
3 | * | |
4 | * Copyright (c) 2017 David Antliff | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all | |
14 | * copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | /** | |
26 | * @file ds18b20.c | |
27 | * | |
28 | * Resolution is cached in the DS18B20_Info object to avoid querying the hardware | |
29 | * every time a temperature conversion is required. However this can result in the | |
30 | * cached value becoming inconsistent with the hardware value, so care must be taken. | |
31 | * | |
32 | */ | |
33 | ||
34 | #include <stddef.h> | |
35 | #include <string.h> | |
36 | #include <stdlib.h> | |
37 | #include <stdbool.h> | |
38 | #include <stdint.h> | |
39 | #include <math.h> | |
40 | ||
41 | #include "freertos/FreeRTOS.h" | |
42 | #include "freertos/task.h" | |
43 | #include "driver/gpio.h" | |
44 | #include "esp_system.h" | |
45 | #include "esp_log.h" | |
46 | ||
47 | #include "ds18b20.h" | |
48 | #include "owb.h" | |
49 | ||
50 | static const char * TAG = "ds18b20"; | |
51 | static const int T_CONV = 750; // maximum conversion time at 12-bit resolution in milliseconds | |
52 | ||
53 | // Function commands | |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
54 | #define DS18B20_FUNCTION_TEMP_CONVERT 0x44 ///< Start temperature conversion |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
55 | #define DS18B20_FUNCTION_SCRATCHPAD_WRITE 0x4E ///< Write scratchpad |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
56 | #define DS18B20_FUNCTION_SCRATCHPAD_READ 0xBE ///< Read scratchpad |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
57 | #define DS18B20_FUNCTION_SCRATCHPAD_COPY 0x48 ///< Copy scratchpad |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
58 | #define DS18B20_FUNCTION_EEPROM_RECALL 0xB8 ///< EEPROM recall |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
59 | #define DS18B20_FUNCTION_POWER_SUPPLY_READ 0xB4 ///< Read powersupply mode |
0 | 60 | |
61 | /// @cond ignore | |
62 | typedef struct | |
63 | { | |
64 | uint8_t temperature[2]; // [0] is LSB, [1] is MSB | |
65 | uint8_t trigger_high; | |
66 | uint8_t trigger_low; | |
67 | uint8_t configuration; | |
68 | uint8_t reserved[3]; | |
69 | uint8_t crc; | |
70 | } Scratchpad; | |
71 | /// @endcond ignore | |
72 | ||
73 | static void _init(DS18B20_Info * ds18b20_info, const OneWireBus * bus) | |
74 | { | |
75 | if (ds18b20_info != NULL) | |
76 | { | |
77 | ds18b20_info->bus = bus; | |
78 | memset(&ds18b20_info->rom_code, 0, sizeof(ds18b20_info->rom_code)); | |
79 | ds18b20_info->use_crc = false; | |
80 | ds18b20_info->resolution = DS18B20_RESOLUTION_INVALID; | |
81 | ds18b20_info->solo = false; // assume multiple devices unless told otherwise | |
82 | ds18b20_info->init = true; | |
83 | } | |
84 | else | |
85 | { | |
86 | ESP_LOGE(TAG, "ds18b20_info is NULL"); | |
87 | } | |
88 | } | |
89 | ||
90 | static bool _is_init(const DS18B20_Info * ds18b20_info) | |
91 | { | |
92 | bool ok = false; | |
93 | if (ds18b20_info != NULL) | |
94 | { | |
95 | if (ds18b20_info->init) | |
96 | { | |
97 | // OK | |
98 | ok = true; | |
99 | } | |
100 | else | |
101 | { | |
102 | ESP_LOGE(TAG, "ds18b20_info is not initialised"); | |
103 | } | |
104 | } | |
105 | else | |
106 | { | |
107 | ESP_LOGE(TAG, "ds18b20_info is NULL"); | |
108 | } | |
109 | return ok; | |
110 | } | |
111 | ||
112 | static bool _address_device(const DS18B20_Info * ds18b20_info) | |
113 | { | |
114 | bool present = false; | |
115 | if (_is_init(ds18b20_info)) | |
116 | { | |
117 | owb_reset(ds18b20_info->bus, &present); | |
118 | if (present) | |
119 | { | |
120 | if (ds18b20_info->solo) | |
121 | { | |
122 | // if there's only one device on the bus, we can skip | |
123 | // sending the ROM code and instruct it directly | |
124 | owb_write_byte(ds18b20_info->bus, OWB_ROM_SKIP); | |
125 | } | |
126 | else | |
127 | { | |
128 | // if there are multiple devices on the bus, a Match ROM command | |
129 | // must be issued to address a specific slave | |
130 | owb_write_byte(ds18b20_info->bus, OWB_ROM_MATCH); | |
131 | owb_write_rom_code(ds18b20_info->bus, ds18b20_info->rom_code); | |
132 | } | |
133 | } | |
134 | else | |
135 | { | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
136 | ESP_LOGE(TAG, "ds18b20 no response"); |
0 | 137 | } |
138 | } | |
139 | return present; | |
140 | } | |
141 | ||
142 | static bool _check_resolution(DS18B20_RESOLUTION resolution) | |
143 | { | |
144 | return (resolution >= DS18B20_RESOLUTION_9_BIT) && (resolution <= DS18B20_RESOLUTION_12_BIT); | |
145 | } | |
146 | ||
147 | static float _wait_for_conversion(DS18B20_RESOLUTION resolution) | |
148 | { | |
149 | float elapsed_time = 0.0f; | |
150 | if (_check_resolution(resolution)) | |
151 | { | |
152 | int divisor = 1 << (DS18B20_RESOLUTION_12_BIT - resolution); | |
153 | float max_conversion_time = (float)T_CONV / (float)divisor; | |
154 | int ticks = ceil(max_conversion_time / portTICK_PERIOD_MS); | |
155 | ||
156 | // wait at least this maximum conversion time | |
157 | vTaskDelay(ticks); | |
158 | ||
159 | // TODO: measure elapsed time more accurately | |
160 | elapsed_time = ticks * portTICK_PERIOD_MS; | |
161 | } | |
162 | return elapsed_time; | |
163 | } | |
164 | ||
165 | static float _decode_temp(uint8_t lsb, uint8_t msb, DS18B20_RESOLUTION resolution) | |
166 | { | |
167 | float result = 0.0f; | |
168 | if (_check_resolution(resolution)) | |
169 | { | |
170 | // masks to remove undefined bits from result | |
171 | static const uint8_t lsb_mask[4] = { ~0x03, ~0x02, ~0x01, ~0x00 }; | |
172 | uint8_t lsb_masked = lsb_mask[resolution - DS18B20_RESOLUTION_9_BIT] & lsb; | |
173 | int16_t raw = (msb << 8) | lsb_masked; | |
174 | result = raw / 16.0f; | |
175 | } | |
176 | else | |
177 | { | |
178 | ESP_LOGE(TAG, "Unsupported resolution %d", resolution); | |
179 | } | |
180 | return result; | |
181 | } | |
182 | ||
183 | static size_t _min(size_t x, size_t y) | |
184 | { | |
185 | return x > y ? y : x; | |
186 | } | |
187 | ||
188 | static Scratchpad _read_scratchpad(const DS18B20_Info * ds18b20_info, size_t count) | |
189 | { | |
190 | count = _min(sizeof(Scratchpad), count); // avoid overflow | |
191 | Scratchpad scratchpad = {0}; | |
192 | ESP_LOGD(TAG, "scratchpad read %d bytes: ", count); | |
193 | if (_address_device(ds18b20_info)) | |
194 | { | |
195 | owb_write_byte(ds18b20_info->bus, DS18B20_FUNCTION_SCRATCHPAD_READ); | |
196 | owb_read_bytes(ds18b20_info->bus, (uint8_t *)&scratchpad, count); | |
197 | //esp_log_buffer_hex(TAG, &scratchpad, count); | |
198 | } | |
199 | return scratchpad; | |
200 | } | |
201 | ||
202 | static bool _write_scratchpad(const DS18B20_Info * ds18b20_info, const Scratchpad * scratchpad, bool verify) | |
203 | { | |
204 | bool result = false; | |
205 | // Only bytes 2, 3 and 4 (trigger and configuration) can be written. | |
206 | // All three bytes MUST be written before the next reset to avoid corruption. | |
207 | if (_is_init(ds18b20_info)) | |
208 | { | |
209 | if (_address_device(ds18b20_info)) | |
210 | { | |
211 | owb_write_byte(ds18b20_info->bus, DS18B20_FUNCTION_SCRATCHPAD_WRITE); | |
212 | owb_write_bytes(ds18b20_info->bus, (uint8_t *)&scratchpad->trigger_high, 3); | |
213 | ESP_LOGD(TAG, "scratchpad write 3 bytes:"); | |
214 | //esp_log_buffer_hex(TAG, &scratchpad->trigger_high, 3); | |
215 | result = true; | |
216 | ||
217 | if (verify) | |
218 | { | |
219 | Scratchpad read = _read_scratchpad(ds18b20_info, offsetof(Scratchpad, configuration) + 1); | |
220 | if (memcmp(&scratchpad->trigger_high, &read.trigger_high, 3) != 0) | |
221 | { | |
222 | ESP_LOGE(TAG, "scratchpad verify failed: " | |
223 | "wrote {0x%02x, 0x%02x, 0x%02x}, " | |
224 | "read {0x%02x, 0x%02x, 0x%02x}", | |
225 | scratchpad->trigger_high, scratchpad->trigger_low, scratchpad->configuration, | |
226 | read.trigger_high, read.trigger_low, read.configuration); | |
227 | result = false; | |
228 | } | |
229 | } | |
230 | } | |
231 | } | |
232 | return result; | |
233 | } | |
234 | ||
235 | ||
236 | // Public API | |
237 | ||
238 | DS18B20_Info * ds18b20_malloc(void) | |
239 | { | |
240 | DS18B20_Info * ds18b20_info = malloc(sizeof(*ds18b20_info)); | |
241 | if (ds18b20_info != NULL) | |
242 | { | |
243 | memset(ds18b20_info, 0, sizeof(*ds18b20_info)); | |
244 | ESP_LOGD(TAG, "malloc %p", ds18b20_info); | |
245 | } | |
246 | else | |
247 | { | |
248 | ESP_LOGE(TAG, "malloc failed"); | |
249 | } | |
250 | ||
251 | return ds18b20_info; | |
252 | } | |
253 | ||
254 | void ds18b20_free(DS18B20_Info ** ds18b20_info) | |
255 | { | |
256 | if (ds18b20_info != NULL && (*ds18b20_info != NULL)) | |
257 | { | |
258 | ESP_LOGD(TAG, "free %p", *ds18b20_info); | |
259 | free(*ds18b20_info); | |
260 | *ds18b20_info = NULL; | |
261 | } | |
262 | } | |
263 | ||
264 | void ds18b20_init(DS18B20_Info * ds18b20_info, const OneWireBus * bus, OneWireBus_ROMCode rom_code) | |
265 | { | |
266 | if (ds18b20_info != NULL) | |
267 | { | |
268 | _init(ds18b20_info, bus); | |
269 | ds18b20_info->rom_code = rom_code; | |
270 | ||
271 | // read current resolution from device as it may not be power-on or factory default | |
272 | ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); | |
273 | } | |
274 | else | |
275 | { | |
276 | ESP_LOGE(TAG, "ds18b20_info is NULL"); | |
277 | } | |
278 | } | |
279 | ||
280 | void ds18b20_init_solo(DS18B20_Info * ds18b20_info, const OneWireBus * bus) | |
281 | { | |
282 | if (ds18b20_info != NULL) | |
283 | { | |
284 | _init(ds18b20_info, bus); | |
285 | ds18b20_info->solo = true; | |
286 | ||
287 | // read current resolution from device as it may not be power-on or factory default | |
288 | ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); | |
289 | } | |
290 | else | |
291 | { | |
292 | ESP_LOGE(TAG, "ds18b20_info is NULL"); | |
293 | } | |
294 | } | |
295 | ||
296 | void ds18b20_use_crc(DS18B20_Info * ds18b20_info, bool use_crc) | |
297 | { | |
298 | if (_is_init(ds18b20_info)) | |
299 | { | |
300 | ds18b20_info->use_crc = use_crc; | |
301 | ESP_LOGD(TAG, "use_crc %d", ds18b20_info->use_crc); | |
302 | } | |
303 | } | |
304 | ||
305 | bool ds18b20_set_resolution(DS18B20_Info * ds18b20_info, DS18B20_RESOLUTION resolution) | |
306 | { | |
307 | bool result = false; | |
308 | if (_is_init(ds18b20_info)) | |
309 | { | |
310 | if (_check_resolution(ds18b20_info->resolution)) | |
311 | { | |
312 | // read scratchpad up to and including configuration register | |
313 | Scratchpad scratchpad = _read_scratchpad(ds18b20_info, | |
314 | offsetof(Scratchpad, configuration) - offsetof(Scratchpad, temperature) + 1); | |
315 | ||
316 | // modify configuration register to set resolution | |
317 | uint8_t value = (((resolution - 1) & 0x03) << 5) | 0x1f; | |
318 | scratchpad.configuration = value; | |
319 | ESP_LOGD(TAG, "configuration value 0x%02x", value); | |
320 | ||
321 | // write bytes 2, 3 and 4 of scratchpad | |
322 | result = _write_scratchpad(ds18b20_info, &scratchpad, /* verify */ true); | |
323 | if (result) | |
324 | { | |
325 | ds18b20_info->resolution = resolution; | |
326 | ESP_LOGD(TAG, "Resolution set to %d bits", (int)resolution); | |
327 | } | |
328 | else | |
329 | { | |
330 | // Resolution change failed - update the info resolution with the value read from configuration | |
331 | ds18b20_info->resolution = ds18b20_read_resolution(ds18b20_info); | |
332 | ESP_LOGW(TAG, "Resolution consistency lost - refreshed from device: %d", ds18b20_info->resolution); | |
333 | } | |
334 | } | |
335 | else | |
336 | { | |
337 | ESP_LOGE(TAG, "Unsupported resolution %d", resolution); | |
338 | } | |
339 | } | |
340 | return result; | |
341 | } | |
342 | ||
343 | DS18B20_RESOLUTION ds18b20_read_resolution(DS18B20_Info * ds18b20_info) | |
344 | { | |
345 | DS18B20_RESOLUTION resolution = DS18B20_RESOLUTION_INVALID; | |
346 | if (_is_init(ds18b20_info)) | |
347 | { | |
348 | // read scratchpad up to and including configuration register | |
349 | Scratchpad scratchpad = _read_scratchpad(ds18b20_info, | |
350 | offsetof(Scratchpad, configuration) - offsetof(Scratchpad, temperature) + 1); | |
351 | ||
352 | resolution = ((scratchpad.configuration >> 5) & 0x03) + DS18B20_RESOLUTION_9_BIT; | |
353 | if (!_check_resolution(resolution)) | |
354 | { | |
355 | ESP_LOGE(TAG, "invalid resolution read from device: 0x%02x", scratchpad.configuration); | |
356 | resolution = DS18B20_RESOLUTION_INVALID; | |
357 | } | |
358 | else | |
359 | { | |
360 | ESP_LOGD(TAG, "Resolution read as %d", resolution); | |
361 | } | |
362 | } | |
363 | return resolution; | |
364 | } | |
365 | ||
366 | bool ds18b20_convert(const DS18B20_Info * ds18b20_info) | |
367 | { | |
368 | bool result = false; | |
369 | if (_is_init(ds18b20_info)) | |
370 | { | |
371 | const OneWireBus * bus = ds18b20_info->bus; | |
372 | if (_address_device(ds18b20_info)) | |
373 | { | |
374 | // initiate a temperature measurement | |
375 | owb_write_byte(bus, DS18B20_FUNCTION_TEMP_CONVERT); | |
376 | result = true; | |
377 | } | |
378 | else | |
379 | { | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
380 | ESP_LOGE(TAG, "ds18b20 no response"); |
0 | 381 | } |
382 | } | |
383 | return result; | |
384 | } | |
385 | ||
386 | void ds18b20_convert_all(const OneWireBus * bus) | |
387 | { | |
388 | bool is_present = false; | |
389 | owb_reset(bus, &is_present); | |
390 | owb_write_byte(bus, OWB_ROM_SKIP); | |
391 | owb_write_byte(bus, DS18B20_FUNCTION_TEMP_CONVERT); | |
392 | } | |
393 | ||
394 | float ds18b20_wait_for_conversion(const DS18B20_Info * ds18b20_info) | |
395 | { | |
396 | float elapsed_time = 0.0f; | |
397 | if (_is_init(ds18b20_info)) | |
398 | { | |
399 | elapsed_time = _wait_for_conversion(ds18b20_info->resolution); | |
400 | } | |
401 | return elapsed_time; | |
402 | } | |
403 | ||
404 | DS18B20_ERROR ds18b20_read_temp(const DS18B20_Info * ds18b20_info, float * value) | |
405 | { | |
406 | DS18B20_ERROR err = DS18B20_ERROR_UNKNOWN; | |
407 | if (_is_init(ds18b20_info)) | |
408 | { | |
409 | const OneWireBus * bus = ds18b20_info->bus; | |
410 | if (_address_device(ds18b20_info)) | |
411 | { | |
412 | // read measurement | |
413 | if (owb_write_byte(bus, DS18B20_FUNCTION_SCRATCHPAD_READ) == OWB_STATUS_OK) | |
414 | { | |
415 | err = DS18B20_OK; | |
416 | ||
417 | uint8_t temp_LSB = 0; | |
418 | uint8_t temp_MSB = 0; | |
419 | if (!ds18b20_info->use_crc) | |
420 | { | |
421 | // Without CRC: | |
422 | owb_read_byte(bus, &temp_LSB); | |
423 | owb_read_byte(bus, &temp_MSB); | |
424 | bool is_present = false; | |
425 | owb_reset(bus, &is_present); // terminate early | |
426 | } | |
427 | else | |
428 | { | |
429 | // with CRC: | |
430 | uint8_t buffer[9] = {0}; | |
431 | owb_read_bytes(bus, buffer, 9); | |
432 | ||
433 | temp_LSB = buffer[0]; | |
434 | temp_MSB = buffer[1]; | |
435 | ||
436 | if (owb_crc8_bytes(0, buffer, 9) != 0) | |
437 | { | |
438 | ESP_LOGE(TAG, "CRC failed"); | |
439 | temp_LSB = 0x00; | |
440 | temp_MSB = 0x80; | |
441 | err = DS18B20_ERROR_CRC; | |
442 | } | |
443 | } | |
444 | ||
445 | if (err == DS18B20_OK) | |
446 | { | |
447 | float temp = _decode_temp(temp_LSB, temp_MSB, ds18b20_info->resolution); | |
448 | ESP_LOGD(TAG, "temp_LSB 0x%02x, temp_MSB 0x%02x, temp %f", temp_LSB, temp_MSB, temp); | |
449 | ||
450 | if (value) | |
451 | { | |
452 | *value = temp; | |
453 | } | |
454 | } | |
455 | } | |
456 | else | |
457 | { | |
458 | ESP_LOGE(TAG, "owb_write_byte failed"); | |
459 | err = DS18B20_ERROR_OWB; | |
460 | } | |
461 | } | |
462 | else | |
463 | { | |
91
255a75322212
Added 7 sgemnt fonts for the web client. Log messages in the components more compact. Added fonts mime-types to the webserver. Switched to stable esp-idf 4.2.1. Upgraded the SD-mmc card API.
Michiel Broek <mbroek@mbse.eu>
parents:
1
diff
changeset
|
464 | ESP_LOGE(TAG, "ds18b20 no response"); |
0 | 465 | err = DS18B20_ERROR_DEVICE; |
466 | } | |
467 | } | |
468 | return err; | |
469 | } | |
470 | ||
471 | DS18B20_ERROR ds18b20_convert_and_read_temp(const DS18B20_Info * ds18b20_info, float * value) | |
472 | { | |
473 | DS18B20_ERROR err = DS18B20_ERROR_UNKNOWN; | |
474 | if (_is_init(ds18b20_info)) | |
475 | { | |
476 | if (ds18b20_convert(ds18b20_info)) | |
477 | { | |
478 | // wait at least maximum conversion time | |
479 | _wait_for_conversion(ds18b20_info->resolution); | |
480 | if (value) | |
481 | { | |
482 | *value = 0.0f; | |
483 | err = ds18b20_read_temp(ds18b20_info, value); | |
484 | } | |
485 | else | |
486 | { | |
487 | err = DS18B20_ERROR_NULL; | |
488 | } | |
489 | } | |
490 | } | |
491 | return err; | |
492 | } | |
493 | ||
494 |