How to get milliseconds since boot?

Discussion to talk about software related topics only.
Post Reply
Anguel
Posts: 30
Joined: Fri Aug 23, 2019 12:39 am

How to get milliseconds since boot?

Post by Anguel »

Hi,

I am trying to port some STM32 code that uses the millis() oder HAL_GetTick() functions to measure time intervals since startup.
Now I wonder how this is properly done with Netburner. I browsed the docs but did not find a suitable function so far.
So what is the proper way to do it?

Thanks in advance,

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

Re: How to get milliseconds since boot?

Post by TomNB »

Hello Anguel,

What version of tools are you using, and what platform?

The value you describe rolls over every 9.9 hours as a 32-bit integer. How do you handle that?
Anguel
Posts: 30
Joined: Fri Aug 23, 2019 12:39 am

Re: How to get milliseconds since boot?

Post by Anguel »

Hi Tom,

I am using your ARM module and plan to use the latest NB tools. The time periods they measure in the original code are not very long, they use it to detect timeouts when communicating with another device, but you are right, I must prevent this rollover problem somehow...

They actually seem to poll the serial port for data and watch for timeouts in the meantime. Can you maybe recommend a more elegant way to solve this when using the Netburner API?

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

Re: How to get milliseconds since boot?

Post by TomNB »

Hello,

I would start with the timer examples in \nburn\examples\timers. There is an interval timer, and a stopwatch timer. Sounds like the stopwatch would work for you.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: How to get milliseconds since boot?

Post by pbreed »

With zero additional work there are two variables
#include <nbrtos.h>
TimeTick and Secs...

TimeTick happens each tick (defaults to 20 per second)
So that gets you to 50 msec with ZERO work.


If you need better:
#include <stopwatch.h>
StopWatch my_stopwatch;
my_stopwatch.Start();


Then you can get the time count (unsigned 64 bit value)
unsigned long long now=my_stopwatch.GetTime();


You can get this time interval in a platform independant way:
double time_of_one_tick=my_stopwatch. CountResolution();

On the M7 it is basically 1 cpu cycle so if you wnat to work with integers:
On the M7 the stopwatch tick is :
1/extern volatile uint32_t CPU_CLOCK; //300Mhz
So its waaaaaaay more accurate then 1msec.


Or you can use the function StopWatch uses...without the start offset...
inside
nburn\arch\cortex-m7\cpu\SAME70\source\stopwatch.cpp
Look for GetNow...

So three ways to solve your problem...
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: How to get milliseconds since boot?

Post by pbreed »

Or go to the stopwatch file listed above and add this at the end...


unsigned long long GetMsec()
{
unsigned long long tv = GetNow();
tv/=(CPU_CLOCK/1000);
return tv;
}
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: How to get milliseconds since boot?

Post by pbreed »

Just added that to the repository so it should be in future versions of the tools.
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: How to get milliseconds since boot?

Post by pbreed »

Actually Get_msec is in future versions of the tools.
Anguel
Posts: 30
Joined: Fri Aug 23, 2019 12:39 am

Re: How to get milliseconds since boot?

Post by Anguel »

Thank you very much for all the information!
Post Reply