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 |
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 { |
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 |
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; |