Merge branch 'philsmaster'
[freeems-vanilla.git] / src / MiataNB.c
blob16a13af7cb9cab6417f4dddcd79b2524d945c4fc
1 /* FreeEMS - the open source engine management system
3 Copyright 2009 Fred Cooke
5 This file is part of the FreeEMS project.
7 FreeEMS software is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 FreeEMS software is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
20 We ask that if you make any changes to this file you email them upstream to
21 us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
23 Thank you for choosing FreeEMS to run your engine! */
26 /** @file MiataNB.c
27 * @ingroup interruptHandlers
28 * @ingroup enginePositionRPMDecoders
30 * @brief Miata from 9x to 0x
32 * @note Pseudo code that does not compile with zero warnings and errors MUST be commented out.
34 * @todo TODO This file contains SFA but Abe Mara is going to fill it up with
35 * @todo TODO wonderful goodness very soon ;-)
37 * @author Who Ever
41 #include "inc/freeEMS.h"
42 #include "inc/interrupts.h"
45 /** Primary RPM ISR
47 * @todo TODO Docs here!
49 void PrimaryRPMISR(void)
51 /* Clear the interrupt flag for this input compare channel */
52 TFLG = 0x01;
54 /* Save all relevant available data here */
55 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */
56 unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */
57 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
58 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */
60 /* Calculate the latency in ticks */
61 ISRLatencyVars.primaryInputLatency = codeStartTimeStamp - edgeTimeStamp;
63 // TODO discard narrow ones! test for tooth width and tooth period
65 /* Set up edges as per config */
66 unsigned char risingEdge;
67 if(fixedConfigs1.coreSettingsA & PRIMARY_POLARITY){
68 risingEdge = PTITCurrentState & 0x01;
69 }else{
70 risingEdge = !(PTITCurrentState & 0x01);
73 if(risingEdge){
74 /* Echo input condition on J7 */
75 PORTJ |= 0x80;
77 // increment crank pulses TODO this needs to be wrapped in tooth period and width checking
78 primaryPulsesPerSecondaryPulse++;
80 LongTime timeStamp;
82 /* Install the low word */
83 timeStamp.timeShorts[1] = edgeTimeStamp;
84 /* Find out what our timer value means and put it in the high word */
85 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
86 timeStamp.timeShorts[0] = timerExtensionClock + 1;
87 }else{
88 timeStamp.timeShorts[0] = timerExtensionClock;
90 RuntimeVars.primaryInputLeadingRuntime = TCNT - codeStartTimeStamp;
91 }else{
92 PORTJ &= 0x7F;
93 RuntimeVars.primaryInputTrailingRuntime = TCNT - codeStartTimeStamp;
96 Counters.primaryTeethSeen++;
100 /** Secondary RPM ISR
102 * @todo TODO Docs here!
104 void SecondaryRPMISR(void)
106 /* Clear the interrupt flag for this input compare channel */
107 TFLG = 0x02;
109 /* Save all relevant available data here */
110 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */
111 unsigned short edgeTimeStamp = TC1; /* Save the timestamp */
112 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
113 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */
115 /* Calculate the latency in ticks */
116 ISRLatencyVars.secondaryInputLatency = codeStartTimeStamp - edgeTimeStamp;
118 // TODO discard narrow ones! test for tooth width and tooth period
120 /* Set up edges as per config */
121 unsigned char risingEdge;
122 if(fixedConfigs1.coreSettingsA & SECONDARY_POLARITY){
123 risingEdge = PTITCurrentState & 0x02;
124 }else{
125 risingEdge = !(PTITCurrentState & 0x02);
128 if(risingEdge){
129 // echo input condition
130 PORTJ |= 0x40;
132 LongTime timeStamp;
134 /* Install the low word */
135 timeStamp.timeShorts[1] = edgeTimeStamp;
136 /* Find out what our timer value means and put it in the high word */
137 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
138 timeStamp.timeShorts[0] = timerExtensionClock + 1;
139 }else{
140 timeStamp.timeShorts[0] = timerExtensionClock;
143 RuntimeVars.secondaryInputLeadingRuntime = TCNT - codeStartTimeStamp;
144 }else{
145 PORTJ &= 0xBF;
146 RuntimeVars.secondaryInputTrailingRuntime = TCNT - codeStartTimeStamp;
149 Counters.secondaryTeethSeen++;