Mon, 24 Jun 2024 17:05:20 +0200
Start migration to IDF 5.1
0 | 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" | |
129 | 41 | #include "rom/gpio.h" // for gpio_pad_select_gpio() |
0 | 42 | |
43 | #include "owb.h" | |
44 | #include "owb_gpio.h" | |
45 | ||
46 | static const char * TAG = "owb"; | |
47 | ||
48 | static bool _is_init(const OneWireBus * bus) | |
49 | { | |
50 | bool ok = false; | |
51 | if (bus != NULL) | |
52 | { | |
53 | if (bus->driver) | |
54 | { | |
55 | // OK | |
56 | ok = true; | |
57 | } | |
58 | else | |
59 | { | |
60 | ESP_LOGE(TAG, "bus is not initialised"); | |
61 | } | |
62 | } | |
63 | else | |
64 | { | |
65 | ESP_LOGE(TAG, "bus is NULL"); | |
66 | } | |
67 | return ok; | |
68 | } | |
69 | ||
70 | /** | |
71 | * @brief 1-Wire 8-bit CRC lookup. | |
72 | * @param[in] crc Starting CRC value. Pass in prior CRC to accumulate. | |
73 | * @param[in] data Byte to feed into CRC. | |
74 | * @return Resultant CRC value. | |
75 | */ | |
76 | static uint8_t _calc_crc(uint8_t crc, uint8_t data) | |
77 | { | |
78 | // https://www.maximintegrated.com/en/app-notes/index.mvp/id/27 | |
79 | static const uint8_t table[256] = { | |
80 | 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, | |
81 | 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, | |
82 | 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, | |
83 | 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, | |
84 | 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, | |
85 | 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, | |
86 | 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, | |
87 | 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, | |
88 | 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, | |
89 | 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, | |
90 | 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, | |
91 | 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, | |
92 | 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, | |
93 | 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, | |
94 | 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, | |
95 | 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53 | |
96 | }; | |
97 | ||
98 | return table[crc ^ data]; | |
99 | } | |
100 | ||
101 | static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len) | |
102 | { | |
103 | do | |
104 | { | |
105 | crc = _calc_crc(crc, *buffer++); | |
129 | 106 | ESP_LOGD(TAG, "buffer 0x%02x, crc 0x%02x, len %d", (uint8_t)*(buffer - 1), (int)crc, (int)len); |
0 | 107 | } |
108 | while (--len > 0); | |
109 | return crc; | |
110 | } | |
111 | ||
112 | /** | |
113 | * @param[out] is_found true if a device was found, false if not | |
114 | * @return status | |
115 | */ | |
129 | 116 | static owb_status _search(const OneWireBus * bus, OneWireBus_SearchState * state, bool * is_found) |
0 | 117 | { |
118 | // Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187 | |
119 | ||
120 | // initialize for search | |
121 | int id_bit_number = 1; | |
122 | int last_zero = 0; | |
123 | int rom_byte_number = 0; | |
124 | uint8_t id_bit = 0; | |
125 | uint8_t cmp_id_bit = 0; | |
126 | uint8_t rom_byte_mask = 1; | |
127 | uint8_t search_direction = 0; | |
128 | bool search_result = false; | |
129 | uint8_t crc8 = 0; | |
129 | 130 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 131 | |
132 | // if the last call was not the last one | |
133 | if (!state->last_device_flag) | |
134 | { | |
135 | // 1-Wire reset | |
136 | bool is_present; | |
137 | bus->driver->reset(bus, &is_present); | |
138 | if (!is_present) | |
139 | { | |
140 | // reset the search | |
141 | state->last_discrepancy = 0; | |
142 | state->last_device_flag = false; | |
143 | state->last_family_discrepancy = 0; | |
144 | *is_found = false; | |
145 | return OWB_STATUS_OK; | |
146 | } | |
147 | ||
148 | // issue the search command | |
149 | bus->driver->write_bits(bus, OWB_ROM_SEARCH, 8); | |
150 | ||
151 | // loop to do the search | |
152 | do | |
153 | { | |
154 | id_bit = cmp_id_bit = 0; | |
155 | ||
156 | // read a bit and its complement | |
157 | bus->driver->read_bits(bus, &id_bit, 1); | |
158 | bus->driver->read_bits(bus, &cmp_id_bit, 1); | |
159 | ||
160 | // check for no devices on 1-wire (signal level is high in both bit reads) | |
161 | if (id_bit && cmp_id_bit) | |
162 | { | |
163 | break; | |
129 | 164 | } |
165 | else | |
0 | 166 | { |
167 | // all devices coupled have 0 or 1 | |
168 | if (id_bit != cmp_id_bit) | |
169 | { | |
170 | search_direction = (id_bit) ? 1 : 0; // bit write value for search | |
129 | 171 | } |
172 | else | |
0 | 173 | { |
174 | // if this discrepancy if before the Last Discrepancy | |
175 | // on a previous next then pick the same as last time | |
176 | if (id_bit_number < state->last_discrepancy) | |
129 | 177 | { |
0 | 178 | search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0); |
129 | 179 | } |
0 | 180 | else |
129 | 181 | { |
0 | 182 | // if equal to last pick 1, if not then pick 0 |
183 | search_direction = (id_bit_number == state->last_discrepancy); | |
129 | 184 | } |
0 | 185 | |
186 | // if 0 was picked then record its position in LastZero | |
187 | if (search_direction == 0) | |
188 | { | |
189 | last_zero = id_bit_number; | |
190 | ||
191 | // check for Last discrepancy in family | |
192 | if (last_zero < 9) | |
129 | 193 | { |
0 | 194 | state->last_family_discrepancy = last_zero; |
129 | 195 | } |
0 | 196 | } |
197 | } | |
198 | ||
199 | // set or clear the bit in the ROM byte rom_byte_number | |
200 | // with mask rom_byte_mask | |
201 | if (search_direction == 1) | |
129 | 202 | { |
0 | 203 | state->rom_code.bytes[rom_byte_number] |= rom_byte_mask; |
129 | 204 | } |
0 | 205 | else |
129 | 206 | { |
0 | 207 | state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask; |
129 | 208 | } |
0 | 209 | |
210 | // serial number search direction write bit | |
211 | bus->driver->write_bits(bus, search_direction, 1); | |
212 | ||
213 | // increment the byte counter id_bit_number | |
214 | // and shift the mask rom_byte_mask | |
215 | id_bit_number++; | |
216 | rom_byte_mask <<= 1; | |
217 | ||
218 | // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask | |
219 | if (rom_byte_mask == 0) | |
220 | { | |
221 | crc8 = owb_crc8_byte(crc8, state->rom_code.bytes[rom_byte_number]); // accumulate the CRC | |
222 | rom_byte_number++; | |
223 | rom_byte_mask = 1; | |
224 | } | |
225 | } | |
226 | } | |
129 | 227 | while (rom_byte_number < 8); // loop until through all ROM bytes 0-7 |
0 | 228 | |
229 | // if the search was successful then | |
230 | if (!((id_bit_number < 65) || (crc8 != 0))) | |
231 | { | |
232 | // search successful so set LastDiscrepancy,LastDeviceFlag,search_result | |
233 | state->last_discrepancy = last_zero; | |
234 | ||
235 | // check for last device | |
236 | if (state->last_discrepancy == 0) | |
129 | 237 | { |
0 | 238 | state->last_device_flag = true; |
129 | 239 | } |
0 | 240 | |
241 | search_result = true; | |
242 | } | |
243 | } | |
244 | ||
245 | // if no device found then reset counters so next 'search' will be like a first | |
246 | if (!search_result || !state->rom_code.bytes[0]) | |
247 | { | |
248 | state->last_discrepancy = 0; | |
249 | state->last_device_flag = false; | |
250 | state->last_family_discrepancy = 0; | |
251 | search_result = false; | |
252 | } | |
253 | ||
254 | status = OWB_STATUS_OK; | |
255 | ||
256 | *is_found = search_result; | |
257 | ||
258 | return status; | |
259 | } | |
260 | ||
261 | // Public API | |
262 | ||
263 | owb_status owb_uninitialize(OneWireBus * bus) | |
264 | { | |
129 | 265 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 266 | |
129 | 267 | if (!_is_init(bus)) |
0 | 268 | { |
269 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 270 | } |
271 | else | |
0 | 272 | { |
273 | bus->driver->uninitialize(bus); | |
274 | status = OWB_STATUS_OK; | |
275 | } | |
276 | ||
277 | return status; | |
278 | } | |
279 | ||
280 | owb_status owb_use_crc(OneWireBus * bus, bool use_crc) | |
281 | { | |
129 | 282 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 283 | |
129 | 284 | if (!bus) |
0 | 285 | { |
286 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 287 | } |
288 | else if (!_is_init(bus)) | |
0 | 289 | { |
290 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 291 | } |
292 | else | |
0 | 293 | { |
294 | bus->use_crc = use_crc; | |
295 | ESP_LOGD(TAG, "use_crc %d", bus->use_crc); | |
296 | ||
297 | status = OWB_STATUS_OK; | |
298 | } | |
299 | ||
300 | return status; | |
301 | } | |
302 | ||
129 | 303 | owb_status owb_use_parasitic_power(OneWireBus * bus, bool use_parasitic_power) |
304 | { | |
305 | owb_status status = OWB_STATUS_NOT_SET; | |
306 | ||
307 | if (!bus) | |
308 | { | |
309 | status = OWB_STATUS_PARAMETER_NULL; | |
310 | } | |
311 | else if (!_is_init(bus)) | |
312 | { | |
313 | status = OWB_STATUS_NOT_INITIALIZED; | |
314 | } | |
315 | else | |
316 | { | |
317 | bus->use_parasitic_power = use_parasitic_power; | |
318 | ESP_LOGD(TAG, "use_parasitic_power %d", bus->use_parasitic_power); | |
319 | ||
320 | status = OWB_STATUS_OK; | |
321 | } | |
322 | ||
323 | return status; | |
324 | } | |
325 | ||
326 | owb_status owb_use_strong_pullup_gpio(OneWireBus * bus, gpio_num_t gpio) | |
327 | { | |
328 | owb_status status = OWB_STATUS_NOT_SET; | |
329 | ||
330 | if (!bus) | |
331 | { | |
332 | status = OWB_STATUS_PARAMETER_NULL; | |
333 | } | |
334 | else if (!_is_init(bus)) | |
335 | { | |
336 | status = OWB_STATUS_NOT_INITIALIZED; | |
337 | } | |
338 | else | |
339 | { | |
340 | if (gpio != GPIO_NUM_NC) { | |
341 | // The strong GPIO pull-up is only activated if parasitic-power mode is enabled | |
342 | if (!bus->use_parasitic_power) { | |
343 | ESP_LOGW(TAG, "Strong pull-up GPIO set with parasitic-power disabled"); | |
344 | } | |
345 | ||
346 | gpio_pad_select_gpio(gpio); | |
347 | gpio_set_direction(gpio, GPIO_MODE_OUTPUT); | |
348 | } | |
349 | else | |
350 | { | |
351 | gpio_reset_pin(bus->strong_pullup_gpio); | |
352 | } | |
353 | ||
354 | bus->strong_pullup_gpio = gpio; | |
355 | ESP_LOGD(TAG, "use_strong_pullup_gpio %d", bus->strong_pullup_gpio); | |
356 | ||
357 | status = OWB_STATUS_OK; | |
358 | } | |
359 | ||
360 | return status; | |
361 | } | |
362 | ||
0 | 363 | owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code) |
364 | { | |
129 | 365 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 366 | |
367 | memset(rom_code, 0, sizeof(OneWireBus_ROMCode)); | |
368 | ||
129 | 369 | if (!bus || !rom_code) |
0 | 370 | { |
371 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 372 | } |
373 | else if (!_is_init(bus)) | |
0 | 374 | { |
375 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 376 | } |
377 | else | |
0 | 378 | { |
379 | bool is_present; | |
380 | bus->driver->reset(bus, &is_present); | |
381 | if (is_present) | |
382 | { | |
383 | uint8_t value = OWB_ROM_READ; | |
384 | bus->driver->write_bits(bus, value, 8); | |
385 | owb_read_bytes(bus, rom_code->bytes, sizeof(OneWireBus_ROMCode)); | |
386 | ||
387 | if (bus->use_crc) | |
388 | { | |
389 | if (owb_crc8_bytes(0, rom_code->bytes, sizeof(OneWireBus_ROMCode)) != 0) | |
390 | { | |
391 | ESP_LOGE(TAG, "CRC failed"); | |
392 | memset(rom_code->bytes, 0, sizeof(OneWireBus_ROMCode)); | |
393 | status = OWB_STATUS_CRC_FAILED; | |
129 | 394 | } |
395 | else | |
0 | 396 | { |
397 | status = OWB_STATUS_OK; | |
398 | } | |
129 | 399 | } |
400 | else | |
0 | 401 | { |
402 | status = OWB_STATUS_OK; | |
403 | } | |
404 | char rom_code_s[OWB_ROM_CODE_STRING_LENGTH]; | |
405 | owb_string_from_rom_code(*rom_code, rom_code_s, sizeof(rom_code_s)); | |
406 | ESP_LOGD(TAG, "rom_code %s", rom_code_s); | |
407 | } | |
408 | else | |
409 | { | |
410 | status = OWB_STATUS_DEVICE_NOT_RESPONDING; | |
129 | 411 | ESP_LOGE(TAG, "ds18b20 device not responding"); |
0 | 412 | } |
413 | } | |
414 | ||
415 | return status; | |
416 | } | |
417 | ||
129 | 418 | owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool * is_present) |
0 | 419 | { |
129 | 420 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 421 | bool result = false; |
422 | ||
423 | if (!bus || !is_present) | |
424 | { | |
425 | status = OWB_STATUS_PARAMETER_NULL; | |
426 | } | |
427 | else if (!_is_init(bus)) | |
428 | { | |
429 | status = OWB_STATUS_NOT_INITIALIZED; | |
430 | } | |
431 | else | |
432 | { | |
433 | OneWireBus_SearchState state = { | |
129 | 434 | .rom_code = rom_code, |
0 | 435 | .last_discrepancy = 64, |
436 | .last_device_flag = false, | |
437 | }; | |
438 | ||
439 | bool is_found = false; | |
129 | 440 | _search(bus, &state, &is_found); |
441 | if (is_found) | |
0 | 442 | { |
129 | 443 | result = true; |
444 | for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i) | |
0 | 445 | { |
129 | 446 | result = rom_code.bytes[i] == state.rom_code.bytes[i]; |
447 | ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[i]); | |
0 | 448 | } |
129 | 449 | is_found = result; |
0 | 450 | } |
129 | 451 | ESP_LOGD(TAG, "state.last_discrepancy %d, state.last_device_flag %d, is_found %d", |
452 | state.last_discrepancy, state.last_device_flag, is_found); | |
0 | 453 | |
454 | ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not "); | |
455 | *is_present = result; | |
456 | status = OWB_STATUS_OK; | |
457 | } | |
458 | ||
459 | return status; | |
460 | } | |
461 | ||
129 | 462 | owb_status owb_reset(const OneWireBus * bus, bool * a_device_present) |
463 | { | |
464 | owb_status status = OWB_STATUS_NOT_SET; | |
465 | ||
466 | if (!bus || !a_device_present) | |
467 | { | |
468 | status = OWB_STATUS_PARAMETER_NULL; | |
469 | } | |
470 | else if(!_is_init(bus)) | |
471 | { | |
472 | status = OWB_STATUS_NOT_INITIALIZED; | |
473 | } | |
474 | else | |
475 | { | |
476 | status = bus->driver->reset(bus, a_device_present); | |
477 | } | |
478 | ||
479 | return status; | |
480 | } | |
481 | ||
482 | owb_status owb_read_bit(const OneWireBus * bus, uint8_t * out) | |
0 | 483 | { |
129 | 484 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 485 | |
129 | 486 | if (!bus || !out) |
487 | { | |
488 | status = OWB_STATUS_PARAMETER_NULL; | |
489 | } | |
490 | else if (!_is_init(bus)) | |
491 | { | |
492 | status = OWB_STATUS_NOT_INITIALIZED; | |
493 | } | |
494 | else | |
495 | { | |
496 | bus->driver->read_bits(bus, out, 1); | |
497 | ESP_LOGD(TAG, "owb_read_bit: %02x", *out); | |
498 | status = OWB_STATUS_OK; | |
499 | } | |
500 | ||
501 | return status; | |
502 | } | |
503 | ||
504 | owb_status owb_read_byte(const OneWireBus * bus, uint8_t * out) | |
505 | { | |
506 | owb_status status = OWB_STATUS_NOT_SET; | |
507 | ||
508 | if (!bus || !out) | |
0 | 509 | { |
510 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 511 | } |
512 | else if (!_is_init(bus)) | |
513 | { | |
514 | status = OWB_STATUS_NOT_INITIALIZED; | |
515 | } | |
516 | else | |
517 | { | |
518 | bus->driver->read_bits(bus, out, 8); | |
519 | ESP_LOGD(TAG, "owb_read_byte: %02x", *out); | |
520 | status = OWB_STATUS_OK; | |
521 | } | |
522 | ||
523 | return status; | |
524 | } | |
525 | ||
526 | owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len) | |
527 | { | |
528 | owb_status status = OWB_STATUS_NOT_SET; | |
529 | ||
530 | if (!bus || !buffer) | |
531 | { | |
532 | status = OWB_STATUS_PARAMETER_NULL; | |
533 | } | |
534 | else if (!_is_init(bus)) | |
0 | 535 | { |
536 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 537 | } |
538 | else | |
0 | 539 | { |
129 | 540 | for (int i = 0; i < len; ++i) |
541 | { | |
542 | uint8_t out; | |
543 | bus->driver->read_bits(bus, &out, 8); | |
544 | buffer[i] = out; | |
545 | } | |
546 | ||
547 | ESP_LOGD(TAG, "owb_read_bytes, len %d:", len); | |
548 | ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG); | |
549 | ||
550 | status = OWB_STATUS_OK; | |
551 | } | |
552 | ||
553 | return status; | |
554 | } | |
555 | ||
556 | owb_status owb_write_bit(const OneWireBus * bus, const uint8_t bit) | |
557 | { | |
558 | owb_status status = OWB_STATUS_NOT_SET; | |
559 | ||
560 | if (!bus) | |
561 | { | |
562 | status = OWB_STATUS_PARAMETER_NULL; | |
563 | } | |
564 | else if (!_is_init(bus)) | |
565 | { | |
566 | status = OWB_STATUS_NOT_INITIALIZED; | |
567 | } | |
568 | else | |
569 | { | |
570 | ESP_LOGD(TAG, "owb_write_bit: %02x", bit); | |
571 | bus->driver->write_bits(bus, bit & 0x01u, 1); | |
0 | 572 | status = OWB_STATUS_OK; |
573 | } | |
574 | ||
575 | return status; | |
576 | } | |
577 | ||
578 | owb_status owb_write_byte(const OneWireBus * bus, uint8_t data) | |
579 | { | |
129 | 580 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 581 | |
129 | 582 | if (!bus) |
0 | 583 | { |
584 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 585 | } |
586 | else if (!_is_init(bus)) | |
0 | 587 | { |
588 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 589 | } |
590 | else | |
0 | 591 | { |
129 | 592 | ESP_LOGD(TAG, "owb_write_byte: %02x", data); |
0 | 593 | bus->driver->write_bits(bus, data, 8); |
594 | status = OWB_STATUS_OK; | |
595 | } | |
596 | ||
597 | return status; | |
598 | } | |
599 | ||
129 | 600 | owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, size_t len) |
0 | 601 | { |
129 | 602 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 603 | |
129 | 604 | if (!bus || !buffer) |
0 | 605 | { |
606 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 607 | } |
608 | else if (!_is_init(bus)) | |
0 | 609 | { |
610 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 611 | } |
612 | else | |
0 | 613 | { |
129 | 614 | ESP_LOGD(TAG, "owb_write_bytes, len %d:", len); |
615 | ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG); | |
0 | 616 | |
617 | for (int i = 0; i < len; i++) | |
618 | { | |
619 | bus->driver->write_bits(bus, buffer[i], 8); | |
620 | } | |
621 | ||
622 | status = OWB_STATUS_OK; | |
623 | } | |
624 | ||
625 | return status; | |
626 | } | |
627 | ||
628 | owb_status owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code) | |
629 | { | |
129 | 630 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 631 | |
129 | 632 | if (!bus) |
0 | 633 | { |
634 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 635 | } |
636 | else if (!_is_init(bus)) | |
0 | 637 | { |
638 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 639 | } |
640 | else | |
0 | 641 | { |
642 | owb_write_bytes(bus, (uint8_t*)&rom_code, sizeof(rom_code)); | |
643 | status = OWB_STATUS_OK; | |
644 | } | |
645 | ||
646 | return status; | |
647 | } | |
648 | ||
649 | uint8_t owb_crc8_byte(uint8_t crc, uint8_t data) | |
650 | { | |
651 | return _calc_crc(crc, data); | |
652 | } | |
653 | ||
654 | uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len) | |
655 | { | |
656 | return _calc_crc_block(crc, data, len); | |
657 | } | |
658 | ||
129 | 659 | owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device) |
0 | 660 | { |
661 | bool result; | |
129 | 662 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 663 | |
129 | 664 | if (!bus || !state || !found_device) |
0 | 665 | { |
666 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 667 | } |
668 | else if (!_is_init(bus)) | |
0 | 669 | { |
670 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 671 | } |
672 | else | |
0 | 673 | { |
674 | memset(&state->rom_code, 0, sizeof(state->rom_code)); | |
675 | state->last_discrepancy = 0; | |
676 | state->last_family_discrepancy = 0; | |
677 | state->last_device_flag = false; | |
678 | _search(bus, state, &result); | |
679 | status = OWB_STATUS_OK; | |
680 | ||
681 | *found_device = result; | |
682 | } | |
683 | ||
684 | return status; | |
685 | } | |
686 | ||
129 | 687 | owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device) |
0 | 688 | { |
129 | 689 | owb_status status = OWB_STATUS_NOT_SET; |
0 | 690 | bool result = false; |
691 | ||
129 | 692 | if (!bus || !state || !found_device) |
0 | 693 | { |
694 | status = OWB_STATUS_PARAMETER_NULL; | |
129 | 695 | } |
696 | else if (!_is_init(bus)) | |
0 | 697 | { |
698 | status = OWB_STATUS_NOT_INITIALIZED; | |
129 | 699 | } |
700 | else | |
0 | 701 | { |
702 | _search(bus, state, &result); | |
703 | status = OWB_STATUS_OK; | |
704 | ||
705 | *found_device = result; | |
706 | } | |
707 | ||
708 | return status; | |
709 | } | |
710 | ||
711 | char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len) | |
712 | { | |
713 | for (int i = sizeof(rom_code.bytes) - 1; i >= 0; i--) | |
714 | { | |
715 | sprintf(buffer, "%02x", rom_code.bytes[i]); | |
716 | buffer += 2; | |
717 | } | |
718 | return buffer; | |
719 | } | |
129 | 720 | |
721 | owb_status owb_set_strong_pullup(const OneWireBus * bus, bool enable) | |
722 | { | |
723 | owb_status status = OWB_STATUS_NOT_SET; | |
724 | ||
725 | if (!bus) | |
726 | { | |
727 | status = OWB_STATUS_PARAMETER_NULL; | |
728 | } | |
729 | else if (!_is_init(bus)) | |
730 | { | |
731 | status = OWB_STATUS_NOT_INITIALIZED; | |
732 | } | |
733 | else | |
734 | { | |
735 | if (bus->use_parasitic_power && bus->strong_pullup_gpio != GPIO_NUM_NC) | |
736 | { | |
737 | gpio_set_level(bus->strong_pullup_gpio, enable ? 1 : 0); | |
738 | ESP_LOGD(TAG, "strong pullup GPIO %d", enable); | |
739 | } // else ignore | |
740 | ||
741 | status = OWB_STATUS_OK; | |
742 | } | |
743 | ||
744 | return status; | |
745 | } |