Get TickCount from RLP

I did not find anything in RLP to measure elapsed time.
s there any way?

I think timer0 is running at 100ns ticks or you can use any other timer to your needs

Hi Gus,

I’ve found the LPC23xx.h header from NXP.
Is T0TC what I am looking for? Can you give me a hint.

#define TMR0_BASE_ADDR 0xE0004000
#define T0TC (*(volatile unsigned long *)(TMR0_BASE_ADDR + 0x08))
…
long start = T0TC;

Thx
Mark

Sounds about right.

Strange, the code below returns 1800 for 100micros, 3600 for 200micros, …So the timer is incrementing 18 times per microsecond.

But I cannot find out how to read this from the timer, both the prescale register (T0PR) and prescale counter (T0PC) are 0.

Any ideas what I am doing wrong?


int GetTimer0CounterDiffAfterDelay100(unsigned int *generalArray, void **args, unsigned int argsCount, unsigned int *argSize)
{
	long start = T0TC;
	
	RLPext->Delay(100);

	long dur = (T0TC - start) / TDIV;
	return (int)dur;
}

int GetTimer0PrescaleRegister(unsigned int *generalArray, void **args, unsigned int argsCount, unsigned int *argSize)
{
	return T0PR;
}

int GetTimer0PrescaleCounter(unsigned int *generalArray, void **args, unsigned int argsCount, unsigned int *argSize)
{
	return T0PC;
}

Are you sure long is 32bits long?.
It depends on your compiler. Try unsigned int.

Sounds right since the clock is 18mhz

Nice. Where do I get this info from?
I looked at the Panda and USBizi manuals but could not find mention of the clock running at 18mhz.

Thx
mark

There is no such public info as you are not suppose to be sharing resources with the GHI firmware but in your case, you can cheat and read the timer…now, what is the timer tick? You can easily reverse engineer that and this is exactly what you did. You can now measure time in RLP!

Thx Gus.

Btw, I just found out that I can get the clock using
Cpu.SlowClock
or
Cpu.SystemClock
Both return 18mhz.

Does the chip always run at 4x the rate of “external” clock?

It is 72Mhz.
Peripherals (like timer) run at 1/4 speed.

These functions may be helpful:


include <lpc23xx.h>
typedef int bool;

 #define TIMERFREQ 18000000
 #define TICKS_PER_US 18
 #define USEC(x) (TICKS_PER_US * x)

bool expired(unsigned long expireTime)
{
	int diff =(expireTime - T0TC);
	return (diff < 0);
}

void delay_until(unsigned long expireTime)
{
	while (!expired(expireTime)) ;
}

test_entry_point()
{
   unsigned long expiryTime = T0TC + USEC(38000);	// stash expiry time. 38msec from now
   while (!expired(expiryTime))
   { 
            // examine hardware for up to 38msec.
   }
}

Possible to share on code.tinyclr.com?

Check if it works on rollover of timer.

Pablo, note the use of unsigned and signed values above.

let’s assume everything is only 1 byte, so min value, 0, max value FF

expiryTime = FE, current Time = FF

FE - FF = FF unsigned = -1 signed, therefore expired.

expiryTime = FF, current Time = 00
FF - 00 = FF unsigned = -1 signed, therefore expired.

The function does not work if more than half the rollover time has elapsed.

The following works even on rollover. At least it is supposed to work.

void delay_until(unsigned int ms)
{
   unsigned long lastTime = TOC;
   unsigned long timeout = USEC(ms);

   while ((TOC-lastTime) < timeout);
}

Some comments to TinyCLR code, HOW TO IMPLEMENT A TIMEOUT IN RLP", meant as constructive critic.

My understanding is that throughout this discussion, we learned that timer0 is using no prescaler but instead running at “full” clock speed.This shows that the timer in NOT running at 100ns intervals. The description under TinyCLR code is a bit misleading then.

2^32 / 18000000 is not 283 but aprox 238.

RLP has a built in delay function, that works at microsecond resolution. So a custom one with milisec resolution is not really needed.

I started the original discussion to find out how to measure elapsed time in RLP. This is a feature missing and it is quite amusing to see how fast these CPUs are when programed in C vs C#. By comparing time elapsed in C (RLP) vs time for the managed-native RLP method call to finish we also see the enormous overhead the current RLP implementation brings with it.

We also saw how to use C# to find out how fast that external clock is - this can be passed to RLP in some init method instead of hardcoding. It would be nice to hear feedback from some ChipworkX user if this works on that platform.

Cheers
mark

1 Like