00001 /* FreeEMS - the open source engine management system 00002 * 00003 * Copyright 2009, 2010 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 00042 #include "inc/freeEMS.h" 00043 #include "inc/interrupts.h" 00044 #include "inc/DecoderInterface.h" 00045 00046 00051 void PrimaryRPMISR(void) 00052 { 00053 /* Clear the interrupt flag for this input compare channel */ 00054 TFLG = 0x01; 00055 00056 /* Save all relevant available data here */ 00057 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00058 unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */ 00059 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00060 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00061 00062 /* Calculate the latency in ticks */ 00063 ISRLatencyVars.primaryInputLatency = codeStartTimeStamp - edgeTimeStamp; 00064 00065 // TODO discard narrow ones! test for tooth width and tooth period 00066 00067 /* Set up edges as per config */ 00068 unsigned char risingEdge; 00069 if(fixedConfigs1.coreSettingsA & PRIMARY_POLARITY){ 00070 risingEdge = PTITCurrentState & 0x01; 00071 }else{ 00072 risingEdge = !(PTITCurrentState & 0x01); 00073 } 00074 00075 if(risingEdge){ 00076 /* Echo input condition on J7 */ 00077 PORTJ |= 0x80; 00078 00079 // increment crank pulses TODO this needs to be wrapped in tooth period and width checking 00080 primaryPulsesPerSecondaryPulse++; 00081 00082 LongTime timeStamp; 00083 00084 /* Install the low word */ 00085 timeStamp.timeShorts[1] = edgeTimeStamp; 00086 /* Find out what our timer value means and put it in the high word */ 00087 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */ 00088 timeStamp.timeShorts[0] = timerExtensionClock + 1; 00089 }else{ 00090 timeStamp.timeShorts[0] = timerExtensionClock; 00091 } 00092 RuntimeVars.primaryInputLeadingRuntime = TCNT - codeStartTimeStamp; 00093 }else{ 00094 PORTJ &= 0x7F; 00095 RuntimeVars.primaryInputTrailingRuntime = TCNT - codeStartTimeStamp; 00096 } 00097 00098 Counters.primaryTeethSeen++; 00099 } 00100 00101 00106 void SecondaryRPMISR(void) 00107 { 00108 /* Clear the interrupt flag for this input compare channel */ 00109 TFLG = 0x02; 00110 00111 /* Save all relevant available data here */ 00112 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */ 00113 unsigned short edgeTimeStamp = TC1; /* Save the timestamp */ 00114 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */ 00115 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */ 00116 00117 /* Calculate the latency in ticks */ 00118 ISRLatencyVars.secondaryInputLatency = codeStartTimeStamp - edgeTimeStamp; 00119 00120 // TODO discard narrow ones! test for tooth width and tooth period 00121 00122 /* Set up edges as per config */ 00123 unsigned char risingEdge; 00124 if(fixedConfigs1.coreSettingsA & SECONDARY_POLARITY){ 00125 risingEdge = PTITCurrentState & 0x02; 00126 }else{ 00127 risingEdge = !(PTITCurrentState & 0x02); 00128 } 00129 00130 if(risingEdge){ 00131 // echo input condition 00132 PORTJ |= 0x40; 00133 00134 LongTime timeStamp; 00135 00136 /* Install the low word */ 00137 timeStamp.timeShorts[1] = edgeTimeStamp; 00138 /* Find out what our timer value means and put it in the high word */ 00139 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */ 00140 timeStamp.timeShorts[0] = timerExtensionClock + 1; 00141 }else{ 00142 timeStamp.timeShorts[0] = timerExtensionClock; 00143 } 00144 00145 RuntimeVars.secondaryInputLeadingRuntime = TCNT - codeStartTimeStamp; 00146 }else{ 00147 PORTJ &= 0xBF; 00148 RuntimeVars.secondaryInputTrailingRuntime = TCNT - codeStartTimeStamp; 00149 } 00150 00151 Counters.secondaryTeethSeen++; 00152 }