From 1afb4753ec66feffc397ecb32d778fc829a3e779 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Wed, 23 Feb 2022 16:03:15 +0000 Subject: [PATCH] Cleaner ack check And drop CPU specific stuff no longer needed. --- DCCACK.cpp | 2 +- MotorDriver.cpp | 30 +++++++++++++++--------------- MotorDriver.h | 1 + 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/DCCACK.cpp b/DCCACK.cpp index 9198893..8afe0f9 100644 --- a/DCCACK.cpp +++ b/DCCACK.cpp @@ -423,7 +423,7 @@ void DCCACK::checkAck(byte sentResetsSincePacket) { return; } - int current=progDriver->getCurrentRaw(); + int current=progDriver->getCurrentRawInInterrupt(); numAckSamples++; if (current > ackMaxCurrent) ackMaxCurrent=current; // An ACK is a pulse lasting between minAckPulseDuration and maxAckPulseDuration uSecs (refer @haba) diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 79167bc..c441f8a 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -156,28 +156,28 @@ bool MotorDriver::canMeasureCurrent() { int MotorDriver::getCurrentRaw() { if (currentPin==UNUSED_PIN) return 0; int current; -#if defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41) - bool irq = disableInterrupts(); + // This function should NOT be called in an interruot so we + // dont need to fart about saving and restoring CPU specific + // interrupt registers. + noInterrupts(); current = analogRead(currentPin)-senseOffset; - enableInterrupts(irq); -#else // Uno, Mega and all the TEENSY3* but not TEENSY4* - unsigned char sreg_backup; - sreg_backup = SREG; /* save interrupt enable/disable state */ - cli(); - current = analogRead(currentPin)-senseOffset; -#if defined(ARDUINO_TEENSY32) || defined(ARDUINO_TEENSY35)|| defined(ARDUINO_TEENSY36) - overflow_count = 0; -#endif - if (sreg_backup & 128) sei(); /* restore interrupt state */ -#endif // outer # + interrupts(); if (current<0) current=0-current; if ((faultPin != UNUSED_PIN) && isLOW(fastFaultPin) && isHIGH(fastPowerPin)) return (current == 0 ? -1 : -current); return current; - // IMPORTANT: This function can be called in Interrupt() time within the 56uS timer + +} + +int MotorDriver::getCurrentRawInInterrupt() { + + // IMPORTANT: This function must be called in Interrupt() time within the 56uS timer // The default analogRead takes ~100uS which is catastrphic // so DCCTimer has set the sample time to be much faster. -} + + if (currentPin==UNUSED_PIN) return 0; + return analogRead(currentPin)-senseOffset; +} unsigned int MotorDriver::raw2mA( int raw) { return (unsigned int)(raw * senseFactor); diff --git a/MotorDriver.h b/MotorDriver.h index 105bff6..80453ea 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -55,6 +55,7 @@ class MotorDriver { virtual void setSignal( bool high); virtual void setBrake( bool on); virtual int getCurrentRaw(); + virtual int getCurrentRawInInterrupt(); virtual unsigned int raw2mA( int raw); virtual int mA2raw( unsigned int mA); inline int getRawCurrentTripValue() {