#ifndef GLOBALS_H #define GLOBALS_H #include const byte ms_version = 20; const byte signature = 20; const byte data_structure_version = 2; //This identifies the data structure when reading / writing. const byte page_size = 64; const int map_page_size = 288; //Handy bitsetting macros #define BIT_SET(a,b) ((a) |= (1<<(b))) #define BIT_CLEAR(a,b) ((a) &= ~(1<<(b))) #define BIT_CHECK(var,pos) ((var) & (1<<(pos))) //Define bit positions within engine virable #define BIT_ENGINE_RUN 0 // Engine running #define BIT_ENGINE_CRANK 1 // Engine cranking #define BIT_ENGINE_ASE 2 // after start enrichment (ASE) #define BIT_ENGINE_WARMUP 3 // Engine in warmup #define BIT_ENGINE_ACC 4 // in TPS acceleration mode #define BIT_ENGINE_DCC 5 // in deceleration mode #define BIT_ENGINE_MAP 6 // in MAP acceleration mode #define BIT_ENGINE_IDLE 7 // idle on //Define masks for Squirt #define BIT_SQUIRT_INJ1 0 //inj1 Squirt #define BIT_SQUIRT_INJ2 1 //inj2 Squirt #define BIT_SQUIRT_INJ3 2 //inj3 Squirt #define BIT_SQUIRT_INJ4 3 //inj4 Squirt #define BIT_SQUIRT_INJ2SCHED 4 #define BIT_SQUIRT_INJ2SQRT 5 //Injector2 (Schedule2) #define BIT_SQUIRT_TOOTHLOG1READY 6 //Used to flag if tooth log 1 is ready #define BIT_SQUIRT_TOOTHLOG2READY 7 //Used to flag if tooth log 2 is ready (Log is not currently used) #define TOOTH_LOG_SIZE 256 #define TOOTH_LOG_BUFFER 512 #define SIZE_BYTE 8 #define SIZE_INT 16 //Table sizes #define CALIBRATION_TABLE_SIZE 512 #define CALIBRATION_TEMPERATURE_OFFSET 40 // All temperature measurements are stored offset by 40 degrees. This is so we can use an unsigned byte (0-255) to represent temperature ranges from -40 to 215 #define SERIAL_BUFFER_THRESHOLD 32 // When the serial buffer is filled to greater than this threshold value, the serial processing operations will be performed more urgently in order to avoid it overflowing. Serial buffer is 64 bytes long, so the threshold is set at half this as a reasonable figure //These are for the direct port manipulation of the injectors and coils volatile byte *inj1_pin_port; volatile byte inj1_pin_mask; volatile byte *inj2_pin_port; volatile byte inj2_pin_mask; volatile byte *inj3_pin_port; volatile byte inj3_pin_mask; volatile byte *inj4_pin_port; volatile byte inj4_pin_mask; volatile byte *ign1_pin_port; volatile byte ign1_pin_mask; //The status struct contains the current values for all 'live' variables //In current version this is 64 bytes struct statuses { volatile boolean hasSync; unsigned int RPM; int mapADC; int MAP; byte TPS; //The current TPS reading (0% - 100%) byte TPSlast; //The previous TPS reading unsigned long TPS_time; //The time the TPS sample was taken unsigned long TPSlast_time; //The time the previous TPS sample was taken byte tpsADC; //0-255 byte representation of the TPS byte tpsDOT; int rpmDOT; byte VE; byte O2; byte O2_2; int coolant; int cltADC; int IAT; int iatADC; int batADC; int O2ADC; int O2_2ADC; int dwell; byte dwellCorrection; //The amount of correction being applied to the dwell time. byte battery10; //The current BRV in volts (multiplied by 10. Eg 12.5V = 125) byte advance; byte corrections; byte TAEamount; //The amount of accleration enrichment currently being applied byte egoCorrection; //The amount of closed loop AFR enrichment currently being applied byte wueCorrection; //The amount of warmup enrichment currently being applied byte batCorrection; //The amount of battery voltage enrichment currently being applied byte iatCorrection; //The amount of inlet air temperature adjustment currently being applied byte launchCorrection; //The amount of correction being applied if launch control is active byte afrTarget; unsigned long TAEEndTime; //The target end time used whenever TAE is turned on volatile byte squirt; volatile byte spark; byte engine; unsigned int PW; //In uS volatile byte runSecs; //Counter of seconds since cranking commenced (overflows at 255 obviously) volatile byte secl; //Continous volatile int loopsPerSecond; boolean launching; //True when in launch control mode int freeRAM; //Helpful bitwise operations: //Useful reference: http://playground.arduino.cc/Code/BitMath // y = (x >> n) & 1; // n=0..15. stores nth bit of x in y. y becomes 0 or 1. // x &= ~(1 << n); // forces nth bit of x to be 0. all other bits left alone. // x |= (1 << n); // forces nth bit of x to be 1. all other bits left alone. }; //Page 1 of the config - See the ini file for further reference //This mostly covers off variables that are required for fuel struct config1 { byte crankCold; //Cold cranking pulsewidth modifier. This is added to the fuel pulsewidth when cranking under a certain temp threshold (ms) byte crankHot; //Warm cranking pulsewidth modifier. This is added to the fuel pulsewidth when cranking (ms) byte asePct; //Afterstart enrichment (%) byte aseCount; //Afterstart enrichment cycles. This is the number of ignition cycles that the afterstart enrichment % lasts for byte wueValues[10]; //Warm up enrichment array (10 bytes) byte crankingPct; //Cranking enrichment byte pinMapping; // The board / ping mapping to be used byte unused16; byte tdePct; // TPS decelleration (%) byte taeColdA; byte tpsThresh; byte taeTime; //Display config bits byte displayType : 3; byte display1 : 3; byte display2 : 2; byte display3 : 3; byte display4 : 2; byte display5 : 3; byte displayB1 : 4; byte displayB2 : 4; byte reqFuel; byte divider; byte alternate; byte injOpen; //Injector opening time (ms * 10) unsigned int inj1Ang; unsigned int inj2Ang; unsigned int inj3Ang; unsigned int inj4Ang; //config1 in ini byte mapSample : 2; byte strokes : 1; byte injType : 1; byte nCylinders : 4; //Number of cylinders //config2 in ini byte cltType : 2; byte matType : 2; byte nInjectors : 4; //Number of injectors //config3 in ini byte engineType : 1; byte egoType : 1; byte algorithm : 1; //"Speed Density", "Alpha-N" byte baroCorr : 1; byte injTiming : 2; byte primePulse; byte dutyLim; byte unused41; byte egoSwitch; byte taeColdM; byte tpsMin; byte tpsMax; byte mapMin; unsigned int mapMax; byte unused49; byte unused50; byte unused51; byte unused52; byte unused53; byte unused54; byte unused55; byte unused56; byte unused57; byte unused58; byte unused59; byte unused60; byte unused61; byte unused62; byte unused63; }; //Page 2 of the config - See the ini file for further reference //This mostly covers off variables that are required for ignition struct config2 { int triggerAngle; byte FixAng; byte CrankAng; byte IgHold; byte TrigEdge : 1; byte TrigSpeed : 1; byte IgInv : 1; byte oddfire : 1; byte TrigPattern : 4; byte IdleAdv; byte IdleAdvTPS; byte IdleAdvRPM; byte IdleAdvCLT; //The temperature below which the idle is advanced byte IdleDelayTime; byte StgCycles; //The number of initial cycles before the ignition should fire when first cranking byte dwellCont : 1; //Fixed duty dwell control byte useDwellLim : 1; //Whether the dwell limiter is off or on byte sparkMode : 2; //Spark output mode (Eg Wasted spark, single channel or Wasted COP) byte dwellUnused : 4; byte dwellCrank; //Dwell time whilst cranking byte dwellRun; //Dwell time whilst running byte triggerTeeth; //The full count of teeth on the trigger wheel if there were no gaps byte triggerMissingTeeth; //The size of the tooth gap (ie number of missing teeth) byte crankRPM; //RPM below which the engine is considered to be cranking byte floodClear; //TPS value that triggers flood clear mode (No fuel whilst cranking) byte SoftRevLim; //Soft rev limit (RPM/100) byte SoftLimRetard; //Amount soft limit retards (degrees) byte SoftLimMax; //Time the soft limit can run byte HardRevLim; //Hard rev limit (RPM/100) byte taeBins[4]; //TPS based acceleration enrichment bins (%/s) byte taeValues[4]; //TPS based acceleration enrichment rates (% to add) byte wueBins[10]; //Warmup Enrichment bins (Values are in configTable1) byte dwellLimit; byte dwellCorrectionValues[6]; //Correction table for dwell vs battery voltage byte unused48; byte unused49; byte unused50; byte unused51; byte unused52; byte unused53; byte unused54; byte unused55; byte unused56; byte unused57; byte unused58; byte unused59; byte unused60; byte unused61; byte unused62; byte unused63; }; //Page 3 of the config - See the ini file for further reference //This mostly covers off variables that are required for AFR targets and closed loop struct config3 { byte egoAlgorithm : 2; byte egoType : 2; byte boostEnabled : 1; byte vvtEnabled : 1; byte unused : 2; byte egoKP; byte egoKI; byte egoKD; byte egoTemp; //The temperature above which closed loop functions byte egoCount; //The number of ignition cylces per step byte egoDelta; //The step size (In %) when using simple algorithm byte egoLimit; //Maximum amount the closed loop will vary the fueling byte ego_min; //AFR must be above this for closed loop to function byte ego_max; //AFR must be below this for closed loop to function byte ego_sdelay; //Time in seconds after engine starts that closed loop becomes available byte egoRPM; //RPM must be above this for closed loop to function byte egoTPSMax; //TPS must be below this for closed loop to function byte egoLoadMax; //Load (TPS or MAP) must be below this for closed loop to function byte egoLoadMin; //Load (TPS or MAP) must be above this for closed loop to function byte voltageCorrectionBins[6]; //X axis bins for voltage correction tables byte injVoltageCorrectionValues[6]; //Correction table for injector PW vs battery voltage byte airDenBins[9]; byte airDenRates[9]; byte boostFreq; //Frequency of the boost PWM valve byte vvtFreq; //Frequency of the vvt PWM valve byte idleFreq; byte launchPin : 6; byte launchEnabled : 1; byte unused48h : 1; byte lnchSoftLim; byte lnchRetard; byte lnchHardLim; byte lnchFuelAdd; byte unused53; byte unused54; byte unused55; byte unused56; byte unused57; byte unused58; byte unused59; byte unused60; byte unused61; byte unused62; byte unused63; }; //Page 4 of the config mostly deals with idle control //See ini file for further info (Config Page 7 in the ini) struct config4 { byte iacCLValues[10]; //Closed loop target RPM value byte iacOLStepVal[10]; //Open loop step values for stepper motors byte iacOLPWMVal[10]; //Open loop duty values for PMWM valves byte iacBins[10]; //Temperature Bins for the above 3 curves byte iacCrankSteps[4]; //Steps to use when cranking (Stepper motor) byte iacCrankDuty[4]; //Duty cycle to use on PWM valves when cranking byte iacCrankBins[4]; //Temperature Bins for the above 2 curves byte iacAlgorithm : 3; //Valid values are: "None", "On/Off", "PWM", "PWM Closed Loop", "Stepper", "Stepper Closed Loop" byte iacStepTime : 3; //How long to pulse the stepper for to ensure the step completes (ms) byte unused52 : 2; byte iacFastTemp; //Fast idle temp when using a simple on/off valve byte iacStepHome; //When using a stepper motor, the number of steps to be taken on startup to home the motor byte iacStepHyster; //Hysteresis temperature (*10). Eg 2.2C = 22 byte fanInv : 1; // Fan output inversion bit byte fanEnable : 1; // Fan enable bit byte unused : 6; byte fanSP; // Cooling fan start temperature byte fanHyster; // Fan hysteresis }; byte pinInjector1; //Output pin injector 1 byte pinInjector2; //Output pin injector 2 byte pinInjector3; //Output pin injector 3 is on byte pinInjector4; //Output pin injector 4 is on byte pinInjector5; //Placeholder only - NOT USED byte pinInjector6; //Placeholder only - NOT USED byte pinInjector7; //Placeholder only - NOT USED byte pinInjector8; //Placeholder only - NOT USED byte pinCoil1; //Pin for coil 1 byte pinCoil2; //Pin for coil 2 byte pinCoil3; //Pin for coil 3 byte pinCoil4; //Pin for coil 4 byte pinTrigger; //The CAS pin byte pinTrigger2; //The Cam Sensor pin byte pinTrigger3; //the 2nd cam sensor pin byte pinTPS;//TPS input pin byte pinMAP; //MAP sensor pin byte pinMAP2; //2nd MAP sensor (Currently unused) byte pinIAT; //IAT sensor pin byte pinCLT; //CLS sensor pin byte pinO2; //O2 Sensor pin byte pinO2_2; //second O2 pin byte pinBat; //O2 Sensor pin byte pinDisplayReset; // OLED reset pin byte pinTachOut; //Tacho output byte pinFuelPump; //Fuel pump on/off byte pinIdle1; //Single wire idle control byte pinIdle2; //2 wire idle control (Not currently used) byte pinSpareTemp1; // Future use only byte pinSpareTemp2; // Future use only byte pinSpareOut1; //Generic output byte pinSpareOut2; //Generic output byte pinSpareOut3; //Generic output byte pinSpareOut4; //Generic output byte pinSpareOut5; //Generic output byte pinSpareOut6; //Generic output byte pinSpareHOut1; //spare high current output byte pinSpareHOut2; // spare high current output byte pinSpareLOut1; // spare low current output byte pinSpareLOut2; // spare low current output byte pinSpareLOut3; byte pinSpareLOut4; byte pinSpareLOut5; byte pinBoost; byte pinVVT_1; // vvt output 1 byte pinVVt_2; // vvt output 2 byte pinFan; // Cooling fan output byte pinStepperDir; //Direction pin for the stepper motor driver byte pinStepperStep; //Step pin for the stepper motor driver byte pinLaunch; // global variables // from speeduino.ino extern struct statuses currentStatus; // from speeduino.ino extern struct table3D fuelTable; //16x16 fuel map extern struct table3D ignitionTable; //16x16 ignition map extern struct table3D afrTable; //16x16 afr target map extern struct table2D taeTable; //4 bin TPS Acceleration Enrichment map (2D) extern struct table2D WUETable; //10 bin Warm Up Enrichment map (2D) extern struct config1 configPage1; extern struct config2 configPage2; extern struct config3 configPage3; extern unsigned long currentLoopTime; //The time the current loop started (uS) extern unsigned long previousLoopTime; //The time the previous loop started (uS) extern byte ignitionCount; extern byte cltCalibrationTable[CALIBRATION_TABLE_SIZE]; extern byte iatCalibrationTable[CALIBRATION_TABLE_SIZE]; extern byte o2CalibrationTable[CALIBRATION_TABLE_SIZE]; #endif // GLOBALS_H