1

I have a PIC32MX460F512L running RTOS and I am looking for a way to get the difference in ms between two points in code.

This code below gets the tick time but not the time in ms.

static unsigned long gMSTP_timer_tick = 0 ;
void MSTP_timer_reset() {
    gMSTP_timer_tick = xTaskGetTickCount( ) ;
}
FLOAT32 MSTP_timer_differences() {
    unsigned long differences = xTaskGetTickCount( ) - gMSTP_timer_tick ;
    gMSTP_timer_tick += differences ;

    return (FLOAT32) differences ;
}

My question is

In free RTOS is there a way to get the current relative time in ms?

Étienne
  • 4,773
  • 2
  • 33
  • 58
Steven Smethurst
  • 4,495
  • 15
  • 55
  • 92

3 Answers3

1

According to this related question, there is a configTICK_RATE_HZ value.

Using this value it should only be a little bit of simple math to determine how many milliseconds a number of ticks corresponds to. Something like:

return (FLOAT32) (differences / configTICK_RATE_HZ / 1000);

There is also a set of timer APIs you might want to look into that supports callbacks into your code on timed intervals. No idea if it would suit your needs or not, but maybe it would be worth a look:

http://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • configTICK_RATE_HZ == 1000, So one tick is worth one ms? my tests show otherwise. – Steven Smethurst Nov 22 '11 at 23:50
  • @Stevensmethurst: Like on Family Guy where he says "I just know that one sentence, and this one sentence explaining that one", I don't know FreeRTOS at all. I just looked up docs/looked for existing answers that might have a tick count constant so you could do the math. It sounds wrong and buggy/misconfigured if your tick count isn't matching the way the constants are configured tho. If it is expected behavior, I'd look into other APIs. Maybe those timer functions... – Merlyn Morgan-Graham Nov 23 '11 at 00:27
1

Ticks should have a set frequency. 1000Hz tick -> interrupt and task switch triggered every 1 ms. It won't be exactly that, especially if you have other interrupts. But it should keep that frequency.

I believe you should be able to compare two tick counts and divide by tick rate to end up with the delay.

Another classic trick would be to directly toggle a GPIO pin at the start of a timed interval and again at the end (repeatedly) and then use an oscilloscope to capture the interval. That should give a very precise real-time result.

You might also ask the FreeRTOS list.

XTL
  • 851
  • 1
  • 8
  • 23
  • 2
    It looks like the timer is off by as much as 5-8ms at any given time. after looking in to it in more detail I found that it was because of another interrupt. – Steven Smethurst Nov 23 '11 at 18:02
0

FreeRtos use timer 1 in pic32 port, you can find timer1 register configure in port.c file and calculated exact tick rate time base config_TICK_RATE_HZ in milisecond. and multiply in deferences tick value. Usually base config_TICK_RATE_HZ: 1000HZ~1MS 100HZ~10MS

Iman.B
  • 68
  • 9