From 9dd210fa1460b48a9f74af3dd388698101e05397 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Mon, 1 Feb 2021 10:06:54 +0000 Subject: [PATCH] Drop analogReadFast (see DCCTimer) AnalogRead speed set in DCCTimer for ease of porting. Code tidy and diagnostics in MotorDriver --- AnalogReadFast.h | 46 ---------------------------------------------- DCCTimer.cpp | 2 ++ MotorDriver.cpp | 36 +++++++++++++++++++++--------------- MotorDriver.h | 6 ++++-- 4 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 AnalogReadFast.h diff --git a/AnalogReadFast.h b/AnalogReadFast.h deleted file mode 100644 index 445b233..0000000 --- a/AnalogReadFast.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * AnalogReadFast.h - * - * Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl - * - * This file is part of CommandStation. - * - * CommandStation is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * CommandStation is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with CommandStation. If not, see . - */ - -#ifndef COMMANDSTATION_DCC_ANALOGREADFAST_H_ -#define COMMANDSTATION_DCC_ANALOGREADFAST_H_ - -#include - -int inline analogReadFast(uint8_t ADCpin); -#if defined(ARDUINO_ARCH_MEGAAVR) - -int inline analogReadFast(uint8_t ADCpin) -{ byte ADC0CTRLCoriginal = ADC0.CTRLC; - ADC0.CTRLC = (ADC0CTRLCoriginal & 0b00110000) + 0b01000011; - int adc = analogRead(ADCpin); - ADC0.CTRLC = ADC0CTRLCoriginal; - return adc; -} -#else -int inline analogReadFast(uint8_t ADCpin) -{ byte ADCSRAoriginal = ADCSRA; - ADCSRA = (ADCSRA & B11111000) | 4; - int adc = analogRead(ADCpin); - ADCSRA = ADCSRAoriginal; - return adc; -} -#endif -#endif // COMMANDSTATION_DCC_ANALOGREADFAST_H_ diff --git a/DCCTimer.cpp b/DCCTimer.cpp index bbec1c1..af10f8f 100644 --- a/DCCTimer.cpp +++ b/DCCTimer.cpp @@ -38,6 +38,7 @@ INTERRUPT_CALLBACK interruptHandler=0; void DCCTimer::begin(INTERRUPT_CALLBACK callback) { interruptHandler=callback; noInterrupts(); + ADC0.CTRLC = (ADC0.CTRLC & 0b00110000) | 0b01000011; // speed up analogRead sample time TCB0.CTRLB = TCB_CNTMODE_INT_gc & ~TCB_CCMPEN_bm; // timer compare mode with output disabled TCB0.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // 8 MHz ~ 0.125 us TCB0.CCMP = CLOCK_CYCLES -1; // 1 tick less for timer reset @@ -59,6 +60,7 @@ INTERRUPT_CALLBACK interruptHandler=0; void DCCTimer::begin(INTERRUPT_CALLBACK callback) { interruptHandler=callback; noInterrupts(); + ADCSRA = (ADCSRA & 0b11111000) | 0b00000100; // speed up analogRead sample time TCCR1A = 0; ICR1 = CLOCK_CYCLES; TCNT1 = 0; diff --git a/MotorDriver.cpp b/MotorDriver.cpp index aef9f6d..3e8c14d 100644 --- a/MotorDriver.cpp +++ b/MotorDriver.cpp @@ -18,7 +18,6 @@ */ #include #include "MotorDriver.h" -#include "AnalogReadFast.h" #include "DIAG.h" #define setHIGH(fastpin) *fastpin.out |= fastpin.maskHIGH @@ -27,29 +26,36 @@ MotorDriver::MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin, byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) { powerPin=power_pin; + pinMode(powerPin, OUTPUT); + signalPin=signal_pin; - getFastPin(signalPin,fastSignalPin); + getFastPin(F("SIG"),signalPin,fastSignalPin); + pinMode(signalPin, OUTPUT); + signalPin2=signal_pin2; if (signalPin2!=UNUSED_PIN) { dualSignal=true; - getFastPin(signalPin2,fastSignalPin2); + getFastPin(F("SIG2"),signalPin2,fastSignalPin2); + pinMode(signalPin2, OUTPUT); } else dualSignal=false; brakePin=brake_pin; + if (brakePin!=UNUSED_PIN){ + pinMode(brakePin < 0 ? -brakePin : brakePin, OUTPUT); + setBrake(false); + } + currentPin=current_pin; - senseFactor=sense_factor; + pinMode(currentPin, INPUT); + faultPin=fault_pin; + if (faultPin != UNUSED_PIN) pinMode(faultPin, INPUT); + + senseFactor=sense_factor; tripMilliamps=trip_milliamps; rawCurrentTripValue=(int)(trip_milliamps / sense_factor); simulatedOverload=(int)(32000/senseFactor); - pinMode(powerPin, OUTPUT); - pinMode(brakePin < 0 ? -brakePin : brakePin, OUTPUT); - setBrake(false); - pinMode(signalPin, OUTPUT); - if (signalPin2 != UNUSED_PIN) pinMode(signalPin2, OUTPUT); - pinMode(currentPin, INPUT); - if (faultPin != UNUSED_PIN) pinMode(faultPin, INPUT); } void MotorDriver::setPower(bool on) { @@ -98,8 +104,8 @@ int MotorDriver::getCurrentRaw() { // 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) - return analogReadFast(currentPin); + // so DCCTimer has set the sample time to be much faster. + return analogRead(currentPin); } unsigned int MotorDriver::raw2mA( int raw) { @@ -109,8 +115,8 @@ int MotorDriver::mA2raw( unsigned int mA) { return (int)(mA / senseFactor); } -void MotorDriver::getFastPin(int pin, FASTPIN & result) { - DIAG(F("\nMotorDriver Pin=%d,"),pin); +void MotorDriver::getFastPin(const FSH* type,int pin, FASTPIN & result) { + DIAG(F("\nMotorDriver %S Pin=%d,"),type,pin); uint8_t port = digitalPinToPort(pin); result.out = portOutputRegister(port); result.maskHIGH = digitalPinToBitMask(pin); diff --git a/MotorDriver.h b/MotorDriver.h index 61b36aa..6ebb62e 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -18,6 +18,8 @@ */ #ifndef MotorDriver_h #define MotorDriver_h +#include "FSH.h" + // Virtualised Motor shield 1-track hardware Interface #ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h @@ -40,11 +42,11 @@ class MotorDriver { virtual unsigned int raw2mA( int raw); virtual int mA2raw( unsigned int mA); inline int getRawCurrentTripValue() { - return rawCurrentTripValue; + return rawCurrentTripValue; } private: - void getFastPin(int pin, FASTPIN & result); + void getFastPin(const FSH* type,int pin, FASTPIN & result); byte powerPin, signalPin, signalPin2, currentPin, faultPin; FASTPIN fastSignalPin, fastSignalPin2; bool dualSignal; // true to use signalPin2