4

I'm using http://www.freertos.org/ for an application, but I can't find how to get the system time since boot. I can create a task and keep updating a counter, but I don't think it will be a good thing since the scheduler might schedule down my task (and putting a higher priority might hang my 'real' work tasks).

So, I want to know what might be the best solution to get how many ms elapsed since the system boot up.

Alexandre Leites
  • 388
  • 1
  • 4
  • 15

3 Answers3

9

I think xTaskGetTickCount() does what you want: http://www.freertos.org/a00021.html#xTaskGetTickCount

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Richard
  • 3,081
  • 11
  • 9
  • 1
    What's wrong with using the time() or clock() from time.h? (I'm not arguing, but asking...) In my generated code, xTaskGetTickCount() always returns 0. Does that mean I missed a setting when running STM32CubeMX? – Paul Williams May 26 '16 at 04:49
  • You may need to enable this function first in the config file to get a valid value out of this. – nchaud Oct 02 '16 at 12:14
  • Take care as the tick count will overflow. – dns13 Jun 16 '23 at 10:22
1

xTaskGetTickCount() is common way, but if you need to have the exact time since boot (for example, uptime, as Tick Counter is unsigned 32-bit integer, thus its overflow is possible), please consider using RTC time, memorizing the start point and return the diff between RTC actual time and start point

D0ct0rD
  • 11
  • 1
0

I personally recommend using clock() from time.h, in conjunction with CLOCKS_PER_SEC to keep things as portable as possible.

(credit Paul Williams' comment above)

Chris
  • 39,719
  • 45
  • 189
  • 235
  • 1
    You need to make sure that `clock()` returns what you expect on the system in question. In principle, `clock()` returns approximate CPU time used by the current process; system time since boot is in general something entirely different. – Jeremy May 22 '19 at 12:28