components/esp32-owb/owb.c

changeset 0
b74b0e4902c3
child 91
255a75322212
equal deleted inserted replaced
-1:000000000000 0:b74b0e4902c3
1 /*
2 * MIT License
3 *
4 * Copyright (c) 2017 David Antliff
5 * Copyright (c) 2017 Chris Morgan <chmorgan@gmail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 /**
27 * @file
28 */
29
30 #include <stddef.h>
31 #include <stdbool.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "freertos/FreeRTOS.h"
37 #include "freertos/task.h"
38 #include "esp_log.h"
39 #include "sdkconfig.h"
40 #include "driver/gpio.h"
41
42 #include "owb.h"
43 #include "owb_gpio.h"
44
45 static const char * TAG = "owb";
46
47 static bool _is_init(const OneWireBus * bus)
48 {
49 bool ok = false;
50 if (bus != NULL)
51 {
52 if (bus->driver)
53 {
54 // OK
55 ok = true;
56 }
57 else
58 {
59 ESP_LOGE(TAG, "bus is not initialised");
60 }
61 }
62 else
63 {
64 ESP_LOGE(TAG, "bus is NULL");
65 }
66 return ok;
67 }
68
69 /**
70 * @brief 1-Wire 8-bit CRC lookup.
71 * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate.
72 * @param[in] data Byte to feed into CRC.
73 * @return Resultant CRC value.
74 */
75 static uint8_t _calc_crc(uint8_t crc, uint8_t data)
76 {
77 // https://www.maximintegrated.com/en/app-notes/index.mvp/id/27
78 static const uint8_t table[256] = {
79 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
80 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
81 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
82 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
83 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
84 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
85 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
86 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
87 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
88 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
89 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
90 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
91 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
92 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
93 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
94 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
95 };
96
97 return table[crc ^ data];
98 }
99
100 static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len)
101 {
102 do
103 {
104 crc = _calc_crc(crc, *buffer++);
105 ESP_LOGD(TAG, "crc 0x%02x, len %d", (int)crc, (int)len);
106 }
107 while (--len > 0);
108 return crc;
109 }
110
111 /**
112 * @param[out] is_found true if a device was found, false if not
113 * @return status
114 */
115 static owb_status _search(const OneWireBus * bus, OneWireBus_SearchState * state, bool *is_found)
116 {
117 // Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187
118
119 // initialize for search
120 int id_bit_number = 1;
121 int last_zero = 0;
122 int rom_byte_number = 0;
123 uint8_t id_bit = 0;
124 uint8_t cmp_id_bit = 0;
125 uint8_t rom_byte_mask = 1;
126 uint8_t search_direction = 0;
127 bool search_result = false;
128 uint8_t crc8 = 0;
129 owb_status status;
130
131 // if the last call was not the last one
132 if (!state->last_device_flag)
133 {
134 // 1-Wire reset
135 bool is_present;
136 bus->driver->reset(bus, &is_present);
137 if (!is_present)
138 {
139 // reset the search
140 state->last_discrepancy = 0;
141 state->last_device_flag = false;
142 state->last_family_discrepancy = 0;
143 *is_found = false;
144 return OWB_STATUS_OK;
145 }
146
147 // issue the search command
148 bus->driver->write_bits(bus, OWB_ROM_SEARCH, 8);
149
150 // loop to do the search
151 do
152 {
153 id_bit = cmp_id_bit = 0;
154
155 // read a bit and its complement
156 bus->driver->read_bits(bus, &id_bit, 1);
157 bus->driver->read_bits(bus, &cmp_id_bit, 1);
158
159 // check for no devices on 1-wire (signal level is high in both bit reads)
160 if (id_bit && cmp_id_bit)
161 {
162 break;
163 } else
164 {
165 // all devices coupled have 0 or 1
166 if (id_bit != cmp_id_bit)
167 {
168 search_direction = (id_bit) ? 1 : 0; // bit write value for search
169 } else
170 {
171 // if this discrepancy if before the Last Discrepancy
172 // on a previous next then pick the same as last time
173 if (id_bit_number < state->last_discrepancy)
174 search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0);
175 else
176 // if equal to last pick 1, if not then pick 0
177 search_direction = (id_bit_number == state->last_discrepancy);
178
179 // if 0 was picked then record its position in LastZero
180 if (search_direction == 0)
181 {
182 last_zero = id_bit_number;
183
184 // check for Last discrepancy in family
185 if (last_zero < 9)
186 state->last_family_discrepancy = last_zero;
187 }
188 }
189
190 // set or clear the bit in the ROM byte rom_byte_number
191 // with mask rom_byte_mask
192 if (search_direction == 1)
193 state->rom_code.bytes[rom_byte_number] |= rom_byte_mask;
194 else
195 state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask;
196
197 // serial number search direction write bit
198 bus->driver->write_bits(bus, search_direction, 1);
199
200 // increment the byte counter id_bit_number
201 // and shift the mask rom_byte_mask
202 id_bit_number++;
203 rom_byte_mask <<= 1;
204
205 // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
206 if (rom_byte_mask == 0)
207 {
208 crc8 = owb_crc8_byte(crc8, state->rom_code.bytes[rom_byte_number]); // accumulate the CRC
209 rom_byte_number++;
210 rom_byte_mask = 1;
211 }
212 }
213 }
214 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
215
216 // if the search was successful then
217 if (!((id_bit_number < 65) || (crc8 != 0)))
218 {
219 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
220 state->last_discrepancy = last_zero;
221
222 // check for last device
223 if (state->last_discrepancy == 0)
224 state->last_device_flag = true;
225
226 search_result = true;
227 }
228 }
229
230 // if no device found then reset counters so next 'search' will be like a first
231 if (!search_result || !state->rom_code.bytes[0])
232 {
233 state->last_discrepancy = 0;
234 state->last_device_flag = false;
235 state->last_family_discrepancy = 0;
236 search_result = false;
237 }
238
239 status = OWB_STATUS_OK;
240
241 *is_found = search_result;
242
243 return status;
244 }
245
246 // Public API
247
248 owb_status owb_uninitialize(OneWireBus * bus)
249 {
250 owb_status status;
251
252 if(!_is_init(bus))
253 {
254 status = OWB_STATUS_NOT_INITIALIZED;
255 } else
256 {
257 bus->driver->uninitialize(bus);
258 status = OWB_STATUS_OK;
259 }
260
261 return status;
262 }
263
264 owb_status owb_use_crc(OneWireBus * bus, bool use_crc)
265 {
266 owb_status status;
267
268 if(!bus)
269 {
270 status = OWB_STATUS_PARAMETER_NULL;
271 } else if (!_is_init(bus))
272 {
273 status = OWB_STATUS_NOT_INITIALIZED;
274 } else
275 {
276 bus->use_crc = use_crc;
277 ESP_LOGD(TAG, "use_crc %d", bus->use_crc);
278
279 status = OWB_STATUS_OK;
280 }
281
282 return status;
283 }
284
285 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code)
286 {
287 owb_status status;
288
289 memset(rom_code, 0, sizeof(OneWireBus_ROMCode));
290
291 if(!bus || !rom_code)
292 {
293 status = OWB_STATUS_PARAMETER_NULL;
294 } else if (!_is_init(bus))
295 {
296 status = OWB_STATUS_NOT_INITIALIZED;
297 } else
298 {
299 bool is_present;
300 bus->driver->reset(bus, &is_present);
301 if (is_present)
302 {
303 uint8_t value = OWB_ROM_READ;
304 bus->driver->write_bits(bus, value, 8);
305 owb_read_bytes(bus, rom_code->bytes, sizeof(OneWireBus_ROMCode));
306
307 if (bus->use_crc)
308 {
309 if (owb_crc8_bytes(0, rom_code->bytes, sizeof(OneWireBus_ROMCode)) != 0)
310 {
311 ESP_LOGE(TAG, "CRC failed");
312 memset(rom_code->bytes, 0, sizeof(OneWireBus_ROMCode));
313 status = OWB_STATUS_CRC_FAILED;
314 } else
315 {
316 status = OWB_STATUS_OK;
317 }
318 } else
319 {
320 status = OWB_STATUS_OK;
321 }
322 char rom_code_s[OWB_ROM_CODE_STRING_LENGTH];
323 owb_string_from_rom_code(*rom_code, rom_code_s, sizeof(rom_code_s));
324 ESP_LOGD(TAG, "rom_code %s", rom_code_s);
325 }
326 else
327 {
328 status = OWB_STATUS_DEVICE_NOT_RESPONDING;
329 ESP_LOGE(TAG, "ds18b20 device not responding");
330 }
331 }
332
333 return status;
334 }
335
336 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool* is_present)
337 {
338 owb_status status;
339 bool result = false;
340
341 if (!bus || !is_present)
342 {
343 status = OWB_STATUS_PARAMETER_NULL;
344 }
345 else if (!_is_init(bus))
346 {
347 status = OWB_STATUS_NOT_INITIALIZED;
348 }
349 else
350 {
351 OneWireBus_SearchState state = {
352 .last_discrepancy = 64,
353 .last_device_flag = false,
354 };
355
356 bool is_found = false;
357 while (!state.last_device_flag && !is_found)
358 {
359 _search(bus, &state, &is_found);
360 if (is_found)
361 {
362 result = true;
363 for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i)
364 {
365 result = rom_code.bytes[i] == state.rom_code.bytes[i];
366 ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[i]);
367 }
368 is_found = result;
369 }
370 }
371
372 ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not ");
373 *is_present = result;
374 status = OWB_STATUS_OK;
375 }
376
377 return status;
378 }
379
380 owb_status owb_reset(const OneWireBus * bus, bool* a_device_present)
381 {
382 owb_status status;
383
384 if(!bus || !a_device_present)
385 {
386 status = OWB_STATUS_PARAMETER_NULL;
387 } else if(!_is_init(bus))
388 {
389 status = OWB_STATUS_NOT_INITIALIZED;
390 } else
391 {
392 bus->driver->reset(bus, a_device_present);
393 status = OWB_STATUS_OK;
394 }
395
396 return status;
397 }
398
399 owb_status owb_write_byte(const OneWireBus * bus, uint8_t data)
400 {
401 owb_status status;
402
403 if(!bus)
404 {
405 status = OWB_STATUS_PARAMETER_NULL;
406 } else if (!_is_init(bus))
407 {
408 status = OWB_STATUS_NOT_INITIALIZED;
409 } else
410 {
411 bus->driver->write_bits(bus, data, 8);
412 status = OWB_STATUS_OK;
413 }
414
415 return status;
416 }
417
418 owb_status owb_read_byte(const OneWireBus * bus, uint8_t *out)
419 {
420 owb_status status;
421
422 if(!bus || !out)
423 {
424 status = OWB_STATUS_PARAMETER_NULL;
425 } else if (!_is_init(bus))
426 {
427 status = OWB_STATUS_NOT_INITIALIZED;
428 } else
429 {
430 bus->driver->read_bits(bus, out, 8);
431 status = OWB_STATUS_OK;
432 }
433
434 return status;
435 }
436
437 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len)
438 {
439 owb_status status;
440
441 if(!bus || !buffer)
442 {
443 status = OWB_STATUS_PARAMETER_NULL;
444 } else if (!_is_init(bus))
445 {
446 status = OWB_STATUS_NOT_INITIALIZED;
447 } else
448 {
449 for (int i = 0; i < len; ++i)
450 {
451 uint8_t out;
452 bus->driver->read_bits(bus, &out, 8);
453 buffer[i] = out;
454 }
455
456 status = OWB_STATUS_OK;
457 }
458
459 return status;
460 }
461
462 owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len)
463 {
464 owb_status status;
465
466 if(!bus || !buffer)
467 {
468 status = OWB_STATUS_PARAMETER_NULL;
469 } else if (!_is_init(bus))
470 {
471 status = OWB_STATUS_NOT_INITIALIZED;
472 } else
473 {
474 for (int i = 0; i < len; i++)
475 {
476 bus->driver->write_bits(bus, buffer[i], 8);
477 }
478
479 status = OWB_STATUS_OK;
480 }
481
482 return status;
483 }
484
485 owb_status owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code)
486 {
487 owb_status status;
488
489 if(!bus)
490 {
491 status = OWB_STATUS_PARAMETER_NULL;
492 } else if (!_is_init(bus))
493 {
494 status = OWB_STATUS_NOT_INITIALIZED;
495 } else
496 {
497 owb_write_bytes(bus, (uint8_t*)&rom_code, sizeof(rom_code));
498 status = OWB_STATUS_OK;
499 }
500
501 return status;
502 }
503
504 uint8_t owb_crc8_byte(uint8_t crc, uint8_t data)
505 {
506 return _calc_crc(crc, data);
507 }
508
509 uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len)
510 {
511 return _calc_crc_block(crc, data, len);
512 }
513
514 owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool* found_device)
515 {
516 bool result;
517 owb_status status;
518
519 if(!bus || !state || !found_device)
520 {
521 status = OWB_STATUS_PARAMETER_NULL;
522 } else if (!_is_init(bus))
523 {
524 status = OWB_STATUS_NOT_INITIALIZED;
525 } else
526 {
527 memset(&state->rom_code, 0, sizeof(state->rom_code));
528 state->last_discrepancy = 0;
529 state->last_family_discrepancy = 0;
530 state->last_device_flag = false;
531 _search(bus, state, &result);
532 status = OWB_STATUS_OK;
533
534 *found_device = result;
535 }
536
537 return status;
538 }
539
540 owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool* found_device)
541 {
542 owb_status status;
543 bool result = false;
544
545 if(!bus || !state || !found_device)
546 {
547 status = OWB_STATUS_PARAMETER_NULL;
548 } else if (!_is_init(bus))
549 {
550 status = OWB_STATUS_NOT_INITIALIZED;
551 } else
552 {
553 _search(bus, state, &result);
554 status = OWB_STATUS_OK;
555
556 *found_device = result;
557 }
558
559 return status;
560 }
561
562 char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len)
563 {
564 for (int i = sizeof(rom_code.bytes) - 1; i >= 0; i--)
565 {
566 sprintf(buffer, "%02x", rom_code.bytes[i]);
567 buffer += 2;
568 }
569 return buffer;
570 }

mercurial