FreeEMS  0.2.0-SNAPSHOT-285-g028e24c
MitsiAndMazda-CAS-4and2.c
Go to the documentation of this file.
1 /* FreeEMS - the open source engine management system
2  *
3  * Copyright 2011-2013 Fred Cooke
4  *
5  * This file is part of the FreeEMS project.
6  *
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.
11  *
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.
16  *
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/
19  *
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!
22  *
23  * Thank you for choosing FreeEMS to run your engine!
24  */
25 
26 
27 #define DECODER_IMPLEMENTATION_C
28 
30 
31 void decoderInitPreliminary(){} // This decoder works with the defaults
33  doubleHighSeen = 0;
34 }
35 
36 
37 /** @file
38  *
39  * @ingroup interruptHandlers
40  * @ingroup enginePositionRPMDecoders
41  *
42  * @brief Reads Mitsi 4 and 2 CAS units
43  *
44  * Two interrupts share state and cross communicate to
45  * find and maintain sync and position information.
46  *
47  * Development thread: http://forum.diyefi.org/viewtopic.php?f=56&t=1110
48  */
49 
50 
52  /* Clear the interrupt flag for this input compare channel */
53  TFLG = 0x01;
55 
56  /* Save all relevant available data here */
57  edgeTimeStamp = TC0; /* Save the edge time stamp */
58  unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
59 
61 
62  /* Install the low word */
64  /* Find out what our timer value means and put it in the high word */
65  if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
67  }else{
69  }
70  unsigned long thisEventTimeStamp = timeStamp.timeLong;
71 
72  unsigned long thisInterEventPeriod = 0;
74  thisInterEventPeriod = thisEventTimeStamp - lastEventTimeStamp;
75  }
76 
77  // Always sample in this ISR
80 
81  // Set flag to say calc required
83 
84  // Reset the clock for reading timeout
86 
87  // Determine the correct event based on post transition state
88  unsigned char correctEvent = 0;
89  if(PTITCurrentState & 0x01){
90  if(!(PTITCurrentState & 0x02)){
91  correctEvent = 10;
92  }else{ // Occurs three times
93  doubleHighSeen = 1;
94  }
95  }else{
96  if(PTITCurrentState & 0x02){
97  // Only this one is not intercepted by a clear
98  if(doubleHighSeen == 1){
99  correctEvent = 6;
100  }
101  }else{ // Clear on double low
102  doubleHighSeen = 0;
103  }
104  }
105 
106  unsigned char lastEvent = 0;
108  lastEvent = KeyUserDebugs.currentEvent;
112  }
113 
114  // ...and check that it's correct
115  if(correctEvent != 0){
116  if(KeyUserDebugs.currentEvent != correctEvent){
118  return;
121  } // Else do nothing, we're running!
122  }
123  }else if(correctEvent != 0){
124  KeyUserDebugs.currentEvent = correctEvent;
125  lastEvent = KeyUserDebugs.currentEvent - 1;
127  }
128 
129  unsigned short thisTicksPerDegree = 0;
131  unsigned short thisAngle = 0;
132  if(KeyUserDebugs.currentEvent == 0){
133  thisAngle = eventAngles[KeyUserDebugs.currentEvent] + totalEventAngleRange - eventAngles[lastEvent] ; // Optimisable... leave readable for now! :-p J/K learn from this...
134  }else{
135  thisAngle = eventAngles[KeyUserDebugs.currentEvent] - eventAngles[lastEvent];
136  }
137 
138  thisTicksPerDegree = (unsigned short)((ticks_per_degree_multiplier * thisInterEventPeriod) / thisAngle); // with current scale range for 60/12000rpm is largest ticks per degree = 3472, smallest = 17 with largish error
139 
141  unsigned short ratioBetweenThisAndLast = (unsigned short)(((unsigned long)lastTicksPerDegree * 1000) / thisTicksPerDegree);
142  KeyUserDebugs.inputEventTimeTolerance = ratioBetweenThisAndLast;
145  return;
146  }else if(ratioBetweenThisAndLast < fixedConfigs2.decoderSettings.accelerationInputEventTimeTolerance){
148  return;
149  }else{
150  if(PTITCurrentState & 0x01){
151  // TODO Calculate RPM from last primaryLeadingEdgeTimeStamp
152  }else{
153  // TODO Calculate RPM from last primaryTrailingEdgeTimeStamp
154  }
155  }
156  }/*else*/ if(KeyUserDebugs.decoderFlags & LAST_TIMESTAMP_VALID){ // TODO temp for testing just do rpm this way, fill above out later.
157  *ticksPerDegreeRecord = thisTicksPerDegree;
158  }
159  }
160 
161  SCHEDULE_ECT_OUTPUTS();
162 
163  OUTPUT_COARSE_BBS();
164 
166  lastTicksPerDegree = thisTicksPerDegree;
168  }
169  // Always
170  lastEventTimeStamp = thisEventTimeStamp;
172 
174 }
175 
176 
178  // Reads the inner slot on the disk.
179 
180  /* Clear the interrupt flag for this input compare channel */
181  TFLG = 0x02;
183 
184  /* Save all relevant available data here */
185  edgeTimeStamp = TC1; /* Save the timestamp */
186  unsigned char PTITCurrentState = PTIT; /* Save the values on port T regardless of the state of DDRT */
187 
189  // remember that this is both edges, though... 8 per cycle, 4 per rev for the outter wheel, 2/1 for this wheel.
190 
191  /* Install the low word */
193  /* Find out what our timer value means and put it in the high word */
194  if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
196  }else{
198  }
199  unsigned long thisEventTimeStamp = timeStamp.timeLong;
200 
201  unsigned long thisInterEventPeriod = 0;
203  thisInterEventPeriod = thisEventTimeStamp - lastEventTimeStamp;
204  }
205 
206  // Determine the correct event based on post transition state (and toggle debug pins)
207  unsigned char correctEvent = 0;
208  if(PTITCurrentState & 0x02){
209  if(PTITCurrentState & 0x01){
210  correctEvent = 11;
211  }else{
212  correctEvent = 4;
213  }
214  } // else the leading edge of the slot is ambiguous
215 
216  // Only sample if not synced, cleans up readings.
220 
221  // Set flag to say calc required
223 
224  // Reset the clock for reading timeout
226  }
227 
228  unsigned char lastEvent = 0;
230  lastEvent = KeyUserDebugs.currentEvent;
232 
233  // ...and check that it's correct
234  if(correctEvent != 0){
235  if(KeyUserDebugs.currentEvent != correctEvent){
237  return;
240  } // Else do nothing, we're running!
241  }
242  }else if(correctEvent != 0){
243  KeyUserDebugs.currentEvent = correctEvent;
244  lastEvent = KeyUserDebugs.currentEvent - 1;
246  }
247 
248  unsigned short thisTicksPerDegree = 0;
250  unsigned short thisAngle = eventAngles[KeyUserDebugs.currentEvent] - eventAngles[lastEvent];
251 
252  thisTicksPerDegree = (unsigned short)((ticks_per_degree_multiplier * thisInterEventPeriod) / thisAngle); // with current scale range for 60/12000rpm is largest ticks per degree = 3472, smallest = 17 with largish error
253 
255  unsigned short ratioBetweenThisAndLast = (unsigned short)(((unsigned long)lastTicksPerDegree * 1000) / thisTicksPerDegree);
256  KeyUserDebugs.inputEventTimeTolerance = ratioBetweenThisAndLast;
259  return;
260  }else if(ratioBetweenThisAndLast < fixedConfigs2.decoderSettings.accelerationInputEventTimeTolerance){
262  return;
263  }
265  *ticksPerDegreeRecord = thisTicksPerDegree;
266  }
267  }
268 
269  SCHEDULE_ECT_OUTPUTS();
270 
271  OUTPUT_COARSE_BBS();
272 
274  lastTicksPerDegree = thisTicksPerDegree;
276  }
277  // Always
278  lastEventTimeStamp = thisEventTimeStamp;
280 
282 }