How can I get the CPU tick count on an MPC5744P?

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get the CPU tick count on an MPC5744P?

751 Views
igormodino
Contributor III

Dear all.

We are willing to implement time-based timeout functions and would need a consistent time reference for that. Our approach is to use the tick counter (TBR register). From an NXP document (https://www.nxp.com/docs/en/reference-manual/MPCFPE32B.pdf ), we've found that the code for doing that should be:

loop:

   mftbu rx #load from TBU

   mftb ry #load from TBL

   mftbu rz #load from TBU

   cmpw rz,rx #see if ‘old’ = ‘new’

   bne loop #loop if carry occurred

We've seem examples from other sources that are exactly the same, so our implementation code we use is as follows:

static uint64_t UTILS_tickCountGet (void) {
   uint64_t result=0;
   uint32_t upper, lower,tmp;
   asm(
      "0: \n\t"
      "mftbu %0 \n\t"
      "mftb %1 \n\t"
      "mftbu %2 \n\t"
      "cmpw %2,%0 \n\t"
      "e_bne 0b \n\t"
      : "=r"(upper),"=r"(lower),"=r"(tmp)
   );
   result = upper;
   result = result<<32;
   result = result|lower;

   return(result);
}

However, when we call this functions, the program halts as an non recoverable exception seems to be triggered.

We've tried removing code, but any "mftb" or "mftbu" instruction seems to trigger it. We've also tried using registers rather than memory references and the result is the same.

May it be that these instructions are not present in the MPC5744P? (should raise a compile time issue, though).

Anyway, how will be the right way to get the CPU tick count?

Thanks

Tags (1)
5 Replies

206 Views
aero72
Contributor III

Agree with David - have had good experience using the PIT as a master scheduler for real-time operating systems and other timed functions. Easy to set up and use.

206 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

Hi, core timers (time-base, decrementer, fixed interval timer and also core watchdog) are no longer present with all core variants used with MPC57xx (excluding MPC5777C).

You can use either some system timer (STM, PIT,...) or use core's Performance Monitor.

206 Views
igormodino
Contributor III

Thanks David, however I think that does not match our requirements perfectly, although can be tweaked. Let me explain a bit more.

Our idea is to have an arbitrary number of timeouts that are initialised by the software using it and then checked periodically to see if it has expired. We'll use it, for example, for breaking a while loop when waiting for a flag that may never be set due to an issue.

We want this mechanism to work both with OS and without OS and in could happen that we have several threads using timeouts at the same time (quite unlikely but possible).

Additionally, PIT seems useful for implementing a delay, but not so sure how useful is it for timeouts, unless the expiration is checked in the ISR.

On our approach, though, having a fixed time reference (number of ticks since the MCU was powered on) allows us to initialise an "unlimited" number of timeouts (just take the current count and calculate the count when it will be expired based on the MCU frequency) and check them by the software using it at it's own pace.

It's also convenient having the tick counter in 64bit as in 32 it's quite easy to overflow it.

Anyway, the main point is to confirm if the counter is available and otherwise, as it seems is the case, find an alternative way to do it :smileyhappy:

Thanks!

0 Kudos

206 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

I see.

It seems really PIT is the right choice for this purpose as you can concatenate two timers to built one 64-bit.

pastedImage_2.png

I can confirm there is no other 64-bit timer option on this device.

206 Views
igormodino
Contributor III

Thanks for the heads up; it looks like a feasible solution indeed!

Thanks.

0 Kudos