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