HiResTimer cause stopwatch fail

Discussion to talk about software related topics only.
Post Reply
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

HiResTimer cause stopwatch fail

Post by RebootExpert »

using NANO and 3.3.9, I got legacy code in 2.x from my colleague. I ported the project into 3.x. when I try use the stopwatch, the elapsed time always return as 0. When I comment out the HiResTimer Init function, the stopwatch work properly.

Here's the simple project I build to test

Code: Select all

#include <predef.h>
#include <stdio.h>
#include <nbrtos.h>
#include <http.h>
#include <init.h>
#include <netinterface.h>
#include <HiResTimer.h>
#include <stopwatch.h>
#include <serial.h>
const char * AppName="hrt";

void UserMain(void * pd)
{
    init();
    GetInterfaceBlock()->discovery_server = "discover.netburner.com";
    WaitForActiveNetwork(TICKS_PER_SECOND * 5);   // Wait up to 5 seconds for active network activity 
    StartHttp();

    SerialClose( 0 );
    int uart0 = SimpleOpenSerial( 0, 115200 );

    ReplaceStdio( 0, uart0 ); 
    ReplaceStdio( 1, uart0 );
    ReplaceStdio( 2, uart0 );

    iprintf("Application %s started\n",AppName );
    HiResTimer *hrt = HiResTimer::getHiResTimer(0);
//    hrt->init();  // thise cause stopwatch return 0 elapsed time

    StopWatch myStopwatch1;
    volatile uint32_t count = 0;
    const int numLoop = 10000;

    while (1)
    {
        myStopwatch1.Start();
        for (int i = 0; i < numLoop; i++)
        {
            count++;
        }
        myStopwatch1.Stop();

        unsigned long long elapsedTime1 = myStopwatch1.GetTime();
        printf("StopWatch1 Elapsed time: %llu, %7.3f microseconds\r\n", elapsedTime1, myStopwatch1.Convert(elapsedTime1)*1000000);

        OSTimeDly(TICKS_PER_SECOND);
    }
}

User avatar
TomNB
Posts: 538
Joined: Tue May 10, 2016 8:22 am

Re: HiResTimer cause stopwatch fail

Post by TomNB »

Instead of porting from 2.x, have you tried the example in 3.x: \nburn\examples\timers\Stopwatch?
Can you try that and let us know if the example works properly?
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

Re: HiResTimer cause stopwatch fail

Post by RebootExpert »

yes it did. In fact, the example is what I start with the stopwatch. However like in my test project, if I have the follow two lines of code incorporated into the example, the stopwatch will return 0 elapsed time.
#include <HiResTimer.h>
...
HiResTimer *hrt = HiResTimer::getHiResTimer(0);
hrt->init(); // thise cause stopwatch return 0 elapsed time
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: HiResTimer cause stopwatch fail

Post by pbreed »

I think your HiResTimer is stepping on the stopwatch timer.
stopwatch looks qand sees timer 0 is unused so grabs that one....
Then

HiResTimer *hrt = HiResTimer::getHiResTimer(0); //<--Specifies Timer 0.....
Try:
HiResTimer *hrt = HiResTimer::getHiResTimer(DEFAULT_TIMER); //Should pick an unused timer....

Or Just remove the parameter...

HiResTimer *hrt = HiResTimer::getHiResTimer(); //Defaults to DEFAULT_TIMER

If this does not fix it I'll spend some time one.
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

Re: HiResTimer cause stopwatch fail

Post by RebootExpert »

I test it out, it still return as 0. The stopwatch only works when I specify the parameter to 1 or greater.
I don't use the HiResTimer at all, it inherits from my coworker's 2.x source code. I can specify it to 1, and my code still runs.
It's just something alert me that I need to fix.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: HiResTimer cause stopwatch fail

Post by pbreed »

The stopwatch and hirestimer use the same set of timers.
DEFAULT_TIMER is supposed to make the code choose an unused timer.
(Not sure why it isn't I'll look at that on Wedensday)

Your fix Putting a 1 in also works as it seems the stopwatch is using timer 0, so hires uses timer 1. ....

Please confirm you are using HiResTimer and Stopwatch from the 3.0 ,library code and not code pulled in from the port?
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: HiResTimer cause stopwatch fail

Post by pbreed »

So I verified this is broken in the current release....

The short answer:
You should not be using HiResTimer. in 3.X apps.
You should using
HiResDelay.h //For microsecond precise delays.
IntervalTimer.h //For repeat timers from 2 to 50Khz.
StopWatch.h //For precise time measuremnts.

Long answer:
HiResTimer is a direct transplant from 2.x to 3.x it ONLY works with the coldfire platforms..... MOD5441X, NANO, SB800EX.
It DOES NOT WORK with the arm M7 parts, will not be supported in any future platform(s).

The three timer headers/functions listed above are supported on ALL 3.x platforms, and will be supported for all future 3.X hardware.
(We have a couple new platforms in Alpha/early Beta.)

If you use these functions your code is guaranteed to be more portable to new platforms.

If you want help porting a 2.X app that used HiResTimer to 3.X please submit a support request...
RebootExpert
Posts: 78
Joined: Fri Oct 09, 2020 2:57 pm

Re: HiResTimer cause stopwatch fail

Post by RebootExpert »

Thanks for the guidance. I will try to replace it with HiResDelay
Post Reply