Rev 47 | Rev 49 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 47 | Rev 48 | ||
---|---|---|---|
Line 24... | Line 24... | ||
24 | /* USER CODE BEGIN Includes */ |
24 | /* USER CODE BEGIN Includes */ |
25 | #include "libSerial/serial.h" |
25 | #include "libSerial/serial.h" |
26 | #include "libPLX/plx.h" |
26 | #include "libPLX/plx.h" |
27 | #include "misc.h" |
27 | #include "misc.h" |
28 | 28 | ||
- | 29 | #include "libIgnTiming/rpm.h" |
|
- | 30 | ||
29 | /* USER CODE END Includes */ |
31 | /* USER CODE END Includes */ |
30 | 32 | ||
31 | /* Private typedef -----------------------------------------------------------*/ |
33 | /* Private typedef -----------------------------------------------------------*/ |
32 | /* USER CODE BEGIN PTD */ |
34 | /* USER CODE BEGIN PTD */ |
33 | 35 | ||
Line 47... | Line 49... | ||
47 | 49 | ||
48 | #define ADC_REF_CHAN 5 |
50 | #define ADC_REF_CHAN 5 |
49 | 51 | ||
50 | #define ADC_TEMP_CHAN 6 |
52 | #define ADC_TEMP_CHAN 6 |
51 | 53 | ||
52 | // with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000 |
- | |
53 | // freq = 5000/60 * 2 = 166Hz. |
- | |
54 | // the TIM2 counter counts in 10uS increments, |
- | |
55 | // Need to accumulate low level for a 400th of a second before accepting it as a pulse |
- | |
56 | #define ACCUM_MAX (RPM_COUNT_RATE / 400) |
- | |
57 | - | ||
58 | #define GLITCH_MAX (RPM_COUNT_RATE / 2000) |
- | |
59 | - | ||
60 | #define RPM_AVERAGE 4 |
- | |
61 | - | ||
62 | // wait for about 1 second to decide whether or not starter is on |
54 | // wait for about 1 second to decide whether or not starter is on |
63 | 55 | ||
64 | #define STARTER_LIMIT 10 |
56 | #define STARTER_LIMIT 10 |
65 | 57 | ||
66 | /* USER CODE END PM */ |
58 | /* USER CODE END PM */ |
Line 84... | Line 76... | ||
84 | volatile char TimerFlag = 0; |
76 | volatile char TimerFlag = 0; |
85 | 77 | ||
86 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
78 | volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1 |
87 | volatile char NoSerialIn = 0; |
79 | volatile char NoSerialIn = 0; |
88 | 80 | ||
89 | // scale for filtered samples |
- | |
90 | #define Scale 1024.0 |
- | |
- | 81 | ||
91 | 82 | ||
92 | // storage for ADC |
83 | // storage for ADC |
93 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
84 | uint16_t ADC_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; |
94 | 85 | ||
95 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
86 | uint32_t FILT_Samples[ADC_CHANNELS] = {[0 ... ADC_CHANNELS - 1] = 0}; // filtered ADC samples * Scale |
Line 102... | Line 93... | ||
102 | const float STM32REF = 1.2; // 1.2V typical |
93 | const float STM32REF = 1.2; // 1.2V typical |
103 | 94 | ||
104 | // scale factor initially assuming |
95 | // scale factor initially assuming |
105 | float ADC_Scale = 1 / (Scale * 4096) * NOM_VREF; |
96 | float ADC_Scale = 1 / (Scale * 4096) * NOM_VREF; |
106 | 97 | ||
107 | // Rev counter processing from original RevCounter Project |
- | |
108 | uint16_t RPM_Diff = 0; |
- | |
109 | uint16_t RPM_Count_Latch = 0; |
- | |
110 | // accumulators |
- | |
111 | uint16_t RPM_Pulsecount = 0; |
- | |
112 | unsigned int RPM_FilteredWidth = 0; |
- | |
113 | - | ||
114 | // last time we detected end of dwell i.e. ignition pulse |
- | |
115 | uint16_t last_dwell_end = 0; |
- | |
116 | uint16_t RPM_Period[RPM_AVERAGE]; |
- | |
117 | unsigned int RPM_Period_Ptr = 0; |
- | |
118 | - | ||
119 | unsigned int Coded_RPM = 0; |
98 | unsigned int Coded_RPM = 0; |
120 | unsigned int Coded_CHT = 0; |
99 | unsigned int Coded_CHT = 0; |
121 | 100 | ||
122 | uint32_t PowerTempTimer; |
101 | uint32_t PowerTempTimer; |
123 | 102 | ||
Line 173... | Line 152... | ||
173 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
152 | ADC_Scale = 1 / (Scale * 4096) * adc_vref; |
174 | } |
153 | } |
175 | 154 | ||
176 | void ProcessRPM(int instance) |
155 | void ProcessRPM(int instance) |
177 | { |
156 | { |
178 | // compute the timer values |
- | |
179 | // snapshot timers |
- | |
180 | unsigned short RPM_Pulsewidth; |
- | |
181 | // current RPM pulse next slot index |
- | |
182 | unsigned short RPM_Count_Val; |
- | |
183 | - | ||
184 | // accumulator for pulse widths |
- | |
185 | static unsigned short RPM_Accumulator = ACCUM_MAX; |
- | |
186 | // Next state of pulse high/low |
- | |
187 | static unsigned char RPM_State = 1; |
157 | static unsigned int Coded_RPM = 0; |
188 | // Current state of pulse high/low |
- | |
189 | static unsigned char RPM_State_Curr = 1; |
- | |
190 | - | ||
191 | __disable_irq(); // copy the counter value |
- | |
192 | RPM_Count_Val = RPM_Count; |
- | |
193 | __enable_irq(); |
- | |
194 | // do calculations |
158 | int32_t rpm = CalculateRPM(); |
195 | - | ||
196 | // if there is only one entry, cannot get difference |
- | |
197 | if (RPM_Count_Latch != RPM_Count_Val) |
- | |
198 | { |
- | |
199 | while (1) |
159 | if (rpm >= 0) |
200 | { |
- | |
201 | unsigned int base_time; |
- | |
202 | unsigned int new_time; |
- | |
203 | // if we are at N-1, stop. |
- | |
204 | unsigned int next_count = (RPM_Count_Latch + 1) % RPM_SAMPLES; |
- | |
205 | if (next_count == RPM_Count_Val) |
- | |
206 | { |
- | |
207 | break; // completed loop |
- | |
208 | } |
- | |
209 | char pulse_level = (RPM_Time[RPM_Count_Latch] & RPM_FLAG) ? 1 : 0; |
- | |
210 | base_time = RPM_Time[RPM_Count_Latch] & ~RPM_FLAG; |
- | |
211 | new_time = RPM_Time[next_count] & ~RPM_FLAG; |
- | |
212 | RPM_Count_Latch = next_count; |
- | |
213 | - | ||
214 | RPM_Pulsewidth = new_time - base_time; |
- | |
215 | - | ||
216 | if (pulse_level == 0 && (RPM_Pulsewidth > ACCUM_MAX)) |
- | |
217 | RPM_State = 1; |
- | |
218 | if (pulse_level == 1) |
- | |
219 | RPM_State = 0; |
- | |
220 | #if 0 |
- | |
221 | RPM_State_Curr = RPM_State; |
- | |
222 | // Count up/down |
- | |
223 | if (pulse_level == 0) |
- | |
224 | { |
- | |
225 | if (RPM_Pulsewidth > RPM_Accumulator) // going to cross zero |
- | |
226 | { |
- | |
227 | RPM_State = 0; |
- | |
228 | RPM_Accumulator = 0; |
- | |
229 | } |
- | |
230 | else |
- | |
231 | { |
- | |
232 | RPM_Accumulator -= RPM_Pulsewidth; |
- | |
233 | } |
- | |
234 | } |
- | |
235 | else |
- | |
236 | { |
- | |
237 | if (RPM_Accumulator + RPM_Pulsewidth > ACCUM_MAX) |
- | |
238 | { |
- | |
239 | RPM_State = 1; |
160 | Coded_RPM = rpm / 19.55; |
240 | RPM_Accumulator = ACCUM_MAX; |
- | |
241 | } |
- | |
242 | else |
- | |
243 | { |
- | |
244 | RPM_Accumulator += RPM_Pulsewidth; |
- | |
245 | } |
- | |
246 | } |
- | |
247 | #endif |
- | |
248 | // low pulse has reached at least minimum width, count it. |
- | |
249 | if ((RPM_State == 1) && (RPM_State_Curr == 0)) |
- | |
250 | { |
- | |
251 | RPM_Diff = new_time - last_dwell_end; |
- | |
252 | - | ||
253 | RPM_Period[RPM_Period_Ptr] = RPM_Diff; |
- | |
254 | RPM_Period_Ptr = (RPM_Period_Ptr + 1) % RPM_AVERAGE; |
- | |
255 | if (RPM_Pulsecount < RPM_AVERAGE) |
- | |
256 | RPM_Pulsecount++; // count one pulse |
- | |
257 | last_dwell_end = new_time; |
- | |
258 | } |
- | |
259 | RPM_State_Curr = RPM_State; |
- | |
260 | } |
- | |
261 | } |
- | |
262 | - | ||
263 | if (RPM_Pulsecount == RPM_AVERAGE) |
- | |
264 | { |
- | |
265 | // now have time for N pulses in clocks |
- | |
266 | // need to scale by 19.55: one unit is 19.55 RPM |
- | |
267 | // 1Hz is 30 RPM |
- | |
268 | int i; |
- | |
269 | RPM_FilteredWidth = 0; |
- | |
270 | for (i = 0; i < RPM_AVERAGE; i++) |
- | |
271 | RPM_FilteredWidth += RPM_Period[i]; |
- | |
272 | - | ||
273 | Coded_RPM = (Scale * 30.0 * RPM_AVERAGE * RPM_COUNT_RATE) / (19.55 * RPM_FilteredWidth); |
- | |
274 | - | ||
275 | #if !defined MY_DEBUG |
- | |
276 | // reset here unless we want to debug |
- | |
277 | RPM_Pulsecount = 0; |
- | |
278 | #endif |
- | |
279 | } |
- | |
280 | 161 | ||
281 | // send the current RPM *calculation |
162 | // send the current RPM *calculation |
282 | plx_sendword(PLX_RPM); |
163 | plx_sendword(PLX_RPM); |
283 | PutCharSerial(&uc1, instance); |
164 | PutCharSerial(&uc1, instance); |
284 | plx_sendword(Coded_RPM / Scale); |
165 | plx_sendword(Coded_RPM / Scale); |