diff --git a/speeduino/auxiliaries.ino b/speeduino/auxiliaries.ino index d10ecac3..5d2f1332 100644 --- a/speeduino/auxiliaries.ino +++ b/speeduino/auxiliaries.ino @@ -8,6 +8,7 @@ A full copy of the license may be found in the projects root directory #include "maths.h" #include "src/PID_v1/PID_v1.h" #include "decoders.h" +#include "timers.h" //Old PID method. Retained incase the new one has issues //integerPID boostPID(&MAPx100, &boost_pwm_target_value, &boostTargetx100, configPage6.boostKP, configPage6.boostKI, configPage6.boostKD, DIRECT); @@ -607,13 +608,13 @@ void boostDisable() if (boost_pwm_state == true) { BOOST_PIN_LOW(); // Switch pin to low - BOOST_TIMER_COMPARE = BOOST_TIMER_COUNTER + (boost_pwm_max_count - boost_pwm_cur_value); + SET_COMPARE(BOOST_TIMER_COMPARE, BOOST_TIMER_COUNTER + (boost_pwm_max_count - boost_pwm_cur_value) ); boost_pwm_state = false; } else { BOOST_PIN_HIGH(); // Switch pin high - BOOST_TIMER_COMPARE = BOOST_TIMER_COUNTER + boost_pwm_target_value; + SET_COMPARE(BOOST_TIMER_COMPARE, BOOST_TIMER_COUNTER + boost_pwm_target_value); boost_pwm_cur_value = boost_pwm_target_value; boost_pwm_state = true; } @@ -641,7 +642,7 @@ void boostDisable() if( (vvt1_pwm_state == true) && ((vvt1_pwm_value <= vvt2_pwm_value) || (vvt2_pwm_state == false)) ) { - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + vvt1_pwm_value; + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + vvt1_pwm_value); vvt1_pwm_cur_value = vvt1_pwm_value; vvt2_pwm_cur_value = vvt2_pwm_value; if (vvt1_pwm_value == vvt2_pwm_value) { nextVVT = 2; } //Next event is for both PWM @@ -649,12 +650,12 @@ void boostDisable() } else if( vvt2_pwm_state == true ) { - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + vvt2_pwm_value; + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + vvt2_pwm_value); vvt1_pwm_cur_value = vvt1_pwm_value; vvt2_pwm_cur_value = vvt2_pwm_value; nextVVT = 1; //Next event is for PWM1 } - else { VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + vvt_pwm_max_count; } //Shouldn't ever get here + else { SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + vvt_pwm_max_count); } //Shouldn't ever get here } else { @@ -668,10 +669,10 @@ void boostDisable() } else { vvt1_max_pwm = true; } nextVVT = 1; //Next event is for PWM1 - if(vvt2_pwm_state == true){ VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt2_pwm_cur_value - vvt1_pwm_cur_value); } + if(vvt2_pwm_state == true){ SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt2_pwm_cur_value - vvt1_pwm_cur_value) ); } else { - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt1_pwm_cur_value); + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt1_pwm_cur_value) ); nextVVT = 2; //Next event is for both PWM } } @@ -685,10 +686,10 @@ void boostDisable() } else { vvt2_max_pwm = true; } nextVVT = 0; //Next event is for PWM0 - if(vvt1_pwm_state == true) { VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt1_pwm_cur_value - vvt2_pwm_cur_value); } + if(vvt1_pwm_state == true) { SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt1_pwm_cur_value - vvt2_pwm_cur_value) ); } else { - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt2_pwm_cur_value); + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt2_pwm_cur_value) ); nextVVT = 2; //Next event is for both PWM } } @@ -699,7 +700,7 @@ void boostDisable() VVT1_PIN_OFF(); vvt1_pwm_state = false; vvt1_max_pwm = false; - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt1_pwm_cur_value); + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt1_pwm_cur_value) ); } else { vvt1_max_pwm = true; } if(vvt2_pwm_value < (long)vvt_pwm_max_count) //Don't toggle if at 100% @@ -707,7 +708,7 @@ void boostDisable() VVT2_PIN_OFF(); vvt2_pwm_state = false; vvt2_max_pwm = false; - VVT_TIMER_COMPARE = VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt2_pwm_cur_value); + SET_COMPARE(VVT_TIMER_COMPARE, VVT_TIMER_COUNTER + (vvt_pwm_max_count - vvt2_pwm_cur_value) ); } else { vvt2_max_pwm = true; } } diff --git a/speeduino/decoders.ino b/speeduino/decoders.ino index 535b90ea..5326c191 100644 --- a/speeduino/decoders.ino +++ b/speeduino/decoders.ino @@ -38,6 +38,7 @@ A full copy of the license may be found in the projects root directory #include "scheduledIO.h" #include "scheduler.h" #include "crankMaths.h" +#include "timers.h" void (*triggerHandler)(); ///Pointer for the trigger function (Gets pointed to the relevant decoder) void (*triggerSecondaryHandler)(); ///Pointer for the secondary trigger function (Gets pointed to the relevant decoder) @@ -307,49 +308,49 @@ static inline void checkPerToothTiming(int16_t crankAngle, uint16_t currentTooth { if ( (currentTooth == ignition1EndTooth) ) { - if( (ignitionSchedule1.Status == RUNNING) ) { IGN1_COMPARE = IGN1_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition1EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule1.Status == RUNNING) ) { SET_COMPARE(IGN1_COMPARE, IGN1_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition1EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule1.endCompare = IGN1_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition1EndAngle - crankAngle) ) ) ); ignitionSchedule1.endScheduleSetByDecoder = true; } } else if ( (currentTooth == ignition2EndTooth) ) { - if( (ignitionSchedule2.Status == RUNNING) ) { IGN2_COMPARE = IGN2_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition2EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule2.Status == RUNNING) ) { SET_COMPARE(IGN2_COMPARE, IGN2_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition2EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule2.endCompare = IGN2_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition2EndAngle - crankAngle) ) ) ); ignitionSchedule2.endScheduleSetByDecoder = true; } } else if ( (currentTooth == ignition3EndTooth) ) { - if( (ignitionSchedule3.Status == RUNNING) ) { IGN3_COMPARE = IGN3_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition3EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule3.Status == RUNNING) ) { SET_COMPARE(IGN3_COMPARE, IGN3_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition3EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule3.endCompare = IGN3_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition3EndAngle - crankAngle) ) ) ); ignitionSchedule3.endScheduleSetByDecoder = true; } } else if ( (currentTooth == ignition4EndTooth) ) { - if( (ignitionSchedule4.Status == RUNNING) ) { IGN4_COMPARE = IGN4_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition4EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule4.Status == RUNNING) ) { SET_COMPARE(IGN4_COMPARE, IGN4_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition4EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule4.endCompare = IGN4_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition4EndAngle - crankAngle) ) ) ); ignitionSchedule4.endScheduleSetByDecoder = true; } } #if IGN_CHANNELS >= 5 else if ( (currentTooth == ignition5EndTooth) ) { - if( (ignitionSchedule5.Status == RUNNING) ) { IGN5_COMPARE = IGN5_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition5EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule5.Status == RUNNING) ) { SET_COMPARE(IGN5_COMPARE, IGN5_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition5EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule5.endCompare = IGN5_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition5EndAngle - crankAngle) ) ) ); ignitionSchedule5.endScheduleSetByDecoder = true; } } #endif #if IGN_CHANNELS >= 6 else if ( (currentTooth == ignition6EndTooth) ) { - if( (ignitionSchedule6.Status == RUNNING) ) { IGN6_COMPARE = IGN6_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition6EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule6.Status == RUNNING) ) { SET_COMPARE(IGN6_COMPARE, IGN6_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition6EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule6.endCompare = IGN6_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition6EndAngle - crankAngle) ) ) ); ignitionSchedule6.endScheduleSetByDecoder = true; } } #endif #if IGN_CHANNELS >= 7 else if ( (currentTooth == ignition7EndTooth) ) { - if( (ignitionSchedule7.Status == RUNNING) ) { IGN7_COMPARE = IGN7_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition7EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule7.Status == RUNNING) ) { SET_COMPARE(IGN7_COMPARE, IGN7_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition7EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule7.endCompare = IGN7_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition7EndAngle - crankAngle) ) ) ); ignitionSchedule7.endScheduleSetByDecoder = true; } } #endif #if IGN_CHANNELS >= 8 else if ( (currentTooth == ignition8EndTooth) ) { - if( (ignitionSchedule8.Status == RUNNING) ) { IGN8_COMPARE = IGN8_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition8EndAngle - crankAngle) ) ) ); } + if( (ignitionSchedule8.Status == RUNNING) ) { SET_COMPARE(IGN8_COMPARE, IGN8_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition8EndAngle - crankAngle) ) ) ) ); } else if(currentStatus.startRevolutions > MIN_CYCLES_FOR_ENDCOMPARE) { ignitionSchedule8.endCompare = IGN8_COUNTER + uS_TO_TIMER_COMPARE( fastDegreesToUS( ignitionLimits( (ignition8EndAngle - crankAngle) ) ) ); ignitionSchedule8.endScheduleSetByDecoder = true; } } #endif diff --git a/speeduino/idle.ino b/speeduino/idle.ino index 36e2e6ca..f7f60a4d 100644 --- a/speeduino/idle.ino +++ b/speeduino/idle.ino @@ -706,7 +706,7 @@ void idleInterrupt() //Most ARM chips can simply call a function *idle_pin_port |= (idle_pin_mask); // Switch pin high if(configPage6.iacChannels == 1) { *idle2_pin_port &= ~(idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 } - IDLE_COMPARE = IDLE_COUNTER + (idle_pwm_max_count - idle_pwm_cur_value); + SET_COMPARE(IDLE_COMPARE, IDLE_COUNTER + (idle_pwm_max_count - idle_pwm_cur_value) ); idle_pwm_state = false; } else @@ -723,7 +723,7 @@ void idleInterrupt() //Most ARM chips can simply call a function *idle_pin_port &= ~(idle_pin_mask); // Switch pin to low (1 pin mode) if(configPage6.iacChannels == 1) { *idle2_pin_port |= (idle2_pin_mask); } //If 2 idle channels are in use, flip idle2 to be the opposite of idle1 } - IDLE_COMPARE = IDLE_COUNTER + idle_pwm_target_value; + SET_COMPARE(IDLE_COMPARE, IDLE_COUNTER + idle_pwm_target_value); idle_pwm_cur_value = idle_pwm_target_value; idle_pwm_state = true; } diff --git a/speeduino/scheduler.h b/speeduino/scheduler.h index f64709a0..860f13eb 100644 --- a/speeduino/scheduler.h +++ b/speeduino/scheduler.h @@ -171,8 +171,8 @@ struct Schedule { volatile COMPARE_TYPE startCompare; ///< The counter value of the timer when this will start volatile COMPARE_TYPE endCompare; ///< The counter value of the timer when this will end - unsigned int nextStartCompare; ///< Planned start of next schedule (when current schedule is RUNNING) - unsigned int nextEndCompare; ///< Planned end of next schedule (when current schedule is RUNNING) + COMPARE_TYPE nextStartCompare; ///< Planned start of next schedule (when current schedule is RUNNING) + COMPARE_TYPE nextEndCompare; ///< Planned end of next schedule (when current schedule is RUNNING) volatile bool hasNextSchedule = false; ///< Enable flag for planned next schedule (when current schedule is RUNNING) volatile bool endScheduleSetByDecoder = false; }; @@ -187,8 +187,8 @@ struct FuelSchedule { volatile COMPARE_TYPE startCompare; ///< The counter value of the timer when this will start volatile COMPARE_TYPE endCompare; ///< The counter value of the timer when this will end - unsigned int nextStartCompare; - unsigned int nextEndCompare; + COMPARE_TYPE nextStartCompare; + COMPARE_TYPE nextEndCompare; volatile bool hasNextSchedule = false; }; @@ -216,7 +216,7 @@ extern Schedule ignitionSchedule8; //IgnitionSchedule nullSchedule; //This is placed at the end of the queue. It's status will always be set to OFF and hence will never perform any action within an ISR -static inline unsigned int setQueue(volatile Schedule *queue[], Schedule *schedule1, Schedule *schedule2, unsigned int CNT) +static inline COMPARE_TYPE setQueue(volatile Schedule *queue[], Schedule *schedule1, Schedule *schedule2, unsigned int CNT) { //Create an array of all the upcoming targets, relative to the current count on the timer unsigned int tmpQueue[4]; diff --git a/speeduino/scheduler.ino b/speeduino/scheduler.ino index 6c227091..06297ece 100644 --- a/speeduino/scheduler.ino +++ b/speeduino/scheduler.ino @@ -27,6 +27,7 @@ A full copy of the license may be found in the projects root directory #include "globals.h" #include "scheduler.h" #include "scheduledIO.h" +#include "timers.h" FuelSchedule fuelSchedule1; FuelSchedule fuelSchedule2; @@ -148,7 +149,7 @@ void setFuelSchedule(struct Schedule *targetSchedule, unsigned long timeout, uns targetSchedule->duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >16x (Each tick represents 16uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -161,7 +162,7 @@ void setFuelSchedule(struct Schedule *targetSchedule, unsigned long timeout, uns targetSchedule->schedulesSet++; //Increment the number of times this schedule has been set //*targetSchedule->compare = targetSchedule->startCompare; - FUEL1_COMPARE = (uint16_t)targetSchedule->startCompare; //Insert corrector compare HERE! + SET_COMPARE(FUEL1_COMPARE, targetSchedule->startCompare); //Insert corrector compare HERE! interrupts(); FUEL1_TIMER_ENABLE(); } @@ -195,10 +196,10 @@ void setFuelSchedule1(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule1.Status = PENDING; //Turn this schedule on fuelSchedule1.schedulesSet++; //Increment the number of times this schedule has been set //Schedule 1 shares a timer with schedule 5 - //if(channel5InjEnabled) { FUEL1_COMPARE = (uint16_t)setQueue(timer3Aqueue, &fuelSchedule1, &fuelSchedule5, FUEL1_COUNTER); } - //else { timer3Aqueue[0] = &fuelSchedule1; timer3Aqueue[1] = &fuelSchedule1; timer3Aqueue[2] = &fuelSchedule1; timer3Aqueue[3] = &fuelSchedule1; FUEL1_COMPARE = (uint16_t)fuelSchedule1.startCompare; } + //if(channel5InjEnabled) { SET_COMPARE(FUEL1_COMPARE, setQueue(timer3Aqueue, &fuelSchedule1, &fuelSchedule5, FUEL1_COUNTER) ); } + //else { timer3Aqueue[0] = &fuelSchedule1; timer3Aqueue[1] = &fuelSchedule1; timer3Aqueue[2] = &fuelSchedule1; timer3Aqueue[3] = &fuelSchedule1; SET_COMPARE(FUEL1_COMPARE, fuelSchedule1.startCompare); } //timer3Aqueue[0] = &fuelSchedule1; timer3Aqueue[1] = &fuelSchedule1; timer3Aqueue[2] = &fuelSchedule1; timer3Aqueue[3] = &fuelSchedule1; - FUEL1_COMPARE = (uint16_t)fuelSchedule1.startCompare; + SET_COMPARE(FUEL1_COMPARE, fuelSchedule1.startCompare); interrupts(); FUEL1_TIMER_ENABLE(); } @@ -233,7 +234,7 @@ void setFuelSchedule2(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule2.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -241,7 +242,7 @@ void setFuelSchedule2(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule2.startCompare = FUEL2_COUNTER + timeout_timer_compare; fuelSchedule2.endCompare = fuelSchedule2.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL2_COMPARE = (uint16_t)fuelSchedule2.startCompare; //Use the B compare unit of timer 3 + SET_COMPARE(FUEL2_COMPARE, fuelSchedule2.startCompare); //Use the B compare unit of timer 3 fuelSchedule2.Status = PENDING; //Turn this schedule on fuelSchedule2.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -271,7 +272,7 @@ void setFuelSchedule3(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule3.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -279,7 +280,7 @@ void setFuelSchedule3(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule3.startCompare = FUEL3_COUNTER + timeout_timer_compare; fuelSchedule3.endCompare = fuelSchedule3.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL3_COMPARE = (uint16_t)fuelSchedule3.startCompare; //Use the C compare unit of timer 3 + SET_COMPARE(FUEL3_COMPARE, fuelSchedule3.startCompare); //Use the C compare unit of timer 3 fuelSchedule3.Status = PENDING; //Turn this schedule on fuelSchedule3.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -309,7 +310,7 @@ void setFuelSchedule4(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule4.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -317,7 +318,7 @@ void setFuelSchedule4(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule4.startCompare = FUEL4_COUNTER + timeout_timer_compare; fuelSchedule4.endCompare = fuelSchedule4.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL4_COMPARE = (uint16_t)fuelSchedule4.startCompare; //Use the B compare unit of timer 4 + SET_COMPARE(FUEL4_COMPARE, fuelSchedule4.startCompare); //Use the B compare unit of timer 4 fuelSchedule4.Status = PENDING; //Turn this schedule on fuelSchedule4.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -345,7 +346,7 @@ void setFuelSchedule5(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule5.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -353,7 +354,7 @@ void setFuelSchedule5(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule5.startCompare = FUEL5_COUNTER + timeout_timer_compare; fuelSchedule5.endCompare = fuelSchedule5.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL5_COMPARE = (uint16_t)fuelSchedule5.startCompare; //Use the C compare unit of timer 4 + SET_COMPARE(FUEL5_COMPARE, fuelSchedule5.startCompare); //Use the C compare unit of timer 4 fuelSchedule5.Status = PENDING; //Turn this schedule on fuelSchedule5.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -382,7 +383,7 @@ void setFuelSchedule6(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule6.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -390,7 +391,7 @@ void setFuelSchedule6(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule6.startCompare = FUEL6_COUNTER + timeout_timer_compare; fuelSchedule6.endCompare = fuelSchedule6.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL6_COMPARE = (uint16_t)fuelSchedule6.startCompare; //Use the A compare unit of timer 4 + SET_COMPARE(FUEL6_COMPARE, fuelSchedule6.startCompare); //Use the A compare unit of timer 4 fuelSchedule6.Status = PENDING; //Turn this schedule on fuelSchedule6.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -419,7 +420,7 @@ void setFuelSchedule7(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule7.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -427,7 +428,7 @@ void setFuelSchedule7(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule7.startCompare = FUEL7_COUNTER + timeout_timer_compare; fuelSchedule7.endCompare = fuelSchedule7.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL7_COMPARE = (uint16_t)fuelSchedule7.startCompare; //Use the C compare unit of timer 5 + SET_COMPARE(FUEL7_COMPARE, fuelSchedule7.startCompare); //Use the C compare unit of timer 5 fuelSchedule7.Status = PENDING; //Turn this schedule on fuelSchedule7.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -456,7 +457,7 @@ void setFuelSchedule8(unsigned long timeout, unsigned long duration) //Uses time fuelSchedule8.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -464,7 +465,7 @@ void setFuelSchedule8(unsigned long timeout, unsigned long duration) //Uses time noInterrupts(); fuelSchedule8.startCompare = FUEL8_COUNTER + timeout_timer_compare; fuelSchedule8.endCompare = fuelSchedule8.startCompare + uS_TO_TIMER_COMPARE(duration); - FUEL8_COMPARE = (uint16_t)fuelSchedule8.startCompare; //Use the B compare unit of timer 5 + SET_COMPARE(FUEL8_COMPARE, fuelSchedule8.startCompare); //Use the B compare unit of timer 5 fuelSchedule8.Status = PENDING; //Turn this schedule on fuelSchedule8.schedulesSet++; //Increment the number of times this schedule has been set interrupts(); @@ -492,7 +493,7 @@ void setIgnitionSchedule1(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule1.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; //timeout -= (micros() - lastCrankAngleCalc); if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case @@ -500,7 +501,7 @@ void setIgnitionSchedule1(void (*startCallback)(), unsigned long timeout, unsign noInterrupts(); ignitionSchedule1.startCompare = IGN1_COUNTER + timeout_timer_compare; //As there is a tick every 4uS, there are timeout/4 ticks until the interrupt should be triggered ( >>2 divides by 4) if(ignitionSchedule1.endScheduleSetByDecoder == false) { ignitionSchedule1.endCompare = ignitionSchedule1.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN1_COMPARE = (uint16_t)ignitionSchedule1.startCompare; + SET_COMPARE(IGN1_COMPARE, ignitionSchedule1.startCompare); ignitionSchedule1.Status = PENDING; //Turn this schedule on ignitionSchedule1.schedulesSet++; interrupts(); @@ -528,7 +529,7 @@ inline void refreshIgnitionSchedule1(unsigned long timeToEnd) { noInterrupts(); ignitionSchedule1.endCompare = IGN1_COUNTER + uS_TO_TIMER_COMPARE(timeToEnd); - IGN1_COMPARE = (uint16_t)ignitionSchedule1.endCompare; + SET_COMPARE(IGN1_COMPARE, ignitionSchedule1.endCompare); interrupts(); } } @@ -542,14 +543,14 @@ void setIgnitionSchedule2(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule2.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule2.startCompare = IGN2_COUNTER + timeout_timer_compare; //As there is a tick every 4uS, there are timeout/4 ticks until the interrupt should be triggered ( >>2 divides by 4) if(ignitionSchedule2.endScheduleSetByDecoder == false) { ignitionSchedule2.endCompare = ignitionSchedule2.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN2_COMPARE = (uint16_t)ignitionSchedule2.startCompare; + SET_COMPARE(IGN2_COMPARE, ignitionSchedule2.startCompare); ignitionSchedule2.Status = PENDING; //Turn this schedule on ignitionSchedule2.schedulesSet++; interrupts(); @@ -577,14 +578,14 @@ void setIgnitionSchedule3(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule3.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule3.startCompare = IGN3_COUNTER + timeout_timer_compare; //As there is a tick every 4uS, there are timeout/4 ticks until the interrupt should be triggered ( >>2 divides by 4) if(ignitionSchedule3.endScheduleSetByDecoder == false) { ignitionSchedule3.endCompare = ignitionSchedule3.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN3_COMPARE = (uint16_t)ignitionSchedule3.startCompare; + SET_COMPARE(IGN3_COMPARE, ignitionSchedule3.startCompare); ignitionSchedule3.Status = PENDING; //Turn this schedule on ignitionSchedule3.schedulesSet++; interrupts(); @@ -612,14 +613,14 @@ void setIgnitionSchedule4(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule4.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule4.startCompare = IGN4_COUNTER + timeout_timer_compare; if(ignitionSchedule4.endScheduleSetByDecoder == false) { ignitionSchedule4.endCompare = ignitionSchedule4.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN4_COMPARE = (uint16_t)ignitionSchedule4.startCompare; + SET_COMPARE(IGN4_COMPARE, ignitionSchedule4.startCompare); ignitionSchedule4.Status = PENDING; //Turn this schedule on ignitionSchedule4.schedulesSet++; interrupts(); @@ -647,14 +648,14 @@ void setIgnitionSchedule5(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule5.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule5.startCompare = IGN5_COUNTER + timeout_timer_compare; if(ignitionSchedule5.endScheduleSetByDecoder == false) { ignitionSchedule5.endCompare = ignitionSchedule5.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN5_COMPARE = (uint16_t)ignitionSchedule5.startCompare; + SET_COMPARE(IGN5_COMPARE, ignitionSchedule5.startCompare); ignitionSchedule5.Status = PENDING; //Turn this schedule on ignitionSchedule5.schedulesSet++; interrupts(); @@ -682,14 +683,14 @@ void setIgnitionSchedule6(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule6.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule6.startCompare = IGN6_COUNTER + timeout_timer_compare; if(ignitionSchedule6.endScheduleSetByDecoder == false) { ignitionSchedule6.endCompare = ignitionSchedule6.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN6_COMPARE = (uint16_t)ignitionSchedule6.startCompare; + SET_COMPARE(IGN6_COMPARE, ignitionSchedule6.startCompare); ignitionSchedule6.Status = PENDING; //Turn this schedule on ignitionSchedule6.schedulesSet++; interrupts(); @@ -717,14 +718,14 @@ void setIgnitionSchedule7(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule7.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule7.startCompare = IGN7_COUNTER + timeout_timer_compare; if(ignitionSchedule7.endScheduleSetByDecoder == false) { ignitionSchedule7.endCompare = ignitionSchedule7.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN7_COMPARE = (uint16_t)ignitionSchedule7.startCompare; + SET_COMPARE(IGN7_COMPARE, ignitionSchedule7.startCompare); ignitionSchedule7.Status = PENDING; //Turn this schedule on ignitionSchedule7.schedulesSet++; interrupts(); @@ -752,14 +753,14 @@ void setIgnitionSchedule8(void (*startCallback)(), unsigned long timeout, unsign ignitionSchedule8.duration = duration; //Need to check that the timeout doesn't exceed the overflow - uint16_t timeout_timer_compare; + COMPARE_TYPE timeout_timer_compare; if (timeout > MAX_TIMER_PERIOD) { timeout_timer_compare = uS_TO_TIMER_COMPARE( (MAX_TIMER_PERIOD - 1) ); } // If the timeout is >4x (Each tick represents 4uS) the maximum allowed value of unsigned int (65535), the timer compare value will overflow when appliedcausing erratic behaviour such as erroneous sparking. else { timeout_timer_compare = uS_TO_TIMER_COMPARE(timeout); } //Normal case noInterrupts(); ignitionSchedule8.startCompare = IGN8_COUNTER + timeout_timer_compare; if(ignitionSchedule8.endScheduleSetByDecoder == false) { ignitionSchedule8.endCompare = ignitionSchedule8.startCompare + uS_TO_TIMER_COMPARE(duration); } //The .endCompare value is also set by the per tooth timing in decoders.ino. The check here is so that it's not getting overridden. - IGN8_COMPARE = (uint16_t)ignitionSchedule8.startCompare; + SET_COMPARE(IGN8_COMPARE, ignitionSchedule8.startCompare); ignitionSchedule8.Status = PENDING; //Turn this schedule on ignitionSchedule8.schedulesSet++; interrupts(); @@ -832,7 +833,7 @@ static inline void fuelSchedule1Interrupt() //Most ARM chips can simply call a f //To use timer queue, change fuelShedule1 to timer3Aqueue[0]; inj1StartFunction(); fuelSchedule1.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL1_COMPARE = (uint16_t)(FUEL1_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule1.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL1_COMPARE, FUEL1_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule1.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule1.Status == RUNNING) { @@ -840,12 +841,12 @@ static inline void fuelSchedule1Interrupt() //Most ARM chips can simply call a f inj1EndFunction(); fuelSchedule1.Status = OFF; //Turn off the schedule fuelSchedule1.schedulesSet = 0; - //FUEL1_COMPARE = (uint16_t)fuelSchedule1.endCompare; + //SET_COMPARE(FUEL1_COMPARE, fuelSchedule1.endCompare); //If there is a next schedule queued up, activate it if(fuelSchedule1.hasNextSchedule == true) { - FUEL1_COMPARE = (uint16_t)fuelSchedule1.nextStartCompare; + SET_COMPARE(FUEL1_COMPARE, fuelSchedule1.nextStartCompare); fuelSchedule1.endCompare = fuelSchedule1.nextEndCompare; fuelSchedule1.Status = PENDING; fuelSchedule1.schedulesSet = 1; @@ -869,7 +870,7 @@ static inline void fuelSchedule2Interrupt() //Most ARM chips can simply call a f //fuelSchedule2.StartCallback(); inj2StartFunction(); fuelSchedule2.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL2_COMPARE = (uint16_t)(FUEL2_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule2.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL2_COMPARE, FUEL2_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule2.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule2.Status == RUNNING) { @@ -881,7 +882,7 @@ static inline void fuelSchedule2Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule2.hasNextSchedule == true) { - FUEL2_COMPARE = (uint16_t)fuelSchedule2.nextStartCompare; + SET_COMPARE(FUEL2_COMPARE, fuelSchedule2.nextStartCompare); fuelSchedule2.endCompare = fuelSchedule2.nextEndCompare; fuelSchedule2.Status = PENDING; fuelSchedule2.schedulesSet = 1; @@ -904,7 +905,7 @@ static inline void fuelSchedule3Interrupt() //Most ARM chips can simply call a f //fuelSchedule3.StartCallback(); inj3StartFunction(); fuelSchedule3.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL3_COMPARE = (uint16_t)(FUEL3_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule3.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL3_COMPARE, FUEL3_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule3.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule3.Status == RUNNING) { @@ -916,7 +917,7 @@ static inline void fuelSchedule3Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule3.hasNextSchedule == true) { - FUEL3_COMPARE = (uint16_t)fuelSchedule3.nextStartCompare; + SET_COMPARE(FUEL3_COMPARE, fuelSchedule3.nextStartCompare); fuelSchedule3.endCompare = fuelSchedule3.nextEndCompare; fuelSchedule3.Status = PENDING; fuelSchedule3.schedulesSet = 1; @@ -939,7 +940,7 @@ static inline void fuelSchedule4Interrupt() //Most ARM chips can simply call a f //fuelSchedule4.StartCallback(); inj4StartFunction(); fuelSchedule4.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL4_COMPARE = (uint16_t)(FUEL4_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule4.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL4_COMPARE, FUEL4_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule4.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule4.Status == RUNNING) { @@ -951,7 +952,7 @@ static inline void fuelSchedule4Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule4.hasNextSchedule == true) { - FUEL4_COMPARE = (uint16_t)fuelSchedule4.nextStartCompare; + SET_COMPARE(FUEL4_COMPARE, fuelSchedule4.nextStartCompare); fuelSchedule4.endCompare = fuelSchedule4.nextEndCompare; fuelSchedule4.Status = PENDING; fuelSchedule4.schedulesSet = 1; @@ -973,7 +974,7 @@ static inline void fuelSchedule5Interrupt() //Most ARM chips can simply call a f { inj5StartFunction(); fuelSchedule5.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL5_COMPARE = (uint16_t)(FUEL5_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule5.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL5_COMPARE, FUEL5_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule5.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule5.Status == RUNNING) { @@ -984,7 +985,7 @@ static inline void fuelSchedule5Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule5.hasNextSchedule == true) { - FUEL5_COMPARE = (uint16_t)fuelSchedule5.nextStartCompare; + SET_COMPARE(FUEL5_COMPARE, fuelSchedule5.nextStartCompare); fuelSchedule5.endCompare = fuelSchedule5.nextEndCompare; fuelSchedule5.Status = PENDING; fuelSchedule5.schedulesSet = 1; @@ -1007,7 +1008,7 @@ static inline void fuelSchedule6Interrupt() //Most ARM chips can simply call a f //fuelSchedule6.StartCallback(); inj6StartFunction(); fuelSchedule6.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL6_COMPARE = (uint16_t)(FUEL6_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule6.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL6_COMPARE, FUEL6_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule6.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule6.Status == RUNNING) { @@ -1019,7 +1020,7 @@ static inline void fuelSchedule6Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule6.hasNextSchedule == true) { - FUEL6_COMPARE = (uint16_t)fuelSchedule6.nextStartCompare; + SET_COMPARE(FUEL6_COMPARE, fuelSchedule6.nextStartCompare); fuelSchedule6.endCompare = fuelSchedule6.nextEndCompare; fuelSchedule6.Status = PENDING; fuelSchedule6.schedulesSet = 1; @@ -1042,7 +1043,7 @@ static inline void fuelSchedule7Interrupt() //Most ARM chips can simply call a f //fuelSchedule7.StartCallback(); inj7StartFunction(); fuelSchedule7.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL7_COMPARE = (uint16_t)(FUEL7_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule7.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL7_COMPARE, FUEL7_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule7.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule7.Status == RUNNING) { @@ -1054,7 +1055,7 @@ static inline void fuelSchedule7Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule7.hasNextSchedule == true) { - FUEL7_COMPARE = (uint16_t)fuelSchedule7.nextStartCompare; + SET_COMPARE(FUEL7_COMPARE, fuelSchedule7.nextStartCompare); fuelSchedule7.endCompare = fuelSchedule7.nextEndCompare; fuelSchedule7.Status = PENDING; fuelSchedule7.schedulesSet = 1; @@ -1077,7 +1078,7 @@ static inline void fuelSchedule8Interrupt() //Most ARM chips can simply call a f //fuelSchedule8.StartCallback(); inj8StartFunction(); fuelSchedule8.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) - FUEL8_COMPARE = (uint16_t)(FUEL8_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule8.duration)); //Doing this here prevents a potential overflow on restarts + SET_COMPARE(FUEL8_COMPARE, FUEL8_COUNTER + uS_TO_TIMER_COMPARE(fuelSchedule8.duration) ); //Doing this here prevents a potential overflow on restarts } else if (fuelSchedule8.Status == RUNNING) { @@ -1089,7 +1090,7 @@ static inline void fuelSchedule8Interrupt() //Most ARM chips can simply call a f //If there is a next schedule queued up, activate it if(fuelSchedule8.hasNextSchedule == true) { - FUEL8_COMPARE = (uint16_t)fuelSchedule8.nextStartCompare; + SET_COMPARE(FUEL8_COMPARE, fuelSchedule8.nextStartCompare); fuelSchedule8.endCompare = fuelSchedule8.nextEndCompare; fuelSchedule8.Status = PENDING; fuelSchedule8.schedulesSet = 1; @@ -1112,8 +1113,8 @@ static inline void ignitionSchedule1Interrupt() //Most ARM chips can simply call ignitionSchedule1.StartCallback(); ignitionSchedule1.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule1.startTime = micros(); - if(ignitionSchedule1.endScheduleSetByDecoder == true) { IGN1_COMPARE = (uint16_t)ignitionSchedule1.endCompare; } - else { IGN1_COMPARE = (uint16_t)(IGN1_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule1.duration)); } //Doing this here prevents a potential overflow on restarts + if(ignitionSchedule1.endScheduleSetByDecoder == true) { SET_COMPARE(IGN1_COMPARE, ignitionSchedule1.endCompare); } + else { SET_COMPARE(IGN1_COMPARE, IGN1_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule1.duration) ); } //Doing this here prevents a potential overflow on restarts } else if (ignitionSchedule1.Status == RUNNING) { @@ -1126,7 +1127,7 @@ static inline void ignitionSchedule1Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule1.hasNextSchedule == true) { - IGN1_COMPARE = (uint16_t)ignitionSchedule1.nextStartCompare; + SET_COMPARE(IGN1_COMPARE, ignitionSchedule1.nextStartCompare); ignitionSchedule1.Status = PENDING; ignitionSchedule1.schedulesSet = 1; ignitionSchedule1.hasNextSchedule = false; @@ -1153,8 +1154,8 @@ static inline void ignitionSchedule2Interrupt() //Most ARM chips can simply call ignitionSchedule2.StartCallback(); ignitionSchedule2.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule2.startTime = micros(); - if(ignitionSchedule2.endScheduleSetByDecoder == true) { IGN2_COMPARE = (uint16_t)ignitionSchedule2.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN2_COMPARE = (uint16_t)(IGN2_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule2.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow that can occur at low RPMs + if(ignitionSchedule2.endScheduleSetByDecoder == true) { SET_COMPARE(IGN2_COMPARE, ignitionSchedule2.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN2_COMPARE, IGN2_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule2.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow that can occur at low RPMs } else if (ignitionSchedule2.Status == RUNNING) { @@ -1167,7 +1168,7 @@ static inline void ignitionSchedule2Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule2.hasNextSchedule == true) { - IGN2_COMPARE = (uint16_t)ignitionSchedule2.nextStartCompare; + SET_COMPARE(IGN2_COMPARE, ignitionSchedule2.nextStartCompare); ignitionSchedule2.Status = PENDING; ignitionSchedule2.schedulesSet = 1; ignitionSchedule2.hasNextSchedule = false; @@ -1194,8 +1195,8 @@ static inline void ignitionSchedule3Interrupt() //Most ARM chips can simply call ignitionSchedule3.StartCallback(); ignitionSchedule3.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule3.startTime = micros(); - if(ignitionSchedule3.endScheduleSetByDecoder == true) { IGN3_COMPARE = (uint16_t)ignitionSchedule3.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN3_COMPARE = (uint16_t)(IGN3_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule3.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow that can occur at low RPMs + if(ignitionSchedule3.endScheduleSetByDecoder == true) { SET_COMPARE(IGN3_COMPARE, ignitionSchedule3.endCompare ); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN3_COMPARE, IGN3_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule3.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow that can occur at low RPMs } else if (ignitionSchedule3.Status == RUNNING) { @@ -1208,7 +1209,7 @@ static inline void ignitionSchedule3Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule3.hasNextSchedule == true) { - IGN3_COMPARE = (uint16_t)ignitionSchedule3.nextStartCompare; + SET_COMPARE(IGN3_COMPARE, ignitionSchedule3.nextStartCompare); ignitionSchedule3.Status = PENDING; ignitionSchedule3.schedulesSet = 1; ignitionSchedule3.hasNextSchedule = false; @@ -1235,8 +1236,8 @@ static inline void ignitionSchedule4Interrupt() //Most ARM chips can simply call ignitionSchedule4.StartCallback(); ignitionSchedule4.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule4.startTime = micros(); - if(ignitionSchedule4.endScheduleSetByDecoder == true) { IGN4_COMPARE = (uint16_t)ignitionSchedule4.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN4_COMPARE = (uint16_t)(IGN4_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule4.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha + if(ignitionSchedule4.endScheduleSetByDecoder == true) { SET_COMPARE(IGN4_COMPARE, ignitionSchedule4.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN4_COMPARE, IGN4_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule4.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha } else if (ignitionSchedule4.Status == RUNNING) { @@ -1249,7 +1250,7 @@ static inline void ignitionSchedule4Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule4.hasNextSchedule == true) { - IGN4_COMPARE = (uint16_t)ignitionSchedule4.nextStartCompare; + SET_COMPARE(IGN4_COMPARE, ignitionSchedule4.nextStartCompare); ignitionSchedule4.Status = PENDING; ignitionSchedule4.schedulesSet = 1; ignitionSchedule4.hasNextSchedule = false; @@ -1276,8 +1277,8 @@ static inline void ignitionSchedule5Interrupt() //Most ARM chips can simply call ignitionSchedule5.StartCallback(); ignitionSchedule5.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule5.startTime = micros(); - if(ignitionSchedule5.endScheduleSetByDecoder == true) { IGN5_COMPARE = (uint16_t)ignitionSchedule5.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN5_COMPARE = (uint16_t)(IGN5_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule5.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha + if(ignitionSchedule5.endScheduleSetByDecoder == true) { SET_COMPARE(IGN5_COMPARE, ignitionSchedule5.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN5_COMPARE, IGN5_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule5.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha } else if (ignitionSchedule5.Status == RUNNING) { @@ -1290,7 +1291,7 @@ static inline void ignitionSchedule5Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule5.hasNextSchedule == true) { - IGN5_COMPARE = (uint16_t)ignitionSchedule5.nextStartCompare; + SET_COMPARE(IGN5_COMPARE, ignitionSchedule5.nextStartCompare); ignitionSchedule5.Status = PENDING; ignitionSchedule5.schedulesSet = 1; ignitionSchedule5.hasNextSchedule = false; @@ -1317,8 +1318,8 @@ static inline void ignitionSchedule6Interrupt() //Most ARM chips can simply call ignitionSchedule6.StartCallback(); ignitionSchedule6.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule6.startTime = micros(); - if(ignitionSchedule6.endScheduleSetByDecoder == true) { IGN6_COMPARE = (uint16_t)ignitionSchedule6.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN6_COMPARE = (uint16_t)(IGN6_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule6.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha + if(ignitionSchedule6.endScheduleSetByDecoder == true) { SET_COMPARE(IGN6_COMPARE, ignitionSchedule6.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN6_COMPARE, IGN6_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule6.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha } else if (ignitionSchedule6.Status == RUNNING) { @@ -1331,7 +1332,7 @@ static inline void ignitionSchedule6Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule6.hasNextSchedule == true) { - IGN6_COMPARE = (uint16_t)ignitionSchedule6.nextStartCompare; + SET_COMPARE(IGN6_COMPARE, ignitionSchedule6.nextStartCompare); ignitionSchedule6.Status = PENDING; ignitionSchedule6.schedulesSet = 1; ignitionSchedule6.hasNextSchedule = false; @@ -1358,8 +1359,8 @@ static inline void ignitionSchedule7Interrupt() //Most ARM chips can simply call ignitionSchedule7.StartCallback(); ignitionSchedule7.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule7.startTime = micros(); - if(ignitionSchedule7.endScheduleSetByDecoder == true) { IGN7_COMPARE = (uint16_t)ignitionSchedule7.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN7_COMPARE = (uint16_t)(IGN7_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule7.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha + if(ignitionSchedule7.endScheduleSetByDecoder == true) { SET_COMPARE(IGN7_COMPARE, ignitionSchedule7.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN7_COMPARE, IGN7_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule7.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha } else if (ignitionSchedule7.Status == RUNNING) { @@ -1372,7 +1373,7 @@ static inline void ignitionSchedule7Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule7.hasNextSchedule == true) { - IGN7_COMPARE = (uint16_t)ignitionSchedule7.nextStartCompare; + SET_COMPARE(IGN7_COMPARE, ignitionSchedule7.nextStartCompare); ignitionSchedule7.Status = PENDING; ignitionSchedule7.schedulesSet = 1; ignitionSchedule7.hasNextSchedule = false; @@ -1399,8 +1400,8 @@ static inline void ignitionSchedule8Interrupt() //Most ARM chips can simply call ignitionSchedule8.StartCallback(); ignitionSchedule8.Status = RUNNING; //Set the status to be in progress (ie The start callback has been called, but not the end callback) ignitionSchedule8.startTime = micros(); - if(ignitionSchedule8.endScheduleSetByDecoder == true) { IGN8_COMPARE = (uint16_t)ignitionSchedule8.endCompare; } //If the decoder has set the end compare value, assign it to the next compare - else { IGN8_COMPARE = (uint16_t)(IGN8_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule8.duration)); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha + if(ignitionSchedule8.endScheduleSetByDecoder == true) { SET_COMPARE(IGN8_COMPARE, ignitionSchedule8.endCompare); } //If the decoder has set the end compare value, assign it to the next compare + else { SET_COMPARE(IGN8_COMPARE, IGN8_COUNTER + uS_TO_TIMER_COMPARE(ignitionSchedule8.duration) ); } //If the decoder based timing isn't set, doing this here prevents a potential overflow tha } else if (ignitionSchedule8.Status == RUNNING) { @@ -1413,7 +1414,7 @@ static inline void ignitionSchedule8Interrupt() //Most ARM chips can simply call //If there is a next schedule queued up, activate it if(ignitionSchedule8.hasNextSchedule == true) { - IGN8_COMPARE = (uint16_t)ignitionSchedule8.nextStartCompare; + SET_COMPARE(IGN8_COMPARE, ignitionSchedule8.nextStartCompare); ignitionSchedule8.Status = PENDING; ignitionSchedule8.schedulesSet = 1; ignitionSchedule8.hasNextSchedule = false; diff --git a/speeduino/timers.h b/speeduino/timers.h index 44c598d5..1cb87a71 100644 --- a/speeduino/timers.h +++ b/speeduino/timers.h @@ -19,6 +19,8 @@ Hence we will preload the timer with 131 cycles to leave 125 until overflow (1ms #ifndef TIMERS_H #define TIMERS_H +#define SET_COMPARE(compare, value) compare = (COMPARE_TYPE)(value) // It is important that we cast this to the actual overflow limit of the timer. The compare variables type can be bigger than the timer overflow. + volatile bool tachoAlt = false; #define TACHO_PULSE_HIGH() *tach_pin_port |= (tach_pin_mask) #define TACHO_PULSE_LOW() *tach_pin_port &= ~(tach_pin_mask)