1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

make compile for other arch but AVR

This commit is contained in:
Harald Barth 2022-10-02 22:53:35 +02:00
parent 24e5e648b8
commit 5e616a9eb2
7 changed files with 41 additions and 39 deletions

View File

@ -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<<REFS0); //select AVCC as reference. We set MUX later
//ADCSRB = 0; //set free running mode
//ADCSRA = (1<<ADEN)|(1<< ADATE)|(1<<ADPS2)|(1<<ADPS0)|4;
ADCSRA = (1<<ADEN)|(1 << ADPS2);
//bitSet(ADCSRA, ADSC); //start the ADC
ADCSRA = (1<<ADEN)|(1 << ADPS2); // ADPS2 means divisor 32 and 16Mhz/32=500kHz.
//bitSet(ADCSRA, ADSC); //do not start the ADC yet. Done when we have set the MUX
TCCR1A = 0;
ICR1 = CLOCK_CYCLES;

View File

@ -81,7 +81,9 @@ void DCCWaveform::interruptHandler() {
TrackManager::setDCCSignal(sigMain);
TrackManager::setPROGSignal(sigProg);
#ifdef ANALOG_READ_INTERRUPT
TrackManager::sampleCurrent();
#endif
// Move on in the state engine
mainTrack.state=stateTransform[mainTrack.state];

View File

@ -1 +1 @@
#define GITHUB_SHA "PORTX-HAL-202210021139Z"
#define GITHUB_SHA "PORTX-HAL-202210022048Z"

View File

@ -213,30 +213,34 @@ bool MotorDriver::canMeasureCurrent() {
*
* senseOffset handles the case where a shield returns values above or below
* a central value depending on direction.
*
* Bool fromISR should be adjusted dependent how function is called
*/
int MotorDriver::getCurrentRaw() {
int MotorDriver::getCurrentRaw(bool fromISR) {
(void)fromISR;
if (currentPin==UNUSED_PIN) return 0;
int current;
// This function should NOT be called in an interruot so we
// dont need to fart about saving and restoring CPU specific
// interrupt registers.
#ifdef ARDUINO_ARCH_ESP32
current = local_adc1_get_raw(pinToADC1Channel(currentPin))-senseOffset;
#else
// noInterrupts();
//current = analogRead(currentPin)-senseOffset;
#ifdef ANALOG_READ_INTERRUPT
current = sampleCurrent-senseOffset;
//DIAG(F("%d %d"), current, sampleCurrentTimestamp);
if ((millis() - sampleCurrentTimestamp) > 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;
}

View File

@ -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;

View File

@ -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

View File

@ -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