From 5f7ac066712aeb95fbd12ecca1801124082b41a3 Mon Sep 17 00:00:00 2001 From: darren siepka Date: Fri, 14 Oct 2016 12:17:17 +0100 Subject: [PATCH] update processor ifdef to tidy code inc teensy options --- auxiliaries.ino | 6 +++--- globals.h | 24 ++++++++++++++++++++++++ idle.ino | 6 +++--- scheduler.h | 4 ++-- scheduler.ino | 40 ++++++++++++++++++++-------------------- speeduino.ino | 6 +++--- timers.h | 2 +- timers.ino | 10 +++++----- utils.ino | 4 ++-- 9 files changed, 63 insertions(+), 39 deletions(-) diff --git a/auxiliaries.ino b/auxiliaries.ino index 17e4673..d4a2874 100644 --- a/auxiliaries.ino +++ b/auxiliaries.ino @@ -21,7 +21,7 @@ void fanControl() else if (currentStatus.coolant <= (configPage4.fanSP - configPage4.fanHyster)) { digitalWrite(pinFan, fanLOW); } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#if defined(PROCESSOR_MEGA_ALL) void initialiseAuxPWM() { TCCR1B = 0x00; //Disbale Timer1 while we set it up @@ -64,7 +64,7 @@ void vvtControl() byte vvtDuty = get3DTableValue(&vvtTable, currentStatus.TPS, currentStatus.RPM); vvt_pwm_target_value = percentage(vvtDuty, vvt_pwm_max_count); } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#if defined(PROCESSOR_MEGA_ALL) else { TIMSK1 &= ~(1 << OCIE1B); } // Disable timer channel #endif } @@ -105,7 +105,7 @@ ISR(TIMER1_COMPB_vect) } } -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) //YET TO BE IMPLEMENTED ON TEENSY void initialiseAuxPWM() { } void boostControl() { } diff --git a/globals.h b/globals.h index 5b88dc0..ded230e 100644 --- a/globals.h +++ b/globals.h @@ -2,6 +2,30 @@ #define GLOBALS_H #include +#if defined(__arm__) + #if defined(__MK20DX256__) && defined(CORE_TEENSY) + #define PROCESSOR_TEENSY_3_2 1 //compile for teensy 3.1/2 only + #elif defined(__MK64FX512__) && defined(CORE_TEENSY) + #define PROCESSOR_TEENSY_3_5 1 //compile for teensy 3.5 only + #endif + #if defined(__MK20DX256__) && defined(CORE_TEENSY) || defined(__MK64FX512__) && defined(CORE_TEENSY) + #define PROCESSOR_TEENSY_3_x 1 //compile for both teensy 3.1/2 and 3.5 + #elif defined (CORE_TEENSY) + #error "Unknown Teensy" + #elif defined (__arm__) + #error "Unknown ARM chip" + #else + #error "Unknown board" + #endif + +#elif defined(__AVR__) + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + #define PROCESSOR_MEGA_NO61 1 + #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) + #define PROCESSOR_MEGA_ALL 1 + #endif + #endif +#endif //const byte ms_version = 20; const byte signature = 20; diff --git a/idle.ino b/idle.ino index 3b8e431..37474a1 100644 --- a/idle.ino +++ b/idle.ino @@ -17,7 +17,7 @@ integerPID idlePID(¤tStatus.longRPM, &idle_pwm_target_value, &idle_cl_targ void initialiseIdle() { //By default, turn off the PWM interrupt (It gets turned on below if needed) -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#if defined(PROCESSOR_MEGA_ALL) TIMSK4 &= ~(1 << OCIE4C); // Disable timer channel for idle #endif @@ -239,7 +239,7 @@ void homeStepper() } //The interrupt to turn off the idle pwm -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) +#if defined(PROCESSOR_MEGA_ALL) //This function simply turns off the idle PWM and sets the pin low static inline void disableIdle() { @@ -293,7 +293,7 @@ ISR(TIMER4_COMPC_vect) } } -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) //This function simply turns off the idle PWM and sets the pin low static inline void disableIdle() { diff --git a/scheduler.h b/scheduler.h index 693d0c7..3f6fb3a 100644 --- a/scheduler.h +++ b/scheduler.h @@ -32,7 +32,7 @@ See page 136 of the processors datasheet: http://www.atmel.com/Images/doc2549.pd #include #endif -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) +#if defined(PROCESSOR_MEGA_ALL) //Refer to http://svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/avr/iomxx0_1.h?root=avr-libc&view=markup #define FUEL1_COUNTER TCNT3 #define FUEL2_COUNTER TCNT3 @@ -74,7 +74,7 @@ See page 136 of the processors datasheet: http://www.atmel.com/Images/doc2549.pd #define IGN3_TIMER_DISABLE() TIMSK5 &= ~(1 << OCIE5C) //Turn off this output compare unit #define IGN4_TIMER_DISABLE() TIMSK4 &= ~(1 << OCIE4A) //Turn off this output compare unit -#elif defined(CORE_TEENSY) +#elif defined(PROCESSOR_TEENSY_3_x) //http://shawnhymel.com/661/learning-the-teensy-lc-interrupt-service-routines/ #define FUEL1_COUNTER FTM0_CNT #define FUEL2_COUNTER FTM0_CNT diff --git a/scheduler.ino b/scheduler.ino index 28b77a8..4e4eb50 100644 --- a/scheduler.ino +++ b/scheduler.ino @@ -11,7 +11,7 @@ void initialiseSchedulers() { nullSchedule.Status = OFF; -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this // Much help in this from http://arduinomega.blogspot.com.au/2011/05/timer2-and-overflow-interrupt-lets-get.html //Fuel Schedules, which uses timer 3 TCCR3B = 0x00; //Disable Timer3 while we set it up @@ -35,7 +35,7 @@ void initialiseSchedulers() TIFR4 = 0x00; //Timer4 INT Flag Reg: Clear Timer Overflow Flag TCCR4A = 0x00; //Timer4 Control Reg A: Wave Gen Mode normal TCCR4B = (1 << CS12); //Timer4 Control Reg B: aka Divisor = 256 = 122.5HzTimer Prescaler set to 256. Refer to http://www.instructables.com/files/orig/F3T/TIKL/H3WSA4V7/F3TTIKLH3WSA4V7.jpg -#elif defined (CORE_TEENSY) && defined (__MK20DX256__) +#elif defined (PROCESSOR_TEENSY_3_x) //Configure ARM timers here #endif @@ -181,7 +181,7 @@ void setFuelSchedule5(void (*startCallback)(), unsigned long timeout, unsigned l /* * The following must be enclosed in the noIntterupts block to avoid contention caused if the relevant interrupts fires before the state is fully set */ -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) +#if defined(PROCESSOR_MEGA_ALL) noInterrupts(); fuelSchedule5.startCompare = TCNT3 + (timeout >> 4); //As above, but with bit shift instead of / 16 fuelSchedule5.endCompare = fuelSchedule5.startCompare + (duration >> 4); @@ -282,7 +282,7 @@ void setIgnitionSchedule5(void (*startCallback)(), unsigned long timeout, unsign //As the timer is ticking every 4uS (Time per Tick = (Prescale)*(1/Frequency)) if (timeout > 262140) { timeout = 262100; } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when applied causing erratic behaviour such as erroneous sparking. This must be set slightly lower than the max of 262140 to avoid strangeness -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) +#if defined(PROCESSOR_MEGA_ALL) OCR5A = TCNT5 + (timeout >> 2); //As there is a tick every 4uS, there are timeout/4 ticks until the interrupt should be triggered ( >>2 divides by 4) ignitionSchedule5.Status = PENDING; //Turn this schedule on TIMSK5 |= (1 << OCIE5A); //Turn on the A compare unit (ie turn on the interrupt) @@ -294,9 +294,9 @@ void setIgnitionSchedule5(void (*startCallback)(), unsigned long timeout, unsign //This calls the relevant callback function (startCallback or endCallback) depending on the status of the schedule. //If the startCallback function is called, we put the scheduler into RUNNING state //Timer3A (fuel schedule 1) Compare Vector -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER3_COMPA_vect, ISR_NOBLOCK) //fuelSchedules 1 and 5 -#elif defined (CORE_TEENSY) +#elif defined(PROCESSOR_TEENSY_3_x) void timer3compareAinterrupt() //Most ARM chips can simply call a function #endif { @@ -316,9 +316,9 @@ void timer3compareAinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER3_COMPB_vect, ISR_NOBLOCK) //fuelSchedule2 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer3compareBinterrupt() //Most ARM chips can simply call a function #endif { @@ -337,9 +337,9 @@ void timer3compareBinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER3_COMPC_vect, ISR_NOBLOCK) //fuelSchedule3 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer3compareCinterrupt() //Most ARM chips can simply call a function #endif { @@ -358,9 +358,9 @@ void timer3compareCinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER4_COMPB_vect, ISR_NOBLOCK) //fuelSchedule4 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer4compareBinterrupt() //Most ARM chips can simply call a function #endif { @@ -379,9 +379,9 @@ void timer4compareBinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER5_COMPA_vect, ISR_NOBLOCK) //ignitionSchedule1 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer5compareAinterrupt() //Most ARM chips can simply call a function #endif { @@ -402,9 +402,9 @@ void timer5compareAinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER5_COMPB_vect, ISR_NOBLOCK) //ignitionSchedule2 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer5compareBinterrupt() //Most ARM chips can simply call a function #endif { @@ -425,9 +425,9 @@ void timer5compareBinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER5_COMPC_vect, ISR_NOBLOCK) //ignitionSchedule3 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer5compareCinterrupt() //Most ARM chips can simply call a function #endif { @@ -448,9 +448,9 @@ void timer5compareCinterrupt() //Most ARM chips can simply call a function } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this ISR(TIMER4_COMPA_vect, ISR_NOBLOCK) //ignitionSchedule4 -#elif defined (CORE_TEENSY) +#elif defined (PROCESSOR_TEENSY_3_x) void timer4compareAinterrupt() //Most ARM chips can simply call a function #endif { diff --git a/speeduino.ino b/speeduino.ino index 0f56e74..b182e4d 100644 --- a/speeduino.ino +++ b/speeduino.ino @@ -151,7 +151,7 @@ volatile bool fpPrimed = false; //Tracks whether or not the fuel pump priming ha void setup() { Serial.begin(115200); -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //ATmega2561 does not have Serial3 +#if defined(PROCESSOR_MEGA_NO61) //ATmega2561 does not have Serial3 if (configPage1.canEnable) { Serial3.begin(115200); } #endif @@ -460,7 +460,7 @@ void setup() currentLoopTime = micros(); -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this //This sets the ADC (Analog to Digitial Converter) to run at 1Mhz, greatly reducing analog read times (MAP/TPS) //1Mhz is the fastest speed permitted by the CPU without affecting accuracy //Please see chapter 11 of 'Practical Arduino' (http://books.google.com.au/books?id=HsTxON1L6D4C&printsec=frontcover#v=onepage&q&f=false) for more details @@ -775,7 +775,7 @@ void loop() command(); } } -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //ATmega2561 does not have Serial3 +#if defined(PROCESSOR_MEGA_NO61) //ATmega2561 does not have Serial3 //if Can interface is enabled then check for serial3 requests. if (configPage1.canEnable) { diff --git a/timers.h b/timers.h index 8b319f4..33a2f12 100644 --- a/timers.h +++ b/timers.h @@ -24,7 +24,7 @@ volatile int loopSec; volatile unsigned long targetOverdwellTime; volatile unsigned long targetTachoPulseTime; -#if defined (CORE_TEENSY) +#if defined (PROCESSOR_TEENSY_3_x) IntervalTimer lowResTimer; #endif void initialiseTimers(); diff --git a/timers.ino b/timers.ino index 44d75aa..1f44741 100644 --- a/timers.ino +++ b/timers.ino @@ -16,7 +16,7 @@ Timers are typically low resolution (Compared to Schedulers), with maximum frequ void initialiseTimers() { -#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) //AVR chips use the ISR for this +#if defined(PROCESSOR_MEGA_ALL) //AVR chips use the ISR for this //Configure Timer2 for our low-freq interrupt code. TCCR2B = 0x00; //Disbale Timer2 while we set it up TCNT2 = 131; //Preload timer2 with 131 cycles, leaving 125 till overflow. As the timer runs at 125Khz, this causes overflow to occur at 1Khz = 1ms @@ -26,7 +26,7 @@ void initialiseTimers() /* Now configure the prescaler to CPU clock divided by 128 = 125Khz */ TCCR2B |= (1<