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 00039 /* Header file multiple inclusion protection courtesy eclipse Header Template */ 00040 /* and http://gcc.gnu.org/onlinedocs/gcc-3.1.1/cpp/ C pre processor manual */ 00041 #ifndef FILE_STRUCTS_H_SEEN 00042 #define FILE_STRUCTS_H_SEEN 00043 00044 00045 /* For noobs : http://www.space.unibe.ch/comp_doc/c_manual/C/SYNTAX/struct.html 00046 * http://en.wikipedia.org/wiki/Composite_type 00047 * http://www.cs.usfca.edu/~wolber/SoftwareDev/C/CStructs.htm 00048 * http://sandbox.mc.edu/~bennet/cs220/codeex/struct_c.html 00049 */ 00050 00051 00052 /* Naming should be in the singular form such that the instantiation can be the plural */ 00053 00054 00055 /* Types summary 00056 * 00057 * BEWARE : Be explicit!! 00058 * 00059 * char 8 bit (defaults to unsigned, but always specify signed/unsigned anyway) 00060 * short 16 bit (defaults to signed, but always specify signed/unsigned anyway) 00061 * 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) 00062 * long 32 bit (defaults to signed, but always specify signed/unsigned anyway) 00063 * long long 64 bit (inefficient, avoid these, if using : defaults to signed, but always specify signed/unsigned anyway) 00064 * float 32 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00065 * double 64 bit IEEE floating point numbers (inefficient, avoid these, used fixed point math) 00066 */ 00067 00068 00069 // TODO various new structs 00070 // Status struct 00071 // Ignition struct 00072 // Injection struct 00073 // Engine position struct 00074 // Validity flags, or they should be in the status struct? 00075 00076 00083 typedef struct { 00084 unsigned char RAMPage; 00085 unsigned char FlashPage; 00086 void* RAMAddress; 00087 void* FlashAddress; 00088 unsigned short size; 00089 } blockDetails; 00090 00091 00092 #define DERIVED_VARS_SIZE sizeof(DerivedVar) 00093 #define DERIVED_VARS_WIDTH IN_OUT_BANKS /* How many elements per array currently 2 at 25/6/08 */ 00094 #define DERIVED_VARS_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00095 /* Use this block to make it easy to manage the derived variables */ 00096 typedef struct { 00097 /* Calculated from core vars */ 00098 unsigned short LoadMain; /* Configurable unit of load */ 00099 // unsigned short LoadSecondary; 00100 00101 unsigned short VEMain; /* Divide by 512 to get % */ 00102 // unsigned short VESecondary; 00103 00104 unsigned short Lambda; /* Divide by 32768 to get Lamda */ 00105 unsigned short AirFlow; /* Top half of the equation */ 00106 unsigned short densityAndFuel; /* Bottom half of the equation */ 00107 00108 unsigned short BasePW; /* Raw PW before corrections */ 00109 unsigned short ETE; /* Additional PW for ETE correction */ 00110 signed short TFCTotal; /* Transient fuel correction PW (+/-) */ 00111 00112 unsigned short EffectivePW; /* Actual PW of fuel delivery */ 00113 unsigned short IDT; /* Minimum PW before fuel flow begins */ 00114 unsigned short RefPW; /* Reference electrical PW */ 00115 00116 unsigned short sp1; /* */ 00117 unsigned short sp2; /* */ 00118 unsigned short sp3; /* */ 00119 unsigned short sp4; /* */ 00120 unsigned short sp5; /* */ 00121 00122 // unsigned short ; /* */ 00123 } DerivedVar; 00124 00125 00126 #define RUNTIME_VARS_SIZE sizeof(RuntimeVar) 00127 #define RUNTIME_VARS_LENGTH 13 /* How many runtime vars */ 00128 #define RUNTIME_VARS_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00129 /* Use this block to manage the execution time of various functions loops and ISRs etc */ 00130 typedef struct { 00131 /* Engine position and RPM code runtimes */ 00132 unsigned short primaryInputLeadingRuntime; 00133 unsigned short primaryInputTrailingRuntime; 00134 unsigned short secondaryInputLeadingRuntime; 00135 unsigned short secondaryInputTrailingRuntime; 00136 00137 /* Mathematics runtimes */ 00138 unsigned short calcsRuntime; 00139 unsigned short genCoreVarsRuntime; 00140 unsigned short genDerivedVarsRuntime; 00141 unsigned short mathTotalRuntime; 00142 unsigned short mathSumRuntime; 00143 00144 unsigned short RTCRuntime; 00145 00146 /* */ 00147 unsigned short mainLoopRuntime; 00148 unsigned short logSendingRuntime; 00149 unsigned short serialISRRuntime; 00150 } RuntimeVar; 00151 00152 00153 #define ISR_LATENCY_VARS_SIZE sizeof(ISRLatencyVar) 00154 #define ISR_LATENCY_VARS_LENGTH 2 /* How many latency vars */ 00155 #define ISR_LATENCY_VARS_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00156 /* Use this block to manage the execution time of various functions loops and ISRs etc */ 00157 typedef struct { 00158 /* Engine position and RPM code latencies */ 00159 unsigned short primaryInputLatency; 00160 unsigned short secondaryInputLatency; 00161 00162 /* Injector latencies */ 00163 unsigned short Injector1Latency; 00164 unsigned short Injector2Latency; 00165 unsigned short Injector3Latency; 00166 unsigned short Injector4Latency; 00167 unsigned short Injector5Latency; 00168 unsigned short Injector6Latency; 00169 00170 unsigned short DwellLatency; 00171 unsigned short IgniteLatency; 00172 00173 /* Not an ISR, but important none the less */ 00174 unsigned short mathLatency; 00175 unsigned short mathSampleTimeStamp0; 00176 unsigned short mathSampleTimeStamp1; 00177 } ISRLatencyVar; 00178 00179 00180 #define CORE_VARS_SIZE sizeof(CoreVar) 00181 #define CORE_VARS_LENGTH 16 /* How many arrays */ 00182 #define CORE_VARS_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00183 /* Use this block to make it easy to manage the core variables */ 00184 typedef struct { 00185 /* Calculated and averaged from ADC0 readings */ 00186 unsigned short IAT; /* Inlet Air Temperature (MAT JS) : 0.0 - 655.35 (0.01 Kelvin (/100)) */ 00187 unsigned short CHT; /* Coolant / Head Temperature (CLT JS) : 0.0 - 655.35 (0.01 Kelvin (/100)) */ 00188 unsigned short TPS; /* Throttle Position Sensor (TPS JS) : 0.0 - 102.39 (0.001? % (/640)) */ 00189 unsigned short EGO; /* Exhaust Gas Oxygen (O2 JS) : 0.000 - 1.999999 (0.0001? lambda (/32768)) */ 00190 unsigned short MAP; /* Manifold Absolute Pressure (5euroh1) : 0.0 - 655.35 (0.01 kPa (/100)) */ 00191 unsigned short AAP; /* Atmospheric Absolute Pressure (6euroh1) : 0.0 - 655.35 (0.01 kPa (/100)) */ 00192 unsigned short BRV; /* Battery Reference Voltage (4euroh1) : 0.000 - 65.535 (0.001 Volts (/1000)) */ 00193 unsigned short MAT; /* Manifold Air Temperature (Spare JS) : 0.0 - 655.35 (0.01 Kelvin (/100)) */ 00194 00195 /* Calculated and averaged from ADC1 readings */ 00196 unsigned short EGO2; /* Exhaust Gas Oxygen (NC) : 0.000 - 1.999999 (0.0001? lambda (/32768)) */ 00197 unsigned short IAP; /* Intercooler Absolute Pressure (NC) : 0.0 - 655.35 (0.01 kPa (/100)) */ 00198 unsigned short MAF; /* Mass Air Flow : 0.0 - 65535.0 (raw units from lookup) */ 00199 00200 /* Calculated from MAP and TPS history */ 00201 unsigned short DMAP; /* Delta MAP kPa/second or similar */ 00202 unsigned short DTPS; /* Delta TPS %/second or similar */ 00203 00204 /* Calculated from engine position data */ 00205 unsigned short RPM; /* Revolutions Per Minute (Calced) : 0 - 32767.5 (0.5 RPM (/2)) */ 00206 unsigned short DRPM; /* Delta RPM (Calced) : 0 - 32767.5 (0.5 RPM/Second (/2)) */ 00207 unsigned short DDRPM; /* Delta Delta RPM (Calced) : 0 - 32767.5 (0.5 RPM/Second^2 (/2)) */ 00208 } CoreVar; 00209 00210 00211 #define ADC_ARRAY_SIZE sizeof(ADCArray) 00212 #define ADC_ARRAY_LENGTH 16 /* How many arrays */ 00213 #define ADC_ARRAY_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00214 /* Use this block to ensure that the components are contiguous and we can then reference them via offsets and pointers */ 00215 typedef struct { 00216 /* ADC0 raw readings */ 00217 unsigned short IAT; /* Inlet Air Temperature (MAT JS) */ /* COMPULSORY! */ 00218 unsigned short CHT; /* Coolant / Head Temperature (CLT JS) */ /* COMPULSORY! */ 00219 unsigned short TPS; /* Throttle Position Sensor (TPS JS) */ /* Reduced performance without */ 00220 unsigned short EGO; /* Exhaust Gas Oxygen (O2 JS) */ /* Recommended */ 00221 unsigned short MAP; /* Manifold Absolute Pressure (5euroh1) */ /* COMPULSORY OR TPS OR MAF */ 00222 unsigned short AAP; /* Atmospheric Absolute Pressure (6euroh1) */ /* Recommended */ 00223 unsigned short BRV; /* Battery Reference Voltage (4euroh1) */ /* COMPULSORY! */ 00224 unsigned short MAT; /* Manifold Air Temperature (Spare JS) */ /* Could help heat soak issues */ 00225 00226 /* ADC1 raw readings */ 00227 unsigned short EGO2; /* Exhaust Gas Oxygen (NC) */ /* V engine option */ 00228 unsigned short IAP; /* Intercooler Absolute Pressure (NC) */ /* Turbo engine option */ 00229 unsigned short MAF; /* Mass Air Flow (NC) */ /* OEM engine option */ 00230 unsigned short SpareADC3; /* Spare ADC1 port 3 (NC) */ 00231 unsigned short SpareADC4; /* Spare ADC1 port 4 (NC) */ 00232 unsigned short SpareADC5; /* Spare ADC1 port 5 (NC) */ 00233 unsigned short SpareADC6; /* Spare ADC1 port 6 (NC) */ 00234 unsigned short SpareADC7; /* Spare ADC1 port 7 (NC) */ 00235 } ADCArray; 00236 00237 00238 #define MAINTABLE_SIZE sizeof(mainTable) 00239 #define MAINTABLE_RPM_LENGTH 24 /* How many cells on the X axis */ 00240 #define MAINTABLE_LOAD_LENGTH 19 /* How many cells on the Y axis */ 00241 #define MAINTABLE_MAX_RPM_LENGTH 27 /* How many cells on the X axis max */ 00242 #define MAINTABLE_MAX_LOAD_LENGTH 21 /* How many cells on the Y axis max */ 00243 #define MAINTABLE_MAX_MAIN_LENGTH 462 /* 924B 462 shorts maximum main table length */ 00244 00245 00279 typedef struct { 00280 unsigned short RPMLength; /* The length of the RPM axis array */ 00281 unsigned short LoadLength; /* The length of the Load axis array */ 00282 unsigned short RPM[MAINTABLE_MAX_RPM_LENGTH]; /* The array of RPM (X) axis values */ 00283 unsigned short Load[MAINTABLE_MAX_LOAD_LENGTH]; /* The array of Load (Y) axis values */ 00284 unsigned short Table[MAINTABLE_MAX_MAIN_LENGTH]; /* The table as an array of values */ 00285 } mainTable; 00286 00287 00288 #define TWODTABLEUS_SIZE sizeof(twoDTableUS) 00289 #define TWODTABLEUS_LENGTH 16 00290 /* This block used for various curves */ 00291 typedef struct { 00292 unsigned short Axis[TWODTABLEUS_LENGTH]; 00293 unsigned short Values[TWODTABLEUS_LENGTH]; 00294 } twoDTableUS; 00295 00296 00297 #define TWODTABLESS_SIZE sizeof(twoDTableSS) 00298 #define TWODTABLESS_LENGTH 16 00299 /* This block used for various curves */ 00300 typedef struct { 00301 signed short Axis[TWODTABLESS_LENGTH]; 00302 signed short Values[TWODTABLESS_LENGTH]; 00303 } twoDTableSS; 00304 00305 00306 #define TWODTABLEMS_SIZE sizeof(twoDTableMS) 00307 #define TWODTABLEMS_LENGTH 16 00308 /* This block used for various curves */ 00309 typedef struct { 00310 unsigned short Axis[TWODTABLEMS_LENGTH]; 00311 signed short Values[TWODTABLEMS_LENGTH]; 00312 } twoDTableMS; 00313 00314 00315 #define TWODTABLEUC_SIZE sizeof(twoDTableUC) 00316 #define TWODTABLEUC_LENGTH 8 00317 /* This block used for various curves */ 00318 typedef struct { 00319 unsigned char Axis[TWODTABLEUC_LENGTH]; 00320 unsigned char Values[TWODTABLEUC_LENGTH]; 00321 } twoDTableUC; 00322 00323 00324 #define COUNTER_SIZE sizeof(Counter) 00325 #define COUNTER_LENGTH 21 /* How many counters */ 00326 #define COUNTER_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00327 /* Use this block to manage the execution count of various functions loops and ISRs etc */ 00328 typedef struct { 00329 /* Event Counters (all require init to zero) */ 00330 unsigned short callsToUISRs; /* Counter to ensure we aren't accidentally triggering unused ISRs */ 00331 unsigned short lowVoltageConditions; /* Counter for low voltage conditions */ 00332 00333 unsigned short crankSyncLosses; /* Counter for number of lost crank syncs */ 00334 unsigned short camSyncLosses; /* Counter for number of lost cam syncs */ 00335 unsigned short RPMValidityLosses; /* Counter for number of lost RPM validity events */ 00336 unsigned short primaryTeethDroppedFromLackOfSync; /* Counter for number of primary teeth dropped due to no primary sync */ 00337 // TODO remove the one above this line about teeth dropped???? probably... 00338 00339 unsigned short primaryTeethSeen; /* Free running counters for number of teeth seen such that... */ 00340 unsigned short secondaryTeethSeen; /* ...tooth timing can be used to reconstruct the signal at lower rpm */ 00341 00342 unsigned short syncedADCreadings; /* Incremented each time a syncronous ADC reading is taken */ 00343 unsigned short timeoutADCreadings; /* Incremented for each ADC reading in RTC because of timeout */ 00344 00345 unsigned short calculationsPerformed; /* Incremented for each time the fuel and ign calcs are done */ 00346 unsigned short datalogsSent; /* Incremented for each time we send out a log entry */ 00347 00348 /* UART/serial specific counters */ 00349 unsigned short serialStartsInsideAPacket; /* Incremented when a start byte is found inside a packet */ 00350 unsigned short serialEscapePairMismatches; /* Incremented when an escape is found but not followed by an escapee */ 00351 unsigned short serialPacketsOverLength; /* Incremented when the buffer fills up before the end */ 00352 unsigned short serialNoiseErrors; /* Incremented when noise is detected */ 00353 unsigned short serialOverrunErrors; /* Incremented when an overrun occurs */ 00354 unsigned short serialFramingErrors; /* Incremented when a framing error occurs */ 00355 unsigned short serialParityErrors; /* Incremented when a parity error occurs */ 00356 00357 /* Generic com counters */ 00358 unsigned short commsChecksumMismatches; /* Incremented when calculated checksum did not match the received one */ 00359 unsigned short commsDebugMessagesNotSent; /* Incremented when a debug message can't be sent due to the TX buffer */ 00360 unsigned short commsErrorMessagesNotSent; /* Incremented when an error message can't be sent due to the TX buffer */ 00361 } Counter; 00362 00363 00364 #define CLOCK_SIZE sizeof(Clock) 00365 #define CLOCK_LENGTH 9 /* How many clocks */ 00366 #define CLOCK_UNIT 2 /* How large each element is in bytes (short = 2 bytes) */ 00367 /* Use this block to manage the various clocks kept */ 00368 typedef struct { 00369 /* Real Time and other Clocks (all require init to zero) */ 00370 unsigned short realTimeClockMain; /* Variable to count RTI executions, 0.125ms exactly */ 00371 unsigned short realTimeClockMillis; /* Variable to count milliseconds exactly */ 00372 unsigned short realTimeClockTenths; /* Variable to count tenths of a second exactly */ 00373 unsigned short realTimeClockSeconds; /* Variable to count seconds exactly */ 00374 unsigned short realTimeClockMinutes; /* Variable to count minutes exactly */ 00375 00376 unsigned short millisToTenths; /* Roll-over variable for counting tenths */ 00377 unsigned short tenthsToSeconds; /* Roll-over variable for counting seconds */ 00378 unsigned short secondsToMinutes; /* Roll-over variable for counting minutes */ 00379 00380 unsigned short timeoutADCreadingClock; /* Timeout clock/counter for synced ADC readings */ 00381 } Clock; 00382 00383 00384 #else 00385 /* let us know if we are being untidy with headers */ 00386 #warning "Header file STRUCTS_H seen before, sort it out!" 00387 /* end of the wrapper ifdef from the very top */ 00388 #endif