Skip to content

Commit 086cc2b

Browse files
committed
fixed multi comparator
1 parent 26d2116 commit 086cc2b

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

ARM/Nordic/src/timer_hf_nrfx.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5151
#define NRF_CLOCK NRF_CLOCK_NS
5252
#endif
5353

54-
#define INTERRUPT_LATENCY 11
55-
5654
static TimerHFnRFx *s_pnRFxTimer[TIMER_NRFX_HF_MAX] = {
5755
NULL,
5856
};
@@ -61,10 +59,24 @@ void TimerHFnRFx::IRQHandler()
6159
{
6260
uint32_t evt = 0;
6361
uint32_t count;
62+
uint32_t t = vpReg->CC[0]; // Preserve comparator
6463

64+
// Read counter
6565
vpReg->TASKS_CAPTURE[0] = 1;
6666
count = vpReg->CC[0];
6767

68+
// Restore comparator
69+
vpReg->CC[0] = t;
70+
71+
if (count < vLastCount)
72+
{
73+
// Counter wrap arround
74+
evt |= TIMER_EVT_COUNTER_OVR;
75+
vRollover += vFreq;
76+
}
77+
78+
vLastCount = count;
79+
6880
for (int i = 0; i < vMaxNbTrigEvt; i++)
6981
{
7082
if (vpReg->EVENTS_COMPARE[i])
@@ -73,7 +85,7 @@ void TimerHFnRFx::IRQHandler()
7385
vpReg->EVENTS_COMPARE[i] = 0;
7486
if (vTrigger[i].Type == TIMER_TRIG_TYPE_CONTINUOUS)
7587
{
76-
vpReg->CC[i] = count + vCC[i] - INTERRUPT_LATENCY;
88+
vpReg->CC[i] = count + vCC[i];
7789
}
7890
if (vTrigger[i].Handler)
7991
{
@@ -83,15 +95,6 @@ void TimerHFnRFx::IRQHandler()
8395

8496
}
8597

86-
if (count < vLastCount)
87-
{
88-
// Counter wrap arround
89-
evt |= TIMER_EVT_COUNTER_OVR;
90-
vRollover += vFreq;
91-
}
92-
93-
vLastCount = count;
94-
9598
if (vEvtHandler)
9699
{
97100
vEvtHandler(this, evt);
@@ -353,11 +356,9 @@ uint32_t TimerHFnRFx::Frequency(uint32_t Freq)
353356
if (Freq > 0)
354357
{
355358
uint32_t divisor = TIMER_NRFX_HF_BASE_FREQ / Freq;
356-
#ifdef __ICCARM__
359+
357360
prescaler = 31 - __CLZ(divisor);
358-
#else
359-
prescaler = 31 - __builtin_clzl(divisor);
360-
#endif
361+
361362
if (prescaler > 9)
362363
{
363364
prescaler = 9;
@@ -369,8 +370,7 @@ uint32_t TimerHFnRFx::Frequency(uint32_t Freq)
369370
vFreq = TIMER_NRFX_HF_BASE_FREQ / (1 << prescaler);
370371

371372
// Pre-calculate periods for faster timer counter to time conversion use later
372-
// for precision this value is x10 (in 100 psec)
373-
vnsPeriod = 10000000000ULL / vFreq; // Period in x10 nsec
373+
vnsPeriod = 1000000000ULL / vFreq; // Period in nsec
374374

375375
vpReg->TASKS_START = 1;
376376

@@ -379,20 +379,21 @@ uint32_t TimerHFnRFx::Frequency(uint32_t Freq)
379379

380380
uint64_t TimerHFnRFx::TickCount()
381381
{
382-
if (vpReg->INTENSET == 0)
383-
{
384-
vpReg->TASKS_CAPTURE[vDevNo] = 1;
382+
uint32_t t = vpReg->CC[0]; // Save comparator
385383

386-
uint32_t count = vpReg->CC[vDevNo];
384+
// Read counter
385+
vpReg->TASKS_CAPTURE[0] = 1;
386+
uint32_t count = vpReg->CC[0];
387387

388-
if (count < vLastCount)
389-
{
390-
// Counter wrap arround
391-
vRollover += 0x100000000ULL;//vFreq;
392-
}
388+
vpReg->CC[0] = t; // Restore comparator
393389

394-
vLastCount = count;
395-
}
390+
if (count < vLastCount)
391+
{
392+
// Counter wrap arround
393+
vRollover += 0x100000000ULL;;
394+
}
395+
396+
vLastCount = count;
396397

397398
return (uint64_t)vLastCount + vRollover;
398399
}
@@ -403,8 +404,7 @@ uint64_t TimerHFnRFx::EnableTimerTrigger(int TrigNo, uint64_t nsPeriod, TIMER_TR
403404
if (TrigNo < 0 || TrigNo >= vMaxNbTrigEvt)
404405
return 0;
405406

406-
// vnsPerios is x10 nsec (100 psec) => nsPeriod * 10ULL
407-
uint32_t cc = (nsPeriod * 10ULL + (vnsPeriod >> 1)) / vnsPeriod;
407+
uint32_t cc = (nsPeriod + (vnsPeriod >> 1)) / vnsPeriod;
408408

409409
if (cc <= 0)
410410
{
@@ -419,21 +419,21 @@ uint64_t TimerHFnRFx::EnableTimerTrigger(int TrigNo, uint64_t nsPeriod, TIMER_TR
419419

420420
vpReg->INTENSET = TIMER_INTENSET_COMPARE0_Msk << TrigNo;
421421

422-
vpReg->CC[TrigNo] = count + cc - INTERRUPT_LATENCY;
422+
vpReg->CC[TrigNo] = count + cc;
423423

424424
if (count < vLastCount)
425425
{
426426
// Counter wrap around
427-
vRollover += 0x100000000ULL;//vFreq;
427+
vRollover += 0x100000000ULL;;
428428
}
429429

430430
vLastCount = count;
431431

432-
vTrigger[TrigNo].nsPeriod = vnsPeriod * (uint64_t)cc / 10ULL;
432+
vTrigger[TrigNo].nsPeriod = vnsPeriod * (uint64_t)cc;
433433
vTrigger[TrigNo].Handler = Handler;
434434
vTrigger[TrigNo].pContext = pContext;
435435

436-
return vnsPeriod * (uint64_t)cc / 10ULL; // Return real period in nsec
436+
return vnsPeriod * (uint64_t)cc; // Return real period in nsec
437437
}
438438
/*
439439
uint32_t TimerHFnRFx::EnableTimerTrigger(int TrigNo, uint32_t msPeriod, TIMER_TRIG_TYPE Type,

0 commit comments

Comments
 (0)