From 300a6be90f0428911c3376d6e655a4bd0333b4f8 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Sun, 26 Apr 2015 21:09:30 +0200 Subject: [PATCH] added background reset check (in case the master sends a reset while another operation had started) --- OneWireSlave.cpp | 174 +++++++++++++++++++++++++++-------------------- OneWireSlave.h | 4 ++ 2 files changed, 106 insertions(+), 72 deletions(-) diff --git a/OneWireSlave.cpp b/OneWireSlave.cpp index ca8deba..0ae7cdb 100644 --- a/OneWireSlave.cpp +++ b/OneWireSlave.cpp @@ -179,84 +179,31 @@ void OneWireSlave::releaseBus_() #endif } -void OneWireSlave::beginWaitReset_() +void OneWireSlave::beginResetDetection_() +{ + setTimerEvent_(ResetMinDuration - 50, &OneWireSlave::resetCheck_); + resetStart_ = micros() - 50; +} + +void OneWireSlave::cancelResetDetection_() { disableTimer_(); - pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); - resetStart_ = (unsigned int)-1; + resetStart_ = (unsigned long)-1; } -void OneWireSlave::waitReset_() +void OneWireSlave::resetCheck_() { onEnterInterrupt_(); - bool state = pin_.read(); - unsigned long now = micros(); - if (state) + if (!pin_.read()) { - if (resetStart_ == (unsigned int)-1) - { - onLeaveInterrupt_(); - return; - } - - unsigned long resetDuration = now - resetStart_; - resetStart_ = (unsigned int)-1; - if (resetDuration >= ResetMinDuration) - { - if (resetDuration > ResetMaxDuration) - { - error_("Reset too long"); - onLeaveInterrupt_(); - return; - } - - lastReset_ = now; - pin_.detachInterrupt(); - setTimerEvent_(PresenceWaitDuration - (micros() - now), &OneWireSlave::beginPresence_); - } - } - else - { - resetStart_ = now; + pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); + #ifdef DEBUG_LOG + debug.SC_APPEND_STR("Reset detected during another operation"); + #endif } onLeaveInterrupt_(); } -void OneWireSlave::beginPresence_() -{ - unsigned long now = micros(); - pullLow_(); - setTimerEvent_(PresenceDuration, &OneWireSlave::endPresence_); - #ifdef DEBUG_LOG - debug.SC_APPEND_STR_TIME("reset", lastReset_); - debug.SC_APPEND_STR_TIME("beginPresence", now); - #endif -} - -void OneWireSlave::endPresence_() -{ - unsigned long now = micros(); - releaseBus_(); - #ifdef DEBUG_LOG - debug.SC_APPEND_STR_TIME("endPresence", now); - #endif - - beginWaitCommand_(); -} - -void OneWireSlave::beginWaitCommand_() -{ - receiveTarget_ = ReceiveCommand; - beginReceive_(); -} - -void OneWireSlave::beginReceive_() -{ - receivingByte_ = 0; - receivingBitPos_ = 0; - beginReceiveBit_(&OneWireSlave::onBitReceived_); -} - void OneWireSlave::beginReceiveBit_(void(*completeCallback)(bool bit, bool error)) { receiveBitCallback_ = completeCallback; @@ -273,6 +220,22 @@ void OneWireSlave::receive_() onLeaveInterrupt_(); } +void OneWireSlave::readBit_() +{ + onEnterInterrupt_(); + + bool bit = pin_.read(); + if (bit) + cancelResetDetection_(); + else + beginResetDetection_(); + receiveBitCallback_(bit, false); + //dbgOutput.writeLow(); + //dbgOutput.writeHigh(); + + onLeaveInterrupt_(); +} + void OneWireSlave::beginSendBit_(bool bit, void(*completeCallback)(bool error)) { bitSentCallback_ = completeCallback; @@ -290,6 +253,7 @@ void OneWireSlave::sendBitOne_() { onEnterInterrupt_(); + beginResetDetection_(); bitSentCallback_(false); onLeaveInterrupt_(); @@ -317,18 +281,84 @@ void OneWireSlave::endSendBitZero_() onLeaveInterrupt_(); } -void OneWireSlave::readBit_() +void OneWireSlave::beginWaitReset_() +{ + disableTimer_(); + pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); + resetStart_ = (unsigned int)-1; +} + +void OneWireSlave::waitReset_() { onEnterInterrupt_(); + bool state = pin_.read(); + unsigned long now = micros(); + if (state) + { + if (resetStart_ == (unsigned int)-1) + { + onLeaveInterrupt_(); + return; + } - bool bit = pin_.read(); - receiveBitCallback_(bit, false); - //dbgOutput.writeLow(); - //dbgOutput.writeHigh(); + unsigned long resetDuration = now - resetStart_; + resetStart_ = (unsigned int)-1; + if (resetDuration >= ResetMinDuration) + { + if (resetDuration > ResetMaxDuration) + { + error_("Reset too long"); + onLeaveInterrupt_(); + return; + } + lastReset_ = now; + pin_.detachInterrupt(); + setTimerEvent_(PresenceWaitDuration - (micros() - now), &OneWireSlave::beginPresence_); + } + } + else + { + resetStart_ = now; + } onLeaveInterrupt_(); } +void OneWireSlave::beginPresence_() +{ + unsigned long now = micros(); + pullLow_(); + setTimerEvent_(PresenceDuration, &OneWireSlave::endPresence_); + #ifdef DEBUG_LOG + debug.SC_APPEND_STR_TIME("reset", lastReset_); + debug.SC_APPEND_STR_TIME("beginPresence", now); + #endif +} + +void OneWireSlave::endPresence_() +{ + unsigned long now = micros(); + releaseBus_(); + #ifdef DEBUG_LOG + debug.SC_APPEND_STR_TIME("endPresence", now); + #endif + + beginWaitCommand_(); +} + +void OneWireSlave::beginWaitCommand_() +{ + receiveTarget_ = ReceiveCommand; + beginReceive_(); +} + +void OneWireSlave::beginReceive_() +{ + receivingByte_ = 0; + receivingBitPos_ = 0; + beginReceiveBit_(&OneWireSlave::onBitReceived_); +} + void OneWireSlave::onBitReceived_(bool bit, bool error) { if (error) diff --git a/OneWireSlave.h b/OneWireSlave.h index 0275698..73ecee7 100644 --- a/OneWireSlave.h +++ b/OneWireSlave.h @@ -39,6 +39,9 @@ private: static void beginReceiveBit_(void(*completeCallback)(bool bit, bool error)); static void beginSendBit_(bool bit, void(*completeCallback)(bool error)); + static void beginResetDetection_(); + static void cancelResetDetection_(); + static void beginWaitReset_(); static void beginWaitCommand_(); static void beginReceive_(); @@ -58,6 +61,7 @@ private: static void sendBitOne_(); static void sendBitZero_(); static void endSendBitZero_(); + static void resetCheck_(); private: static byte rom_[8];