Subversion Repositories chibiosIgnition

Rev

Rev 20 | Rev 23 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 21
Line 141... Line 141...
141
 
141
 
142
   //   if(lock)
142
   //   if(lock)
143
 
143
 
144
       int intSample = sampleAverage / freqScale;
144
       int intSample = sampleAverage / freqScale;
145
 
145
 
146
static unsigned strange = 0;
-
 
147
 
146
 
148
 
147
 
149
       int deltaPd= pd/ phaseScale;
148
       int deltaPd= pd/ phaseScale;
150
 
149
 
151
 
150
 
152
       if(pd < -2000 || pd > 2000)
151
       if(deltaPd == 0)
153
       {
152
       {
154
           strange++;
153
       if(pd > 0)
155
       }
154
           deltaPd =1;
156
 
155
 
-
 
156
       if(pd<  0)
157
       arr =  intSample + deltaPd;
157
           deltaPd =-1;
-
 
158
       }
158
 
159
 
-
 
160
           arr =  intSample + deltaPd;
159
 
161
 
160
 
162
 
-
 
163
// clamp values
161
 
164
 
162
 
165
 
163
    if(arr > 65535)
166
    if(arr > 65535)
164
        arr = 65535;
167
        arr = 65535;
165
    if(arr < 1000)
168
    if(arr < 1000)
166
        arr = 1000;
169
        arr = 1000;
167
 
170
 
168
 
-
 
169
 
-
 
170
     count = arr;
171
     count = arr;
171
 
172
 
172
    TIM2->ARR = arr -1;
173
    TIM2->ARR = arr -1;
173
 
174
 
174
        nominal = intSample * (long) (phase10)/ 3600;
175
    nominal = intSample * (long) (phase10)/ 3600;
175
 
-
 
176
 
-
 
177
 
176
 
178
    float nomRPM = 30E6 / (MICROSECS_PULSE * arr);
177
    float nomRPM = 30E6 / (MICROSECS_PULSE * arr);
179
 
178
 
180
        rpm =  nomRPM ;
179
        rpm =  nomRPM ;
181
 
180
 
182
 
-
 
183
        adjustRPM();
181
        adjustRPM();
184
    }
182
    }
185
 
183
 
186
 
184
 
187
 
185
 
Line 194... Line 192...
194
}
192
}
195
 
193
 
196
 
194
 
197
 
195
 
198
// specialist timer setup :
196
// specialist timer setup :
-
 
197
// timer 2 is a reloading counter with a cycle period controlled by its ARR register.
-
 
198
// Just before terminal count it produces a pulse of 200 microseconds using its CCR1 count compare register,
-
 
199
// used to drive the strobe LED.
-
 
200
// Timer 3 is then used to count the time of the reload of Timer 2 via its Trigger Out being selected as reload.
-
 
201
// The time is latched in TIM3  CCR2
-
 
202
// and ignition pulses are latched on TIM3 CCR1, to allow it to be used in a PLL.
-
 
203
//
199
void initTimer2()
204
void initTimers()
200
{
205
{
201
        rccEnableTIM2(FALSE);
206
        rccEnableTIM2(FALSE);
202
        rccResetTIM2();
207
        rccResetTIM2();
203
 
208
 
204
        TIM2->PSC = 72*MICROSECS_PULSE;
209
        TIM2->PSC = 72*MICROSECS_PULSE;
Line 216... Line 221...
216
 
221
 
217
 
222
 
218
    TIM2->CR2 = TIM_CR2_MMS_1 ; // trigger out is 010 = update
223
    TIM2->CR2 = TIM_CR2_MMS_1 ; // trigger out is 010 = update
219
 
224
 
220
 
225
 
221
    // change the TIM2 CC2 to TIM3 CC1
-
 
222
        rccEnableTIM3(FALSE);
226
    rccEnableTIM3(FALSE);
223
        rccResetTIM3();
227
        rccResetTIM3();
224
        // TIM3 on the PA6 ... pins : remap code 00
228
        // TIM3 on the PA6 ... pins : remap code 00
225
        AFIO->MAPR &= ~ AFIO_MAPR_TIM3_REMAP;
229
        AFIO->MAPR &= ~ AFIO_MAPR_TIM3_REMAP;
226
 
230
 
227
        TIM3->PSC = 72*MICROSECS_PULSE;
231
        TIM3->PSC = 72*MICROSECS_PULSE;