@ -11,19 +11,40 @@
namespace
{
const unsigned long ResetMinDuration = 480 ;
const unsigned long ResetMaxDuration = 900 ;
# if defined (__AVR_ATtiny85__)
const unsigned long PresenceWaitDuration = 15 ;
const unsigned long PresenceDuration = 200 ;
const unsigned long ResetMinDuration = 480 ; // min 480us / 484us measured
const unsigned long ResetMaxDuration = 640 ; //
const unsigned long ReadBitSamplingTime = 25 ;
const unsigned long PresenceWaitDuration = 35 ; // spec 15us to 60us / 40us measured
const unsigned long SendBitDuration = 35 ;
const unsigned long PresenceDuration = 150 ; // spec 60us to 240us / 148us measured
const byte ReceiveCommand = ( byte ) - 1 ;
const unsigned long ReadBitSamplingTime = 15 ; // spec > 15us to 60us / 31us measured
void ( * timerEvent ) ( ) = 0 ;
// SendBitDuration varies the bus release time to indicate a "1"
// measured total send bit duration is 63us versus 60us in the spec
const unsigned long SendBitDuration = 20 ; // bus release time spec 15us / measured 19us
# elif defined (__AVR_ATmega328P__)
const unsigned long ResetMinDuration = 480 ;
const unsigned long ResetMaxDuration = 900 ;
const unsigned long PresenceWaitDuration = 15 ;
const unsigned long PresenceDuration = 200 ;
const unsigned long ReadBitSamplingTime = 25 ;
const unsigned long SendBitDuration = 35 ;
# endif
const byte ReceiveCommand = ( byte ) - 1 ;
void ( * timerEvent ) ( ) = 0 ;
}
OneWireSlave OWSlave ;
@ -79,7 +100,7 @@ ISR(USERTIMER_COMPA_vect) // timer1 interrupt
void OneWireSlave : : begin ( const byte * rom , byte pinNumber )
{
pin_ = Pin ( pinNumber ) ;
resetStart_ = ( unsigned long ) - 1 ;
resetStart_ = ( unsigned long ) - 1 ;
lastReset_ = 0 ;
memcpy ( rom_ , rom , 7 ) ;
@ -143,7 +164,7 @@ void OneWireSlave::beginWrite(const byte* bytes, short numBytes, void(*complete)
void OneWireSlave : : endWrite_ ( bool error , bool resetInterrupts )
{
if ( resetInterrupts )
if ( resetInterrupts )
beginWaitReset_ ( ) ;
if ( sendingClientBytes_ )
@ -231,16 +252,33 @@ byte OneWireSlave::crc8(const byte* data, short numBytes)
return crc ;
}
# if defined (__AVR_ATtiny85__)
void OneWireSlave : : setTimerEvent_ ( short delayMicroSeconds , void ( * handler ) ( ) )
{
delayMicroSeconds - = 8 ; // remove overhead (tuned on attiny85, values 0 - 10 work ok)
short skipTicks = delayMicroSeconds / 2 ; // 16mhz clock, prescaler = 32
if ( skipTicks < 1 ) skipTicks = 1 ;
timerEvent = handler ;
UserTimer_Run ( skipTicks ) ;
}
# elif defined (__AVR_ATmega328P__)
void OneWireSlave : : setTimerEvent_ ( short delayMicroSeconds , void ( * handler ) ( ) )
{
delayMicroSeconds - = 10 ; // remove overhead (tuned on Arduino Uno)
// prescaler 64
short skipTicks = ( delayMicroSeconds - 3 ) / 4 ; // round the micro seconds delay to a number of ticks to skip (4us per tick, so 4us must skip 0 tick, 8us must skip 1 tick, etc.)
if ( skipTicks < 1 ) skipTicks = 1 ;
timerEvent = handler ;
UserTimer_Run ( skipTicks ) ;
}
# endif
void OneWireSlave : : disableTimer_ ( )
{
UserTimer_Stop ( ) ;
@ -289,7 +327,7 @@ void OneWireSlave::beginResetDetectionSendZero_()
void OneWireSlave : : cancelResetDetection_ ( )
{
disableTimer_ ( ) ;
resetStart_ = ( unsigned long ) - 1 ;
resetStart_ = ( unsigned long ) - 1 ;
}
void OneWireSlave : : resetCheck_ ( )
@ -386,9 +424,11 @@ void OneWireSlave::beginWaitReset_()
disableTimer_ ( ) ;
pin_ . inputMode ( ) ;
pin_ . attachInterrupt ( & OneWireSlave : : waitReset_ , CHANGE ) ;
resetStart_ = ( unsigned int ) - 1 ;
//resetStart_ = (unsigned int) - 1;
resetStart_ = ( unsigned long ) - 1 ;
}
void OneWireSlave : : waitReset_ ( )
{
onEnterInterrupt_ ( ) ;
@ -396,22 +436,28 @@ void OneWireSlave::waitReset_()
unsigned long now = micros ( ) ;
if ( state )
{
if ( resetStart_ = = ( unsigned int ) - 1 )
if ( resetStart_ = = ( unsigned long ) - 1 )
{
onLeaveInterrupt_ ( ) ;
return ;
}
unsigned long resetDuration = now - resetStart_ ;
resetStart_ = ( unsigned int ) - 1 ;
static unsigned long resetDuration = now - resetStart_ ;
//resetStart_ = (unsigned int) - 1;
resetStart_ = ( unsigned long ) - 1 ;
if ( resetDuration > = ResetMinDuration )
{
if ( resetDuration > ResetMaxDuration )
{
ERROR ( " Reset too long " ) ;
onLeaveInterrupt_ ( ) ;
return ;
}
// The following test has been removed because of a bug which causes the value of
// resetduration to exceed ResetMaxDuration intermittently. This happens
// only on the attiny platform - possibly due to the implementation of
// micros() in the attiny core
// if (resetDuration > ResetMaxDuration)
// {
// ERROR("Reset too long");
// onLeaveInterrupt_();
// return;
// }
lastReset_ = now ;
pin_ . detachInterrupt ( ) ;
@ -420,6 +466,9 @@ void OneWireSlave::waitReset_()
endWrite_ ( true , false ) ;
if ( clientReceiveCallback_ ! = 0 )
clientReceiveCallback_ ( RE_Reset , 0 ) ;
} else
{
//ERROR("Reset too short");
}
}
else
@ -614,7 +663,7 @@ void OneWireSlave::searchRomOnBitReceived_(bool bit, bool error)
}
}
void OneWireSlave : : beginWriteBytes_ ( const byte * data , short numBytes , void ( * complete ) ( bool error ) )
void OneWireSlave : : beginWriteBytes_ ( const byte * data , short numBytes , void ( * complete ) ( bool error ) )
{
sendBuffer_ = data ;
bufferLength_ = numBytes ;
@ -661,7 +710,7 @@ void OneWireSlave::bitSent_(bool error)
beginSendBit_ ( bit , & OneWireSlave : : bitSent_ ) ;
}
void OneWireSlave : : beginReceiveBytes_ ( byte * buffer , short numBytes , void ( * complete ) ( bool error ) )
void OneWireSlave : : beginReceiveBytes_ ( byte * buffer , short numBytes , void ( * complete ) ( bool error ) )
{
recvBuffer_ = buffer ;
bufferLength_ = numBytes ;