00001 /* Copyright 2008 Fred Cooke 00002 00003 This file is part of the FreeEMS project. 00004 00005 FreeEMS software is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 FreeEMS software is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with any FreeEMS software. If not, see http://www.gnu.org/licenses/ 00017 00018 We ask that if you make any changes to this file you email them upstream to 00019 us at admin(at)diyefi(dot)org or, even better, fork the code on github.com! 00020 00021 Thank you for choosing FreeEMS to run your engine! */ 00022 00023 00040 /* Header file multiple inclusion protection courtesy eclipse Header Template */ 00041 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */ 00042 #ifndef FILE_FREEEMS_H_SEEN 00043 #define FILE_FREEEMS_H_SEEN 00044 00045 00046 /* Include top level files that everything else relies on */ 00047 #include "memory.h" 00048 #include "9S12XDP512.h" 00049 #include "flashGlobals.h" 00050 00051 00052 /* Include define files at the top here as other includes use them */ 00053 #include "errorDefines.h" 00054 #include "globalDefines.h" 00055 00056 /* Include data types at the top as other includes use them */ 00057 #include "structs.h" 00058 #include "FixedConfigs.h" 00059 #include "TunableConfigs.h" 00060 00061 /* Global constant declarations */ 00062 #include "globalConstants.h" 00063 00064 00065 /* Where extern is used instead of EXTERN it indicates that */ 00066 /* the variable is initialised in staticInit.c, if someone */ 00067 /* attempts to use extern and doesn't initialise the variable */ 00068 /* statically then the linker should error on undefined symbol */ 00069 00070 00071 #ifdef EXTERN 00072 #warning "EXTERN already defined by another header, please sort it out!" 00073 #undef EXTERN /* If fail on warning is off, remove the definition such that we can redefine correctly. */ 00074 #endif 00075 00076 00077 #ifdef FREEEMS_C 00078 #define EXTERN 00079 #else 00080 #define EXTERN extern 00081 #endif 00082 00083 00084 /* Types summary 00085 * 00086 * BEWARE : Be explicit!! 00087 * 00088 * char 8 bit (defaults to unsigned, but always specify signed/unsigned anyway) 00089 * short 16 bit (defaults to signed, but always specify signed/unsigned anyway) 00090 * int 16 bit DO NOT USE! (current compile flags make this 16 bits, but a change of flags could will change your program if you use this because they will all be 32 bit all of a sudden) 00091 * long 32 bit (defaults to signed, but always specify signed/unsigned anyway) 00092 * long long 64 bit (inefficient, avoid these, if using : defaults to signed, but always specify signed/unsigned anyway) 00093 * float 32 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00094 * double 64 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00095 */ 00096 00097 00098 /* GLOBAL Variables */ 00099 // TODO change any of these that need it to volatile!!! 00100 00101 00102 // temporary test vars 00103 EXTERN unsigned short RPM0; // to be replaced with logging scheme for teeth. 00104 EXTERN unsigned short RPM1; // to be replaced with logging scheme for teeth. 00105 extern unsigned short tachoPeriod; 00106 EXTERN unsigned char portHDebounce; 00107 00108 // these should not be here... TODO move to a comms header 00109 extern unsigned char asyncDatalogType; 00110 #define asyncDatalogOff 0x00 00111 #define asyncDatalogBasic 0x01 00112 #define asyncDatalogConfig 0x02 00113 #define asyncDatalogLogic 0x03 00114 #define asyncDatalogADC 0x04 00115 #define asyncDatalogCircBuf 0x05 00116 #define asyncDatalogCircCAS 0x06 00117 #define asyncDatalogTrigger 0x07 // what is this 00118 EXTERN unsigned short configuredBasicDatalogLength; 00119 00120 00121 // temporary test vars 00122 EXTERN void* memdumpaddr; 00123 EXTERN unsigned char ShouldSendLog; 00124 00125 /* Declare instances of variable structs for use */ 00126 EXTERN Clock Clocks; /* Timer Clocks for various functions */ 00127 EXTERN Counter Counters; /* Execution count for various blocks of code */ 00128 EXTERN RuntimeVar RuntimeVars; /* Execution times for various blocks of code */ 00129 EXTERN ISRLatencyVar ISRLatencyVars; /* Delay in execution start for various blocks of code */ 00130 00131 00165 EXTERN CoreVar* CoreVars; 00166 EXTERN CoreVar CoreVars0; 00167 /* If we move to xgate or isr driven logging, add bank 1 back in */ 00168 00169 EXTERN DerivedVar* DerivedVars; 00170 EXTERN DerivedVar DerivedVars0; 00171 /* If we move to xgate or isr driven logging, add bank 1 back in */ 00172 00173 EXTERN ADCArray* ADCArrays; 00174 EXTERN ADCArray* ADCArraysRecord; 00175 EXTERN ADCArray ADCArrays0; 00176 EXTERN ADCArray ADCArrays1; 00178 EXTERN ADCArray* asyncADCArrays; 00179 EXTERN ADCArray* asyncADCArraysRecord; 00180 EXTERN ADCArray asyncADCArrays0; 00181 EXTERN ADCArray asyncADCArrays1; 00183 EXTERN unsigned short* mathSampleTimeStamp; // TODO temp, remove 00184 EXTERN unsigned short* mathSampleTimeStampRecord; // TODO temp, remove 00185 EXTERN unsigned short* RPM; // TODO temp, remove 00186 EXTERN unsigned short* RPMRecord; // TODO temp, remove 00187 EXTERN unsigned short* currentDwellMath; // TODO temp, remove 00188 EXTERN unsigned short* currentDwellRealtime; // TODO temp, remove 00189 EXTERN unsigned short currentDwell0; // TODO temp, remove 00190 EXTERN unsigned short currentDwell1; // TODO temp, remove 00191 00192 /*break this on purpose so i fix it later 00193 #define VETablereference (*((volatile mainTable*)(0x1000))) 00194 EXTERN const mainTable *VETableRef; 00195 PLUS 00196 const volatile mainTable *VETableRef = (volatile mainTable*)0x1000; 00197 broken too, need to research how to do this. 00198 00199 see line 80 or so from inc/injectorISR.c for array of pointer use. the above may not be possible... TODO */ 00200 00201 00202 /* Potentially pointers for data in ram depending on how it gets implemented */ 00203 // volatile ?? 00204 //EXTERN tunableConfig tunableConfigs; 00205 00206 /* Layout the tunable copies and buffers in ram space */ 00207 00208 00209 00210 /* TODO explanation of paged ram operation Unions for paged large table access using RPAGE */ 00211 typedef union { 00212 mainTable VETableMain; 00213 mainTable IgnitionAdvanceTableMain; 00214 SmallTables1 SmallTablesA; 00215 } Tables1; 00216 00217 typedef union { 00218 mainTable VETableSecondary; 00219 mainTable IgnitionAdvanceTableSecondary; 00220 SmallTables2 SmallTablesB; 00221 } Tables2; 00222 00223 typedef union { 00224 mainTable VETableMainTertiary; 00225 mainTable InjectionAdvanceTableMain; 00226 SmallTables3 SmallTablesC; 00227 } Tables3; 00228 00229 typedef union { 00230 mainTable LambdaTable; 00231 mainTable InjectionAdvanceTableSecondary; 00232 SmallTables4 SmallTablesD; 00233 } Tables4; 00234 00235 00236 /* Large blocks */ 00237 EXTERN unsigned char TXBuffer[TX_BUFFER_SIZE] TXBUF; 00238 EXTERN unsigned char RXBuffer[RX_BUFFER_SIZE] RXBUF; 00239 EXTERN Tables1 TablesA RWINDOW; 00240 EXTERN Tables2 TablesB RWINDOW; 00241 EXTERN Tables3 TablesC RWINDOW; 00242 EXTERN Tables4 TablesD RWINDOW; 00243 00244 00245 /* RAM page variables */ 00246 EXTERN unsigned char currentFuelRPage; 00247 EXTERN unsigned char currentTuneRPage; 00248 EXTERN unsigned char currentTimeRPage; 00249 00250 00251 //union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00252 // unsigned long timeLong; 00253 // unsigned short timeShorts[2]; 00254 //} LongNoTime RWINDOW ; 00255 00256 /* These are inited once and remain the same, rpage switches change meaning. */ 00257 00259 //EXTERN mainTable* VETableMain; 00260 //EXTERN mainTable* VETableSecondary; 00261 //EXTERN mainTable* VETableTertiary; 00262 //EXTERN mainTable* LambdaTable; 00263 // 00264 //EXTERN mainTable* IgnitionAdvanceTableMain; 00265 //EXTERN mainTable* IgnitionAdvanceTableSecondary; 00266 //EXTERN mainTable* InjectionAdvanceTableMain; 00267 //EXTERN mainTable* InjectionAdvanceTableSecondary; 00268 // 00270 //EXTERN twoDTableUS* dwellDesiredVersusVoltageTable; 00271 //EXTERN twoDTableUS* injectorDeadTimeTable; 00272 //EXTERN twoDTableUS* postStartEnrichmentTable; 00273 //EXTERN twoDTableUS* engineTempEnrichmentTableFixed; 00274 //EXTERN twoDTableUS* primingVolumeTable; 00275 //EXTERN twoDTableUS* engineTempEnrichmentTablePercent; 00276 //EXTERN twoDTableUS* dwellMaxVersusRPMTable; 00277 // 00279 //EXTERN unsigned short* perCylinderFuelTrims; 00280 00281 00282 /* Pointers to SmallTablesC */ 00283 00284 00285 /* Pointers to SmallTablesD */ 00286 00287 00288 00289 00290 /* Output variables (init not required) TODO ditch this in favour of the real vars in the calcs function and struct */ 00291 extern unsigned short masterPulseWidth; 00292 EXTERN unsigned short totalDwell; 00293 extern unsigned short totalAngleAfterReferenceInjection; 00294 extern unsigned short totalAngleAfterReferenceIgnition; 00295 00296 EXTERN unsigned long bootFuelConst; /* constant derived from configurable constants */ 00297 EXTERN unsigned short TPSMAPRange; /* The MAP range used to convert fake TPS from MAP and vice versa */ 00298 EXTERN unsigned short TPSADCRange; /* The ADC range used to generate TPS percentage */ 00299 EXTERN unsigned short boundedTPSADC; // temp to view to debug 00300 00301 EXTERN unsigned short bootTimeAAP; /* TODO populate this at switch on time depending on a few things. */ 00302 00303 /* ALL STATUS STUFF HERE */ 00304 00305 // TODO these flags are used for coreSettingsA and it is not clear that they are dual purpose, fix this... 00306 /* State variables : 0 = false (don't forget to change the init mask to suit!) */ 00307 EXTERN unsigned short coreStatusA; /* Each bit represents the state of some core parameter, masks below */ 00308 /* Bit masks for coreStatusA */ // TODO needs a rename as does coresetingsA 00309 #define COREA01 BIT1_16 /* 1 this was RPM_VALID Whether we are sure rpm is what the variable says (used to inject fuel without ignition below the threshold rpm) */ 00310 #define PRIMARY_SYNC BIT2_16 /* 2 Wasted spark/Semi sequential */ 00311 #define SECONDARY_SYNC BIT3_16 /* 3 COP/Full sequential */ 00312 #define ENGINE_PHASE BIT4_16 /* 4 For COP/Sequential, which revolution we are in, first or second */ 00313 #define FUEL_CUT BIT5_16 /* 5 Remove injection completely */ 00314 #define HARD_SPARK_CUT BIT6_16 /* 6 Remove ignition completely */ 00315 #define SOFT_SPARK_CUT BIT7_16 /* 7 Remove ignition events round robin style */ 00316 #define SPARK_RETARD BIT8_16 /* 8 Retard ignition in RPM dependent way */ 00317 #define STAGED_REQUIRED BIT9_16 /* 9 Fire the staged injectors */ 00318 #define CALC_FUEL_IGN BIT10_16 /* 10 Fuel and ignition require calculation (i.e. variables have been updated) */ 00319 #define FORCE_READING BIT11_16 /* 11 Flag to force ADC sampling at low rpm/stall */ 00320 #define COREA12 BIT12_16 /* 12 */ 00321 #define COREA13 BIT13_16 /* 13 */ 00322 #define COREA14 BIT14_16 /* 14 */ 00323 #define COREA15 BIT15_16 /* 15 */ 00324 #define COREA16 BIT16_16 /* 16 */ 00325 00326 #define CLEAR_PRIMARY_SYNC NBIT2_16 /* */ 00327 #define STAGED_NOT_REQUIRED NBIT9_16 /* 9 Do not fire the staged injectors */ 00328 #define CLEAR_CALC_FUEL_IGN NBIT10_16 /* 10 Fuel and ignition don't require calculation */ 00329 #define CLEAR_FORCE_READING NBIT11_16 /* 11 Clear flag to force ADC sampling at low rpm/stall */ 00330 00331 00332 //TODO make this volatile? 00333 /* ECT IC extension variable (init not required, don't care where it is, only differences between figures) */ 00334 unsigned short timerExtensionClock; /* Increment for each overflow of the main timer, allows finer resolution and longer time period */ 00335 /* section 10.3.5 page 290 68hc11 reference manual e.g. groups.csail.mit.edu/drl/courses/cs54-2001s/pdf/M68HC11RM.pdf */ 00336 00337 00338 /* For extracting 32 bit long time stamps from the overflow counter and timer registers */ 00339 typedef union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00340 unsigned long timeLong; 00341 unsigned short timeShorts[2]; 00342 } LongTime; 00343 00344 00345 /* Flag registers, init to zero required */ 00346 EXTERN unsigned char mainOn; /* Keep track of where we are at for possible use as multi interrupt per injection */ 00347 EXTERN unsigned short dwellOn; /* Keep track of ignition output state */ 00348 EXTERN unsigned char stagedOn; /* Ensure we turn an injector off again if we turn it on. */ 00349 EXTERN unsigned char selfSetTimer; /* Set the start time of injection at the end of the last one in the channels ISR instead of the input ISR */ 00350 EXTERN unsigned char rescheduleFuelFlags; /* Pulse width is probably longer than engine cycle so schedule a restart at the next start time */ 00351 00352 00353 /* Engine Position and RPM reading variables */ 00354 00355 /* Engine runtime properties (inits???) TODO */ 00356 EXTERN unsigned short primaryPulsesPerSecondaryPulse; /* Type short because of nissan style cam wheels (char would do for other types) */ 00357 EXTERN unsigned short primaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00358 EXTERN unsigned short primaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00359 EXTERN unsigned short primaryTrailingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00360 EXTERN unsigned short timeBetweenSuccessivePrimaryPulses; /* This number equates to the speed of the engine */ 00361 EXTERN unsigned short timeBetweenSuccessivePrimaryPulsesBuffer; /* This number equates to the speed of the engine */ 00362 EXTERN unsigned short lastPrimaryPulseTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00363 extern unsigned long engineCyclePeriod; /* Timer units between engine cycle starts */ 00364 EXTERN unsigned long lastSecondaryOddTimeStamp; 00365 EXTERN unsigned short primaryTeethDroppedFromLackOfSync; 00366 00367 /* Ignition stuff */ 00368 00369 // ignition experimentation stuff 00370 EXTERN unsigned char dwellQueueLength; /* 0 = no dwell pending start, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00371 EXTERN unsigned char ignitionQueueLength; /* 0 = no spark event pending, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00372 EXTERN unsigned char nextDwellChannel; /* Which one to bang off next */ 00373 EXTERN unsigned char nextIgnitionChannel; /* Which one to bang off next */ 00374 EXTERN unsigned short ignitionAdvances[IGNITION_CHANNELS * 2]; // Uses channel + offset to have two values at any time 00375 EXTERN unsigned short queuedDwellOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00376 EXTERN unsigned short queuedIgnitionOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00377 00378 00379 /* Injection stuff */ 00380 00381 /* Register addresses */ 00382 EXTERN volatile unsigned short * volatile injectorMainTimeRegisters[INJECTION_CHANNELS]; 00383 EXTERN volatile unsigned char * volatile injectorMainControlRegisters[INJECTION_CHANNELS]; 00384 00385 /* Timer holding vars (init not required) */ 00386 EXTERN unsigned short injectorMainStartTimesHolding[INJECTION_CHANNELS]; 00387 EXTERN unsigned long injectorMainEndTimes[INJECTION_CHANNELS]; 00388 00389 // TODO make these names consistent 00390 /* Code time to run variables (init not required) */ 00391 EXTERN unsigned short injectorCodeOpenRuntimes[INJECTION_CHANNELS]; 00392 EXTERN unsigned short injectorCodeCloseRuntimes[INJECTION_CHANNELS]; 00393 00394 /* individual channel pulsewidths (init not required) */ 00395 EXTERN unsigned short* injectorMainPulseWidthsMath; 00396 EXTERN unsigned short* injectorStagedPulseWidthsMath; 00397 EXTERN unsigned short* injectorMainPulseWidthsRealtime; 00398 EXTERN unsigned short* injectorStagedPulseWidthsRealtime; 00399 EXTERN unsigned short injectorMainPulseWidths0[INJECTION_CHANNELS]; 00400 EXTERN unsigned short injectorMainPulseWidths1[INJECTION_CHANNELS]; 00401 EXTERN unsigned short injectorStagedPulseWidths0[INJECTION_CHANNELS]; 00402 EXTERN unsigned short injectorStagedPulseWidths1[INJECTION_CHANNELS]; 00403 00404 /* Channel latencies (init not required) */ 00405 EXTERN unsigned short injectorCodeLatencies[INJECTION_CHANNELS]; 00406 00407 00408 #undef EXTERN 00409 00410 00411 #else 00412 /* let us know if we are being untidy with headers */ 00413 #warning "Header file FREEEMS_H seen before, sort it out!" 00414 /* end of the wrapper ifdef from the very top */ 00415 #endif