@@ -56,7 +56,7 @@ void us_ticker_irq_handler(void);
56
56
57
57
static int us_ticker_inited = 0 ;
58
58
59
- static ADI_TMR_CONFIG tmrConfig , tmr2Config ;
59
+ static ADI_TMR_CONFIG tmrConfig ;
60
60
61
61
static volatile uint32_t Upper_count = 0 , largecnt = 0 ;
62
62
@@ -201,14 +201,14 @@ static void event_timer()
201
201
cnt = 65536u - cnt ;
202
202
}
203
203
204
- tmr2Config .nLoad = cnt ;
205
- tmr2Config .nAsyncLoad = cnt ;
206
- adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmr2Config );
204
+ tmrConfig .nLoad = cnt ;
205
+ tmrConfig .nAsyncLoad = cnt ;
206
+ adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmrConfig );
207
207
adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , true);
208
208
} else {
209
- tmr2Config .nLoad = 65535u ;
210
- tmr2Config .nAsyncLoad = 65535u ;
211
- adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmr2Config );
209
+ tmrConfig .nLoad = 65535u ;
210
+ tmrConfig .nAsyncLoad = 65535u ;
211
+ adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmrConfig );
212
212
adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , true);
213
213
}
214
214
}
@@ -249,6 +249,8 @@ static void GP2CallbackFunction(void *pCBParam, uint32_t Event, void * pArg)
249
249
void us_ticker_init (void )
250
250
{
251
251
if (us_ticker_inited ) {
252
+ // Disable ticker interrupt on reinitialization
253
+ adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , false);
252
254
return ;
253
255
}
254
256
@@ -283,15 +285,15 @@ void us_ticker_init(void)
283
285
adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP1 , & tmrConfig );
284
286
285
287
/* Configure GP2 for doing event counts */
286
- tmr2Config .bCountingUp = true;
287
- tmr2Config .bPeriodic = true;
288
- tmr2Config .ePrescaler = ADI_TMR_PRESCALER_256 ; // TMR2 at 26MHz/256
289
- tmr2Config .eClockSource = ADI_TMR_CLOCK_PCLK ; // TMR source is PCLK (most examples use HFOSC)
290
- tmr2Config .nLoad = 0 ;
291
- tmr2Config .nAsyncLoad = 0 ;
292
- tmr2Config .bReloading = false;
293
- tmr2Config .bSyncBypass = true; // Allow x1 prescale
294
- adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmr2Config );
288
+ tmrConfig .bCountingUp = true;
289
+ tmrConfig .bPeriodic = true;
290
+ tmrConfig .ePrescaler = ADI_TMR_PRESCALER_256 ; // TMR2 at 26MHz/256
291
+ tmrConfig .eClockSource = ADI_TMR_CLOCK_PCLK ; // TMR source is PCLK (most examples use HFOSC)
292
+ tmrConfig .nLoad = 0 ;
293
+ tmrConfig .nAsyncLoad = 0 ;
294
+ tmrConfig .bReloading = false;
295
+ tmrConfig .bSyncBypass = true; // Allow x1 prescale
296
+ adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , & tmrConfig );
295
297
296
298
297
299
/*------------------------- GP TIMER ENABLE ------------------------------*/
@@ -328,14 +330,15 @@ void us_ticker_clear_interrupt(void)
328
330
329
331
void us_ticker_set_interrupt (timestamp_t timestamp )
330
332
{
331
-
333
+ // if timestamp is already past, do not set interrupt
334
+ if ((timestamp + 10 ) <= us_ticker_read ()) return ;
332
335
/* timestamp is when interrupt should fire.
333
336
*
334
337
* This MUST not be called if another timer event is currently enabled.
335
338
*
336
339
*/
337
340
calc_event_counts (timestamp ); // use timestamp to calculate largecnt to control number of timer interrupts
338
- tmr2Config .ePrescaler = ADI_TMR_PRESCALER_256 ; // TMR2 at 26MHz/256
341
+ tmrConfig .ePrescaler = ADI_TMR_PRESCALER_256 ; // TMR2 at 26MHz/256
339
342
event_timer (); // uses largecnt to initiate timer interrupts
340
343
}
341
344
@@ -348,7 +351,7 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
348
351
void us_ticker_fire_interrupt (void )
349
352
{
350
353
largecnt = 1 ; // set a minimal interval so interrupt fire immediately
351
- tmr2Config .ePrescaler = ADI_TMR_PRESCALER_1 ; // TMR2 at 26MHz/1
354
+ tmrConfig .ePrescaler = ADI_TMR_PRESCALER_1 ; // TMR2 at 26MHz/1
352
355
event_timer (); // enable the timer and interrupt
353
356
}
354
357
0 commit comments