From 5e616a9eb2202c9230de540a3eaf6d44b77eea2e Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sun, 2 Oct 2022 22:53:35 +0200 Subject: [PATCH] make compile for other arch but AVR --- DCCTimerAVR.cpp | 8 +++----- DCCWaveform.cpp | 2 ++ GITHUB_SHA.h | 2 +- MotorDriver.cpp | 42 ++++++++++++++---------------------------- MotorDriver.h | 17 +++++++++++++---- TrackManager.cpp | 2 ++ defines.h | 7 ++++++- 7 files changed, 41 insertions(+), 39 deletions(-) diff --git a/DCCTimerAVR.cpp b/DCCTimerAVR.cpp index c82afe5..dfdbeee 100644 --- a/DCCTimerAVR.cpp +++ b/DCCTimerAVR.cpp @@ -1,6 +1,6 @@ /* * © 2021 Mike S - * © 2021 Harald Barth + * © 2021-2022 Harald Barth * © 2021 Fred Decker * © 2021 Chris Harlow * © 2021 David Cutting @@ -48,10 +48,8 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) { // ADCSRA = (ADCSRA & 0b11111000) | 0b00000100; // speed up analogRead sample time // Set up ADC for free running mode ADMUX=(1< 3) DIAG(F("Current sample old %d"), millis() - sampleCurrentTimestamp); - //interrupts(); -#endif +#else + if (!fromISR) noInterrupts(); + current = analogRead(currentPin)-senseOffset; + if (!fromISR) interrupts(); +#endif //ANALOG_READ_INTERRUPT +#endif //ARDUINO_ARCH_ESP32 if (current<0) current=0-current; if ((faultPin != UNUSED_PIN) && isLOW(fastFaultPin) && powerMode==POWERMODE::ON) return (current == 0 ? -1 : -current); return current; } + +#ifdef ANALOG_READ_INTERRUPT /* * This should only be called in interrupt context * Copies current value from HW to cached value in @@ -245,11 +249,6 @@ int MotorDriver::getCurrentRaw() { #pragma GCC push_options #pragma GCC optimize ("-O3") bool MotorDriver::sampleCurrentFromHW() { -#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) - const byte mask = 7; -#else - const byte mask = 31; -#endif byte low, high; //if (!bit_is_set(ADCSRA, ADIF)) if (bit_is_set(ADCSRA, ADSC)) @@ -273,6 +272,7 @@ void MotorDriver::startCurrentFromHW() { bitSet(ADCSRA,ADSC); // start conversion } #pragma GCC pop_options +#endif //ANALOG_READ_INTERRUPT void MotorDriver::setDCSignal(byte speedcode) { if (brakePin == UNUSED_PIN) @@ -327,20 +327,6 @@ void MotorDriver::setDCSignal(byte speedcode) { } } -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; -#ifdef ARDUINO_ARCH_ESP32 //On ESP we do all in loop() instead of in interrupt - return getCurrentRaw(); -#else - //return analogRead(currentPin)-senseOffset; - return getCurrentRaw(); -#endif -} - unsigned int MotorDriver::raw2mA( int raw) { return (int32_t)raw * senseFactorInternal / senseScale; } diff --git a/MotorDriver.h b/MotorDriver.h index 91106e9..9164b0a 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -153,8 +153,12 @@ class MotorDriver { setDCSignal(128); #endif }; - int getCurrentRaw(); - int getCurrentRawInInterrupt(); + int getCurrentRaw() { + return getCurrentRaw(false); + } + int getCurrentRawInInterrupt() { + return getCurrentRaw(true); + }; unsigned int raw2mA( int raw); unsigned int mA2raw( unsigned int mA); inline bool brakeCanPWM() { @@ -183,9 +187,12 @@ class MotorDriver { isProgTrack = on; } void checkPowerOverload(bool useProgLimit, byte trackno); +#ifdef ANALOG_READ_INTERRUPT bool sampleCurrentFromHW(); void startCurrentFromHW(); +#endif private: + int getCurrentRaw(bool fromISR); bool isProgTrack = false; // tells us if this is a prog track void getFastPin(const FSH* type,int pin, bool input, FASTPIN & result); void getFastPin(const FSH* type,int pin, FASTPIN & result) { @@ -214,8 +221,10 @@ class MotorDriver { unsigned int sampleDelay; int progTripValue; int lastCurrent; - unsigned long sampleCurrentTimestamp; - uint16_t sampleCurrent; +#ifdef ANALOG_READ_INTERRUPT + volatile unsigned long sampleCurrentTimestamp; + volatile uint16_t sampleCurrent; +#endif int maxmA; int tripmA; diff --git a/TrackManager.cpp b/TrackManager.cpp index e596d8a..cbb672a 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -54,6 +54,7 @@ int16_t TrackManager::joinRelay=UNUSED_PIN; byte TrackManager::tempProgTrack=MAX_TRACKS+1; #endif +#ifdef ANALOG_READ_INTERRUPT /* * sampleCurrent() runs from Interrupt */ @@ -95,6 +96,7 @@ void TrackManager::sampleCurrent() { } } } +#endif // The setup call is done this way so that the tracks can be in a list // from the config... the tracks default to NULL in the declaration diff --git a/defines.h b/defines.h index ba96b4e..bc99413 100644 --- a/defines.h +++ b/defines.h @@ -3,7 +3,7 @@ * © 2021 Neil McKechnie * © 2021 Mike S * © 2021 Fred Decker - * © 2020-2021 Harald Barth + * © 2020-2022 Harald Barth * © 2020-2021 Chris Harlow * * This file is part of CommandStation-EX @@ -43,6 +43,11 @@ #undef USB_SERIAL // Teensy has this defined by default... #define USB_SERIAL Serial +// All AVRs must read analog values from the DCC timer interrupt +#ifdef ARDUINO_ARCH_AVR +#define ANALOG_READ_INTERRUPT +#endif + #if defined(ARDUINO_AVR_UNO) #define ARDUINO_TYPE "UNO" #undef HAS_ENOUGH_MEMORY