00001 /* freeEMS.h 00002 00003 Copyright 2008 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 send them upstream to us at admin@diyefi.org 00021 00022 Thank you for choosing FreeEMS to run your engine! */ 00023 00024 /* Header file multiple inclusion protection courtesy eclipse Header Template */ 00025 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */ 00026 #ifndef FILE_FREEEMS_H_SEEN 00027 #define FILE_FREEEMS_H_SEEN 00028 00029 00030 /* Include top level files that everything else relies on */ 00031 #include "memory.h" 00032 #include "9S12XDP512.h" 00033 00034 00035 /* Where extern is used instead of EXTERN it indicates that */ 00036 /* the variable is initialised in staticInit.c, if someone */ 00037 /* attempts to use extern and doesn't initialise the variable */ 00038 /* statically then the linker should error on undefined symbol */ 00039 #ifdef MAIN_OR_GLOBALS 00040 #define EXTERN 00041 #else 00042 #define EXTERN extern 00043 #endif 00044 00045 00046 /* Include define files at the top here as other includes use them */ 00047 #include "errorDefines.h" 00048 #include "globalDefines.h" 00049 00050 00051 /* Include items that would normally be found in here */ 00052 #include "structs.h" 00053 #include "tunables.h" 00054 #include "globalConstants.h" 00055 #include "flashGlobals.h" // For Sean to work on this in parallel with me 00056 00057 00058 /* Types summary 00059 * 00060 * BEWARE : Be explicit!! 00061 * 00062 * char 8 bit (defaults to unsigned, but always specify signed/unsigned anyway) 00063 * short 16 bit (defaults to signed, but always specify signed/unsigned anyway) 00064 * 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) 00065 * long 32 bit (defaults to signed, but always specify signed/unsigned anyway) 00066 * long long 64 bit (inefficient, avoid these, if using : defaults to signed, but always specify signed/unsigned anyway) 00067 * float 32 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00068 * double 64 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00069 */ 00070 00071 00072 /* GLOBAL Variables */ 00073 // TODO change any of these that need it to volatile!!! 00074 00075 00076 // temporary test vars 00077 EXTERN unsigned short RPM0; // to be replaced with logging scheme for teeth. 00078 EXTERN unsigned short RPM1; // to be replaced with logging scheme for teeth. 00079 extern unsigned short tachoPeriod; 00080 EXTERN unsigned char portHDebounce; 00081 00082 /* these should not be here... TODO */ 00083 extern unsigned char asyncDatalogType; 00084 #define asyncDatalogOff 0x00 00085 #define asyncDatalogBasic 0x01 00086 #define asyncDatalogConfig 0x02 00087 #define asyncDatalogLogic 0x03 00088 #define asyncDatalogADC 0x04 00089 #define asyncDatalogCircBuf 0x05 00090 #define asyncDatalogCircCAS 0x06 00091 #define asyncDatalogTrigger 0x07 // what is this 00092 EXTERN unsigned short configuredBasicDatalogLength; 00093 00094 00095 // temporary test vars 00096 EXTERN void* memdumpaddr; 00097 EXTERN unsigned char ShouldSendLog; 00098 00099 /* Declare instances of variable structs for use */ 00100 EXTERN Clock Clocks; /* Timer Clocks for various functions */ 00101 EXTERN Counter Counters; /* Execution count for various blocks of code */ 00102 EXTERN RuntimeVar RuntimeVars; /* Execution times for various blocks of code */ 00103 EXTERN ISRLatencyVar ISRLatencyVars; /* Delay in execution start for various blocks of code */ 00104 00105 00106 /* The banked running variable system and structure 00107 * 00108 * The program running variables are divided into three broad groups: inputs, working 00109 * and outputs. For both the input and output groups there are two copies of each set 00110 * of variables, whereas there is only one copy of each set in the working group. This 00111 * is required to allow both the inputs and outputs to be safely written and read at 00112 * the same time. To facilitate this all sets of variables within the input and output 00113 * groups are referenced with two pointers each. For the input group, the copy pointed 00114 * to is swapped when fresh data is both available to and required by the mathematics 00115 * function. For the output group the copy pointed to is swapped when the mathematics 00116 * function has produced new output data. The input data is supplied by the engine 00117 * position interrupt service routines as ADC readings and RPM values. The output data 00118 * consists of pulse widths, timing angles, dwell periods and scheduling information. 00119 * 00120 * Accessory functions (Idle, Boost, etc) 00121 * 00122 * In order to achieve minimal latency and maximum frequency of execution of the 00123 * main mathematics code the accessory functions must run asynchronously. Although 00124 * we can guarantee that these functions will base their calculations on a matched 00125 * set of data, we can not guarantee that it will be the same set of data presented 00126 * along side the accessory data in the data log. Thus, where it is required to see 00127 * the inputs that an accessory functions calculations were based on, those values 00128 * should be cached for logging on a per function basis. 00129 * 00130 * Although it seems like a lot of trouble to go to, it is critical to transient 00131 * performance that the environmental conditions the engine is operating under are 00132 * tracked and reacted to as quickly as possible. Having the less important stuff 00133 * run asynchronously will result in an order of magnitude improvement of parameter 00134 * tracking and is well worth the extra memory expense and complication. 00135 */ 00136 00137 EXTERN CoreVar* CoreVars; /* Pointer to the core running variables */ 00138 EXTERN CoreVar CoreVars0; /* Bank 0 core running variables */ 00139 /* If we move to xgate or isr driven logging, add bank 1 back in */ 00140 00141 EXTERN DerivedVar* DerivedVars; /* Pointer to the secondary running variables */ 00142 EXTERN DerivedVar DerivedVars0; /* Bank 0 secondary running variables */ 00143 /* If we move to xgate or isr driven logging, add bank 1 back in */ 00144 00145 EXTERN ADCArray* ADCArrays; /* main adc storage area for syncronous sampling in the engine position ISR or injection ISR or ignition ISR etc. */ 00146 EXTERN ADCArray* ADCArraysRecord; /* main adc storage area for syncronous sampling in the engine position ISR or injection ISR or ignition ISR etc. */ 00147 EXTERN ADCArray ADCArrays0; /* main adc storage area for syncronous sampling in the engine position ISR or injection ISR or ignition ISR etc. */ 00148 EXTERN ADCArray ADCArrays1; /* main adc storage area for syncronous sampling in the engine position ISR or injection ISR or ignition ISR etc. */ 00149 00150 EXTERN ADCArray* asyncADCArrays; /* secondary adc storage area for asynchronously sampling in the RTC/RTI ISR */ 00151 EXTERN ADCArray* asyncADCArraysRecord; /* secondary adc storage area for asynchronously sampling in the RTC/RTI ISR */ 00152 EXTERN ADCArray asyncADCArrays0; /* secondary adc storage area for asynchronously sampling in the RTC/RTI ISR */ 00153 EXTERN ADCArray asyncADCArrays1; /* secondary adc storage area for asynchronously sampling in the RTC/RTI ISR */ 00154 00155 EXTERN unsigned short* mathSampleTimeStamp; // TODO temp, remove 00156 EXTERN unsigned short* mathSampleTimeStampRecord; // TODO temp, remove 00157 EXTERN unsigned short* RPM; // TODO temp, remove 00158 EXTERN unsigned short* RPMRecord; // TODO temp, remove 00159 EXTERN unsigned short* currentDwellMath; // TODO temp, remove 00160 EXTERN unsigned short* currentDwellRealtime; // TODO temp, remove 00161 EXTERN unsigned short currentDwell0; // TODO temp, remove 00162 EXTERN unsigned short currentDwell1; // TODO temp, remove 00163 00164 /*break this on purpose so i fix it later 00165 #define VETablereference (*((volatile mainTable*)(0x1000))) 00166 EXTERN const mainTable *VETableRef; 00167 PLUS 00168 const volatile mainTable *VETableRef = (volatile mainTable*)0x1000; 00169 broken too, need to research how to do this. 00170 00171 see line 80 or so from inc/injectorISR.c for array of pointer use. the above may not be possible... TODO */ 00172 00173 00174 /* Potentially pointers for data in ram depending on how it gets implemented */ 00175 // volatile ?? 00176 //EXTERN tunableConfig tunableConfigs; 00177 00178 /* Layout the tunable copies and buffers in ram space */ 00179 00180 00181 00182 /* Unions for paged large table access using RPAGE */ 00183 typedef union { 00184 mainTable VETableMain; 00185 mainTable IgnitionAdvanceTableMain; 00186 SmallTables1 SmallTablesA; 00187 } Tables1; 00188 00189 typedef union { 00190 mainTable VETableSecondary; 00191 mainTable IgnitionAdvanceTableSecondary; 00192 SmallTables2 SmallTablesB; 00193 } Tables2; 00194 00195 typedef union { 00196 mainTable VETableMainTertiary; 00197 mainTable InjectionAdvanceTableMain; 00198 SmallTables3 SmallTablesC; 00199 } Tables3; 00200 00201 typedef union { 00202 mainTable LambdaTable; 00203 mainTable InjectionAdvanceTableSecondary; 00204 SmallTables4 SmallTablesD; 00205 } Tables4; 00206 00207 00208 /* Large blocks */ 00209 EXTERN unsigned char TXBuffer[TX_BUFFER_SIZE] TXBUF; 00210 EXTERN unsigned char RXBuffer[RX_BUFFER_SIZE] RXBUF; 00211 EXTERN Tables1 TablesA RWINDOW; 00212 EXTERN Tables2 TablesB RWINDOW; 00213 EXTERN Tables3 TablesC RWINDOW; 00214 EXTERN Tables4 TablesD RWINDOW; 00215 00216 00217 /* RAM page variables */ 00218 EXTERN unsigned char currentFuelRPage; 00219 EXTERN unsigned char currentTuneRPage; 00220 EXTERN unsigned char currentTimeRPage; 00221 00222 /* Fueling blocks */ 00223 EXTERN void* VETableMainFlashLocation; 00224 EXTERN void* VETableMainFlash2Location; 00225 EXTERN void* VETableSecondaryFlashLocation; 00226 EXTERN void* VETableSecondaryFlash2Location; 00227 EXTERN void* VETableTertiaryFlashLocation; 00228 EXTERN void* VETableTertiaryFlash2Location; 00229 EXTERN void* LambdaTableFlashLocation; 00230 EXTERN void* LambdaTableFlash2Location; 00231 /* Timing blocks */ 00232 EXTERN void* IgnitionAdvanceTableMainFlashLocation; 00233 EXTERN void* IgnitionAdvanceTableMainFlash2Location; 00234 EXTERN void* IgnitionAdvanceTableSecondaryFlashLocation; 00235 EXTERN void* IgnitionAdvanceTableSecondaryFlash2Location; 00236 EXTERN void* InjectionAdvanceTableMainFlashLocation; 00237 EXTERN void* InjectionAdvanceTableMainFlash2Location; 00238 EXTERN void* InjectionAdvanceTableSecondaryFlashLocation; 00239 EXTERN void* InjectionAdvanceTableSecondaryFlash2Location; 00240 /* Tuable blocks */ 00241 EXTERN void* SmallTablesAFlashLocation; 00242 EXTERN void* SmallTablesAFlash2Location; 00243 EXTERN void* SmallTablesBFlashLocation; 00244 EXTERN void* SmallTablesBFlash2Location; 00245 EXTERN void* SmallTablesCFlashLocation; 00246 EXTERN void* SmallTablesCFlash2Location; 00247 EXTERN void* SmallTablesDFlashLocation; 00248 EXTERN void* SmallTablesDFlash2Location; 00249 /* Flash ONLY blocks */ 00250 EXTERN void* IATTransferTableLocation; 00251 EXTERN void* CHTTransferTableLocation; 00252 EXTERN void* MAFTransferTableLocation; 00253 EXTERN void* TestTransferTableLocation; 00254 /* Small tables */ 00255 //EXTERN void* 00256 00257 00258 //union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00259 // unsigned long timeLong; 00260 // unsigned short timeShorts[2]; 00261 //} LongNoTime RWINDOW ; 00262 00263 /* These are inited once and remain the same, rpage switches change meaning. */ 00264 00266 //EXTERN mainTable* VETableMain; 00267 //EXTERN mainTable* VETableSecondary; 00268 //EXTERN mainTable* VETableTertiary; 00269 //EXTERN mainTable* LambdaTable; 00270 // 00271 //EXTERN mainTable* IgnitionAdvanceTableMain; 00272 //EXTERN mainTable* IgnitionAdvanceTableSecondary; 00273 //EXTERN mainTable* InjectionAdvanceTableMain; 00274 //EXTERN mainTable* InjectionAdvanceTableSecondary; 00275 // 00277 //EXTERN twoDTableUS* dwellDesiredVersusVoltageTable; 00278 //EXTERN twoDTableUS* injectorDeadTimeTable; 00279 //EXTERN twoDTableUS* postStartEnrichmentTable; 00280 //EXTERN twoDTableUS* engineTempEnrichmentTableFixed; 00281 //EXTERN twoDTableUS* primingVolumeTable; 00282 //EXTERN twoDTableUS* engineTempEnrichmentTablePercent; 00283 //EXTERN twoDTableUS* dwellMaxVersusRPMTable; 00284 // 00286 //EXTERN unsigned short* perCylinderFuelTrims; 00287 00288 00289 /* Pointers to SmallTablesC */ 00290 00291 00292 /* Pointers to SmallTablesD */ 00293 00294 00295 00296 00297 /* Output variables (init not required) TODO ditch this in favour of the real vars in the calcs function and struct */ 00298 extern unsigned short masterPulseWidth; 00299 EXTERN unsigned short totalDwell; 00300 extern unsigned short totalAngleAfterReferenceInjection; 00301 extern unsigned short totalAngleAfterReferenceIgnition; 00302 00303 EXTERN unsigned long bootFuelConst; /* constant derived from configurable constants */ 00304 EXTERN unsigned short TPSMAPRange; /* The MAP range used to convert fake TPS from MAP and vice versa */ 00305 EXTERN unsigned short TPSADCRange; /* The ADC range used to generate TPS percentage */ 00306 EXTERN unsigned short boundedTPSADC; // temp to view to debug 00307 00308 EXTERN unsigned short bootTimeAAP; /* TODO populate this at switch on time depending on a few things. */ 00309 00310 /* ALL STATUS STUFF HERE */ 00311 00312 /* State variables : 0 = false (don't forget to change the init mask to suit!) */ 00313 EXTERN unsigned short coreStatusA; /* Each bit represents the state of some core parameter, masks below */ 00314 /* Bit masks for coreStatusA */ // TODO needs a rename as does coresetingsA 00315 #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) */ 00316 #define PRIMARY_SYNC BIT2_16 /* 2 Wasted spark/Semi sequential */ 00317 #define SECONDARY_SYNC BIT3_16 /* 3 COP/Full sequential */ 00318 #define ENGINE_PHASE BIT4_16 /* 4 For COP/Sequential, which revolution we are in, first or second */ 00319 #define FUEL_CUT BIT5_16 /* 5 Remove injection completely */ 00320 #define HARD_SPARK_CUT BIT6_16 /* 6 Remove ignition completely */ 00321 #define SOFT_SPARK_CUT BIT7_16 /* 7 Remove ignition events round robin style */ 00322 #define SPARK_RETARD BIT8_16 /* 8 Retard ignition in RPM dependent way */ 00323 #define STAGED_REQUIRED BIT9_16 /* 9 Fire the staged injectors */ 00324 #define CALC_FUEL_IGN BIT10_16 /* 10 Fuel and ignition require calculation (i.e. variables have been updated) */ 00325 #define FORCE_READING BIT11_16 /* 11 Flag to force ADC sampling at low rpm/stall */ 00326 #define COREA12 BIT12_16 /* 12 */ 00327 #define COREA13 BIT13_16 /* 13 */ 00328 #define COREA14 BIT14_16 /* 14 */ 00329 #define COREA15 BIT15_16 /* 15 */ 00330 #define COREA16 BIT16_16 /* 16 */ 00331 00332 #define CLEAR_PRIMARY_SYNC NBIT2_16 00333 #define STAGED_NOT_REQUIRED NBIT9_16 /* 9 Do not fire the staged injectors */ 00334 #define CLEAR_CALC_FUEL_IGN NBIT10_16 /* 10 Fuel and ignition don't require calculation */ 00335 #define CLEAR_FORCE_READING NBIT11_16 /* 11 Clear flag to force ADC sampling at low rpm/stall */ 00336 00337 00338 //TODO make this volatile? 00339 /* ECT IC extension variable (init not required, don't care where it is, only differences between figures) */ 00340 unsigned short timerExtensionClock; /* Increment for each overflow of the main timer, allows finer resolution and longer time period */ 00341 /* section 10.3.5 page 290 68hc11 reference manual e.g. groups.csail.mit.edu/drl/courses/cs54-2001s/pdf/M68HC11RM.pdf */ 00342 00343 00344 /* For extracting 32 bit long time stamps from the overflow counter and timer registers */ 00345 typedef union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00346 unsigned long timeLong; 00347 unsigned short timeShorts[2]; 00348 } LongTime; 00349 00350 00351 /* Flag registers, init to zero required */ 00352 EXTERN unsigned char mainOn; /* Keep track of where we are at for possible use as multi interrupt per injection */ 00353 EXTERN unsigned short dwellOn; /* Keep track of ignition output state */ 00354 EXTERN unsigned char stagedOn; /* Ensure we turn an injector off again if we turn it on. */ 00355 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 */ 00356 EXTERN unsigned char rescheduleFuelFlags; /* Pulse width is probably longer than engine cycle so schedule a restart at the next start time */ 00357 00358 00359 /* Engine Position and RPM reading variables */ 00360 00361 /* Engine runtime properties (inits???) TODO */ 00362 EXTERN unsigned short primaryPulsesPerSecondaryPulse; /* Type short because of nissan style cam wheels (char would do for other types) */ 00363 EXTERN unsigned short primaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00364 EXTERN unsigned short primaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00365 EXTERN unsigned short primaryTrailingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00366 EXTERN unsigned short timeBetweenSuccessivePrimaryPulses; /* This number equates to the speed of the engine */ 00367 EXTERN unsigned short timeBetweenSuccessivePrimaryPulsesBuffer; /* This number equates to the speed of the engine */ 00368 EXTERN unsigned short lastPrimaryPulseTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00369 extern unsigned long engineCyclePeriod; /* Timer units between engine cycle starts */ 00370 EXTERN unsigned long lastSecondaryOddTimeStamp; 00371 EXTERN unsigned short primaryTeethDroppedFromLackOfSync; 00372 00373 /* Ignition stuff */ 00374 00375 // ignition experimentation stuff 00376 EXTERN unsigned char dwellQueueLength; /* 0 = no dwell pending start, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00377 EXTERN unsigned char ignitionQueueLength; /* 0 = no spark event pending, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00378 EXTERN unsigned char nextDwellChannel; /* Which one to bang off next */ 00379 EXTERN unsigned char nextIgnitionChannel; /* Which one to bang off next */ 00380 EXTERN unsigned short ignitionAdvances[IGNITION_CHANNELS * 2]; // Uses channel + offset to have two values at any time 00381 EXTERN unsigned short queuedDwellOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00382 EXTERN unsigned short queuedIgnitionOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00383 00384 00385 /* Injection stuff */ 00386 00387 /* Register addresses */ 00388 EXTERN volatile unsigned short * volatile injectorMainTimeRegisters[INJECTION_CHANNELS]; 00389 EXTERN volatile unsigned char * volatile injectorMainControlRegisters[INJECTION_CHANNELS]; 00390 00391 /* Timer holding vars (init not required) */ 00392 EXTERN unsigned short injectorMainStartTimesHolding[INJECTION_CHANNELS]; 00393 EXTERN unsigned long injectorMainEndTimes[INJECTION_CHANNELS]; 00394 00395 // TODO make these names consistent 00396 /* Code time to run variables (init not required) */ 00397 EXTERN unsigned short injectorCodeOpenRuntimes[INJECTION_CHANNELS]; 00398 EXTERN unsigned short injectorCodeCloseRuntimes[INJECTION_CHANNELS]; 00399 00400 /* individual channel pulsewidths (init not required) */ 00401 EXTERN unsigned short* injectorMainPulseWidthsMath; 00402 EXTERN unsigned short* injectorStagedPulseWidthsMath; 00403 EXTERN unsigned short* injectorMainPulseWidthsRealtime; 00404 EXTERN unsigned short* injectorStagedPulseWidthsRealtime; 00405 EXTERN unsigned short injectorMainPulseWidths0[INJECTION_CHANNELS]; 00406 EXTERN unsigned short injectorMainPulseWidths1[INJECTION_CHANNELS]; 00407 EXTERN unsigned short injectorStagedPulseWidths0[INJECTION_CHANNELS]; 00408 EXTERN unsigned short injectorStagedPulseWidths1[INJECTION_CHANNELS]; 00409 00410 /* Channel latencies (init not required) */ 00411 EXTERN unsigned short injectorCodeLatencies[INJECTION_CHANNELS]; 00412 00413 #undef IN_OUT_BANKS 00414 #undef EXTERN 00415 00416 #else 00417 /* let us know if we are being untidy with headers */ 00418 #warning "Header file FREEEMS_H seen before, sort it out!" 00419 /* end of the wrapper ifdef from the very top */ 00420 #endif