Time measurement unit.

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Time measurement unit.

Post by barthess »

I am in doubt, how to use it in proper way. There is no exported variable for time measurement driver (like for USART, etc.) and void parameter passes to tmInit() function, so I can not pass to it pointer to my own TimeMeasurement structure. I added exported variable TMU1 to tm.h and tm.c and check the driver against hardware counter - results are equal.

An I right? If so than I will commit my changes to trunk.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Time measurement unit.

Post by Giovanni »

Hi,

The TMU is not a normal driver, you can have as many instances as you want, in practice you can make multiple measurements at the same time so there is no need to have a predefined one, it is the user that decides how many of those to allocate.

It is like the MMC_SPI, it does not defines fixed instances because it is not tied to a specific HW cell.

About the conversions you added, it is OK but right now the conversions truncate down to the next lower integer, should those round the result up to the next higher integer instead?

Giovanni
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Time measurement unit.

Post by barthess »

Giovanni wrote:you can have as many instances as you want

In that case we must change

Code: Select all

void tmInit(void)
to

Code: Select all

void tmInit(TimeMeasurement *tm)
to allow creation and initialization of driver from userspace code. Ore there is different way to create measurement structure?

Giovanni wrote:should those round the result up to the next higher integer instead?

I think no. Because current implementation behaves like a regular hardware counter - it is most expected by user.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Time measurement unit.

Post by Giovanni »

I think no. Because current implementation behaves like a regular hardware counter - it is most expected by user.


Good point.

About the TM, tmInit() initializes the subsystem and performs a calibration that is required only once (it is invoked from halInit()). What you suggest is done by tmObjectInit() which initializes a measurement instance. Probably I should add examples of this, it is not used anywhere yet.

Giovanni
manton
Posts: 6
Joined: Thu May 24, 2012 10:18 pm

Re: Time measurement unit.

Post by manton »

Hi,

Do you have any code snippets to show how to use this time measurement driver?

Thanks.
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Time measurement unit.

Post by barthess »

It is very simple:
define measurement object

Code: Select all

static TimeMeasurement itg3200_tmup;

init it

Code: Select all

tmObjectInit(&itg3200_tmup);

than perform start and stop:

Code: Select all

tmStartMeasurement(&itg3200_tmup);
do some work here
tmStopMeasurement(&itg3200_tmup);
imu_update_period = RTT2US(itg3200_tmup.last);
manton
Posts: 6
Joined: Thu May 24, 2012 10:18 pm

Re: Time measurement unit.

Post by manton »

Wow ! It is simple as you said.
Thank you very much for your help.
Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

Re: Time measurement unit.

Post by Jevermeister »

Hi, i'm trying to measure the time needed to complete one task loop.

The task is activated every ~5ms by an external interrupt which releases a semaphore.
code is like this:

Code: Select all

while (TRUE) {
        tmStartMeasurement(&TimeMonitor);
         chBSemWait(&semaphore);
        doStuff();
        chThdSleepMicroseconds(5000);
        tmStopMeasurement(&TimeMonitor);
}


but the code execution time won't change if i let the thread sleep until i set it to ~9000 us which causes a FIFO buffer to overflow and add a huge delay. After passing the "magic" sleep of 9000 us every thread sleep increase is added directly to the execution time of the loop.

My CH_FREQUENCY is set to 10000 on a STM32F407 so it should be able to measure up to 100us precision. Why is the measured execution time of the loop not increased with values lower than 9000?

This is used as a PID controller loop and the thread sleep seems to have no influence on the behaviour until i pass the 9000 mark, then it behaves totally unstable.

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

Re: Time measurement unit.

Post by Giovanni »

Hi,

Is the TimeMonitor object initialized using tmObjectInit()? note the measurement has one CPU clock cycle resolution, it is not related to CH_FREQUENCY.
The counter is 32bits wide so watch for overflows, the maximum period is 2^32 / CPUCLK.

Giovanni
Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

Re: Time measurement unit.

Post by Jevermeister »

It is initialised. The task takes about 6ms to complete and i want to measure up to 20ms. So this should be enough?
With a thread sleep of less than 9000 us, it always takes ~9900us to complete.
With a thread sleep of more than 9000 us, it takes ~9900us + x to complete, x is roughly threadsleep - 9000.
Post Reply