FreeEMS  0.2.0-SNAPSHOT-282-g9efc524
MitsiAndMazda-CAS-4and1.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
34 }
35 
36 
37 /** @file
38  *
39  * @ingroup interruptHandlers
40  * @ingroup enginePositionRPMDecoders
41  *
42  * @brief Reads Mitsi 4 and 1 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=1078
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  // Pins 0, 2, 4 and 7 - no need to check for numbers, just always do on rising edge and only in primary isr
78  // Always sample in this ISR
81 
82  // Set flag to say calc required
84 
85  // Reset the clock for reading timeout
87 
88  // Determine the correct event based on post transition state
89  unsigned char correctEvent = 0;
90  if(PTITCurrentState & 0x01){
91  if(!(PTITCurrentState & 0x02)){
92  correctEvent = 8;
93  }
94  }else{
95  if(PTITCurrentState & 0x02){
97  if(unknownLeadingEdges == 3){
98  correctEvent = 4;
99  }
100  }else{
101  correctEvent = 7;
103  }
104  }
105 
106  unsigned char lastEvent = 0;
108  lastEvent = KeyUserDebugs.currentEvent;
112  }
113 
114  // ...and, if known, 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;
208  if(PTITCurrentState & 0x02){
209  correctEvent = 9;
210  }else{
211  correctEvent = 6;
212  }
213 
214  // Only sample if not synced, cleans up readings.
218 
219  // Set flag to say calc required
221 
222  // Reset the clock for reading timeout
224  }
225 
226  unsigned char lastEvent = 0;
228  lastEvent = KeyUserDebugs.currentEvent;
230 
231  // ...and check that it's correct
232  if(KeyUserDebugs.currentEvent != correctEvent){
234  return;
237  } // Else do nothing, we're running!
238  }else{ // If not synced, sync, as in this ISR we always know where we are.
239  KeyUserDebugs.currentEvent = correctEvent;
240  lastEvent = KeyUserDebugs.currentEvent - 1;
242  }
243 
244  unsigned short thisTicksPerDegree = 0;
246  unsigned short thisAngle = eventAngles[KeyUserDebugs.currentEvent] - eventAngles[lastEvent];
247 
248  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
249 
251  unsigned short ratioBetweenThisAndLast = (unsigned short)(((unsigned long)lastTicksPerDegree * 1000) / thisTicksPerDegree);
252  KeyUserDebugs.inputEventTimeTolerance = ratioBetweenThisAndLast;
255  return;
256  }else if(ratioBetweenThisAndLast < fixedConfigs2.decoderSettings.accelerationInputEventTimeTolerance){
258  return;
259  }
261  *ticksPerDegreeRecord = thisTicksPerDegree;
262  }
263  }
264 
265  SCHEDULE_ECT_OUTPUTS();
266 
267  OUTPUT_COARSE_BBS();
268 
270  lastTicksPerDegree = thisTicksPerDegree;
272  }
273  // Always
274  lastEventTimeStamp = thisEventTimeStamp;
276 
278 }