From 21ee2d560d28c3478c3557eaf9a309e91eb6f055 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 10 Jan 2025 23:16:45 +0300 Subject: [PATCH] onewire: check for presence & more debug strings - halt methods when reset() does not result in PRESENCE pulse clear up naming, separate reset() and presence() check - port search, writes and requests are logged - wire reset is logged --- code/espurna/driver_onewire.cpp | 146 +++++++++++++++++++++++--------- code/espurna/driver_onewire.h | 11 ++- 2 files changed, 116 insertions(+), 41 deletions(-) diff --git a/code/espurna/driver_onewire.cpp b/code/espurna/driver_onewire.cpp index 79a8e14296..0e67a3405b 100644 --- a/code/espurna/driver_onewire.cpp +++ b/code/espurna/driver_onewire.cpp @@ -26,14 +26,84 @@ Copyright (C) 2019-2024 by Maxim Prokhorov references; -bool reset(OneWire* wire) { - return wire->reset() != 0; +ResetResult reset(OneWire* wire) { + auto out = ResetResult::Unknown; + + switch (wire->reset()) { + case 0: + out = ResetResult::Busy; + break; + + case 1: + out = ResetResult::Presence; + break; + } + +#if DEBUG_SUPPORT + if (debug) { + const auto ret = reset_result(out); + DEBUG_MSG_P(PSTR("[W1] Reset (%.*s)\n"), + ret.length(), ret.data()); + } +#endif + + return out; } void skip(OneWire* wire) { @@ -222,6 +292,11 @@ Error Port::attach(unsigned char pin, bool parasite) { auto wire = std::make_unique(pin); auto devices = search(*wire, pin); + if (internal::debug) { + DEBUG_MSG_P(PSTR("[W1] Found %zu device(s) on GPIO%zu\n"), + devices.size(), pin); + } + if (!devices.size()) { gpioUnlock(pin); return Error::NotFound; @@ -291,12 +366,23 @@ Port::Devices Port::search(OneWire& wire, unsigned char pin) { return out; } -bool Port::reset() { - return _wire->reset() == 0; +ResetResult Port::reset() const { + return internal::reset(_wire.get()); +} + +bool Port::presence() const { + return reset() == ResetResult::Presence; } void Port::write(Address address, Span data) { - internal::reset(_wire.get()); + if (!presence()) { + if (internal::debug) { + DEBUG_MSG_P(PSTR("[W1] Write to %s failed\n"), + hexEncode(address).c_str()); + } + return; + } + internal::select(_wire.get(), address); internal::write_bytes(_wire.get(), data, parasite()); } @@ -311,7 +397,13 @@ void Port::write(Address address, uint8_t value) { } void Port::write(uint8_t value) { - internal::reset(_wire.get()); + if (!presence()) { + if (internal::debug) { + DEBUG_MSG_P(PSTR("[W1] Write failed\n")); + } + return; + } + internal::skip(_wire.get()); const std::array data{{ value }}; @@ -319,16 +411,19 @@ void Port::write(uint8_t value) { } bool Port::request(Address address, Span input, Span output) { - //if (!// - // return false; - //} - internal::reset(_wire.get()); + if (!presence()) { + if (internal::debug) { + DEBUG_MSG_P(PSTR("[W1] Request to %s failed\n"), + hexEncode(address).c_str()); + } + return false; + } internal::select(_wire.get(), address); internal::write_bytes(_wire.get(), input); internal::read_bytes(_wire.get(), output); - return internal::reset(_wire.get()); + return presence(); } bool Port::request(Address address, uint8_t value, Span output) { @@ -336,35 +431,6 @@ bool Port::request(Address address, uint8_t value, Span output) { return request(address, make_span(input), output); } -StringView error(Error error) { - StringView out; - - switch (error) { - case Error::Ok: - out = STRING_VIEW("OK"); - break; - - case Error::NotFound: - out = STRING_VIEW("Not found"); - break; - - case Error::Unresponsive: - out = STRING_VIEW("Device does not respond"); - break; - - case Error::GpioUsed: - out = STRING_VIEW("GPIO Already Used"); - break; - - case Error::Config: - out = STRING_VIEW("Invalid Configuration"); - break; - - } - - return out; -} - void setup() { #if DEBUG_SUPPORT debug::setup(); diff --git a/code/espurna/driver_onewire.h b/code/espurna/driver_onewire.h index 5e0d2acd6c..3dc382c368 100644 --- a/code/espurna/driver_onewire.h +++ b/code/espurna/driver_onewire.h @@ -42,6 +42,12 @@ enum class Error { NotFound, }; +enum class ResetResult { + Unknown, + Busy, // no devices on the bus / bus is shortened + Presence, // PRESENCE pulse was sent +}; + class Port { public: using Address = std::array; @@ -63,7 +69,8 @@ class Port { Error attach(unsigned char pin, bool parasite); void detach(); - bool reset(); + ResetResult reset() const; + bool presence() const; void write(Address address, Span); void write(Address, const uint8_t*, size_t); @@ -127,6 +134,8 @@ void dereference(PortPtr); void reference(PortPtr); StringView error(Error); +StringView reset_result(ResetResult); + void setup(); } // namesapce onewire