From b80d7bd5170737a1207d97be70ff88161468d1ca Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 21 May 2023 11:54:46 +0200 Subject: [PATCH] Pin handling supports pins up to 254 --- GITHUB_SHA.h | 2 +- MotorDriver.cpp | 33 ++++++++++++++++++++++++--------- MotorDriver.h | 7 ++++--- MotorDrivers.h | 2 +- version.h | 3 ++- 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h index 267e562..19661bf 100644 --- a/GITHUB_SHA.h +++ b/GITHUB_SHA.h @@ -1 +1 @@ -#define GITHUB_SHA "devel-202305202114Z" +#define GITHUB_SHA "devel-202305210948Z" diff --git a/MotorDriver.cpp b/MotorDriver.cpp index 603549e..c27f777 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -33,9 +33,10 @@ volatile portreg_t shadowPORTA; volatile portreg_t shadowPORTB; volatile portreg_t shadowPORTC; -MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, - byte current_pin, float sense_factor, unsigned int trip_milliamps, int8_t fault_pin) { - powerPin=power_pin; +MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int16_t brake_pin, + byte current_pin, float sense_factor, unsigned int trip_milliamps, int16_t fault_pin) { + bool pinWarning = false; + invertPower=power_pin < 0; if (invertPower) { powerPin = 0-power_pin; @@ -91,28 +92,38 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i } else dualSignal=false; - brakePin=brake_pin; if (brake_pin!=UNUSED_PIN){ invertBrake=brake_pin < 0; - brakePin=invertBrake ? 0-brake_pin : brake_pin; + if (invertBrake) + brake_pin = 0-brake_pin; + if (brake_pin > MAX_PIN) + pinWarning = true; + brakePin=(byte)brake_pin; getFastPin(F("BRAKE"),brakePin,fastBrakePin); // if brake is used for railcom cutout we need to do PORTX register trick here as well pinMode(brakePin, OUTPUT); setBrake(true); // start with brake on in case we hace DC stuff going on + } else { + brakePin=UNUSED_PIN; } - else brakePin=UNUSED_PIN; currentPin=current_pin; - if (currentPin!=UNUSED_PIN) ADCee::init(currentPin); + if (currentPin!=UNUSED_PIN) + ADCee::init(currentPin); senseOffset=0; // value can not be obtained until waveform is activated - faultPin=fault_pin; if (faultPin != UNUSED_PIN) { invertFault=fault_pin < 0; - faultPin=invertFault ? 0-fault_pin : fault_pin; + if (invertFault) + fault_pin = 0-fault_pin; + if (fault_pin > MAX_PIN) + pinWarning = true; + faultPin=(byte)fault_pin; DIAG(F("Fault pin = %d invert %d"), faultPin, invertFault); getFastPin(F("FAULT"),faultPin, 1 /*input*/, fastFaultPin); pinMode(faultPin, INPUT); + } else { + faultPin=UNUSED_PIN; } // This conversion performed at compile time so the remainder of the code never needs @@ -143,6 +154,10 @@ MotorDriver::MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, i // senseFactorInternal, raw2mA(1000),mA2raw(1000)); } + // give general warning if pin values out of range were encountered + if (pinWarning) + DIAG(F("** WARNING ** Pin values > 255")); + // prepare values for current detection sampleDelay = 0; lastSampleTaken = millis(); diff --git a/MotorDriver.h b/MotorDriver.h index 743e579..be77d2d 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -74,8 +74,9 @@ // Virtualised Motor shield 1-track hardware Interface #ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h -#define UNUSED_PIN 127 // inside int8_t +#define UNUSED_PIN 255 // inside uint8_t #endif +#define MAX_PIN 254 class pinpair { public: @@ -111,8 +112,8 @@ enum class POWERMODE : byte { OFF, ON, OVERLOAD }; class MotorDriver { public: - MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, - byte current_pin, float senseFactor, unsigned int tripMilliamps, int8_t fault_pin); + MotorDriver(int16_t power_pin, byte signal_pin, byte signal_pin2, int16_t brake_pin, + byte current_pin, float senseFactor, unsigned int tripMilliamps, int16_t fault_pin); void setPower( POWERMODE mode); POWERMODE getPower() { return powerMode;} // as the port registers can be shadowed to get syncronized DCC signals diff --git a/MotorDrivers.h b/MotorDrivers.h index a40c4f1..907c11b 100644 --- a/MotorDrivers.h +++ b/MotorDrivers.h @@ -36,7 +36,7 @@ // custom defines in config.h. #ifndef UNUSED_PIN // sync define with the one in MotorDriver.h -#define UNUSED_PIN 127 // inside int8_t +#define UNUSED_PIN 255 // inside uint8_t #endif // The MotorDriver definition is: diff --git a/version.h b/version.h index 9ddacd2..bdfbacb 100644 --- a/version.h +++ b/version.h @@ -4,7 +4,8 @@ #include "StringFormatter.h" -#define VERSION "4.2.53" +#define VERSION "4.2.54pre1" +// 4.2.54 - Fix: Pin handling supports pins up to 254 // 4.2.53 - Fix: Fault pin handling made more straight forward // 4.2.52 - Experimental support for sabertooth motor controller on ESP32 // 4.2.51 - Add DISABLE_PROG to disable programming to save RAM/Flash