components/esp32-owb/owb.c

changeset 72
acc1904cd70d
parent 0
88d965579617
equal deleted inserted replaced
71:995557380e5f 72:acc1904cd70d
36 #include "freertos/FreeRTOS.h" 36 #include "freertos/FreeRTOS.h"
37 #include "freertos/task.h" 37 #include "freertos/task.h"
38 #include "esp_log.h" 38 #include "esp_log.h"
39 #include "sdkconfig.h" 39 #include "sdkconfig.h"
40 #include "driver/gpio.h" 40 #include "driver/gpio.h"
41 #include "rom/gpio.h" // for gpio_pad_select_gpio()
41 42
42 #include "owb.h" 43 #include "owb.h"
43 #include "owb_gpio.h" 44 #include "owb_gpio.h"
44 45
45 static const char * TAG = "owb"; 46 static const char * TAG = "owb";
100 static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len) 101 static uint8_t _calc_crc_block(uint8_t crc, const uint8_t * buffer, size_t len)
101 { 102 {
102 do 103 do
103 { 104 {
104 crc = _calc_crc(crc, *buffer++); 105 crc = _calc_crc(crc, *buffer++);
105 ESP_LOGD(TAG, "crc 0x%02x, len %d", (int)crc, (int)len); 106 ESP_LOGD(TAG, "buffer 0x%02x, crc 0x%02x, len %d", (uint8_t)*(buffer - 1), (int)crc, (int)len);
106 } 107 }
107 while (--len > 0); 108 while (--len > 0);
108 return crc; 109 return crc;
109 } 110 }
110 111
111 /** 112 /**
112 * @param[out] is_found true if a device was found, false if not 113 * @param[out] is_found true if a device was found, false if not
113 * @return status 114 * @return status
114 */ 115 */
115 static owb_status _search(const OneWireBus * bus, OneWireBus_SearchState * state, bool *is_found) 116 static owb_status _search(const OneWireBus * bus, OneWireBus_SearchState * state, bool * is_found)
116 { 117 {
117 // Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187 118 // Based on https://www.maximintegrated.com/en/app-notes/index.mvp/id/187
118 119
119 // initialize for search 120 // initialize for search
120 int id_bit_number = 1; 121 int id_bit_number = 1;
124 uint8_t cmp_id_bit = 0; 125 uint8_t cmp_id_bit = 0;
125 uint8_t rom_byte_mask = 1; 126 uint8_t rom_byte_mask = 1;
126 uint8_t search_direction = 0; 127 uint8_t search_direction = 0;
127 bool search_result = false; 128 bool search_result = false;
128 uint8_t crc8 = 0; 129 uint8_t crc8 = 0;
129 owb_status status; 130 owb_status status = OWB_STATUS_NOT_SET;
130 131
131 // if the last call was not the last one 132 // if the last call was not the last one
132 if (!state->last_device_flag) 133 if (!state->last_device_flag)
133 { 134 {
134 // 1-Wire reset 135 // 1-Wire reset
158 159
159 // check for no devices on 1-wire (signal level is high in both bit reads) 160 // check for no devices on 1-wire (signal level is high in both bit reads)
160 if (id_bit && cmp_id_bit) 161 if (id_bit && cmp_id_bit)
161 { 162 {
162 break; 163 break;
163 } else 164 }
165 else
164 { 166 {
165 // all devices coupled have 0 or 1 167 // all devices coupled have 0 or 1
166 if (id_bit != cmp_id_bit) 168 if (id_bit != cmp_id_bit)
167 { 169 {
168 search_direction = (id_bit) ? 1 : 0; // bit write value for search 170 search_direction = (id_bit) ? 1 : 0; // bit write value for search
169 } else 171 }
172 else
170 { 173 {
171 // if this discrepancy if before the Last Discrepancy 174 // if this discrepancy if before the Last Discrepancy
172 // on a previous next then pick the same as last time 175 // on a previous next then pick the same as last time
173 if (id_bit_number < state->last_discrepancy) 176 if (id_bit_number < state->last_discrepancy)
177 {
174 search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0); 178 search_direction = ((state->rom_code.bytes[rom_byte_number] & rom_byte_mask) > 0);
179 }
175 else 180 else
181 {
176 // if equal to last pick 1, if not then pick 0 182 // if equal to last pick 1, if not then pick 0
177 search_direction = (id_bit_number == state->last_discrepancy); 183 search_direction = (id_bit_number == state->last_discrepancy);
184 }
178 185
179 // if 0 was picked then record its position in LastZero 186 // if 0 was picked then record its position in LastZero
180 if (search_direction == 0) 187 if (search_direction == 0)
181 { 188 {
182 last_zero = id_bit_number; 189 last_zero = id_bit_number;
183 190
184 // check for Last discrepancy in family 191 // check for Last discrepancy in family
185 if (last_zero < 9) 192 if (last_zero < 9)
193 {
186 state->last_family_discrepancy = last_zero; 194 state->last_family_discrepancy = last_zero;
195 }
187 } 196 }
188 } 197 }
189 198
190 // set or clear the bit in the ROM byte rom_byte_number 199 // set or clear the bit in the ROM byte rom_byte_number
191 // with mask rom_byte_mask 200 // with mask rom_byte_mask
192 if (search_direction == 1) 201 if (search_direction == 1)
202 {
193 state->rom_code.bytes[rom_byte_number] |= rom_byte_mask; 203 state->rom_code.bytes[rom_byte_number] |= rom_byte_mask;
204 }
194 else 205 else
206 {
195 state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask; 207 state->rom_code.bytes[rom_byte_number] &= ~rom_byte_mask;
208 }
196 209
197 // serial number search direction write bit 210 // serial number search direction write bit
198 bus->driver->write_bits(bus, search_direction, 1); 211 bus->driver->write_bits(bus, search_direction, 1);
199 212
200 // increment the byte counter id_bit_number 213 // increment the byte counter id_bit_number
209 rom_byte_number++; 222 rom_byte_number++;
210 rom_byte_mask = 1; 223 rom_byte_mask = 1;
211 } 224 }
212 } 225 }
213 } 226 }
214 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 227 while (rom_byte_number < 8); // loop until through all ROM bytes 0-7
215 228
216 // if the search was successful then 229 // if the search was successful then
217 if (!((id_bit_number < 65) || (crc8 != 0))) 230 if (!((id_bit_number < 65) || (crc8 != 0)))
218 { 231 {
219 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result 232 // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
220 state->last_discrepancy = last_zero; 233 state->last_discrepancy = last_zero;
221 234
222 // check for last device 235 // check for last device
223 if (state->last_discrepancy == 0) 236 if (state->last_discrepancy == 0)
237 {
224 state->last_device_flag = true; 238 state->last_device_flag = true;
239 }
225 240
226 search_result = true; 241 search_result = true;
227 } 242 }
228 } 243 }
229 244
245 260
246 // Public API 261 // Public API
247 262
248 owb_status owb_uninitialize(OneWireBus * bus) 263 owb_status owb_uninitialize(OneWireBus * bus)
249 { 264 {
250 owb_status status; 265 owb_status status = OWB_STATUS_NOT_SET;
251 266
252 if(!_is_init(bus)) 267 if (!_is_init(bus))
253 { 268 {
254 status = OWB_STATUS_NOT_INITIALIZED; 269 status = OWB_STATUS_NOT_INITIALIZED;
255 } else 270 }
271 else
256 { 272 {
257 bus->driver->uninitialize(bus); 273 bus->driver->uninitialize(bus);
258 status = OWB_STATUS_OK; 274 status = OWB_STATUS_OK;
259 } 275 }
260 276
261 return status; 277 return status;
262 } 278 }
263 279
264 owb_status owb_use_crc(OneWireBus * bus, bool use_crc) 280 owb_status owb_use_crc(OneWireBus * bus, bool use_crc)
265 { 281 {
266 owb_status status; 282 owb_status status = OWB_STATUS_NOT_SET;
267 283
268 if(!bus) 284 if (!bus)
269 { 285 {
270 status = OWB_STATUS_PARAMETER_NULL; 286 status = OWB_STATUS_PARAMETER_NULL;
271 } else if (!_is_init(bus)) 287 }
272 { 288 else if (!_is_init(bus))
273 status = OWB_STATUS_NOT_INITIALIZED; 289 {
274 } else 290 status = OWB_STATUS_NOT_INITIALIZED;
291 }
292 else
275 { 293 {
276 bus->use_crc = use_crc; 294 bus->use_crc = use_crc;
277 ESP_LOGD(TAG, "use_crc %d", bus->use_crc); 295 ESP_LOGD(TAG, "use_crc %d", bus->use_crc);
278 296
279 status = OWB_STATUS_OK; 297 status = OWB_STATUS_OK;
280 } 298 }
281 299
282 return status; 300 return status;
283 } 301 }
284 302
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
285 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code) 363 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code)
286 { 364 {
287 owb_status status; 365 owb_status status = OWB_STATUS_NOT_SET;
288 366
289 memset(rom_code, 0, sizeof(OneWireBus_ROMCode)); 367 memset(rom_code, 0, sizeof(OneWireBus_ROMCode));
290 368
291 if(!bus || !rom_code) 369 if (!bus || !rom_code)
292 { 370 {
293 status = OWB_STATUS_PARAMETER_NULL; 371 status = OWB_STATUS_PARAMETER_NULL;
294 } else if (!_is_init(bus)) 372 }
295 { 373 else if (!_is_init(bus))
296 status = OWB_STATUS_NOT_INITIALIZED; 374 {
297 } else 375 status = OWB_STATUS_NOT_INITIALIZED;
376 }
377 else
298 { 378 {
299 bool is_present; 379 bool is_present;
300 bus->driver->reset(bus, &is_present); 380 bus->driver->reset(bus, &is_present);
301 if (is_present) 381 if (is_present)
302 { 382 {
309 if (owb_crc8_bytes(0, rom_code->bytes, sizeof(OneWireBus_ROMCode)) != 0) 389 if (owb_crc8_bytes(0, rom_code->bytes, sizeof(OneWireBus_ROMCode)) != 0)
310 { 390 {
311 ESP_LOGE(TAG, "CRC failed"); 391 ESP_LOGE(TAG, "CRC failed");
312 memset(rom_code->bytes, 0, sizeof(OneWireBus_ROMCode)); 392 memset(rom_code->bytes, 0, sizeof(OneWireBus_ROMCode));
313 status = OWB_STATUS_CRC_FAILED; 393 status = OWB_STATUS_CRC_FAILED;
314 } else 394 }
395 else
315 { 396 {
316 status = OWB_STATUS_OK; 397 status = OWB_STATUS_OK;
317 } 398 }
318 } else 399 }
400 else
319 { 401 {
320 status = OWB_STATUS_OK; 402 status = OWB_STATUS_OK;
321 } 403 }
322 char rom_code_s[OWB_ROM_CODE_STRING_LENGTH]; 404 char rom_code_s[OWB_ROM_CODE_STRING_LENGTH];
323 owb_string_from_rom_code(*rom_code, rom_code_s, sizeof(rom_code_s)); 405 owb_string_from_rom_code(*rom_code, rom_code_s, sizeof(rom_code_s));
331 } 413 }
332 414
333 return status; 415 return status;
334 } 416 }
335 417
336 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool* is_present) 418 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool * is_present)
337 { 419 {
338 owb_status status; 420 owb_status status = OWB_STATUS_NOT_SET;
339 bool result = false; 421 bool result = false;
340 422
341 if (!bus || !is_present) 423 if (!bus || !is_present)
342 { 424 {
343 status = OWB_STATUS_PARAMETER_NULL; 425 status = OWB_STATUS_PARAMETER_NULL;
347 status = OWB_STATUS_NOT_INITIALIZED; 429 status = OWB_STATUS_NOT_INITIALIZED;
348 } 430 }
349 else 431 else
350 { 432 {
351 OneWireBus_SearchState state = { 433 OneWireBus_SearchState state = {
434 .rom_code = rom_code,
352 .last_discrepancy = 64, 435 .last_discrepancy = 64,
353 .last_device_flag = false, 436 .last_device_flag = false,
354 }; 437 };
355 438
356 bool is_found = false; 439 bool is_found = false;
357 while (!state.last_device_flag && !is_found) 440 _search(bus, &state, &is_found);
358 { 441 if (is_found)
359 _search(bus, &state, &is_found); 442 {
360 if (is_found) 443 result = true;
444 for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i)
361 { 445 {
362 result = true; 446 result = rom_code.bytes[i] == state.rom_code.bytes[i];
363 for (int i = 0; i < sizeof(state.rom_code.bytes) && result; ++i) 447 ESP_LOGD(TAG, "%02x %02x", rom_code.bytes[i], state.rom_code.bytes[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 } 448 }
370 } 449 is_found = result;
450 }
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);
371 453
372 ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not "); 454 ESP_LOGD(TAG, "rom code %sfound", result ? "" : "not ");
373 *is_present = result; 455 *is_present = result;
374 status = OWB_STATUS_OK; 456 status = OWB_STATUS_OK;
375 } 457 }
376 458
377 return status; 459 return status;
378 } 460 }
379 461
380 owb_status owb_reset(const OneWireBus * bus, bool* a_device_present) 462 owb_status owb_reset(const OneWireBus * bus, bool * a_device_present)
381 { 463 {
382 owb_status status; 464 owb_status status = OWB_STATUS_NOT_SET;
383 465
384 if(!bus || !a_device_present) 466 if (!bus || !a_device_present)
385 { 467 {
386 status = OWB_STATUS_PARAMETER_NULL; 468 status = OWB_STATUS_PARAMETER_NULL;
387 } else if(!_is_init(bus)) 469 }
388 { 470 else if(!_is_init(bus))
389 status = OWB_STATUS_NOT_INITIALIZED; 471 {
390 } else 472 status = OWB_STATUS_NOT_INITIALIZED;
391 { 473 }
392 bus->driver->reset(bus, a_device_present); 474 else
393 status = OWB_STATUS_OK; 475 {
394 } 476 status = bus->driver->reset(bus, a_device_present);
395 477 }
396 return status; 478
397 } 479 return status;
398 480 }
399 owb_status owb_write_byte(const OneWireBus * bus, uint8_t data) 481
400 { 482 owb_status owb_read_bit(const OneWireBus * bus, uint8_t * out)
401 owb_status status; 483 {
402 484 owb_status status = OWB_STATUS_NOT_SET;
403 if(!bus) 485
404 { 486 if (!bus || !out)
405 status = OWB_STATUS_PARAMETER_NULL; 487 {
406 } else if (!_is_init(bus)) 488 status = OWB_STATUS_PARAMETER_NULL;
407 { 489 }
408 status = OWB_STATUS_NOT_INITIALIZED; 490 else if (!_is_init(bus))
409 } else 491 {
410 { 492 status = OWB_STATUS_NOT_INITIALIZED;
411 bus->driver->write_bits(bus, data, 8); 493 }
412 status = OWB_STATUS_OK; 494 else
413 } 495 {
414 496 bus->driver->read_bits(bus, out, 1);
415 return status; 497 ESP_LOGD(TAG, "owb_read_bit: %02x", *out);
416 } 498 status = OWB_STATUS_OK;
417 499 }
418 owb_status owb_read_byte(const OneWireBus * bus, uint8_t *out) 500
419 { 501 return status;
420 owb_status status; 502 }
421 503
422 if(!bus || !out) 504 owb_status owb_read_byte(const OneWireBus * bus, uint8_t * out)
423 { 505 {
424 status = OWB_STATUS_PARAMETER_NULL; 506 owb_status status = OWB_STATUS_NOT_SET;
425 } else if (!_is_init(bus)) 507
426 { 508 if (!bus || !out)
427 status = OWB_STATUS_NOT_INITIALIZED; 509 {
428 } else 510 status = OWB_STATUS_PARAMETER_NULL;
511 }
512 else if (!_is_init(bus))
513 {
514 status = OWB_STATUS_NOT_INITIALIZED;
515 }
516 else
429 { 517 {
430 bus->driver->read_bits(bus, out, 8); 518 bus->driver->read_bits(bus, out, 8);
519 ESP_LOGD(TAG, "owb_read_byte: %02x", *out);
431 status = OWB_STATUS_OK; 520 status = OWB_STATUS_OK;
432 } 521 }
433 522
434 return status; 523 return status;
435 } 524 }
436 525
437 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len) 526 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len)
438 { 527 {
439 owb_status status; 528 owb_status status = OWB_STATUS_NOT_SET;
440 529
441 if(!bus || !buffer) 530 if (!bus || !buffer)
442 { 531 {
443 status = OWB_STATUS_PARAMETER_NULL; 532 status = OWB_STATUS_PARAMETER_NULL;
444 } else if (!_is_init(bus)) 533 }
445 { 534 else if (!_is_init(bus))
446 status = OWB_STATUS_NOT_INITIALIZED; 535 {
447 } else 536 status = OWB_STATUS_NOT_INITIALIZED;
537 }
538 else
448 { 539 {
449 for (int i = 0; i < len; ++i) 540 for (int i = 0; i < len; ++i)
450 { 541 {
451 uint8_t out; 542 uint8_t out;
452 bus->driver->read_bits(bus, &out, 8); 543 bus->driver->read_bits(bus, &out, 8);
453 buffer[i] = out; 544 buffer[i] = out;
454 } 545 }
455 546
456 status = OWB_STATUS_OK; 547 ESP_LOGD(TAG, "owb_read_bytes, len %d:", len);
457 } 548 ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG);
458 549
459 return status; 550 status = OWB_STATUS_OK;
460 } 551 }
461 552
462 owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, unsigned int len) 553 return status;
463 { 554 }
464 owb_status status; 555
465 556 owb_status owb_write_bit(const OneWireBus * bus, const uint8_t bit)
466 if(!bus || !buffer) 557 {
467 { 558 owb_status status = OWB_STATUS_NOT_SET;
468 status = OWB_STATUS_PARAMETER_NULL; 559
469 } else if (!_is_init(bus)) 560 if (!bus)
470 { 561 {
471 status = OWB_STATUS_NOT_INITIALIZED; 562 status = OWB_STATUS_PARAMETER_NULL;
472 } else 563 }
473 { 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);
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 {
580 owb_status status = OWB_STATUS_NOT_SET;
581
582 if (!bus)
583 {
584 status = OWB_STATUS_PARAMETER_NULL;
585 }
586 else if (!_is_init(bus))
587 {
588 status = OWB_STATUS_NOT_INITIALIZED;
589 }
590 else
591 {
592 ESP_LOGD(TAG, "owb_write_byte: %02x", data);
593 bus->driver->write_bits(bus, data, 8);
594 status = OWB_STATUS_OK;
595 }
596
597 return status;
598 }
599
600 owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, size_t len)
601 {
602 owb_status status = OWB_STATUS_NOT_SET;
603
604 if (!bus || !buffer)
605 {
606 status = OWB_STATUS_PARAMETER_NULL;
607 }
608 else if (!_is_init(bus))
609 {
610 status = OWB_STATUS_NOT_INITIALIZED;
611 }
612 else
613 {
614 ESP_LOGD(TAG, "owb_write_bytes, len %d:", len);
615 ESP_LOG_BUFFER_HEX_LEVEL(TAG, buffer, len, ESP_LOG_DEBUG);
616
474 for (int i = 0; i < len; i++) 617 for (int i = 0; i < len; i++)
475 { 618 {
476 bus->driver->write_bits(bus, buffer[i], 8); 619 bus->driver->write_bits(bus, buffer[i], 8);
477 } 620 }
478 621
482 return status; 625 return status;
483 } 626 }
484 627
485 owb_status owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code) 628 owb_status owb_write_rom_code(const OneWireBus * bus, OneWireBus_ROMCode rom_code)
486 { 629 {
487 owb_status status; 630 owb_status status = OWB_STATUS_NOT_SET;
488 631
489 if(!bus) 632 if (!bus)
490 { 633 {
491 status = OWB_STATUS_PARAMETER_NULL; 634 status = OWB_STATUS_PARAMETER_NULL;
492 } else if (!_is_init(bus)) 635 }
493 { 636 else if (!_is_init(bus))
494 status = OWB_STATUS_NOT_INITIALIZED; 637 {
495 } else 638 status = OWB_STATUS_NOT_INITIALIZED;
639 }
640 else
496 { 641 {
497 owb_write_bytes(bus, (uint8_t*)&rom_code, sizeof(rom_code)); 642 owb_write_bytes(bus, (uint8_t*)&rom_code, sizeof(rom_code));
498 status = OWB_STATUS_OK; 643 status = OWB_STATUS_OK;
499 } 644 }
500 645
509 uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len) 654 uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len)
510 { 655 {
511 return _calc_crc_block(crc, data, len); 656 return _calc_crc_block(crc, data, len);
512 } 657 }
513 658
514 owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool* found_device) 659 owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device)
515 { 660 {
516 bool result; 661 bool result;
517 owb_status status; 662 owb_status status = OWB_STATUS_NOT_SET;
518 663
519 if(!bus || !state || !found_device) 664 if (!bus || !state || !found_device)
520 { 665 {
521 status = OWB_STATUS_PARAMETER_NULL; 666 status = OWB_STATUS_PARAMETER_NULL;
522 } else if (!_is_init(bus)) 667 }
523 { 668 else if (!_is_init(bus))
524 status = OWB_STATUS_NOT_INITIALIZED; 669 {
525 } else 670 status = OWB_STATUS_NOT_INITIALIZED;
671 }
672 else
526 { 673 {
527 memset(&state->rom_code, 0, sizeof(state->rom_code)); 674 memset(&state->rom_code, 0, sizeof(state->rom_code));
528 state->last_discrepancy = 0; 675 state->last_discrepancy = 0;
529 state->last_family_discrepancy = 0; 676 state->last_family_discrepancy = 0;
530 state->last_device_flag = false; 677 state->last_device_flag = false;
535 } 682 }
536 683
537 return status; 684 return status;
538 } 685 }
539 686
540 owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool* found_device) 687 owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool * found_device)
541 { 688 {
542 owb_status status; 689 owb_status status = OWB_STATUS_NOT_SET;
543 bool result = false; 690 bool result = false;
544 691
545 if(!bus || !state || !found_device) 692 if (!bus || !state || !found_device)
546 { 693 {
547 status = OWB_STATUS_PARAMETER_NULL; 694 status = OWB_STATUS_PARAMETER_NULL;
548 } else if (!_is_init(bus)) 695 }
549 { 696 else if (!_is_init(bus))
550 status = OWB_STATUS_NOT_INITIALIZED; 697 {
551 } else 698 status = OWB_STATUS_NOT_INITIALIZED;
699 }
700 else
552 { 701 {
553 _search(bus, state, &result); 702 _search(bus, state, &result);
554 status = OWB_STATUS_OK; 703 status = OWB_STATUS_OK;
555 704
556 *found_device = result; 705 *found_device = result;
566 sprintf(buffer, "%02x", rom_code.bytes[i]); 715 sprintf(buffer, "%02x", rom_code.bytes[i]);
567 buffer += 2; 716 buffer += 2;
568 } 717 }
569 return buffer; 718 return buffer;
570 } 719 }
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 }

mercurial