From 514bb31cdd06bfd641c1c9d523e4006756a7ac01 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Thu, 4 Feb 2021 11:43:13 +0100 Subject: [PATCH] Distunguish between in/out of FASTPIN --- DCCWaveform.cpp | 9 ++++++++- MotorDriver.cpp | 24 +++++++++++++----------- MotorDriver.h | 8 +++++--- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 8344995..ec5d0bc 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -119,6 +119,11 @@ void DCCWaveform::checkPowerOverload() { case POWERMODE::ON: // Check current lastCurrent=motorDriver->getCurrentRaw(); + if (lastCurrent < 0) { + // We have a fault pin condition to take care of + DIAG(F("\n*** %S FAULT PIN ACTIVE TOGGLE POWER ON THIS OR BOTH TRACKS ***\n"), isMainTrack ? F("MAIN") : F("PROG")); + lastCurrent = -lastCurrent; + } if (lastCurrent <= tripValue) { sampleDelay = POWER_SAMPLE_ON_WAIT; if(power_good_counter<100) @@ -129,9 +134,9 @@ void DCCWaveform::checkPowerOverload() { setPowerMode(POWERMODE::OVERLOAD); unsigned int mA=motorDriver->raw2mA(lastCurrent); unsigned int maxmA=motorDriver->raw2mA(tripValue); - DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d offtime=%l ***\n"), isMainTrack ? F("MAIN") : F("PROG"), mA, maxmA, power_sample_overload_wait); power_good_counter=0; sampleDelay = power_sample_overload_wait; + DIAG(F("\n*** %S TRACK POWER OVERLOAD current=%d max=%d offtime=%d ***\n"), isMainTrack ? F("MAIN") : F("PROG"), mA, maxmA, sampleDelay); if (power_sample_overload_wait >= 10000) power_sample_overload_wait = 10000; else @@ -142,6 +147,8 @@ void DCCWaveform::checkPowerOverload() { // Try setting it back on after the OVERLOAD_WAIT setPowerMode(POWERMODE::ON); sampleDelay = POWER_SAMPLE_ON_WAIT; + // Debug code.... + DIAG(F("\n*** %S TRACK POWER RESET delay=%d ***\n"), isMainTrack ? F("MAIN") : F("PROG"), sampleDelay); break; default: sampleDelay = 999; // cant get here..meaningless statement to avoid compiler warning. diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 071417a..96f43f3 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -20,9 +20,9 @@ #include "MotorDriver.h" #include "DIAG.h" -#define setHIGH(fastpin) *fastpin.out |= fastpin.maskHIGH -#define setLOW(fastpin) *fastpin.out &= fastpin.maskLOW -#define isHIGH(fastpin) (*fastpin.out & fastpin.maskHIGH) +#define setHIGH(fastpin) *fastpin.inout |= fastpin.maskHIGH +#define setLOW(fastpin) *fastpin.inout &= fastpin.maskLOW +#define isHIGH(fastpin) (*fastpin.inout & fastpin.maskHIGH) #define isLOW(fastpin) (!isHIGH(fastpin)) @@ -59,14 +59,13 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8 faultPin=fault_pin; if (faultPin != UNUSED_PIN) { - getFastPin(F("FAULT"),faultPin,fastFaultPin); + getFastPin(F("FAULT"),faultPin, 1 /*input*/, fastFaultPin); pinMode(faultPin, INPUT); } senseFactor=sense_factor; tripMilliamps=trip_milliamps; rawCurrentTripValue=(int)(trip_milliamps / sense_factor); - simulatedOverload=(int)(32000/senseFactor); } void MotorDriver::setPower(bool on) { @@ -107,13 +106,13 @@ void MotorDriver::setSignal( bool high) { int MotorDriver::getCurrentRaw() { + int current = analogRead(currentPin); if (faultPin != UNUSED_PIN && isLOW(fastFaultPin) && isHIGH(fastPowerPin)) - return simulatedOverload; - + return -current; + return current; // IMPORTANT: This function can 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. - return analogRead(currentPin); } unsigned int MotorDriver::raw2mA( int raw) { @@ -123,11 +122,14 @@ int MotorDriver::mA2raw( unsigned int mA) { return (int)(mA / senseFactor); } -void MotorDriver::getFastPin(const FSH* type,int pin, FASTPIN & result) { +void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & result) { DIAG(F("\nMotorDriver %S Pin=%d,"),type,pin); uint8_t port = digitalPinToPort(pin); - result.out = portOutputRegister(port); + if (input) + result.inout = portInputRegister(port); + else + result.inout = portOutputRegister(port); result.maskHIGH = digitalPinToBitMask(pin); result.maskLOW = ~result.maskHIGH; - DIAG(F(" port=0x%x, out=0x%x, mask=0x%x\n"),port, result.out,result.maskHIGH); + DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x\n"),port, result.inout,input,result.maskHIGH); } diff --git a/MotorDriver.h b/MotorDriver.h index 093c895..b8bef7d 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -27,7 +27,7 @@ #endif struct FASTPIN { - volatile uint8_t *out; + volatile uint8_t *inout; uint8_t maskHIGH; uint8_t maskLOW; }; @@ -46,7 +46,10 @@ class MotorDriver { } private: - void getFastPin(const FSH* type,int pin, FASTPIN & result); + void getFastPin(const FSH* type,int pin, bool input, FASTPIN & result); + void getFastPin(const FSH* type,int pin, FASTPIN & result) { + getFastPin(type, pin, 0, result); + } byte powerPin, signalPin, signalPin2, currentPin, faultPin, brakePin; FASTPIN fastPowerPin,fastSignalPin, fastSignalPin2, fastBrakePin,fastFaultPin; bool dualSignal; // true to use signalPin2 @@ -54,6 +57,5 @@ class MotorDriver { float senseFactor; unsigned int tripMilliamps; int rawCurrentTripValue; - int simulatedOverload; }; #endif