unable to link to gettimeofday on embedded system, elapsed time suggestions?

12,493

Solution 1

What you need to do is create your own _gettimeofday() function to get it to link properly. This function could use the appropriate code to get the time for your processor, assuming you have a free-running system timer available.

#include <sys/time.h>

int _gettimeofday( struct timeval *tv, void *tzvp )
{
    uint64_t t = __your_system_time_function_here__();  // get uptime in nanoseconds
    tv->tv_sec = t / 1000000000;  // convert to seconds
    tv->tv_usec = ( t % 1000000000 ) / 1000;  // get remaining microseconds
    return 0;  // return non-zero for error
} // end _gettimeofday()

Solution 2

What I usually do, is to have a timer running at 1khz, so it will generate an interrupt every millisecond, in the interrupt handler I increment a global var by one, say ms_ticks then do something like:

volatile unsigned int ms_ticks = 0;

void timer_isr() { //every ms
    ms_ticks++;
}

void delay(int ms) {
    ms += ms_ticks;
    while (ms > ms_ticks)
        ;
}

It is also possible to use this as a timestamp, so let's say I want to do something every 500ms:

last_action = ms_ticks;

while (1) {  //app super loop

    if (ms_ticks - last_action >= 500) {
        last_action = ms_ticks;
        //action code here
    }

    //rest of the code
}

Another alternative, since ARMs are 32bits and your timer will probably be a 32bits one, is to instead of generating a 1khz interrupt, you leave it free running and simply use the counter as your ms_ticks.

Solution 3

Use one of the timers in the chip...

Share:
12,493
gnychis
Author by

gnychis

Just a student

Updated on August 29, 2022

Comments

  • gnychis
    gnychis over 1 year

    I am trying to use gettimeofday on an embedded ARM device, however it seems as though I am unable to use it:

    gnychis@ubuntu:~/Documents/coexisyst/econotag_firmware$ make
    Building for board: redbee-econotag
           CC obj_redbee-econotag/econotag_coexisyst_firmware.o
    LINK (romvars) econotag_coexisyst_firmware_redbee-econotag.elf
    /home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none- eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-gettimeofdayr.o): In function `_gettimeofday_r':
    gettimeofdayr.c:(.text+0x1c): undefined reference to `_gettimeofday'
    /home/gnychis/Documents/CodeSourcery/Sourcery_G++_Lite/bin/../lib/gcc/arm-none-eabi/4.3.2/../../../../arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
    sbrkr.c:(.text+0x18): undefined reference to `_sbrk'
    collect2: ld returned 1 exit status
    make[1]: *** [econotag_coexisyst_firmware_redbee-econotag.elf] Error 1
    make: *** [mc1322x-default] Error 2
    

    I am assuming I cannot use gettimeofday() ? Does anyone have any suggestions for being able to tell elapsed time? (e.g., 100ms)