Arduino's millis(), micros() equivalent in chibios

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Arduino's millis(), micros() equivalent in chibios

Post by trunet »

How can I get a millis() and micros() of arduino in chibios?

Thanks,

Wagner Sartori Junior
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: Arduino's millis(), micros() equivalent in chibios

Post by trunet »

trunet wrote:How can I get a millis() and micros() of arduino in chibios?

found:

Code: Select all

millis = MS2ST(chTimeNow());
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Arduino's millis(), micros() equivalent in chibios

Post by Giovanni »

Hi,

chTimeNow() returns the number of system ticks since system start (and note, it can wrap to zero and restart) to you have to convert it to milliseconds or microseconds. MS2ST() does the opposite. You can use CH_FREQUENCY for the conversion.

Giovanni
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

Re: Arduino's millis(), micros() equivalent in chibios

Post by alex31 »

chTimeNow() return the number of chibios ticks, with low 1ms resolution.
Is there any method to access high resolution time counter (cpu tick ?) to get elapsed time with better accuracy ?

Alexandre
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Arduino's millis(), micros() equivalent in chibios

Post by Giovanni »

Sure,

halGetCounterValue() <- returns the free running counter
halGetCounterFrequency()
halIsCounterWithin()
halPolledDelay()

This is clock cycle accurate, the module also has other macros to handle time conversions, also look into the "tm" Time Measurement unit if you need to measure periods of time in high resolution.

Giovanni
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: Arduino's millis(), micros() equivalent in chibios

Post by trunet »

I was able to get frequency this way inside a function that needs this to perform PID calculations:

Code: Select all

now = chTimeNow();
sampleFreq = 1.0 / ((now-lastupdate) / (CH_FREQUENCY / 1.0));
lastupdate = now;


However my code is running so fast that I'm getting always 1000Hz or 500Hz(only 1 millisecond difference).

How can I achieve microsecond resolution to get frequency using halGetCounterValue()?
mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Been thanked: 1 time

Re: Arduino's millis(), micros() equivalent in chibios

Post by mabl »

Hi trunet,

there are literally hundreds of possibilities. Just play around a bit. You could use halGetCounterValue/halGetCounterFrequency (There are examples in the forum). You could increase your frequency measurement period. You could use hardware timers with events.

It all strongly depends on how much accuracy, jitter etc. you want. If you want your PID to run as fast as possible, but in unpredictable intervals - or in fixed intervals. Just tinker a bit with different possibilities.
manton
Posts: 6
Joined: Thu May 24, 2012 10:18 pm

Re: Arduino's millis(), micros() equivalent in chibios

Post by manton »

trunet wrote:I was able to get frequency this way inside a function that needs this to perform PID calculations:

Code: Select all

now = chTimeNow();
sampleFreq = 1.0 / ((now-lastupdate) / (CH_FREQUENCY / 1.0));
lastupdate = now;


However my code is running so fast that I'm getting always 1000Hz or 500Hz(only 1 millisecond difference).

How can I achieve microsecond resolution to get frequency using halGetCounterValue()?


Maybe you CH_FREQUENCY is set at 1000 so that it gives 1ms resolution. I have tried 100000 to get 10us tick.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Arduino's millis(), micros() equivalent in chibios

Post by Giovanni »

Hi,

10uS is definitely possible on fast CPUs using ChibiOS but it is still going to take a toll in terms of CPU/power usage. For accurate timings the RT counter in the HAL is often a better choice. Use of the GPT driver is also an option.

Giovanni
trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: Arduino's millis(), micros() equivalent in chibios

Post by trunet »

I definitely couldn't make it using HAL.

I have to measure frequency of a piece of code, maybe an example will be great.
Post Reply