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* RPM; // TODO temp, remove 00189 EXTERN unsigned short* RPMRecord; // TODO temp, remove 00190 EXTERN unsigned short* currentDwellMath; // TODO temp, remove 00191 EXTERN unsigned short* currentDwellRealtime; // TODO temp, remove 00192 EXTERN unsigned short currentDwell0; // TODO temp, remove 00193 EXTERN unsigned short currentDwell1; // TODO temp, remove 00194 00195 /*break this on purpose so i fix it later 00196 #define VETablereference (*((volatile mainTable*)(0x1000))) 00197 EXTERN const mainTable *VETableRef; 00198 PLUS 00199 const volatile mainTable *VETableRef = (volatile mainTable*)0x1000; 00200 broken too, need to research how to do this. 00201 00202 see line 80 or so from inc/injectorISR.c for array of pointer use. the above may not be possible... TODO */ 00203 00204 00205 /* Potentially pointers for data in ram depending on how it gets implemented */ 00206 // volatile ?? 00207 //EXTERN tunableConfig tunableConfigs; 00208 00209 /* Layout the tunable copies and buffers in ram space */ 00210 00211 00212 00213 /* TODO explanation of paged ram operation Unions for paged large table access using RPAGE */ 00214 typedef union { 00215 mainTable VETableMain; 00216 mainTable IgnitionAdvanceTableMain; 00217 SmallTables1 SmallTablesA; 00218 } Tables1; 00219 00220 typedef union { 00221 mainTable VETableSecondary; 00222 mainTable IgnitionAdvanceTableSecondary; 00223 SmallTables2 SmallTablesB; 00224 } Tables2; 00225 00226 typedef union { 00227 mainTable VETableMainTertiary; 00228 mainTable InjectionAdvanceTableMain; 00229 SmallTables3 SmallTablesC; 00230 } Tables3; 00231 00232 typedef union { 00233 mainTable LambdaTable; 00234 mainTable InjectionAdvanceTableSecondary; 00235 SmallTables4 SmallTablesD; 00236 } Tables4; 00237 00238 00239 /* Large blocks */ 00240 EXTERN unsigned char TXBuffer[TX_BUFFER_SIZE] TXBUF; 00241 EXTERN unsigned char RXBuffer[RX_BUFFER_SIZE] RXBUF; 00242 EXTERN Tables1 TablesA RWINDOW; 00243 EXTERN Tables2 TablesB RWINDOW; 00244 EXTERN Tables3 TablesC RWINDOW; 00245 EXTERN Tables4 TablesD RWINDOW; 00246 00247 00248 /* RAM page variables */ 00249 EXTERN unsigned char currentFuelRPage; 00250 EXTERN unsigned char currentTuneRPage; 00251 EXTERN unsigned char currentTimeRPage; 00252 00253 00254 //union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00255 // unsigned long timeLong; 00256 // unsigned short timeShorts[2]; 00257 //} LongNoTime RWINDOW ; 00258 00259 /* These are inited once and remain the same, rpage switches change meaning. */ 00260 00262 //EXTERN mainTable* VETableMain; 00263 //EXTERN mainTable* VETableSecondary; 00264 //EXTERN mainTable* VETableTertiary; 00265 //EXTERN mainTable* LambdaTable; 00266 // 00267 //EXTERN mainTable* IgnitionAdvanceTableMain; 00268 //EXTERN mainTable* IgnitionAdvanceTableSecondary; 00269 //EXTERN mainTable* InjectionAdvanceTableMain; 00270 //EXTERN mainTable* InjectionAdvanceTableSecondary; 00271 // 00273 //EXTERN twoDTableUS* dwellDesiredVersusVoltageTable; 00274 //EXTERN twoDTableUS* injectorDeadTimeTable; 00275 //EXTERN twoDTableUS* postStartEnrichmentTable; 00276 //EXTERN twoDTableUS* engineTempEnrichmentTableFixed; 00277 //EXTERN twoDTableUS* primingVolumeTable; 00278 //EXTERN twoDTableUS* engineTempEnrichmentTablePercent; 00279 //EXTERN twoDTableUS* dwellMaxVersusRPMTable; 00280 // 00282 //EXTERN unsigned short* perCylinderFuelTrims; 00283 00284 00285 /* Pointers to SmallTablesC */ 00286 00287 00288 /* Pointers to SmallTablesD */ 00289 00290 00291 00292 00293 /* Output variables (init not required) TODO ditch this in favour of the real vars in the calcs function and struct */ 00294 extern unsigned short masterPulseWidth; 00295 EXTERN unsigned short totalDwell; 00296 extern unsigned short totalAngleAfterReferenceInjection; 00297 extern unsigned short totalAngleAfterReferenceIgnition; 00298 00299 EXTERN unsigned long bootFuelConst; /* constant derived from configurable constants */ 00300 EXTERN unsigned short TPSMAPRange; /* The MAP range used to convert fake TPS from MAP and vice versa */ 00301 EXTERN unsigned short TPSADCRange; /* The ADC range used to generate TPS percentage */ 00302 EXTERN unsigned short boundedTPSADC; // temp to view to debug 00303 00304 EXTERN unsigned short bootTimeAAP; /* TODO populate this at switch on time depending on a few things. */ 00305 00306 /* ALL STATUS STUFF HERE */ 00307 00308 // TODO these flags are used for coreSettingsA and it is not clear that they are dual purpose, fix this... 00309 /* State variables : 0 = false (don't forget to change the init mask to suit!) */ 00310 EXTERN unsigned short coreStatusA; /* Each bit represents the state of some core parameter, masks below */ 00311 /* Bit masks for coreStatusA */ // TODO needs a rename as does coresetingsA 00312 #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) */ 00313 #define PRIMARY_SYNC BIT2_16 /* 2 Wasted spark/Semi sequential */ 00314 #define SECONDARY_SYNC BIT3_16 /* 3 COP/Full sequential */ 00315 #define ENGINE_PHASE BIT4_16 /* 4 For COP/Sequential, which revolution we are in, first or second */ 00316 #define FUEL_CUT BIT5_16 /* 5 Remove injection completely */ 00317 #define HARD_SPARK_CUT BIT6_16 /* 6 Remove ignition completely */ 00318 #define SOFT_SPARK_CUT BIT7_16 /* 7 Remove ignition events round robin style */ 00319 #define SPARK_RETARD BIT8_16 /* 8 Retard ignition in RPM dependent way */ 00320 #define STAGED_REQUIRED BIT9_16 /* 9 Fire the staged injectors */ 00321 #define CALC_FUEL_IGN BIT10_16 /* 10 Fuel and ignition require calculation (i.e. variables have been updated) */ 00322 #define FORCE_READING BIT11_16 /* 11 Flag to force ADC sampling at low rpm/stall */ 00323 #define COREA12 BIT12_16 /* 12 */ 00324 #define COREA13 BIT13_16 /* 13 */ 00325 #define COREA14 BIT14_16 /* 14 */ 00326 #define COREA15 BIT15_16 /* 15 */ 00327 #define COREA16 BIT16_16 /* 16 */ 00328 00329 #define CLEAR_PRIMARY_SYNC NBIT2_16 /* */ 00330 #define STAGED_NOT_REQUIRED NBIT9_16 /* 9 Do not fire the staged injectors */ 00331 #define CLEAR_CALC_FUEL_IGN NBIT10_16 /* 10 Fuel and ignition don't require calculation */ 00332 #define CLEAR_FORCE_READING NBIT11_16 /* 11 Clear flag to force ADC sampling at low rpm/stall */ 00333 00334 00335 //TODO make this volatile? 00336 /* ECT IC extension variable (init not required, don't care where it is, only differences between figures) */ 00337 unsigned short timerExtensionClock; /* Increment for each overflow of the main timer, allows finer resolution and longer time period */ 00338 /* section 10.3.5 page 290 68hc11 reference manual e.g. groups.csail.mit.edu/drl/courses/cs54-2001s/pdf/M68HC11RM.pdf */ 00339 00340 00341 /* For extracting 32 bit long time stamps from the overflow counter and timer registers */ 00342 typedef union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00343 unsigned long timeLong; 00344 unsigned short timeShorts[2]; 00345 } LongTime; 00346 00347 00348 /* Flag registers, init to zero required */ 00349 EXTERN unsigned char mainOn; /* Keep track of where we are at for possible use as multi interrupt per injection */ 00350 EXTERN unsigned short dwellOn; /* Keep track of ignition output state */ 00351 EXTERN unsigned char stagedOn; /* Ensure we turn an injector off again if we turn it on. */ 00352 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 */ 00353 EXTERN unsigned char rescheduleFuelFlags; /* Pulse width is probably longer than engine cycle so schedule a restart at the next start time */ 00354 00355 00356 /* Engine Position and RPM reading variables */ 00357 00358 /* Engine runtime properties (inits???) TODO */ 00359 EXTERN unsigned short primaryPulsesPerSecondaryPulse; /* Type short because of nissan style cam wheels (char would do for other types) */ 00360 //EXTERN unsigned short primaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00361 EXTERN unsigned long primaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00362 EXTERN unsigned long primaryTrailingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00363 EXTERN unsigned long timeBetweenSuccessivePrimaryPulses; /* This number equates to the speed of the engine */ 00364 //EXTERN unsigned short timeBetweenSuccessivePrimaryPulsesBuffer; /* This number equates to the speed of the engine */ 00365 EXTERN unsigned long lastPrimaryPulseTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00366 //EXTERN unsigned short primaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00367 //EXTERN LongTime primaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00368 //EXTERN LongTime primaryTrailingEdgeTimeStamp; /* Store the timestamp of the trailing edge during a pulse */ 00369 00370 EXTERN unsigned long timeBetweenSuccessivePrimaryPulses; /* This number equates to the speed of the engine */ 00371 //EXTERN unsigned long timeBetweenSuccessivePrimaryPulsesBuffer; /* This number equates to the speed of the engine */ 00372 //EXTERN unsigned long lengthOfSecondaryHighPulses; /* This number equates to the speed of the engine */ 00373 //EXTERN unsigned long lengthOfSecondaryHighPulsesBuffer; /* This number equates to the speed of the engine */ 00374 EXTERN unsigned long lengthOfSecondaryLowPulses; /* This number equates to the speed of the engine */ 00375 //EXTERN unsigned long lengthOfSecondaryLowPulsesBuffer; /* This number equates to the speed of the engine */ 00376 00377 //EXTERN LongTime lastPrimaryPulseTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00378 EXTERN unsigned long lastSecondaryPulseLeadingTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00379 EXTERN unsigned long lastSecondaryPulseTrailingTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00380 extern unsigned long engineCyclePeriod; /* Timer units between engine cycle starts */ 00381 EXTERN unsigned long lastSecondaryOddTimeStamp; 00382 00383 //EXTERN unsigned short secondaryPulsesPerSecondaryPulse; /* Type short because of nissan style cam wheels (char would do for other types) */ 00384 //EXTERN unsigned short secondaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00385 //EXTERN unsigned short secondaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00386 //EXTERN unsigned short secondaryTrailingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00387 00388 EXTERN unsigned short primaryTeethDroppedFromLackOfSync; 00389 00390 /* Ignition stuff */ 00391 00392 // ignition experimentation stuff 00393 EXTERN unsigned char dwellQueueLength; /* 0 = no dwell pending start, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00394 EXTERN unsigned char ignitionQueueLength; /* 0 = no spark event pending, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00395 EXTERN unsigned char nextDwellChannel; /* Which one to bang off next */ 00396 EXTERN unsigned char nextIgnitionChannel; /* Which one to bang off next */ 00397 EXTERN unsigned short ignitionAdvances[IGNITION_CHANNELS * 2]; // Uses channel + offset to have two values at any time 00398 EXTERN unsigned short queuedDwellOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00399 EXTERN unsigned short queuedIgnitionOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00400 00401 00402 /* Injection stuff */ 00403 00404 /* Register addresses */ 00405 EXTERN volatile unsigned short * volatile injectorMainTimeRegisters[INJECTION_CHANNELS]; 00406 EXTERN volatile unsigned char * volatile injectorMainControlRegisters[INJECTION_CHANNELS]; 00407 00408 /* Timer holding vars (init not required) */ 00409 EXTERN unsigned short injectorMainStartTimesHolding[INJECTION_CHANNELS]; 00410 EXTERN unsigned long injectorMainEndTimes[INJECTION_CHANNELS]; 00411 00412 // TODO make these names consistent 00413 /* Code time to run variables (init not required) */ 00414 EXTERN unsigned short injectorCodeOpenRuntimes[INJECTION_CHANNELS]; 00415 EXTERN unsigned short injectorCodeCloseRuntimes[INJECTION_CHANNELS]; 00416 00417 /* individual channel pulsewidths (init not required) */ 00418 EXTERN unsigned short* injectorMainPulseWidthsMath; 00419 EXTERN unsigned short* injectorStagedPulseWidthsMath; 00420 EXTERN unsigned short* injectorMainPulseWidthsRealtime; 00421 EXTERN unsigned short* injectorStagedPulseWidthsRealtime; 00422 EXTERN unsigned short injectorMainPulseWidths0[INJECTION_CHANNELS]; 00423 EXTERN unsigned short injectorMainPulseWidths1[INJECTION_CHANNELS]; 00424 EXTERN unsigned short injectorStagedPulseWidths0[INJECTION_CHANNELS]; 00425 EXTERN unsigned short injectorStagedPulseWidths1[INJECTION_CHANNELS]; 00426 00427 /* Channel latencies (init not required) */ 00428 EXTERN unsigned short injectorCodeLatencies[INJECTION_CHANNELS]; 00429 00430 00431 #undef EXTERN 00432 00433 00434 #else 00435 /* let us know if we are being untidy with headers */ 00436 #warning "Header file FREEEMS_H seen before, sort it out!" 00437 /* end of the wrapper ifdef from the very top */ 00438 #endif