mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-23 16:16:13 +01:00
Drop analogReadFast (see DCCTimer)
AnalogRead speed set in DCCTimer for ease of porting. Code tidy and diagnostics in MotorDriver
This commit is contained in:
parent
13757c8c57
commit
9dd210fa14
|
@ -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 <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef COMMANDSTATION_DCC_ANALOGREADFAST_H_
|
|
||||||
#define COMMANDSTATION_DCC_ANALOGREADFAST_H_
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
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_
|
|
|
@ -38,6 +38,7 @@ INTERRUPT_CALLBACK interruptHandler=0;
|
||||||
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
|
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
|
||||||
interruptHandler=callback;
|
interruptHandler=callback;
|
||||||
noInterrupts();
|
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.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.CTRLA = TCB_CLKSEL_CLKDIV2_gc; // 8 MHz ~ 0.125 us
|
||||||
TCB0.CCMP = CLOCK_CYCLES -1; // 1 tick less for timer reset
|
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) {
|
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
|
||||||
interruptHandler=callback;
|
interruptHandler=callback;
|
||||||
noInterrupts();
|
noInterrupts();
|
||||||
|
ADCSRA = (ADCSRA & 0b11111000) | 0b00000100; // speed up analogRead sample time
|
||||||
TCCR1A = 0;
|
TCCR1A = 0;
|
||||||
ICR1 = CLOCK_CYCLES;
|
ICR1 = CLOCK_CYCLES;
|
||||||
TCNT1 = 0;
|
TCNT1 = 0;
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
*/
|
*/
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "MotorDriver.h"
|
#include "MotorDriver.h"
|
||||||
#include "AnalogReadFast.h"
|
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
|
|
||||||
#define setHIGH(fastpin) *fastpin.out |= fastpin.maskHIGH
|
#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,
|
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) {
|
byte current_pin, float sense_factor, unsigned int trip_milliamps, byte fault_pin) {
|
||||||
powerPin=power_pin;
|
powerPin=power_pin;
|
||||||
|
pinMode(powerPin, OUTPUT);
|
||||||
|
|
||||||
signalPin=signal_pin;
|
signalPin=signal_pin;
|
||||||
getFastPin(signalPin,fastSignalPin);
|
getFastPin(F("SIG"),signalPin,fastSignalPin);
|
||||||
|
pinMode(signalPin, OUTPUT);
|
||||||
|
|
||||||
signalPin2=signal_pin2;
|
signalPin2=signal_pin2;
|
||||||
if (signalPin2!=UNUSED_PIN) {
|
if (signalPin2!=UNUSED_PIN) {
|
||||||
dualSignal=true;
|
dualSignal=true;
|
||||||
getFastPin(signalPin2,fastSignalPin2);
|
getFastPin(F("SIG2"),signalPin2,fastSignalPin2);
|
||||||
|
pinMode(signalPin2, OUTPUT);
|
||||||
}
|
}
|
||||||
else dualSignal=false;
|
else dualSignal=false;
|
||||||
|
|
||||||
brakePin=brake_pin;
|
brakePin=brake_pin;
|
||||||
|
if (brakePin!=UNUSED_PIN){
|
||||||
|
pinMode(brakePin < 0 ? -brakePin : brakePin, OUTPUT);
|
||||||
|
setBrake(false);
|
||||||
|
}
|
||||||
|
|
||||||
currentPin=current_pin;
|
currentPin=current_pin;
|
||||||
senseFactor=sense_factor;
|
pinMode(currentPin, INPUT);
|
||||||
|
|
||||||
faultPin=fault_pin;
|
faultPin=fault_pin;
|
||||||
|
if (faultPin != UNUSED_PIN) pinMode(faultPin, INPUT);
|
||||||
|
|
||||||
|
senseFactor=sense_factor;
|
||||||
tripMilliamps=trip_milliamps;
|
tripMilliamps=trip_milliamps;
|
||||||
rawCurrentTripValue=(int)(trip_milliamps / sense_factor);
|
rawCurrentTripValue=(int)(trip_milliamps / sense_factor);
|
||||||
simulatedOverload=(int)(32000/senseFactor);
|
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) {
|
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
|
// IMPORTANT: This function can be called in Interrupt() time within the 56uS timer
|
||||||
// The default analogRead takes ~100uS which is catastrphic
|
// The default analogRead takes ~100uS which is catastrphic
|
||||||
// so analogReadFast is used here. (-2uS)
|
// so DCCTimer has set the sample time to be much faster.
|
||||||
return analogReadFast(currentPin);
|
return analogRead(currentPin);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int MotorDriver::raw2mA( int raw) {
|
unsigned int MotorDriver::raw2mA( int raw) {
|
||||||
|
@ -109,8 +115,8 @@ int MotorDriver::mA2raw( unsigned int mA) {
|
||||||
return (int)(mA / senseFactor);
|
return (int)(mA / senseFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MotorDriver::getFastPin(int pin, FASTPIN & result) {
|
void MotorDriver::getFastPin(const FSH* type,int pin, FASTPIN & result) {
|
||||||
DIAG(F("\nMotorDriver Pin=%d,"),pin);
|
DIAG(F("\nMotorDriver %S Pin=%d,"),type,pin);
|
||||||
uint8_t port = digitalPinToPort(pin);
|
uint8_t port = digitalPinToPort(pin);
|
||||||
result.out = portOutputRegister(port);
|
result.out = portOutputRegister(port);
|
||||||
result.maskHIGH = digitalPinToBitMask(pin);
|
result.maskHIGH = digitalPinToBitMask(pin);
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
*/
|
*/
|
||||||
#ifndef MotorDriver_h
|
#ifndef MotorDriver_h
|
||||||
#define MotorDriver_h
|
#define MotorDriver_h
|
||||||
|
#include "FSH.h"
|
||||||
|
|
||||||
// Virtualised Motor shield 1-track hardware Interface
|
// Virtualised Motor shield 1-track hardware Interface
|
||||||
|
|
||||||
#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h
|
#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h
|
||||||
|
@ -44,7 +46,7 @@ class MotorDriver {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void getFastPin(int pin, FASTPIN & result);
|
void getFastPin(const FSH* type,int pin, FASTPIN & result);
|
||||||
byte powerPin, signalPin, signalPin2, currentPin, faultPin;
|
byte powerPin, signalPin, signalPin2, currentPin, faultPin;
|
||||||
FASTPIN fastSignalPin, fastSignalPin2;
|
FASTPIN fastSignalPin, fastSignalPin2;
|
||||||
bool dualSignal; // true to use signalPin2
|
bool dualSignal; // true to use signalPin2
|
||||||
|
|
Loading…
Reference in New Issue
Block a user