Fix for fuel interrupt clashes

This commit is contained in:
Josh Stewart 2017-12-05 09:15:17 +11:00
parent 1e432514f9
commit 298ad3f1b4
2 changed files with 22 additions and 6 deletions

View File

@ -25,7 +25,7 @@ See page 136 of the processors datasheet: http://www.atmel.com/Images/doc2549.pd
#ifndef SCHEDULER_H
#define SCHEDULER_H
#define IGNITION_REFRESH_THRESHOLD 30 //Time in uS that the refresh functions will check to ensure there is enough time before changing the end compare
#if defined(CORE_AVR)
#include <avr/interrupt.h>
#include <avr/io.h>
@ -298,7 +298,7 @@ static inline void refreshIgnitionSchedule1(unsigned long timeToEnd) __attribute
static inline void ignitionSchedule5Interrupt();
#endif
enum ScheduleStatus {OFF, PENDING, RUNNING}; //The 3 statuses that a schedule can have
enum ScheduleStatus {OFF, PENDING, STAGED, RUNNING}; //The 3 statuses that a schedule can have
struct Schedule {
volatile unsigned long duration;

View File

@ -444,6 +444,8 @@ void setIgnitionSchedule1(void (*startCallback)(), unsigned long timeout, unsign
static inline void refreshIgnitionSchedule1(unsigned long timeToEnd)
{
if( (ignitionSchedule1.Status == RUNNING) && (timeToEnd < ignitionSchedule1.duration) )
//Must have the threshold check here otherwise it can cause a condition where the compare fires twice, once after the other, both for the end
//if( (timeToEnd < ignitionSchedule1.duration) && (timeToEnd > IGNITION_REFRESH_THRESHOLD) )
{
noInterrupts();
ignitionSchedule1.endCompare = IGN1_COUNTER + uS_TO_TIMER_COMPARE(timeToEnd);
@ -570,7 +572,7 @@ void setIgnitionSchedule5(void (*startCallback)(), unsigned long timeout, unsign
//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
ISR(TIMER3_COMPA_vect, ISR_NOBLOCK) //fuelSchedules 1 and 5
ISR(TIMER3_COMPA_vect) //fuelSchedules 1 and 5
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
static inline void fuelSchedule1Interrupt() //Most ARM chips can simply call a function
#endif
@ -607,7 +609,7 @@ static inline void fuelSchedule1Interrupt() //Most ARM chips can simply call a f
}
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this
ISR(TIMER3_COMPB_vect, ISR_NOBLOCK) //fuelSchedule2
ISR(TIMER3_COMPB_vect) //fuelSchedule2
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
static inline void fuelSchedule2Interrupt() //Most ARM chips can simply call a function
#endif
@ -642,7 +644,7 @@ static inline void fuelSchedule2Interrupt() //Most ARM chips can simply call a f
}
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this
ISR(TIMER3_COMPC_vect, ISR_NOBLOCK) //fuelSchedule3
ISR(TIMER3_COMPC_vect) //fuelSchedule3
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
static inline void fuelSchedule3Interrupt() //Most ARM chips can simply call a function
#endif
@ -679,7 +681,7 @@ static inline void fuelSchedule3Interrupt() //Most ARM chips can simply call a f
}
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) //AVR chips use the ISR for this
ISR(TIMER4_COMPB_vect, ISR_NOBLOCK) //fuelSchedule4
ISR(TIMER4_COMPB_vect) //fuelSchedule4
#elif defined (CORE_TEENSY) || defined(CORE_STM32)
static inline void fuelSchedule4Interrupt() //Most ARM chips can simply call a function
#endif
@ -724,6 +726,20 @@ static inline void ignitionSchedule1Interrupt() //Most ARM chips can simply call
ignitionSchedule1.startTime = micros();
IGN1_COMPARE = ignitionSchedule1.endCompare;
}
/*
//This code is all to do with the staged ignition timing testing. That is, calling this interrupt slightly before the true ignition point and recalculating the end time for more accuracy
IGN1_COMPARE = ignitionSchedule1.endCompare - 20;
ignitionSchedule1.Status = STAGED;
}
else if (ignitionSchedule1.Status == STAGED)
{
int16_t crankAngle = getCrankAngle(timePerDegree);
if(ignition1EndAngle > crankAngle)
{ IGN1_COMPARE = IGN1_COUNTER + uS_TO_TIMER_COMPARE( (ignition1EndAngle - crankAngle) * timePerDegree ); }
else { IGN1_COMPARE = ignitionSchedule1.endCompare; }
ignitionSchedule1.Status = RUNNING;
}*/
else if (ignitionSchedule1.Status == RUNNING)
{
ignitionSchedule1.EndCallback();