
unsigned long get_tbus (void) { unsigned long tbl=0; unsigned long tbu1=0, tbu2=0; unsigned long us=0; unsigned long long tmp=0;
unsigned long tbclk = os_get_tbclk(); // get the timebase ticks do { __asm__ __volatile__ ("mftbu %0":"=r" (tbu1):); __asm__ __volatile__ ("mftb %0":"=r" (tbl):); __asm__ __volatile__ ("mftbu %0":"=r" (tbu2):); } while (tbu1 != tbu2);
// convert ticks to ms tmp = (unsigned long long)(tbu1); tmp = (tmp << 32); tmp += (unsigned long long)(tbl); us = tmp / (tbclk / 1000000); return us; }
void main() { unsigned long uiTBusB,s_uiTBusA; while(1) { s_uiTBusA = uiTBusB; uiTBusB = (get_tbus() / 1000);
printf("uiTBusB:%d,%u\n",uiTBusB,uiTBusB); sleep(100); }
} Different performance results, the code is as follows: My platform is mpc8313, core is the e300, running 30 minutes later when there will be: uiTBusB overflow occurred. get_tbus () = 0x8001A69B, but uiTBusB = 4292819925 but modify the function as follows: unsigned long get_tbus (void) { unsigned long us =0x8001A69B return us; }
Didn't read this carefully but figured I should make one comment, overflow handling for signed int's are undefined in C and gcc uses that when optimizing. Use unsigned int's instead.
Jocke