|
|
@ -179,82 +179,29 @@ void OneWireSlave::releaseBus_() |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void OneWireSlave::beginWaitReset_() |
|
|
|
void OneWireSlave::beginResetDetection_() |
|
|
|
{ |
|
|
|
{ |
|
|
|
disableTimer_(); |
|
|
|
setTimerEvent_(ResetMinDuration - 50, &OneWireSlave::resetCheck_); |
|
|
|
pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); |
|
|
|
resetStart_ = micros() - 50; |
|
|
|
resetStart_ = (unsigned int)-1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void OneWireSlave::waitReset_() |
|
|
|
void OneWireSlave::cancelResetDetection_() |
|
|
|
{ |
|
|
|
|
|
|
|
onEnterInterrupt_(); |
|
|
|
|
|
|
|
bool state = pin_.read(); |
|
|
|
|
|
|
|
unsigned long now = micros(); |
|
|
|
|
|
|
|
if (state) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
onLeaveInterrupt_(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OneWireSlave::beginPresence_() |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned long now = micros(); |
|
|
|
disableTimer_(); |
|
|
|
pullLow_(); |
|
|
|
resetStart_ = (unsigned long)-1; |
|
|
|
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_() |
|
|
|
void OneWireSlave::resetCheck_() |
|
|
|
{ |
|
|
|
{ |
|
|
|
unsigned long now = micros(); |
|
|
|
onEnterInterrupt_(); |
|
|
|
releaseBus_(); |
|
|
|
if (!pin_.read()) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); |
|
|
|
#ifdef DEBUG_LOG |
|
|
|
#ifdef DEBUG_LOG |
|
|
|
debug.SC_APPEND_STR_TIME("endPresence", now); |
|
|
|
debug.SC_APPEND_STR("Reset detected during another operation"); |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
beginWaitCommand_(); |
|
|
|
onLeaveInterrupt_(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)) |
|
|
|
void OneWireSlave::beginReceiveBit_(void(*completeCallback)(bool bit, bool error)) |
|
|
@ -273,6 +220,22 @@ void OneWireSlave::receive_() |
|
|
|
onLeaveInterrupt_(); |
|
|
|
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)) |
|
|
|
void OneWireSlave::beginSendBit_(bool bit, void(*completeCallback)(bool error)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
bitSentCallback_ = completeCallback; |
|
|
|
bitSentCallback_ = completeCallback; |
|
|
@ -290,6 +253,7 @@ void OneWireSlave::sendBitOne_() |
|
|
|
{ |
|
|
|
{ |
|
|
|
onEnterInterrupt_(); |
|
|
|
onEnterInterrupt_(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
beginResetDetection_(); |
|
|
|
bitSentCallback_(false); |
|
|
|
bitSentCallback_(false); |
|
|
|
|
|
|
|
|
|
|
|
onLeaveInterrupt_(); |
|
|
|
onLeaveInterrupt_(); |
|
|
@ -317,18 +281,84 @@ void OneWireSlave::endSendBitZero_() |
|
|
|
onLeaveInterrupt_(); |
|
|
|
onLeaveInterrupt_(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void OneWireSlave::readBit_() |
|
|
|
void OneWireSlave::beginWaitReset_() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
disableTimer_(); |
|
|
|
|
|
|
|
pin_.attachInterrupt(&OneWireSlave::waitReset_, CHANGE); |
|
|
|
|
|
|
|
resetStart_ = (unsigned int)-1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void OneWireSlave::waitReset_() |
|
|
|
{ |
|
|
|
{ |
|
|
|
onEnterInterrupt_(); |
|
|
|
onEnterInterrupt_(); |
|
|
|
|
|
|
|
bool state = pin_.read(); |
|
|
|
|
|
|
|
unsigned long now = micros(); |
|
|
|
|
|
|
|
if (state) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (resetStart_ == (unsigned int)-1) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
onLeaveInterrupt_(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool bit = pin_.read(); |
|
|
|
unsigned long resetDuration = now - resetStart_; |
|
|
|
receiveBitCallback_(bit, false); |
|
|
|
resetStart_ = (unsigned int)-1; |
|
|
|
//dbgOutput.writeLow();
|
|
|
|
if (resetDuration >= ResetMinDuration) |
|
|
|
//dbgOutput.writeHigh();
|
|
|
|
{ |
|
|
|
|
|
|
|
if (resetDuration > ResetMaxDuration) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
error_("Reset too long"); |
|
|
|
|
|
|
|
onLeaveInterrupt_(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastReset_ = now; |
|
|
|
|
|
|
|
pin_.detachInterrupt(); |
|
|
|
|
|
|
|
setTimerEvent_(PresenceWaitDuration - (micros() - now), &OneWireSlave::beginPresence_); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
resetStart_ = now; |
|
|
|
|
|
|
|
} |
|
|
|
onLeaveInterrupt_(); |
|
|
|
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) |
|
|
|
void OneWireSlave::onBitReceived_(bool bit, bool error) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (error) |
|
|
|
if (error) |
|
|
|