From 3d7a9500cfba08cc27f4211bbbeecb1b822612ac Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Fri, 3 Jul 2020 00:19:59 +0200 Subject: [PATCH 1/2] First test on Uno --- CVReader.ino | 13 +++++++++---- Config.h | 5 ++--- DCCEXParser.cpp | 4 ++++ Hardware.cpp | 6 +++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CVReader.ino b/CVReader.ino index 9ba6724..9b2edd8 100644 --- a/CVReader.ino +++ b/CVReader.ino @@ -22,10 +22,10 @@ void myFilter(Print & stream, byte & opcode, byte & paramCount, int p[]) { DCC::setFn(p[0],p[1],p[2]==1); opcode=0; // tell parser to ignore this command break; - case '#': // Diagnose parser <#....> - DIAG(F("# paramCount=%d\n"),paramCount); + case '$': // Diagnose parser <$....> + DIAG(F("$ paramCount=%d\n"),paramCount); for (int i=0;i + StringFormatter::send(stream,F("<# %d>"), MAX_LOCOS); + return; default: //anything else will drop out to break; diff --git a/Hardware.cpp b/Hardware.cpp index e9eb218..04bf3d5 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -40,14 +40,14 @@ void Hardware::setSignal(bool isMainTrack, bool high) { } int Hardware::getCurrentMilliamps(bool isMainTrack) { - int pin = isMainTrack ? MAIN_SENSE_PIN : PROG_SENSE_PIN; + byte pin = isMainTrack ? MAIN_SENSE_PIN : PROG_SENSE_PIN; float factor = isMainTrack ? MAIN_SENSE_FACTOR : PROG_SENSE_FACTOR; - + // IMPORTANT: This function can be called in Interrupt() time within the 56uS timer // The default analogRead takes ~100uS which is catastrphic // so analogReadFast is used here. (-2uS) int rawCurrent = analogReadFast(pin); - + return (int)(rawCurrent * factor); } From 0c9b2d962bcfa30fd6f1f9497ec0615c521f8eeb Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Fri, 3 Jul 2020 16:03:10 +0200 Subject: [PATCH 2/2] Correct place for checkAck() so ack pulse lenth is correct --- DCCWaveform.cpp | 14 +++++--------- DCCWaveform.h | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index f9e45b5..c696575 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -135,8 +135,9 @@ bool DCCWaveform::interrupt1() { state = 0; break; } + // ACK check is prog track only + if (ackPending) checkAck(); return false; - } @@ -185,11 +186,6 @@ void DCCWaveform::interrupt2() { } } } - - // ACK check is prog track only and will only be checked if bits_sent=4 ... - // This means only once per 9-bit-byte AND never at the same cycle as the - // relatively expensive packet change code just above. - if (ackPending && bits_sent==4) checkAck(); } @@ -265,13 +261,13 @@ void DCCWaveform::checkAck() { // detected trailing edge of pulse ackPulseDuration=micros()-ackPulseStart; - - if (ackPulseDuration>1000 && ackPulseDuration<9000) { + + if (ackPulseDuration>3000 && ackPulseDuration<8500) { ackCheckDuration=millis()-ackCheckStart; ackDetected=true; ackPending=false; transmitRepeats=0; // shortcut remaining repeat packets return; // we have a genuine ACK result - } + } ackPulseStart=0; // We have detected a too-short or too-long pulse so ignore and wait for next leading edge } diff --git a/DCCWaveform.h b/DCCWaveform.h index 5c6cd15..fff524d 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -2,7 +2,7 @@ #define DCCWaveform_h -const int POWER_SAMPLE_MAX = 300; +const int POWER_SAMPLE_MAX = 1000; // XXX only until correct short detection on prog rail is implemented const int POWER_SAMPLE_ON_WAIT = 100; const int POWER_SAMPLE_OFF_WAIT = 1000; const int POWER_SAMPLE_OVERLOAD_WAIT = 4000;