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