Subversion Repositories chibiosIgnition

Rev

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

Rev 8 Rev 11
Line 10... Line 10...
10
 
10
 
11
#include "timer2.h"
11
#include "timer2.h"
12
#define  MICROSECS_PULSE 10
12
#define  MICROSECS_PULSE 10
13
 
13
 
14
 
14
 
15
 
-
 
16
 
-
 
17
 
-
 
18
 
-
 
19
// with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000
15
// with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000
20
// freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the
16
// freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the
21
// first pulse longer than 1/300 of a second as being a proper closure .
17
// first pulse longer than 1/300 of a second as being a proper closure .
22
// the TIM2 counter counts in 10uS increments,
18
// the TIM2 counter counts in 10uS increments,
23
#define BREAKER_COUNT_MIN (1E6/(MICROSECS_PULSE * 300))
19
#define BREAKER_COUNT_MIN (1E6/(MICROSECS_PULSE * 300))
Line 27... Line 23...
27
uint16_t nominal  = 0;
23
uint16_t nominal  = 0;
28
uint16_t phase10 = 100; // 10 degrees
24
uint16_t phase10 = 100; // 10 degrees
29
volatile uint16_t sampleCount = 0;
25
volatile uint16_t sampleCount = 0;
30
uint16_t outSampleCount = 0;
26
uint16_t outSampleCount = 0;
31
volatile uint16_t sampleBuff[SAMPLE_BUFF_SIZE];
27
volatile uint16_t sampleBuff[SAMPLE_BUFF_SIZE];
32
typedef enum { WAIT_GAP, SKIP_BOUNCE } sampleState_t ;
28
typedef enum { WAIT_GAP, SKIP_BOUNCE, HAVE_SAMPLE } sampleState_t ;
33
sampleState_t  sampleState = WAIT_GAP;
29
sampleState_t  sampleState = WAIT_GAP;
-
 
30
// difference between samples
-
 
31
volatile uint16_t deltaTime;
34
 
32
 
35
 
33
 
36
uint16_t rpm;
34
uint16_t rpm;
37
 
35
 
38
void initTimer2()
36
void initTimer2()
Line 46... Line 44...
46
                        TIM_CR1_ARPE );
44
                        TIM_CR1_ARPE );
47
 
45
 
48
        /// pulse width 200 uS
46
        /// pulse width 200 uS
49
        TIM2->CCR1 = 200/MICROSECS_PULSE;
47
        TIM2->CCR1 = 200/MICROSECS_PULSE;
50
 
48
 
51
    TIM2->CCER =  TIM_CCER_CC1E | TIM_CCER_CC1P  ; //enabled and low
49
    TIM2->CCER =  TIM_CCER_CC1E | TIM_CCER_CC1P  ; //enabled and active high
52
 
50
 
53
    TIM2->CCMR1 = TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 |
51
    TIM2->CCMR1 = TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 |
54
                           TIM_CCMR1_OC1PE ;
52
                           TIM_CCMR1_OC1PE ;
55
 
53
 
56
 
54
 
57
    TIM2->CR2 = TIM_CR2_MMS_1 ; // trigger out is 010 = update
55
    TIM2->CR2 = TIM_CR2_MMS_1 ; // trigger out is 010 = update
58
 
56
 
Line 122... Line 120...
122
uint16_t getRPM(void)
120
uint16_t getRPM(void)
123
{
121
{
124
        return rpm;
122
        return rpm;
125
}
123
}
126
 
124
 
-
 
125
uint16_t getDelta(void)
-
 
126
{
-
 
127
        return deltaTime;
-
 
128
}
-
 
129
 
127
uint16_t wrapIndex(uint16_t index)
130
uint16_t wrapIndex(uint16_t index)
128
{
131
{
129
        if (index > SAMPLE_BUFF_SIZE)
132
        if (index >= SAMPLE_BUFF_SIZE)
130
                index -= SAMPLE_BUFF_SIZE;
133
                index -= SAMPLE_BUFF_SIZE;
131
    return index;
134
    return index;
132
}
135
}
133
 
136
 
134
 
137
 
135
// allows for wrapping
138
// allows for wrapping
136
uint16_t getSampleBuff(uint16_t index)
139
uint16_t getSampleBuff(uint16_t index)
137
{
140
{
-
 
141
        chSysLock();
138
        return sampleBuff[wrapIndex(index)];
142
        return sampleBuff[wrapIndex(index)];
-
 
143
        chSysUnlock();
139
}
144
}
140
 
145
 
-
 
146
 
141
// waits for ignition pulse , debounces readings
147
// waits for ignition pulse , debounces readings,
-
 
148
// returns the pulse time, skips debounce time
142
uint16_t getNextPulse(void)
149
uint16_t getNextPulse(void)
143
{
150
{
144
        static uint16_t lastVal = 0;
151
        static uint16_t lastSampleIndex = 0;
145
        uint16_t retVal ;
-
 
146
        uint8_t done = 0;
-
 
147
        while(done == 0)
152
        while(1)
148
        {
153
        {
149
                uint16_t diff;
-
 
150
                // wait until there are enough samples
-
 
151
                while(1)
-
 
152
                {
-
 
153
 
-
 
154
                        diff = sampleCount - outSampleCount;
-
 
155
                        if(outSampleCount >= sampleCount)
154
                        while (outSampleCount == sampleCount)
156
                                diff = sampleCount - outSampleCount;
-
 
157
                        else
-
 
158
                                diff = SAMPLE_BUFF_SIZE + sampleCount - outSampleCount;
-
 
159
 
-
 
160
                        if(diff > 1)
-
 
161
                                break;
-
 
162
                        chThdSleep(10);
155
                                chThdSleep(10);
163
                }
-
 
164
 
156
 
165
                // pick the next out of gap sample
-
 
166
                if(sampleState == WAIT_GAP)
-
 
167
                {
-
 
168
                        done = 1;
-
 
169
                        retVal = getSampleBuff(outSampleCount);
-
 
170
                }
-
 
171
                // see how many samples are too close together
-
 
172
                sampleState = SKIP_BOUNCE;
-
 
173
                uint16_t endCount = sampleCount;
-
 
174
                while((sampleState == SKIP_BOUNCE) && (outSampleCount != endCount))
-
 
175
                {
-
 
176
                        uint16_t thisTime  = getSampleBuff(outSampleCount);
157
                        uint16_t thisTime  = getSampleBuff(outSampleCount);
-
 
158
 
177
                        outSampleCount = wrapIndex(outSampleCount + 1);
159
                        outSampleCount = wrapIndex(outSampleCount + 1);
178
                        uint16_t nextTime = getSampleBuff(outSampleCount);
160
                        while (outSampleCount == sampleCount)
-
 
161
                                chThdSleep(10);
179
 
162
 
180
                        uint16_t deltaTime;
163
                        uint16_t nextTime = getSampleBuff(outSampleCount);
181
 
164
 
182
                        // calculate wrapped time delta : should be > than bounce time to allow
165
                        // calculate wrapped time delta : should be > than bounce time to allow
183
                        if(nextTime > thisTime)
-
 
184
                                deltaTime = nextTime - thisTime;
166
                        uint16_t diffTime = nextTime - thisTime;
185
                        else
-
 
186
                                deltaTime = 65536 + nextTime - thisTime;
-
 
187
 
167
 
188
                        if(deltaTime > BREAKER_COUNT_MIN)
168
                        if(diffTime > BREAKER_COUNT_MIN)
189
                        {
169
                        {
190
                                sampleState = WAIT_GAP;
170
                                lastSampleIndex = outSampleCount;
191
                                break;
171
                                return nextTime;
192
                        }
172
                        }
193
 
173
 
194
                }
-
 
195
        }
174
        }
-
 
175
  return 0;
-
 
176
}
196
 
177
 
-
 
178
 
-
 
179
void processNextPulse(uint16_t retVal)
-
 
180
{
-
 
181
 
-
 
182
        static uint16_t lastVal = 0;
197
        // at this point we should try to phase lock
183
        // at this point we should try to phase lock
198
    uint32_t period;
-
 
199
    if(retVal > lastVal)
184
    deltaTime = retVal - lastVal;
-
 
185
 
-
 
186
 
-
 
187
 
200
        period = retVal - lastVal;
188
    if(deltaTime > 10000)
201
    else
189
    {
202
        period = 65536 + retVal - lastVal;
190
        __asm(" BKPT #0");
-
 
191
    }
203
 
192
 
204
    lastVal = retVal;
193
    lastVal = retVal;
205
 
194
 
-
 
195
 
-
 
196
 
-
 
197
 
-
 
198
 
206
    float nomRPM = 30E6 / (MICROSECS_PULSE * period) ;
199
    float nomRPM = 30E6 / (MICROSECS_PULSE * deltaTime) ;
207
 
200
 
208
        rpm = rpm + (nomRPM -rpm)/10;
201
        rpm = rpm + (nomRPM -rpm)/10;
209
 
202
 
210
 
203
 
211
 
204
 
Line 220... Line 213...
220
                rpm = rpm + 1;
213
                rpm = rpm + 1;
221
 
214
 
222
//      rpm += delta / 256;
215
//      rpm += delta / 256;
223
 
216
 
224
        adjustRPM();
217
        adjustRPM();
-
 
218
    }
-
 
219
 
225
 
220
 
226
        return retVal;
-
 
227
}
-
 
228
 
221
 
229
 
222
 
230
// set the timing advance from reference to
223
// set the timing advance from reference to
231
void setAdvance(int16_t deg10)
224
void setAdvance(int16_t deg10)
232
{
225
{
Line 242... Line 235...
242
        if(stat & TIM_SR_CC1IF)
235
        if(stat & TIM_SR_CC1IF)
243
        {
236
        {
244
                TIM3->SR &= ~TIM_SR_CC1IF;
237
                TIM3->SR &= ~TIM_SR_CC1IF;
245
            uint16_t sample = TIM3->CCR1;
238
            uint16_t sample = TIM3->CCR1;
246
            sampleBuff[sampleCount++] = sample;
239
            sampleBuff[sampleCount++] = sample;
247
            if (sampleCount > SAMPLE_BUFF_SIZE)
240
            if (sampleCount >= SAMPLE_BUFF_SIZE)
248
                sampleCount = 0;
241
                sampleCount = 0;
249
        }
242
        }
250
}
243
}
251
 
244
 
252
 
245