1
1
/*******************************************************************************
2
- * Copyright (c) 2010-2017 Analog Devices, Inc.
2
+ * Copyright (c) 2010-2018 Analog Devices, Inc.
3
3
*
4
4
* All rights reserved.
5
5
*
@@ -116,7 +116,7 @@ static uint32_t get_current_time(void)
116
116
* thereby clearing any TMR1 pend's. This have no effect if this routine is called with interrupts globally disabled.
117
117
*/
118
118
119
- NVIC_DisableIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]); // Prevent Upper_count increment
119
+ NVIC_DisableIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]); // Prevent Upper_count increment
120
120
tmrpend0 = NVIC_GetPendingIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]);
121
121
// Check if there is a pending interrupt for timer 1
122
122
@@ -128,27 +128,27 @@ static uint32_t get_current_time(void)
128
128
129
129
tmrcnt1 = adi_tmr_registers [ADI_TMR_DEVICE_GP1 ]-> CURCNT ; // read both timers manually
130
130
131
- totaltmr0 = tmrcnt0 ; // expand to u32 bits
132
- totaltmr1 = tmrcnt1 ; // expand to u32 bits
131
+ totaltmr0 = tmrcnt0 ; // expand to u32 bits
132
+ totaltmr1 = tmrcnt1 ; // expand to u32 bits
133
133
134
134
tmrcnt0 &= 0xff00u ;
135
135
tmrcnt1 <<= 8 ;
136
136
137
137
__DMB ();
138
138
139
- uc1 = * ucptr ; // Read Upper_count
139
+ uc1 = * ucptr ; // Read Upper_count
140
140
141
141
tmrpend1 = NVIC_GetPendingIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]);
142
142
// Check for a pending interrupt again. Only leave loop if they match
143
143
144
- NVIC_EnableIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]); // enable interrupt on every loop to allow TMR1 interrupt to run
144
+ NVIC_EnableIRQ (adi_tmr_interrupt [ADI_TMR_DEVICE_GP1 ]); // enable interrupt on every loop to allow TMR1 interrupt to run
145
145
} while ((tmrcnt0 != tmrcnt1 ) || (tmrpend0 != tmrpend1 ));
146
146
147
147
totaltmr1 <<= 8 ; // Timer1 runs 256x slower
148
148
totaltmr1 += totaltmr0 & 0xffu ; // Use last 8 bits of Timer0 as it runs faster
149
149
// totaltmr1 now contain 24 bits of significance
150
150
151
- if (tmrpend0 ) { // If an interrupt is pending, then increment local copy of upper count
151
+ if (tmrpend0 ) { // If an interrupt is pending, then increment local copy of upper count
152
152
uc1 ++ ;
153
153
}
154
154
@@ -158,7 +158,7 @@ static uint32_t get_current_time(void)
158
158
// Divide Uc by 26 (26MHz converted to 1MHz) todo scale for other clock freqs
159
159
160
160
Uc *= 1290555u ; // Divide total(1/26) << 25
161
- Uc >>= 25 ; // shift back. Fixed point avoid use of floating point divide.
161
+ Uc >>= 25 ; // shift back. Fixed point avoid use of floating point divide.
162
162
// Compiler does this inline using shifts and adds.
163
163
164
164
return Uc ;
@@ -205,7 +205,10 @@ static void event_timer()
205
205
adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , tmr2Config );
206
206
adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , true);
207
207
} else {
208
- us_ticker_irq_handler ();
208
+ tmr2Config .nLoad = 65535u ;
209
+ tmr2Config .nAsyncLoad = 65535u ;
210
+ adi_tmr_ConfigTimer (ADI_TMR_DEVICE_GP2 , tmr2Config );
211
+ adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , true);
209
212
}
210
213
}
211
214
@@ -229,7 +232,11 @@ static void GP2CallbackFunction(void *pCBParam, uint32_t Event, void * pArg)
229
232
230
233
if (largecnt < 65536u ) {
231
234
adi_tmr_Enable (ADI_TMR_DEVICE_GP2 , false);
232
- event_timer ();
235
+ if (largecnt ) {
236
+ event_timer ();
237
+ } else {
238
+ us_ticker_irq_handler ();
239
+ }
233
240
}
234
241
}
235
242
@@ -326,7 +333,8 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
326
333
*
327
334
*/
328
335
calc_event_counts (timestamp ); // use timestamp to calculate largecnt to control number of timer interrupts
329
- event_timer (); // uses largecnt to initiate timer interrupts
336
+ tmr2Config .ePrescaler = ADI_TMR_PRESCALER_256 ; // TMR2 at 26MHz/256
337
+ event_timer (); // uses largecnt to initiate timer interrupts
330
338
}
331
339
332
340
/** Set pending interrupt that should be fired right away.
@@ -337,7 +345,9 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
337
345
*/
338
346
void us_ticker_fire_interrupt (void )
339
347
{
340
- NVIC_SetPendingIRQ (TMR2_EVT_IRQn );
348
+ largecnt = 1 ; // set a minimal interval so interrupt fire immediately
349
+ tmr2Config .ePrescaler = ADI_TMR_PRESCALER_1 ; // TMR2 at 26MHz/1
350
+ event_timer (); // enable the timer and interrupt
341
351
}
342
352
343
353
0 commit comments