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