Skip to content

Commit

Permalink
dcc debug produces correct bit pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
habazut committed Dec 25, 2024
1 parent afef55b commit 6abf1a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
13 changes: 10 additions & 3 deletions CommandStation-EX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,15 @@ void loopdiag(unsigned long timeout)
if (timeout != 0) {
unsigned long diff = now - lasttimestamp;
if (diff > timeout) {
if (DCCSniffer)
DIAG(F("ticks=%L"), DCCSniffer->getTicks());
if (DCCSniffer){
uint64_t val = DCCSniffer->getDebug();
int n = 64;
Serial.print("< ");
while (n--) {
Serial.print(val&(1ULL<<n)?"1":"0");
}
Serial.println(" >");
}
lasttimestamp = millis();
return;
}
Expand All @@ -179,7 +186,7 @@ void loopdiag(unsigned long timeout)
void loop()
{
// Some debug for sniffer code
// loopdiag(937); // Do not use a value that does divide even in 80Mhz ticks
loopdiag(937); // Do not use a value that does divide even in 80Mhz ticks
digitalWrite(2,LOW);

// The main sketch has responsibilities during loop()
Expand Down
31 changes: 23 additions & 8 deletions Sniffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ Sniffer::Sniffer(byte snifferpin) {
#define DCC_TOO_SHORT 4000L // 4000 ticks are 50usec
#define DCC_ONE_LIMIT 6400L // 6400 ticks are 80usec

volatile int fakecounter = 0;

void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
bool bit = 0;
if (fakecounter >= 64)
fakecounter = 0;
fakecounter++;
byte bit = 0;
diffticks = capticks - lastticks;
if (lastedge != posedge) {
if (diffticks < DCC_TOO_SHORT) {
Expand All @@ -81,11 +86,16 @@ void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
bit = 1;
} else {
bit = 0;
}
}/*
if (fakecounter == 7 || fakecounter == 34 || fakecounter == 62 || fakecounter == 63) {
bit = 0;
} else {
bit = 1;
}*/
lastticks = capticks;
lastedge = posedge;
bitfield = bitfield << 1;
bitfield = bitfield + bit;
bitfield = bitfield << (uint64_t)1;
bitfield = bitfield + (uint64_t)bit;

// now the halfbit is in the bitfield. Analyze...

Expand All @@ -94,7 +104,6 @@ void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
// and detects a preamble if
// 22 are ONE and 2 are ZERO which is a
// preabmle of 11 ONES and one ZERO
digitalWrite(2,HIGH);
if (inpacket) {
// if we are already inpacket here we
// got a preamble in the middle of a
Expand All @@ -104,13 +113,17 @@ void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
currentbyte = 0;
dcclen = 0;
inpacket = true;
halfbitcounter = 17; // count 18 steps from 17 to 0 and then look at the byte
halfbitcounter = 18; // count 18 steps from 17 to 0 and then look at the byte
digitalWrite(2,HIGH);
return;
}
if (inpacket) {
if (halfbitcounter--) {
halfbitcounter--;
if (halfbitcounter) {
return; // wait until we have full byte
} else {
// have reached end of byte
//if (currentbyte == 2) debugfield = bitfield;
byte twohalfbits = bitfield & 0x03;
switch (twohalfbits) {
case 0x01:
Expand All @@ -132,10 +145,12 @@ void IRAM_ATTR Sniffer::processInterrupt(int32_t capticks, bool posedge) {
}
if (twohalfbits == 0x03) {
inpacket = false;
dcclen = currentbyte;
dcclen = currentbyte+1;
debugfield = bitfield;
}
break;
}
halfbitcounter = 18;
currentbyte++; // everything done for this end of byte
}
}
Expand Down
7 changes: 7 additions & 0 deletions Sniffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ class Sniffer {
interrupts();
return i;
};
inline int64_t getDebug() {
noInterrupts();
int64_t i = debugfield;
interrupts();
return i;
};
private:
uint64_t bitfield = 0;
uint64_t debugfield = 0;
int32_t diffticks;
int32_t lastticks;
bool lastedge;
Expand Down

0 comments on commit 6abf1a4

Please sign in to comment.