Added firmware/ to the ignore file, Makefile is functional but still needs work ...
[freeems-vanilla.git] / src / MiataNB.c
blobc3e19a70d9c40148550aa62239c809cf13e702f2
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 * @todo TODO This file contains SFA but Abe Mara is going to fill it up with
33 * @todo TODO wonderful goodness very soon ;-)
35 * @author Who Ever
39 #include "inc/freeEMS.h"
40 #include "inc/interrupts.h"
43 /** Primary RPM ISR
45 * @todo TODO Docs here!
47 void PrimaryRPMISR(void)
49 /* Clear the interrupt flag for this input compare channel */
50 TFLG = 0x01;
52 /* Save all relevant available data here */
53 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */
54 unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */
55 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
56 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */
58 /* Calculate the latency in ticks */
59 ISRLatencyVars.primaryInputLatency = codeStartTimeStamp - edgeTimeStamp;
61 // TODO discard narrow ones! test for tooth width and tooth period
63 /* Set up edges as per config */
64 unsigned char risingEdge;
65 if(fixedConfigs1.coreSettingsA & PRIMARY_POLARITY){
66 risingEdge = PTITCurrentState & 0x01;
67 }else{
68 risingEdge = !(PTITCurrentState & 0x01);
71 if(risingEdge){
72 /* Echo input condition on J7 */
73 PORTJ |= 0x80;
75 // increment crank pulses TODO this needs to be wrapped in tooth period and width checking
76 primaryPulsesPerSecondaryPulse++;
78 LongTime timeStamp;
80 /* Install the low word */
81 timeStamp.timeShorts[1] = edgeTimeStamp;
82 /* Find out what our timer value means and put it in the high word */
83 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
84 timeStamp.timeShorts[0] = timerExtensionClock + 1;
85 }else{
86 timeStamp.timeShorts[0] = timerExtensionClock;
88 RuntimeVars.primaryInputLeadingRuntime = TCNT - codeStartTimeStamp;
89 }else{
90 PORTJ &= 0x7F;
91 RuntimeVars.primaryInputTrailingRuntime = TCNT - codeStartTimeStamp;
94 Counters.primaryTeethSeen++;
98 /** Secondary RPM ISR
100 * @todo TODO Docs here!
102 void SecondaryRPMISR(void)
104 /* Clear the interrupt flag for this input compare channel */
105 TFLG = 0x02;
107 /* Save all relevant available data here */
108 unsigned short codeStartTimeStamp = TCNT; /* Save the current timer count */
109 unsigned short edgeTimeStamp = TC1; /* Save the timestamp */
110 unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
111 // unsigned short PORTS_BACurrentState = PORTS_BA; /* Save ignition output state */
113 /* Calculate the latency in ticks */
114 ISRLatencyVars.secondaryInputLatency = codeStartTimeStamp - edgeTimeStamp;
116 // TODO discard narrow ones! test for tooth width and tooth period
118 /* Set up edges as per config */
119 unsigned char risingEdge;
120 if(fixedConfigs1.coreSettingsA & SECONDARY_POLARITY){
121 risingEdge = PTITCurrentState & 0x02;
122 }else{
123 risingEdge = !(PTITCurrentState & 0x02);
126 if(risingEdge){
127 // echo input condition
128 PORTJ |= 0x40;
130 LongTime timeStamp;
132 /* Install the low word */
133 timeStamp.timeShorts[1] = edgeTimeStamp;
134 /* Find out what our timer value means and put it in the high word */
135 if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
136 timeStamp.timeShorts[0] = timerExtensionClock + 1;
137 }else{
138 timeStamp.timeShorts[0] = timerExtensionClock;
141 RuntimeVars.secondaryInputLeadingRuntime = TCNT - codeStartTimeStamp;
142 }else{
143 PORTJ &= 0xBF;
144 RuntimeVars.secondaryInputTrailingRuntime = TCNT - codeStartTimeStamp;
147 Counters.secondaryTeethSeen++;