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 #ifdef MAIN_OR_GLOBALS 00036 #define EXTERN 00037 #else 00038 #define EXTERN extern 00039 #endif 00040 00041 00042 /* Include define files at the top here as other includes use them */ 00043 #include "errorDefines.h" 00044 #include "globalDefines.h" 00045 00046 00047 /* Include items that would normally be found in here */ 00048 #include "structs.h" 00049 #include "tunables.h" 00050 #include "globalConstants.h" 00051 #include "flashGlobals.h" // For Sean to work on this in parallel with me 00052 00053 00054 /* Types summary 00055 * 00056 * BEWARE : Be explicit!! 00057 * 00058 * char 8 bit (defaults to unsigned, but always specify signed/unsigned anyway) 00059 * short 16 bit (defaults to signed, but always specify signed/unsigned anyway) 00060 * 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) 00061 * long 32 bit (defaults to signed, but always specify signed/unsigned anyway) 00062 * long long 64 bit (inefficient, avoid these, if using : defaults to signed, but always specify signed/unsigned anyway) 00063 * float 32 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00064 * double 64 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00065 */ 00066 00067 00068 /* GLOBAL Variables */ 00069 // TODO change any of these that need it to volatile!!! 00070 00071 00073 //EXTERN unsigned char portHDebounce; // init to zero required 00074 //EXTERN unsigned short RPM[IN_OUT_BANKS]; // to be replaced with logging scheme for teeth. 00075 //EXTERN unsigned short tachoPeriod; 00076 // temporary test vars 00077 EXTERN unsigned short RPM[IN_OUT_BANKS]; // to be replaced with logging scheme for teeth. 00078 extern unsigned char portHDebounce; // init to zero required 00079 extern unsigned short tachoPeriod; 00080 00081 00082 /* Declare instances of variable structs for use */ 00083 EXTERN Clock Clocks; /* Timer Clocks for various functions */ 00084 EXTERN Counter Counters; /* Execution count for various blocks of code */ 00085 EXTERN RuntimeVar RuntimeVars; /* Execution times for various blocks of code */ 00086 EXTERN ISRLatencyVar ISRLatencyVars; /* Delay in execution start for various blocks of code */ 00087 00088 EXTERN CoreVar CoreVars; /* main running variables */ 00089 EXTERN DerivedVar DerivedVars; /* secondary running variables */ 00090 00091 EXTERN ADCArray ADCArrays; /* main adc storage area for syncronous sampling in the engine position ISR or injection ISR or ignition ISR etc. */ 00092 EXTERN ADCArray asyncADCArrays; /* secondary adc storage area for asynchronously sampling in the RTC/RTI ISR */ 00093 00094 00095 /* TODO declare ram references with names currently used in code. Change all flash structures to have different names. */ 00096 00097 00098 /*break this on purpose so i fix it later 00099 #define VETablereference (*((volatile mainTable*)(0x1000))) 00100 EXTERN const mainTable *VETableRef; 00101 PLUS 00102 const volatile mainTable *VETableRef = (volatile mainTable*)0x1000; 00103 broken too, need to research how to do this. 00104 00105 see line 80 or so from inc/injectorISR.c for array of pointer use. the above may not be possible... TODO */ 00106 00107 00108 /* Potentially pointers for data in ram depending on how it gets implemented */ 00109 // volatile ?? 00110 //EXTERN tunableConfig tunableConfigs; 00111 00112 /* Layout the tunable copies and buffers in ram space */ 00113 00114 00115 00116 /* Unions for paged large table access using RPAGE */ 00117 typedef union { 00118 mainTable VETableMain; 00119 mainTable IgnitionAdvanceTableMain; 00120 SmallTables1 SmallTablesA; 00121 } Tables1; 00122 00123 typedef union { 00124 mainTable VETableSecondary; 00125 mainTable IgnitionAdvanceTableSecondary; 00126 SmallTables2 SmallTablesB; 00127 } Tables2; 00128 00129 typedef union { 00130 mainTable VETableMainTertiary; 00131 mainTable InjectionAdvanceTableMain; 00132 SmallTables3 SmallTablesC; 00133 } Tables3; 00134 00135 typedef union { 00136 mainTable LambdaTable; 00137 mainTable InjectionAdvanceTableSecondary; 00138 SmallTables4 SmallTablesD; 00139 } Tables4; 00140 00141 00142 /* Large blocks */ 00143 EXTERN unsigned char TXBuffer[TX_BUFFER_SIZE] TXBUF; 00144 EXTERN unsigned char RXBuffer[RX_BUFFER_SIZE] RXBUF; 00145 //EXTERN unsigned char FlashBuffer[1024] BURNBUF; 00146 EXTERN Tables1 TablesA RWINDOW; 00147 EXTERN Tables2 TablesB RWINDOW; 00148 EXTERN Tables3 TablesC RWINDOW; 00149 EXTERN Tables4 TablesD RWINDOW; 00150 00151 00152 /* RAM page variables */ 00153 EXTERN unsigned char currentFuelRPage; 00154 EXTERN unsigned char currentTuneRPage; 00155 EXTERN unsigned char currentTimeRPage; 00156 00157 00158 //union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00159 // unsigned long timeLong; 00160 // unsigned short timeShorts[2]; 00161 //} LongNoTime RWINDOW ; 00162 00163 00164 //EXTERN config *configp; 00165 //EXTERN mainTable *VETableMain; 00166 //EXTERN mainTable *VETableSecondary; 00167 //EXTERN mainTable *VETableTertiary; 00168 //EXTERN mainTable *LambdaTable; 00169 00170 //EXTERN mainTable *AdvanceTableMain; 00171 //EXTERN mainTable *AdvanceTableSecondary; 00172 //EXTERN mainTable *AdvanceTableTertiary; 00173 00174 00175 /* Output variables (init not required) TODO ditch this in favour of the real vars in the calcs function and struct */ 00176 EXTERN unsigned short masterPulseWidth; 00177 EXTERN unsigned short totalDwell; 00178 EXTERN unsigned short totalAngleAfterReferenceInjection; 00179 EXTERN unsigned short totalAngleAfterReferenceIgnition; 00180 00181 EXTERN unsigned long bootFuelConst; /* constant derived from configurable constants */ 00182 EXTERN unsigned short TPSMAPRange; /* The MAP range used to convert fake TPS from MAP and vice versa */ 00183 EXTERN unsigned short TPSADCRange; /* The ADC range used to generate TPS percentage */ 00184 EXTERN unsigned short boundedTPSADC; // temp to view to debug 00185 00186 EXTERN unsigned short bootTimeAAP; /* TODO populate this at switch on time depending on a few things. */ 00187 00188 /* ALL STATUS STUFF HERE */ 00189 00190 /* State variables : 0 = false (don't forget to change the init mask to suit!) */ 00191 EXTERN unsigned short coreStatusA; /* Each bit represents the state of some core parameter, masks below */ 00192 /* Bit masks for coreStatusA */ // TODO needs a rename as does coresetingsA 00193 #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) */ 00194 #define PRIMARY_SYNC BIT2_16 /* 2 Wasted spark/Semi sequential */ 00195 #define SECONDARY_SYNC BIT3_16 /* 3 COP/Full sequential */ 00196 #define ENGINE_PHASE BIT4_16 /* 4 For COP/Sequential, which revolution we are in, first or second */ 00197 #define FUEL_CUT BIT5_16 /* 5 Remove injection completely */ 00198 #define HARD_SPARK_CUT BIT6_16 /* 6 Remove ignition completely */ 00199 #define SOFT_SPARK_CUT BIT7_16 /* 7 Remove ignition events round robin style */ 00200 #define SPARK_RETARD BIT8_16 /* 8 Retard ignition in RPM dependent way */ 00201 #define STAGED_REQUIRED BIT9_16 /* 9 Fire the staged injectors */ 00202 #define CALC_FUEL_IGN BIT10_16 /* 10 Fuel and ignition require calculation (i.e. variables have been updated) */ 00203 #define FORCE_READING BIT11_16 /* 11 Flag to force ADC sampling at low rpm/stall */ 00204 #define COREA12 BIT12_16 /* 12 */ 00205 #define COREA13 BIT13_16 /* 13 */ 00206 #define COREA14 BIT14_16 /* 14 */ 00207 #define COREA15 BIT15_16 /* 15 */ 00208 #define COREA16 BIT16_16 /* 16 */ 00209 00210 #define CLEAR_PRIMARY_SYNC NBIT2_16 00211 #define STAGED_NOT_REQUIRED NBIT9_16 /* 9 Do not fire the staged injectors */ 00212 #define CLEAR_CALC_FUEL_IGN NBIT10_16 /* 10 Fuel and ignition don't require calculation */ 00213 #define CLEAR_FORCE_READING NBIT11_16 /* 11 Clear flag to force ADC sampling at low rpm/stall */ 00214 00215 00216 /* Which bank to read from or write to at any time 00217 * 00218 * This banking system has four regions and four main controlling variables. 00219 * Each pair of variables should always be opposite each other, correct values being zero and one. 00220 * The two pairs can be reversed independently and indeed, this is how it functions. 00221 * Additionally, there are two pairs of offset variables for the math output/real time side 00222 * that depend on the value of the second main pair and should be consistent with those. 00223 */ 00224 00225 /* These two should always be opposite to each other */ 00226 00227 EXTERN unsigned char recordADCBank; /* For reading ADC inputs into */ 00228 EXTERN unsigned char mathInputBank; /* For reading values for math */ 00229 00230 /* This pair of tripplets should always be opposite to each other */ 00231 00232 /* For writing values out from math and reuse of values written out */ 00233 EXTERN unsigned char mathInternalBank; 00234 EXTERN unsigned char mathInternalInjectionOffset; 00235 EXTERN unsigned char mathInternalIgnitionOffset; 00236 00237 /* For reading values out of */ 00238 EXTERN unsigned char realTimeUseBank; 00239 EXTERN unsigned char realTimeUseInjectionOffset; 00240 EXTERN unsigned char realTimeUseIgnitionOffset; 00241 00242 00243 //TODO make this volatile? 00244 /* ECT IC extension variable (init not required, don't care where it is, only differences between figures) */ 00245 unsigned short timerExtensionClock; /* Increment for each overflow of the main timer, allows finer resolution and longer time period */ 00246 /* section 10.3.5 page 290 68hc11 reference manual e.g. groups.csail.mit.edu/drl/courses/cs54-2001s/pdf/M68HC11RM.pdf */ 00247 00248 00249 /* For extracting 32 bit long time stamps from the overflow counter and timer registers */ 00250 typedef union { /* Declare Union http://www.esacademy.com/faq/docs/cpointers/structures.htm */ 00251 unsigned long timeLong; 00252 unsigned short timeShorts[2]; 00253 } LongTime; 00254 00255 00256 /* Flag registers, init to zero required */ 00257 EXTERN unsigned char mainOn; /* Keep track of where we are at for possible use as multi interrupt per injection */ 00258 EXTERN unsigned short dwellOn; /* Keep track of ignition output state */ 00259 EXTERN unsigned char stagedOn; /* Ensure we turn an injector off again if we turn it on. */ 00260 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 */ 00261 EXTERN unsigned char rescheduleFuelFlags; /* Pulse width is probably longer than engine cycle so schedule a restart at the next start time */ 00262 00263 00264 /* Engine Position and RPM reading variables */ 00265 00266 /* Engine runtime properties (inits???) TODO */ 00267 EXTERN unsigned short primaryPulsesPerSecondaryPulse; /* Type short because of nissan style cam wheels (char would do for other types) */ 00268 EXTERN unsigned short primaryPulsesPerSecondaryPulseBuffer; /* Type short because of nissan style cam wheels (char would do for other types) */ 00269 EXTERN unsigned short primaryLeadingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00270 EXTERN unsigned short primaryTrailingEdgeTimeStamp; /* Store the timestamp of the leading edge during a pulse */ 00271 EXTERN unsigned short timeBetweenSuccessivePrimaryPulses; /* This number equates to the speed of the engine */ 00272 EXTERN unsigned short timeBetweenSuccessivePrimaryPulsesBuffer; /* This number equates to the speed of the engine */ 00273 EXTERN unsigned short lastPrimaryPulseTimeStamp; /* Store the timer value of the each pulse here before exiting the ISR */ 00274 EXTERN unsigned long engineCyclePeriod; /* Timer units between engine cycle starts */ 00275 EXTERN unsigned long lastSecondaryOddTimeStamp; 00276 EXTERN unsigned short primaryTeethDroppedFromLackOfSync; 00277 00278 /* Ignition stuff */ 00279 00280 // ignition experimentation stuff 00281 EXTERN unsigned char dwellQueueLength; /* 0 = no dwell pending start, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00282 EXTERN unsigned char ignitionQueueLength; /* 0 = no spark event pending, 1 = single event scheduled, 2 = one scheduled, and one in the queue, etc */ 00283 EXTERN unsigned short currentDwell[IN_OUT_BANKS]; /* Current dwell to be used. */ 00284 EXTERN unsigned char nextDwellChannel; /* Which one to bang off next */ 00285 EXTERN unsigned char nextIgnitionChannel; /* Which one to bang off next */ 00286 EXTERN unsigned short ignitionAdvances[IGNITION_CHANNELS * 2]; // Uses channel + offset to have two values at any time 00287 EXTERN unsigned short queuedDwellOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00288 EXTERN unsigned short queuedIgnitionOffsets[IGNITION_CHANNELS]; // Uses next channel as index 00289 00290 00291 /* Injection stuff */ 00292 00293 /* Register addresses */ 00294 EXTERN volatile unsigned short * volatile injectorMainTimeRegisters[INJECTION_CHANNELS]; 00295 EXTERN volatile unsigned char * volatile injectorMainControlRegisters[INJECTION_CHANNELS]; 00296 00297 /* Timer holding vars (init not required) */ 00298 EXTERN unsigned short injectorMainStartTimesHolding[INJECTION_CHANNELS]; 00299 EXTERN unsigned long injectorMainEndTimes[INJECTION_CHANNELS]; 00300 00301 // TODO make these names consistent 00302 /* Code time to run variables (init not required) */ 00303 EXTERN unsigned short injectorCodeOpenRuntimes[INJECTION_CHANNELS]; 00304 EXTERN unsigned short injectorCodeCloseRuntimes[INJECTION_CHANNELS]; 00305 00306 /* individual channel pulsewidths (init not required) */ 00307 EXTERN unsigned short injectorMainPulseWidths[INJECTION_CHANNELS * 2]; 00308 EXTERN unsigned short injectorStagedPulseWidths[INJECTION_CHANNELS * 2]; 00309 00310 /* Channel latencies (init not required) */ 00311 EXTERN unsigned short injectorCodeLatencies[INJECTION_CHANNELS]; 00312 00313 #undef IN_OUT_BANKS 00314 #undef EXTERN 00315 00316 #else 00317 /* let us know if we are being untidy with headers */ 00318 #warning "Header file FREEEMS_H seen before, sort it out!" 00319 /* end of the wrapper ifdef from the very top */ 00320 #endif