mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-22 23:56:13 +01:00
Back out this as it is bigger and slower
This reverts commit efb2666060
.
This commit is contained in:
parent
0266936875
commit
7783837545
14
DCCTimer.h
14
DCCTimer.h
|
@ -105,9 +105,14 @@ private:
|
||||||
// that an offset can be initialized.
|
// that an offset can be initialized.
|
||||||
class ADCee {
|
class ADCee {
|
||||||
public:
|
public:
|
||||||
// init does add the pin to the list of scanned pins (if this
|
// begin is called for any setup that must be done before
|
||||||
|
// **init** can be called. On some architectures this involves ADC
|
||||||
|
// initialisation and clock routing, sampling times etc.
|
||||||
|
static void begin();
|
||||||
|
// init adds the pin to the list of scanned pins (if this
|
||||||
// platform's implementation scans pins) and returns the first
|
// platform's implementation scans pins) and returns the first
|
||||||
// read value. It is called before the regular scan is started.
|
// read value (which is why it required begin to have been called first!)
|
||||||
|
// It must be called before the regular scan is started.
|
||||||
static int init(uint8_t pin);
|
static int init(uint8_t pin);
|
||||||
// read does read the pin value from the scanned cache or directly
|
// read does read the pin value from the scanned cache or directly
|
||||||
// if this is a platform that does not scan. fromISR is a hint if
|
// if this is a platform that does not scan. fromISR is a hint if
|
||||||
|
@ -116,9 +121,6 @@ public:
|
||||||
static int read(uint8_t pin, bool fromISR=false);
|
static int read(uint8_t pin, bool fromISR=false);
|
||||||
// returns possible max value that the ADC can return
|
// returns possible max value that the ADC can return
|
||||||
static int16_t ADCmax();
|
static int16_t ADCmax();
|
||||||
// begin is called for any setup that must be done before
|
|
||||||
// scan can be called.
|
|
||||||
static void begin();
|
|
||||||
private:
|
private:
|
||||||
// On platforms that scan, it is called from waveform ISR
|
// On platforms that scan, it is called from waveform ISR
|
||||||
// only on a regular basis.
|
// only on a regular basis.
|
||||||
|
@ -127,8 +129,6 @@ private:
|
||||||
static uint16_t usedpins;
|
static uint16_t usedpins;
|
||||||
// cached analog values (malloc:ed to actual number of ADC channels)
|
// cached analog values (malloc:ed to actual number of ADC channels)
|
||||||
static int *analogvals;
|
static int *analogvals;
|
||||||
// ids to scan (new way)
|
|
||||||
static byte *idarr;
|
|
||||||
// friend so that we can call scan() and begin()
|
// friend so that we can call scan() and begin()
|
||||||
friend class DCCWaveform;
|
friend class DCCWaveform;
|
||||||
};
|
};
|
||||||
|
|
|
@ -123,14 +123,13 @@ void DCCTimer::reset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
|
||||||
#define NUM_ADC_INPUTS 16
|
#define NUM_ADC_INPUTS 15
|
||||||
#else
|
#else
|
||||||
#define NUM_ADC_INPUTS 8
|
#define NUM_ADC_INPUTS 7
|
||||||
#endif
|
#endif
|
||||||
uint16_t ADCee::usedpins = 0;
|
uint16_t ADCee::usedpins = 0;
|
||||||
int * ADCee::analogvals = NULL;
|
int * ADCee::analogvals = NULL;
|
||||||
byte *ADCee::idarr = NULL;
|
bool ADCusesHighPort = false;
|
||||||
static bool ADCusesHighPort = false;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Register a new pin to be scanned
|
* Register a new pin to be scanned
|
||||||
|
@ -139,28 +138,16 @@ static bool ADCusesHighPort = false;
|
||||||
*/
|
*/
|
||||||
int ADCee::init(uint8_t pin) {
|
int ADCee::init(uint8_t pin) {
|
||||||
uint8_t id = pin - A0;
|
uint8_t id = pin - A0;
|
||||||
byte n;
|
if (id > NUM_ADC_INPUTS)
|
||||||
if (id >= NUM_ADC_INPUTS)
|
|
||||||
return -1023;
|
return -1023;
|
||||||
if (id > 7)
|
if (id > 7)
|
||||||
ADCusesHighPort = true;
|
ADCusesHighPort = true;
|
||||||
pinMode(pin, INPUT);
|
pinMode(pin, INPUT);
|
||||||
int value = analogRead(pin);
|
int value = analogRead(pin);
|
||||||
if (analogvals == NULL) {
|
if (analogvals == NULL)
|
||||||
analogvals = (int *)calloc(NUM_ADC_INPUTS, sizeof(int));
|
analogvals = (int *)calloc(NUM_ADC_INPUTS+1, sizeof(int));
|
||||||
for (n=0 ; n < NUM_ADC_INPUTS; n++) // set unreasonable value at startup as marker
|
analogvals[id] = value;
|
||||||
analogvals[n] = -32768; // 16 bit int min value
|
usedpins |= (1<<id);
|
||||||
idarr = (byte *)calloc(NUM_ADC_INPUTS+1, sizeof(byte)); // +1 for terminator value
|
|
||||||
for (n=0 ; n <= NUM_ADC_INPUTS; n++)
|
|
||||||
idarr[n] = 255; // set 255 as end of array marker
|
|
||||||
}
|
|
||||||
analogvals[id] = value; // store before enable by idarr[n]
|
|
||||||
for (n=0 ; n <= NUM_ADC_INPUTS; n++) {
|
|
||||||
if (idarr[n] == 255) {
|
|
||||||
idarr[n] = id;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
int16_t ADCee::ADCmax() {
|
int16_t ADCee::ADCmax() {
|
||||||
|
@ -172,13 +159,11 @@ int16_t ADCee::ADCmax() {
|
||||||
int ADCee::read(uint8_t pin, bool fromISR) {
|
int ADCee::read(uint8_t pin, bool fromISR) {
|
||||||
(void)fromISR; // AVR does ignore this arg
|
(void)fromISR; // AVR does ignore this arg
|
||||||
uint8_t id = pin - A0;
|
uint8_t id = pin - A0;
|
||||||
int a;
|
if ((usedpins & (1<<id) ) == 0)
|
||||||
|
return -1023;
|
||||||
// we do not need to check (analogvals == NULL)
|
// we do not need to check (analogvals == NULL)
|
||||||
// because usedpins would still be 0 in that case
|
// because usedpins would still be 0 in that case
|
||||||
noInterrupts();
|
return analogvals[id];
|
||||||
a = analogvals[id];
|
|
||||||
interrupts();
|
|
||||||
return a;
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Scan function that is called from interrupt
|
* Scan function that is called from interrupt
|
||||||
|
@ -186,7 +171,8 @@ int ADCee::read(uint8_t pin, bool fromISR) {
|
||||||
#pragma GCC push_options
|
#pragma GCC push_options
|
||||||
#pragma GCC optimize ("-O3")
|
#pragma GCC optimize ("-O3")
|
||||||
void ADCee::scan() {
|
void ADCee::scan() {
|
||||||
static byte num = 0; // index into id array
|
static byte id = 0; // id and mask are the same thing but it is faster to
|
||||||
|
static uint16_t mask = 1; // increment and shift instead to calculate mask from id
|
||||||
static bool waiting = false;
|
static bool waiting = false;
|
||||||
|
|
||||||
if (waiting) {
|
if (waiting) {
|
||||||
|
@ -198,26 +184,45 @@ void ADCee::scan() {
|
||||||
low = ADCL; //must read low before high
|
low = ADCL; //must read low before high
|
||||||
high = ADCH;
|
high = ADCH;
|
||||||
bitSet(ADCSRA, ADIF);
|
bitSet(ADCSRA, ADIF);
|
||||||
analogvals[idarr[num]] = (high << 8) | low;
|
analogvals[id] = (high << 8) | low;
|
||||||
|
// advance at least one track
|
||||||
|
// for scope debug TrackManager::track[1]->setBrake(0);
|
||||||
waiting = false;
|
waiting = false;
|
||||||
|
id++;
|
||||||
|
mask = mask << 1;
|
||||||
|
if (id == NUM_ADC_INPUTS+1) {
|
||||||
|
id = 0;
|
||||||
|
mask = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!waiting) {
|
if (!waiting) {
|
||||||
// cycle around in-use analogue pins
|
if (usedpins == 0) // otherwise we would loop forever
|
||||||
num++;
|
return;
|
||||||
if (idarr[num] == 255)
|
// look for a valid track to sample or until we are around
|
||||||
num = 0;
|
while (true) {
|
||||||
// start new ADC aquire on id
|
if (mask & usedpins) {
|
||||||
|
// start new ADC aquire on id
|
||||||
#if defined(ADCSRB) && defined(MUX5)
|
#if defined(ADCSRB) && defined(MUX5)
|
||||||
if (ADCusesHighPort) { // if we ever have started to use high pins)
|
if (ADCusesHighPort) { // if we ever have started to use high pins)
|
||||||
if (idarr[num] > 7) // if we use a high ADC pin
|
if (id > 7) // if we use a high ADC pin
|
||||||
bitSet(ADCSRB, MUX5); // set MUX5 bit
|
bitSet(ADCSRB, MUX5); // set MUX5 bit
|
||||||
else
|
else
|
||||||
bitClear(ADCSRB, MUX5);
|
bitClear(ADCSRB, MUX5);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
ADMUX = (1 << REFS0) | (idarr[num] & 0x07); // select AVCC as reference and set MUX
|
ADMUX=(1<<REFS0)|(id & 0x07); //select AVCC as reference and set MUX
|
||||||
bitSet(ADCSRA, ADSC); // start conversion
|
bitSet(ADCSRA,ADSC); // start conversion
|
||||||
waiting = true;
|
// for scope debug TrackManager::track[1]->setBrake(1);
|
||||||
|
waiting = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
id++;
|
||||||
|
mask = mask << 1;
|
||||||
|
if (id == NUM_ADC_INPUTS+1) {
|
||||||
|
id = 0;
|
||||||
|
mask = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#pragma GCC pop_options
|
#pragma GCC pop_options
|
||||||
|
@ -231,4 +236,4 @@ void ADCee::begin() {
|
||||||
//bitSet(ADCSRA, ADSC); //do not start the ADC yet. Done when we have set the MUX
|
//bitSet(ADCSRA, ADSC); //do not start the ADC yet. Done when we have set the MUX
|
||||||
interrupts();
|
interrupts();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user