Minor memory optimisation

This commit is contained in:
Josh Stewart 2023-12-06 14:47:13 +11:00
parent 4d4b2a87f7
commit 4d4486df4f
7 changed files with 48 additions and 48 deletions

View File

@ -896,7 +896,7 @@ int8_t correctionSoftLaunch(int8_t advance)
{
byte ignSoftLaunchValue = advance;
//SoftCut rev limit for 2-step launch control.
if (configPage6.launchEnabled && clutchTrigger && (currentStatus.clutchEngagedRPM < ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.RPM > ((unsigned int)(configPage6.lnchSoftLim) * 100)) && (currentStatus.TPS >= configPage10.lnchCtrlTPS) )
if (configPage6.launchEnabled && currentStatus.clutchTrigger && (currentStatus.clutchEngagedRPM < ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.RPM > ((unsigned int)(configPage6.lnchSoftLim) * 100)) && (currentStatus.TPS >= configPage10.lnchCtrlTPS) )
{
currentStatus.launchingSoft = true;
BIT_SET(currentStatus.spark, BIT_SPARK_SLAUNCH);
@ -916,7 +916,7 @@ int8_t correctionSoftFlatShift(int8_t advance)
{
int8_t ignSoftFlatValue = advance;
if(configPage6.flatSEnable && clutchTrigger && (currentStatus.clutchEngagedRPM > ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.RPM > (currentStatus.clutchEngagedRPM - (configPage6.flatSSoftWin * 100) ) ) )
if(configPage6.flatSEnable && currentStatus.clutchTrigger && (currentStatus.clutchEngagedRPM > ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.RPM > (currentStatus.clutchEngagedRPM - (configPage6.flatSSoftWin * 100) ) ) )
{
BIT_SET(currentStatus.spark2, BIT_SPARK2_FLATSS);
ignSoftFlatValue = configPage6.flatSRetard;

View File

@ -1326,7 +1326,7 @@ void triggerSetup_4G63(void)
BIT_SET(decoderState, BIT_DECODER_TOOTH_ANG_CORRECT);
BIT_SET(decoderState, BIT_DECODER_HAS_SECONDARY);
MAX_STALL_TIME = 366667UL; //Minimum 50rpm based on the 110 degree tooth spacing
if(initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
//Note that these angles are for every rising and falling edge
if(configPage2.nCylinders == 6)
@ -1780,7 +1780,7 @@ void triggerSetup_24X(void)
toothAngles[23] = 357;
MAX_STALL_TIME = ((MICROS_PER_DEG_1_RPM/50U) * triggerToothAngle); //Minimum 50rpm. (3333uS is the time per degree at 50rpm)
if(initialisationComplete == false) { toothCurrentCount = 25; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the init check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { toothCurrentCount = 25; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the init check to prevent the fuel pump just staying on all the time
BIT_CLEAR(decoderState, BIT_DECODER_2ND_DERIV);
BIT_SET(decoderState, BIT_DECODER_IS_SEQUENTIAL);
BIT_SET(decoderState, BIT_DECODER_TOOTH_ANG_CORRECT);
@ -1890,7 +1890,7 @@ void triggerSetup_Jeep2000(void)
toothAngles[11] = 474;
MAX_STALL_TIME = ((MICROS_PER_DEG_1_RPM/50U) * 60U); //Minimum 50rpm. (3333uS is the time per degree at 50rpm). Largest gap between teeth is 60 degrees.
if(initialisationComplete == false) { toothCurrentCount = 13; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { toothCurrentCount = 13; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
BIT_CLEAR(decoderState, BIT_DECODER_2ND_DERIV);
BIT_CLEAR(decoderState, BIT_DECODER_IS_SEQUENTIAL);
BIT_SET(decoderState, BIT_DECODER_TOOTH_ANG_CORRECT);
@ -2208,7 +2208,7 @@ void triggerSetup_Miata9905(void)
BIT_SET(decoderState, BIT_DECODER_IS_SEQUENTIAL);
triggerActualTeeth = 8;
if(initialisationComplete == false) { secondaryToothCount = 0; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { secondaryToothCount = 0; toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
else { toothLastToothTime = 0; }
toothLastMinusOneToothTime = 0;
@ -3365,7 +3365,7 @@ void triggerSetup_Harley(void)
BIT_CLEAR(decoderState, BIT_DECODER_IS_SEQUENTIAL);
BIT_CLEAR(decoderState, BIT_DECODER_HAS_SECONDARY);
MAX_STALL_TIME = ((MICROS_PER_DEG_1_RPM/50U) * 60U); //Minimum 50rpm. (3333uS is the time per degree at 50rpm)
if(initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initial check to prevent the fuel pump just staying on all the time
triggerFilterTime = 1500;
}
@ -4646,7 +4646,7 @@ void triggerSetup_Vmax(void)
BIT_CLEAR(decoderState, BIT_DECODER_IS_SEQUENTIAL);
BIT_CLEAR(decoderState, BIT_DECODER_HAS_SECONDARY);
MAX_STALL_TIME = ((MICROS_PER_DEG_1_RPM/50U) * 60U); //Minimum 50rpm. (3333uS is the time per degree at 50rpm)
if(initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initi check to prevent the fuel pump just staying on all the time
if(currentStatus.initialisationComplete == false) { toothLastToothTime = micros(); } //Set a startup value here to avoid filter errors when starting. This MUST have the initi check to prevent the fuel pump just staying on all the time
triggerFilterTime = 1500;
BIT_SET(decoderState, BIT_DECODER_VALID_TRIGGER); // We must start with a valid trigger or we cannot start measuring the lobe width. We only have a false trigger on the lobe up event when it doesn't pass the filter. Then, the lobe width will also not be beasured.
toothAngles[1] = 0; //tooth #1, these are the absolute tooth positions

View File

@ -518,7 +518,7 @@ extern byte triggerInterrupt;
extern byte triggerInterrupt2;
extern byte triggerInterrupt3;
extern bool initialisationComplete; //Tracks whether the setup() function has run completely
extern byte fpPrimeTime; //The time (in seconds, based on currentStatus.secl) that the fuel pump started priming
extern uint8_t softLimitTime; //The time (in 0.1 seconds, based on seclx10) that the soft limiter started
extern volatile uint16_t mainLoopCount;
@ -526,12 +526,8 @@ extern unsigned long revolutionTime; //The time in uS that one revolution would
extern volatile unsigned long timer5_overflow_count; //Increments every time counter 5 overflows. Used for the fast version of micros()
extern volatile unsigned long ms_counter; //A counter that increments once per ms
extern uint16_t fixedCrankingOverride;
extern bool clutchTrigger;
extern bool previousClutchTrigger;
extern volatile uint32_t toothHistory[TOOTH_LOG_SIZE];
extern volatile uint8_t compositeLogHistory[TOOTH_LOG_SIZE];
extern volatile bool fpPrimed; //Tracks whether or not the fuel pump priming has been completed yet
extern volatile bool injPrimed; //Tracks whether or not the injector priming has been completed yet
extern volatile unsigned int toothHistoryIndex;
extern unsigned long currentLoopTime; /**< The time (in uS) that the current mainloop started */
extern volatile uint16_t ignitionCount; /**< The count of ignition events that have taken place since the engine started */
@ -555,10 +551,7 @@ extern volatile byte HWTest_IGN; /**< Each bit in this variable represents
extern volatile byte HWTest_IGN_Pulsed; /**< Each bit in this variable represents one of the ignition channels and it's 50% HW test status */
extern byte maxIgnOutputs; /**< Number of ignition outputs being used by the current tune configuration */
extern byte maxInjOutputs; /**< Number of injection outputs being used by the current tune configuration */
extern byte resetControl; ///< resetControl needs to be here (as global) because using the config page (4) directly can prevent burning the setting
extern volatile byte TIMER_mask;
extern volatile byte LOOP_TIMER;
@ -576,8 +569,16 @@ extern volatile byte LOOP_TIMER;
* unit based values in similar variable(s) without ADC part in name (see sensors.ino for reading of sensors).
*/
struct statuses {
volatile bool hasSync; /**< Flag for crank/cam position being known by decoders (See decoders.ino).
This is used for sanity checking e.g. before logging tooth history or reading some sensors and computing readings. */
volatile bool hasSync : 1; /**< Flag for crank/cam position being known by decoders (See decoders.ino).
This is used for sanity checking e.g. before logging tooth history or reading some sensors and computing readings. */
bool initialisationComplete : 1; //Tracks whether the setup() function has run completely
bool clutchTrigger : 1;
bool previousClutchTrigger : 1;
volatile bool fpPrimed : 1; //Tracks whether or not the fuel pump priming has been completed yet
volatile bool injPrimed : 1; //Tracks whether or not the injector priming has been completed yet
volatile bool tachoSweepEnabled : 1;
volatile bool tachoAlt : 1;
uint16_t RPM; ///< RPM - Current Revs per minute
byte RPMdiv100; ///< RPM value scaled (divided by 100) to fit a byte (0-255, e.g. 12000 => 120)
long longRPM; ///< RPM as long int (gets assigned to / maintained in statuses.RPM as well)

View File

@ -50,8 +50,8 @@
*/
void initialiseAll(void)
{
fpPrimed = false;
injPrimed = false;
currentStatus.fpPrimed = false;
currentStatus.injPrimed = false;
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
@ -1343,19 +1343,19 @@ void initialiseAll(void)
FUEL_PUMP_ON();
currentStatus.fuelPumpOn = true;
}
else { fpPrimed = true; } //If the user has set 0 for the pump priming, immediately mark the priming as being completed
else { currentStatus.fpPrimed = true; } //If the user has set 0 for the pump priming, immediately mark the priming as being completed
interrupts();
readCLT(false); // Need to read coolant temp to make priming pulsewidth work correctly. The false here disables use of the filter
readTPS(false); // Need to read tps to detect flood clear state
/* tacho sweep function. */
tachoSweepEnabled = (configPage2.useTachoSweep > 0);
//tachoStatus.tachoSweepEnabled = (configPage2.useTachoSweep > 0);
/* SweepMax is stored as a byte, RPM/100. divide by 60 to convert min to sec (net 5/3). Multiply by ignition pulses per rev.
tachoSweepIncr is also the number of tach pulses per second */
tachoSweepIncr = configPage2.tachoSweepMaxRPM * maxIgnOutputs * 5 / 3;
initialisationComplete = true;
currentStatus.initialisationComplete = true;
digitalWrite(LED_BUILTIN, HIGH);
}

View File

@ -241,7 +241,7 @@ void instanteneousMAPReading(void)
else { mapErrorCount = 0; }
//During startup a call is made here to get the baro reading. In this case, we can't apply the ADC filter
if(initialisationComplete == true) { currentStatus.mapADC = ADC_FILTER(tempReading, configPage4.ADCFILTER_MAP, currentStatus.mapADC); } //Very weak filter
if(currentStatus.initialisationComplete == true) { currentStatus.mapADC = ADC_FILTER(tempReading, configPage4.ADCFILTER_MAP, currentStatus.mapADC); } //Very weak filter
else { currentStatus.mapADC = tempReading; } //Baro reading (No filter)
currentStatus.MAP = fastMap10Bit(currentStatus.mapADC, configPage2.mapMin, configPage2.mapMax); //Get the current MAP value
@ -555,7 +555,7 @@ void readBaro(void)
tempReading = analogRead(pinBaro);
#endif
if(initialisationComplete == true) { currentStatus.baroADC = ADC_FILTER(tempReading, configPage4.ADCFILTER_BARO, currentStatus.baroADC); }//Very weak filter
if(currentStatus.initialisationComplete == true) { currentStatus.baroADC = ADC_FILTER(tempReading, configPage4.ADCFILTER_BARO, currentStatus.baroADC); }//Very weak filter
else { currentStatus.baroADC = tempReading; } //Baro reading (No filter)
currentStatus.baro = fastMap10Bit(currentStatus.baroADC, configPage2.baroMin, configPage2.baroMax); //Get the current MAP value
@ -658,7 +658,7 @@ void readBat(void)
{
//Re-prime the fuel pump
fpPrimeTime = currentStatus.secl;
fpPrimed = false;
currentStatus.fpPrimed = false;
FUEL_PUMP_ON();
//Redo the stepper homing

View File

@ -62,7 +62,7 @@ uint16_t staged_req_fuel_mult_sec = 0;
#ifndef UNIT_TEST // Scope guard for unit testing
void setup(void)
{
initialisationComplete = false; //Tracks whether the initialiseAll() function has run completely
currentStatus.initialisationComplete = false; //Tracks whether the initialiseAll() function has run completely
initialiseAll();
}
@ -105,7 +105,7 @@ void loop(void)
{
serialReceive();
}
//Check for any CAN comms requiring action
#if defined(secondarySerial_AVAILABLE)
//if can or secondary serial interface is enabled then check for requests.
@ -114,7 +114,7 @@ void loop(void)
if ( ((mainLoopCount & 31) == 1) || (secondarySerial.available() > SERIAL_BUFFER_THRESHOLD) )
{
if (secondarySerial.available() > 0) { secondserial_Command(); }
}
}
}
#endif
#if defined (NATIVE_CAN_AVAILABLE)
@ -170,7 +170,7 @@ void loop(void)
ignitionCount = 0;
ignitionChannelsOn = 0;
fuelChannelsOn = 0;
if (fpPrimed == true) { FUEL_PUMP_OFF(); currentStatus.fuelPumpOn = false; } //Turn off the fuel pump, but only if the priming is complete
if (currentStatus.fpPrimed == true) { FUEL_PUMP_OFF(); currentStatus.fuelPumpOn = false; } //Turn off the fuel pump, but only if the priming is complete
if (configPage6.iacPWMrun == false) { disableIdle(); } //Turn off the idle PWM
BIT_CLEAR(currentStatus.engine, BIT_ENGINE_CRANK); //Clear cranking bit (Can otherwise get stuck 'on' even with 0 rpm)
BIT_CLEAR(currentStatus.engine, BIT_ENGINE_WARMUP); //Same as above except for WUE
@ -189,7 +189,6 @@ void loop(void)
boostDisable();
if(configPage4.ignBypassEnabled > 0) { digitalWrite(pinIgnBypass, LOW); } //Reset the ignition bypass ready for next crank attempt
}
//***Perform sensor reads***
//-----------------------------------------------------------------------------------------------------
if (BIT_CHECK(LOOP_TIMER, BIT_TIMER_1KHZ)) //Every 1ms. NOTE: This is NOT guaranteed to run at 1kHz on AVR systems. It will run at 1kHz if possible or as fast as loops/s allows if not.
@ -200,7 +199,10 @@ void loop(void)
if(BIT_CHECK(LOOP_TIMER, BIT_TIMER_200HZ))
{
BIT_CLEAR(TIMER_mask, BIT_TIMER_200HZ);
BIT_SET(ADCSRA,ADIE); //Enable ADC
#if defined(ANALOG_ISR)
//ADC in free running mode does 1 complete conversion of all 16 channels and then the interrupt is disabled. Every 200Hz we re-enable the interrupt to get another conversion cycle
BIT_SET(ADCSRA,ADIE); //Enable ADC interrupt
#endif
}
if (BIT_CHECK(LOOP_TIMER, BIT_TIMER_15HZ)) //Every 32 loops
@ -1635,21 +1637,21 @@ void calculateStaging(uint32_t pwLimit)
void checkLaunchAndFlatShift()
{
//Check for launching/flat shift (clutch) based on the current and previous clutch states
previousClutchTrigger = clutchTrigger;
currentStatus.previousClutchTrigger = currentStatus.clutchTrigger;
//Only check for pinLaunch if any function using it is enabled. Else pins might break starting a board
if(configPage6.flatSEnable || configPage6.launchEnabled)
{
if(configPage6.launchHiLo > 0) { clutchTrigger = digitalRead(pinLaunch); }
else { clutchTrigger = !digitalRead(pinLaunch); }
if(configPage6.launchHiLo > 0) { currentStatus.clutchTrigger = digitalRead(pinLaunch); }
else { currentStatus.clutchTrigger = !digitalRead(pinLaunch); }
}
if(clutchTrigger && (previousClutchTrigger != clutchTrigger) ) { currentStatus.clutchEngagedRPM = currentStatus.RPM; } //Check whether the clutch has been engaged or disengaged and store the current RPM if so
if(currentStatus.clutchTrigger && (currentStatus.previousClutchTrigger != currentStatus.clutchTrigger) ) { currentStatus.clutchEngagedRPM = currentStatus.RPM; } //Check whether the clutch has been engaged or disengaged and store the current RPM if so
//Default flags to off
currentStatus.launchingHard = false;
BIT_CLEAR(currentStatus.spark, BIT_SPARK_HLAUNCH);
currentStatus.flatShiftingHard = false;
if (configPage6.launchEnabled && clutchTrigger && (currentStatus.clutchEngagedRPM < ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.TPS >= configPage10.lnchCtrlTPS) )
if (configPage6.launchEnabled && currentStatus.clutchTrigger && (currentStatus.clutchEngagedRPM < ((unsigned int)(configPage6.flatSArm) * 100)) && (currentStatus.TPS >= configPage10.lnchCtrlTPS) )
{
//Check whether RPM is above the launch limit
uint16_t launchRPMLimit = (configPage6.lnchHardLim * 100);
@ -1665,7 +1667,7 @@ void checkLaunchAndFlatShift()
else
{
//If launch is not active, check whether flat shift should be active
if(configPage6.flatSEnable && clutchTrigger && (currentStatus.clutchEngagedRPM >= ((unsigned int)(configPage6.flatSArm * 100)) ) )
if(configPage6.flatSEnable && currentStatus.clutchTrigger && (currentStatus.clutchEngagedRPM >= ((unsigned int)(configPage6.flatSArm * 100)) ) )
{
uint16_t flatRPMLimit = currentStatus.clutchEngagedRPM;
if( (configPage2.hardCutType == HARD_CUT_ROLLING) ) { flatRPMLimit += (configPage15.rollingProtRPMDelta[0] * 10); } //Add the rolling cut delta if enabled (Delta is a negative value)

View File

@ -37,11 +37,8 @@ volatile unsigned int dwellLimit_uS;
volatile uint8_t tachoEndTime; //The time (in ms) that the tacho pulse needs to end at
volatile TachoOutputStatus tachoOutputFlag;
volatile bool tachoSweepEnabled;
volatile bool tachoAlt = false;
volatile uint16_t tachoSweepIncr;
volatile uint16_t tachoSweepAccum;
volatile uint8_t testInjectorPulseCount = 0;
volatile uint8_t testIgnitionPulseCount = 0;
@ -120,9 +117,9 @@ void oneMSInterval(void) //Most ARM chips can simply call a function
//Tacho is flagged as being ready for a pulse by the ignition outputs, or the sweep interval upon startup
// See if we're in power-on sweep mode
if( tachoSweepEnabled )
if( currentStatus.tachoSweepEnabled )
{
if( (currentStatus.engine != 0) || (ms_counter >= TACHO_SWEEP_TIME_MS) ) { tachoSweepEnabled = false; } // Stop the sweep after SWEEP_TIME, or if real tach signals have started
if( (currentStatus.engine != 0) || (ms_counter >= TACHO_SWEEP_TIME_MS) ) { currentStatus.tachoSweepEnabled = false; } // Stop the sweep after SWEEP_TIME, or if real tach signals have started
else
{
// Ramp the needle smoothly to the max over the SWEEP_RAMP time
@ -142,7 +139,7 @@ void oneMSInterval(void) //Most ARM chips can simply call a function
if(tachoOutputFlag == READY)
{
//Check for half speed tacho
if( (configPage2.tachoDiv == 0) || (tachoAlt == true) )
if( (configPage2.tachoDiv == 0) || (currentStatus.tachoAlt == true) )
{
TACHO_PULSE_LOW();
//ms_counter is cast down to a byte as the tacho duration can only be in the range of 1-6, so no extra resolution above that is required
@ -154,7 +151,7 @@ void oneMSInterval(void) //Most ARM chips can simply call a function
//Don't run on this pulse (Half speed tacho)
tachoOutputFlag = TACHO_INACTIVE;
}
tachoAlt = !tachoAlt; //Flip the alternating value in case half speed tacho is in use.
currentStatus.tachoAlt = !currentStatus.tachoAlt; //Flip the alternating value in case half speed tacho is in use.
}
else if(tachoOutputFlag == ACTIVE)
{
@ -226,7 +223,7 @@ void oneMSInterval(void) //Most ARM chips can simply call a function
if ( BIT_CHECK(currentStatus.engine, BIT_ENGINE_RUN) ) { runSecsX10++; }
else { runSecsX10 = 0; }
if ( (injPrimed == false) && (seclx10 == configPage2.primingDelay) && (currentStatus.RPM == 0) ) { beginInjectorPriming(); injPrimed = true; }
if ( (currentStatus.injPrimed == false) && (seclx10 == configPage2.primingDelay) && (currentStatus.RPM == 0) ) { beginInjectorPriming(); currentStatus.injPrimed = true; }
seclx10++;
}
@ -272,12 +269,12 @@ void oneMSInterval(void) //Most ARM chips can simply call a function
}
//Check whether fuel pump priming is complete
if(fpPrimed == false)
if(currentStatus.fpPrimed == false)
{
//fpPrimeTime is the time that the pump priming started. This is 0 on startup, but can be changed if the unit has been running on USB power and then had the ignition turned on (Which starts the priming again)
if( (currentStatus.secl - fpPrimeTime) >= configPage2.fpPrime)
{
fpPrimed = true; //Mark the priming as being completed
currentStatus.fpPrimed = true; //Mark the priming as being completed
if(currentStatus.RPM == 0)
{
//If we reach here then the priming is complete, however only turn off the fuel pump if the engine isn't running