Arduino's millis(), micros() equivalent in chibios
Moderator: tfAteba
Arduino's millis(), micros() equivalent in chibios
How can I get a millis() and micros() of arduino in chibios?
Thanks,
Wagner Sartori Junior
Thanks,
Wagner Sartori Junior
Re: Arduino's millis(), micros() equivalent in chibios
trunet wrote:How can I get a millis() and micros() of arduino in chibios?
found:
Code: Select all
millis = MS2ST(chTimeNow());- 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
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
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
Re: Arduino's millis(), micros() equivalent in chibios
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
Is there any method to access high resolution time counter (cpu tick ?) to get elapsed time with better accuracy ?
Alexandre
- 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
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
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
Re: Arduino's millis(), micros() equivalent in chibios
I was able to get frequency this way inside a function that needs this to perform PID calculations:
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()?
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()?
Re: Arduino's millis(), micros() equivalent in chibios
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.
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.
Re: Arduino's millis(), micros() equivalent in chibios
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.
- 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
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
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
Re: Arduino's millis(), micros() equivalent in chibios
I definitely couldn't make it using HAL.
I have to measure frequency of a piece of code, maybe an example will be great.
I have to measure frequency of a piece of code, maybe an example will be great.