Browse Source

search rom algorithm almost working (but code is in an awful state, and master doesn't recognize the ROM yet, maybe a CRC issue)

pull/5/head
Youen Toupin 10 years ago
parent
commit
74b3f2100e
  1. 255
      OneWireIO.ino

255
OneWireIO.ino

@ -12,10 +12,9 @@
#define PresenceWaitDuration 30 #define PresenceWaitDuration 30
#define PresenceDuration 300 #define PresenceDuration 300
#define BitZeroMinDuration 20 #define ReadBitSamplingTime 13 // the theorical time is about 30us, but given various overhead, this is the empirical delay I've found works best (on Arduino Uno)
#define BitZeroMaxDuration 75
#define SendBitDuration 30 #define SendBitDuration 35
const byte InvalidBit = (byte)-1; const byte InvalidBit = (byte)-1;
const byte IncompleteBit = (byte)-2; const byte IncompleteBit = (byte)-2;
@ -30,7 +29,6 @@ enum OwStatus
{ {
OS_WaitReset, OS_WaitReset,
OS_Presence, OS_Presence,
OS_AfterPresence,
OS_WaitCommand, OS_WaitCommand,
OS_SearchRom, OS_SearchRom,
}; };
@ -55,15 +53,7 @@ void owRelease()
owOutTestPin.writeHigh(); owOutTestPin.writeHigh();
} }
void owWrite(bool value) volatile unsigned long resetStart = (unsigned long)-1;
{
if (value)
owRelease();
else
owPullLow();
}
unsigned long resetStart = (unsigned long)-1;
unsigned long lastReset = (unsigned long)-1; unsigned long lastReset = (unsigned long)-1;
unsigned long bitStart = (unsigned long)-1; unsigned long bitStart = (unsigned long)-1;
byte receivingByte = 0; byte receivingByte = 0;
@ -79,7 +69,7 @@ void setup()
led.writeLow(); led.writeLow();
cli(); // disable interrupts cli(); // disable interrupts
attachInterrupt(InterruptNumber,onewireInterrupt,CHANGE); attachInterrupt(InterruptNumber,onewireInterrupt,FALLING);
// set timer0 interrupt at 250KHz (actually depends on compare match register OCR0A) // set timer0 interrupt at 250KHz (actually depends on compare match register OCR0A)
// 4us between each tick // 4us between each tick
@ -104,6 +94,8 @@ void loop()
sei();//enable interrupts sei();//enable interrupts
SerialChannel::flush(); SerialChannel::flush();
owHandleReset();
} }
void(*timerEvent)() = 0; void(*timerEvent)() = 0;
@ -127,200 +119,195 @@ void owError(const char* message)
status = OS_WaitReset; status = OS_WaitReset;
} }
void owResetError() void owClearError()
{ {
led.writeLow(); led.writeLow();
} }
void onewireInterrupt(void) void owHandleReset()
{ {
bool state = owPin.read(); unsigned long localResetStart = resetStart;
unsigned long now = micros(); if (owPin.read())
//led.write(state);
if (!state)
resetStart = now;
// handle reset
if (state)
{ {
unsigned long resetDuration = resetStart == (unsigned long)-1 ? (unsigned long)-1 : now - resetStart;
resetStart = (unsigned long)-1; resetStart = (unsigned long)-1;
lastReset = now; }
else if (localResetStart != (unsigned long)-1)
if (resetDuration >= ResetMinDuration && resetDuration <= ResetMaxDuration) {
unsigned long resetDuration = micros() - localResetStart;
if (resetDuration >= ResetMinDuration)
{
// wait for master to release the pin (or timeout if the pin is pulled low for too long)
unsigned long now = micros();
while (!owPin.read())
{ {
//debug.SC_APPEND_STR_TIME("reset", now); if (resetStart != localResetStart)
owResetError(); return;
now = micros();
if (now - localResetStart > ResetMaxDuration)
{
owError("Reset too long");
return;
}
}
cli();
owClearError();
lastReset = now;
status = OS_Presence; status = OS_Presence;
setTimerEvent(PresenceWaitDuration - (micros() - now), &beginPresence); setTimerEvent(PresenceWaitDuration - (micros() - now), &beginPresence);
return; sei();
}
} }
} }
if (status == OS_AfterPresence) void onewireInterrupt()
{ {
status = OS_WaitCommand; //owOutTestPin.writeLow();
receivingByte = 0; onewireInterruptImpl();
receivingBitPos = 0; //owOutTestPin.writeHigh();
bitStart = (unsigned long)-1; }
if (state) //bool debugState = false;
volatile unsigned long lastInterrupt = 0;
void onewireInterruptImpl(void)
{ {
// this is the rising edge of end-of-presence ; don't try to interpret it as a bit unsigned long now = micros();
return; if (now < lastInterrupt + 20)
} return; // don't react to our own actions
} lastInterrupt = now;
//debugState = !debugState;
//owOutTestPin.write(debugState);
//led.write(state);
resetStart = now;
// read bytes // read bytes
if (status == OS_WaitCommand) switch (status) {
case OS_WaitCommand:
{ {
byte bit = interpretReceivedEdgeAsBit(state, now); bool bit = readBit();
if (bit != IncompleteBit)
{
if (bit == InvalidBit)
return;
receivingByte |= (bit << receivingBitPos); receivingByte |= ((bit ? 1 : 0) << receivingBitPos);
++receivingBitPos; ++receivingBitPos;
if (receivingBitPos == 8) if (receivingBitPos == 8)
{ {
debug.SC_APPEND_STR_INT("received byte", (long)receivingByte); byte receivedByte = receivingByte;
receivingBitPos = 0;
receivingByte = 0;
//debug.SC_APPEND_STR_INT("received byte", (long)receivedByte);
if (status == OS_WaitCommand && receivingByte == 0xF0) if (status == OS_WaitCommand && receivedByte == 0xF0)
{ {
status = OS_SearchRom; status = OS_SearchRom;
searchROMReadingMasterResponseBit = false; searchROMReadingMasterResponseBit = false;
searchROMSendingInverse = false; searchROMSendingInverse = false;
searchROMCurrentByte = 0; searchROMCurrentByte = 0;
searchROMCurrentBit = 0; searchROMCurrentBit = 0;
attachInterrupt(InterruptNumber, onewireInterruptSearchROM, FALLING);
return; return;
} }
receivingBitPos = 0;
receivingByte = 0;
}
}
} }
} break;
if (status == OS_SearchRom) case OS_SearchRom:
{ {
byte currentByte = owROM[searchROMCurrentByte];
bool currentBit = bitRead(currentByte, searchROMCurrentBit);
if (searchROMReadingMasterResponseBit) } break;
{
byte bit = interpretReceivedEdgeAsBit(state, now);
if (bit != IncompleteBit)
{
searchROMReadingMasterResponseBit = false;
if (bit == InvalidBit)
return;
if ((bit == 1) != currentBit)
{
debug.SC_APPEND_STR_TIME("Master didn't send our bit, leaving ROM search", now);
status = OS_WaitReset;
}
} }
} }
else
bool ignoreNextFallingEdge = false;
void onewireInterruptSearchROM()
{ {
if (!state) /*unsigned long now = micros();
if (now < lastInterrupt + 20)
return; // don't react to our own actions
lastInterrupt = now;*/
if (ignoreNextFallingEdge)
{ {
// master is pulling low, we must send our bit ignoreNextFallingEdge = false;
bool bitToSend = searchROMSendingInverse ? !currentBit : currentBit; return;
sendBit(bitToSend); }
if (bitToSend)
debug.SC_APPEND_STR_TIME("Sent ROM search bit : 1", now);
else
debug.SC_APPEND_STR_TIME("Sent ROM search bit : 0", now);
if (searchROMSendingInverse) if (searchROMReadingMasterResponseBit)
{ {
searchROMSendingInverse = false; bool bit = readBit();
searchROMReadingMasterResponseBit = true;
} byte currentByte = owROM[searchROMCurrentByte];
else bool currentBit = bitRead(currentByte, searchROMCurrentBit);
if (bit != currentBit)
{ {
searchROMSendingInverse = true; debug.SC_APPEND_STR("Master didn't send our bit, leaving ROM search");
status = OS_WaitReset;
attachInterrupt(InterruptNumber, onewireInterrupt, FALLING);
return;
} }
searchROMReadingMasterResponseBit = false;
++searchROMCurrentBit;
if (searchROMCurrentBit == 8) if (searchROMCurrentBit == 8)
{ {
++searchROMCurrentByte; ++searchROMCurrentByte;
searchROMCurrentBit = 0; searchROMCurrentBit = 0;
debug.SC_APPEND_STR_TIME("sent another ROM byte", now); debug.SC_APPEND_STR("sent another ROM byte");
} }
if (searchROMCurrentByte == 8) if (searchROMCurrentByte == 8)
{ {
searchROMCurrentByte = 0; searchROMCurrentByte = 0;
status = OS_WaitReset; status = OS_WaitReset;
debug.SC_APPEND_STR_TIME("ROM sent entirely", now); debug.SC_APPEND_STR("ROM sent entirely");
attachInterrupt(InterruptNumber, onewireInterrupt, FALLING);
return;
} }
} }
else else
{ {
debug.SC_APPEND_STR_TIME("Search ROM : seen rising edge", now); byte currentByte = owROM[searchROMCurrentByte];
} bool currentBit = bitRead(currentByte, searchROMCurrentBit);
} //bool currentBit = 0;
}
}
byte interpretReceivedEdgeAsBit(bool wireState, unsigned long now) bool bitToSend = searchROMSendingInverse ? !currentBit : currentBit;
{ sendBit(bitToSend);
if (!wireState) /*if (bitToSend)
{ debug.SC_APPEND_STR("sent ROM search bit : 1");
// master just pulled low, this is a bit start
//debug.SC_APPEND_STR_TIME("bit start", now);
bitStart = now;
return IncompleteBit;
}
else else
{ debug.SC_APPEND_STR("sent ROM search bit : 0");*/
if (bitStart == (unsigned long)-1)
{
//owError("Invalid read sequence");
//return InvalidBit;
// we missed the falling edge, we emulate one here (this can happen if handling of rising edge interrupt takes too long) if (searchROMSendingInverse)
bitStart = now;
}
// master released the line, we interpret it as a bit 1 or 0 depending on timing
unsigned long bitLength = now - bitStart;
bitStart = (unsigned long)-1;
if (bitLength < BitZeroMinDuration)
{
// received bit = 1
//debug.SC_APPEND_STR_TIME("received bit 1", now);
return 1;
}
else if (bitLength < BitZeroMaxDuration)
{ {
// received bit = 0 searchROMSendingInverse = false;
//debug.SC_APPEND_STR_TIME("received bit 0", now); searchROMReadingMasterResponseBit = true;
return 0;
} }
else else
{ {
// this is not a valid bit searchROMSendingInverse = true;
owError("Invalid read timing");
return InvalidBit;
} }
} }
} }
bool readBit()
{
delayMicroseconds(ReadBitSamplingTime);
return owPin.read();
}
void sendBit(bool bit) void sendBit(bool bit)
{ {
if (!bit) if (bit)
{
delayMicroseconds(SendBitDuration);
}
else
{ {
owPullLow(); owPullLow();
delayMicroseconds(SendBitDuration); delayMicroseconds(SendBitDuration);
owRelease(); owRelease();
ignoreNextFallingEdge = true;
} }
} }
@ -328,7 +315,6 @@ void beginPresence()
{ {
unsigned long now = micros(); unsigned long now = micros();
owPullLow(); owPullLow();
owOutTestPin.writeLow();
setTimerEvent(PresenceDuration, &endPresence); setTimerEvent(PresenceDuration, &endPresence);
debug.SC_APPEND_STR_TIME("reset", lastReset); debug.SC_APPEND_STR_TIME("reset", lastReset);
debug.SC_APPEND_STR_TIME("beginPresence", now); debug.SC_APPEND_STR_TIME("beginPresence", now);
@ -340,7 +326,10 @@ void endPresence()
owRelease(); owRelease();
debug.SC_APPEND_STR_TIME("endPresence", now); debug.SC_APPEND_STR_TIME("endPresence", now);
status = OS_AfterPresence; status = OS_WaitCommand;
receivingByte = 0;
receivingBitPos = 0;
bitStart = (unsigned long)-1;
} }
ISR(TIMER1_COMPA_vect) // timer1 interrupt ISR(TIMER1_COMPA_vect) // timer1 interrupt

Loading…
Cancel
Save