00001 /* FreeEMS - the open source engine management system 00002 00003 Copyright 2009, 2010 Philip L Johnson, 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 00038 #include "inc/freeEMS.h" 00039 #include "inc/interrupts.h" 00040 #include "inc/DecoderInterface.h" 00041 00042 00047 void PrimaryRPMISR(void) { 00048 static LongTime thisHighLowTime = { 0 }; 00049 static LongTime lastHighLowTime = { 0 }; 00050 static LongTime lowTime = { 0 }; 00051 static LongTime lastPeriod = { 0 }; 00052 static LongTime lastTimeStamp = { 0 }; 00053 static unsigned int count = 0; 00054 00055 /* Clear the interrupt flag for this input compare channel */ 00056 TFLG = 0x01; 00057 00058 /* Save all relevant available data here */ 00059 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00060 unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */ 00061 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00062 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00063 00064 /* Calculate the latency in ticks */ 00065 ISRLatencyVars.primaryInputLatency = codeStartTimeStamp - edgeTimeStamp; 00066 00067 LongTime thisTimeStamp; 00068 /* Install the low word */ 00069 thisTimeStamp.timeShorts[1] = edgeTimeStamp; 00070 /* Find out what our timer value means and put it in the high word */ 00071 if (TFLGOF && !(edgeTimeStamp & 0x8000)) { /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */ 00072 thisTimeStamp.timeShorts[0] = timerExtensionClock + 1; 00073 } else { 00074 thisTimeStamp.timeShorts[0] = timerExtensionClock; 00075 } 00076 00077 /* How many ticks between transitions? */ 00078 LongTime thisPeriod; 00079 if (thisTimeStamp.timeLong > lastTimeStamp.timeLong) { 00080 thisPeriod.timeLong = thisTimeStamp.timeLong - lastTimeStamp.timeLong; 00081 } else { 00082 thisPeriod.timeLong = thisTimeStamp.timeLong + (0xFFFFFFFF 00083 - lastTimeStamp.timeLong); 00084 } 00085 lastTimeStamp.timeLong = thisTimeStamp.timeLong; 00086 00087 /* Set up edges as per config */ 00088 unsigned char risingEdge = PTITCurrentState & 0x01; 00089 00090 if (lastPeriod.timeLong != 0) { 00091 if (risingEdge) { 00092 thisHighLowTime.timeLong = thisPeriod.timeLong + lowTime.timeLong; 00093 //Find the missing tooth 00094 if (count == 0 || count == 70) { 00095 if (thisHighLowTime.timeLong > (lastHighLowTime.timeLong + (lastHighLowTime.timeLong>>1)) && 00096 thisHighLowTime.timeLong < ((lastHighLowTime.timeLong<<1) + (lastHighLowTime.timeLong>>1))) { 00097 // We have sync 00098 PORTP |= 0x80; 00099 count = 1; 00100 } else { 00101 //We have lost sync 00102 count = 0; 00103 PORTP &= 0x7F; 00104 } 00105 }else if (count == 2 || (count%2 == 0 && 00106 thisHighLowTime.timeLong > (lastHighLowTime.timeLong>>1) && 00107 thisHighLowTime.timeLong < (lastHighLowTime.timeLong<<1) )) { 00108 count++; 00109 } else { 00110 //We have lost sync 00111 count = 0; 00112 PORTP &= 0x7F; 00113 } 00114 00115 /* Echo input condition on J7 */ 00116 PORTJ |= 0x80; 00117 // increment crank pulses TODO this needs to be wrapped in tooth period and width checking 00118 lastHighLowTime.timeLong = thisHighLowTime.timeLong; 00119 primaryPulsesPerSecondaryPulse++; 00120 RuntimeVars.primaryInputLeadingRuntime = TCNT - codeStartTimeStamp; 00121 } else { 00122 if (count%2 == 1) { 00123 count++; 00124 } else { 00125 //We have lost sync 00126 count = 0; 00127 PORTP &= 0x7F; 00128 } 00129 /* Echo input condition on J7 */ 00130 PORTJ &= 0x7F; 00131 RuntimeVars.primaryInputTrailingRuntime = TCNT - codeStartTimeStamp; 00132 lowTime.timeLong = thisPeriod.timeLong; 00133 } 00134 } 00135 lastPeriod.timeLong = thisPeriod.timeLong; 00136 Counters.primaryTeethSeen++; 00137 } 00138 00139 00144 void SecondaryRPMISR(void) { 00145 /* Clear the interrupt flag for this input compare channel */ 00146 TFLG = 0x02; 00147 00148 /* Save all relevant available data here */ 00149 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00150 unsigned short edgeTimeStamp = TC1; /* Save the timestamp */ 00151 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00152 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00153 00154 /* Calculate the latency in ticks */ 00155 ISRLatencyVars.secondaryInputLatency = codeStartTimeStamp - edgeTimeStamp; 00156 00157 // TODO discard narrow ones! test for tooth width and tooth period 00158 00159 /* Set up edges as per config */ 00160 unsigned char risingEdge; 00161 if (fixedConfigs1.coreSettingsA & SECONDARY_POLARITY) { 00162 risingEdge = PTITCurrentState & 0x02; 00163 } else { 00164 risingEdge = !(PTITCurrentState & 0x02); 00165 } 00166 00167 if (risingEdge) { 00168 // echo input condition 00169 PORTJ |= 0x40; 00170 00171 LongTime timeStamp; 00172 00173 /* Install the low word */ 00174 timeStamp.timeShorts[1] = edgeTimeStamp; 00175 /* Find out what our timer value means and put it in the high word */ 00176 if (TFLGOF && !(edgeTimeStamp & 0x8000)) { /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */ 00177 timeStamp.timeShorts[0] = timerExtensionClock + 1; 00178 } else { 00179 timeStamp.timeShorts[0] = timerExtensionClock; 00180 } 00181 00182 RuntimeVars.secondaryInputLeadingRuntime = TCNT - codeStartTimeStamp; 00183 } else { 00184 PORTJ &= 0xBF; 00185 RuntimeVars.secondaryInputTrailingRuntime = TCNT - codeStartTimeStamp; 00186 } 00187 00188 Counters.secondaryTeethSeen++; 00189 }